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