8

I often copy Mathematica code from websites (such as SO) to a notebook. The code usually gets pasted as a single input cell. I'm looking for a simple way to paste it as several input cells for convenient step-by-step evaluation.

For example,

a = 2;
f[x_] := x^a

Plot[f[x], {x,0,2}]

would ideally paste as two input cells. Manual formatting (i.e. the original newlines) should preferably also be preserved (this is not the case with default pasting).

Generally, if one selects all input cells (ALT-click), copies them into a text editor, then copies the resulting text back into a notebook, one should get a close equivalent of the original cells that were copied.

dionys
  • 141
  • 7
Szabolcs
  • 21,860
  • 5
  • 72
  • 158

2 Answers2

15

Or.... you can just Hit Ctrl+Shift+D at the point where you'd like to split your singe cell into 2 separate cells. Some times it's advantageous to group multiple operations into a single cell (or rather not split them to begin with). To undo such a split, select both cells (or more than 2 if you'd like) and click Ctrl+Shift+M to merge them into one.

Hulk1991
  • 2,557
  • 11
  • 29
  • 44
Gregory Klopper
  • 2,225
  • 1
  • 12
  • 14
  • 1
    @Szabolcs in that case it's no wonder you asked this question. That would be quite annoying! – Mr.Wizard Nov 07 '11 at 18:46
  • +1, I did not know about that shortcut, either. I've just lived with it, and assumed there was nothing to be done. – rcollyer Nov 07 '11 at 18:58
  • 1
    @Szabolcs yes this is a standard shortcut for dividing and merging. Now what I would like to have is a shortcut for making/unmaking an initialization cell. There is none standard, afaik. Any suggestions? – magma Nov 08 '11 at 15:07
  • @magma While I don't have time to test this now, it should be possible to modify the `MenuSetup.tr` file (found in `SystemFiles/FrontEnd/TextResources/Windows`) to add a shortcut key for this item. Make a backup of the file though and make sure the backup *doesn't* have the .tr extension. – Szabolcs Nov 08 '11 at 15:30
  • 1
    @magma Alt+C P I is pretty quick for toggling initialisation. – Chris Degnen Nov 08 '11 at 20:04
  • 1
    @Gr3gK1 +1. Thanks for those tips. I didn't know about Ctrl+Shift+M. As regards `Cell -> Divide Cell` (Ctrl+Shift+D), an insertion point divides the cell in two, but a text selection divides it in three: everything before, the text selection, and everything afterwards. See [here](http://reference.wolfram.com/mathematica/ref/menuitem/DivideCell.html) (I didn't know about this either until I searched after reading your answer). – tomd Nov 10 '11 at 21:21
  • @Chris yes, but i was hoping for something simpler. I guess there is no hope – magma Nov 11 '11 at 11:23
0

This is a simple implementation (I'd also appreciate a code review, I'm not good in front end programming):

(* This converts a sequence of expressions into boxes *)
Clear[makeManyBoxes]
SetAttributes[makeManyBoxes, HoldAllComplete];
makeManyBoxes[e__] := List@ReleaseHold[MakeBoxes /@ HoldComplete[e]]

(* Split a list at separator *)
split[list_, sep_] := 
 DeleteCases[Split[list, #1 =!= sep && #2 =!= sep &], {sep}] 

wr[e_] := NotebookWrite[InputNotebook[], Cell[BoxData[e], "Input"]]

CreatePalette@Button["Paste!",
  Module[{clipboard},
   clipboard = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
   If[StringQ[clipboard],

    wr /@ 
     split[ToExpression[clipboard, InputForm, makeManyBoxes], "Null"]
    ]
   ]
  ]

It breaks cells at empty lines. For this, we need to parse the expression first (what if an empty line appears in the middle of a long Module?). But parsing alone will cause several problems.

Problems with this implementation:

  • it removes comments
  • it can't handle incorrect inputs
  • it doesn't preserve the formatting (newlines)
  • I'm sure there must be several other things that can go wrong
Szabolcs
  • 21,860
  • 5
  • 72
  • 158
  • 2
    Now we need something that can extract a notebook and generate a SO post, interleaving input & output cells, including graphics and commenting out the output cells ... – Dr. belisarius Nov 07 '11 at 11:53
  • @belisarius A good start would be an imgur uploader. imgur[g] should rasterize the argument (to PNG) at an appropriate width for SO, upload it to imgur, and return the markdown ready to be pasted: `[mma](http://i.imgur.com/ZENa4.jpg)` This should be a good starting point, but I failed at adapting it: http://stackoverflow.com/q/5853134/695132 **EDIT:** I think I'll make that a question – Szabolcs Nov 07 '11 at 14:47