8
$\begingroup$

Imagine that in this toy code I want to evaluate just the selected (blue) expression: enter image description here

To get as output result: {09/14,09/15,09/16,09/17,09/18,09/19,09/20}

There is some shortcut to do this? I'm looking for something like Command+Shift+Enter (Mac). But I don't want it to evaluate in place, but as an output. Maybe something that I can put into KeyEventTranslations.tr

Today I have to copy the code to outside the module, or comment the rest of it. In this toy code it's a easy task, but for bigger code it's very annoying to do.

$\endgroup$
5
  • $\begingroup$ Maybe you can accept a pallete? $\endgroup$
    – Kuba
    Commented Sep 14, 2013 at 17:01
  • $\begingroup$ There have been quite a few instances where I would've really liked something like this, but I was too lazy to automate it. In interactive sessions, you might often also have a global symbol r that's the same as in the module. So just doing an evaluation in the same notebook might overwrite the global r, which is not desirable. So a possible solution might be to copy selection, create a new notebook with a context unique to the notebook, evaluate, copy result and discard notebook $\endgroup$
    – rm -rf
    Commented Sep 14, 2013 at 17:21
  • $\begingroup$ @Kuba Hi. I prefer keyboard shortcut. $\endgroup$
    – Murta
    Commented Sep 14, 2013 at 20:03
  • $\begingroup$ @Murta Did you read this post? I never bind my own shortcuts, but this looks promising. For the first part of your question I probably have a nice solution, although it's currently bound to a button. $\endgroup$
    – halirutan
    Commented Sep 14, 2013 at 20:07
  • $\begingroup$ @halirutan Interesting post. As I said in the question, a believe that change the KeyEventTranslation.tr file is one way, but I don't know handle with Cells Execution syntax. $\endgroup$
    – Murta
    Commented Sep 14, 2013 at 20:14

2 Answers 2

3
$\begingroup$

The following will almost do it (the only two problems I did not manage to solve so far are that Out values are not printed and previous output cells are not deleted - but these should be solvable):

You can use CellEventActions, to effectively override the action for SHIFT+RETURN. Here is the code for the CellEventActions:

partialEvalRule = 
   {"MenuCommand", "HandleShiftReturn"} :>
       With[{nb = InputNotebook[]},
         With[{sel = NotebookRead[nb]},
           CellPrint @
              ExpressionCell[#, "Output", CellAutoOverwrite -> True] & @
                 ToExpression  @  
                    If[sel === {},
                      SelectionMove[nb, All, Cell];
                      First[
                         FrontEndExecute[
                            FrontEnd`ExportPacket[
                               NotebookSelection[nb], 
                               "InputText"
                            ]
                         ]
                      ],
                      (* else *)
                      sel
                    ]
         ]
       ];

where I used this excellent answer of John Fultz for code extraction from the selection (actually, it might have been better to use this for both branches of the If).

Here is how you can set this on a notebook level: create a new notebook

nb  = CreateDocument[]

Set the option:

SetOptions[nb, CellEventActions -> {partialEvalRule}]

Now if you start working in that notebook, you will see that you get what you requested, apart from the two issues I mentioned above.

$\endgroup$
2
  • $\begingroup$ The Out label may be no harder than adding the option CellLabel -> ToString@"Out"[$Line] to ExpressionCell I think $\endgroup$
    – Rojo
    Commented Sep 15, 2013 at 0:48
  • $\begingroup$ @Rojo You might be right, just no time to play more with this, at the moment. $\endgroup$ Commented Sep 15, 2013 at 19:49
2
$\begingroup$

It seems that this key shortcut added to KeyEventTranslations.tr does what you need:

 Item[
  KeyEvent["t", Modifiers -> {Control}],
      FrontEndExecute[
              FrontEnd`SelectionCreateCell[FrontEnd`InputNotebook[]];
              FrontEnd`SelectionMove[FrontEnd`InputNotebook[], All, CellContents];
              FrontEnd`SelectionEvaluate[FrontEnd`InputNotebook[]];
                     ]
     ]

This will work if the result is going to be one cell output, otherwise only first cell will stay. If you want all of them then use:

SelectionMove[FrontEnd`InputNotebook[], All, **Cell**]

but doubled input cell will not be overwritten.


I was trying to deal with multiple cell output with tagging input cell and then deleting it just after evaluation but I've faced a problem.

I failed setting CellTags. Is the syntax incorrect? Any comments appreciated.

 FrontEnd`SelectionCreateCell[FrontEnd`InputNotebook[]];
 FrontEnd`SelectionMove[FrontEnd`InputNotebook[], All, Cell];
 FrontEnd`SetOptions[FrontEnd`NotebookSelection[FrontEnd`InputNotebook[]], 
                     CellTags -> "temp"];
 FrontEnd`SelectionEvaluate[FrontEnd`InputNotebook[]];
 FrontEnd`NotebookDelete[FrontEnd`Cells[FrontEnd`InputNotebook[],
                                        CellTags -> "temp", GeneratedCell -> False]];
                 ]

Those procedures do what I've decribed, when added to Pallete. For complete code go to this answer edit history.

$\endgroup$
2
  • $\begingroup$ Hi @Kuba. Your first solution worked nice for one line. $\endgroup$
    – Murta
    Commented Sep 16, 2013 at 3:19
  • $\begingroup$ @Murta Yes it does. Also for multiple lines with only one line as a resut too. Just like the case you provided. $\endgroup$
    – Kuba
    Commented Sep 16, 2013 at 9:59

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.