17
$\begingroup$

Sometimes when you have long code you need to check some part of this code.

the way I am using currently is to selected the part that I want and then copy it to new notebook and then evaluate it there. this process becomes annoying when repeated several times.

is there a better way to automatically evaluate selection in new notebook without need to go through copy paste procedure?

Update :

Thanks to halirutan for his suggestions. But one of my concerns is to evaluate selection within the cell in to another window. For example: enter image description here

how to evaluate the selection without manually copy and paste. If the cell itself contain long content, then the only way to debug the cell is be copying part by part and pasting into another notebook and evaluate as a whole new cell.

$\endgroup$
  • $\begingroup$ Yes, I guess it should be fairly easy to make a button which copies the currently selected cells, inserts them into a new notebook and evaluates everything. Please look at SelectedCells, CreateDocument and NotebookEvaluate. $\endgroup$ – halirutan Jul 23 '14 at 4:44
  • $\begingroup$ related 32340 $\endgroup$ – Kuba Jul 23 '14 at 9:20
13
$\begingroup$

Context Menu

The content of context menus is defined in the ContextMenus.tr in the $InstallationDirectory\SystemFiles\FrontEnd\TextResources folder. It is possible to modify this file with A) a text editor (e.g. Notepad++) or B) programmatically.

In order to get a new context menu item with the functionality you described:
First copy the original ContextMenus.tr form the $InstallationDirectory to your $UserBaseDirectory

If[! DirectoryQ[
   FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]],
 CreateDirectory[
  FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]]]

contextMenusFile =    
 CopyFile[
   FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources", 
     "ContextMenus.tr"}], 
   FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources",
     "ContextMenus.tr"}]]

then

A) open ContextMenus.tr with an editor

SystemOpen[contextMenusFile] (*to open the file with your default editor*)
SystemOpen[FileNameDrop[contextMenusFile, -1]] (*to just open the directory*)

and find the "Input" -> { section. Now insert the following part between MenuItem["Evaluate &in Place", Evaluate[All]], and MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"], (or wherever you prefer) and save the file.

MenuItem["Evaluate in &New Notebook",
        FrontEndExecute[{
            FrontEndToken["Copy"],
            FrontEndToken["New"],
            FrontEndToken["Paste"],
            FrontEndToken["HandleShiftReturn"]
        }]],

B) programmatically insert the new MenuItem into the existing list

contextMenus = Import[contextMenusFile, "HeldExpressions"];

insertPosition = 
 Flatten[Position[contextMenus, 
   MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"]]]

newContextMenus = 
  Insert[contextMenus, 
   MenuItem["Evaluate in &New Notebook", 
    FrontEndExecute[{FrontEndToken["Copy"], FrontEndToken["New"], 
      FrontEndToken["Paste"], FrontEndToken["HandleShiftReturn"]}]], 
   insertPosition];

and export it with

Export[contextMenusFile, newContextMenus, {"Package", "HeldExpressions"}, "Comments" -> None]

After you restarted Mathematica the menu item "Evaluate in New Notebook" appears whenever you right-click on a selected input. Menu

A left-click on "Evaluate in New Notebook" will create a new notebook with your selected input as its first input. It will then automatically be evaluated. newNB


Extra tip: To close the window of that new notebook without saving and without the pop-up of the "Save changes to ..." dialog, just have Shift pressed when you click on the close window x (in Windows).


Keyboard Shortcut

In order to get the same functionality as a keyboard shortcut (Alt + n):
Add

Item[KeyEvent["n", Modifiers->{Command}], 
    FrontEndExecute[{
        FrontEndToken["Copy"],
        FrontEndToken["New"],
        FrontEndToken["Paste"],
        FrontEndToken["HandleShiftReturn"]
    }]]

to the list in KeyEventTranslations.tr, which is located in the $InstallationDirectory\SystemFiles\FrontEnd\TextResources\Windows folder.

$\endgroup$
  • $\begingroup$ Great solution, thanks a lot $\endgroup$ – Algohi Aug 16 '14 at 5:10
8
$\begingroup$

To give you a first idea: Try this button, which uses all currently selected cells in the selected notebook, copies them into a new document and evaluates everything

Button["Test Selected Cells",
 NotebookEvaluate[CreateDocument[NotebookRead[SelectedNotebook[]]], 
  InsertResults -> True], Method -> "Queued"]

You can use this to create a new palette which is then always available when you want to test your code.

Regarding your updated question: What you have to do is, look at the code I have given you and use NotebookRead and Print to examine what exactly the result of the reading procedure is when you only select parts of the text. You will see that you get box expression in this case. Now you can adapt the code to produce a correctly working button.

Button["Test Selected Cells", 
 NotebookEvaluate[
  CreateDocument[
   Cell[BoxData[# ], "Input"] & /@ 
    Flatten@{NotebookRead[SelectedNotebook[]]}], 
  InsertResults -> True], Method -> "Queued"]

Now it should be possible to select complete cells and additionally, only parts of the text.

$\endgroup$
  • $\begingroup$ thanks. can you look at the update in the question and see if what I want can be achieved. $\endgroup$ – Algohi Jul 23 '14 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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