Mercurial > emacs
comparison lisp/emacs-lisp/ewoc.el @ 70557:84297e870585
(ewoc--dll-create, ewoc--node-delete)
(ewoc--node-enter-first, ewoc--node-enter-last)
(ewoc--delete-node-internal): Merge funcs into unique callers.
author | Thien-Thi Nguyen <ttn@gnuvola.org> |
---|---|
date | Wed, 10 May 2006 08:00:33 +0000 |
parents | bc300a06815f |
children | f2d7dbf603c5 146cd8369025 |
comparison
equal
deleted
inserted
replaced
70556:fa894e85781d | 70557:84297e870585 |
---|---|
142 (defalias 'ewoc--node-branch 'aref | 142 (defalias 'ewoc--node-branch 'aref |
143 "Get the left (CHILD=0) or right (CHILD=1) child of the NODE. | 143 "Get the left (CHILD=0) or right (CHILD=1) child of the NODE. |
144 | 144 |
145 \(fn NODE CHILD)") | 145 \(fn NODE CHILD)") |
146 | 146 |
147 (defun ewoc--dll-create () | |
148 "Create an empty doubly linked list." | |
149 (let ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST))) | |
150 (setf (ewoc--node-right dummy-node) dummy-node) | |
151 (setf (ewoc--node-left dummy-node) dummy-node) | |
152 dummy-node)) | |
153 | |
154 (defun ewoc--node-enter-before (node elemnode) | 147 (defun ewoc--node-enter-before (node elemnode) |
155 "Insert ELEMNODE before NODE in a DLL." | 148 "Insert ELEMNODE before NODE in a DLL." |
156 (assert (and (null (ewoc--node-left elemnode)) (null (ewoc--node-right elemnode)))) | 149 (assert (and (null (ewoc--node-left elemnode)) (null (ewoc--node-right elemnode)))) |
157 (setf (ewoc--node-left elemnode) (ewoc--node-left node)) | 150 (setf (ewoc--node-left elemnode) (ewoc--node-left node)) |
158 (setf (ewoc--node-right elemnode) node) | 151 (setf (ewoc--node-right elemnode) node) |
159 (setf (ewoc--node-right (ewoc--node-left node)) elemnode) | 152 (setf (ewoc--node-right (ewoc--node-left node)) elemnode) |
160 (setf (ewoc--node-left node) elemnode)) | 153 (setf (ewoc--node-left node) elemnode)) |
161 | 154 |
162 (defun ewoc--node-enter-first (dll node) | |
163 "Add a free floating NODE first in DLL." | |
164 (ewoc--node-enter-before (ewoc--node-right dll) node)) | |
165 | |
166 (defun ewoc--node-enter-last (dll node) | |
167 "Add a free floating NODE last in DLL." | |
168 (ewoc--node-enter-before dll node)) | |
169 | |
170 (defun ewoc--node-next (dll node) | 155 (defun ewoc--node-next (dll node) |
171 "Return the node after NODE, or nil if NODE is the last node." | 156 "Return the node after NODE, or nil if NODE is the last node." |
172 (unless (eq (ewoc--node-right node) dll) (ewoc--node-right node))) | 157 (unless (eq (ewoc--node-right node) dll) (ewoc--node-right node))) |
173 | 158 |
174 (defun ewoc--node-prev (dll node) | 159 (defun ewoc--node-prev (dll node) |
175 "Return the node before NODE, or nil if NODE is the first node." | 160 "Return the node before NODE, or nil if NODE is the first node." |
176 (unless (eq (ewoc--node-left node) dll) (ewoc--node-left node))) | 161 (unless (eq (ewoc--node-left node) dll) (ewoc--node-left node))) |
177 | |
178 (defun ewoc--node-delete (node) | |
179 "Unbind NODE from its doubly linked list and return it." | |
180 ;; This is a no-op when applied to the dummy node. This will return | |
181 ;; nil if applied to the dummy node since it always contains nil. | |
182 (setf (ewoc--node-right (ewoc--node-left node)) (ewoc--node-right node)) | |
183 (setf (ewoc--node-left (ewoc--node-right node)) (ewoc--node-left node)) | |
184 (setf (ewoc--node-left node) nil) | |
185 (setf (ewoc--node-right node) nil) | |
186 node) | |
187 | 162 |
188 (defun ewoc--node-nth (dll n) | 163 (defun ewoc--node-nth (dll n) |
189 "Return the Nth node from the doubly linked list DLL. | 164 "Return the Nth node from the doubly linked list DLL. |
190 N counts from zero. If DLL is not that long, nil is returned. | 165 N counts from zero. If DLL is not that long, nil is returned. |
191 If N is negative, return the -(N+1)th last element. | 166 If N is negative, return the -(N+1)th last element. |
255 ;; Move back, and call the pretty-printer. | 230 ;; Move back, and call the pretty-printer. |
256 (backward-char 1) | 231 (backward-char 1) |
257 (funcall pretty-printer data) | 232 (funcall pretty-printer data) |
258 (ewoc--node-create (copy-marker pos) data)))) | 233 (ewoc--node-create (copy-marker pos) data)))) |
259 | 234 |
260 | |
261 (defun ewoc--delete-node-internal (ewoc node) | |
262 "Delete a data string from EWOC. | |
263 Can not be used on the footer. Return the wrapper that is deleted. | |
264 The start-marker in the wrapper is set to nil, so that it doesn't | |
265 consume any more resources." | |
266 (let ((dll (ewoc--dll ewoc)) | |
267 (inhibit-read-only t)) | |
268 ;; If we are about to delete the node pointed at by last-node, | |
269 ;; set last-node to nil. | |
270 (if (eq (ewoc--last-node ewoc) node) | |
271 (setf (ewoc--last-node ewoc) nil)) | |
272 | |
273 (delete-region (ewoc--node-start-marker node) | |
274 (ewoc--node-start-marker (ewoc--node-next dll node))) | |
275 (set-marker (ewoc--node-start-marker node) nil) | |
276 ;; Delete the node, and return the wrapper. | |
277 (ewoc--node-delete node))) | |
278 | |
279 | |
280 (defun ewoc--refresh-node (pp node) | 235 (defun ewoc--refresh-node (pp node) |
281 "Redisplay the element represented by NODE using the pretty-printer PP." | 236 "Redisplay the element represented by NODE using the pretty-printer PP." |
282 (let ((inhibit-read-only t)) | 237 (let ((inhibit-read-only t)) |
283 (save-excursion | 238 (save-excursion |
284 ;; First, remove the string from the buffer: | 239 ;; First, remove the string from the buffer: |
307 | 262 |
308 Optional second argument HEADER is a string that will always be | 263 Optional second argument HEADER is a string that will always be |
309 present at the top of the ewoc. HEADER should end with a | 264 present at the top of the ewoc. HEADER should end with a |
310 newline. Optional third argument FOOTER is similar, and will | 265 newline. Optional third argument FOOTER is similar, and will |
311 be inserted at the bottom of the ewoc." | 266 be inserted at the bottom of the ewoc." |
312 (let ((new-ewoc | 267 (let* ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST)) |
313 (ewoc--create (current-buffer) | 268 (dll (progn (setf (ewoc--node-right dummy-node) dummy-node) |
314 pretty-printer nil nil (ewoc--dll-create))) | 269 (setf (ewoc--node-left dummy-node) dummy-node) |
315 (pos (point))) | 270 dummy-node)) |
271 (new-ewoc | |
272 (ewoc--create (current-buffer) | |
273 pretty-printer nil nil dll)) | |
274 (pos (point))) | |
316 (ewoc--set-buffer-bind-dll new-ewoc | 275 (ewoc--set-buffer-bind-dll new-ewoc |
317 ;; Set default values | 276 ;; Set default values |
318 (unless header (setq header "")) | 277 (unless header (setq header "")) |
319 (unless footer (setq footer "")) | 278 (unless footer (setq footer "")) |
320 (setf (ewoc--node-start-marker dll) (copy-marker pos)) | 279 (setf (ewoc--node-start-marker dll) (copy-marker pos)) |
321 (let ((foot (ewoc--create-node footer 'insert pos)) | 280 (let ((foot (ewoc--create-node footer 'insert pos)) |
322 (head (ewoc--create-node header 'insert pos))) | 281 (head (ewoc--create-node header 'insert pos))) |
323 (ewoc--node-enter-first dll head) | 282 (ewoc--node-enter-before (ewoc--node-right dll) head) |
324 (ewoc--node-enter-last dll foot) | 283 (ewoc--node-enter-before dll foot) |
325 (setf (ewoc--header new-ewoc) head) | 284 (setf (ewoc--header new-ewoc) head) |
326 (setf (ewoc--footer new-ewoc) foot))) | 285 (setf (ewoc--footer new-ewoc) foot))) |
327 ;; Return the ewoc | 286 ;; Return the ewoc |
328 new-ewoc)) | 287 new-ewoc)) |
329 | 288 |
415 The PREDICATE is called with the element as its first argument. If any | 374 The PREDICATE is called with the element as its first argument. If any |
416 ARGS are given they will be passed to the PREDICATE." | 375 ARGS are given they will be passed to the PREDICATE." |
417 (ewoc--set-buffer-bind-dll-let* ewoc | 376 (ewoc--set-buffer-bind-dll-let* ewoc |
418 ((node (ewoc--node-nth dll 1)) | 377 ((node (ewoc--node-nth dll 1)) |
419 (footer (ewoc--footer ewoc)) | 378 (footer (ewoc--footer ewoc)) |
420 (next nil)) | 379 (next nil) |
380 (L nil) (R nil) | |
381 (inhibit-read-only t)) | |
421 (while (not (eq node footer)) | 382 (while (not (eq node footer)) |
422 (setq next (ewoc--node-next dll node)) | 383 (setq next (ewoc--node-next dll node)) |
423 (unless (apply predicate (ewoc--node-data node) args) | 384 (unless (apply predicate (ewoc--node-data node) args) |
424 (ewoc--delete-node-internal ewoc node)) | 385 ;; If we are about to delete the node pointed at by last-node, |
386 ;; set last-node to nil. | |
387 (if (eq (ewoc--last-node ewoc) node) | |
388 (setf (ewoc--last-node ewoc) nil)) | |
389 (delete-region (ewoc--node-start-marker node) | |
390 (ewoc--node-start-marker (ewoc--node-next dll node))) | |
391 (set-marker (ewoc--node-start-marker node) nil) | |
392 (setf L (ewoc--node-left node) | |
393 R (ewoc--node-right node) | |
394 ;; Link neighbors to each other. | |
395 (ewoc--node-right L) R | |
396 (ewoc--node-left R) L | |
397 ;; Forget neighbors. | |
398 (ewoc--node-left node) nil | |
399 (ewoc--node-right node) nil)) | |
425 (setq node next)))) | 400 (setq node next)))) |
426 | 401 |
427 (defun ewoc-locate (ewoc &optional pos guess) | 402 (defun ewoc-locate (ewoc &optional pos guess) |
428 "Return the node that POS (a buffer position) is within. | 403 "Return the node that POS (a buffer position) is within. |
429 POS may be a marker or an integer. It defaults to point. | 404 POS may be a marker or an integer. It defaults to point. |