emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-capture: Update plist before finalizing
@ 2020-07-25 22:50 Leo Vivier
  2020-09-02  4:50 ` Kyle Meyer
  0 siblings, 1 reply; 4+ messages in thread
From: Leo Vivier @ 2020-07-25 22:50 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3124 bytes --]

Hi there,

I’m working on the parallelisation of `org-capture' for Org-roam, and
I’ve run into a problem with the updating of `org-capture-plist'.

;;---------------------------------------------------------------------
;; DESCRIPTION
;;---------------------------------------------------------------------
We use the global-variable `org-capture-plist' to populate the
local-variable `org-capture-current-plist' on the init of the
`org-capture' buffer.  However, we do not do the opposite (i.e. update
the global-variable with the local-variable) on `org-capture-finalize'.

This is fine for the majority of `org-capture-finalize', since we’re
using the LOCAL arg of `org-capture-get' to read
`org-capture-current-plist' instead of `org-capture-list', but this
trick does not work for `org-capture-after-finalize', since the hook is
run after the `org-capture-buffer' has been closed.

This causes problem with `:kill-buffer t', and it limits what can be
done with cleanup functions in `org-capture-after-finalize'.

;;---------------------------------------------------------------------
;; DEMONSTRATION
;;---------------------------------------------------------------------
Tested in emacs -Q.

Summary:
- Start a capture process (A)
- Start another capture process (B)
- Cancel B
- Cancel A

Form:
--------------------------------[START]--------------------------------
;; Eval the following form:
(progn
  (setq org-capture-templates
        '(("a" "foo" plain (file "/tmp/foo.org") "* %?"
           :kill-buffer t)
          ("b" "bar" plain (file "/tmp/bar.org") "* %?"
           :kill-buffer t)))
  (let* ((a (save-window-excursion
              (org-capture nil "a")
              (current-buffer)))
         (b (save-window-excursion
              (org-capture nil "b")
              (current-buffer))))
    (with-current-buffer b
      (org-capture-kill))
    (with-current-buffer a
      (org-capture-kill))))

;; Result: (error "Selecting deleted buffer")
;; `foo.org' is still alive
---------------------------------[END]---------------------------------

;;---------------------------------------------------------------------
;; ANALYSIS
;;---------------------------------------------------------------------
The problem happens during `org-capture-after-finalize'. A’s
`org-capture-finalize' does not update `org-capture-plist' with the value
of `org-current-capture-plist' during its execution, which means that
`org-capture-plist' still holds B’s value, including :buffer.  Since B’s
base-buffer was killed, :buffer points to a killed buffer, which is what
is raising the error.

;;---------------------------------------------------------------------
;; PATCH
;;---------------------------------------------------------------------
I propose to update `org-capture-plist' early in `org-capture-finalize'.
I don’t think this would have unforeseen effects, since
`org-capture-list' is already meant to be transient, and we’d only be
expanding its use from init-only to init-and-exit.

HTH,

-- 
Leo Vivier

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-capture-Update-plist-before-finalizing.patch --]
[-- Type: text/x-patch, Size: 882 bytes --]

From d14f15ab76d60c3f65837a6d14712dadabf324bf Mon Sep 17 00:00:00 2001
From: Leo Vivier <leo.vivier+dev@gmail.com>
Date: Sat, 25 Jul 2020 21:53:07 +0200
Subject: [PATCH] org-capture: Update plist before finalizing

* lisp/org-capture.el (org-capture-finalize): Update
`org-capture-plist' with local-value before finalizing.
---
 lisp/org-capture.el | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 2cc1ce394..223ed4124 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -728,6 +728,9 @@ captured item after finalizing."
 
   (run-hooks 'org-capture-prepare-finalize-hook)
 
+  ;; Update `org-capture-plist' with the local-value
+  (setq org-capture-plist org-capture-current-plist)
+
   ;; Did we start the clock in this capture buffer?
   (when (and org-capture-clock-was-started
 	     org-clock-marker
-- 
2.27.0


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

* Re: [PATCH] org-capture: Update plist before finalizing
  2020-07-25 22:50 [PATCH] org-capture: Update plist before finalizing Leo Vivier
@ 2020-09-02  4:50 ` Kyle Meyer
  2020-09-05  7:11   ` Leo Vivier
  0 siblings, 1 reply; 4+ messages in thread
From: Kyle Meyer @ 2020-09-02  4:50 UTC (permalink / raw)
  To: Leo Vivier; +Cc: emacs-orgmode

Leo Vivier writes:

> Hi there,
>
> I’m working on the parallelisation of `org-capture' for Org-roam, and
> I’ve run into a problem with the updating of `org-capture-plist'.
>
> ;;---------------------------------------------------------------------
> ;; DESCRIPTION
> ;;---------------------------------------------------------------------
> We use the global-variable `org-capture-plist' to populate the
> local-variable `org-capture-current-plist' on the init of the
> `org-capture' buffer.  However, we do not do the opposite (i.e. update
> the global-variable with the local-variable) on `org-capture-finalize'.
>
> This is fine for the majority of `org-capture-finalize', since we’re
> using the LOCAL arg of `org-capture-get' to read
> `org-capture-current-plist' instead of `org-capture-list', but this
> trick does not work for `org-capture-after-finalize', since the hook is
> run after the `org-capture-buffer' has been closed.
>
> This causes problem with `:kill-buffer t', and it limits what can be
> done with cleanup functions in `org-capture-after-finalize'.
[...]

