emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get list of top-level headings


From: John Kitchin
Subject: Re: Get list of top-level headings
Date: Wed, 19 May 2021 10:36:41 -0400

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


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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]