annotate lisp/prim/undo-stack.el @ 2:ac2d302a0011 r19-15b2

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