How can I hide $Aborted message?
I know that I'm aborting the evaluation and can see it when the black bar on the side开发者_高级运维 goes away. So there is no need for this message. How do I turn it off?
The following is a generalization of the $Pre
-based method suggested by Simon and the $Post
-based method suggested by Mr.Wizard.
In both cases one should set HoldAllComplete
attribute to the pure functions and wrap x
(input expression) inside them by Unevaluated
so they work correctly with inputs having Head
s Sequence
, Unevaluated
and Evaluate
.
Compare two versions of the $Pre
-based solution:
f1 = Function[{x}, CheckAbort[x, Null], {HoldAll}]
f2 = Function[{x}, CheckAbort[Unevaluated@x, Null], {HoldAllComplete}]
In[3]:= f1@Sequence[1,2]
f1@Abort[]
f1@Evaluate[Abort[]]
f1@Unevaluated[1+1]
Out[3]= 1
Out[5]= $Aborted
Out[6]= 2
In[7]:= f2@Sequence[1,2]
f2@Evaluate[Abort[]]
f2@Unevaluated[1+1]
Out[7]= Sequence[1,2]
Out[9]= Unevaluated[1+1]
One can see that the first version can easily be broken with Evaluate[Abort[]]
and works incorrectly with input expressions having Head
s Sequence
and Unevaluated
. It is generally true for any one-argument function without HoldAllComplete
attribute. Since $Pre
, $Post
and friends all are one-argument functions one must always set HoldAllComplete
attribute to them.
I also see no problems with using Null
as the second argument of CheckAbort
(there is no need for Return@Null
). On my machine (Mathematica 7.0.1 under Windows 2000) $Abort
is never returned in the case of the f2
.
The same is true for the $Post
-based solution. It can be generalized as follows:
$Post = Function[x, If[Unevaluated@x =!= $Aborted, Unevaluated@x],
HoldAllComplete]
Test expressions:
In[14]:= Unevaluated[1+2]
Sequence[1,2]
$Aborted
Abort[]
Out[14]= Unevaluated[1+2]
Out[15]= Sequence[1,2]
If you are not using $Post
for something else, or if you can combine functions, you could use:
$Post = # /. $Aborted -> Null &;
Be warned that you may break things, as programs may use $Aborted
in their control flow.
More robustly, addressing the problem Alexey demonstrates:
$Post = Function[Null, Unevaluated@# /. $Aborted -> Null, HoldAllComplete];
The following seems to work:
SetAttributes[katch, HoldAll];
katch[x_] := CheckAbort[x, Return@Null]
$Pre = katch;
After testing, remember to return $Pre to its previous value:
$Pre =.
Probably interferes with some internal abort catching, though.
Edit
Simon's version:
$Pre = Function[{x}, CheckAbort[x,Return@Null], {HoldAll}]
精彩评论