Next: , Previous: , Up: Update Utility Commands   [Index]


M.3.2 Check for CID

This code checks whether an Org file contains a ‘custom_id’ of a particular value. It accepts a cid-value and an optional directory. If the directory is not given, then it defaults to the current directory. If throws an error if the directory does not exist. It returns ‘nil’ if the given directory does not contain an Org file. It returns ‘t’ if the Org file contains a node property of ‘custom_id’ and value cid-value, or ‘nil’ if not. It uses get-parsed-org-tree.

(defun org-tree-cid-p (cid-value &optional org-dir)
  "Check whether an org file contains a custom_id of CID"
  (let ((tree (get-parsed-org-tree org-dir)))
    (car (org-element-map tree 'property-drawer
           (lambda (pd) (org-element-map (org-element-contents pd) 'node-property
                          (lambda (np)
                            (and
                             (string= "custom_id" (org-element-property :key np))
                             (string= cid-value (org-element-property :value np))))))
           nil t))))
(org-tree-cid-p cid dir)