Browse Source

org-id: Allow file name to be overridden on ID creation

* lisp/org-id.el (org-id-overriding-file-name): New variable.
(org-id-get): Prefer org-id-overriding-file-name over the buffer's
file name if set.
(org-id-locations-load): Give a more informative error when file is
nil.
* lisp/org.el (org-clone-subtree-with-time-shift): Let-bind
org-id-overriding-file-name, enabling an ID to be created for a cloned
subtree rather than crashing with a type error.

Note that, before 9865e6bd8 (org-id: Speedup, minor functional change
and fix, 2019-08-01), this wouldn't fail with a type error.  However,
the ID would not be added correctly because org-id-add-location simply
wouldn't process the ID at all if the passed file was nil.

Reported-by: Ian Eure <ian@retrospec.tv>
<87a73caayj.fsf@phaktory>
Kyle Meyer 5 years ago
parent
commit
c716b7c085
2 changed files with 13 additions and 2 deletions
  1. 11 2
      lisp/org-id.el
  2. 2 0
      lisp/org.el

+ 11 - 2
lisp/org-id.el

@@ -259,6 +259,11 @@ Create an ID if necessary."
   (interactive)
   (org-kill-new (org-id-get nil 'create)))
 
+(defvar org-id-overriding-file-name nil
+  "Tell `org-id-get' to use this as the file name when creating an ID.
+This is useful when working with contents in a temporary buffer
+that will be copied back to the original.")
+
 ;;;###autoload
 (defun org-id-get (&optional pom create prefix)
   "Get the ID property of the entry at point-or-marker POM.
@@ -275,7 +280,9 @@ In any case, the ID of the entry is returned."
        (create
 	(setq id (org-id-new prefix))
 	(org-entry-put pom "ID" id)
-	(org-id-add-location id (buffer-file-name (buffer-base-buffer)))
+	(org-id-add-location id
+			     (or org-id-overriding-file-name
+				 (buffer-file-name (buffer-base-buffer))))
 	id)))))
 
 ;;;###autoload
@@ -572,8 +579,10 @@ When FILES is given, scan also these files."
 (defun org-id-add-location (id file)
   "Add the ID with location FILE to the database of ID locations."
   ;; Only if global tracking is on, and when the buffer has a file
+  (unless file
+    (error "bug: org-id-get expects a file-visiting buffer"))
   (let ((afile (abbreviate-file-name file)))
-    (when (and org-id-track-globally id file)
+    (when (and org-id-track-globally id)
       (unless org-id-locations (org-id-locations-load))
       (puthash id afile org-id-locations)
       (unless (member afile org-id-files)

+ 2 - 0
lisp/org.el

@@ -192,6 +192,7 @@ Stars are put in group 1 and the trimmed body in group 2.")
 (defvar org-radio-target-regexp)
 (defvar org-target-link-regexp)
 (defvar org-target-regexp)
+(defvar org-id-overriding-file-name)
 
 ;; load languages based on value of `org-babel-load-languages'
 (defvar org-babel-load-languages)
@@ -7863,6 +7864,7 @@ with the original repeater."
 	   (nmin 1)
 	   (nmax n)
 	   (n-no-remove -1)
+	   (org-id-overriding-file-name (buffer-file-name (buffer-base-buffer)))
 	   (idprop (org-entry-get beg "ID")))
       (when (and doshift
 		 (string-match-p "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>"