Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Maybe you can accept a pallete? –  Kuba Sep 14 '13 at 17:01
    
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 –  rm -rf Sep 14 '13 at 17:21
    
@Kuba Hi. I prefer keyboard shortcut. –  Murta Sep 14 '13 at 20:03
    
@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. –  halirutan Sep 14 '13 at 20:07
    
@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. –  Murta Sep 14 '13 at 20:14
add comment

2 Answers

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.

share|improve this answer
    
The Out label may be no harder than adding the option CellLabel -> ToString@"Out"[$Line] to ExpressionCell I think –  Rojo Sep 15 '13 at 0:48
    
@Rojo You might be right, just no time to play more with this, at the moment. –  Leonid Shifrin Sep 15 '13 at 19:49
add comment

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.

share|improve this answer
    
Hi @Kuba. Your first solution worked nice for one line. –  Murta Sep 16 '13 at 3:19
    
@Murta Yes it does. Also for multiple lines with only one line as a resut too. Just like the case you provided. –  Kuba Sep 16 '13 at 9:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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