0

Suppose I want to find all notes with the property "prop-name".

I think org-map-entries can do this but I'm having problems working out how properties work.

The documentation talks about a "MATCH" parameter but offloads the documentation to org-agenda, which I'm finding difficult to understand or call programmatically. I think this looks like the relevant documentation.

MATCH is a tags/property/todo match as it is used in the agenda tags view.
Only headlines that are matched by this query will be considered during
the iteration.  When MATCH is nil or t, all headlines will be
visited by the iteration.

I think be something like (org-map-entries (lamdba (x) x) "prop-name={.*}").

3
  • 1
    The value of the property does not matter? Can there be entries with an empty value for the property and if so, do you want those included or excluded?
    – NickD
    May 9, 2023 at 15:22
  • 1
    If you are looking for the actual example code for org-map-entries, how about this documentation? Using the Mapping API
    – roomworoof
    May 9, 2023 at 16:38
  • You can probably get what you want with C-c / m i.e. a sparse tree search. Either this or org-map-entries will get you a list of headlines. The main question is: what do you want to do with those headlines?
    – NickD
    May 9, 2023 at 17:28

1 Answer 1

2

After renaming the property, so that its name does not contain a dash[1], here is an example file with a code block that you can try out:


* foo

** one

** two
:PROPERTIES:
:someprop: some value
:END:


** three

** four
:PROPERTIES:
:someprop:
:END:


* Code                                                                                                        :noexport:

#+begin_src elisp :results drawer
  (save-excursion
    (goto-char  2)
    (org-map-entries (lambda () (nth 4 (org-heading-components))) "someprop<>\"\"" 'tree))
#+end_src

#+RESULTS:
:results:
(two)
:end:

The matcher matches entries with non-empty values for the property someprop. Note that the scope is set to tree, so in conjunction with the save-excursion/goto-char, it applies only to the tree starting with headline foo: it does not apply to the Code headline. Try adding another top-level headline (with or without property drawers) after the foo tree (i.e. just before the Code headline) and see that it too will be skipped.

Note that the function that org-map-entries applies to each matching headline should take no arguments - see its doc string with C-h f org-map-entries. The function is called with point set to the beginning of the headline, so that's all it knows: in the example, we call org-heading-components which assumes that point is at a headline and proceeds to parse it to get the individual components as a list; in particular the title is the fifth element of the list (remember nth counts elements from 0). See the doc string of the function for the other components.

One other important point is that the matcher does not seem to distinguish between headlines with no someprop property at all and headlines that have a someprop property with an empty value: change the matcher to "someprop=\"\"" and it will match every headline in the foo tree except the two entry.


[1] Although dashes are supposedly legal in property names (see Org syntax), the matcher code seems to be buggy and does not like the dash: with the dash, it found no matches at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.