From b34bdc871950f9780615ab4c64f149a0f5983d22 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sun, 22 Jan 2023 13:49:07 +0300 Subject: [PATCH] org-capture-finalize: Do not save new file with :no-save aborted capture * lisp/org-capture.el (org-capture-finalize): Do not save the newly created file buffer when the capture is aborted and org-capture is not asked to save upon capture. * testing/lisp/test-org-capture.el (test-org-capture/abort): Add test. --- lisp/org-capture.el | 14 ++++++++++---- testing/lisp/test-org-capture.el | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lisp/org-capture.el b/lisp/org-capture.el index a696c615b..b96e9f336 100644 --- a/lisp/org-capture.el +++ b/lisp/org-capture.el @@ -889,10 +889,16 @@ captured item after finalizing." (goto-char (+ size pos)) (goto-char (if (< ipt pos) (+ size pos) pos)))))) - ;; Kill the target buffer if that is desired - (when (and base-buffer new-buffer kill-buffer) - (with-current-buffer base-buffer (save-buffer)) - (kill-buffer base-buffer)) + (if (and base-buffer org-note-abort new-buffer) + ;; Unconditionally kill the new buffer when capture is + ;; aborted. + (with-current-buffer base-buffer + (set-buffer-modified-p nil) + (kill-buffer)) + ;; Kill the target buffer if that is desired + (when (and base-buffer new-buffer kill-buffer) + (with-current-buffer base-buffer (save-buffer)) + (kill-buffer base-buffer))) ;; Restore the window configuration before capture (set-window-configuration return-wconf)) diff --git a/testing/lisp/test-org-capture.el b/testing/lisp/test-org-capture.el index 44702c3ce..0ed44c6af 100644 --- a/testing/lisp/test-org-capture.el +++ b/testing/lisp/test-org-capture.el @@ -152,6 +152,18 @@ (ert-deftest test-org-capture/abort () "Test aborting a capture process." + ;; Newly create capture buffer should not be saved. + (let ((capture-file (make-temp-name + (org-file-name-concat + temporary-file-directory + "org-test")))) + (unwind-protect + (let ((org-capture-templates `(("t" "Todo" entry (file ,capture-file) nil :no-save t)))) + (org-capture nil "t") + (org-capture-kill) + (should-not (file-exists-p capture-file))) + (when (file-exists-p capture-file) + (delete-file capture-file)))) ;; Test that capture can be aborted after inserting at end of ;; capture buffer. (should