Next: Replace Build Tools, Previous: Add a Key-Value, Up: Update Old Project [Index]
This code checks a project file F to see if it contains a ‘property: value’ pair (P, V) in a property drawer right under the headline HL. If it does not, it adds one. It will return nil if the headline is not found.
(defun add-pv-to-hl (f hl p v)
"In file F add a property P with value V into a property
drawer (creating one if necessary) at headline HL."
(message "In `add-pv-to-hl' with file:`%s' headline:`%s' property:`%s' value:`%s'" f hl p v)
(with-current-buffer (find-file-noselect f)
(save-excursion
(goto-char (point-min))
(let ((found (re-search-forward (concat "^[*]\\{1,\\}\s*" hl))))
(message "...found %s" hl)
(beginning-of-line 2)
(let* ((e (org-element-at-point))
(et (org-element-type e)))
(unless (string= et "property-drawer")
(message "...adding property drawer to headline:`%s'" hl)
(org-insert-property-drawer))
(unless (org-entry-get (point) p)
(message "...adding property:`%s' with value:`%s' to property drawer." p v)
(org-entry-put (point) p v))))
(when (buffer-modified-p)
(message "Saving buffer:`%s'" (current-buffer))
(save-buffer)))))