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

From: Stephen Berman <[email-masked]>
Date: Wed, 26 Nov 2014 14:38:01 +0100

On Tue, 25 Nov 2014 21:43:02 -0500 Stefan Monnier <[email-masked]> wrote:

>> +	(maybe-forward-char (lambda ()
>> +			      (and (eobp) (not (bolp)) (newline))
>> +			      (and (eolp) (not (bolp)) (forward-char 1))))
> 
> If we add a newline, we know don't want to do a `forward-char'.  So:

Oops, you commented on the last version of my patch, which, however, I
had already considered superseded by the last version of the command
subsequently posted by Paul Rankin, which in effect already addressed
your concerns.  Rather than repost the diff for the latter, I'm
appending the two corrected versions of outline-move-subtree-down to
facilitate comparison and deciding which to use.  The only thing I added
to both is a condition for triggering the user-error, since the message
doesn't seem appropriate when you try to move a subtree down at eob or
up at bob (if you don't think it's worth avoiding the message there,
I'll remove the condition).

Here's the version of my patch with your improvements:

(defun outline-move-subtree-down (&optional arg)
  "Move the current subtree down past ARG headlines of the same level."
  (interactive "p")
  (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
		   '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 ()
			      (if (eq (char-after) ?\n) (forward-char 1)
				(if (and (eobp) (not (bolp))) (insert "\n")))))
	beg end folded)
    ;; 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))
    (funcall maybe-forward-char)
    (setq end (point))
    ;; Find insertion point, with error handling.
    (goto-char beg)
    (while (> cnt 0)
      (or (funcall movfunc)
	  (unless (or (bobp) (eobp))
	    (goto-char beg)
	    (user-error "Cannot move past superior level")))
      (setq cnt (1- cnt)))
    (if (> arg 0)
	;; Moving forward - still need to move over subtree.
	(progn (outline-end-of-subtree)
	       (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)))

And here's Paul's version (tweaked to make it closer to the outline.el
style):

(defun outline-move-subtree-down (&optional arg)
  "Move the current subtree down past ARG headlines of the same level."
  (interactive "p")
  (outline-back-to-heading)
  (let* ((movfunc (if (> arg 0) 'outline-get-next-sibling
		    'outline-get-last-sibling))
	 ;; Find the end of the subtree to be moved as well as the point to
	 ;; move it to, adding a newline if necessary, to ensure these points
	 ;; are at bol on the line below the subtree.
         (end-point-func (lambda ()
			   (outline-end-of-subtree)
			   (if (and (eobp) (eolp) (not (bolp)))
			       (insert-char ?\n))
			   (unless (eobp)
			     (forward-char 1))
			   (point)))
         (beg (point))
         (folded (save-match-data
		   (outline-end-of-heading)
		   (outline-invisible-p)))
         (end (save-match-data
		(funcall end-point-func)))
         (ins-point (make-marker))
         (cnt (abs arg)))
    ;; Find insertion point, with error handling.
    (goto-char beg)
    (while (> cnt 0)
      (or (funcall movfunc)
	  (unless (or (bobp) (eobp))
	    (goto-char beg)
	    (user-error "Cannot move past superior level")))
      (setq cnt (1- cnt)))
    (if (> arg 0)
	;; Moving forward - still need to move over subtree.
	(funcall end-point-func))
    (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)))

Which should we use?

Steve Berman