From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Re: [PATCH] org-capture: Add a custom to control save target file or not. Date: Fri, 28 Sep 2018 17:17:39 -0400 Message-ID: References: <19dc1211.1e75.162b770bd5c.Coremail.tumashu@163.com> <87po34y2ya.fsf@nicolasgoaziou.fr> <77a63bcf.32cc.162bcc4a5d0.Coremail.tumashu@163.com> <877epap70f.fsf@nicolasgoaziou.fr> <875zyro5jz.fsf@gmail.com> <87sh1t5ots.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="00000000000084cd240576f4feca" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:56900) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g609X-0005BQ-Ft for emacs-orgmode@gnu.org; Fri, 28 Sep 2018 17:18:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g609V-0007tj-I8 for emacs-orgmode@gnu.org; Fri, 28 Sep 2018 17:18:23 -0400 Received: from mail-lf1-x143.google.com ([2a00:1450:4864:20::143]:33271) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g609T-0007t1-U9 for emacs-orgmode@gnu.org; Fri, 28 Sep 2018 17:18:20 -0400 Received: by mail-lf1-x143.google.com with SMTP id o21-v6so6036045lfe.0 for ; Fri, 28 Sep 2018 14:18:19 -0700 (PDT) In-Reply-To: <87sh1t5ots.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: tumashu , emacs-org list --00000000000084cd240576f4feca Content-Type: text/plain; charset="UTF-8" On Fri, Sep 28, 2018 at 4:50 PM Nicolas Goaziou wrote: > Hello, > > Kaushal Modi writes: > > > @Nicolas, tumashu: Would love to get your comments. > > I don't remember the initial report. > Sorry. Here's the original thread: https://lists.gnu.org/r/emacs-orgmode/2018-04/msg00204.html My email client shows the whole thread from then to now, so I forgot pasting that. > However, saving the capture buffer may be problematic if there was > unsaved modifications before the capture process. Saving silently the > file would also save unrelated changes. > That's a valid concern, though I see the risk of losing unsaved data as higher than that. Also, isn't it easy enough to add a `save-buffer' call in a hook? > The problem is that when org-capture-after-finalize-hook is run, the indirect capture buffer is already killed, and the "context" is back to the buffer where the M-x org-capture was initiated. So trying to do save-buffer in that hook simply saves the unrelated buffer. So another possible solution is: - Save the capture base buffer to a new key in the org-capture-plist. And have a org-capture-save-buffer function that save the buffer retrieved from that key. That fn can then be run in org-capture-after-finalize-hook. Since I last posted my workaround, I have updated that workaround to this: (defun modi/org-capture-get-base-buffer () "Stores base buffer of the Org Capture indirect buffer. The base buffer is stored in `:base-buffer' entry in `org-capture-plist'." (let ((base-buffer (buffer-base-buffer (current-buffer)))) (org-capture-put :base-buffer base-buffer))) (add-hook 'org-capture-before-finalize-hook #'modi/org-capture-get-base-buffer) (defun modi/org-capture-save-base-buffer () "Saves the base buffer of the Org Capture indirect buffer. The base buffer is retrieved from the `:base-buffer' entry in `org-capture-plist'." (when-let ((base-buffer (org-capture-get :base-buffer))) (with-current-buffer base-buffer (save-buffer)))) (add-hook 'org-capture-after-finalize-hook #'modi/org-capture-save-base-buffer) So the proposal is to do modify org-capture-finalize to set that :base-buffer key so that one doesn't need to customize the org-capture-before-finalize-hook hook. And then modi/org-capture-save-base-buffer is basically org-capture-save-buffer that I mentioned above. With these changes, a user would need to just do: (add-hook 'org-capture-after-finalize-hook #'org-capture-save-buffer) --00000000000084cd240576f4feca Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Sep 28, 2018 at 4:50 PM Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote= :
Hello,

Kaushal Modi <kaushal.modi@gmail.com> writes:

> @Nicolas, tumashu: Would love to get your comments.

I don't remember the initial report.

Sorry. Here's the original thread: https://lists.gnu.org/r/emacs-orgmode= /2018-04/msg00204.html

My email client shows t= he whole thread from then to now, so I forgot pasting that.
= =C2=A0
However, saving the capture buffer may be problematic if there was
unsaved modifications before the capture process. Saving silently the
file would also save unrelated changes.

That's a valid concern, though I see the risk of losing unsaved data a= s higher than that.

Also, isn't it easy enough to add a `save-buffer' call in a hook?

The problem is that when=20 org-capture-after-finalize-hook is run, the indirect capture buffer is alre= ady killed, and the "context" is back to the buffer where the M-x= org-capture was initiated. So trying to do save-buffer in that hook simply= saves the unrelated buffer.

So another possible solution is:

- Save the capture base b= uffer to a new key in the org-capture-plist. And have a org-capture-save-bu= ffer function that save the buffer retrieved from that key. That fn can the= n be run in org-capture-after-finalize-hook.

Since I last posted my workaround, I= have updated that workaround to this:

=
(defun modi/org-capture-get-base-buffer ()=
=C2=A0 "Stores base buffer of the Org Capture indirect buffer.
= =C2=A0The base buffer is stored in `:base-buffer' entry in
=C2=A0`or= g-capture-plist'."
=C2=A0 (let ((base-buffer (buffer-base-buffe= r (current-buffer))))
=C2=A0=C2=A0=C2=A0 (org-capture-put :base-buffer b= ase-buffer)))
(add-hook 'org-capture-before-finalize-hook #'modi= /org-capture-get-base-buffer)

(defun modi/org-capture-save-base-buff= er ()
=C2=A0 "Saves the base buffer of the Org Capture indirect buf= fer.
=C2=A0The base buffer is retrieved from the `:base-buffer' entr= y in
=C2=A0`org-capture-plist'."
=C2=A0 (when-let ((base-buf= fer (org-capture-get :base-buffer)))
=C2=A0=C2=A0=C2=A0 (with-current-bu= ffer base-buffer
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (save-buffer))))
(add= -hook 'org-capture-after-finalize-hook #'modi/org-capture-save-base= -buffer)

So the proposal is to do modify org-capture-finalize to set that :base-b= uffer key so that one doesn't need to customize the=20 org-capture-before-finalize-hook hook.
And = then=20 modi/org-capture-save-base-buffer is basically org-capture-save-buffer that= I mentioned above.

With these changes, a user would need to just do:

(add-hook 'org-capture-after-finalize-hook #= 'org-capture-save-buffer)

--00000000000084cd240576f4feca--