annotate lisp/prim/undo-stack.el @ 126:1370575f1259 xemacs-20-1p1

Import from CVS: tag xemacs-20-1p1
author cvs
date Mon, 13 Aug 2007 09:27:39 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; undo-stack.el --- An "undoable stack" object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Copyright (C) 1996 Ben Wing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; You should have received a copy of the GNU General Public License
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
19 ;; along with XEmacs; see the file COPYING. If not, write to the
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 16
diff changeset
20 ;; Free Software Foundation, 59 Temple Place - Suite 330,
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
21 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; An "undoable stack" is an object that can be used to implement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; a history of positions, with undo and redo. Conceptually, it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; is the kind of data structure used to keep track of (e.g.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; visited Web pages, so that the "Back" and "Forward" operations
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; in the browser work. Basically, I can successively visit a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; number of Web pages through links, and then hit "Back" a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; few times to go to previous positions, and then "Forward" a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; few times to reverse this process. This is similar to an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; "undo" and "redo" mechanism.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; Note that Emacs does not standardly contain structures like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; this. Instead, it implements history using either a ring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; (the kill ring, the mark ring), or something like the undo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; stack, where successive "undo" operations get recorded as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;; normal modifications, so that if you do a bunch of successive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;; undo's, then something else, then start undoing, you will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; be redoing all your undo's back to the point before you did
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; the undo's, and then further undo's will act like the previous
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; round of undo's. I think that both of these paradigms are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;;; inferior to the "undoable-stack" paradigm because they're
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;;; confusing and difficult to keep track of.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;;; Conceptually, imagine a position history like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;;; 1 -> 2 -> 3 -> 4 -> 5 -> 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;;; ^^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;;; where the arrow indicates where you currently are. "Going back"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;;; and "going forward" just amount to moving the arrow. However,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;;; what happens if the history state is this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;;; 1 -> 2 -> 3 -> 4 -> 5 -> 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;;; ^^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ;;; and then I visit new positions (7) and (8)? In the most general
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;;; implementation, you've just caused a new branch like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;;; 1 -> 2 -> 3 -> 4 -> 5 -> 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;;; |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;;; |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;;; 7 -> 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;;; ^^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;;; But then you can end up with a whole big tree, and you need
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ;;; more sophisticated ways of navigating ("Forward" might involve
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;;; a choice of paths to follow) and managing its size (if you don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;;; want to keep unlimited history, you have to truncate at some point,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;;; and how do you truncate a tree?)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;;; My solution to this is just to insert the new positions like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;;; this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;;; 1 -> 2 -> 3 -> 4 -> 7 -> 8 -> 5 -> 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;;; ^^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;;; (Netscape, I think, would just truncate 5 and 6 completely,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;;; but that seems a bit drastic. In the Emacs-standard "ring"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;;; structure, this problem is avoided by simply moving 5 and 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;;; to the beginning of the ring. However, it doesn't seem
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;;; logical to me to have "going back past 1" get you to 6.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;;; Now what if we have a "maximum" size of (say) 7 elements?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 ;;; When we add 8, we could truncate either 1 or 6. Since 5 and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ;;; 6 are "undone" positions, we should presumably truncate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 ;;; them before 1. So, adding 8 truncates 6, adding 9 truncates
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 ;;; 5, and adding 10 truncates 1 because there is nothing more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 ;;; that is forward of the insertion point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ;;; Interestingly, this method of truncation is almost like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ;;; how a ring would truncate. A ring would move 5 and 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;;; around to the back, like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;;; 5 -> 6 -> 1 -> 2 -> 3 -> 4 -> 7 -> 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;;; ^^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;;; However, when 8 is added, the ring truncates 5 instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;;; 6, which is less than optimal.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 ;;; Conceptually, we can implement the "undoable stack" using
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ;;; two stacks of a sort called "truncatable stack", which are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ;;; just simple stacks, but where you can truncate elements
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
108 ;;; off of the bottom of the stack. Then, the undoable stack
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ;;; 1 -> 2 -> 3 -> 4 -> 5 -> 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ;;; ^^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;;; is equivalent to two truncatable stacks:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ;;; 4 <- 3 <- 2 <- 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ;;; 5 <- 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ;;; where I reversed the direction to accord with the probable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 ;;; implementation of a standard list. To do another undo,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;;; I pop 4 off of the first stack and move it to the top of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;;; the second stack. A redo operation does the opposite.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 ;;; To truncate to the proper size, first chop off 6, then 5,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 ;;; then 1 -- in all cases, truncating off the bottom.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (define-error 'trunc-stack-bottom "Bottom of stack reached.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (defsubst trunc-stack-stack (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 ;; return the list representing the trunc-stack's elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;; the head of the list is the most recent element.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (aref stack 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (defsubst trunc-stack-length (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 ;; return the number of elements in the trunc-stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (aref stack 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (defsubst set-trunc-stack-stack (stack new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ;; set the list representing the trunc-stack's elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (aset stack 1 new))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (defsubst set-trunc-stack-length (stack new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ;; set the length of the trunc-stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (aset stack 2 new))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 ;; public functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (defun make-trunc-stack ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 ;; make an empty trunc-stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (vector 'trunc-stack nil 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (defun trunc-stack-push (stack el)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; push a new element onto the head of the trunc-stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (set-trunc-stack-stack stack (cons el (trunc-stack-stack stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (set-trunc-stack-length stack (1+ (trunc-stack-length stack))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (defun trunc-stack-top (stack &optional n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 ;; return the nth topmost element from the trunc-stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 ;; signal an error if the stack doesn't have that many elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (or n (setq n 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (if (>= n (trunc-stack-length stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (signal-error 'trunc-stack-bottom (list stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (nth n (trunc-stack-stack stack))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (defun trunc-stack-pop (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ;; pop and return the topmost element from the stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (prog1 (trunc-stack-top stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (set-trunc-stack-stack stack (cdr (trunc-stack-stack stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (set-trunc-stack-length stack (1- (trunc-stack-length stack)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (defun trunc-stack-truncate (stack &optional n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 ;; truncate N items off the bottom of the stack. If the stack is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ;; not that big, it just becomes empty.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (or n (setq n 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (if (> n 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (let ((len (trunc-stack-length stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (if (>= n len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (set-trunc-stack-length stack 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (set-trunc-stack-stack stack nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (setcdr (nthcdr (1- (- len n)) (trunc-stack-stack stack)) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (set-trunc-stack-length stack (- len n))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ;;; FMH! FMH! FMH! This object-oriented stuff doesn't really work
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 ;;; properly without built-in structures (vectors suck) and without
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 ;;; public and private functions and fields. Bogons descend on
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 ;;; RMS for not believing in any of this.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (defsubst undoable-stack-max (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (aref stack 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (defsubst undoable-stack-a (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (aref stack 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (defsubst undoable-stack-b (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (aref stack 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 ;; public functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (defun make-undoable-stack (max)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 ;; make an empty undoable stack of max size MAX.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (vector 'undoable-stack max (make-trunc-stack) (make-trunc-stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (defsubst set-undoable-stack-max (stack new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 ;; change the max size of an undoable stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (aset stack 1 new))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (defun undoable-stack-a-top (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 ;; return the topmost element off the "A" stack of an undoable stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 ;; this is the most recent position pushed on the undoable stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (trunc-stack-top (undoable-stack-a stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (defun undoable-stack-a-length (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (trunc-stack-length (undoable-stack-a stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (defun undoable-stack-b-top (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 ;; return the topmost element off the "B" stack of an undoable stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;; this is the position that will become the most recent position,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 ;; after a redo operation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (trunc-stack-top (undoable-stack-b stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (defun undoable-stack-b-length (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (trunc-stack-length (undoable-stack-b stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (defun undoable-stack-push (stack el)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 ;; push an element onto the stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (let*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 ((lena (trunc-stack-length (undoable-stack-a stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (lenb (trunc-stack-length (undoable-stack-b stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (max (undoable-stack-max stack))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (len (+ lena lenb)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 ;; maybe truncate some elements. We have to deal with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 ;; possibility that we have more elements than our max
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 ;; (someone might have reduced the max).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (if (>= len max)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (let ((must-nuke (1+ (- len max))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 ;; chop off must-nuke elements from the B stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (trunc-stack-truncate (undoable-stack-b stack) must-nuke)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 ;; but if there weren't that many elements to chop,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;; take the rest off the A stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (if (< lenb must-nuke)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (trunc-stack-truncate (undoable-stack-a stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (- must-nuke lenb)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (trunc-stack-push (undoable-stack-a stack) el)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (defun undoable-stack-pop (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ;; pop an element off the stack.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (trunc-stack-pop (undoable-stack-a stack)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (defun undoable-stack-undo (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ;; transfer an element from the top of A to the top of B.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 ;; return value is undefined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (trunc-stack-push (undoable-stack-b stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (trunc-stack-pop (undoable-stack-a stack))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (defun undoable-stack-redo (stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 ;; transfer an element from the top of B to the top of A.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 ;; return value is undefined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (trunc-stack-push (undoable-stack-a stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (trunc-stack-pop (undoable-stack-b stack))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264