GNU TeXmacsUser manualReferenceAboutEnglish
The Scheme interface to the history

1.Functions

The functions below are defined in

build-glue-editor.scm

(they act on the archiver of the current editor) and

build-glue-basic.scm

.

(undo 0), (redo i)

Undo the last step of the current editor's author (see the archiver for the steps of other authors), or redo future number i. The argument of undo must be 0. They first drop the pending cursor modifications, and do nothing while other modifications of the current action are pending, and the C++ undo also resets the graphics editor and refuses to undo inside a graphics while graphics-undo-enabled is false.

(undo-possibilities), (redo-possibilities)

The number of possible undos (0 or 1) and of futures. The Edit menu uses them.

(unredoable-undo)

Cancel the pending modifications of the current action, then undo the last step and forget it, without creating a future.

(start-editing), (end-editing), (cancel-editing)

Delimit a user action. The event handlers already call them; Scheme code only needs them when it changes documents outside an event, for instance in a delayed block or a callback of a network request, where (commit-changes), the same as end-editing, closes the step.

(add-undo-mark), (remove-undo-mark)

Confirm the pending modifications as a step now; or reopen the last step of the current author, so that the next modifications become part of it (the graphics editor uses this to merge the start of an operation with its end,

graphics-group.scm:339

).

(mark-new), (mark-start m), (mark-end m), (mark-cancel m)

Markers: a new marker number, and the operations described in the archiver. Every mark-start must be followed by exactly one mark-end or mark-cancel with the same marker, in the same editor.

(archive-state)

Record the cursor and the selection in the pending step, so that undoing it restores them.

(new-author), (set-author a), (get-author), (start-slave a)

Authors: a fresh author; the author to which the next modifications are attributed; and the birth of a slave author in the current step.

(busy-versioning?)

True while an undo, a redo or a cancellation is being applied. Observers which react to modifications can use it to avoid modifying the document in turn.

(clear-undo-history)

Empty the history. Note: this acts on the archivers of all open buffers, see the pitfalls.

(show-history)

Print the history of the current editor on the standard output.

2.How to

Make a command one undo step.
Nothing has to be done for a command which runs within one event (a key, a menu entry, a mouse click): all its modifications form one step. A command which needs several events, for instance a dialog which applies a change for each input, can bracket them with a marker:
(define my-mark #f)
(define (my-start)
  (set! my-mark (mark-new))
  (mark-start my-mark)
  (archive-state))
(define (my-done ok?)
  (if ok? (mark-end my-mark) (mark-cancel my-mark))
  (set! my-mark #f))

Within one event, the macro try-modification does the same: its changes are kept as one step when the body returns a true value and are cancelled otherwise.

(try-modification
  (insert "x")
  (my-check-something))
Show a temporary preview.
Commands which change the document repeatedly in response to a gesture, such as the pinch gestures of
format-geometry-edit.scm

, undo the previous preview with (undo 0) before applying the next one. This only works if each preview is a step of its own and nothing else was changed in between.

Attribute changes to another author.
Changes which do not come from the user, such as the output of a computation, should be attributed to their own author, so that the user's undo does not remove them unless needed. Take a new-author, record its birth with start-slave in the step which starts the computation, and make the later changes between (set-author a) and (commit-changes), restoring the previous author afterwards, as with-author in
plugin-eval.scm

does.

Test undo.
The test suites check the history with undo-possibilities, see for instance
check/editing-test.scm

. In a test which runs without events, wrap each simulated action in (start-editing) and (end-editing): otherwise all the modifications end up in one step, or remain pending and are ignored by undo.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".