Better org CUSTOM_ID properties

See https://blog.phundrak.com/better-custom-ids-orgmode/
This commit is contained in:
Lucien Cartier-Tilet 2020-06-05 15:46:14 +02:00
parent b81224865f
commit ef67fa6e25
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -2324,7 +2324,7 @@ haskell
(message-make-fqdn)))))) (message-make-fqdn))))))
(setq unique (concat etime postfix)))) (setq unique (concat etime postfix))))
(t (error "Invalid `org-id-method'"))) (t (error "Invalid `org-id-method'")))
(concat prefix unique))) (concat prefix (car (split-string unique "-")))))
#+END_SRC #+END_SRC
Now, lets see the function that will be used to get the custom id of a Now, lets see the function that will be used to get the custom id of a
@ -2333,19 +2333,27 @@ haskell
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun eos/org-custom-id-get (&optional pom create prefix) (defun eos/org-custom-id-get (&optional pom create prefix)
"Get the CUSTOM_ID property of the entry at point-or-marker POM. "Get the CUSTOM_ID property of the entry at point-or-marker POM.
If POM is nil, refer to the entry at point. If the entry does
not have an CUSTOM_ID, the function returns nil. However, when If POM is nil, refer to the entry at point. If the entry does not
CREATE is non nil, create a CUSTOM_ID if none is present have an CUSTOM_ID, the function returns nil. However, when CREATE
already. PREFIX will be passed through to `eos/org-id-new'. In is non nil, create a CUSTOM_ID if none is present already. PREFIX
any case, the CUSTOM_ID of the entry is returned." will be passed through to `eos/org-id-new'. In any case, the
CUSTOM_ID of the entry is returned."
(interactive) (interactive)
(org-with-point-at pom (org-with-point-at pom
(let ((id (org-entry-get nil "CUSTOM_ID"))) (let* ((orgpath (mapconcat #'identity (org-get-outline-path) "-"))
(heading (replace-regexp-in-string
"/" "-"
(replace-regexp-in-string
" " "_" (if (string= orgpath "")
(org-get-heading t t t t)
(concat orgpath "-" (org-get-heading t t t t))))))
(id (org-entry-get nil "CUSTOM_ID")))
(cond (cond
((and id ((and id
(stringp id) (stringp id)
(string-match "\\S-" id)) id) (string-match "\\S-" id)) id)
(create (setq id (eos/org-id-new (concat prefix "h"))) (create (setq id (eos/org-id-new (concat prefix heading)))
(org-entry-put pom "CUSTOM_ID" id) (org-entry-put pom "CUSTOM_ID" id)
(org-id-add-location id (org-id-add-location id
(buffer-file-name (buffer-base-buffer))) (buffer-file-name (buffer-base-buffer)))
@ -2357,10 +2365,11 @@ haskell
header, then the above function is called. header, then the above function is called.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun eos/org-add-ids-to-headlines-in-file () (defun eos/org-add-ids-to-headlines-in-file ()
"Add CUSTOM_ID properties to all headlines in the current "Add CUSTOM_ID properties to all headlines in the current file
file which do not already have one. Only adds ids if the which do not already have one.
`auto-id' option is set to `t' in the file somewhere. ie,
,#+OPTIONS: auto-id:t" Only adds ids if the `auto-id' option is set to `t' in the file
somewhere. ie, #+OPTIONS: auto-id:t"
(interactive) (interactive)
(save-excursion (save-excursion
(widen) (widen)