Subject: Org-like cycling in outline-minor-mode

From: Juri Linkov <[email-masked]>
Date: Wed, 09 Dec 2020 21:15:14 +0200

As suggested in bug#41198, here is a patch that adds
two new options (disabled by default):

- outline-minor-mode-font-lock that enables outline font-lock in
  outline-minor-mode

- outline-minor-mode-cycle that enables heading cycle in
  outline-minor-mode

And at the end of the patch are two examples that enable this feature
in the files etc/compilation.txt and etc/grep.txt.  So visiting these files
will highlight their outline headers, and will allow typing 'TAB' to cycle
between `hide all', `headings only' and `show all' when point is on the heading line.
Typing 'S-TAB' on the heading line cycles the whole buffer.  Typing these keys
anywhere outside heading lines uses their default bindings, so TAB will go
to the next link, and S-TAB will navigate back to the previous link.

diff --git a/lisp/outline.el b/lisp/outline.el
index 85f9de4e1b..b6973e60dd 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -175,10 +175,8 @@ outline-minor-mode-menu-bar-map
 				   outline-mode-menu-bar-map))))))
     map))
 
-(defvar outline-mode-map
+(defvar outline-mode-cycle-map
   (let ((map (make-sparse-keymap)))
-    (define-key map "\C-c" outline-mode-prefix-map)
-    (define-key map [menu-bar] outline-mode-menu-bar-map)
     ;; Only takes effect if point is on a heading.
     (define-key map (kbd "TAB")
       `(menu-item "" outline-cycle
@@ -187,11 +185,22 @@ outline-mode-map
     (define-key map (kbd "<backtab>") #'outline-cycle-buffer)
     map))
 
+(defvar outline-mode-map
+  (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map outline-mode-cycle-map)
+    (define-key map "\C-c" outline-mode-prefix-map)
+    (define-key map [menu-bar] outline-mode-menu-bar-map)
+    map))
+
 (defvar outline-font-lock-keywords
   '(
     ;; Highlight headings according to the level.
     (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
-		  0 '(outline-font-lock-face) nil t)))
+		  0 '(if outline-minor-mode-cycle
+			 (list 'face (outline-font-lock-face)
+			       'local-map outline-mode-cycle-map)
+		       (outline-font-lock-face))
+		  nil t)))
   "Additional expressions to highlight in Outline mode.")
 
 (defface outline-1
@@ -305,6 +314,19 @@ outline-minor-mode-prefix
          (define-key outline-minor-mode-map val outline-mode-prefix-map)
          (set-default sym val)))
 
+(defvar outline-minor-mode-font-lock nil
+  "Enable outline font-lock in `outline-minor-mode'.
+Non-nil value works well only when outline font-lock keywords
+don't conflict with the major mode's font-lock keywords.")
+;;;###autoload(put 'outline-minor-mode-font-lock 'safe-local-variable 'booleanp)
+(defvar outline-minor-mode-cycle nil
+  "Enable heading cycle in `outline-minor-mode'.
+When point is on a heading line, then typing 'TAB' cycles between `hide all',
+`headings only' and `show all' (`outline-cycle').  Typing 'S-TAB' on
+a heading line cycles the whole buffer (`outline-cycle-buffer').
+Typing these keys anywhere outside heading lines uses their default bindings.")
+;;;###autoload(put 'outline-minor-mode-cycle 'safe-local-variable 'booleanp)
+
 ;;;###autoload
 (define-minor-mode outline-minor-mode
   "Toggle Outline minor mode.
@@ -314,6 +336,9 @@ outline-minor-mode
 		    (cons outline-minor-mode-prefix outline-mode-prefix-map))
   (if outline-minor-mode
       (progn
+        (when outline-minor-mode-font-lock
+          (font-lock-add-keywords nil outline-font-lock-keywords)
+          (font-lock-flush))
 	;; Turn off this mode if we change major modes.
 	(add-hook 'change-major-mode-hook
 		  (lambda () (outline-minor-mode -1))
@@ -321,6 +346,9 @@ outline-minor-mode
         (setq-local line-move-ignore-invisible t)
 	;; Cause use of ellipses for invisible text.
 	(add-to-invisibility-spec '(outline . t)))
+    (when outline-minor-mode-font-lock
+      (font-lock-remove-keywords nil outline-font-lock-keywords)
+      (font-lock-flush))
     (setq line-move-ignore-invisible nil)
     ;; Cause use of ellipses for invisible text.
     (remove-from-invisibility-spec '(outline . t))
diff --git a/etc/compilation.txt b/etc/compilation.txt
index 7e406389d4..5f28ca1d13 100644
--- a/etc/compilation.txt
+++ b/etc/compilation.txt
@@ -692,3 +692,11 @@ COPYING PERMISSIONS:
 
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+
+;;; Local Variables:
+;;; outline-minor-mode-font-lock: t
+;;; outline-minor-mode-cycle: t
+;;; outline-regexp: "\\*\\_>"
+;;; eval: (outline-minor-mode 1)
+;;; End:
diff --git a/etc/grep.txt b/etc/grep.txt
index 19a3b4b47b..0f03609762 100644
--- a/etc/grep.txt
+++ b/etc/grep.txt
@@ -118,4 +123,7 @@ COPYING PERMISSIONS:
 ;;; Local Variables:
 ;;; eval: (let ((inhibit-read-only t) (compilation-filter-start (point-min))) (save-excursion (goto-char (point-max)) (grep-filter) (set-buffer-modified-p nil)))
 ;;; buffer-read-only: t
+;;; outline-minor-mode-font-lock: t
+;;; outline-minor-mode-cycle: t
+;;; eval: (outline-minor-mode 1)
 ;;; End: