diff options
author | Yuan Fu <casouri@gmail.com> | 2020-10-13 05:14:21 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2020-10-13 05:14:21 +0200 |
commit | 1b45079ffa2d0b8f66f77cdcf1af2d3d08a5515b (patch) | |
tree | 80defa6ea620b738d19e9e4f4e6c8a70d22c0963 | |
parent | b31e48d4efb030b59a9058796c2da53357c379a3 (diff) | |
download | emacs-1b45079ffa2d0b8f66f77cdcf1af2d3d08a5515b.tar.gz emacs-1b45079ffa2d0b8f66f77cdcf1af2d3d08a5515b.tar.bz2 emacs-1b45079ffa2d0b8f66f77cdcf1af2d3d08a5515b.zip |
Add cycling commands to outline
* lisp/outline.el (outline--cycle-state, outline-has-subheading-p)
(outline-cycle, outline-cycle-buffer): New functions.
(outline-mode-map): Add key bindings for the two new commands.
(outline--cycle-buffer-state): New variable.
* doc/emacs/text.text (Outline Visibility): Add 'outline-cycle' and
'outline-cycle-buffer'.
* etc/NEWS (Outline): Record the change (bug#41130).
-rw-r--r-- | doc/emacs/text.texi | 10 | ||||
-rw-r--r-- | etc/NEWS | 9 | ||||
-rw-r--r-- | lisp/outline.el | 83 |
3 files changed, 102 insertions, 0 deletions
diff --git a/doc/emacs/text.texi b/doc/emacs/text.texi index 281e24421c2..9c2822ce15b 100644 --- a/doc/emacs/text.texi +++ b/doc/emacs/text.texi @@ -1207,6 +1207,16 @@ everything except the top @var{n} levels of heading lines. Note that it completely reveals all the @var{n} top levels and the body lines before the first heading. +@findex outline-cycle +@findex outline-cycle-buffer + Outline also provides two convenience commands to cycle the +visibility of each section and the whole buffer. Typing @kbd{TAB} on +a heading invokes @code{outline-cycle}, which cycles the current +section between "hide all", "subheadings", and "show all" state. +Typing @kbd{S-TAB} invokes @code{outline-cycle-buffer}, which cycles +the whole buffer between "only top-level headings", "all headings and +subheadings", and "show all" states. + @anchor{Outline Search} @findex reveal-mode @vindex search-invisible @@ -233,6 +233,15 @@ preserving markers, properties and overlays. The new variable number of seconds that 'revert-buffer-with-fine-grain' should spend trying to be non-destructive. +** Outline + ++++ +*** New commands to cycle heading visibility. +Typing 'TAB' on a heading cycles the current section between "hide +all", "subheadings", and "show all" state. Typing 'S-TAB' anywhere in +the buffer cycles the whole buffer between "only top-level headings", +"all headings and subheadings", and "show all" states. + * Changes in Specialized Modes and Packages in Emacs 28.1 diff --git a/lisp/outline.el b/lisp/outline.el index 6158ed594e9..95670e04936 100644 --- a/lisp/outline.el +++ b/lisp/outline.el @@ -179,6 +179,12 @@ in the file it applies to.") (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 the 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 @@ -1125,6 +1131,83 @@ convenient way to make a table of contents of the buffer." (insert "\n\n")))))) (kill-new (buffer-string))))))) +(defun outline--cycle-state () + "Return the cycle state of current heading. +Return either 'hide-all, 'headings-only, or 'show-all." + (save-excursion + (let (start end ov-list heading-end) + (outline-back-to-heading) + (setq start (point)) + (outline-end-of-heading) + (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) + ;; (eq (length ov-list) 1) wouldn’t work: what if there is + ;; one folded subheading? + ((and (eq (overlay-end (car ov-list)) end) + (eq (overlay-start (car ov-list)) heading-end)) + 'hide-all) + (t 'headings-only))))) + +(defun outline-has-subheading-p () + "Return t if this heading has subheadings, nil otherwise." + (save-excursion + (outline-back-to-heading) + (< (save-excursion (outline-next-heading) (point)) + (save-excursion (outline-end-of-subtree) (point))))) + +(defun outline-cycle () + "Cycle between `hide all', `headings only' and `show all'. + +`Hide all' means hide all subheadings and their bodies. +`Headings only' means show sub headings but not their bodies. +`Show all' means show all subheadings and their bodies." + (interactive) + (pcase (outline--cycle-state) + ('hide-all + (if (outline-has-subheading-p) + (progn (outline-show-children) + (message "Only headings")) + (outline-show-subtree) + (message "Show all"))) + ('headings-only + (outline-show-subtree) + (message "Show all")) + ('show-all + (outline-hide-subtree) + (message "Hide all")))) + +(defvar-local outline--cycle-buffer-state 'show-all + "Internal variable used for tracking buffer cycle state.") + +(defun outline-cycle-buffer () + "Cycle the whole buffer like in `outline-cycle'." + (interactive) + (pcase outline--cycle-buffer-state + ('show-all + (save-excursion + (let ((start-point (point))) + (while (not (eq (point) start-point)) + (outline-up-heading 1)) + (outline-hide-sublevels + (progn (outline-back-to-heading) + (funcall 'outline-level))))) + (setq outline--cycle-buffer-state 'top-level) + (message "Top level headings")) + ('top-level + (outline-show-all) + (outline-hide-region-body (point-min) (point-max)) + (setq outline--cycle-buffer-state 'all-heading) + (message "All headings")) + ('all-heading + (outline-show-all) + (setq outline--cycle-buffer-state 'show-all) + (message "Show all")))) + (provide 'outline) (provide 'noutline) |