From 6ca5337a986fea7252a81887827e6c451fd8a9e9 Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Thu, 8 Sep 2022 09:56:59 +0200 Subject: [PATCH] Add prefix argument to outline-cycle-buffer * lisp/outline.el (outline-cycle-buffer): Add prefix argument to show headings up to a given level. Handle the case where the top heading level is not 1. --- lisp/outline.el | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lisp/outline.el b/lisp/outline.el index 178cbe3071..f09481f7ea 100644 --- a/lisp/outline.el +++ b/lisp/outline.el @@ -1581,7 +1581,7 @@ outline-cycle (defvar-local outline--cycle-buffer-state 'show-all "Internal variable used for tracking buffer cycle state.") -(defun outline-cycle-buffer () +(defun outline-cycle-buffer (&optional level) "Cycle visibility state of the body lines of the whole buffer. This cycles the visibility of all the subheadings and bodies of all @@ -1590,20 +1590,28 @@ outline-cycle-buffer `Hide all' means hide all the buffer's subheadings and their bodies. `Headings only' means show all the subheadings, but not their bodies. -`Show all' means show all the buffer's subheadings and their bodies." - (interactive) - (let (has-top-level) +`Show all' means show all the buffer's subheadings and their bodies. + +With a prefix argument, show headings up to that LEVEL." + (interactive (list (when current-prefix-arg + (prefix-numeric-value current-prefix-arg)))) + (let (top-level) (save-excursion (goto-char (point-min)) - (while (not (or has-top-level (eobp))) - (when (outline-on-heading-p t) - (when (= (funcall outline-level) 1) - (setq has-top-level t))) + (while (not (or (eq top-level 1) (eobp))) + (when-let ((level (and (outline-on-heading-p t) + (funcall outline-level)))) + (when (< level (or top-level most-positive-fixnum)) + (setq top-level (max level 1)))) (outline-next-heading))) (cond + (level + (outline-hide-sublevels level) + (setq outline--cycle-buffer-state 'all-heading) + (message "All headings up to level %s" level)) ((and (eq outline--cycle-buffer-state 'show-all) - has-top-level) - (outline-hide-sublevels 1) + top-level) + (outline-hide-sublevels top-level) (setq outline--cycle-buffer-state 'top-level) (message "Top level headings")) ((or (eq outline--cycle-buffer-state 'show-all) -- 2.37.3