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