PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/distel/elisp/ewoc.el

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