bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#45147: Org-like cycling in outline-minor-mode


From: Juri Linkov
Subject: bug#45147: Org-like cycling in outline-minor-mode
Date: Mon, 14 Dec 2020 22:35:44 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu)

severity 45147 wishlist
tags 45147 + patch
quit

>> As suggested in bug#41198, here is a patch that adds
>> two new options (disabled by default):
>
> Here is a better patch.  It makes possible to enable
> outline-minor-mode in any buffer, e.g. in prog mode,
> when outline-minor-mode-cycle is set to non-nil.
> Then typing S-TAB will collapse all headings, and
> TAB will expand the current function body, like this:

This patch also allows using only outline-minor-mode-cycle
without highlighting heading with outline-minor-mode-font-lock
that is useful in prog-mode buffers:

diff --git a/lisp/outline.el b/lisp/outline.el
index 85f9de4e1b..db10e2667f 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -175,23 +175,43 @@ outline-minor-mode-menu-bar-map
                                   outline-mode-menu-bar-map))))))
     map))
 
+(defvar outline-mode-cycle-map
+  (let ((map (make-sparse-keymap)))
+    (let ((binding `(menu-item
+                     "" outline-cycle
+                     ;; Only takes effect if point is on a heading.
+                     :filter ,(lambda (cmd)
+                                (when (outline-on-heading-p) cmd)))))
+      (define-key map (kbd "TAB") binding)
+      (define-key map [tab] binding)
+      (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)
-    ;; Only takes effect if point is on a heading.
-    (define-key map (kbd "TAB")
-      `(menu-item "" outline-cycle
-                  :filter ,(lambda (cmd)
-                             (when (outline-on-heading-p) cmd))))
-    (define-key map (kbd "<backtab>") #'outline-cycle-buffer)
     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
+                        (if outline-minor-mode-font-lock
+                             (list 'face (outline-font-lock-face)
+                                  'local-map (make-composed-keymap
+                                               outline-mode-cycle-map
+                                               (current-local-map)))
+                           (list 'face nil 'local-map (make-composed-keymap
+                                                       outline-mode-cycle-map
+                                                       (current-local-map))))
+                      (outline-font-lock-face))
+                 nil
+                  (if (or outline-minor-mode-font-lock 
outline-minor-mode-cycle)
+                      'append
+                    t))))
   "Additional expressions to highlight in Outline mode.")
 
 (defface outline-1
@@ -305,6 +325,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 +347,9 @@ outline-minor-mode
                    (cons outline-minor-mode-prefix outline-mode-prefix-map))
   (if outline-minor-mode
       (progn
+        (when (or outline-minor-mode-font-lock outline-minor-mode-cycle)
+          (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 +357,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 (or outline-minor-mode-font-lock outline-minor-mode-cycle)
+      (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))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]