emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jonathan Gregory <jgrg@autistici.org>
To: emacs-orgmode@gnu.org
Subject: Re: Get list of top-level headings
Date: Wed, 19 May 2021 10:48:29 -0300	[thread overview]
Message-ID: <874keyg74d.fsf@autistici.org> (raw)
In-Reply-To: <41-60a4c380-5-7d67d100@4304905>

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 ()
  (let (headlines)
     (lambda ()
       (let* ((element (org-element-at-point))
	      (headline (org-element-property :title element)))
	 (push headline headlines)))
    (print (nreverse headlines))))
--8<---------------cut here---------------end--------------->8---
Or by searching the buffer:

--8<---------------cut here---------------start------------->8---
(defun test-org-map ()
  (let (headlines)
      (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 

Maybe that's why you're getting errors.


      parent reply	other threads:[~2021-05-19 13:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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:

  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=874keyg74d.fsf@autistici.org \
    --to=jgrg@autistici.org \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


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).