8
$\begingroup$

Let's say we have a cell:


(*newline*)
(*newline*)
Plot[
    x, 
    {x, 0, 1}
]
(*newline*)    
Plot[x, {x, 0, 2}]

How to read it expression by expression?

So e.g to read Plots one by one, in whatever form, Boxes, Hold[expression], String?

Here's my attempt:

SelectionMove[PreviousCell[] (*cellobject in general*), All, Cell];

text = First[  FrontEndExecute[
    FrontEnd`ExportPacket[NotebookSelection[], "InputText"]]
]; (*1*)

Module[{stream = StringToStream[text], temp},
   temp = ReadList[stream, Hold[Expression]];
   Close[stream];
   temp
]
{Hold[Plot[x, {x, 0, 1}]], Hold[Plot[x, {x, 0, 2}]]}

Quite long, and problematic because it changes selection.

I think there should be better way since this is what FrontEnd is doing anyway, reading expression by expression:

{
    1, Abort[]
}
2
$Aborted
2

(*1*) - How do I extract the contents of a selected cell as plain text?

$\endgroup$
  • 1
    $\begingroup$ At least tangentially related: (29264) $\endgroup$ – Mr.Wizard May 9 '16 at 9:18
3
$\begingroup$

Instead of SelectionMove and NotebookSelection one can use NotebookRead[PreviousCell[]] and then cleanup the text returned by the ExportPacket.

Thread @ MakeExpression[
 "{" <> 
  StringReplace[
   First[FrontEndExecute[FrontEnd`ExportPacket[NotebookRead[PreviousCell[]], "InputText"]]], 
   {"\r\n " -> "", "\r\n" -> ","}] 
  <> "}", 
 StandardForm] /. HoldComplete[Null] :> Nothing

Output for the two examples:

{HoldComplete[Plot[x, {x, 0, 1}]], HoldComplete[Plot[x, {x, 0, 2}]]}

and

{HoldComplete[{1, Abort[]}], HoldComplete[2]}
$\endgroup$
2
$\begingroup$

Here is a slight modification of Karsten's answer. Previously I felt it was too similar, but I suppose it cannot hurt to post it. The main difference is that I avoid MakeExpression. I also like the alternative in your question, which is to use StringToStream.

read[cObj_] :=
 DeleteCases[#, HoldComplete[Null]] &@(
   ToExpression[#, InputForm, 
      HoldComplete] & /@
    (StringSplit[#, "\n"] &)@
     First@FrontEndExecute@
       FrontEnd`ExportPacket[NotebookRead@cObj, "PlainText"]
   )
$\endgroup$
  • $\begingroup$ Not general enough, if you have a cell indentical as my example: {"(*newline*)\r", "(*newline*)\r", "Plot[\r", "x,\r", "{x,0,1}\r", "]\ \r", "(*newline*)\r", "Plot[x,{x,0,2}]"} so StringSplit has to be more careful. $\endgroup$ – Kuba May 4 '16 at 16:19
  • $\begingroup$ @Kuba hmm thats strange, I thought "\n" caught all newlines on my other computer (OSX). $\endgroup$ – Jacob Akkerboom May 4 '16 at 20:23

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.