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

From: Paul Rankin <[email-masked]>
Date: Thu, 20 Nov 2014 17:22:50 +1000

Stephen Berman <[email-masked]> writes:

> 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

I needed to rewrite this for functionality with my own package, so I
rewrote it very arrogantly and this is what I came up with...

(defun fountain-outline-shift-down (&optional arg)
  (interactive "p")
  (outline-back-to-heading)
  (let* ((move-func
          (if (< 0 arg)
              'outline-get-next-sibling
            'outline-get-last-sibling))
         (end-point-func
          '(lambda ()
             (outline-end-of-subtree)
             (if (and (eolp)
                      (not (bolp))
                      (eobp))
                 (open-line 1))
             (unless (eobp)
               (forward-char 1))
             (point)))
         (beg (point))
         (end
          (save-excursion
            (funcall end-point-func)))
         (folded
          (save-excursion
            (outline-end-of-heading)
            (outline-invisible-p)))
         (insert-point (make-marker))
         (i (abs arg)))
    (goto-char beg)
    (while (< 0 i)
      (or (funcall move-func)
          (progn (goto-char beg)
                 (message "Cannot move past superior level")))
      (setq i (1- i)))
    (if (< 0 arg)                       ; what does this bit do??
        (funcall end-point-func))
    (move-marker insert-point (point))
    (insert (delete-and-extract-region beg end))
    (goto-char insert-point)
    (if folded
        (hide-subtree))
    (set-marker insert-point nil)))

The only thing I'm not sure about is the line marked above. This is not
intended for Emacs, just wanna see if I'm on the right track :)

-- 
Paul W. Rankin
http://www.paulwrankin.com