Subject: bug#41198: 28.0.50; heading cycling command for outline

From: "Paul W. Rankin" <[email-masked]>
Date: Sun, 15 Nov 2020 15:50:31 +1000

Hello,

Sorry to revisit this but there are a few edge cases with the new 
outline-cycle and outline-cycle-buffer commands...

1. In command outline-hide-sublevels#L901 we see:

       ;; Finally unhide any trailing newline.
       (goto-char (point-max))
       (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
           (outline-flag-region (1- (point)) (point) nil))))

When calling this function the overlay created to hide the buffer's 
final subheading ends at the end-of-subtree - 1. This means that the 
following code in outline--cycle-state#L1130 fails with an off-by-1:

             ((and (eq (overlay-end (car ov-list)) end)
                   (eq (overlay-start (car ov-list)) heading-end))

The result of this is that when calling outline-cycle-buffer to set the 
outline in a top-level state, if the user navigates to the last 
top-level heading and presses TAB for outline-cycle, the expectation is 
to show that heading's subheadings, but the result is show all.

To fix, if leaving outline-hide-sublevels alone, a workaround would be:

diff --git a/lisp/outline.el b/lisp/outline.el
index 47e6528859..054d2cb62b 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -1127,7 +1127,7 @@ outline--cycle-state
        (cond ((eq ov-list nil) 'show-all)
              ;; (eq (length ov-list) 1) wouldn’t work: what if there is
              ;; one folded subheading?
-            ((and (eq (overlay-end (car ov-list)) end)
+            ((and (<= 0 (- end (overlay-end (car ov-list))) 1)
                    (eq (overlay-start (car ov-list)) heading-end))
               'hide-all)
              (t 'headings-only)))))

2. This may sound strange, but overlays with an invisible property of 
'outline are not guaranteed to be overlays for collapsed outline 
headings. This is true if a lisp program has used outline-flag-region 
for something other than an outline heading. I had done this for 
collapsing a major mode's notes markup:

     [[ here's a note ]] -> [[...]]

This is simply solved by testing each overlay-start with 
outline-on-heading-p:

diff --git a/lisp/outline.el b/lisp/outline.el
index 054d2cb62b..a05cf87d4e 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -1121,10 +1121,15 @@ outline--cycle-state
        (setq heading-end (point))
        (outline-end-of-subtree)
        (setq end (point))
-      (setq ov-list (cl-remove-if-not
-                     (lambda (o) (eq (overlay-get o 'invisible) 
'outline))
-                     (overlays-in start end)))
-      (cond ((eq ov-list nil) 'show-all)
+      (setq ov-list
+            (seq-filter
+             (lambda (o)
+               (and (eq (overlay-get o 'invisible) 'outline)
+                    (save-excursion
+                      (goto-char (overlay-start o))
+                      (outline-on-heading-p t)))))
+            (overlays-in start end)))
+    (cond ((eq ov-list nil) 'show-all)
              ;; (eq (length ov-list) 1) wouldn’t work: what if there is
              ;; one folded subheading?
              ((and (<= 0 (- end (overlay-end (car ov-list))) 1)

n.b. This will fail if the user/program has changed 
outline-heading-end-regexp to have multiple newlines, because 
outline-on-heading-p only checks if it's looking at outline-regexp from 
the beginning of that line.

3. When a buffer contains outline headings of only < 1 (e.g. all 
headings are level 3) calling outline-cycle-buffer to show only 
top-level headings will results in an unexpected buffer state of:

     ...

This is fixed with a simple test of whether the buffer has top-level 
headings before allowing cycling to top-level.

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

The single best thing you can do for the world is delete your social 
media accounts.