ewoc.el 22 KB

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