(blink-matching-open): When in minibuffer,
[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,
f0fa15c5 4;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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;;
0741626e 91;; The Emacs Lisp Reference Manual documents ewoc.el's "public interface".
5b467bf4
SM
92
93;; Coding conventions
94;; ==================
95;;
96;; All functions of course start with `ewoc'. Functions and macros
97;; starting with the prefix `ewoc--' are meant for internal use,
98;; while those starting with `ewoc-' are exported for public use.
5b467bf4
SM
99
100;;; Code:
101
7ece7aba 102(eval-when-compile (require 'cl))
5b467bf4 103
bb7a346f
SM
104;; The doubly linked list is implemented as a circular list with a dummy
105;; node first and last. The dummy node is used as "the dll".
5b467bf4 106(defstruct (ewoc--node
63910b23 107 (:type vector) ;ewoc--node-nth needs this
7f934f3d 108 (:constructor nil)
5b467bf4
SM
109 (:constructor ewoc--node-create (start-marker data)))
110 left right data start-marker)
111
bb7a346f 112(defun ewoc--node-next (dll node)
5b467bf4 113 "Return the node after NODE, or nil if NODE is the last node."
7dd2e64c 114 (let ((R (ewoc--node-right node)))
bb7a346f 115 (unless (eq dll R) R)))
5b467bf4 116
bb7a346f 117(defun ewoc--node-prev (dll node)
5b467bf4 118 "Return the node before NODE, or nil if NODE is the first node."
7dd2e64c 119 (let ((L (ewoc--node-left node)))
bb7a346f 120 (unless (eq dll L) L)))
7dd2e64c 121
bb7a346f
SM
122(defun ewoc--node-nth (dll n)
123 "Return the Nth node from the doubly linked list `dll'.
7dd2e64c
TTN
124N counts from zero. If N is negative, return the -(N+1)th last element.
125If N is out of range, return nil.
bb7a346f
SM
126Thus, (ewoc--node-nth dll 0) returns the first node,
127and (ewoc--node-nth dll -1) returns the last node."
63910b23 128 ;; Presuming a node is ":type vector", starting with `left' and `right':
5b467bf4
SM
129 ;; Branch 0 ("follow left pointer") is used when n is negative.
130 ;; Branch 1 ("follow right pointer") is used otherwise.
131 (let* ((branch (if (< n 0) 0 1))
bb7a346f 132 (node (aref dll branch)))
5b467bf4 133 (if (< n 0) (setq n (- -1 n)))
bb7a346f 134 (while (and (not (eq dll node)) (> n 0))
63910b23 135 (setq node (aref node branch))
5b467bf4 136 (setq n (1- n)))
bb7a346f 137 (unless (eq dll node) node)))
5b467bf4 138
10c471e6
SM
139(defun ewoc-location (node)
140 "Return the start location of NODE."
141 (ewoc--node-start-marker node))
142
5b467bf4
SM
143\f
144;;; The ewoc data type
145
146(defstruct (ewoc
147 (:constructor nil)
f860b721 148 (:constructor ewoc--create (buffer pretty-printer dll))
5b467bf4 149 (:conc-name ewoc--))
2bbacc0e 150 buffer pretty-printer header footer dll last-node hf-pp)
5b467bf4
SM
151
152(defmacro ewoc--set-buffer-bind-dll-let* (ewoc varlist &rest forms)
153 "Execute FORMS with ewoc--buffer selected as current buffer,
bb7a346f
SM
154`dll' bound to the dll, and VARLIST bound as in a let*.
155`dll' will be bound when VARLIST is initialized, but
7dd2e64c 156the current buffer will *not* have been changed.
5b467bf4 157Return value of last form in FORMS."
b1c36c0f
TTN
158 (let ((hnd (make-symbol "ewoc")))
159 `(let* ((,hnd ,ewoc)
bb7a346f 160 (dll (ewoc--dll ,hnd))
8a946354 161 ,@varlist)
b1c36c0f
TTN
162 (with-current-buffer (ewoc--buffer ,hnd)
163 ,@forms))))
5b467bf4
SM
164
165(defmacro ewoc--set-buffer-bind-dll (ewoc &rest forms)
166 `(ewoc--set-buffer-bind-dll-let* ,ewoc nil ,@forms))
167
168(defsubst ewoc--filter-hf-nodes (ewoc node)
169 "Evaluate NODE once and return it.
170BUT if it is the header or the footer in EWOC return nil instead."
171 (unless (or (eq node (ewoc--header ewoc))
172 (eq node (ewoc--footer ewoc)))
173 node))
174
bb7a346f 175(defun ewoc--adjust (beg end node dll)
60eae434
TTN
176 ;; "Manually reseat" markers for NODE and its successors (including footer
177 ;; and dll), in the case where they originally shared start position with
178 ;; BEG, to END. BEG and END are buffer positions describing NODE's left
179 ;; neighbor. This operation is functionally equivalent to temporarily
180 ;; setting these nodes' markers' insertion type to t around the pretty-print
7dd2e64c 181 ;; call that precedes the call to `ewoc--adjust', and then changing them back
60eae434
TTN
182 ;; to nil.
183 (when (< beg end)
184 (let (m)
185 (while (and (= beg (setq m (ewoc--node-start-marker node)))
bb7a346f
SM
186 ;; The "dummy" node `dll' actually holds the marker that
187 ;; points to the end of the footer, so we check `dll'
188 ;; *after* reseating the marker.
60eae434
TTN
189 (progn
190 (set-marker m end)
bb7a346f 191 (not (eq dll node))))
60eae434
TTN
192 (setq node (ewoc--node-right node))))))
193
7f0ea399
TTN
194(defun ewoc--insert-new-node (node data pretty-printer)
195 "Insert before NODE a new node for DATA, displayed by PRETTY-PRINTER.
196Call PRETTY-PRINTER with point at NODE's start, thus pushing back
197NODE and leaving the new node's start there. Return the new node."
5b467bf4 198 (save-excursion
7ece7aba
SM
199 (let ((elemnode (ewoc--node-create
200 (copy-marker (ewoc--node-start-marker node)) data)))
201 (setf (ewoc--node-left elemnode) (ewoc--node-left node)
7f0ea399
TTN
202 (ewoc--node-right elemnode) node
203 (ewoc--node-right (ewoc--node-left node)) elemnode
204 (ewoc--node-left node) elemnode)
bb7a346f 205 (ewoc--refresh-node pretty-printer elemnode dll)
7f0ea399 206 elemnode)))
5b467bf4 207
bb7a346f 208(defun ewoc--refresh-node (pp node dll)
cb3430a1 209 "Redisplay the element represented by NODE using the pretty-printer PP."
60eae434
TTN
210 (let ((inhibit-read-only t)
211 (m (ewoc--node-start-marker node))
212 (R (ewoc--node-right node)))
bfbdb5ca 213 ;; First, remove the string from the buffer:
60eae434 214 (delete-region m (ewoc--node-start-marker R))
bfbdb5ca 215 ;; Calculate and insert the string.
60eae434
TTN
216 (goto-char m)
217 (funcall pp (ewoc--node-data node))
bb7a346f 218 (ewoc--adjust m (point) R dll)))
2bbacc0e
TTN
219
220(defun ewoc--wrap (func)
221 (lexical-let ((ewoc--user-pp func))
222 (lambda (data)
223 (funcall ewoc--user-pp data)
224 (insert "\n"))))
225
5b467bf4
SM
226\f
227;;; ===========================================================================
228;;; Public members of the Ewoc package
229
3fe35897 230;;;###autoload
2bbacc0e 231(defun ewoc-create (pretty-printer &optional header footer nosep)
5b467bf4
SM
232 "Create an empty ewoc.
233
cb3430a1 234The ewoc will be inserted in the current buffer at the current position.
5b467bf4
SM
235
236PRETTY-PRINTER should be a function that takes one argument, an
237element, and inserts a string representing it in the buffer (at
0111ab41 238point). The string PRETTY-PRINTER inserts may be empty or span
60eae434 239several lines. The PRETTY-PRINTER should use `insert', and not
0111ab41
JB
240`insert-before-markers'.
241
60eae434
TTN
242Optional second and third arguments HEADER and FOOTER are strings,
243possibly empty, that will always be present at the top and bottom,
2bbacc0e
TTN
244respectively, of the ewoc.
245
246Normally, a newline is automatically inserted after the header,
247the footer and every node's printed representation. Optional
248fourth arg NOSEP non-nil inhibits this."
2c246c9f
TTN
249 (let* ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST))
250 (dll (progn (setf (ewoc--node-right dummy-node) dummy-node)
251 (setf (ewoc--node-left dummy-node) dummy-node)
252 dummy-node))
2bbacc0e 253 (wrap (if nosep 'identity 'ewoc--wrap))
f860b721 254 (new-ewoc (ewoc--create (current-buffer)
2bbacc0e 255 (funcall wrap pretty-printer)
f860b721 256 dll))
2bbacc0e 257 (hf-pp (funcall wrap 'insert))
7f0ea399
TTN
258 (pos (point))
259 head foot)
5b467bf4
SM
260 (ewoc--set-buffer-bind-dll new-ewoc
261 ;; Set default values
262 (unless header (setq header ""))
263 (unless footer (setq footer ""))
7f0ea399 264 (setf (ewoc--node-start-marker dll) (copy-marker pos)
2bbacc0e
TTN
265 foot (ewoc--insert-new-node dll footer hf-pp)
266 head (ewoc--insert-new-node foot header hf-pp)
267 (ewoc--hf-pp new-ewoc) hf-pp
7f0ea399
TTN
268 (ewoc--footer new-ewoc) foot
269 (ewoc--header new-ewoc) head))
5b467bf4
SM
270 ;; Return the ewoc
271 new-ewoc))
272
8d1bec8d
TTN
273(defalias 'ewoc-data 'ewoc--node-data
274 "Extract the data encapsulated by NODE and return it.
275
276\(fn NODE)")
5b467bf4 277
bb8d35a2
TTN
278(defun ewoc-set-data (node data)
279 "Set NODE to encapsulate DATA."
280 (setf (ewoc--node-data node) data))
281
5b467bf4 282(defun ewoc-enter-first (ewoc data)
d4ddf783
TTN
283 "Enter DATA first in EWOC.
284Return the new node."
5b467bf4 285 (ewoc--set-buffer-bind-dll ewoc
bb7a346f 286 (ewoc-enter-after ewoc (ewoc--node-nth dll 0) data)))
5b467bf4
SM
287
288(defun ewoc-enter-last (ewoc data)
d4ddf783
TTN
289 "Enter DATA last in EWOC.
290Return the new node."
5b467bf4 291 (ewoc--set-buffer-bind-dll ewoc
bb7a346f 292 (ewoc-enter-before ewoc (ewoc--node-nth dll -1) data)))
5b467bf4 293
5b467bf4 294(defun ewoc-enter-after (ewoc node data)
10c471e6 295 "Enter a new element DATA after NODE in EWOC.
d4ddf783 296Return the new node."
5b467bf4 297 (ewoc--set-buffer-bind-dll ewoc
bb7a346f 298 (ewoc-enter-before ewoc (ewoc--node-next dll node) data)))
5b467bf4
SM
299
300(defun ewoc-enter-before (ewoc node data)
10c471e6 301 "Enter a new element DATA before NODE in EWOC.
d4ddf783 302Return the new node."
5b467bf4 303 (ewoc--set-buffer-bind-dll ewoc
7f0ea399 304 (ewoc--insert-new-node node data (ewoc--pretty-printer ewoc))))
5b467bf4
SM
305
306(defun ewoc-next (ewoc node)
d4ddf783
TTN
307 "Return the node in EWOC that follows NODE.
308Return nil if NODE is nil or the last element."
5b467bf4
SM
309 (when node
310 (ewoc--filter-hf-nodes
bb7a346f 311 ewoc (ewoc--node-next (ewoc--dll ewoc) node))))
5b467bf4
SM
312
313(defun ewoc-prev (ewoc node)
d4ddf783
TTN
314 "Return the node in EWOC that precedes NODE.
315Return nil if NODE is nil or the first element."
5b467bf4
SM
316 (when node
317 (ewoc--filter-hf-nodes
bb7a346f 318 ewoc (ewoc--node-prev (ewoc--dll ewoc) node))))
5b467bf4 319
5b467bf4
SM
320(defun ewoc-nth (ewoc n)
321 "Return the Nth node.
f0529b5b 322N counts from zero. Return nil if there is less than N elements.
5b467bf4 323If N is negative, return the -(N+1)th last element.
7dd2e64c
TTN
324Thus, (ewoc-nth ewoc 0) returns the first node,
325and (ewoc-nth ewoc -1) returns the last node.
8d1bec8d 326Use `ewoc-data' to extract the data from the node."
5b467bf4
SM
327 ;; Skip the header (or footer, if n is negative).
328 (setq n (if (< n 0) (1- n) (1+ n)))
329 (ewoc--filter-hf-nodes ewoc
bb7a346f 330 (ewoc--node-nth (ewoc--dll ewoc) n)))
5b467bf4
SM
331
332(defun ewoc-map (map-function ewoc &rest args)
333 "Apply MAP-FUNCTION to all elements in EWOC.
334MAP-FUNCTION is applied to the first element first.
335If MAP-FUNCTION returns non-nil the element will be refreshed (its
336pretty-printer will be called once again).
337
0111ab41
JB
338Note that the buffer for EWOC will be the current buffer when
339MAP-FUNCTION is called. MAP-FUNCTION must restore the current
340buffer before it returns, if it changes it.
5b467bf4
SM
341
342If more than two arguments are given, the remaining
343arguments will be passed to MAP-FUNCTION."
344 (ewoc--set-buffer-bind-dll-let* ewoc
345 ((footer (ewoc--footer ewoc))
8433d470 346 (pp (ewoc--pretty-printer ewoc))
bb7a346f 347 (node (ewoc--node-nth dll 1)))
bfbdb5ca
TTN
348 (save-excursion
349 (while (not (eq node footer))
350 (if (apply map-function (ewoc--node-data node) args)
bb7a346f
SM
351 (ewoc--refresh-node pp node dll))
352 (setq node (ewoc--node-next dll node))))))
5b467bf4 353
f569c26e
TTN
354(defun ewoc-delete (ewoc &rest nodes)
355 "Delete NODES from EWOC."
356 (ewoc--set-buffer-bind-dll-let* ewoc
ec491f90 357 ((L nil) (R nil) (last (ewoc--last-node ewoc)))
f569c26e
TTN
358 (dolist (node nodes)
359 ;; If we are about to delete the node pointed at by last-node,
360 ;; set last-node to nil.
ec491f90
TTN
361 (when (eq last node)
362 (setf last nil (ewoc--last-node ewoc) nil))
f569c26e 363 (delete-region (ewoc--node-start-marker node)
bb7a346f 364 (ewoc--node-start-marker (ewoc--node-next dll node)))
f569c26e
TTN
365 (set-marker (ewoc--node-start-marker node) nil)
366 (setf L (ewoc--node-left node)
367 R (ewoc--node-right node)
368 ;; Link neighbors to each other.
369 (ewoc--node-right L) R
370 (ewoc--node-left R) L
371 ;; Forget neighbors.
372 (ewoc--node-left node) nil
373 (ewoc--node-right node) nil))))
374
5b467bf4
SM
375(defun ewoc-filter (ewoc predicate &rest args)
376 "Remove all elements in EWOC for which PREDICATE returns nil.
a1506d29 377Note that the buffer for EWOC will be current-buffer when PREDICATE
0111ab41 378is called. PREDICATE must restore the current buffer before it returns
5b467bf4 379if it changes it.
0111ab41 380The PREDICATE is called with the element as its first argument. If any
5b467bf4
SM
381ARGS are given they will be passed to the PREDICATE."
382 (ewoc--set-buffer-bind-dll-let* ewoc
bb7a346f 383 ((node (ewoc--node-nth dll 1))
5b467bf4 384 (footer (ewoc--footer ewoc))
f569c26e 385 (goodbye nil)
2c246c9f 386 (inhibit-read-only t))
5b467bf4 387 (while (not (eq node footer))
5b467bf4 388 (unless (apply predicate (ewoc--node-data node) args)
f569c26e 389 (push node goodbye))
bb7a346f 390 (setq node (ewoc--node-next dll node)))
f569c26e 391 (apply 'ewoc-delete ewoc goodbye)))
5b467bf4 392
44946a4c 393(defun ewoc-locate (ewoc &optional pos guess)
5b467bf4 394 "Return the node that POS (a buffer position) is within.
44946a4c 395POS may be a marker or an integer. It defaults to point.
0111ab41 396GUESS should be a node that it is likely to be near POS.
5b467bf4
SM
397
398If POS points before the first element, the first node is returned.
399If POS points after the last element, the last node is returned.
400If the EWOC is empty, nil is returned."
44946a4c 401 (unless pos (setq pos (point)))
bb7a346f 402 (ewoc--set-buffer-bind-dll ewoc
5b467bf4
SM
403
404 (cond
405 ;; Nothing present?
bb7a346f 406 ((eq (ewoc--node-nth dll 1) (ewoc--node-nth dll -1))
5b467bf4
SM
407 nil)
408
409 ;; Before second elem?
bb7a346f
SM
410 ((< pos (ewoc--node-start-marker (ewoc--node-nth dll 2)))
411 (ewoc--node-nth dll 1))
5b467bf4
SM
412
413 ;; After one-before-last elem?
bb7a346f
SM
414 ((>= pos (ewoc--node-start-marker (ewoc--node-nth dll -2)))
415 (ewoc--node-nth dll -2))
5b467bf4
SM
416
417 ;; We now know that pos is within a elem.
418 (t
419 ;; Make an educated guess about which of the three known
420 ;; node'es (the first, the last, or GUESS) is nearest.
bb7a346f 421 (let* ((best-guess (ewoc--node-nth dll 1))
5b467bf4
SM
422 (distance (abs (- pos (ewoc--node-start-marker best-guess)))))
423 (when guess
424 (let ((d (abs (- pos (ewoc--node-start-marker guess)))))
425 (when (< d distance)
426 (setq distance d)
427 (setq best-guess guess))))
428
bb7a346f 429 (let* ((g (ewoc--node-nth dll -1)) ;Check the last elem
5b467bf4
SM
430 (d (abs (- pos (ewoc--node-start-marker g)))))
431 (when (< d distance)
432 (setq distance d)
433 (setq best-guess g)))
434
7dd2e64c 435 (when (ewoc--last-node ewoc) ;Check "previous".
5b467bf4
SM
436 (let* ((g (ewoc--last-node ewoc))
437 (d (abs (- pos (ewoc--node-start-marker g)))))
438 (when (< d distance)
439 (setq distance d)
440 (setq best-guess g))))
441
442 ;; best-guess is now a "best guess".
443 ;; Find the correct node. First determine in which direction
444 ;; it lies, and then move in that direction until it is found.
a1506d29 445
5b467bf4
SM
446 (cond
447 ;; Is pos after the guess?
448 ((>= pos
449 (ewoc--node-start-marker best-guess))
450 ;; Loop until we are exactly one node too far down...
451 (while (>= pos (ewoc--node-start-marker best-guess))
bb7a346f 452 (setq best-guess (ewoc--node-next dll best-guess)))
5b467bf4 453 ;; ...and return the previous node.
bb7a346f 454 (ewoc--node-prev dll best-guess))
5b467bf4
SM
455
456 ;; Pos is before best-guess
457 (t
458 (while (< pos (ewoc--node-start-marker best-guess))
bb7a346f 459 (setq best-guess (ewoc--node-prev dll best-guess)))
5b467bf4
SM
460 best-guess)))))))
461
462(defun ewoc-invalidate (ewoc &rest nodes)
d4ddf783
TTN
463 "Call EWOC's pretty-printer for each element in NODES.
464Delete current text first, thus effecting a \"refresh\"."
8433d470
TTN
465 (ewoc--set-buffer-bind-dll-let* ewoc
466 ((pp (ewoc--pretty-printer ewoc)))
bfbdb5ca
TTN
467 (save-excursion
468 (dolist (node nodes)
bb7a346f 469 (ewoc--refresh-node pp node dll)))))
5b467bf4 470
44946a4c 471(defun ewoc-goto-prev (ewoc arg)
d4ddf783 472 "Move point to the ARGth previous element in EWOC.
5b467bf4 473Don't move if we are at the first element, or if EWOC is empty.
d4ddf783 474Return the node we moved to."
5b467bf4 475 (ewoc--set-buffer-bind-dll-let* ewoc
6d84ac85 476 ((node (ewoc-locate ewoc (point))))
5b467bf4 477 (when node
44946a4c
SM
478 ;; If we were past the last element, first jump to it.
479 (when (>= (point) (ewoc--node-start-marker (ewoc--node-right node)))
480 (setq arg (1- arg)))
5b467bf4
SM
481 (while (and node (> arg 0))
482 (setq arg (1- arg))
bb7a346f 483 (setq node (ewoc--node-prev dll node)))
5b467bf4
SM
484 ;; Never step above the first element.
485 (unless (ewoc--filter-hf-nodes ewoc node)
bb7a346f 486 (setq node (ewoc--node-nth dll 1)))
5b467bf4
SM
487 (ewoc-goto-node ewoc node))))
488
44946a4c 489(defun ewoc-goto-next (ewoc arg)
d4ddf783
TTN
490 "Move point to the ARGth next element in EWOC.
491Return the node (or nil if we just passed the last node)."
5b467bf4 492 (ewoc--set-buffer-bind-dll-let* ewoc
6d84ac85 493 ((node (ewoc-locate ewoc (point))))
5b467bf4
SM
494 (while (and node (> arg 0))
495 (setq arg (1- arg))
bb7a346f 496 (setq node (ewoc--node-next dll node)))
5b467bf4 497 ;; Never step below the first element.
44946a4c 498 ;; (unless (ewoc--filter-hf-nodes ewoc node)
bb7a346f 499 ;; (setq node (ewoc--node-nth dll -2)))
5b467bf4
SM
500 (ewoc-goto-node ewoc node)))
501
502(defun ewoc-goto-node (ewoc node)
d4ddf783 503 "Move point to NODE in EWOC."
5b467bf4
SM
504 (ewoc--set-buffer-bind-dll ewoc
505 (goto-char (ewoc--node-start-marker node))
506 (if goal-column (move-to-column goal-column))
507 (setf (ewoc--last-node ewoc) node)))
508
509(defun ewoc-refresh (ewoc)
510 "Refresh all data in EWOC.
511The pretty-printer that was specified when the EWOC was created
512will be called for all elements in EWOC.
513Note that `ewoc-invalidate' is more efficient if only a small
514number of elements needs to be refreshed."
515 (ewoc--set-buffer-bind-dll-let* ewoc
cb3430a1 516 ((footer (ewoc--footer ewoc)))
5b467bf4 517 (let ((inhibit-read-only t))
bb7a346f 518 (delete-region (ewoc--node-start-marker (ewoc--node-nth dll 1))
5b467bf4
SM
519 (ewoc--node-start-marker footer))
520 (goto-char (ewoc--node-start-marker footer))
340d9945 521 (let ((pp (ewoc--pretty-printer ewoc))
bb7a346f 522 (node (ewoc--node-nth dll 1)))
5b467bf4
SM
523 (while (not (eq node footer))
524 (set-marker (ewoc--node-start-marker node) (point))
340d9945 525 (funcall pp (ewoc--node-data node))
bb7a346f 526 (setq node (ewoc--node-next dll node)))))
5b467bf4
SM
527 (set-marker (ewoc--node-start-marker footer) (point))))
528
529(defun ewoc-collect (ewoc predicate &rest args)
530 "Select elements from EWOC using PREDICATE.
531Return a list of all selected data elements.
0111ab41
JB
532PREDICATE is a function that takes a data element as its first
533argument. The elements on the returned list will appear in the
534same order as in the buffer. You should not rely on the order of
535calls to PREDICATE.
536Note that the buffer the EWOC is displayed in is the current
537buffer when PREDICATE is called. PREDICATE must restore it if it
538changes it.
5b467bf4
SM
539If more than two arguments are given the
540remaining arguments will be passed to PREDICATE."
541 (ewoc--set-buffer-bind-dll-let* ewoc
542 ((header (ewoc--header ewoc))
bb7a346f 543 (node (ewoc--node-nth dll -2))
5b467bf4
SM
544 result)
545 (while (not (eq node header))
546 (if (apply predicate (ewoc--node-data node) args)
547 (push (ewoc--node-data node) result))
bb7a346f 548 (setq node (ewoc--node-prev dll node)))
6d84ac85 549 (nreverse result)))
5b467bf4
SM
550
551(defun ewoc-buffer (ewoc)
552 "Return the buffer that is associated with EWOC.
d4ddf783 553Return nil if the buffer has been deleted."
5b467bf4
SM
554 (let ((buf (ewoc--buffer ewoc)))
555 (when (buffer-name buf) buf)))
556
cb3430a1
SM
557(defun ewoc-get-hf (ewoc)
558 "Return a cons cell containing the (HEADER . FOOTER) of EWOC."
559 (cons (ewoc--node-data (ewoc--header ewoc))
560 (ewoc--node-data (ewoc--footer ewoc))))
561
562(defun ewoc-set-hf (ewoc header footer)
563 "Set the HEADER and FOOTER of EWOC."
60eae434
TTN
564 (ewoc--set-buffer-bind-dll-let* ewoc
565 ((head (ewoc--header ewoc))
2bbacc0e
TTN
566 (foot (ewoc--footer ewoc))
567 (hf-pp (ewoc--hf-pp ewoc)))
60eae434
TTN
568 (setf (ewoc--node-data head) header
569 (ewoc--node-data foot) footer)
570 (save-excursion
bb7a346f
SM
571 (ewoc--refresh-node hf-pp head dll)
572 (ewoc--refresh-node hf-pp foot dll))))
cb3430a1 573
5b467bf4
SM
574\f
575(provide 'ewoc)
576
7ece7aba
SM
577;; Local Variables:
578;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
579;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
580;; End:
5b467bf4 581
7ece7aba 582;; arch-tag: d78915b9-9a07-44bf-aac6-04a1fc1bd6d4
5b467bf4 583;;; ewoc.el ends here