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

Bernhard Herzog bh at intevation.de
Sat Feb 8 13:47:06 EST 2003


"Andrew Dalke" <adalke at mindspring.com> writes:

> Edward K. Ream:
> > 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.
> 
> One of the papers I read used
> 
> class BaseCommand:
>   def Do(self):
>     ...
>   def Undo(self):
>     ...
>   def ReDo(self):
>     self.Do()

IMO undoing shouldn't be handled by the commands directly. This approach
might work well for documents with a relatively simple structure such as
e.g. a simple text editor where you can express all actions as
insertions and deletions.

For more complex documents the objects that make up the document should
take the burden of collecting the information of what needs to be done
when operations on them have to be undone. After all the objects are
responsible for the action in the first place and know best what
internal state needs to be remembered. With this approach the commands
only need to deal with undo in a very abstract and uniform way.

The approach I took in Sketch is described in
http://sketch.sourceforge.net/Doc/devguide-19.html


Recently I've started to think about a somewhat enhanced undo scheme.

In most programs the undo/redo history is completely linear. Once you
undo an operation you can redo it or you can do some different action.
If you don't redo your redo information is lost.

It might be useful to remember the redo information in that case so that
instead of a linear undo/redo model you get a tree. Of course there
would have to be a good way to communicate to the user what this tree
looks like which I'm not sure how to do.

The one program I regularly use that does not follow the common linear
model is Emacs which sort of uses a linearization of the above tree
model. Undoing in emacs is an action that itself can be undone. There is
no real concept of redo. Redoing is done by undoing the undo. For a
better description see
http://www.gnu.org/manual/emacs-20.3/html_node/emacs_19.html

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Python-list mailing list