# Implementing efficient multiple undo

The Mathematica Front End's built-in Undo command can only undo a single editing step.

Has anyone implemented or seen implemented an efficient and comfortable multiple undo?

I have in mind ideas such as, CellEventActions that keeps a stack in the cell context pushing the cell state every time a key among a list (brackets, @, operators) is pressed. Some other shortcut to recover older states (or a palette, or perhaps tweaking the front end's .tr files??)

I am not familiar at all with typical text based implementations and their issues. I guess it wouldn't be a practical problem to lose the undo state between sessions, would it?

So, has this been done, or is it doable or practical? What are your thoughts?

-
This certainly seems possible at least for the "Program" - style cells, and I have plans to integrate this as a feature in the next versions of the syntax highlighter, which you are probably familiar with. Since my highlighting engine is grabbing the cell's contents on every trigered event, this should be a relatively simple matter to do. One problem I face here is that I don't know how to trigger the CTRL key (or its equivalent on other platforms) yet. –  Leonid Shifrin Feb 18 '12 at 21:41
@LeonidShifrin I would happily write Undo[] multiple times, don't let Ctrl get in your way. –  David Mar 6 '12 at 7:35
I can't undertand. Why Mathematica is missing this? This action is extremly needed and useful. And missing until latest Mathematica versions. Lol. Why? Maybe someone should contact Mathematica and show this question for them. –  GarouDan Jun 26 '12 at 19:03
undo-for-mathematica.alaifari.com –  Tyilo Aug 24 '13 at 22:45
@JacobAkkerboom I believe you and the sentence I cited explains why such a significant statement was removed ("non-disclosure agreement"). But may be there is a video of this conference with that statement? In any case, thank you for citing, it is wonderful news. –  Alexey Popkov Jan 8 at 10:55

I made a simple version control and undo function. (Since it uses system commands right now it is only tested for Linux. Windows support is added but not tested. MacOS is not tested either.)

Here is the link to undo.m or the github if you just want to try it (and trust me enough to run the code without checking) run

Get["https://raw.github.com/jensbob/mathematica_undo/master/undo.m"]


Would be great to have some feedback with ideas how to improve it, also I didn't test this a lot but it seems to be working fine. It is probably not the most efficient way but I think it is a good start.

This package adds a poor mans version control and undo functionality to Mathematica. For a previously saved notebook 'notebook.nb' a version info file 'notebook.nb.undo.mx' is created and every time changes are committed a backup file 'notebook.nb[version].bak' is created. System menu entries in insert are created.

## Keyboard Shortcuts

Alt+z : Undo

Alt+x : Redo

Alt+s : Commit

Alt+d : Opens CommitInfo Dialog

## Usage: Evaluate the following commands

ManualCommit (Default) - Only do commits manually.

AutoCommit - Turn on automatic commits. Every time a cell is evaluated a new commit is made. (This can lead to a lot of files)

CronCommit[n] - makes a new commit every n minutes.

CommitNow - Making a commit CommitInfo - Show a list of all Versions CommitClean - Remove all commited files

Undo - Undo to the previous commit Redo - Undo to the next commit GotoCommit[n] - Go to the n-th version

## NOTICE

When altering not the latest version, the changes (when running undo or redo) are not saved, only when running 'commit' these changes are changed (with a new highest version number). Only changes to the version with the highest version number are changed upon 'undo'

Whether a commit needs to be made when running undo is checked with a system variable that can be altered by manually saving the document. Leading to possible loss of changes. For best results do not save the notebook manually and turn auto save off - just use commit to save the work.

EDIT: This version also has problems with individual context for each notebook, so just be warned. :)

-
thanks to Chris Degnen for helping with the keyboard shortcuts. mathematica.stackexchange.com/questions/16165/… –  jenson Dec 12 '12 at 16:04
from looking at the code, it should work under OS X (the default shell is bash) –  acl Dec 12 '12 at 22:48