Hi,

Please see the attached patch that remedies a longstanding, simple shortcoming in Org's Imenu support.

Thanks,
Adam
From 00104b2b9246b19cdb02bbce993d120581dc9f0e Mon Sep 17 00:00:00 2001
From: Adam Porter <a...@alphapapa.net>
Date: Mon, 30 May 2022 02:59:06 -0500
Subject: [PATCH] org-imenu-get-tree: Allow parent headings to be selected
 themselves

Imenu only allows leaf nodes to be chosen.  In programming language
buffers, non-leaf nodes are "virtual" nodes, i.e. categories like
"functions" or "variables" rather than actual locations in the buffer.
But in Org buffers, non-leaf nodes are headings, which the user may
want to select with Imenu.

So now, for a non-leaf node (i.e. headings that have children), we
push an extra item to the result, without including its children, so
that it can be listed and selected in Imenu as a leaf node.
---
 lisp/org-compat.el | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index 7041976..e9c53cf 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -1053,6 +1053,11 @@ 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)
+             (when (save-excursion (org-goto-first-child))
+               ;; Entry has children: push an extra item for entry
+               ;; itself so it can be selected (Imenu only allows
+               ;; selection of leaf nodes).
+               (push (cons item m) (aref subs level)))
 	     (if (>= level last-level)
 		 (push (cons item m) (aref subs level))
 	       (push (cons item
-- 
2.7.4

Reply via email to