Remove nonfree old and crusty vc-cc pkg
[packages] / xemacs-packages / xetla / ewoc.el
1 ;;; ewoc.el --- utility to maintain a view of a list of objects in a buffer
2
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000   Free Software Foundation
4
5 ;; Author: Per Cederqvist <ceder@lysator.liu.se>
6 ;;      Inge Wallin <inge@lysator.liu.se>
7 ;; Maintainer: monnier@gnu.org
8 ;; Created: 3 Aug 1992
9 ;; Keywords: extensions, lisp
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; Ewoc Was Once Cookie
31 ;; But now it's Emacs' Widget for Object Collections
32
33 ;; As the name implies this derives from the `cookie' package (part
34 ;; of Elib).  The changes are pervasive though mostly superficial:
35
36 ;; - uses CL (and its `defstruct')
37 ;; - separate from Elib.
38 ;; - uses its own version of a doubly-linked list which allows us
39 ;;   to merge the elib-wrapper and the elib-node structures into ewoc-node
40 ;; - dropping functions not used by PCL-CVS (the only client of ewoc at the
41 ;;   time of writing)
42 ;; - removing unused arguments
43 ;; - renaming:
44 ;;   elib-node  ==>  ewoc--node
45 ;;   collection ==>  ewoc
46 ;;   tin        ==>  ewoc--node
47 ;;   cookie     ==>  data or element or elem
48
49 ;;     Introduction
50 ;;     ============
51 ;;
52 ;; Ewoc is a package that implements a connection between an
53 ;; dll (a doubly linked list) and the contents of a buffer.
54 ;; Possible uses are dired (have all files in a list, and show them),
55 ;; buffer-list, kom-prioritize (in the LysKOM elisp client) and
56 ;; others.  pcl-cvs.el uses ewoc.el.
57 ;;
58 ;; Ewoc can be considered as the `view' part of a model-view-controller.
59 ;;
60 ;; A `element' can be any lisp object.  When you use the ewoc
61 ;; package you specify a pretty-printer, a function that inserts
62 ;; a printable representation of the element in the buffer.  (The
63 ;; pretty-printer should use "insert" and not
64 ;; "insert-before-markers").
65 ;;
66 ;; A `ewoc' consists of a doubly linked list of elements, a
67 ;; header, a footer and a pretty-printer.  It is displayed at a
68 ;; certain point in a certain buffer.  (The buffer and point are
69 ;; fixed when the ewoc is created).  The header and the footer
70 ;; are constant strings.  They appear before and after the elements.
71 ;;
72 ;; Ewoc does not affect the mode of the buffer in any way. It
73 ;; merely makes it easy to connect an underlying data representation
74 ;; to the buffer contents.
75 ;;
76 ;; A `ewoc--node' is an object that contains one element.  There are
77 ;; functions in this package that given an ewoc--node extract the data, or
78 ;; give the next or previous ewoc--node.  (All ewoc--nodes are linked together
79 ;; in a doubly linked list.  The `previous' ewoc--node is the one that appears
80 ;; before the other in the buffer.)  You should not do anything with
81 ;; an ewoc--node except pass it to the functions in this package.
82 ;;
83 ;; An ewoc is a very dynamic thing.  You can easily add or delete elements.
84 ;; You can apply a function to all elements in an ewoc, etc, etc.
85 ;;
86 ;; Remember that an element can be anything.  Your imagination is the
87 ;; limit!  It is even possible to have another ewoc as an
88 ;; element.  In that way some kind of tree hierarchy can be created.
89 ;;
90 ;; Full documentation will, God willing, soon be available in a
91 ;; Texinfo manual.
92
93 ;; In the mean time `grep '^(.*ewoc-[^-]' emacs-lisp/ewoc.el' can help
94 ;; you find all the exported functions:
95 ;; 
96 ;; (defun ewoc-create (pretty-printer &optional header footer)
97 ;; (defalias 'ewoc-data 'ewoc--node-data)
98 ;; (defun ewoc-location (node)
99 ;; (defun ewoc-enter-first (ewoc data)
100 ;; (defun ewoc-enter-last (ewoc data)
101 ;; (defun ewoc-enter-after (ewoc node data)
102 ;; (defun ewoc-enter-before (ewoc node data)
103 ;; (defun ewoc-next (ewoc node)
104 ;; (defun ewoc-prev (ewoc node)
105 ;; (defun ewoc-nth (ewoc n)
106 ;; (defun ewoc-map (map-function ewoc &rest args)
107 ;; (defun ewoc-filter (ewoc predicate &rest args)
108 ;; (defun ewoc-locate (ewoc &optional pos guess)
109 ;; (defun ewoc-invalidate (ewoc &rest nodes)
110 ;; (defun ewoc-goto-prev (ewoc arg)
111 ;; (defun ewoc-goto-next (ewoc arg)
112 ;; (defun ewoc-goto-node (ewoc node)
113 ;; (defun ewoc-refresh (ewoc)
114 ;; (defun ewoc-collect (ewoc predicate &rest args)
115 ;; (defun ewoc-buffer (ewoc)
116 ;; (defun ewoc-get-hf (ewoc)
117 ;; (defun ewoc-set-hf (ewoc header footer)
118
119 ;;     Coding conventions
120 ;;     ==================
121 ;;
122 ;; All functions of course start with `ewoc'.  Functions and macros
123 ;; starting with the prefix `ewoc--' are meant for internal use,
124 ;; while those starting with `ewoc-' are exported for public use.
125 ;; There are currently no global or buffer-local variables used.
126
127
128 ;;; Code:
129
130 (eval-when-compile (require 'cl))       ;because of CL compiler macros
131
132 ;; The doubly linked list is implemented as a circular list
133 ;; with a dummy node first and last. The dummy node is used as
134 ;; "the dll" (or rather is the dll handle passed around).
135
136 (defstruct (ewoc--node
137             (:type vector)              ;required for ewoc--node-branch hack
138             (:constructor ewoc--node-create (start-marker data)))
139   left right data start-marker)
140
141 (eval-when-compile (defvar dll))
142
143 (defalias 'ewoc--node-branch 'aref)
144
145 (defun ewoc--dll-create ()
146   "Create an empty doubly linked list."
147   (let ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST)))
148     (setf (ewoc--node-right dummy-node) dummy-node)
149     (setf (ewoc--node-left dummy-node) dummy-node)
150     dummy-node))
151
152 (defun ewoc--node-enter-before (node elemnode)
153   "Insert ELEMNODE before NODE in a DLL."
154   (assert (and (null (ewoc--node-left elemnode)) (null (ewoc--node-right elemnode))))
155   (setf (ewoc--node-left elemnode) (ewoc--node-left node))
156   (setf (ewoc--node-right elemnode) node)
157   (setf (ewoc--node-right (ewoc--node-left node)) elemnode)
158   (setf (ewoc--node-left node) elemnode))
159
160 (defun ewoc--node-enter-first (dll node)
161   "Add a free floating NODE first in DLL."
162   (ewoc--node-enter-before (ewoc--node-right dll) node))
163
164 (defun ewoc--node-enter-last (dll node)
165   "Add a free floating NODE last in DLL."
166   (ewoc--node-enter-before dll node))
167
168 (defun ewoc--node-next (dll node)
169   "Return the node after NODE, or nil if NODE is the last node."
170   (unless (eq (ewoc--node-right node) dll) (ewoc--node-right node)))
171
172 (defun ewoc--node-prev (dll node)
173   "Return the node before NODE, or nil if NODE is the first node."
174   (unless (eq (ewoc--node-left node) dll) (ewoc--node-left node)))
175
176 (defun ewoc--node-delete (node)
177   "Unbind NODE from its doubly linked list and return it."
178   ;; This is a no-op when applied to the dummy node. This will return
179   ;; nil if applied to the dummy node since it always contains nil.
180   (setf (ewoc--node-right (ewoc--node-left node)) (ewoc--node-right node))
181   (setf (ewoc--node-left (ewoc--node-right node)) (ewoc--node-left node))
182   (setf (ewoc--node-left node) nil)
183   (setf (ewoc--node-right node) nil)
184   node)
185
186 (defun ewoc--node-nth (dll n)
187   "Return the Nth node from the doubly linked list DLL.
188 N counts from zero. If DLL is not that long, nil is returned.
189 If N is negative, return the -(N+1)th last element.
190 Thus, (ewoc--node-nth dll 0) returns the first node,
191 and (ewoc--node-nth dll -1) returns the last node."
192   ;; Branch 0 ("follow left pointer") is used when n is negative.
193   ;; Branch 1 ("follow right pointer") is used otherwise.
194   (let* ((branch (if (< n 0) 0 1))
195          (node   (ewoc--node-branch dll branch)))
196     (if (< n 0) (setq n (- -1 n)))
197     (while (and (not (eq dll node)) (> n 0))
198       (setq node (ewoc--node-branch node branch))
199       (setq n (1- n)))
200     (unless (eq dll node) node)))
201
202 (defun ewoc-location (node)
203   "Return the start location of NODE."
204   (ewoc--node-start-marker node))
205
206 \f
207 ;;; The ewoc data type
208
209 (defstruct (ewoc
210             (:constructor nil)
211             (:constructor ewoc--create
212                           (buffer pretty-printer header footer dll))
213             (:conc-name ewoc--))
214   buffer pretty-printer header footer dll last-node)
215
216 (defmacro ewoc--set-buffer-bind-dll-let* (ewoc varlist &rest forms)
217   "Execute FORMS with ewoc--buffer selected as current buffer,
218 dll bound to dll, and VARLIST bound as in a let*.
219 dll will be bound when VARLIST is initialized, but the current
220 buffer will *not* have been changed.
221 Return value of last form in FORMS."
222   (let ((old-buffer (make-symbol "old-buffer"))
223         (hnd (make-symbol "ewoc")))
224     (` (let* (((, old-buffer) (current-buffer))
225               ((, hnd) (, ewoc))
226               (dll (ewoc--dll (, hnd)))
227               (,@ varlist))
228          (set-buffer (ewoc--buffer (, hnd)))
229          (unwind-protect
230              (progn (,@ forms))
231            (set-buffer (, old-buffer)))))))
232
233 (defmacro ewoc--set-buffer-bind-dll (ewoc &rest forms)
234   `(ewoc--set-buffer-bind-dll-let* ,ewoc nil ,@forms))
235
236 (defsubst ewoc--filter-hf-nodes (ewoc node)
237   "Evaluate NODE once and return it.
238 BUT if it is the header or the footer in EWOC return nil instead."
239   (unless (or (eq node (ewoc--header ewoc))
240               (eq node (ewoc--footer ewoc)))
241     node))
242
243
244 (defun ewoc--create-node (data pretty-printer pos)
245   "Call PRETTY-PRINTER with point set at POS in current buffer.
246 Remember the start position. Create a wrapper containing that
247 start position and the element DATA."
248   (save-excursion
249     ;; Remember the position as a number so that it doesn't move
250     ;; when we insert the string.
251     (when (markerp pos) (setq pos (marker-position pos)))
252     (goto-char pos)
253     (let ((inhibit-read-only t))
254       ;; Insert the trailing newline using insert-before-markers
255       ;; so that the start position for the next element is updated.
256       (insert-before-markers ?\n)
257       ;; Move back, and call the pretty-printer.
258       (backward-char 1)
259       (funcall pretty-printer data)
260       (ewoc--node-create (copy-marker pos) data))))
261
262
263 (defun ewoc--delete-node-internal (ewoc node)
264   "Delete a data string from EWOC.
265 Can not be used on the footer. Returns the wrapper that is deleted.
266 The start-marker in the wrapper is set to nil, so that it doesn't
267 consume any more resources."
268   (let ((dll (ewoc--dll ewoc))
269         (inhibit-read-only t))
270     ;; If we are about to delete the node pointed at by last-node,
271     ;; set last-node to nil.
272     (if (eq (ewoc--last-node ewoc) node)
273         (setf (ewoc--last-node ewoc) nil))
274
275     (delete-region (ewoc--node-start-marker node)
276                    (ewoc--node-start-marker (ewoc--node-next dll node)))
277     (set-marker (ewoc--node-start-marker node) nil)
278     ;; Delete the node, and return the wrapper.
279     (ewoc--node-delete node)))
280
281
282 (defun ewoc--refresh-node (pp node)
283   "Redisplay the element represented by NODE using the pretty-printer PP."
284   (let ((inhibit-read-only t))
285     (save-excursion
286       ;; First, remove the string from the buffer:
287       (delete-region (ewoc--node-start-marker node)
288                      (1- (marker-position
289                           (ewoc--node-start-marker (ewoc--node-right node)))))
290       ;; Calculate and insert the string.
291       (goto-char (ewoc--node-start-marker node))
292       (funcall pp (ewoc--node-data node)))))
293 \f
294 ;;; ===========================================================================
295 ;;;                  Public members of the Ewoc package
296
297
298 (defun ewoc-create (pretty-printer &optional header footer)
299   "Create an empty ewoc.
300
301 The ewoc will be inserted in the current buffer at the current position.
302
303 PRETTY-PRINTER should be a function that takes one argument, an
304 element, and inserts a string representing it in the buffer (at
305 point). The string PRETTY-PRINTER inserts may be empty or span
306 several linse. A trailing newline will always be inserted
307 automatically. The PRETTY-PRINTER should use insert, and not
308 insert-before-markers.
309
310 Optional third argument HEADER is a string that will always be
311 present at the top of the ewoc. HEADER should end with a
312 newline.  Optionaly fourth argument FOOTER is similar, and will
313 be inserted at the bottom of the ewoc."
314   (let ((new-ewoc
315          (ewoc--create (current-buffer)
316                        pretty-printer nil nil (ewoc--dll-create)))
317         (pos (point)))
318     (ewoc--set-buffer-bind-dll new-ewoc
319       ;; Set default values
320       (unless header (setq header ""))
321       (unless footer (setq footer ""))
322       (setf (ewoc--node-start-marker dll) (copy-marker pos))
323       (let ((foot (ewoc--create-node footer (lambda (x) (insert footer)) pos))
324             (head (ewoc--create-node header (lambda (x) (insert header)) pos)))
325         (ewoc--node-enter-first dll head)
326         (ewoc--node-enter-last  dll foot)
327         (setf (ewoc--header new-ewoc) head)
328         (setf (ewoc--footer new-ewoc) foot)))
329     ;; Return the ewoc
330     new-ewoc))
331
332 (defalias 'ewoc-data 'ewoc--node-data)
333
334 (defun ewoc-enter-first (ewoc data)
335   "Enter DATA first in EWOC."
336   (ewoc--set-buffer-bind-dll ewoc
337     (ewoc-enter-after ewoc (ewoc--node-nth dll 0) data)))
338
339 (defun ewoc-enter-last (ewoc data)
340   "Enter DATA last in EWOC."
341   (ewoc--set-buffer-bind-dll ewoc
342     (ewoc-enter-before ewoc (ewoc--node-nth dll -1) data)))
343
344
345 (defun ewoc-enter-after (ewoc node data)
346   "Enter a new element DATA after NODE in EWOC.
347 Returns the new NODE."
348   (ewoc--set-buffer-bind-dll ewoc
349     (ewoc-enter-before ewoc (ewoc--node-next dll node) data)))
350
351 (defun ewoc-enter-before (ewoc node data)
352   "Enter a new element DATA before NODE in EWOC.
353 Returns the new NODE."
354   (ewoc--set-buffer-bind-dll ewoc
355     (ewoc--node-enter-before
356      node
357      (ewoc--create-node
358       data
359       (ewoc--pretty-printer ewoc)
360       (ewoc--node-start-marker node)))))
361
362 (defun ewoc-next (ewoc node)
363   "Get the next node.
364 Returns nil if NODE is nil or the last element."
365   (when node
366     (ewoc--filter-hf-nodes
367      ewoc (ewoc--node-next (ewoc--dll ewoc) node))))
368
369 (defun ewoc-prev (ewoc node)
370   "Get the previous node.
371 Returns nil if NODE is nil or the first element."
372   (when node
373     (ewoc--filter-hf-nodes
374      ewoc
375      (ewoc--node-prev (ewoc--dll ewoc) node))))
376
377
378 (defun ewoc-nth (ewoc n)
379   "Return the Nth node.
380 N counts from zero. Nil is returned if there is less than N elements.
381 If N is negative, return the -(N+1)th last element.
382 Thus, (ewoc-nth dll 0) returns the first node,
383 and (ewoc-nth dll -1) returns the last node.
384 Use `ewoc--node-data' to extract the data from the node."
385   ;; Skip the header (or footer, if n is negative).
386   (setq n (if (< n 0) (1- n) (1+ n)))
387   (ewoc--filter-hf-nodes ewoc
388                   (ewoc--node-nth (ewoc--dll ewoc) n)))
389
390 (defun ewoc-map (map-function ewoc &rest args)
391   "Apply MAP-FUNCTION to all elements in EWOC.
392 MAP-FUNCTION is applied to the first element first.
393 If MAP-FUNCTION returns non-nil the element will be refreshed (its
394 pretty-printer will be called once again).
395
396 Note that the buffer for EWOC will be current buffer when MAP-FUNCTION 
397 is called.  MAP-FUNCTION must restore the current buffer to BUFFER before 
398 it returns, if it changes it.
399
400 If more than two arguments are given, the remaining
401 arguments will be passed to MAP-FUNCTION."
402   (ewoc--set-buffer-bind-dll-let* ewoc
403       ((footer (ewoc--footer ewoc))
404        (node (ewoc--node-nth dll 1)))
405     (while (not (eq node footer))
406       (if (apply map-function (ewoc--node-data node) args)
407           (ewoc--refresh-node (ewoc--pretty-printer ewoc) node))
408       (setq node (ewoc--node-next dll node)))))
409
410 (defun ewoc-filter (ewoc predicate &rest args)
411   "Remove all elements in EWOC for which PREDICATE returns nil.
412 Note that the buffer for EWOC will be current-buffer when PREDICATE 
413 is called. PREDICATE must restore the current buffer before it returns
414 if it changes it.
415 The PREDICATE is called with the element as its first argument. If any
416 ARGS are given they will be passed to the PREDICATE."
417   (ewoc--set-buffer-bind-dll-let* ewoc
418       ((node (ewoc--node-nth dll 1))
419        (footer (ewoc--footer ewoc))
420        (next nil))
421     (while (not (eq node footer))
422       (setq next (ewoc--node-next dll node))
423       (unless (apply predicate (ewoc--node-data node) args)
424         (ewoc--delete-node-internal ewoc node))
425       (setq node next))))
426
427 (defun ewoc-locate (ewoc &optional pos guess)
428   "Return the node that POS (a buffer position) is within.
429 POS may be a marker or an integer.  It defaults to point.
430 GUESS should be a node that it is likely that POS is near.
431
432 If POS points before the first element, the first node is returned.
433 If POS points after the last element, the last node is returned.
434 If the EWOC is empty, nil is returned."
435   (unless pos (setq pos (point)))
436   (ewoc--set-buffer-bind-dll-let* ewoc
437       () ;; ((footer (ewoc--footer ewoc)))
438
439     (cond
440      ;; Nothing present?
441      ((eq (ewoc--node-nth dll 1) (ewoc--node-nth dll -1))
442       nil)
443
444      ;; Before second elem?
445      ((< pos (ewoc--node-start-marker (ewoc--node-nth dll 2)))
446       (ewoc--node-nth dll 1))
447
448      ;; After one-before-last elem?
449      ((>= pos (ewoc--node-start-marker (ewoc--node-nth dll -2)))
450       (ewoc--node-nth dll -2))
451
452      ;; We now know that pos is within a elem.
453      (t
454       ;; Make an educated guess about which of the three known
455       ;; node'es (the first, the last, or GUESS) is nearest.
456       (let* ((best-guess (ewoc--node-nth dll 1))
457              (distance (abs (- pos (ewoc--node-start-marker best-guess)))))
458         (when guess
459           (let ((d (abs (- pos (ewoc--node-start-marker guess)))))
460             (when (< d distance)
461               (setq distance d)
462               (setq best-guess guess))))
463
464         (let* ((g (ewoc--node-nth dll -1))      ;Check the last elem
465                (d (abs (- pos (ewoc--node-start-marker g)))))
466           (when (< d distance)
467             (setq distance d)
468             (setq best-guess g)))
469
470         (when (ewoc--last-node ewoc) ;Check "previous".
471           (let* ((g (ewoc--last-node ewoc))
472                  (d (abs (- pos (ewoc--node-start-marker g)))))
473             (when (< d distance)
474               (setq distance d)
475               (setq best-guess g))))
476
477         ;; best-guess is now a "best guess".
478         ;; Find the correct node. First determine in which direction
479         ;; it lies, and then move in that direction until it is found.
480     
481         (cond
482          ;; Is pos after the guess?
483          ((>= pos
484               (ewoc--node-start-marker best-guess))
485           ;; Loop until we are exactly one node too far down...
486           (while (>= pos (ewoc--node-start-marker best-guess))
487             (setq best-guess (ewoc--node-next dll best-guess)))
488           ;; ...and return the previous node.
489           (ewoc--node-prev dll best-guess))
490
491          ;; Pos is before best-guess
492          (t
493           (while (< pos (ewoc--node-start-marker best-guess))
494             (setq best-guess (ewoc--node-prev dll best-guess)))
495           best-guess)))))))
496
497 (defun ewoc-invalidate (ewoc &rest nodes)
498   "Refresh some elements.
499 The pretty-printer that for EWOC will be called for all NODES."
500   (ewoc--set-buffer-bind-dll ewoc
501     (dolist (node nodes)
502       (ewoc--refresh-node (ewoc--pretty-printer ewoc) node))))
503
504 (defun ewoc-goto-prev (ewoc arg)
505   "Move point to the ARGth previous element.
506 Don't move if we are at the first element, or if EWOC is empty.
507 Returns the node we moved to."
508   (ewoc--set-buffer-bind-dll-let* ewoc
509       ((node (ewoc-locate ewoc (point))))
510     (when node
511       ;; If we were past the last element, first jump to it.
512       (when (>= (point) (ewoc--node-start-marker (ewoc--node-right node)))
513         (setq arg (1- arg)))
514       (while (and node (> arg 0))
515         (setq arg (1- arg))
516         (setq node (ewoc--node-prev dll node)))
517       ;; Never step above the first element.
518       (unless (ewoc--filter-hf-nodes ewoc node)
519         (setq node (ewoc--node-nth dll 1)))
520       (ewoc-goto-node ewoc node))))
521
522 (defun ewoc-goto-next (ewoc arg)
523   "Move point to the ARGth next element.
524 Returns the node (or nil if we just passed the last node)."
525   (ewoc--set-buffer-bind-dll-let* ewoc
526       ((node (ewoc-locate ewoc (point))))
527     (while (and node (> arg 0))
528       (setq arg (1- arg))
529       (setq node (ewoc--node-next dll node)))
530     ;; Never step below the first element.
531     ;; (unless (ewoc--filter-hf-nodes ewoc node)
532     ;;   (setq node (ewoc--node-nth dll -2)))
533     (ewoc-goto-node ewoc node)))
534
535 (defun ewoc-goto-node (ewoc node)
536   "Move point to NODE."
537   (ewoc--set-buffer-bind-dll ewoc
538     (goto-char (ewoc--node-start-marker node))
539     (if goal-column (move-to-column goal-column))
540     (setf (ewoc--last-node ewoc) node)))
541
542 (defun ewoc-refresh (ewoc)
543   "Refresh all data in EWOC.
544 The pretty-printer that was specified when the EWOC was created
545 will be called for all elements in EWOC.
546 Note that `ewoc-invalidate' is more efficient if only a small
547 number of elements needs to be refreshed."
548   (ewoc--set-buffer-bind-dll-let* ewoc
549       ((footer (ewoc--footer ewoc)))
550     (let ((inhibit-read-only t))
551       (delete-region (ewoc--node-start-marker (ewoc--node-nth dll 1))
552                      (ewoc--node-start-marker footer))
553       (goto-char (ewoc--node-start-marker footer))
554       (let ((node (ewoc--node-nth dll 1)))
555         (while (not (eq node footer))
556           (set-marker (ewoc--node-start-marker node) (point))
557           (funcall (ewoc--pretty-printer ewoc)
558                    (ewoc--node-data node))
559           (insert "\n")
560           (setq node (ewoc--node-next dll node)))))
561     (set-marker (ewoc--node-start-marker footer) (point))))
562
563 (defun ewoc-collect (ewoc predicate &rest args)
564   "Select elements from EWOC using PREDICATE.
565 Return a list of all selected data elements.
566 PREDICATE is a function that takes a data element as its first argument.
567 The elements on the returned list will appear in the same order as in
568 the buffer.  You should not rely on in which order PREDICATE is
569 called.
570 Note that the buffer the EWOC is displayed in is current-buffer
571 when PREDICATE is called.  If PREDICATE must restore current-buffer if
572 it changes it.
573 If more than two arguments are given the
574 remaining arguments will be passed to PREDICATE."
575   (ewoc--set-buffer-bind-dll-let* ewoc
576       ((header (ewoc--header ewoc))
577        (node (ewoc--node-nth dll -2))
578        result)
579     (while (not (eq node header))
580       (if (apply predicate (ewoc--node-data node) args)
581           (push (ewoc--node-data node) result))
582       (setq node (ewoc--node-prev dll node)))
583     (nreverse result)))
584
585 (defun ewoc-buffer (ewoc)
586   "Return the buffer that is associated with EWOC.
587 Returns nil if the buffer has been deleted."
588   (let ((buf (ewoc--buffer ewoc)))
589     (when (buffer-name buf) buf)))
590
591 (defun ewoc-get-hf (ewoc)
592   "Return a cons cell containing the (HEADER . FOOTER) of EWOC."
593   (cons (ewoc--node-data (ewoc--header ewoc))
594         (ewoc--node-data (ewoc--footer ewoc))))
595
596 (defun ewoc-set-hf (ewoc header footer)
597   "Set the HEADER and FOOTER of EWOC."
598   (setf (ewoc--node-data (ewoc--header ewoc)) header)
599   (setf (ewoc--node-data (ewoc--footer ewoc)) footer)
600   (ewoc--refresh-node (lambda (x) (insert header)) (ewoc--header ewoc))
601   (ewoc--refresh-node (lambda (x) (insert footer)) (ewoc--footer ewoc)))
602
603 \f
604 (provide 'ewoc)
605
606 ;;; Local Variables:
607 ;;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
608 ;;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
609 ;;; End:
610
611 ;;; ewoc.el ends here