1

I have files with this format

#+TITLE: Magit Cheatsheet
:PROPERTIES:
:Status: Open
:Tags: tools, emacs, org, magit
:Creation: [2021-03-03 Wed]
:END:

I'd like to be able to read the title and these properties for a given file name (not the currently open file) with elisp. I found this but it isn't working for me and seems too complex for something as simple as this.

3
  • The contents you show above is not a legal Org mode file. Do M-x org-lint to see why. There are two problems: the :PROPERTIES: drawer is only legal under a headline (there are other ways to define properties at the top level); and there is a special property called TAGS that Org mode uses internally (and the case does not matter), which conflicts with your Tags property.
    – NickD
    Mar 11 at 18:56
  • @NickD Has this changed? I seem to recall seeing something about a file-level properties drawer that was due to be release..?
    – Adam
    Jul 20 at 9:25
  • Yes, this went into 9.4: see ORG-NEWS - currently at line 594 of the file, but that is likely to change. And org-lint needs to be updated to recognize this possibility.
    – NickD
    Jul 20 at 18:26
0

I cannot answer the question about the properties until you fix the file (how you fix it will affect the proposed solution).

For the title however, the following code works (Q: what is the shortest lie in computing? A: It works!):

#+begin_src elisp :results drawer
  (defun ndk/get-keyword-key-value (kwd)
     (let ((data (cadr kwd)))
       (list (plist-get data :key)
             (plist-get data :value))))

  (defun ndk/org-current-buffer-get-title ()
      (nth 1
       (assoc "TITLE"
        (org-element-map (org-element-parse-buffer 'greater-element)
            '(keyword)
            #'ndk/get-keyword-key-value))))

  (defun ndk/org-file-get-title (file)
    (with-current-buffer (find-file-noselect file)
         (ndk/org-current-buffer-get-title)))

  (ndk/org-file-get-title "/tmp/foo86.org")
  #+end_src

The main function is the last one ndk/org-file-get-title which takes care of opening the file and running the ndk/org-current-buffer-get-title function on the resulting buffer. The latter functions uses the almighty org-element-map function on a subset of the data that the parser returns (that's for efficiency only): we map the ndk/get-keyword-key-value function on all the keyword elements. This function returns a (key value) list for each of the keywords that it gets called on, and the mapper accumulates those pairs into a list. We then select the pair that contains the string "TITLE" as the key, and the (nth 1 ...) call gets the value part of the pair (the key part would be (nth 0 ...)).

A minor variation of this would also work for dealing with #+PROPERTY keywords, but dealing with property drawers under a headline would be more complicated (I think - I haven't tried it out).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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