summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/ewoc.el
diff options
context:
space:
mode:
authorKaroly Lorentey <lorentey@elte.hu>2006-06-12 07:27:12 +0000
committerKaroly Lorentey <lorentey@elte.hu>2006-06-12 07:27:12 +0000
commit476e9367ec1f440aa23904b7bc482ea4a3b8041c (patch)
tree4f7f5a5e9a6668f908834bb6e216c8fa3727d4b3 /lisp/emacs-lisp/ewoc.el
parenta13f8f50d4cc544d3bbfa78568e82ce09e68bded (diff)
parent6b519504c3297595101628e823e72c91e562ab45 (diff)
downloademacs-476e9367ec1f440aa23904b7bc482ea4a3b8041c.tar.gz
Merged from emacs@sv.gnu.org.
Patches applied: * emacs@sv.gnu.org/emacs--devo--0--patch-294 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-295 Merge from gnus--rel--5.10 * emacs@sv.gnu.org/emacs--devo--0--patch-296 Update from CVS: admin/FOR-RELEASE: Update refcard section. * emacs@sv.gnu.org/emacs--devo--0--patch-297 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-298 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-299 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-300 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-301 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-302 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-303 Update from CVS * emacs@sv.gnu.org/emacs--devo--0--patch-304 Update from CVS * emacs@sv.gnu.org/gnus--rel--5.10--patch-103 Update from CVS * emacs@sv.gnu.org/gnus--rel--5.10--patch-104 Update from CVS git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-570
Diffstat (limited to 'lisp/emacs-lisp/ewoc.el')
-rw-r--r--lisp/emacs-lisp/ewoc.el172
1 files changed, 73 insertions, 99 deletions
diff --git a/lisp/emacs-lisp/ewoc.el b/lisp/emacs-lisp/ewoc.el
index 2cb90738072..b4857f4310d 100644
--- a/lisp/emacs-lisp/ewoc.el
+++ b/lisp/emacs-lisp/ewoc.el
@@ -88,36 +88,7 @@
;; limit! It is even possible to have another ewoc as an
;; element. In that way some kind of tree hierarchy can be created.
;;
-;; Full documentation will, God willing, soon be available in a
-;; Texinfo manual.
-
-;; In the mean time `grep '^(.*ewoc-[^-]' emacs-lisp/ewoc.el' can help
-;; you find all the exported functions:
-;;
-;; (defun ewoc-create (pretty-printer &optional header footer)
-;; (defalias 'ewoc-data 'ewoc--node-data)
-;; (defun ewoc-set-data (node data)
-;; (defun ewoc-location (node)
-;; (defun ewoc-enter-first (ewoc data)
-;; (defun ewoc-enter-last (ewoc data)
-;; (defun ewoc-enter-after (ewoc node data)
-;; (defun ewoc-enter-before (ewoc node data)
-;; (defun ewoc-next (ewoc node)
-;; (defun ewoc-prev (ewoc node)
-;; (defun ewoc-nth (ewoc n)
-;; (defun ewoc-map (map-function ewoc &rest args)
-;; (defun ewoc-filter (ewoc predicate &rest args)
-;; (defun ewoc-delete (ewoc &rest nodes)
-;; (defun ewoc-locate (ewoc &optional pos guess)
-;; (defun ewoc-invalidate (ewoc &rest nodes)
-;; (defun ewoc-goto-prev (ewoc arg)
-;; (defun ewoc-goto-next (ewoc arg)
-;; (defun ewoc-goto-node (ewoc node)
-;; (defun ewoc-refresh (ewoc)
-;; (defun ewoc-collect (ewoc predicate &rest args)
-;; (defun ewoc-buffer (ewoc)
-;; (defun ewoc-get-hf (ewoc)
-;; (defun ewoc-set-hf (ewoc header footer)
+;; The Emacs Lisp Reference Manual documents ewoc.el's "public interface".
;; Coding conventions
;; ==================
@@ -125,48 +96,43 @@
;; All functions of course start with `ewoc'. Functions and macros
;; starting with the prefix `ewoc--' are meant for internal use,
;; while those starting with `ewoc-' are exported for public use.
-;; There are currently no global or buffer-local variables used.
-
;;; Code:
-(eval-when-compile (require 'cl)) ;because of CL compiler macros
-
-;; The doubly linked list is implemented as a circular list
-;; with a dummy node first and last. The dummy node is used as
-;; "the dll" (or rather is the dll handle passed around).
+(eval-when-compile (require 'cl))
+;; The doubly linked list is implemented as a circular list with a dummy
+;; node first and last. The dummy node is used as "the dll".
(defstruct (ewoc--node
- (:type vector) ;required for ewoc--node-branch hack
+ (:type vector) ;ewoc--node-nth needs this
+ (:constructor nil)
(:constructor ewoc--node-create (start-marker data)))
left right data start-marker)
-(defalias 'ewoc--node-branch 'aref
- "Get the left (CHILD=0) or right (CHILD=1) child of the NODE.
-
-\(fn NODE CHILD)")
-
(defun ewoc--node-next (dll node)
"Return the node after NODE, or nil if NODE is the last node."
- (unless (eq (ewoc--node-right node) dll) (ewoc--node-right node)))
+ (let ((R (ewoc--node-right node)))
+ (unless (eq dll R) R)))
(defun ewoc--node-prev (dll node)
"Return the node before NODE, or nil if NODE is the first node."
- (unless (eq (ewoc--node-left node) dll) (ewoc--node-left node)))
+ (let ((L (ewoc--node-left node)))
+ (unless (eq dll L) L)))
(defun ewoc--node-nth (dll n)
- "Return the Nth node from the doubly linked list DLL.
-N counts from zero. If DLL is not that long, nil is returned.
-If N is negative, return the -(N+1)th last element.
+ "Return the Nth node from the doubly linked list `dll'.
+N counts from zero. If N is negative, return the -(N+1)th last element.
+If N is out of range, return nil.
Thus, (ewoc--node-nth dll 0) returns the first node,
and (ewoc--node-nth dll -1) returns the last node."
+ ;; Presuming a node is ":type vector", starting with `left' and `right':
;; Branch 0 ("follow left pointer") is used when n is negative.
;; Branch 1 ("follow right pointer") is used otherwise.
(let* ((branch (if (< n 0) 0 1))
- (node (ewoc--node-branch dll branch)))
+ (node (aref dll branch)))
(if (< n 0) (setq n (- -1 n)))
(while (and (not (eq dll node)) (> n 0))
- (setq node (ewoc--node-branch node branch))
+ (setq node (aref node branch))
(setq n (1- n)))
(unless (eq dll node) node)))
@@ -179,16 +145,15 @@ and (ewoc--node-nth dll -1) returns the last node."
(defstruct (ewoc
(:constructor nil)
- (:constructor ewoc--create
- (buffer pretty-printer header footer dll))
+ (:constructor ewoc--create (buffer pretty-printer dll))
(:conc-name ewoc--))
- buffer pretty-printer header footer dll last-node)
+ buffer pretty-printer header footer dll last-node hf-pp)
(defmacro ewoc--set-buffer-bind-dll-let* (ewoc varlist &rest forms)
"Execute FORMS with ewoc--buffer selected as current buffer,
-dll bound to ewoc--dll, and VARLIST bound as in a let*.
-dll will be bound when VARLIST is initialized, but the current
-buffer will *not* have been changed.
+`dll' bound to the dll, and VARLIST bound as in a let*.
+`dll' will be bound when VARLIST is initialized, but
+the current buffer will *not* have been changed.
Return value of last form in FORMS."
(let ((hnd (make-symbol "ewoc")))
`(let* ((,hnd ,ewoc)
@@ -207,17 +172,20 @@ BUT if it is the header or the footer in EWOC return nil instead."
(eq node (ewoc--footer ewoc)))
node))
-(defun ewoc--adjust (beg end node)
+(defun ewoc--adjust (beg end node dll)
;; "Manually reseat" markers for NODE and its successors (including footer
;; and dll), in the case where they originally shared start position with
;; BEG, to END. BEG and END are buffer positions describing NODE's left
;; neighbor. This operation is functionally equivalent to temporarily
;; setting these nodes' markers' insertion type to t around the pretty-print
- ;; call that precedes the call to `ewoc-adjust', and then changing them back
+ ;; call that precedes the call to `ewoc--adjust', and then changing them back
;; to nil.
(when (< beg end)
(let (m)
(while (and (= beg (setq m (ewoc--node-start-marker node)))
+ ;; The "dummy" node `dll' actually holds the marker that
+ ;; points to the end of the footer, so we check `dll'
+ ;; *after* reseating the marker.
(progn
(set-marker m end)
(not (eq dll node))))
@@ -228,21 +196,16 @@ BUT if it is the header or the footer in EWOC return nil instead."
Call PRETTY-PRINTER with point at NODE's start, thus pushing back
NODE and leaving the new node's start there. Return the new node."
(save-excursion
- (let* ((inhibit-read-only t)
- (m (copy-marker (ewoc--node-start-marker node)))
- (pos (marker-position m))
- (elemnode (ewoc--node-create m data)))
- (goto-char pos)
- (funcall pretty-printer data)
- (setf (marker-position m) pos
- (ewoc--node-left elemnode) (ewoc--node-left node)
+ (let ((elemnode (ewoc--node-create
+ (copy-marker (ewoc--node-start-marker node)) data)))
+ (setf (ewoc--node-left elemnode) (ewoc--node-left node)
(ewoc--node-right elemnode) node
(ewoc--node-right (ewoc--node-left node)) elemnode
(ewoc--node-left node) elemnode)
- (ewoc--adjust pos (point) node)
+ (ewoc--refresh-node pretty-printer elemnode dll)
elemnode)))
-(defun ewoc--refresh-node (pp node)
+(defun ewoc--refresh-node (pp node dll)
"Redisplay the element represented by NODE using the pretty-printer PP."
(let ((inhibit-read-only t)
(m (ewoc--node-start-marker node))
@@ -252,13 +215,20 @@ NODE and leaving the new node's start there. Return the new node."
;; Calculate and insert the string.
(goto-char m)
(funcall pp (ewoc--node-data node))
- (ewoc--adjust m (point) R)))
+ (ewoc--adjust m (point) R dll)))
+
+(defun ewoc--wrap (func)
+ (lexical-let ((ewoc--user-pp func))
+ (lambda (data)
+ (funcall ewoc--user-pp data)
+ (insert "\n"))))
+
;;; ===========================================================================
;;; Public members of the Ewoc package
;;;###autoload
-(defun ewoc-create (pretty-printer &optional header footer)
+(defun ewoc-create (pretty-printer &optional header footer nosep)
"Create an empty ewoc.
The ewoc will be inserted in the current buffer at the current position.
@@ -271,14 +241,20 @@ several lines. The PRETTY-PRINTER should use `insert', and not
Optional second and third arguments HEADER and FOOTER are strings,
possibly empty, that will always be present at the top and bottom,
-respectively, of the ewoc."
+respectively, of the ewoc.
+
+Normally, a newline is automatically inserted after the header,
+the footer and every node's printed representation. Optional
+fourth arg NOSEP non-nil inhibits this."
(let* ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST))
(dll (progn (setf (ewoc--node-right dummy-node) dummy-node)
(setf (ewoc--node-left dummy-node) dummy-node)
dummy-node))
- (new-ewoc
- (ewoc--create (current-buffer)
- pretty-printer nil nil dll))
+ (wrap (if nosep 'identity 'ewoc--wrap))
+ (new-ewoc (ewoc--create (current-buffer)
+ (funcall wrap pretty-printer)
+ dll))
+ (hf-pp (funcall wrap 'insert))
(pos (point))
head foot)
(ewoc--set-buffer-bind-dll new-ewoc
@@ -286,8 +262,9 @@ respectively, of the ewoc."
(unless header (setq header ""))
(unless footer (setq footer ""))
(setf (ewoc--node-start-marker dll) (copy-marker pos)
- foot (ewoc--insert-new-node dll footer 'insert)
- head (ewoc--insert-new-node foot header 'insert)
+ foot (ewoc--insert-new-node dll footer hf-pp)
+ head (ewoc--insert-new-node foot header hf-pp)
+ (ewoc--hf-pp new-ewoc) hf-pp
(ewoc--footer new-ewoc) foot
(ewoc--header new-ewoc) head))
;; Return the ewoc
@@ -314,7 +291,6 @@ Return the new node."
(ewoc--set-buffer-bind-dll ewoc
(ewoc-enter-before ewoc (ewoc--node-nth dll -1) data)))
-
(defun ewoc-enter-after (ewoc node data)
"Enter a new element DATA after NODE in EWOC.
Return the new node."
@@ -339,21 +315,19 @@ Return nil if NODE is nil or the last element."
Return nil if NODE is nil or the first element."
(when node
(ewoc--filter-hf-nodes
- ewoc
- (ewoc--node-prev (ewoc--dll ewoc) node))))
-
+ ewoc (ewoc--node-prev (ewoc--dll ewoc) node))))
(defun ewoc-nth (ewoc n)
"Return the Nth node.
N counts from zero. Return nil if there is less than N elements.
If N is negative, return the -(N+1)th last element.
-Thus, (ewoc-nth dll 0) returns the first node,
-and (ewoc-nth dll -1) returns the last node.
+Thus, (ewoc-nth ewoc 0) returns the first node,
+and (ewoc-nth ewoc -1) returns the last node.
Use `ewoc-data' to extract the data from the node."
;; Skip the header (or footer, if n is negative).
(setq n (if (< n 0) (1- n) (1+ n)))
(ewoc--filter-hf-nodes ewoc
- (ewoc--node-nth (ewoc--dll ewoc) n)))
+ (ewoc--node-nth (ewoc--dll ewoc) n)))
(defun ewoc-map (map-function ewoc &rest args)
"Apply MAP-FUNCTION to all elements in EWOC.
@@ -374,18 +348,18 @@ arguments will be passed to MAP-FUNCTION."
(save-excursion
(while (not (eq node footer))
(if (apply map-function (ewoc--node-data node) args)
- (ewoc--refresh-node pp node))
+ (ewoc--refresh-node pp node dll))
(setq node (ewoc--node-next dll node))))))
(defun ewoc-delete (ewoc &rest nodes)
"Delete NODES from EWOC."
(ewoc--set-buffer-bind-dll-let* ewoc
- ((L nil) (R nil))
+ ((L nil) (R nil) (last (ewoc--last-node ewoc)))
(dolist (node nodes)
;; If we are about to delete the node pointed at by last-node,
;; set last-node to nil.
- (if (eq (ewoc--last-node ewoc) node)
- (setf (ewoc--last-node ewoc) nil))
+ (when (eq last node)
+ (setf last nil (ewoc--last-node ewoc) nil))
(delete-region (ewoc--node-start-marker node)
(ewoc--node-start-marker (ewoc--node-next dll node)))
(set-marker (ewoc--node-start-marker node) nil)
@@ -425,8 +399,7 @@ If POS points before the first element, the first node is returned.
If POS points after the last element, the last node is returned.
If the EWOC is empty, nil is returned."
(unless pos (setq pos (point)))
- (ewoc--set-buffer-bind-dll-let* ewoc
- ((footer (ewoc--footer ewoc)))
+ (ewoc--set-buffer-bind-dll ewoc
(cond
;; Nothing present?
@@ -459,7 +432,7 @@ If the EWOC is empty, nil is returned."
(setq distance d)
(setq best-guess g)))
- (when (ewoc--last-node ewoc) ;Check "previous".
+ (when (ewoc--last-node ewoc) ;Check "previous".
(let* ((g (ewoc--last-node ewoc))
(d (abs (- pos (ewoc--node-start-marker g)))))
(when (< d distance)
@@ -493,7 +466,7 @@ Delete current text first, thus effecting a \"refresh\"."
((pp (ewoc--pretty-printer ewoc)))
(save-excursion
(dolist (node nodes)
- (ewoc--refresh-node pp node)))))
+ (ewoc--refresh-node pp node dll)))))
(defun ewoc-goto-prev (ewoc arg)
"Move point to the ARGth previous element in EWOC.
@@ -590,20 +563,21 @@ Return nil if the buffer has been deleted."
"Set the HEADER and FOOTER of EWOC."
(ewoc--set-buffer-bind-dll-let* ewoc
((head (ewoc--header ewoc))
- (foot (ewoc--footer ewoc)))
+ (foot (ewoc--footer ewoc))
+ (hf-pp (ewoc--hf-pp ewoc)))
(setf (ewoc--node-data head) header
(ewoc--node-data foot) footer)
(save-excursion
- (ewoc--refresh-node 'insert head)
- (ewoc--refresh-node 'insert foot))))
+ (ewoc--refresh-node hf-pp head dll)
+ (ewoc--refresh-node hf-pp foot dll))))
(provide 'ewoc)
-;;; Local Variables:
-;;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
-;;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
-;;; End:
+;; Local Variables:
+;; eval: (put 'ewoc--set-buffer-bind-dll 'lisp-indent-hook 1)
+;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
+;; End:
-;;; arch-tag: d78915b9-9a07-44bf-aac6-04a1fc1bd6d4
+;; arch-tag: d78915b9-9a07-44bf-aac6-04a1fc1bd6d4
;;; ewoc.el ends here