From aed2972d7400834210759727117c50de34826db9 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 22 Mar 2022 02:48:07 +0100 Subject: [PATCH] fix(org): don't show parent trees by default Instead of changing the behavior of the showNlevels startup options, I've added a new set of showNlevels* options to perform the new behavior of unfolding parent trees all the way up to the target level. Ref: https://www.reddit.com/r/emacs/comments/tj0na6/comment/i1laya6/?utm_source=reddit&utm_medium=web2x&context=3 --- modules/lang/org/config.el | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/modules/lang/org/config.el b/modules/lang/org/config.el index 65424cfae..89d768849 100644 --- a/modules/lang/org/config.el +++ b/modules/lang/org/config.el @@ -685,23 +685,35 @@ mutating hooks on exported output, like formatters." ;; Open help:* links with helpful-* instead of describe-* (advice-add #'org-link--open-help :around #'doom-use-helpful-a) - (defadvice! +org--show-parents-a (&optional arg) - "Show all headlines in the buffer, like a table of contents. -With numerical argument N, show content up to level N." - :override #'org-content - (interactive "p") - (org-show-all '(headings drawers)) - (save-excursion - (goto-char (point-max)) - (let ((regexp (if (and (wholenump arg) (> arg 0)) - (format "^\\*\\{%d,%d\\} " (1- arg) arg) - "^\\*+ ")) - (last (point))) - (while (re-search-backward regexp nil t) - (when (or (not (wholenump arg)) - (= (org-current-level) arg)) - (org-flag-region (line-end-position) last t 'outline)) - (setq last (line-end-position 0)))))) + ;; Unlike the stock showNlevels options, these will also show the parents of + ;; the target level, recursively. + (pushnew! org-startup-options + '("show2levels*" org-startup-folded show2levels*) + '("show3levels*" org-startup-folded show3levels*) + '("show4levels*" org-startup-folded show4levels*) + '("show5levels*" org-startup-folded show5levels*)) + + (defadvice! +org--more-startup-folded-options-a () + "Adds support for 'showNlevels*' startup options. +Unlike showNlevels, this will also unfold parent trees." + :before #'org-set-startup-visibility + (when-let (n (pcase org-startup-folded + (`show2levels* 2) + (`show3levels* 3) + (`show4levels* 4) + (`show5levels* 5))) + (org-show-all '(headings drawers)) + (save-excursion + (goto-char (point-max)) + (let ((regexp (if (and (wholenump n) (> n 0)) + (format "^\\*\\{%d,%d\\} " (1- n) n) + "^\\*+ ")) + (last (point))) + (while (re-search-backward regexp nil t) + (when (or (not (wholenump n)) + (= (org-current-level) n)) + (org-flag-region (line-end-position) last t 'outline)) + (setq last (line-end-position 0))))))) ;; Some uses of `org-fix-tags-on-the-fly' occur without a check on ;; `org-auto-align-tags', such as in `org-self-insert-command' and