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.

I have a relatively simple bit of code that looks like the following:

---some code here---
If[statement, Abort[]];
---more code following---

However, while $Aborted appears as output, the evaluation of the rest of the cell does not abort, continuing execution of code beyond the If statement. How do I actually get the running cell to cease evaluating the rest of the cell?

Edit: It seems that one way to accomplish this is to wrap the entire cell in parenthesis, e.g., but this is a fairly ugly solution, especially with auto-formatting turned on (my cell has many lines of code, and the parenthesis are huuuuuge). Anyone have a more elegant solution?

(
---some code here---
If[statement, Abort[]];
---more code following---
)
share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

This question has been asked before on stack overflow. However we will summerize some of the answers given there on our new Mathematica site.

Wrap In Compound Expression

One suggestion by Michael Pilat, given there was to wrap your lines in CompoundExpression, e.g.

(
   Print@Range[5];
   Abort[];
   Print@Range[5];
)

During evaluation of In[39]:= {1,2,3,4,5}

$Aborted

If you mind the formatting of parenthesis, you could explicitly use the FullForm of CompoundExpression like this:

CompoundExpression[
   Print@Range[5],
   Abort[],
   Print@Range[5],
]

Use of $PreRead

Suggested by Alexey Popkov:

In[1]:= $new$PreRead = False;
AbortAllPendingInputs := 
  AbortProtect[If[! $new$PreRead, $new$PreRead = True;
    $TimeOfAbort = SessionTime[];
    last$PreRead = ToString[Definition[$PreRead], InputForm];
    ClearAll[$PreRead];
    $PreRead := If[TrueQ[SessionTime[] - $TimeOfAbort < 1], "",
       $new$PreRead = False;
       ClearAll[$PreRead];
       If[last$PreRead === "Null", #, 
        ToExpression[last$PreRead]; $PreRead@#]
       ] &;]];

In[3]:= f := CheckAbort[Pause[10], AbortAllPendingInputs; Abort[]]

In[4]:= While[True, f]
While[True, f]
While[True, f]

Out[4]= $Aborted

Note that this solution will need to be modified if you use $PreRead for anything else.

Use of CellEvaluationFunction

CellEvaluationFunction gets the BoxData expression, before it is even split. Just assigning something like CellEvaluationFunction:>ToExpression seems to do the trick.

You can set it as an Input cell style, or as a notebook option, or front end session option, or however you like

SetOptions[$FrontEndSession, CellEvaluationFunction :> ToExpression]

However, you lose the multiple outputs in those new lines when you don't use ;. This can probably be solved but I don't know very well how CellEvalutionFunction works, and how can it receive a box structure, default to Identity but expect to return an expression. Edit should you come up with any improvements

share|improve this answer
    
Since you tagged this community wiki, would you mind if others were to port over some of the suggestions from the stack overflow question here? With appropriate credit and whatnot of course! –  nixeagle Mar 28 '12 at 21:52
    
@nixeagle No, of course I don't mind! That was the idea. –  Leonid Shifrin Mar 28 '12 at 21:55
    
Alright, I'll go ahead and work on that. Plus I ought to suggest to @Guillochon that using more smaller functions might help with the concern raised in his Edit. –  nixeagle Mar 28 '12 at 21:56
1  
Alright, done editing this. If anyone else can make the answer clearer or wants to volunteer a 3rd (possibly better!) solution, please do so. As a side note, as I don't know where else to mention this, do we want to keep that abort tag on this question? –  nixeagle Mar 28 '12 at 22:06
    
@LeonidShifrin, I just saw that your answer was a community wiki and that in the link you provided someone suggests in comments a CellEvaluationFunction approach, like my answer. Should I delete it and put it in the community answer? –  Rojo Mar 31 '12 at 18:23
show 4 more comments

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.