[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 1b45079 1/3: Add cycling commands to outline
From: |
Lars Ingebrigtsen |
Subject: |
master 1b45079 1/3: Add cycling commands to outline |
Date: |
Tue, 13 Oct 2020 00:52:59 -0400 (EDT) |
branch: master
commit 1b45079ffa2d0b8f66f77cdcf1af2d3d08a5515b
Author: Yuan Fu <casouri@gmail.com>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
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).
---
doc/emacs/text.texi | 10 +++++++
etc/NEWS | 9 ++++++
lisp/outline.el | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 102 insertions(+)
diff --git a/doc/emacs/text.texi b/doc/emacs/text.texi
index 281e244..9c2822c 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
diff --git a/etc/NEWS b/etc/NEWS
index 071edc5..79a8d11 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -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 6158ed5..95670e0 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)