emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Bug: org-id-add-location fails when cloning a subtree [9.1.9 (release_9.1.9-65-g5e4542 @ /home/ieure/.emacs.d/straight/build/org/)]
@ 2020-04-15 17:57 Ian Eure
  2020-04-16  5:18 ` Subject: [PATCH] org-id: Allow file name to be overridden on ID creation Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Eure @ 2020-04-15 17:57 UTC (permalink / raw)
  To: emacs-orgmode


If you have a subtree of entries with :ID: properties and call M-x 
`org-clone-subtree-with-time-shift' on it, an error occurs:

    Wrong type argument: stringp, nil

This is due to the interaction of 
`org-clone-subtree-with-time-shift' and `org-id-add-location'. 
When the former is called, it creates a temp buffer to operate on: 
https://code.orgmode.org/bzg/org-mode/src/master/lisp/org.el#L7801

Then calls `org-id-get-create': 
https://code.orgmode.org/bzg/org-mode/src/master/lisp/org.el#L7808

Which calls `org-id-get', which calls `buffer-file-name', which 
evals to nil, because no file backs the temp buffer.  That nil is 
then passed to `org-id-add-location': 
https://code.orgmode.org/bzg/org-mode/src/master/lisp/org-id.el#L277

Which unconditionally calls `abbreviate-file-name': 
https://code.orgmode.org/bzg/org-mode/src/master/lisp/org-id.el#L575

Which signals the error.

I’ve confirmed that this is still a problem in the 9.3.6, which is 
the latest release, as well as Git master.

A simplistic fix would be to swap the let and when lines in 
`org-id-add-location':

diff --git a/lisp/org-id.el b/lisp/org-id.el
index 1ff433bd8..945a9a1f6 100644
--- a/lisp/org-id.el
+++ b/lisp/org-id.el
@@ -572,8 +572,8 @@ 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
-  (let ((afile (abbreviate-file-name file)))
-    (when (and org-id-track-globally id file)
+  (when (and org-id-track-globally id file)
+    (let ((afile (abbreviate-file-name file)))
       (unless org-id-locations (org-id-locations-load))
       (puthash id afile org-id-locations)
       (unless (member afile org-id-files)

However, this causes a more subtle bug, which is that the IDs of 
cloned subtrees won’t be tracked correctly.  Perhaps a solution to 
that is to use an indirect buffer of the source when cloning, 
instead of a temp buffer.


Emacs  : GNU Emacs 26.1 (build 1, x86_64-pc-linux-gnu, X toolkit, 
Xaw3d scroll bars)
 of 2019-09-22, modified by Debian
Package: Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ 
/home/ieure/.emacs.d/straight/build/org/)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Subject: [PATCH] org-id: Allow file name to be overridden on ID creation
  2020-04-15 17:57 Bug: org-id-add-location fails when cloning a subtree [9.1.9 (release_9.1.9-65-g5e4542 @ /home/ieure/.emacs.d/straight/build/org/)] Ian Eure
@ 2020-04-16  5:18 ` Kyle Meyer
  2020-04-21  0:52   ` Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2020-04-16  5:18 UTC (permalink / raw)
  To: Ian Eure; +Cc: emacs-orgmode

Ian Eure <ian@retrospec.tv> writes:

> If you have a subtree of entries with :ID: properties and call M-x 
> `org-clone-subtree-with-time-shift' on it, an error occurs:
>
>     Wrong type argument: stringp, nil
>
> This is due to the interaction of 
> `org-clone-subtree-with-time-shift' and `org-id-add-location'. 
> [...]

Thanks for the nice analysis.

> I’ve confirmed that this is still a problem in the 9.3.6, which is 
> the latest release, as well as Git master.

Yeah, I suspect this worked at one time, given there is dedicated
handling of IDs in org-clone-subtree-with-time-shift.
  [... bisecting ...]
The failure started with 9865e6bd8 (org-id: Speedup, minor functional
change and fix, 2019-08-01), which was part of the 9.3 release.
Interestingly that seems to be functionally equivalent to what you
mentioned next:

> A simplistic fix would be to swap the let and when lines in 
> `org-id-add-location':
>
> diff --git a/lisp/org-id.el b/lisp/org-id.el
> index 1ff433bd8..945a9a1f6 100644
> --- a/lisp/org-id.el
> +++ b/lisp/org-id.el
> @@ -572,8 +572,8 @@ 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
> -  (let ((afile (abbreviate-file-name file)))
> -    (when (and org-id-track-globally id file)
> +  (when (and org-id-track-globally id file)
> +    (let ((afile (abbreviate-file-name file)))
>        (unless org-id-locations (org-id-locations-load))
>        (puthash id afile org-id-locations)
>        (unless (member afile org-id-files)
>
> However, this causes a more subtle bug, which is that the IDs of 
> cloned subtrees won’t be tracked correctly.

That sounds right, so it seems it was just broken in a quieter way
before.

> Perhaps a solution to that is to use an indirect buffer of the source
> when cloning, instead of a temp buffer.

Hmm, that should indeed work, but I'd be leery of it introducing other
bugs and complications.  How about something like this?

-- >8 --
Subject: [PATCH] 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>
---
 lisp/org-id.el | 13 +++++++++++--
 lisp/org.el    |  2 ++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/lisp/org-id.el b/lisp/org-id.el
index 14a461500..90d13275f 100644
--- a/lisp/org-id.el
+++ b/lisp/org-id.el
@@ -259,6 +259,11 @@ (defun org-id-copy ()
   (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 @@ (defun org-id-get (&optional pom create prefix)
        (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 @@ (defun org-id-locations-load ()
 (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)
diff --git a/lisp/org.el b/lisp/org.el
index 275120662..879d3a03a 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -192,6 +192,7 @@ (defvar org-indent-indentation-per-level)
 (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 @@ (defun org-clone-subtree-with-time-shift (n &optional shift)
 	   (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]*>"

base-commit: 4f9e07cbb4bd9e9353e941b0a00bbd401f8696c4
-- 
2.26.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: Subject: [PATCH] org-id: Allow file name to be overridden on ID creation
  2020-04-16  5:18 ` Subject: [PATCH] org-id: Allow file name to be overridden on ID creation Kyle Meyer
@ 2020-04-21  0:52   ` Kyle Meyer
  0 siblings, 0 replies; 3+ messages in thread
From: Kyle Meyer @ 2020-04-21  0:52 UTC (permalink / raw)
  To: Ian Eure; +Cc: emacs-orgmode

Kyle Meyer <kyle@kyleam.com> writes:

> Subject: [PATCH] org-id: Allow file name to be overridden on ID creation

Applied (c716b7c08).


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-04-21  0:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-15 17:57 Bug: org-id-add-location fails when cloning a subtree [9.1.9 (release_9.1.9-65-g5e4542 @ /home/ieure/.emacs.d/straight/build/org/)] Ian Eure
2020-04-16  5:18 ` Subject: [PATCH] org-id: Allow file name to be overridden on ID creation Kyle Meyer
2020-04-21  0:52   ` Kyle Meyer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).