undo (was Re: Why does Dynamic Typing really matter?!?)

Edward K. Ream edream at tds.net
Sat Feb 8 05:22:41 EST 2003


Leo implements undo using the same "string of beads" model found in yellow
box.  It supports unlimited undo/redo in a very demanding environment, i.e.,
in an outliner in which a) outline nodes may be inserted, deleted, moved,
sorted, etc., b) text corresponding to nodes may be changed and c) the two
kinds of operations may be intermixed freely.

Here is the overview of undo from LeoPy.leo:

[starts]
Unlimited undo is straightforward; it merely requires that all commands that
affect the outline or body text must be undoable. In other words, everything
that affects the outline or body text must be remembered.

We may think of all the actions that may be Undone or Redone as a string of
beads (undo nodes). Undoing an operation moves backwards to the next bead;
redoing an operation moves forwards to the next bead. A bead pointer points
to the present bead. The bead pointer points in front of the first bead when
Undo is disabled.  The bead pointer points at the last bead when Redo is
disabled. An undo node is a Python dictionary containing all information
needed to undo or redo the operation.

The Undo command uses the present bead to undo the action, then moves the
bead pointer backwards. The Redo command uses the bead after the present
bead to redo the action, then moves the bead pointer forwards. All undoable
operations call setUndoParams() to create a new bead. The list of beads does
not branch; all undoable operations (except the Undo and Redo commands
themselves) delete any beads following the newly created bead.

I did not invent this model of unlimited undo.  I first came across it in
the documentation for Apple's Yellow Box classes.
[ends]

BTW, Python makes implementing this relatively easy.  Each "bead" is simply
a dictionary containing whatever is needed to undo _and_ redo the operation.
setUndoParams has a **keywords parameter.  The caller specifies whatever is
needed as keyword arguments; setUndoParams puts the items of the keywords
dict into the bead's dict.

Full details are in LeoPy.leo at
http://sourceforge.net/project/showfiles.php?group_id=3458 See the node
called @file leoUndo.py. There is some complex machinery involved behind the
scenes, and I have found that adding new undoable operations is
straightforward.

You will need to download and install leo2.py 3.10 to read LeoPy.leo,
although you could read the "raw" code of leoUndo.py at Leo's CVS site:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/leo/leo/leoUndo.py?rev=1.37&content-type=text/vnd.viewcvs-markup

HTH

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edream at tds.net
Leo: Literate Editor with Outlines
Leo: http://personalpages.tds.net/~edream/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list