]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/ewoc.el
(Abbrevs): A @node line without explicit Prev, Next, and Up links.
[gnu-emacs] / lisp / emacs-lisp / ewoc.el
index 2af8dd492982ae3d94fe3d9b16b718414ed4d910..74931c3c34a14f9c05f2a5fec05ce314b00d56a7 100644 (file)
@@ -1,6 +1,7 @@
-;;; ewoc.el -- Utility to maintain a view of a list of objects in a buffer
+;;; ewoc.el --- utility to maintain a view of a list of objects in a buffer
 
-;; Copyright (C) 1991-2000   Free Software Foundation
+;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+;;   2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: Per Cederqvist <ceder@lysator.liu.se>
 ;;     Inge Wallin <inge@lysator.liu.se>
@@ -22,8 +23,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -31,9 +32,9 @@
 ;; But now it's Emacs' Widget for Object Collections
 
 ;; As the name implies this derives from the `cookie' package (part
-;; of Elib).  The changes are mostly superficial:
+;; of Elib).  The changes are pervasive though mostly superficial:
 
-;; - uses CL (and its `defstruct'
+;; - uses CL (and its `defstruct')
 ;; - separate from Elib.
 ;; - uses its own version of a doubly-linked list which allows us
 ;;   to merge the elib-wrapper and the elib-node structures into ewoc-node
 ;; certain point in a certain buffer.  (The buffer and point are
 ;; fixed when the ewoc is created).  The header and the footer
 ;; are constant strings.  They appear before and after the elements.
-;; (Currently, once set, they can not be changed).
 ;;
 ;; Ewoc does not affect the mode of the buffer in any way. It
 ;; merely makes it easy to connect an underlying data representation
 ;; to the buffer contents.
 ;;
 ;; A `ewoc--node' is an object that contains one element.  There are
-;; functions in this package that given an ewoc--node extracts the data, or
-;; gives the next or previous ewoc--node.  (All ewoc--nodes are linked together
-;; in a doubly linked list.  The 'previous' ewoc--node is the one that appears
+;; functions in this package that given an ewoc--node extract the data, or
+;; give the next or previous ewoc--node.  (All ewoc--nodes are linked together
+;; in a doubly linked list.  The `previous' ewoc--node is the one that appears
 ;; before the other in the buffer.)  You should not do anything with
 ;; an ewoc--node except pass it to the functions in this package.
 ;;
 
 ;; In the mean time `grep '^(.*ewoc-[^-]' emacs-lisp/ewoc.el' can help
 ;; you find all the exported functions:
-;; 
-;; (defun ewoc-create (buffer pretty-printer &optional header footer pos)
+;;
+;; (defun ewoc-create (pretty-printer &optional header footer)
 ;; (defalias 'ewoc-data 'ewoc--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-nth (ewoc n)
 ;; (defun ewoc-map (map-function ewoc &rest args)
 ;; (defun ewoc-filter (ewoc predicate &rest args)
-;; (defun ewoc-locate (ewoc pos &optional guess)
+;; (defun ewoc-locate (ewoc &optional pos guess)
 ;; (defun ewoc-invalidate (ewoc &rest nodes)
-;; (defun ewoc-goto-prev (ewoc pos arg)
-;; (defun ewoc-goto-next (ewoc pos arg)
+;; (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)
 
 ;;     Coding conventions
 ;;     ==================
            (:constructor ewoc--node-create (start-marker data)))
   left right data start-marker)
 
-(defalias 'ewoc--node-branch 'aref)
+(defalias 'ewoc--node-branch 'aref
+  "Get the left (CHILD=0) or right (CHILD=1) child of the NODE.
+
+\(fn NODE CHILD)")
 
 (defun ewoc--dll-create ()
   "Create an empty doubly linked list."
@@ -196,6 +201,10 @@ and (ewoc--node-nth dll -1) returns the last node."
       (setq n (1- n)))
     (unless (eq dll node) node)))
 
+(defun ewoc-location (node)
+  "Return the start location of NODE."
+  (ewoc--node-start-marker node))
+
 \f
 ;;; The ewoc data type
 
@@ -214,14 +223,14 @@ buffer will *not* have been changed.
 Return value of last form in FORMS."
   (let ((old-buffer (make-symbol "old-buffer"))
        (hnd (make-symbol "ewoc")))
-    (` (let* (((, old-buffer) (current-buffer))
-             ((, hnd) (, ewoc))
-             (dll (ewoc--dll (, hnd)))
-             (,@ varlist))
-        (set-buffer (ewoc--buffer (, hnd)))
-        (unwind-protect
-            (progn (,@ forms))
-          (set-buffer (, old-buffer)))))))
+    `(let* ((,old-buffer (current-buffer))
+            (,hnd ,ewoc)
+            (dll (ewoc--dll ,hnd))
+            ,@varlist)
+      (set-buffer (ewoc--buffer ,hnd))
+      (unwind-protect
+           (progn ,@forms)
+        (set-buffer ,old-buffer)))))
 
 (defmacro ewoc--set-buffer-bind-dll (ewoc &rest forms)
   `(ewoc--set-buffer-bind-dll-let* ,ewoc nil ,@forms))
@@ -234,30 +243,9 @@ BUT if it is the header or the footer in EWOC return nil instead."
     node))
 
 
-(defun ewoc--create-special-node (data string pos)
-  "Insert STRING at POS in current buffer. Remember the start
-position. Create a wrapper containing that start position and the
-element DATA."
-  (save-excursion
-    ;; Remember the position as a number so that it doesn't move
-    ;; when we insert the string.
-    (when (markerp pos) (setq pos (marker-position pos)))
-    (goto-char pos)
-    (let ((inhibit-read-only t))
-      ;; Use insert-before-markers so that the marker for the
-      ;; next element is updated.
-      (insert-before-markers string)
-      ;; Always insert a newline. You want invisible elements? You
-      ;; lose. (At least in this version). FIXME-someday. (It is
-      ;; harder to fix than it might seem. All markers have to point
-      ;; to the right place all the time...)
-      (insert-before-markers ?\n)
-      (ewoc--node-create (copy-marker pos) data))))
-
-
 (defun ewoc--create-node (data pretty-printer pos)
   "Call PRETTY-PRINTER with point set at POS in current buffer.
-Remember the start position. Create a wrapper containing that
+Remember the start position.  Create a wrapper containing that
 start position and the element DATA."
   (save-excursion
     ;; Remember the position as a number so that it doesn't move
@@ -276,7 +264,7 @@ start position and the element DATA."
 
 (defun ewoc--delete-node-internal (ewoc node)
   "Delete a data string from EWOC.
-Can not be used on the footer. Returns the wrapper that is deleted.
+Can not be used on the footer.  Return the wrapper that is deleted.
 The start-marker in the wrapper is set to nil, so that it doesn't
 consume any more resources."
   (let ((dll (ewoc--dll ewoc))
@@ -293,87 +281,80 @@ consume any more resources."
     (ewoc--node-delete node)))
 
 
-(defvar dll)                           ;passed by dynamic binding
-
-(defun ewoc--refresh-node (ewoc node)
-  "Redisplay the element represented by NODE.
-Can not be used on the footer. dll *must* be bound to
-\(ewoc--dll ewoc)."
+(defun ewoc--refresh-node (pp node)
+  "Redisplay the element represented by NODE using the pretty-printer PP."
   (let ((inhibit-read-only t))
     (save-excursion
       ;; First, remove the string from the buffer:
       (delete-region (ewoc--node-start-marker node)
                     (1- (marker-position
-                         (ewoc--node-start-marker (ewoc--node-next dll node)))))
+                         (ewoc--node-start-marker (ewoc--node-right node)))))
       ;; Calculate and insert the string.
       (goto-char (ewoc--node-start-marker node))
-      (funcall (ewoc--pretty-printer ewoc)
-              (ewoc--node-data node)))))
+      (funcall pp (ewoc--node-data node)))))
 \f
 ;;; ===========================================================================
 ;;;                  Public members of the Ewoc package
 
 
-(defun ewoc-create (buffer pretty-printer &optional header footer pos)
+(defun ewoc-create (pretty-printer &optional header footer)
   "Create an empty ewoc.
 
-The ewoc will be inserted in BUFFER. BUFFER may be a
-buffer or a buffer name. It is created if it does not exist.
+The ewoc will be inserted in the current buffer at the current position.
 
 PRETTY-PRINTER should be a function that takes one argument, an
 element, and inserts a string representing it in the buffer (at
-point). The string PRETTY-PRINTER inserts may be empty or span
-several linse. A trailing newline will always be inserted
-automatically. The PRETTY-PRINTER should use insert, and not
-insert-before-markers.
-
-Optional third argument HEADER is a string that will always be
-present at the top of the ewoc. HEADER should end with a
-newline.  Optionaly fourth argument FOOTER is similar, and will
-always be inserted at the bottom of the ewoc.
-
-Optional fifth argument POS is a buffer position, specifying
-where the ewoc will be inserted. It defaults to the
-beginning of the buffer."
+point).  The string PRETTY-PRINTER inserts may be empty or span
+several lines.  A trailing newline will always be inserted
+automatically.  The PRETTY-PRINTER should use `insert', and not
+`insert-before-markers'.
+
+Optional second argument HEADER is a string that will always be
+present at the top of the ewoc.  HEADER should end with a
+newline.  Optional third argument FOOTER is similar, and will
+be inserted at the bottom of the ewoc."
   (let ((new-ewoc
-        (ewoc--create (get-buffer-create buffer)
-                           pretty-printer nil nil (ewoc--dll-create))))
+        (ewoc--create (current-buffer)
+                      pretty-printer nil nil (ewoc--dll-create)))
+       (pos (point)))
     (ewoc--set-buffer-bind-dll new-ewoc
       ;; Set default values
       (unless header (setq header ""))
       (unless footer (setq footer ""))
-      (unless pos (setq pos (point-min)))
-      ;; Force header to be above footer.
-      (if (markerp pos) (setq pos (marker-position pos)))
-      (let ((foot (ewoc--create-special-node footer footer pos))
-           (head (ewoc--create-special-node header header pos)))
+      (setf (ewoc--node-start-marker dll) (copy-marker pos))
+      (let ((foot (ewoc--create-node footer 'insert pos))
+           (head (ewoc--create-node header 'insert pos)))
        (ewoc--node-enter-first dll head)
        (ewoc--node-enter-last  dll foot)
-       (setf (ewoc--header new-ewoc) (ewoc--node-nth dll 0))
-       (setf (ewoc--footer new-ewoc) (ewoc--node-nth dll -1))))
+       (setf (ewoc--header new-ewoc) head)
+       (setf (ewoc--footer new-ewoc) foot)))
     ;; Return the ewoc
     new-ewoc))
 
 (defalias 'ewoc-data 'ewoc--node-data)
 
 (defun ewoc-enter-first (ewoc data)
-  "Enter DATA first in EWOC."
+  "Enter DATA first in EWOC.
+Return the new node."
   (ewoc--set-buffer-bind-dll ewoc
     (ewoc-enter-after ewoc (ewoc--node-nth dll 0) data)))
 
 (defun ewoc-enter-last (ewoc data)
-  "Enter DATA last in EWOC."
+  "Enter DATA last in EWOC.
+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."
+  "Enter a new element DATA after NODE in EWOC.
+Return the new node."
   (ewoc--set-buffer-bind-dll ewoc
     (ewoc-enter-before ewoc (ewoc--node-next dll node) data)))
 
 (defun ewoc-enter-before (ewoc node data)
-  "Enter a new element DATA before NODE in EWOC."
+  "Enter a new element DATA before NODE in EWOC.
+Return the new node."
   (ewoc--set-buffer-bind-dll ewoc
     (ewoc--node-enter-before
      node
@@ -383,15 +364,15 @@ beginning of the buffer."
       (ewoc--node-start-marker node)))))
 
 (defun ewoc-next (ewoc node)
-  "Get the next node.
-Returns nil if NODE is nil or the last element."
+  "Return the node in EWOC that follows NODE.
+Return nil if NODE is nil or the last element."
   (when node
     (ewoc--filter-hf-nodes
      ewoc (ewoc--node-next (ewoc--dll ewoc) node))))
 
 (defun ewoc-prev (ewoc node)
-  "Get the previous node.
-Returns nil if NODE is nil or the first element."
+  "Return the node in EWOC that precedes NODE.
+Return nil if NODE is nil or the first element."
   (when node
     (ewoc--filter-hf-nodes
      ewoc
@@ -400,7 +381,7 @@ Returns nil if NODE is nil or the first element."
 
 (defun ewoc-nth (ewoc n)
   "Return the Nth node.
-N counts from zero. Nil is returned if there is less than N elements.
+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.
@@ -416,9 +397,9 @@ MAP-FUNCTION is applied to the first element first.
 If MAP-FUNCTION returns non-nil the element will be refreshed (its
 pretty-printer will be called once again).
 
-Note that the buffer for EWOC will be current buffer when MAP-FUNCTION 
-is called.  MAP-FUNCTION must restore the current buffer to BUFFER before 
-it returns, if it changes it.
+Note that the buffer for EWOC will be the current buffer when
+MAP-FUNCTION is called.  MAP-FUNCTION must restore the current
+buffer before it returns, if it changes it.
 
 If more than two arguments are given, the remaining
 arguments will be passed to MAP-FUNCTION."
@@ -427,15 +408,15 @@ arguments will be passed to MAP-FUNCTION."
        (node (ewoc--node-nth dll 1)))
     (while (not (eq node footer))
       (if (apply map-function (ewoc--node-data node) args)
-         (ewoc--refresh-node ewoc node))
+         (ewoc--refresh-node (ewoc--pretty-printer ewoc) node))
       (setq node (ewoc--node-next dll node)))))
 
 (defun ewoc-filter (ewoc predicate &rest args)
   "Remove all elements in EWOC for which PREDICATE returns nil.
-Note that the buffer for EWOC will be current-buffer when PREDICATE 
-is called. PREDICATE must restore the current buffer before it returns
+Note that the buffer for EWOC will be current-buffer when PREDICATE
+is called.  PREDICATE must restore the current buffer before it returns
 if it changes it.
-The PREDICATE is called with the element as its first argument. If any
+The PREDICATE is called with the element as its first argument.  If any
 ARGS are given they will be passed to the PREDICATE."
   (ewoc--set-buffer-bind-dll-let* ewoc
       ((node (ewoc--node-nth dll 1))
@@ -447,14 +428,15 @@ ARGS are given they will be passed to the PREDICATE."
        (ewoc--delete-node-internal ewoc node))
       (setq node next))))
 
-(defun ewoc-locate (ewoc pos &optional guess)
+(defun ewoc-locate (ewoc &optional pos guess)
   "Return the node that POS (a buffer position) is within.
-POS may be a marker or an integer.
-GUESS should be a node that it is likely that POS is near.
+POS may be a marker or an integer.  It defaults to point.
+GUESS should be a node that it is likely to be near POS.
 
 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)))
 
@@ -499,7 +481,7 @@ If the EWOC is empty, nil is returned."
        ;; best-guess is now a "best guess".
        ;; Find the correct node. First determine in which direction
        ;; it lies, and then move in that direction until it is found.
-    
+
        (cond
         ;; Is pos after the guess?
         ((>= pos
@@ -517,19 +499,22 @@ If the EWOC is empty, nil is returned."
          best-guess)))))))
 
 (defun ewoc-invalidate (ewoc &rest nodes)
-  "Refresh some elements.
-The pretty-printer that for EWOC will be called for all NODES."
+  "Call EWOC's pretty-printer for each element in NODES.
+Delete current text first, thus effecting a \"refresh\"."
   (ewoc--set-buffer-bind-dll ewoc
     (dolist (node nodes)
-      (ewoc--refresh-node ewoc node))))
+      (ewoc--refresh-node (ewoc--pretty-printer ewoc) node))))
 
-(defun ewoc-goto-prev (ewoc pos arg)
-  "Move point to the ARGth previous element.
+(defun ewoc-goto-prev (ewoc arg)
+  "Move point to the ARGth previous element in EWOC.
 Don't move if we are at the first element, or if EWOC is empty.
-Returns the node we moved to."
+Return the node we moved to."
   (ewoc--set-buffer-bind-dll-let* ewoc
-      ((node (ewoc-locate ewoc pos (ewoc--last-node ewoc))))
+      ((node (ewoc-locate ewoc (point))))
     (when node
+      ;; If we were past the last element, first jump to it.
+      (when (>= (point) (ewoc--node-start-marker (ewoc--node-right node)))
+       (setq arg (1- arg)))
       (while (and node (> arg 0))
        (setq arg (1- arg))
        (setq node (ewoc--node-prev dll node)))
@@ -538,22 +523,21 @@ Returns the node we moved to."
        (setq node (ewoc--node-nth dll 1)))
       (ewoc-goto-node ewoc node))))
 
-(defun ewoc-goto-next (ewoc pos arg)
-  "Move point to the ARGth next element.
-Don't move if we are at the last element.
-Returns the node."
+(defun ewoc-goto-next (ewoc arg)
+  "Move point to the ARGth next element in EWOC.
+Return the node (or nil if we just passed the last node)."
   (ewoc--set-buffer-bind-dll-let* ewoc
-      ((node (ewoc-locate ewoc pos (ewoc--last-node ewoc))))
+      ((node (ewoc-locate ewoc (point))))
     (while (and node (> arg 0))
       (setq arg (1- arg))
       (setq node (ewoc--node-next dll node)))
     ;; Never step below the first element.
-    (unless (ewoc--filter-hf-nodes ewoc node)
-      (setq node (ewoc--node-nth dll -2)))
+    ;; (unless (ewoc--filter-hf-nodes ewoc node)
+    ;;   (setq node (ewoc--node-nth dll -2)))
     (ewoc-goto-node ewoc node)))
 
 (defun ewoc-goto-node (ewoc node)
-  "Move point to NODE."
+  "Move point to NODE in EWOC."
   (ewoc--set-buffer-bind-dll ewoc
     (goto-char (ewoc--node-start-marker node))
     (if goal-column (move-to-column goal-column))
@@ -566,8 +550,7 @@ will be called for all elements in EWOC.
 Note that `ewoc-invalidate' is more efficient if only a small
 number of elements needs to be refreshed."
   (ewoc--set-buffer-bind-dll-let* ewoc
-      ((header (ewoc--header ewoc))
-       (footer (ewoc--footer ewoc)))
+      ((footer (ewoc--footer ewoc)))
     (let ((inhibit-read-only t))
       (delete-region (ewoc--node-start-marker (ewoc--node-nth dll 1))
                     (ewoc--node-start-marker footer))
@@ -584,13 +567,13 @@ number of elements needs to be refreshed."
 (defun ewoc-collect (ewoc predicate &rest args)
   "Select elements from EWOC using PREDICATE.
 Return a list of all selected data elements.
-PREDICATE is a function that takes a data element as its first argument.
-The elements on the returned list will appear in the same order as in
-the buffer.  You should not rely on in which order PREDICATE is
-called.
-Note that the buffer the EWOC is displayed in is current-buffer
-when PREDICATE is called.  If PREDICATE must restore current-buffer if
-it changes it.
+PREDICATE is a function that takes a data element as its first
+argument.  The elements on the returned list will appear in the
+same order as in the buffer.  You should not rely on the order of
+calls to PREDICATE.
+Note that the buffer the EWOC is displayed in is the current
+buffer when PREDICATE is called.  PREDICATE must restore it if it
+changes it.
 If more than two arguments are given the
 remaining arguments will be passed to PREDICATE."
   (ewoc--set-buffer-bind-dll-let* ewoc
@@ -601,14 +584,26 @@ remaining arguments will be passed to PREDICATE."
       (if (apply predicate (ewoc--node-data node) args)
          (push (ewoc--node-data node) result))
       (setq node (ewoc--node-prev dll node)))
-    result))
+    (nreverse result)))
 
 (defun ewoc-buffer (ewoc)
   "Return the buffer that is associated with EWOC.
-Returns nil if the buffer has been deleted."
+Return nil if the buffer has been deleted."
   (let ((buf (ewoc--buffer ewoc)))
     (when (buffer-name buf) buf)))
 
+(defun ewoc-get-hf (ewoc)
+  "Return a cons cell containing the (HEADER . FOOTER) of EWOC."
+  (cons (ewoc--node-data (ewoc--header ewoc))
+       (ewoc--node-data (ewoc--footer ewoc))))
+
+(defun ewoc-set-hf (ewoc header footer)
+  "Set the HEADER and FOOTER of EWOC."
+  (setf (ewoc--node-data (ewoc--header ewoc)) header)
+  (setf (ewoc--node-data (ewoc--footer ewoc)) footer)
+  (ewoc--refresh-node 'insert (ewoc--header ewoc))
+  (ewoc--refresh-node 'insert (ewoc--footer ewoc)))
+
 \f
 (provide 'ewoc)
 
@@ -617,4 +612,5 @@ Returns nil if the buffer has been deleted."
 ;;; eval: (put 'ewoc--set-buffer-bind-dll-let* 'lisp-indent-hook 2)
 ;;; End:
 
+;;; arch-tag: d78915b9-9a07-44bf-aac6-04a1fc1bd6d4
 ;;; ewoc.el ends here