Comment fix.
[bpt/emacs.git] / lisp / emacs-lisp / ewoc.el
CommitLineData
5b467bf4
SM
1;;; ewoc.el -- Utility to maintain a view of a list of objects in a buffer
2
e7cff550 3;; Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation
5b467bf4
SM
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
10c471e6 34;; of Elib). The changes are pervasive though mostly superficial:
5b467bf4 35
10c471e6 36;; - uses CL (and its `defstruct')
5b467bf4
SM
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.
5b467bf4
SM
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
10c471e6
SM
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
5b467bf4
SM
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;;
cb3430a1 96;; (defun ewoc-create (pretty-printer &optional header footer)
5b467bf4 97;; (defalias 'ewoc-data 'ewoc--node-data)
10c471e6 98;; (defun ewoc-location (node)
5b467bf4
SM
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)
44946a4c 108;; (defun ewoc-locate (ewoc &optional pos guess)
5b467bf4 109;; (defun ewoc-invalidate (ewoc &rest nodes)
44946a4c
SM
110;; (defun ewoc-goto-prev (ewoc arg)
111;; (defun ewoc-goto-next (ewoc arg)
5b467bf4
SM
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)
cb3430a1
SM
116;; (defun ewoc-get-hf (ewoc)
117;; (defun ewoc-set-hf (ewoc header footer)
5b467bf4
SM
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(defalias 'ewoc--node-branch 'aref)
142
143(defun ewoc--dll-create ()
144 "Create an empty doubly linked list."
145 (let ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST)))
146 (setf (ewoc--node-right dummy-node) dummy-node)
147 (setf (ewoc--node-left dummy-node) dummy-node)
148 dummy-node))
149
150(defun ewoc--node-enter-before (node elemnode)
151 "Insert ELEMNODE before NODE in a DLL."
152 (assert (and (null (ewoc--node-left elemnode)) (null (ewoc--node-right elemnode))))
153 (setf (ewoc--node-left elemnode) (ewoc--node-left node))
154 (setf (ewoc--node-right elemnode) node)
155 (setf (ewoc--node-right (ewoc--node-left node)) elemnode)
156 (setf (ewoc--node-left node) elemnode))
157
158(defun ewoc--node-enter-first (dll node)
159 "Add a free floating NODE first in DLL."
160 (ewoc--node-enter-before (ewoc--node-right dll) node))
161
162(defun ewoc--node-enter-last (dll node)
163 "Add a free floating NODE last in DLL."
164 (ewoc--node-enter-before dll node))
165
166(defun ewoc--node-next (dll node)
167 "Return the node after NODE, or nil if NODE is the last node."
168 (unless (eq (ewoc--node-right node) dll) (ewoc--node-right node)))
169
170(defun ewoc--node-prev (dll node)
171 "Return the node before NODE, or nil if NODE is the first node."
172 (unless (eq (ewoc--node-left node) dll) (ewoc--node-left node)))
173
174(defun ewoc--node-delete (node)
175 "Unbind NODE from its doubly linked list and return it."
176 ;; This is a no-op when applied to the dummy node. This will return
177 ;; nil if applied to the dummy node since it always contains nil.
178 (setf (ewoc--node-right (ewoc--node-left node)) (ewoc--node-right node))
179 (setf (ewoc--node-left (ewoc--node-right node)) (ewoc--node-left node))
180 (setf (ewoc--node-left node) nil)
181 (setf (ewoc--node-right node) nil)
182 node)
183
184(defun ewoc--node-nth (dll n)
185 "Return the Nth node from the doubly linked list DLL.
186N counts from zero. If DLL is not that long, nil is returned.
187If N is negative, return the -(N+1)th last element.
188Thus, (ewoc--node-nth dll 0) returns the first node,
189and (ewoc--node-nth dll -1) returns the last node."
190 ;; Branch 0 ("follow left pointer") is used when n is negative.
191 ;; Branch 1 ("follow right pointer") is used otherwise.
192 (let* ((branch (if (< n 0) 0 1))
193 (node (ewoc--node-branch dll branch)))
194 (if (< n 0) (setq n (- -1 n)))
195 (while (and (not (eq dll node)) (> n 0))
196 (setq node (ewoc--node-branch node branch))
197 (setq n (1- n)))
198 (unless (eq dll node) node)))
199
10c471e6
SM
200(defun ewoc-location (node)
201 "Return the start location of NODE."
202 (ewoc--node-start-marker node))
203
5b467bf4
SM
204\f
205;;; The ewoc data type
206
207(defstruct (ewoc
208 (:constructor nil)
209 (:constructor ewoc--create
210 (buffer pretty-printer header footer dll))
211 (:conc-name ewoc--))
212 buffer pretty-printer header footer dll last-node)
213
214(defmacro ewoc--set-buffer-bind-dll-let* (ewoc varlist &rest forms)
215 "Execute FORMS with ewoc--buffer selected as current buffer,
216dll bound to ewoc--dll, and VARLIST bound as in a let*.
217dll will be bound when VARLIST is initialized, but the current
218buffer will *not* have been changed.
219Return value of last form in FORMS."
220 (let ((old-buffer (make-symbol "old-buffer"))
221 (hnd (make-symbol "ewoc")))
222 (` (let* (((, old-buffer) (current-buffer))
223 ((, hnd) (, ewoc))
224 (dll (ewoc--dll (, hnd)))
225 (,@ varlist))
226 (set-buffer (ewoc--buffer (, hnd)))
227 (unwind-protect
228 (progn (,@ forms))
229 (set-buffer (, old-buffer)))))))
230
231(defmacro ewoc--set-buffer-bind-dll (ewoc &rest forms)
232 `(ewoc--set-buffer-bind-dll-let* ,ewoc nil ,@forms))
233
234(defsubst ewoc--filter-hf-nodes (ewoc node)
235 "Evaluate NODE once and return it.
236BUT if it is the header or the footer in EWOC return nil instead."
237 (unless (or (eq node (ewoc--header ewoc))
238 (eq node (ewoc--footer ewoc)))
239 node))
240
241
5b467bf4
SM
242(defun ewoc--create-node (data pretty-printer pos)
243 "Call PRETTY-PRINTER with point set at POS in current buffer.
244Remember the start position. Create a wrapper containing that
245start position and the element DATA."
246 (save-excursion
247 ;; Remember the position as a number so that it doesn't move
248 ;; when we insert the string.
249 (when (markerp pos) (setq pos (marker-position pos)))
250 (goto-char pos)
251 (let ((inhibit-read-only t))
252 ;; Insert the trailing newline using insert-before-markers
253 ;; so that the start position for the next element is updated.
254 (insert-before-markers ?\n)
255 ;; Move back, and call the pretty-printer.
256 (backward-char 1)
257 (funcall pretty-printer data)
258 (ewoc--node-create (copy-marker pos) data))))
259
260
261(defun ewoc--delete-node-internal (ewoc node)
262 "Delete a data string from EWOC.
263Can not be used on the footer. Returns the wrapper that is deleted.
264The start-marker in the wrapper is set to nil, so that it doesn't
265consume 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
cb3430a1
SM
280(defun ewoc--refresh-node (pp node)
281 "Redisplay the element represented by NODE using the pretty-printer PP."
5b467bf4
SM
282 (let ((inhibit-read-only t))
283 (save-excursion
284 ;; First, remove the string from the buffer:
285 (delete-region (ewoc--node-start-marker node)
286 (1- (marker-position
cb3430a1 287 (ewoc--node-start-marker (ewoc--node-right node)))))
5b467bf4
SM
288 ;; Calculate and insert the string.
289 (goto-char (ewoc--node-start-marker node))
cb3430a1 290 (funcall pp (ewoc--node-data node)))))
5b467bf4
SM
291\f
292;;; ===========================================================================
293;;; Public members of the Ewoc package
294
295
cb3430a1 296(defun ewoc-create (pretty-printer &optional header footer)
5b467bf4
SM
297 "Create an empty ewoc.
298
cb3430a1 299The ewoc will be inserted in the current buffer at the current position.
5b467bf4
SM
300
301PRETTY-PRINTER should be a function that takes one argument, an
302element, and inserts a string representing it in the buffer (at
303point). The string PRETTY-PRINTER inserts may be empty or span
304several linse. A trailing newline will always be inserted
305automatically. The PRETTY-PRINTER should use insert, and not
306insert-before-markers.
307
308Optional third argument HEADER is a string that will always be
309present at the top of the ewoc. HEADER should end with a
310newline. Optionaly fourth argument FOOTER is similar, and will
cb3430a1 311be inserted at the bottom of the ewoc."
5b467bf4 312 (let ((new-ewoc
cb3430a1
SM
313 (ewoc--create (current-buffer)
314 pretty-printer nil nil (ewoc--dll-create)))
315 (pos (point)))
5b467bf4
SM
316 (ewoc--set-buffer-bind-dll new-ewoc
317 ;; Set default values
318 (unless header (setq header ""))
319 (unless footer (setq footer ""))
cb3430a1
SM
320 (setf (ewoc--node-start-marker dll) (copy-marker pos))
321 (let ((foot (ewoc--create-node footer (lambda (x) (insert footer)) pos))
322 (head (ewoc--create-node header (lambda (x) (insert header)) pos)))
5b467bf4
SM
323 (ewoc--node-enter-first dll head)
324 (ewoc--node-enter-last dll foot)
cb3430a1
SM
325 (setf (ewoc--header new-ewoc) head)
326 (setf (ewoc--footer new-ewoc) foot)))
5b467bf4
SM
327 ;; Return the ewoc
328 new-ewoc))
329
330(defalias 'ewoc-data 'ewoc--node-data)
331
332(defun ewoc-enter-first (ewoc data)
333 "Enter DATA first in EWOC."
334 (ewoc--set-buffer-bind-dll ewoc
335 (ewoc-enter-after ewoc (ewoc--node-nth dll 0) data)))
336
337(defun ewoc-enter-last (ewoc data)
338 "Enter DATA last in EWOC."
339 (ewoc--set-buffer-bind-dll ewoc
340 (ewoc-enter-before ewoc (ewoc--node-nth dll -1) data)))
341
342
343(defun ewoc-enter-after (ewoc node data)
10c471e6
SM
344 "Enter a new element DATA after NODE in EWOC.
345Returns the new NODE."
5b467bf4
SM
346 (ewoc--set-buffer-bind-dll ewoc
347 (ewoc-enter-before ewoc (ewoc--node-next dll node) data)))
348
349(defun ewoc-enter-before (ewoc node data)
10c471e6
SM
350 "Enter a new element DATA before NODE in EWOC.
351Returns the new NODE."
5b467bf4
SM
352 (ewoc--set-buffer-bind-dll ewoc
353 (ewoc--node-enter-before
354 node
355 (ewoc--create-node
356 data
357 (ewoc--pretty-printer ewoc)
358 (ewoc--node-start-marker node)))))
359
360(defun ewoc-next (ewoc node)
361 "Get the next node.
362Returns nil if NODE is nil or the last element."
363 (when node
364 (ewoc--filter-hf-nodes
365 ewoc (ewoc--node-next (ewoc--dll ewoc) node))))
366
367(defun ewoc-prev (ewoc node)
368 "Get the previous node.
369Returns nil if NODE is nil or the first element."
370 (when node
371 (ewoc--filter-hf-nodes
372 ewoc
373 (ewoc--node-prev (ewoc--dll ewoc) node))))
374
375
376(defun ewoc-nth (ewoc n)
377 "Return the Nth node.
378N counts from zero. Nil is returned if there is less than N elements.
379If N is negative, return the -(N+1)th last element.
380Thus, (ewoc-nth dll 0) returns the first node,
381and (ewoc-nth dll -1) returns the last node.
382Use `ewoc--node-data' to extract the data from the node."
383 ;; Skip the header (or footer, if n is negative).
384 (setq n (if (< n 0) (1- n) (1+ n)))
385 (ewoc--filter-hf-nodes ewoc
386 (ewoc--node-nth (ewoc--dll ewoc) n)))
387
388(defun ewoc-map (map-function ewoc &rest args)
389 "Apply MAP-FUNCTION to all elements in EWOC.
390MAP-FUNCTION is applied to the first element first.
391If MAP-FUNCTION returns non-nil the element will be refreshed (its
392pretty-printer will be called once again).
393
394Note that the buffer for EWOC will be current buffer when MAP-FUNCTION
395is called. MAP-FUNCTION must restore the current buffer to BUFFER before
396it returns, if it changes it.
397
398If more than two arguments are given, the remaining
399arguments will be passed to MAP-FUNCTION."
400 (ewoc--set-buffer-bind-dll-let* ewoc
401 ((footer (ewoc--footer ewoc))
402 (node (ewoc--node-nth dll 1)))
403 (while (not (eq node footer))
404 (if (apply map-function (ewoc--node-data node) args)
cb3430a1 405 (ewoc--refresh-node (ewoc--pretty-printer ewoc) node))
5b467bf4
SM
406 (setq node (ewoc--node-next dll node)))))
407
408(defun ewoc-filter (ewoc predicate &rest args)
409 "Remove all elements in EWOC for which PREDICATE returns nil.
410Note that the buffer for EWOC will be current-buffer when PREDICATE
411is called. PREDICATE must restore the current buffer before it returns
412if it changes it.
413The PREDICATE is called with the element as its first argument. If any
414ARGS are given they will be passed to the PREDICATE."
415 (ewoc--set-buffer-bind-dll-let* ewoc
416 ((node (ewoc--node-nth dll 1))
417 (footer (ewoc--footer ewoc))
418 (next nil))
419 (while (not (eq node footer))
420 (setq next (ewoc--node-next dll node))
421 (unless (apply predicate (ewoc--node-data node) args)
422 (ewoc--delete-node-internal ewoc node))
423 (setq node next))))
424
44946a4c 425(defun ewoc-locate (ewoc &optional pos guess)
5b467bf4 426 "Return the node that POS (a buffer position) is within.
44946a4c 427POS may be a marker or an integer. It defaults to point.
5b467bf4
SM
428GUESS should be a node that it is likely that POS is near.
429
430If POS points before the first element, the first node is returned.
431If POS points after the last element, the last node is returned.
432If the EWOC is empty, nil is returned."
44946a4c 433 (unless pos (setq pos (point)))
5b467bf4
SM
434 (ewoc--set-buffer-bind-dll-let* ewoc
435 ((footer (ewoc--footer ewoc)))
436
437 (cond
438 ;; Nothing present?
439 ((eq (ewoc--node-nth dll 1) (ewoc--node-nth dll -1))
440 nil)
441
442 ;; Before second elem?
443 ((< pos (ewoc--node-start-marker (ewoc--node-nth dll 2)))
444 (ewoc--node-nth dll 1))
445
446 ;; After one-before-last elem?
447 ((>= pos (ewoc--node-start-marker (ewoc--node-nth dll -2)))
448 (ewoc--node-nth dll -2))
449
450 ;; We now know that pos is within a elem.
451 (t
452 ;; Make an educated guess about which of the three known
453 ;; node'es (the first, the last, or GUESS) is nearest.
454 (let* ((best-guess (ewoc--node-nth dll 1))
455 (distance (abs (- pos (ewoc--node-start-marker best-guess)))))
456 (when guess
457 (let ((d (abs (- pos (ewoc--node-start-marker guess)))))
458 (when (< d distance)
459 (setq distance d)
460 (setq best-guess guess))))
461
462 (let* ((g (ewoc--node-nth dll -1)) ;Check the last elem
463 (d (abs (- pos (ewoc--node-start-marker g)))))
464 (when (< d distance)
465 (setq distance d)
466 (setq best-guess g)))
467
468 (when (ewoc--last-node ewoc) ;Check "previous".
469 (let* ((g (ewoc--last-node ewoc))
470 (d (abs (- pos (ewoc--node-start-marker g)))))
471 (when (< d distance)
472 (setq distance d)
473 (setq best-guess g))))
474
475 ;; best-guess is now a "best guess".
476 ;; Find the correct node. First determine in which direction
477 ;; it lies, and then move in that direction until it is found.
478
479 (cond
480 ;; Is pos after the guess?
481 ((>= pos
482 (ewoc--node-start-marker best-guess))
483 ;; Loop until we are exactly one node too far down...
484 (while (>= pos (ewoc--node-start-marker best-guess))
485 (setq best-guess (ewoc--node-next dll best-guess)))
486 ;; ...and return the previous node.
487 (ewoc--node-prev dll best-guess))
488
489 ;; Pos is before best-guess
490 (t
491 (while (< pos (ewoc--node-start-marker best-guess))
492 (setq best-guess (ewoc--node-prev dll best-guess)))
493 best-guess)))))))
494
495(defun ewoc-invalidate (ewoc &rest nodes)
496 "Refresh some elements.
497The pretty-printer that for EWOC will be called for all NODES."
498 (ewoc--set-buffer-bind-dll ewoc
499 (dolist (node nodes)
cb3430a1 500 (ewoc--refresh-node (ewoc--pretty-printer ewoc) node))))
5b467bf4 501
44946a4c 502(defun ewoc-goto-prev (ewoc arg)
5b467bf4
SM
503 "Move point to the ARGth previous element.
504Don't move if we are at the first element, or if EWOC is empty.
505Returns the node we moved to."
506 (ewoc--set-buffer-bind-dll-let* ewoc
6d84ac85 507 ((node (ewoc-locate ewoc (point))))
5b467bf4 508 (when node
44946a4c
SM
509 ;; If we were past the last element, first jump to it.
510 (when (>= (point) (ewoc--node-start-marker (ewoc--node-right node)))
511 (setq arg (1- arg)))
5b467bf4
SM
512 (while (and node (> arg 0))
513 (setq arg (1- arg))
514 (setq node (ewoc--node-prev dll node)))
515 ;; Never step above the first element.
516 (unless (ewoc--filter-hf-nodes ewoc node)
517 (setq node (ewoc--node-nth dll 1)))
518 (ewoc-goto-node ewoc node))))
519
44946a4c 520(defun ewoc-goto-next (ewoc arg)
5b467bf4 521 "Move point to the ARGth next element.
44946a4c 522Returns the node (or nil if we just passed the last node)."
5b467bf4 523 (ewoc--set-buffer-bind-dll-let* ewoc
6d84ac85 524 ((node (ewoc-locate ewoc (point))))
5b467bf4
SM
525 (while (and node (> arg 0))
526 (setq arg (1- arg))
527 (setq node (ewoc--node-next dll node)))
528 ;; Never step below the first element.
44946a4c
SM
529 ;; (unless (ewoc--filter-hf-nodes ewoc node)
530 ;; (setq node (ewoc--node-nth dll -2)))
5b467bf4
SM
531 (ewoc-goto-node ewoc node)))
532
533(defun ewoc-goto-node (ewoc node)
534 "Move point to NODE."
535 (ewoc--set-buffer-bind-dll ewoc
536 (goto-char (ewoc--node-start-marker node))
537 (if goal-column (move-to-column goal-column))
538 (setf (ewoc--last-node ewoc) node)))
539
540(defun ewoc-refresh (ewoc)
541 "Refresh all data in EWOC.
542The pretty-printer that was specified when the EWOC was created
543will be called for all elements in EWOC.
544Note that `ewoc-invalidate' is more efficient if only a small
545number of elements needs to be refreshed."
546 (ewoc--set-buffer-bind-dll-let* ewoc
cb3430a1 547 ((footer (ewoc--footer ewoc)))
5b467bf4
SM
548 (let ((inhibit-read-only t))
549 (delete-region (ewoc--node-start-marker (ewoc--node-nth dll 1))
550 (ewoc--node-start-marker footer))
551 (goto-char (ewoc--node-start-marker footer))
552 (let ((node (ewoc--node-nth dll 1)))
553 (while (not (eq node footer))
554 (set-marker (ewoc--node-start-marker node) (point))
555 (funcall (ewoc--pretty-printer ewoc)
556 (ewoc--node-data node))
557 (insert "\n")
558 (setq node (ewoc--node-next dll node)))))
559 (set-marker (ewoc--node-start-marker footer) (point))))
560
561(defun ewoc-collect (ewoc predicate &rest args)
562 "Select elements from EWOC using PREDICATE.
563Return a list of all selected data elements.
564PREDICATE is a function that takes a data element as its first argument.
565The elements on the returned list will appear in the same order as in
566the buffer. You should not rely on in which order PREDICATE is
567called.
568Note that the buffer the EWOC is displayed in is current-buffer
569when PREDICATE is called. If PREDICATE must restore current-buffer if
570it changes it.
571If more than two arguments are given the
572remaining arguments will be passed to PREDICATE."
573 (ewoc--set-buffer-bind-dll-let* ewoc
574 ((header (ewoc--header ewoc))
575 (node (ewoc--node-nth dll -2))
576 result)
577 (while (not (eq node header))
578 (if (apply predicate (ewoc--node-data node) args)
579 (push (ewoc--node-data node) result))
580 (setq node (ewoc--node-prev dll node)))
6d84ac85 581 (nreverse result)))
5b467bf4
SM
582
583(defun ewoc-buffer (ewoc)
584 "Return the buffer that is associated with EWOC.
585Returns nil if the buffer has been deleted."
586 (let ((buf (ewoc--buffer ewoc)))
587 (when (buffer-name buf) buf)))
588
cb3430a1
SM
589(defun ewoc-get-hf (ewoc)
590 "Return a cons cell containing the (HEADER . FOOTER) of EWOC."
591 (cons (ewoc--node-data (ewoc--header ewoc))
592 (ewoc--node-data (ewoc--footer ewoc))))
593
594(defun ewoc-set-hf (ewoc header footer)
595 "Set the HEADER and FOOTER of EWOC."
596 (setf (ewoc--node-data (ewoc--header ewoc)) header)
597 (setf (ewoc--node-data (ewoc--footer ewoc)) footer)
598 (ewoc--refresh-node (lambda (x) (insert header)) (ewoc--header ewoc))
599 (ewoc--refresh-node (lambda (x) (insert footer)) (ewoc--footer ewoc)))
600
5b467bf4
SM
601\f
602(provide 'ewoc)
603
604;;; Local Variables:
605;;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
606;;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
607;;; End:
608
609;;; ewoc.el ends here