emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Adam Porter <adam@alphapapa.net>
To: emacs-orgmode@gnu.org
Subject: [PATCH] org-imenu-get-tree: Allow parent headings to be selected themselves
Date: Mon, 30 May 2022 03:07:01 -0500	[thread overview]
Message-ID: <e205635f-0b18-bbf8-5a7d-65f907079fce@alphapapa.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 122 bytes --]

Hi,

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

Thanks,
Adam

[-- Attachment #2: 0001-org-imenu-get-tree-Allow-parent-headings-to-be-selec.patch --]
[-- Type: text/x-patch, Size: 1538 bytes --]

From 00104b2b9246b19cdb02bbce993d120581dc9f0e Mon Sep 17 00:00:00 2001
From: Adam Porter <adam@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	other threads:[~2022-05-30  8:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-30  8:07 Adam Porter [this message]
2022-05-30 12:22 ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e205635f-0b18-bbf8-5a7d-65f907079fce@alphapapa.net \
    --to=adam@alphapapa.net \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: [PATCH] org-imenu-get-tree: Allow parent headings to be selected themselves' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).