The TLDR is the code:
(while (?????)
(org-next-visible-heading 1)
(org-todo 'todo)
)
What should I put in the condition ?????
in order to stop at the end of the file, or after the last visible heading?
The longer context:
I'm using trying to use elisp to modify org-mode files for emacs. I'm new to elisp and finding it hard to find the relevant documentation for some basic things.
I want to iterate over the headings in an org-mode buffer and modify the status (i.e. todo) keyword. I've seen it suggested that if I want to modify the buffer, it is better not to use org-element-map
but instead to use something like org-next-visible-heading
or org-forward-same-level
, and at each point use org-todo
to modify the heading.
I know how to write a while loop, but I don't know how to write the condition as I haven't been able to find out how to detect if I'm at the end of the buffer, even though this seems to be a trivial thing to want to do.
I'm happy to be told there is a better approach to my problem but I would still like to know how to detect the end of a buffer in general.
Rather than looping, you can use org-map-entries
. Here is an example, pretty much from the doc, that marks each headline as a TODO item:
(org-map-entries '(org-todo "TODO") t 'file 'archive 'comment)
It specifies a function to apply to each headline: (org-todo "TODO")
; no tag or property matching; file
scope; and skips archive
and comment
headlines.
Read the linked doc and also do C-h f org-map-entries
, but don't get too scared by the doc string of the function: org-map-entries
is very powerful, but it will take some time to understand how to do things with it and harness its power.