From b7b188e233b76b0fa5116b099f5b5e324a2beac3 Mon Sep 17 00:00:00 2001 From: Morgan Smith Date: Wed, 12 Jun 2024 10:14:04 -0400 Subject: [PATCH] lisp/org-compat.el: Allow using imenu to visit non-leaf headlines * lisp/org-compat.el (org-imenu-get-tree): Add the current headline to the tree as a simple item even if it isn't a leaf. With a file like this: * headline 1 ** headline 2 We currently produce an imenu tree that looks like this: '(("headline 1" ("headline 2" . marker-2))) imenu has no clue where "headline 1" is located and thus the user can't navigate to it. With this patch installed imenu knows where non-leaf headlines are as the tree will now look like this: '(("headline 1" . marker-1) ("headline 1" ("headline 2" . marker-2))) Quirks: With the default `imenu-flatten' value of nil, it is still impossible to visit non-leaf headlines and no change is perceived. Setting `imenu-flatten' to 'group works as expected with the quirk that top level headlines don't end up in the group. Ex: * Headline 1 Group is "*" Setting the group to "Headline 1" somehow might be nice but would require upstream changes in imenu. ** Headline 2 Group is "Headline 1" *** Headline 3 Group is "Headline 1:Headline 2" Everything seems to work as expected when `imenu-flatten' is set to 'prefix or 'annotation. Link: https://orgmode.org/list/CH3PR84MB34241FF78D2A1D8653FE6056C5C22@CH3PR84MB3424.NAMPRD84.PROD.OUTLOOK.COM --- lisp/org-compat.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lisp/org-compat.el b/lisp/org-compat.el index 41c26ad72..a1b1bff47 100644 --- a/lisp/org-compat.el +++ b/lisp/org-compat.el @@ -1458,8 +1458,8 @@ This also applied for speedbar access." (let* ((m (point-marker)) (item (propertize headline 'org-imenu-marker m 'org-imenu t))) (push m org-imenu-markers) - (if (>= level last-level) - (push (cons item m) (aref subs level)) + (push (cons item m) (aref subs level)) + (unless (>= level last-level) (push (cons item (cl-mapcan #'identity (cl-subseq subs (1+ level)))) (aref subs level))