ewoc.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. ;;; ewoc.el --- utility to maintain a view of a list of objects in a buffer
  2. ;; Copyright (C) 1991-2012 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))
  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. (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. (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. (lexical-let ((ewoc--user-pp func))
  191. (lambda (data)
  192. (funcall ewoc--user-pp data)
  193. (insert "\n"))))
  194. ;;; ===========================================================================
  195. ;;; Public members of the Ewoc package
  196. ;;;###autoload
  197. (defun ewoc-create (pretty-printer &optional header footer nosep)
  198. "Create an empty ewoc.
  199. The ewoc will be inserted in the current buffer at the current position.
  200. PRETTY-PRINTER should be a function that takes one argument, an
  201. element, and inserts a string representing it in the buffer (at
  202. point). The string PRETTY-PRINTER inserts may be empty or span
  203. several lines. The PRETTY-PRINTER should use `insert', and not
  204. `insert-before-markers'.
  205. Optional second and third arguments HEADER and FOOTER are strings,
  206. possibly empty, that will always be present at the top and bottom,
  207. respectively, of the ewoc.
  208. Normally, a newline is automatically inserted after the header,
  209. the footer and every node's printed representation. Optional
  210. fourth arg NOSEP non-nil inhibits this."
  211. (let* ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST))
  212. (dll (progn (setf (ewoc--node-right dummy-node) dummy-node)
  213. (setf (ewoc--node-left dummy-node) dummy-node)
  214. dummy-node))
  215. (wrap (if nosep 'identity 'ewoc--wrap))
  216. (new-ewoc (ewoc--create (current-buffer)
  217. (funcall wrap pretty-printer)
  218. dll))
  219. (hf-pp (funcall wrap 'insert))
  220. (pos (point))
  221. head foot)
  222. (ewoc--set-buffer-bind-dll new-ewoc
  223. ;; Set default values
  224. (unless header (setq header ""))
  225. (unless footer (setq footer ""))
  226. (setf (ewoc--node-start-marker dll) (copy-marker pos)
  227. foot (ewoc--insert-new-node dll footer hf-pp dll)
  228. head (ewoc--insert-new-node foot header hf-pp dll)
  229. (ewoc--hf-pp new-ewoc) hf-pp
  230. (ewoc--footer new-ewoc) foot
  231. (ewoc--header new-ewoc) head))
  232. ;; Return the ewoc
  233. new-ewoc))
  234. (defalias 'ewoc-data 'ewoc--node-data
  235. "Extract the data encapsulated by NODE and return it.
  236. \(fn NODE)")
  237. (defun ewoc-set-data (node data)
  238. "Set NODE to encapsulate DATA."
  239. (setf (ewoc--node-data node) data))
  240. (defun ewoc-enter-first (ewoc data)
  241. "Enter DATA first in EWOC.
  242. Return the new node."
  243. (ewoc--set-buffer-bind-dll ewoc
  244. (ewoc-enter-after ewoc (ewoc--node-nth dll 0) data)))
  245. (defun ewoc-enter-last (ewoc data)
  246. "Enter DATA last in EWOC.
  247. Return the new node."
  248. (ewoc--set-buffer-bind-dll ewoc
  249. (ewoc-enter-before ewoc (ewoc--node-nth dll -1) data)))
  250. (defun ewoc-enter-after (ewoc node data)
  251. "Enter a new element DATA after NODE in EWOC.
  252. Return the new node."
  253. (ewoc--set-buffer-bind-dll ewoc
  254. (ewoc-enter-before ewoc (ewoc--node-next dll node) data)))
  255. (defun ewoc-enter-before (ewoc node data)
  256. "Enter a new element DATA before NODE in EWOC.
  257. Return the new node."
  258. (ewoc--set-buffer-bind-dll ewoc
  259. (ewoc--insert-new-node node data (ewoc--pretty-printer ewoc) dll)))
  260. (defun ewoc-next (ewoc node)
  261. "Return the node in EWOC that follows NODE.
  262. Return nil if NODE is nil or the last element."
  263. (when node
  264. (ewoc--filter-hf-nodes
  265. ewoc (ewoc--node-next (ewoc--dll ewoc) node))))
  266. (defun ewoc-prev (ewoc node)
  267. "Return the node in EWOC that precedes NODE.
  268. Return nil if NODE is nil or the first element."
  269. (when node
  270. (ewoc--filter-hf-nodes
  271. ewoc (ewoc--node-prev (ewoc--dll ewoc) node))))
  272. (defun ewoc-nth (ewoc n)
  273. "Return the Nth node.
  274. N counts from zero. Return nil if there is less than N elements.
  275. If N is negative, return the -(N+1)th last element.
  276. Thus, (ewoc-nth ewoc 0) returns the first node,
  277. and (ewoc-nth ewoc -1) returns the last node.
  278. Use `ewoc-data' to extract the data from the node."
  279. ;; Skip the header (or footer, if n is negative).
  280. (setq n (if (< n 0) (1- n) (1+ n)))
  281. (ewoc--filter-hf-nodes ewoc
  282. (ewoc--node-nth (ewoc--dll ewoc) n)))
  283. (defun ewoc-map (map-function ewoc &rest args)
  284. "Apply MAP-FUNCTION to all elements in EWOC.
  285. MAP-FUNCTION is applied to the first element first.
  286. If MAP-FUNCTION returns non-nil the element will be refreshed (its
  287. pretty-printer will be called once again).
  288. Note that the buffer for EWOC will be the current buffer when
  289. MAP-FUNCTION is called. MAP-FUNCTION must restore the current
  290. buffer before it returns, if it changes it.
  291. If more than two arguments are given, the remaining
  292. arguments will be passed to MAP-FUNCTION."
  293. (ewoc--set-buffer-bind-dll-let* ewoc
  294. ((footer (ewoc--footer ewoc))
  295. (pp (ewoc--pretty-printer ewoc))
  296. (node (ewoc--node-nth dll 1)))
  297. (save-excursion
  298. (while (not (eq node footer))
  299. (if (apply map-function (ewoc--node-data node) args)
  300. (ewoc--refresh-node pp node dll))
  301. (setq node (ewoc--node-next dll node))))))
  302. (defun ewoc-delete (ewoc &rest nodes)
  303. "Delete NODES from EWOC."
  304. (ewoc--set-buffer-bind-dll-let* ewoc
  305. ((L nil) (R nil) (last (ewoc--last-node ewoc)))
  306. (dolist (node nodes)
  307. ;; If we are about to delete the node pointed at by last-node,
  308. ;; set last-node to nil.
  309. (when (eq last node)
  310. (setf last nil (ewoc--last-node ewoc) nil))
  311. (delete-region (ewoc--node-start-marker node)
  312. (ewoc--node-start-marker (ewoc--node-next dll node)))
  313. (set-marker (ewoc--node-start-marker node) nil)
  314. (setf L (ewoc--node-left node)
  315. R (ewoc--node-right node)
  316. ;; Link neighbors to each other.
  317. (ewoc--node-right L) R
  318. (ewoc--node-left R) L
  319. ;; Forget neighbors.
  320. (ewoc--node-left node) nil
  321. (ewoc--node-right node) nil))))
  322. (defun ewoc-filter (ewoc predicate &rest args)
  323. "Remove all elements in EWOC for which PREDICATE returns nil.
  324. Note that the buffer for EWOC will be current-buffer when PREDICATE
  325. is called. PREDICATE must restore the current buffer before it returns
  326. if it changes it.
  327. The PREDICATE is called with the element as its first argument. If any
  328. ARGS are given they will be passed to the PREDICATE."
  329. (ewoc--set-buffer-bind-dll-let* ewoc
  330. ((node (ewoc--node-nth dll 1))
  331. (footer (ewoc--footer ewoc))
  332. (goodbye nil)
  333. (inhibit-read-only t))
  334. (while (not (eq node footer))
  335. (unless (apply predicate (ewoc--node-data node) args)
  336. (push node goodbye))
  337. (setq node (ewoc--node-next dll node)))
  338. (apply 'ewoc-delete ewoc goodbye)))
  339. (defun ewoc-locate (ewoc &optional pos guess)
  340. "Return the node that POS (a buffer position) is within.
  341. POS may be a marker or an integer. It defaults to point.
  342. GUESS should be a node that it is likely to be near POS.
  343. If POS points before the first element, the first node is returned.
  344. If POS points after the last element, the last node is returned.
  345. If the EWOC is empty, nil is returned."
  346. (unless pos (setq pos (point)))
  347. (ewoc--set-buffer-bind-dll ewoc
  348. (cond
  349. ;; Nothing present?
  350. ((eq (ewoc--node-nth dll 1) (ewoc--node-nth dll -1))
  351. nil)
  352. ;; Before second elem?
  353. ((< pos (ewoc--node-start-marker (ewoc--node-nth dll 2)))
  354. (ewoc--node-nth dll 1))
  355. ;; After one-before-last elem?
  356. ((>= pos (ewoc--node-start-marker (ewoc--node-nth dll -2)))
  357. (ewoc--node-nth dll -2))
  358. ;; We now know that pos is within a elem.
  359. (t
  360. ;; Make an educated guess about which of the three known
  361. ;; node'es (the first, the last, or GUESS) is nearest.
  362. (let* ((best-guess (ewoc--node-nth dll 1))
  363. (distance (abs (- pos (ewoc--node-start-marker best-guess)))))
  364. (when guess
  365. (let ((d (abs (- pos (ewoc--node-start-marker guess)))))
  366. (when (< d distance)
  367. (setq distance d)
  368. (setq best-guess guess))))
  369. (let* ((g (ewoc--node-nth dll -1)) ;Check the last elem
  370. (d (abs (- pos (ewoc--node-start-marker g)))))
  371. (when (< d distance)
  372. (setq distance d)
  373. (setq best-guess g)))
  374. (when (ewoc--last-node ewoc) ;Check "previous".
  375. (let* ((g (ewoc--last-node ewoc))
  376. (d (abs (- pos (ewoc--node-start-marker g)))))
  377. (when (< d distance)
  378. (setq distance d)
  379. (setq best-guess g))))
  380. ;; best-guess is now a "best guess".
  381. ;; Find the correct node. First determine in which direction
  382. ;; it lies, and then move in that direction until it is found.
  383. (cond
  384. ;; Is pos after the guess?
  385. ((>= pos
  386. (ewoc--node-start-marker best-guess))
  387. ;; Loop until we are exactly one node too far down...
  388. (while (>= pos (ewoc--node-start-marker best-guess))
  389. (setq best-guess (ewoc--node-next dll best-guess)))
  390. ;; ...and return the previous node.
  391. (ewoc--node-prev dll best-guess))
  392. ;; Pos is before best-guess
  393. (t
  394. (while (< pos (ewoc--node-start-marker best-guess))
  395. (setq best-guess (ewoc--node-prev dll best-guess)))
  396. best-guess)))))))
  397. (defun ewoc-invalidate (ewoc &rest nodes)
  398. "Call EWOC's pretty-printer for each element in NODES.
  399. Delete current text first, thus effecting a \"refresh\"."
  400. (ewoc--set-buffer-bind-dll-let* ewoc
  401. ((pp (ewoc--pretty-printer ewoc)))
  402. (save-excursion
  403. (dolist (node nodes)
  404. (ewoc--refresh-node pp node dll)))))
  405. (defun ewoc-goto-prev (ewoc arg)
  406. "Move point to the ARGth previous element in EWOC.
  407. Don't move if we are at the first element, or if EWOC is empty.
  408. Return the node we moved to."
  409. (ewoc--set-buffer-bind-dll-let* ewoc
  410. ((node (ewoc-locate ewoc (point))))
  411. (when node
  412. ;; If we were past the last element, first jump to it.
  413. (when (>= (point) (ewoc--node-start-marker (ewoc--node-right node)))
  414. (setq arg (1- arg)))
  415. (while (and node (> arg 0))
  416. (setq arg (1- arg))
  417. (setq node (ewoc--node-prev dll node)))
  418. ;; Never step above the first element.
  419. (unless (ewoc--filter-hf-nodes ewoc node)
  420. (setq node (ewoc--node-nth dll 1)))
  421. (ewoc-goto-node ewoc node))))
  422. (defun ewoc-goto-next (ewoc arg)
  423. "Move point to the ARGth next element in EWOC.
  424. Return the node (or nil if we just passed the last node)."
  425. (ewoc--set-buffer-bind-dll-let* ewoc
  426. ((node (ewoc-locate ewoc (point))))
  427. (while (and node (> arg 0))
  428. (setq arg (1- arg))
  429. (setq node (ewoc--node-next dll node)))
  430. ;; Never step below the first element.
  431. ;; (unless (ewoc--filter-hf-nodes ewoc node)
  432. ;; (setq node (ewoc--node-nth dll -2)))
  433. (unless node
  434. (error "No next"))
  435. (ewoc-goto-node ewoc node)))
  436. (defun ewoc-goto-node (ewoc node)
  437. "Move point to NODE in EWOC."
  438. (ewoc--set-buffer-bind-dll ewoc
  439. (goto-char (ewoc--node-start-marker node))
  440. (if goal-column (move-to-column goal-column))
  441. (setf (ewoc--last-node ewoc) node)))
  442. (defun ewoc-refresh (ewoc)
  443. "Refresh all data in EWOC.
  444. The pretty-printer that was specified when the EWOC was created
  445. will be called for all elements in EWOC.
  446. Note that `ewoc-invalidate' is more efficient if only a small
  447. number of elements needs to be refreshed."
  448. (ewoc--set-buffer-bind-dll-let* ewoc
  449. ((footer (ewoc--footer ewoc)))
  450. (let ((inhibit-read-only t))
  451. (delete-region (ewoc--node-start-marker (ewoc--node-nth dll 1))
  452. (ewoc--node-start-marker footer))
  453. (goto-char (ewoc--node-start-marker footer))
  454. (let ((pp (ewoc--pretty-printer ewoc))
  455. (node (ewoc--node-nth dll 1)))
  456. (while (not (eq node footer))
  457. (set-marker (ewoc--node-start-marker node) (point))
  458. (funcall pp (ewoc--node-data node))
  459. (setq node (ewoc--node-next dll node)))))
  460. (set-marker (ewoc--node-start-marker footer) (point))))
  461. (defun ewoc-collect (ewoc predicate &rest args)
  462. "Select elements from EWOC using PREDICATE.
  463. Return a list of all selected data elements.
  464. PREDICATE is a function that takes a data element as its first
  465. argument. The elements on the returned list will appear in the
  466. same order as in the buffer. You should not rely on the order of
  467. calls to PREDICATE.
  468. Note that the buffer the EWOC is displayed in is the current
  469. buffer when PREDICATE is called. PREDICATE must restore it if it
  470. changes it.
  471. If more than two arguments are given the
  472. remaining arguments will be passed to PREDICATE."
  473. (ewoc--set-buffer-bind-dll-let* ewoc
  474. ((header (ewoc--header ewoc))
  475. (node (ewoc--node-nth dll -2))
  476. result)
  477. (while (not (eq node header))
  478. (if (apply predicate (ewoc--node-data node) args)
  479. (push (ewoc--node-data node) result))
  480. (setq node (ewoc--node-prev dll node)))
  481. result))
  482. (defun ewoc-buffer (ewoc)
  483. "Return the buffer that is associated with EWOC.
  484. Return nil if the buffer has been deleted."
  485. (let ((buf (ewoc--buffer ewoc)))
  486. (when (buffer-name buf) buf)))
  487. (defun ewoc-get-hf (ewoc)
  488. "Return a cons cell containing the (HEADER . FOOTER) of EWOC."
  489. (cons (ewoc--node-data (ewoc--header ewoc))
  490. (ewoc--node-data (ewoc--footer ewoc))))
  491. (defun ewoc-set-hf (ewoc header footer)
  492. "Set the HEADER and FOOTER of EWOC."
  493. (ewoc--set-buffer-bind-dll-let* ewoc
  494. ((head (ewoc--header ewoc))
  495. (foot (ewoc--footer ewoc))
  496. (hf-pp (ewoc--hf-pp ewoc)))
  497. (setf (ewoc--node-data head) header
  498. (ewoc--node-data foot) footer)
  499. (save-excursion
  500. (ewoc--refresh-node hf-pp head dll)
  501. (ewoc--refresh-node hf-pp foot dll))))
  502. (provide 'ewoc)
  503. ;; Local Variables:
  504. ;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
  505. ;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
  506. ;; End:
  507. ;;; ewoc.el ends here