# Combine InputAlias with InputString to parse expressions from a string

My knowledge of low-level notebook programming is very limited so I would be very grateful for any input regarding the following issue:

I would like to define an input alias (via InputAliases) that evaluates

parse[InputString[]]


in-place (like selecting it and then performing MenuEvaluationEvaluate in Place or Cmd + Return). The function parse will translate my textual input into an expression (and insert it at the current cursor position).

Currently, I am using the following alias,

SetOptions[EvaluationNotebook[],
InputAliases->{"parse"->RowBox[{"parse","[","InputString","[","]","]"}]}]


and the workflow looks like this

1. ESC parse ESC to produce parse[InputString[]]
2. Press Ctrl + . a few times to select parse[InputString[]]
3. Evaluate in place via Cmd + Return
4. Type something into the input field and press Return

Basically, I would like to get rid of steps 2 and 3. I would appreciate any other solution if the desired behavior cannot be achieved using InputAliases. I would also be more than happy if an InputField could be used instead of InputString.

-
I would suggest setting a hotkey, like this. I don't think you can start any evaluation from entering an InputAlias, so there seems to be little else you can do. –  Jacob Akkerboom Jun 5 '13 at 15:37

Success! Please evaluate the following code in a new notebook:

 Clear[eval];
eval[cmd_] := Module[{nb},
nb = InputNotebook[];
NotebookWrite[nb, cmd, All];
SelectionMove[nb, Previous, Cell];
NotebookDelete[nb];
SelectionMove[nb, Next, Cell];
SelectionMove[nb, Next, Cell];
Null];
SetOptions[EvaluationNotebook[],
InputAliases -> {"eval" ->
RowBox[{"eval", "[", "\[SelectionPlaceholder]", "]"}]}];


1.-Press [ESC]eval[ESC]

2.-Write an operation, for example 2+2

3.-Press shift-enter

What actually happens is that a new input cell is written, and the old input cell is erased. I promise this is my last attempt ;)

-
I think in MMA 9 it is better to use EvaluationCell and a consequent SelectionMove, rather relying on SelectionMove[nb,Previous, Cell] to select the right cell. But I am afraid this is not exactly what the OP wants. Looks nice though :) –  Jacob Akkerboom Jun 5 '13 at 15:19
@JacobAkkerboom You are right, because in a long evaluation (one that takes several seconds) the end-user could click somewhere else in the document while the evaluation is runing, and then the NotebookWrite and NotebookDelete could destroy other cells, not those that were intended. I am also not very sure I did answer the question, however it is a nice example of NotebookWrite, SelectionMove, etc. isn't it? –  MaTECmatica Jun 5 '13 at 16:43
Yeah, I like that kind of stuff :). Glad to see you are enthousiastic too. –  Jacob Akkerboom Jun 5 '13 at 17:02
Ahhh, why is the bounty gone? You're solution came pretty close. The only issue that I still have is that it cannot be used to consecutively write out notation because (i) the cursor is jumping to a new cell (probably easy to change) and (ii) for example, var in var + [ESC]eval[ESC] will not lead to var + whateverExprItyped. I have a feeling that this can be fixed by using something else then Cell in SelectionMove[nb, Previous, Cell]. –  phantomas1234 Jun 6 '13 at 22:35
@phantomas1234 yeah bounties work like that, they only stay for a week I think. Hm maybe I will take another wack at this –  Jacob Akkerboom Aug 11 '13 at 14:54

You can do step 2 and 3 with one hotkey, by adding the item

Item[KeyEvent["u", Modifiers -> {Control}],
FrontEndExecute[
FrontEndToken["ExpandSelection"];
FrontEndToken["ExpandSelection"];
FrontEndSelectionEvaluate[FrontEndInputNotebook[]]
]
]


to KeyEventTranslations.tr, located at the path that is the result of

FileNames["KeyEventTranslations.tr", \$InstallationDirectory,
8] // First

-