1 ;;; ewoc.el --- utility to maintain a view of a list of objects in a buffer
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Per Cederqvist <ceder@lysator.liu.se>
7 ;; Inge Wallin <inge@lysator.liu.se>
8 ;; Maintainer: monnier@gnu.org
10 ;; Keywords: extensions, lisp
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Ewoc Was Once Cookie
30 ;; But now it's Emacs' Widget for Object Collections
32 ;; As the name implies this derives from the `cookie' package (part
33 ;; of Elib). The changes are pervasive though mostly superficial:
35 ;; - uses CL (and its `defstruct')
36 ;; - separate from Elib.
37 ;; - uses its own version of a doubly-linked list which allows us
38 ;; to merge the elib-wrapper and the elib-node structures into ewoc-node
39 ;; - dropping functions not used by PCL-CVS (the only client of ewoc at the
41 ;; - removing unused arguments
43 ;; elib-node ==> ewoc--node
44 ;; collection ==> ewoc
46 ;; cookie ==> data or element or elem
51 ;; Ewoc is a package that implements a connection between an
52 ;; dll (a doubly linked list) and the contents of a buffer.
53 ;; Possible uses are dired (have all files in a list, and show them),
54 ;; buffer-list, kom-prioritize (in the LysKOM elisp client) and
55 ;; others. pcl-cvs.el and vc.el use ewoc.el.
57 ;; Ewoc can be considered as the `view' part of a model-view-controller.
59 ;; A `element' can be any lisp object. When you use the ewoc
60 ;; package you specify a pretty-printer, a function that inserts
61 ;; a printable representation of the element in the buffer. (The
62 ;; pretty-printer should use "insert" and not
63 ;; "insert-before-markers").
65 ;; A `ewoc' consists of a doubly linked list of elements, a
66 ;; header, a footer and a pretty-printer. It is displayed at a
67 ;; certain point in a certain buffer. (The buffer and point are
68 ;; fixed when the ewoc is created). The header and the footer
69 ;; are constant strings. They appear before and after the elements.
71 ;; Ewoc does not affect the mode of the buffer in any way. It
72 ;; merely makes it easy to connect an underlying data representation
73 ;; to the buffer contents.
75 ;; A `ewoc--node' is an object that contains one element. There are
76 ;; functions in this package that given an ewoc--node extract the data, or
77 ;; give the next or previous ewoc--node. (All ewoc--nodes are linked together
78 ;; in a doubly linked list. The `previous' ewoc--node is the one that appears
79 ;; before the other in the buffer.) You should not do anything with
80 ;; an ewoc--node except pass it to the functions in this package.
82 ;; An ewoc is a very dynamic thing. You can easily add or delete elements.
83 ;; You can apply a function to all elements in an ewoc, etc, etc.
85 ;; Remember that an element can be anything. Your imagination is the
86 ;; limit! It is even possible to have another ewoc as an
87 ;; element. In that way some kind of tree hierarchy can be created.
89 ;; The Emacs Lisp Reference Manual documents ewoc.el's "public interface".
94 ;; All functions of course start with `ewoc'. Functions and macros
95 ;; starting with the prefix `ewoc--' are meant for internal use,
96 ;; while those starting with `ewoc-' are exported for public use.
100 (eval-when-compile (require 'cl
))
102 ;; The doubly linked list is implemented as a circular list with a dummy
103 ;; node first and last. The dummy node is used as "the dll".
104 (defstruct (ewoc--node
105 (:type vector
) ;ewoc--node-nth needs this
107 (:constructor ewoc--node-create
(start-marker data
)))
108 left right data start-marker
)
110 (defun ewoc--node-next (dll node
)
111 "Return the node after NODE, or nil if NODE is the last node."
112 (let ((R (ewoc--node-right node
)))
113 (unless (eq dll R
) R
)))
115 (defun ewoc--node-prev (dll node
)
116 "Return the node before NODE, or nil if NODE is the first node."
117 (let ((L (ewoc--node-left node
)))
118 (unless (eq dll L
) L
)))
120 (defun ewoc--node-nth (dll n
)
121 "Return the Nth node from the doubly linked list `dll'.
122 N counts from zero. If N is negative, return the -(N+1)th last element.
123 If N is out of range, return nil.
124 Thus, (ewoc--node-nth dll 0) returns the first node,
125 and (ewoc--node-nth dll -1) returns the last node."
126 ;; Presuming a node is ":type vector", starting with `left' and `right':
127 ;; Branch 0 ("follow left pointer") is used when n is negative.
128 ;; Branch 1 ("follow right pointer") is used otherwise.
129 (let* ((branch (if (< n
0) 0 1))
130 (node (aref dll branch
)))
131 (if (< n
0) (setq n
(- -
1 n
)))
132 (while (and (not (eq dll node
)) (> n
0))
133 (setq node
(aref node branch
))
135 (unless (eq dll node
) node
)))
137 (defun ewoc-location (node)
138 "Return the start location of NODE."
139 (ewoc--node-start-marker node
))
142 ;;; The ewoc data type
146 (:constructor ewoc--create
(buffer pretty-printer dll
))
148 buffer pretty-printer header footer dll last-node hf-pp
)
150 (defmacro ewoc--set-buffer-bind-dll-let
* (ewoc varlist
&rest forms
)
151 "Execute FORMS with ewoc--buffer selected as current buffer,
152 `dll' bound to the dll, and VARLIST bound as in a let*.
153 `dll' will be bound when VARLIST is initialized, but
154 the current buffer will *not* have been changed.
155 Return value of last form in FORMS."
156 (let ((hnd (make-symbol "ewoc")))
158 (dll (ewoc--dll ,hnd
))
160 (with-current-buffer (ewoc--buffer ,hnd
)
163 (defmacro ewoc--set-buffer-bind-dll
(ewoc &rest forms
)
164 `(ewoc--set-buffer-bind-dll-let* ,ewoc nil
,@forms
))
166 (defsubst ewoc--filter-hf-nodes
(ewoc node
)
167 "Evaluate NODE once and return it.
168 BUT if it is the header or the footer in EWOC return nil instead."
169 (unless (or (eq node
(ewoc--header ewoc
))
170 (eq node
(ewoc--footer ewoc
)))
173 (defun ewoc--adjust (beg end node dll
)
174 ;; "Manually reseat" markers for NODE and its successors (including footer
175 ;; and dll), in the case where they originally shared start position with
176 ;; BEG, to END. BEG and END are buffer positions describing NODE's left
177 ;; neighbor. This operation is functionally equivalent to temporarily
178 ;; setting these nodes' markers' insertion type to t around the pretty-print
179 ;; call that precedes the call to `ewoc--adjust', and then changing them back
183 (while (and (= beg
(setq m
(ewoc--node-start-marker node
)))
184 ;; The "dummy" node `dll' actually holds the marker that
185 ;; points to the end of the footer, so we check `dll'
186 ;; *after* reseating the marker.
189 (not (eq dll node
))))
190 (setq node
(ewoc--node-right node
))))))
192 (defun ewoc--insert-new-node (node data pretty-printer dll
)
193 "Insert before NODE a new node for DATA, displayed by PRETTY-PRINTER.
194 Fourth arg DLL -- from `(ewoc--dll EWOC)' -- is for internal purposes.
195 Call PRETTY-PRINTER with point at NODE's start, thus pushing back
196 NODE and leaving the new node's start there. Return the new node."
198 (let ((elemnode (ewoc--node-create
199 (copy-marker (ewoc--node-start-marker node
)) data
)))
200 (setf (ewoc--node-left elemnode
) (ewoc--node-left node
)
201 (ewoc--node-right elemnode
) node
202 (ewoc--node-right (ewoc--node-left node
)) elemnode
203 (ewoc--node-left node
) elemnode
)
204 (ewoc--refresh-node pretty-printer elemnode dll
)
207 (defun ewoc--refresh-node (pp node dll
)
208 "Redisplay the element represented by NODE using the pretty-printer PP."
209 (let ((inhibit-read-only t
)
210 (m (ewoc--node-start-marker node
))
211 (R (ewoc--node-right node
)))
212 ;; First, remove the string from the buffer:
213 (delete-region m
(ewoc--node-start-marker R
))
214 ;; Calculate and insert the string.
216 (funcall pp
(ewoc--node-data node
))
217 (ewoc--adjust m
(point) R dll
)))
219 (defun ewoc--wrap (func)
220 (lexical-let ((ewoc--user-pp func
))
222 (funcall ewoc--user-pp data
)
226 ;;; ===========================================================================
227 ;;; Public members of the Ewoc package
230 (defun ewoc-create (pretty-printer &optional header footer nosep
)
231 "Create an empty ewoc.
233 The ewoc will be inserted in the current buffer at the current position.
235 PRETTY-PRINTER should be a function that takes one argument, an
236 element, and inserts a string representing it in the buffer (at
237 point). The string PRETTY-PRINTER inserts may be empty or span
238 several lines. The PRETTY-PRINTER should use `insert', and not
239 `insert-before-markers'.
241 Optional second and third arguments HEADER and FOOTER are strings,
242 possibly empty, that will always be present at the top and bottom,
243 respectively, of the ewoc.
245 Normally, a newline is automatically inserted after the header,
246 the footer and every node's printed representation. Optional
247 fourth arg NOSEP non-nil inhibits this."
248 (let* ((dummy-node (ewoc--node-create 'DL-LIST
'DL-LIST
))
249 (dll (progn (setf (ewoc--node-right dummy-node
) dummy-node
)
250 (setf (ewoc--node-left dummy-node
) dummy-node
)
252 (wrap (if nosep
'identity
'ewoc--wrap
))
253 (new-ewoc (ewoc--create (current-buffer)
254 (funcall wrap pretty-printer
)
256 (hf-pp (funcall wrap
'insert
))
259 (ewoc--set-buffer-bind-dll new-ewoc
260 ;; Set default values
261 (unless header
(setq header
""))
262 (unless footer
(setq footer
""))
263 (setf (ewoc--node-start-marker dll
) (copy-marker pos
)
264 foot
(ewoc--insert-new-node dll footer hf-pp dll
)
265 head
(ewoc--insert-new-node foot header hf-pp dll
)
266 (ewoc--hf-pp new-ewoc
) hf-pp
267 (ewoc--footer new-ewoc
) foot
268 (ewoc--header new-ewoc
) head
))
272 (defalias 'ewoc-data
'ewoc--node-data
273 "Extract the data encapsulated by NODE and return it.
277 (defun ewoc-set-data (node data
)
278 "Set NODE to encapsulate DATA."
279 (setf (ewoc--node-data node
) data
))
281 (defun ewoc-enter-first (ewoc data
)
282 "Enter DATA first in EWOC.
283 Return the new node."
284 (ewoc--set-buffer-bind-dll ewoc
285 (ewoc-enter-after ewoc
(ewoc--node-nth dll
0) data
)))
287 (defun ewoc-enter-last (ewoc data
)
288 "Enter DATA last in EWOC.
289 Return the new node."
290 (ewoc--set-buffer-bind-dll ewoc
291 (ewoc-enter-before ewoc
(ewoc--node-nth dll -
1) data
)))
293 (defun ewoc-enter-after (ewoc node data
)
294 "Enter a new element DATA after NODE in EWOC.
295 Return the new node."
296 (ewoc--set-buffer-bind-dll ewoc
297 (ewoc-enter-before ewoc
(ewoc--node-next dll node
) data
)))
299 (defun ewoc-enter-before (ewoc node data
)
300 "Enter a new element DATA before NODE in EWOC.
301 Return the new node."
302 (ewoc--set-buffer-bind-dll ewoc
303 (ewoc--insert-new-node node data
(ewoc--pretty-printer ewoc
) dll
)))
305 (defun ewoc-next (ewoc node
)
306 "Return the node in EWOC that follows NODE.
307 Return nil if NODE is nil or the last element."
309 (ewoc--filter-hf-nodes
310 ewoc
(ewoc--node-next (ewoc--dll ewoc
) node
))))
312 (defun ewoc-prev (ewoc node
)
313 "Return the node in EWOC that precedes NODE.
314 Return nil if NODE is nil or the first element."
316 (ewoc--filter-hf-nodes
317 ewoc
(ewoc--node-prev (ewoc--dll ewoc
) node
))))
319 (defun ewoc-nth (ewoc n
)
320 "Return the Nth node.
321 N counts from zero. Return nil if there is less than N elements.
322 If N is negative, return the -(N+1)th last element.
323 Thus, (ewoc-nth ewoc 0) returns the first node,
324 and (ewoc-nth ewoc -1) returns the last node.
325 Use `ewoc-data' to extract the data from the node."
326 ;; Skip the header (or footer, if n is negative).
327 (setq n
(if (< n
0) (1- n
) (1+ n
)))
328 (ewoc--filter-hf-nodes ewoc
329 (ewoc--node-nth (ewoc--dll ewoc
) n
)))
331 (defun ewoc-map (map-function ewoc
&rest args
)
332 "Apply MAP-FUNCTION to all elements in EWOC.
333 MAP-FUNCTION is applied to the first element first.
334 If MAP-FUNCTION returns non-nil the element will be refreshed (its
335 pretty-printer will be called once again).
337 Note that the buffer for EWOC will be the current buffer when
338 MAP-FUNCTION is called. MAP-FUNCTION must restore the current
339 buffer before it returns, if it changes it.
341 If more than two arguments are given, the remaining
342 arguments will be passed to MAP-FUNCTION."
343 (ewoc--set-buffer-bind-dll-let* ewoc
344 ((footer (ewoc--footer ewoc
))
345 (pp (ewoc--pretty-printer ewoc
))
346 (node (ewoc--node-nth dll
1)))
348 (while (not (eq node footer
))
349 (if (apply map-function
(ewoc--node-data node
) args
)
350 (ewoc--refresh-node pp node dll
))
351 (setq node
(ewoc--node-next dll node
))))))
353 (defun ewoc-delete (ewoc &rest nodes
)
354 "Delete NODES from EWOC."
355 (ewoc--set-buffer-bind-dll-let* ewoc
356 ((L nil
) (R nil
) (last (ewoc--last-node ewoc
)))
358 ;; If we are about to delete the node pointed at by last-node,
359 ;; set last-node to nil.
361 (setf last nil
(ewoc--last-node ewoc
) nil
))
362 (delete-region (ewoc--node-start-marker node
)
363 (ewoc--node-start-marker (ewoc--node-next dll node
)))
364 (set-marker (ewoc--node-start-marker node
) nil
)
365 (setf L
(ewoc--node-left node
)
366 R
(ewoc--node-right node
)
367 ;; Link neighbors to each other.
368 (ewoc--node-right L
) R
369 (ewoc--node-left R
) L
371 (ewoc--node-left node
) nil
372 (ewoc--node-right node
) nil
))))
374 (defun ewoc-filter (ewoc predicate
&rest args
)
375 "Remove all elements in EWOC for which PREDICATE returns nil.
376 Note that the buffer for EWOC will be current-buffer when PREDICATE
377 is called. PREDICATE must restore the current buffer before it returns
379 The PREDICATE is called with the element as its first argument. If any
380 ARGS are given they will be passed to the PREDICATE."
381 (ewoc--set-buffer-bind-dll-let* ewoc
382 ((node (ewoc--node-nth dll
1))
383 (footer (ewoc--footer ewoc
))
385 (inhibit-read-only t
))
386 (while (not (eq node footer
))
387 (unless (apply predicate
(ewoc--node-data node
) args
)
389 (setq node
(ewoc--node-next dll node
)))
390 (apply 'ewoc-delete ewoc goodbye
)))
392 (defun ewoc-locate (ewoc &optional pos guess
)
393 "Return the node that POS (a buffer position) is within.
394 POS may be a marker or an integer. It defaults to point.
395 GUESS should be a node that it is likely to be near POS.
397 If POS points before the first element, the first node is returned.
398 If POS points after the last element, the last node is returned.
399 If the EWOC is empty, nil is returned."
400 (unless pos
(setq pos
(point)))
401 (ewoc--set-buffer-bind-dll ewoc
405 ((eq (ewoc--node-nth dll
1) (ewoc--node-nth dll -
1))
408 ;; Before second elem?
409 ((< pos
(ewoc--node-start-marker (ewoc--node-nth dll
2)))
410 (ewoc--node-nth dll
1))
412 ;; After one-before-last elem?
413 ((>= pos
(ewoc--node-start-marker (ewoc--node-nth dll -
2)))
414 (ewoc--node-nth dll -
2))
416 ;; We now know that pos is within a elem.
418 ;; Make an educated guess about which of the three known
419 ;; node'es (the first, the last, or GUESS) is nearest.
420 (let* ((best-guess (ewoc--node-nth dll
1))
421 (distance (abs (- pos
(ewoc--node-start-marker best-guess
)))))
423 (let ((d (abs (- pos
(ewoc--node-start-marker guess
)))))
426 (setq best-guess guess
))))
428 (let* ((g (ewoc--node-nth dll -
1)) ;Check the last elem
429 (d (abs (- pos
(ewoc--node-start-marker g
)))))
432 (setq best-guess g
)))
434 (when (ewoc--last-node ewoc
) ;Check "previous".
435 (let* ((g (ewoc--last-node ewoc
))
436 (d (abs (- pos
(ewoc--node-start-marker g
)))))
439 (setq best-guess g
))))
441 ;; best-guess is now a "best guess".
442 ;; Find the correct node. First determine in which direction
443 ;; it lies, and then move in that direction until it is found.
446 ;; Is pos after the guess?
448 (ewoc--node-start-marker best-guess
))
449 ;; Loop until we are exactly one node too far down...
450 (while (>= pos
(ewoc--node-start-marker best-guess
))
451 (setq best-guess
(ewoc--node-next dll best-guess
)))
452 ;; ...and return the previous node.
453 (ewoc--node-prev dll best-guess
))
455 ;; Pos is before best-guess
457 (while (< pos
(ewoc--node-start-marker best-guess
))
458 (setq best-guess
(ewoc--node-prev dll best-guess
)))
461 (defun ewoc-invalidate (ewoc &rest nodes
)
462 "Call EWOC's pretty-printer for each element in NODES.
463 Delete current text first, thus effecting a \"refresh\"."
464 (ewoc--set-buffer-bind-dll-let* ewoc
465 ((pp (ewoc--pretty-printer ewoc
)))
468 (ewoc--refresh-node pp node dll
)))))
470 (defun ewoc-goto-prev (ewoc arg
)
471 "Move point to the ARGth previous element in EWOC.
472 Don't move if we are at the first element, or if EWOC is empty.
473 Return the node we moved to."
474 (ewoc--set-buffer-bind-dll-let* ewoc
475 ((node (ewoc-locate ewoc
(point))))
477 ;; If we were past the last element, first jump to it.
478 (when (>= (point) (ewoc--node-start-marker (ewoc--node-right node
)))
480 (while (and node
(> arg
0))
482 (setq node
(ewoc--node-prev dll node
)))
483 ;; Never step above the first element.
484 (unless (ewoc--filter-hf-nodes ewoc node
)
485 (setq node
(ewoc--node-nth dll
1)))
486 (ewoc-goto-node ewoc node
))))
488 (defun ewoc-goto-next (ewoc arg
)
489 "Move point to the ARGth next element in EWOC.
490 Return the node (or nil if we just passed the last node)."
491 (ewoc--set-buffer-bind-dll-let* ewoc
492 ((node (ewoc-locate ewoc
(point))))
493 (while (and node
(> arg
0))
495 (setq node
(ewoc--node-next dll node
)))
496 ;; Never step below the first element.
497 ;; (unless (ewoc--filter-hf-nodes ewoc node)
498 ;; (setq node (ewoc--node-nth dll -2)))
499 (ewoc-goto-node ewoc node
)))
501 (defun ewoc-goto-node (ewoc node
)
502 "Move point to NODE in EWOC."
503 (ewoc--set-buffer-bind-dll ewoc
504 (goto-char (ewoc--node-start-marker node
))
505 (if goal-column
(move-to-column goal-column
))
506 (setf (ewoc--last-node ewoc
) node
)))
508 (defun ewoc-refresh (ewoc)
509 "Refresh all data in EWOC.
510 The pretty-printer that was specified when the EWOC was created
511 will be called for all elements in EWOC.
512 Note that `ewoc-invalidate' is more efficient if only a small
513 number of elements needs to be refreshed."
514 (ewoc--set-buffer-bind-dll-let* ewoc
515 ((footer (ewoc--footer ewoc
)))
516 (let ((inhibit-read-only t
))
517 (delete-region (ewoc--node-start-marker (ewoc--node-nth dll
1))
518 (ewoc--node-start-marker footer
))
519 (goto-char (ewoc--node-start-marker footer
))
520 (let ((pp (ewoc--pretty-printer ewoc
))
521 (node (ewoc--node-nth dll
1)))
522 (while (not (eq node footer
))
523 (set-marker (ewoc--node-start-marker node
) (point))
524 (funcall pp
(ewoc--node-data node
))
525 (setq node
(ewoc--node-next dll node
)))))
526 (set-marker (ewoc--node-start-marker footer
) (point))))
528 (defun ewoc-collect (ewoc predicate
&rest args
)
529 "Select elements from EWOC using PREDICATE.
530 Return a list of all selected data elements.
531 PREDICATE is a function that takes a data element as its first
532 argument. The elements on the returned list will appear in the
533 same order as in the buffer. You should not rely on the order of
535 Note that the buffer the EWOC is displayed in is the current
536 buffer when PREDICATE is called. PREDICATE must restore it if it
538 If more than two arguments are given the
539 remaining arguments will be passed to PREDICATE."
540 (ewoc--set-buffer-bind-dll-let* ewoc
541 ((header (ewoc--header ewoc
))
542 (node (ewoc--node-nth dll -
2))
544 (while (not (eq node header
))
545 (if (apply predicate
(ewoc--node-data node
) args
)
546 (push (ewoc--node-data node
) result
))
547 (setq node
(ewoc--node-prev dll node
)))
550 (defun ewoc-buffer (ewoc)
551 "Return the buffer that is associated with EWOC.
552 Return nil if the buffer has been deleted."
553 (let ((buf (ewoc--buffer ewoc
)))
554 (when (buffer-name buf
) buf
)))
556 (defun ewoc-get-hf (ewoc)
557 "Return a cons cell containing the (HEADER . FOOTER) of EWOC."
558 (cons (ewoc--node-data (ewoc--header ewoc
))
559 (ewoc--node-data (ewoc--footer ewoc
))))
561 (defun ewoc-set-hf (ewoc header footer
)
562 "Set the HEADER and FOOTER of EWOC."
563 (ewoc--set-buffer-bind-dll-let* ewoc
564 ((head (ewoc--header ewoc
))
565 (foot (ewoc--footer ewoc
))
566 (hf-pp (ewoc--hf-pp ewoc
)))
567 (setf (ewoc--node-data head
) header
568 (ewoc--node-data foot
) footer
)
570 (ewoc--refresh-node hf-pp head dll
)
571 (ewoc--refresh-node hf-pp foot dll
))))
577 ;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
578 ;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
581 ;; arch-tag: d78915b9-9a07-44bf-aac6-04a1fc1bd6d4
582 ;;; ewoc.el ends here