Subject: bug#19102: 24.4; outline-move-subtree-up/down error at last and second-last subtree

From: Stephen Berman <[email-masked]>
Date: Wed, 19 Nov 2014 23:07:03 +0100

On Wed, 19 Nov 2014 22:32:06 +0200 Eli Zaretskii <[email-masked]> wrote:

>> From: Stephen Berman <[email-masked]>
>> Cc: [email-masked],  [email-masked]
>> Date: Wed, 19 Nov 2014 21:14:42 +0100
>> 
>> > Wouldn't using eolp instead of the comparison solve that problem more
>> > easily?
>> 
>> Well, that eliminates the wrong-type-argument error in the current code,
>> but it instead signals "End of buffer"
>
> From forward-char?  If so, you could avoid the call if eobp.  Or wrap
> the call in condition-case and ignore errors.

Those solutions work only if there's an empty line after the last
subtree.  If there isn't, I can't see any way other than my patch.

>> Do you see any other problems with the patch or more room for
>> improvement?
>
> I was worried by the complexity of maybe-forward-char, but maybe now
> it is much simpler.

I simplified it just a bit more, but that's all I can think of.  I also
had overlooked a side-effect of the error signaled when trying to move
the last subtree down: it leaves a dangling newline; this is avoided
using with-demoted-errors.  The revised patch is appended.

Steve Berman

diff --git a/lisp/outline.el b/lisp/outline.el
index c7cad31..a34ff12 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -649,32 +649,46 @@ the match data is set appropriately."
 		   'outline-get-last-sibling))
 	(ins-point (make-marker))
 	(cnt (abs arg))
+	;; Make sure we can move forward to find the end of the
+	;; subtree and the insertion point.
+	(maybe-forward-char (lambda ()
+			      (and (eobp) (not (bolp)) (newline))
+			      (and (eolp) (not (bolp)) (forward-char 1))))
+	(empty-last-line (save-excursion
+			   (goto-char (point-max))
+			   (and (bolp) (eolp))))
 	beg end folded)
-    ;; Select the tree
+    ;; Select the tree.
     (outline-back-to-heading)
     (setq beg (point))
     (save-match-data
       (save-excursion (outline-end-of-heading)
 		      (setq folded (outline-invisible-p)))
       (outline-end-of-subtree))
-    (if (= (char-after) ?\n) (forward-char 1))
+    (funcall maybe-forward-char)
     (setq end (point))
-    ;; Find insertion point, with error handling
+    ;; Find insertion point, with error handling.
     (goto-char beg)
     (while (> cnt 0)
       (or (funcall movfunc)
 	  (progn (goto-char beg)
-		 (error "Cannot move past superior level")))
+		 (with-demoted-errors "%s"
+		   (message "Cannot move past superior level"))))
       (setq cnt (1- cnt)))
     (if (> arg 0)
-	;; Moving forward - still need to move over subtree
+	;; Moving forward - still need to move over subtree.
 	(progn (outline-end-of-subtree)
-	       (if (= (char-after) ?\n) (forward-char 1))))
+	       (funcall maybe-forward-char)))
     (move-marker ins-point (point))
     (insert (delete-and-extract-region beg end))
     (goto-char ins-point)
     (if folded (hide-subtree))
-    (move-marker ins-point nil)))
+    (move-marker ins-point nil)
+    ;; If we added a newline to move forward, delete it.
+    (save-excursion
+      (goto-char (point-max))
+      (when (and (bolp) (eolp) (not empty-last-line))
+	(delete-char -1)))))
 
 (defun outline-end-of-heading ()
   (if (re-search-forward outline-heading-end-regexp nil 'move)