0

I have a function I wrote that edits a file's PROPERTIES drawer on save and, if it finds the "MODIFIED" field it updates it with the current time. This works great as long as the drawer is open, but if the drawer is closed (the default state of a drawer on a newly opened file) than it deletes all parts of the drawer below the "MODIFIED" field.

This is my function

(defun 0x44/update-org-modified-property ()
  "If a file contains a '#+MODIFED property update it to contain
  the current date/time"
  (interactive)
  (save-excursion
    (widen)
    (goto-char (point-min))
    (when (re-search-forward "^:MODIFIED:" (point-max) t)
      (progn
        (kill-line)
        (insert (format-time-string " <%Y-%m-%d %a %H:%M>"))))))

And I just use add-hook to add it to before-save-hook.

How can I edit the values in a closed drawer? When a drawer is closed it seems that the concept of "lines" goes out the window and everything shares a single line. Thus, when I go to a point and run (kill-line) it kills everything after that point, which includes everything in the drawer below that specific k/v pair.

4
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Aug 17, 2022 at 15:26
  • What's the question?
    – Drew
    Commented Aug 17, 2022 at 15:27
  • @Drew the question is "How can I edit the values in a closed drawer?" When a drawer is closed it seems that the concept of "lines" goes out the window and everything shares a single line. Thus, when I go to a point and run (kill-line) it kills everything after that point, which includes everything in the drawer below that specific k/v pair. Commented Aug 20, 2022 at 11:49
  • 1
    Put that info in the question itself - there's no question there. (Comments can be deleted at any time. Q & A need to stand on their own.) Thx.
    – Drew
    Commented Aug 20, 2022 at 15:51

1 Answer 1

1

I am not sure it's possible to edit a closed drawer the way I was trying to originally. It seems every line in the closed drawer is treated as being on the same line, and so when you clear the line after a given point, like I was doing, you end up clearing the drawer below that point. So I ended up looking for a function I could use to edit the drawer instead.

I did some digging in the docs for emacs and elisp. I initially found org-set-property-and-value and this function in turn was calling org-set-property. I updated the above to use org-set-property instead of searching for a keyword, killing the line, and then inserting a new line. However that resulted in the "MODIFIED" property getting set to the current entry which might be the file or it might be the heading at point. I read into the org-set-property function and discovered it's calling org-entry-put, which takes a point, a property key and a value. With this I updated my above function to:

(defun 0x44/update-org-modified-property ()
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^:MODIFIED:" nil t)
      (org-entry-put (point-min) "MODIFIED"
                     (format-time-string " <%Y-%m-%d %a %H:%M>")))))

It will now update a file, which contains the "MODIFIED" key without deleting other pairs in the drawer.

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.