]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/redo.el
submodulized .emacs.d setup
[.emacs.d.git] / emacs / redo.el
1 ;;; redo.el -- Redo/undo system for XEmacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993-1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5 ;; Copyright (C) 1997 Kyle E. Jones
6
7 ;; Author: Kyle E. Jones, February 1997
8 ;; Keywords: lisp, extensions
9
10 ;; This file is part of XEmacs.
11
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ;; 02111-1307, USA.
26
27 ;;; Synched up with: Not in FSF.
28
29 ;;; Commentary:
30
31 ;; Derived partly from lisp/prim/simple.el in XEmacs.
32
33 ;; Emacs' normal undo system allows you to undo an arbitrary
34 ;; number of buffer changes.  These undos are recorded as ordinary
35 ;; buffer changes themselves.  So when you break the chain of
36 ;; undos by issuing some other command, you can then undo all
37 ;; the undos.  The chain of recorded buffer modifications
38 ;; therefore grows without bound, truncated only at garbage
39 ;; collection time.
40 ;;
41 ;; The redo/undo system is different in two ways:
42 ;;   1. The undo/redo command chain is only broken by a buffer
43 ;;      modification.  You can move around the buffer or switch
44 ;;      buffers and still come back and do more undos or redos.
45 ;;   2. The `redo' command rescinds the most recent undo without
46 ;;      recording the change as a _new_ buffer change.  It
47 ;;      completely reverses the effect of the undo, which
48 ;;      includes making the chain of buffer modification records
49 ;;      shorter by one, to counteract the effect of the undo
50 ;;      command making the record list longer by one.
51 ;;
52 ;; Installation:
53 ;;
54 ;; Save this file as redo.el, byte compile it and put the
55 ;; resulting redo.elc file in a directory that is listed in
56 ;; load-path.
57 ;;
58 ;; In your .emacs file, add
59 ;;   (require 'redo)
60 ;; and the system will be enabled.
61
62 ;;; Code:
63
64 (provide 'redo)
65
66 (defvar redo-version "1.02"
67   "Version number for the Redo package.")
68
69 (defvar last-buffer-undo-list nil
70   "The head of buffer-undo-list at the last time an undo or redo was done.")
71 (make-variable-buffer-local 'last-buffer-undo-list)
72
73 (make-variable-buffer-local 'pending-undo-list)
74
75 ;; Emacs 20 variable
76 (defvar undo-in-progress)
77
78 (defun redo (&optional count)
79   "Redo the the most recent undo.
80 Prefix arg COUNT means redo the COUNT most recent undos.
81 If you have modified the buffer since the last redo or undo,
82 then you cannot redo any undos before then."
83   (interactive "*p")
84   (if (eq buffer-undo-list t)
85       (error "No undo information in this buffer"))
86   (if (eq last-buffer-undo-list nil)
87       (error "No undos to redo"))
88   (or (eq last-buffer-undo-list buffer-undo-list)
89       ;; skip one undo boundary and all point setting commands up
90       ;; until the next undo boundary and try again.
91       (let ((p buffer-undo-list))
92         (and (null (car-safe p)) (setq p (cdr-safe p)))
93         (while (and p (integerp (car-safe p)))
94           (setq p (cdr-safe p)))
95         (eq last-buffer-undo-list p))
96       (error "Buffer modified since last undo/redo, cannot redo"))
97   (and (or (eq buffer-undo-list pending-undo-list)
98            (eq (cdr buffer-undo-list) pending-undo-list))
99        (error "No further undos to redo in this buffer"))
100   (or (eq (selected-window) (minibuffer-window))
101       (message "Redo..."))
102   (let ((modified (buffer-modified-p))
103         (undo-in-progress t)
104         (recent-save (recent-auto-save-p))
105         (old-undo-list buffer-undo-list)
106         (p (cdr buffer-undo-list))
107         (records-between 0))
108     ;; count the number of undo records between the head of the
109     ;; undo chain and the pointer to the next change.  Note that
110     ;; by `record' we mean clumps of change records, not the
111     ;; boundary records.  The number of records will always be a
112     ;; multiple of 2, because an undo moves the pending pointer
113     ;; forward one record and prepend a record to the head of the
114     ;; chain.  Thus the separation always increases by two.  When
115     ;; we decrease it we will decrease it by a multiple of 2
116     ;; also.
117     (while p
118       (cond ((eq p pending-undo-list)
119              (setq p nil))
120             ((null (car p))
121              (setq records-between (1+ records-between))
122              (setq p (cdr p)))
123             (t
124              (setq p (cdr p)))))
125     ;; we're off by one if pending pointer is nil, because there
126     ;; was no boundary record in front of it to count.
127     (and (null pending-undo-list)
128          (setq records-between (1+ records-between)))
129     ;; don't allow the user to redo more undos than exist.
130     ;; only half the records between the list head and the pending
131     ;; pointer are undos that are a part of this command chain.
132     (setq count (min (/ records-between 2) count)
133           p (primitive-undo (1+ count) buffer-undo-list))
134     (if (eq p old-undo-list)
135         nil ;; nothing happened
136       ;; set buffer-undo-list to the new undo list.  if has been
137       ;; shortened by `count' records.
138       (setq buffer-undo-list p)
139       ;; primitive-undo returns a list without a leading undo
140       ;; boundary.  add one.
141       (undo-boundary)
142       ;; now move the pending pointer backward in the undo list
143       ;; to reflect the redo.  sure would be nice if this list
144       ;; were doubly linked, but no... so we have to run down the
145       ;; list from the head and stop at the right place.
146       (let ((n (- records-between count)))
147         (setq p (cdr old-undo-list))
148         (while (and p (> n 0))
149           (if (null (car p))
150               (setq n (1- n)))
151           (setq p (cdr p)))
152         (setq pending-undo-list p)))
153     (and modified (not (buffer-modified-p))
154          (delete-auto-save-file-if-necessary recent-save))
155     (or (eq (selected-window) (minibuffer-window))
156         (message "Redo!"))
157     (setq last-buffer-undo-list buffer-undo-list)))
158
159 (defun undo (&optional arg)
160   "Undo some previous changes.
161 Repeat this command to undo more changes.
162 A numeric argument serves as a repeat count."
163   (interactive "*p")
164   (let ((modified (buffer-modified-p))
165         (recent-save (recent-auto-save-p)))
166     (or (eq (selected-window) (minibuffer-window))
167         (message "Undo..."))
168     (or (eq last-buffer-undo-list buffer-undo-list)
169         ;; skip one undo boundary and all point setting commands up
170         ;; until the next undo boundary and try again.
171         (let ((p buffer-undo-list))
172           (and (null (car-safe p)) (setq p (cdr-safe p)))
173           (while (and p (integerp (car-safe p)))
174             (setq p (cdr-safe p)))
175           (eq last-buffer-undo-list p))
176         (progn (undo-start)
177                (undo-more 1)))
178     (undo-more (or arg 1))
179     ;; Don't specify a position in the undo record for the undo command.
180     ;; Instead, undoing this should move point to where the change is.
181     ;;
182     ;;;; The old code for this was mad!  It deleted all set-point
183     ;;;; references to the position from the whole undo list,
184     ;;;; instead of just the cells from the beginning to the next
185     ;;;; undo boundary.  This does what I think the other code
186     ;;;; meant to do.
187     (let ((list buffer-undo-list)
188           (prev nil))
189       (while (and list (not (null (car list))))
190         (if (integerp (car list))
191             (if prev
192                 (setcdr prev (cdr list))
193               ;; impossible now, but maybe not in the future 
194               (setq buffer-undo-list (cdr list))))
195         (setq prev list
196               list (cdr list))))
197     (and modified (not (buffer-modified-p))
198          (delete-auto-save-file-if-necessary recent-save)))
199   (or (eq (selected-window) (minibuffer-window))
200       (message "Undo!"))
201   (setq last-buffer-undo-list buffer-undo-list))
202
203 ;;; redo.el ends here