8
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Jun 5, 2013 at 15:37

2 Answers 2

3
$\begingroup$

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]", "]"}]}];

Then follow this work flow:

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 ;)

$\endgroup$
6
  • $\begingroup$ 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 :) $\endgroup$ Jun 5, 2013 at 15:19
  • $\begingroup$ @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? $\endgroup$ Jun 5, 2013 at 16:43
  • $\begingroup$ Yeah, I like that kind of stuff :). Glad to see you are enthousiastic too. $\endgroup$ Jun 5, 2013 at 17:02
  • $\begingroup$ 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]. $\endgroup$ Jun 6, 2013 at 22:35
  • $\begingroup$ @phantomas1234 yeah bounties work like that, they only stay for a week I think. Hm maybe I will take another wack at this $\endgroup$ Aug 11, 2013 at 14:54
0
$\begingroup$

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

Item[KeyEvent["u", Modifiers -> {Control}], 
    FrontEndExecute[
        FrontEndToken["ExpandSelection"];
        FrontEndToken["ExpandSelection"];
        FrontEnd`SelectionEvaluate[FrontEnd`InputNotebook[]]
    ]
]

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.