From ef67fa6e25b727f3d4188d261c776cb5815e7ddf Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Fri, 5 Jun 2020 15:46:14 +0200 Subject: [PATCH] Better org CUSTOM_ID properties See https://blog.phundrak.com/better-custom-ids-orgmode/ --- org/config/spacemacs.org | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/org/config/spacemacs.org b/org/config/spacemacs.org index c816d2f..eb3c9ba 100644 --- a/org/config/spacemacs.org +++ b/org/config/spacemacs.org @@ -2324,7 +2324,7 @@ haskell (message-make-fqdn)))))) (setq unique (concat etime postfix)))) (t (error "Invalid `org-id-method'"))) - (concat prefix unique))) + (concat prefix (car (split-string unique "-"))))) #+END_SRC Now, let’s see the function that will be used to get the custom id of a @@ -2333,19 +2333,27 @@ haskell #+BEGIN_SRC emacs-lisp (defun eos/org-custom-id-get (&optional pom create prefix) "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 - CREATE is non nil, create a CUSTOM_ID if none is present - already. PREFIX will be passed through to `eos/org-id-new'. In - any case, the CUSTOM_ID of the entry is returned." + + 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 CREATE + is non nil, create a CUSTOM_ID if none is present already. PREFIX + will be passed through to `eos/org-id-new'. In any case, the + CUSTOM_ID of the entry is returned." (interactive) (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 ((and id (stringp 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-id-add-location id (buffer-file-name (buffer-base-buffer))) @@ -2357,10 +2365,11 @@ haskell header, then the above function is called. #+BEGIN_SRC emacs-lisp (defun eos/org-add-ids-to-headlines-in-file () - "Add CUSTOM_ID properties to all headlines in the current - file which do not already have one. Only adds ids if the - `auto-id' option is set to `t' in the file somewhere. ie, - ,#+OPTIONS: auto-id:t" + "Add CUSTOM_ID properties to all headlines in the current file + which do not already have one. + + Only adds ids if the `auto-id' option is set to `t' in the file + somewhere. ie, #+OPTIONS: auto-id:t" (interactive) (save-excursion (widen)