* Get list of top-level headings
@ 2021-05-19 7:50 Florian Lindner
2021-05-19 13:33 ` John Kitchin
2021-05-19 13:48 ` Jonathan Gregory
0 siblings, 2 replies; 5+ messages in thread
From: Florian Lindner @ 2021-05-19 7:50 UTC (permalink / raw)
To: Org Mode List
[-- Attachment #1: Type: text/plain, Size: 1456 bytes --]
Hello,
I, an Emacs Lisp newbie, want to get a list of all top-level headings of the current buffer. My approach so far is:
(defun test-org-map()
(interactive)
(setq headings '())
(org-map-entries (lambda ()
(setq current-header-item (org-element-property :title (org-element-at-point))
(message "Header: %s" current-header-item)
(message "Is String: %s" (stringp (org-element-property :title (org-element-at-point))))
(setq headings (append current-header-item headings))
)
"LEVEL=1"
)
(dolist (heading headings)
(message "Header Item: %s" heading)
)
)
This gives the otput:
Header: AAA
Is String: t
Header: BBB
Is String: t
Header Item: 66 [3 times]
Header Item: 65 [3 times]
so basically the (org-element-property :title (org-element-at-point) does exactly what I want, but building the list does not what I want. I suppose that comes from a fundamental misunderstanding of how strings work in elisp.
I would appreciate a short explanation (or pointers) why this does not work. And of course, I am very open to completely different, likely better, approches to that simply problem!
Thanks,
Florian
[-- Attachment #2: Type: text/html, Size: 2199 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Get list of top-level headings
2021-05-19 7:50 Get list of top-level headings Florian Lindner
@ 2021-05-19 13:33 ` John Kitchin
2021-05-19 14:15 ` Jonathan Gregory
2021-05-19 13:48 ` Jonathan Gregory
1 sibling, 1 reply; 5+ messages in thread
From: John Kitchin @ 2021-05-19 13:33 UTC (permalink / raw)
To: Florian Lindner; +Cc: Org Mode List
[-- Attachment #1: Type: text/plain, Size: 1983 bytes --]
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
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 3:51 AM Florian Lindner <mailinglists@xgm.de> wrote:
> Hello,
>
> I, an Emacs Lisp newbie, want to get a list of all top-level headings of
> the current buffer. My approach so far is:
>
> (defun test-org-map()
> (interactive)
> (setq headings '())
> (org-map-entries (lambda ()
> (setq current-header-item (org-element-property
> :title (org-element-at-point))
> (message "Header: %s" current-header-item)
> (message "Is String: %s" (stringp
> (org-element-property :title (org-element-at-point))))
> (setq headings (append current-header-item headings))
> )
> "LEVEL=1"
> )
> (dolist (heading headings)
> (message "Header Item: %s" heading)
> )
> )
>
> This gives the otput:
>
> Header: AAA
> Is String: t
> Header: BBB
> Is String: t
> Header Item: 66 [3 times]
> Header Item: 65 [3 times]
>
> so basically the (org-element-property :title (org-element-at-point) does
> exactly what I want, but building the list does not what I want. I suppose
> that comes from a fundamental misunderstanding of how strings work in elisp.
>
> I would appreciate a short explanation (or pointers) why this does not
> work. And of course, I am very open to completely different, likely better,
> approches to that simply problem!
>
> Thanks,
> Florian
>
[-- Attachment #2: Type: text/html, Size: 2919 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Get list of top-level headings
2021-05-19 7:50 Get list of top-level headings Florian Lindner
2021-05-19 13:33 ` John Kitchin
@ 2021-05-19 13:48 ` Jonathan Gregory
1 sibling, 0 replies; 5+ messages in thread
From: Jonathan Gregory @ 2021-05-19 13:48 UTC (permalink / raw)
To: emacs-orgmode
Hello Florian
On 19 May 2021, Florian Lindner wrote:
> Hello,
>
> I, an Emacs Lisp newbie, want to get a list of all top-level
> headings
> of the current buffer. My approach so far is:
>
> (defun test-org-map()
> (interactive)
> (setq headings '())
> (org-map-entries (lambda ()
> (setq current-header-item
> (org-element-property :
> title (org-element-at-point))
> (message "Header: %s" current-header-item)
> (message "Is String: %s" (stringp
> (org-element-property :title (org-element-at-point))))
> (setq headings (append current-header-item
> headings))
> )
> "LEVEL=1"
> )
> (dolist (heading headings)
> (message "Header Item: %s" heading)
> )
> )
>
> This gives the otput:
>
> Header: AAA
> Is String: t
> Header: BBB
> Is String: t
> Header Item: 66 [3 times]
> Header Item: 65 [3 times]
>
> so basically the (org-element-property :title
> (org-element-at-point)
> does exactly what I want, but building the list does not what I
> want.
> I suppose that comes from a fundamental misunderstanding of how
> strings work in elisp.
>
> I would appreciate a short explanation (or pointers) why this
> does not
> work. And of course, I am very open to completely different,
> likely
> better, approches to that simply problem!
>
> Thanks,
> Florian
The org-map-entries function calls FUNC at each headline, so you
have to (1) find the headline/title and (2) add it to your list.
One way to do this is with the push macro.
--8<---------------cut here---------------start------------->8---
(defun test-org-map ()
(interactive)
(let (headlines)
(org-map-entries
(lambda ()
(let* ((element (org-element-at-point))
(headline (org-element-property :title element)))
(push headline headlines)))
"LEVEL=1")
(print (nreverse headlines))))
--8<---------------cut here---------------end--------------->8---
Or by searching the buffer:
--8<---------------cut here---------------start------------->8---
(defun test-org-map ()
(interactive)
(let (headlines)
(save-excursion
(goto-char (point-max))
(while (re-search-backward org-complex-heading-regexp nil t)
(let ((headline (match-string-no-properties 4)))
(when (= (org-current-level) 1)
(push headline headlines))))
(print headlines))))
--8<---------------cut here---------------end--------------->8---
BTW you're missing a closing parenthesis in:
(setq current-header-item (org-element-property :title
(org-element-at-point)))
Maybe that's why you're getting errors.
--
Jonathan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Get list of top-level headings
2021-05-19 13:33 ` John Kitchin
@ 2021-05-19 14:15 ` Jonathan Gregory
2021-05-19 14:36 ` John Kitchin
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Gregory @ 2021-05-19 14:15 UTC (permalink / raw)
To: emacs-orgmode
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Get list of top-level headings
2021-05-19 14:15 ` Jonathan Gregory
@ 2021-05-19 14:36 ` John Kitchin
0 siblings, 0 replies; 5+ messages in thread
From: John Kitchin @ 2021-05-19 14:36 UTC (permalink / raw)
To: Jonathan Gregory; +Cc: org-mode-email
[-- 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 --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-05-19 14:38 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-19 7:50 Get list of top-level headings Florian Lindner
2021-05-19 13:33 ` John Kitchin
2021-05-19 14:15 ` Jonathan Gregory
2021-05-19 14:36 ` John Kitchin
2021-05-19 13:48 ` Jonathan Gregory
Code repositories for project(s) associated with this public 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).