]> code.delx.au - gnu-emacs/blobdiff - lisp/outline.el
*** empty log message ***
[gnu-emacs] / lisp / outline.el
index c9863083b926b9e1631168c40b71c435af14df47..92e521afc9f3bc6732ea31406a9663d963b30646 100644 (file)
@@ -10,7 +10,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -471,19 +471,22 @@ If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
                              (if up "Parent" "Demoted") head)
                      head nil nil t)))))
 
-(defun outline-promote (&optional children)
+(defun outline-promote (&optional which)
   "Promote headings higher up the tree.
-If prefix argument CHILDREN is given, promote also all the children.
-If the region is active in `transient-mark-mode', promote all headings
-in the region."
+If transient-mark-mode is on, and mark is active, promote headings in
+the region (from a Lisp program, pass `region' for WHICH).  Otherwise:
+without prefix argument, promote current heading and all headings in the
+subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
+argument, promote just the current heading (from a Lisp program, pass
+nil for WHICH, or do not pass any argument)."
   (interactive
    (list (if (and transient-mark-mode mark-active) 'region
           (outline-back-to-heading)
           (if current-prefix-arg nil 'subtree))))
   (cond
-   ((eq children 'region)
+   ((eq which 'region)
     (outline-map-region 'outline-promote (region-beginning) (region-end)))
-   (children
+   (which
     (outline-map-region 'outline-promote
                        (point)
                        (save-excursion (outline-get-next-sibling) (point))))
@@ -507,19 +510,22 @@ in the region."
 
       (replace-match up-head nil t)))))
 
-(defun outline-demote (&optional children)
+(defun outline-demote (&optional which)
   "Demote headings lower down the tree.
-If prefix argument CHILDREN is given, demote also all the children.
-If the region is active in `transient-mark-mode', demote all headings
-in the region."
+If transient-mark-mode is on, and mark is active, demote headings in
+the region (from a Lisp program, pass `region' for WHICH).  Otherwise:
+without prefix argument, demote current heading and all headings in the
+subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
+argument, demote just the current heading (from a Lisp program, pass
+nil for WHICH, or do not pass any argument)."
   (interactive
    (list (if (and transient-mark-mode mark-active) 'region
           (outline-back-to-heading)
           (if current-prefix-arg nil 'subtree))))
   (cond
-   ((eq children 'region)
+   ((eq which 'region)
     (outline-map-region 'outline-demote (region-beginning) (region-end)))
-   (children
+   (which
     (outline-map-region 'outline-demote
                        (point)
                        (save-excursion (outline-get-next-sibling) (point))))
@@ -706,7 +712,10 @@ If nil, `show-entry' is called to reveal the invisible text.")
 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
   (remove-overlays from to 'invisible 'outline)
   (when flag
-    (let ((o (make-overlay from to)))
+    ;; We use `front-advance' here because the invisible text begins at the
+    ;; very end of the heading, before the newline, so text inserted at FROM
+    ;; belongs to the heading rather than to the entry.
+    (let ((o (make-overlay from to nil 'front-advance)))
       (overlay-put o 'invisible 'outline)
       (overlay-put o 'isearch-open-invisible
                   (or outline-isearch-open-invisible-function
@@ -850,19 +859,25 @@ Show the heading too, if it is currently invisible."
                 (t 1))))
   (if (< levels 1)
       (error "Must keep at least one level of headers"))
-  (let (outline-view-change-hook)
-    (save-excursion
-      (goto-char (point-min))
-      ;; Skip the prelude, if any.
-      (unless (outline-on-heading-p t) (outline-next-heading))
+  (save-excursion
+    (let* (outline-view-change-hook
+           (beg (progn
+                  (goto-char (point-min))
+                  ;; Skip the prelude, if any.
+                  (unless (outline-on-heading-p t) (outline-next-heading))
+                  (point)))
+           (end (progn
+                  (goto-char (point-max))
+                  ;; Keep empty last line, if available.
+                  (if (bolp) (1- (point)) (point)))))
       ;; First hide everything.
-      (outline-flag-region (point) (point-max) t)
+      (outline-flag-region beg end t)
       ;; Then unhide the top level headers.
       (outline-map-region
        (lambda ()
         (if (<= (funcall outline-level) levels)
             (outline-show-heading)))
-       (point) (point-max))))
+       beg end)))
   (run-hooks 'outline-view-change-hook))
 
 (defun hide-other ()
@@ -988,7 +1003,8 @@ Stop at the first and last subheadings of a superior heading."
          (error "No following same-level heading"))))))
 
 (defun outline-get-next-sibling ()
-  "Move to next heading of the same level, and return point or nil if none."
+  "Move to next heading of the same level, and return point.
+If there is no such heading, return nil."
   (let ((level (funcall outline-level)))
     (outline-next-visible-heading 1)
     (while (and (not (eobp)) (> (funcall outline-level) level))
@@ -1014,15 +1030,18 @@ Stop at the first and last subheadings of a superior heading."
          (error "No previous same-level heading"))))))
 
 (defun outline-get-last-sibling ()
-  "Move to previous heading of the same level, and return point or nil if none."
-  (let ((level (funcall outline-level)))
+  "Move to previous heading of the same level, and return point.
+If there is no such heading, return nil."
+  (let ((opoint (point))
+       (level (funcall outline-level)))
     (outline-previous-visible-heading 1)
-    (while (and (> (funcall outline-level) level)
-               (not (bobp)))
-      (outline-previous-visible-heading 1))
-    (if (< (funcall outline-level) level)
-       nil
-        (point))))
+    (when (and (/= (point) opoint) (outline-on-heading-p))
+      (while (and (> (funcall outline-level) level)
+                 (not (bobp)))
+       (outline-previous-visible-heading 1))
+      (if (< (funcall outline-level) level)
+         nil
+        (point)))))
 \f
 (defun outline-headers-as-kill (beg end)
   "Save the visible outline headers in region at the start of the kill ring.