> ;;---------------------------------------------------------------------
> ;; PATCH
> ;;---------------------------------------------------------------------
> I propose to update `org-capture-plist' early in `org-capture-finalize'.
> I don’t think this would have unforeseen effects, since
> `org-capture-list' is already meant to be transient, and we’d only be
> expanding its use from init-only to init-and-exit.

Thanks for the detailed write-up and the patch (and sorry for the slow
reply).  Based on a quick glance, I too think this would be safe to do.

> Subject: [PATCH] org-capture: Update plist before finalizing
>
> * lisp/org-capture.el (org-capture-finalize): Update
> `org-capture-plist' with local-value before finalizing.

It'd be good to at least point to the motivation/usecase for this change
here.  (Your description section above already does a nice job of that.)

> ---
>  lisp/org-capture.el | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/lisp/org-capture.el b/lisp/org-capture.el
> index 2cc1ce394..223ed4124 100644
> --- a/lisp/org-capture.el
> +++ b/lisp/org-capture.el
> @@ -728,6 +728,9 @@ captured item after finalizing."
>  
>    (run-hooks 'org-capture-prepare-finalize-hook)
>  
> +  ;; Update `org-capture-plist' with the local-value
> +  (setq org-capture-plist org-capture-current-plist)

Convention nit: please end your comment with a period.

Perhaps add a brief mention of `org-capture-after-finalize' (or some
other hint of why) here.


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

* Re: [PATCH] org-capture: Update plist before finalizing
  2020-09-02  4:50 ` Kyle Meyer
@ 2020-09-05  7:11   ` Leo Vivier
  2020-09-05  9:14     ` Bastien
  0 siblings, 1 reply; 4+ messages in thread
From: Leo Vivier @ 2020-09-05  7:11 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 809 bytes --]

Hi there,

Kyle Meyer <kyle@kyleam.com> writes:

> Thanks for the detailed write-up and the patch (and sorry for the slow
> reply).

No worries, and thanks for the review.

> It'd be good to at least point to the motivation/usecase for this change
> here.  (Your description section above already does a nice job of
> that.)

Done.  I’ve also added a link to this thread.

> Convention nit: please end your comment with a period.

Done.

> Perhaps add a brief mention of `org-capture-after-finalize' (or some
> other hint of why) here.

I’ve added some details to bridge the gap with the docstring for
`org-capture-current-plist'.

You’ll find the amended commit below.

Best,

-- 
Leo Vivier
Freelance Software Engineer
Website: www.leovivier.com | Blog: www.zaeph.net

[-- Attachment #2: 0001-org-capture-Update-plist-before-finalizing.patch --]
[-- Type: text/x-patch, Size: 1942 bytes --]

From ab47e50dae4029622d3e8378f816f77153c180d9 Mon Sep 17 00:00:00 2001
From: Leo Vivier <zaeph@zaeph.net>
Date: Sat, 25 Jul 2020 21:53:07 +0200
Subject: [PATCH] org-capture: Update plist before finalizing
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/org-capture.el (org-capture-finalize): Update
`org-capture-plist' with local-value before finalizing.

We use the global-variable `org-capture-plist' to populate the
local-variable `org-capture-current-plist' on the init of the
`org-capture' buffer.  However, we do not do the opposite (i.e. update
the global-variable with the local-variable) on
`org-capture-finalize'.

This is fine for the majority of `org-capture-finalize', since we’re
using the LOCAL arg of `org-capture-get' to read
`org-capture-current-plist' instead of `org-capture-list', but this
trick does not work for `org-capture-after-finalize', since the hook
is run after the `org-capture-buffer' has been closed.

This causes problem with `:kill-buffer t', and it limits what can be
done with cleanup functions in `org-capture-after-finalize'.

See <https://orgmode.org/list/87h7tv9pkm.fsf@hidden/> for details.
---
 lisp/org-capture.el | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 0ca75c772..b74978c82 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -735,6 +735,11 @@ captured item after finalizing."
 
   (run-hooks 'org-capture-prepare-finalize-hook)
 
+  ;; Update `org-capture-plist' with the buffer-local value.  Since
+  ;; captures can be run concurrently, this is to ensure that
+  ;; `org-capture-after-finalize-hook' accesses the proper plist.
+  (setq org-capture-plist org-capture-current-plist)
+
   ;; Did we start the clock in this capture buffer?
   (when (and org-capture-clock-was-started
 	     org-clock-marker
-- 
2.28.0


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

* Re: [PATCH] org-capture: Update plist before finalizing
  2020-09-05  7:11   ` Leo Vivier
@ 2020-09-05  9:14     ` Bastien
  0 siblings, 0 replies; 4+ messages in thread
From: Bastien @ 2020-09-05  9:14 UTC (permalink / raw)
  To: Leo Vivier; +Cc: emacs-orgmode

Hi Leo,

applied as 3008fb45d on maint, thanks a lot!

Best,

-- 
 Bastien


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

end of thread, other threads:[~2020-09-05  9:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-25 22:50 [PATCH] org-capture: Update plist before finalizing Leo Vivier
2020-09-02  4:50 ` Kyle Meyer
2020-09-05  7:11   ` Leo Vivier
2020-09-05  9:14     ` Bastien

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).