emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: Jonathan Gregory <jgrg@autistici.org>
Cc: org-mode-email <emacs-orgmode@gnu.org>
Subject: Re: Get list of top-level headings
Date: Wed, 19 May 2021 10:36:41 -0400	[thread overview]
Message-ID: <CAJ51EToUdRYS97NHQO2+CzEJ3Wzo7iYtm1DHMzz3TJbyMAfgig@mail.gmail.com> (raw)
In-Reply-To: <871ra2g65q.fsf@autistici.org>

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

that is often true, especially with large buffers, but you have to add a
bunch of code to go to point-max, and check the level with this.

#+BEGIN_SRC emacs-lisp
(save-excursion
  (goto-char (point-max))
  (let (components
(headings '()))
    (while (re-search-backward org-complex-heading-regexp nil t)
      (setq components (org-heading-components))
      (when (= (first components) 1)
(push (fifth components) headings)))
    headings))
#+END_SRC

This takes about 0.04 ms on a small example. The org-map-entries approach
takes 0.6ms on the same example. In a big buffer that might be noticeable!

John

-----------------------------------
Professor John Kitchin (he/him/his)
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



On Wed, May 19, 2021 at 10:19 AM Jonathan Gregory <jgrg@autistici.org>
wrote:

> Hi
>
> On 19 May 2021, John Kitchin wrote:
>
> > I think this is all you need to get a list of titles of level 1
> > headings as strings
> >
> > (org-map-entries (lambda () (fifth (org-heading-components)))
> > "LEVEL=1")
> >
> > this also works for me:
> >
> > #+BEGIN_SRC emacs-lisp
> > (org-map-entries (lambda () (org-element-property :title
> > (org-element-at-point)) ) "LEVEL=1")
> > #+END_SRC
>
> This is a better approach indeed. No need to create a new list,
> although I get faster results using:
>
>  (while (re-search-backward org-complex-heading-regexp nil t)
>
>
> --
> Jonathan
>
>

[-- Attachment #2: Type: text/html, Size: 2336 bytes --]

  reply	other threads:[~2021-05-19 14:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-19  7:50 Florian Lindner
2021-05-19 13:33 ` John Kitchin
2021-05-19 14:15   ` Jonathan Gregory
2021-05-19 14:36     ` John Kitchin [this message]
2021-05-19 13:48 ` Jonathan Gregory

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=CAJ51EToUdRYS97NHQO2+CzEJ3Wzo7iYtm1DHMzz3TJbyMAfgig@mail.gmail.com \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=jgrg@autistici.org \
    --subject='Re: Get list of top-level headings' \
    /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).