Preserving Notebook Changes

• To: mathgroup at smc.vnet.net
• Subject: [mg98885] Preserving Notebook Changes
• From: Yuri Kandrashkin <spinalgebra at gmail.com>
• Date: Mon, 20 Apr 2009 19:14:09 -0400 (EDT)

```Hi,

Two weeks ago there was discussion about UNDO function. I have spent
some time trying to find out a reasonable solution. As I understand
currently there is no solution to have two or more channels to control
the notebook selection. Thus there is no =93real multiple UNDO=94 can be
realized on the level of language of Kernel and FronEnd. The only
strategy I have found is automatic saving of the modified cells to a
separate notebook within given time interval. Below is the code of the
button to be placed in the Docked Cell. The notebook with preserved
cells can be later investigated with some tools like NotebookDiff from
AuthorTools`. I would appreciate if anybody can improve the code and
combine it with the comparison tool.

dockedCellsBtn[] :=
Cell[BoxData@{ToBoxes@
DynamicModule[
{NotebookPreserve, initialize,
nb, t0,
q = False,
sty = "Input" | "Text"},
NotebookPreserve[__] := Null;
NotebookPreserve[True, nb_NotebookObject, sty_, t0_] :=
Module[{cls, ops, ps, lastUpd},
cls = First@NotebookGet[ButtonNotebook[]];
cls = Cases[cls, Cell[_, sty, ___], \[Infinity]];
ops = Drop[#, 2] & /@ List @@@ cls;
ps = CellChangeTimes /. ops /. CellChangeTimes :> {{0}};
lastUpd = Map[Max, ps];
cls = Extract[cls,
Position[lastUpd - If[NumberQ@t0, t0, 0], _?Positive]];
SelectionMove[nb, After, Notebook];
NotebookWrite[nb, cls];
NotebookSave[nb];
Max@lastUpd];
initialize[] :=
Module[{nbobj, fn},
SetOptions[ButtonNotebook[], {"TrackCellChangeTimes" -> True}];
fn = StringInsert[NotebookFileName[], "Preserved.", -3];
If[FileExistsQ[fn],
nbobj = NotebookOpen[fn, Visible -> False],
nbobj = NotebookPut@Notebook[{}, Visible -> False];
NotebookSave[nbobj, fn]];
SetOptions[nbobj,
WindowMargins -> {{Automatic, 0}, {Automatic, 0}},
ClosingAutoSave -> True,
"TrackCellChangeTimes" -> False,
WindowTitle ->
(WindowTitle /.
AbsoluteOptions[ButtonNotebook[], WindowTitle]) <>
".Preserved"];
nbobj];
Button[
Dynamic[Refresh[
t0 = NotebookPreserve[q, nb, sty, t0];
"Notebook Preserve: " <> If[q, "On", "Off"],
TrackedSymbols -> Full,
UpdateInterval -> 60]],
q = ! q;
t0 = If[q,
nb = initialize[];
Module[{cls, tt},
cls = First@NotebookGet[nb];
cls = Drop[#, 2] & /@ List @@@ cls;
tt = CellChangeTimes /. cls /. CellChangeTimes :> {{0}};
Max@Flatten@tt],
SetOptions[nb, Visible -> True];
NotebookSave[nb];
NotebookClose[nb]]];
]]}]

If[NotebookFileName[] =!= \$Failed,
SetOptions[InputNotebook[],
"TrackCellChangeTimes" -> True,
DockedCells -> dockedCellsBtn[]]]

Sincerely,
Yuri Kandrashkin

```

• Prev by Date: Re: Plot Axis Unit Conversions
• Next by Date: Re: Re: Looking for more Mathematica online user groups/forums
• Previous by thread: Mathematica in conjunction with outside program; NMinimize fails. Symbolic v. numeric evaluation problem?