A while back I wrote a Mathematica function for starting completely fresh on a filled and previously saved notebook. A word of caution: it clears notebook history and system cache among several other things. I'll give you all definitions. Most of it is just an experiment in creating a self-correcting function. The only definition you really need is main, defaults, and clearToolbars[]. You may also wish to adjust default values to your preferences so that no arguments need be filled. You can remove patterns from arguments if you wish but make certain to change argument names into pattern names. For example n:zpat
becomes n_
, and h:fpat
becomes h_
.
I just noticed that for some reason my alerts arent rendering properly to warn on invalid inputs. Not sure why but everything else works. In short, both arguments must be n >= 0
and h >= 0 or infinity
, Please write back if you have any questions or difficulties. I am very happy to help. :-)
(* freshStart 0.1.0 *)
(*** defaults and patterns ***)
freshdef = {0, 16};
{zpat = _Integer?(# >= 0 &), fpat = zpat | Infinity};
(*** main ***)
Options[freshStart] = {save -> True, quit -> False,
syscache -> True, toolbars -> False, remove -> {"Global`*"}};
freshStart[n : zpat, h : fpat, OptionsPattern[]] :=
Block[{nb = EvaluationNotebook},
If[OptionValue@save, NotebookSave[]];
Unprotect[In, Out];
Clear[In, Out];
Protect[In, Out];
$Line = n; $HistoryLength = h;
If[OptionValue@syscache, ClearSystemCache[]];
If[OptionValue@toolbars, clearToolbars[]];
If[Length@OptionValue@remove > 0, (Quiet@Remove[#]) & /@
remove]; If[OptionValue@quit, Quit[]];
];
(*** direct defaults ***)
freshStart[OptionsPattern[]] :=
freshStart[freshdef[[1]], freshdef[[2]]];
freshStart[x : zpat, OptionsPattern[]] := freshStart[x, freshdef[[2]]];
(*** self-correcting ***)
freshStart[x_, y_,
z__] := (alert[freshStart, "trailing at pos 3", "dropped",
(Style[#, col[trail], Bold] &) /@ {z}]; freshStart[x, y]);
freshStart[x_ /; x === Infinity] := freshStart[freshdef[[1]], x];
freshStart[x_ /; x === Infinity, y : fpat] :=
freshStart[freshdef[[1]], x];
(*** alert fail ***)
freshStart[xy__] :=
alert[freshStart, "inv args", (Style[#, Bold, col[fail]] &) xy,
"expecting",
"{n \[Element] \[DoubleStruckCapitalZ] \[VerticalSeparator] n \
\[GreaterEqual] 0, h \[Element] {n,+\[Infinity]}}"];
(*** args and alerts generators ***)
Options[alert] = {id -> Null, info -> Null};
alert[fn_, anomaly_ : "trailing dropped at pos 3", init_, final_,
OptionsPattern[]] :=
Echo@Row[{fn, ": ", anomaly, " ", init,
Style[" \[Rule] ", Orange, Bold], final}];
(*** additionl utilities ***)
clearToolbars[] :=
SetOptions[EvaluationNotebook[], DockedCells -> Inherited,
TaggingRules -> Inherited, CellContext -> Inherited];
freshStart[]