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.
This commit is contained in:
Ihor Radchenko 2023-01-22 13:49:07 +03:00
parent 005c9ae747
commit b34bdc8719
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 22 additions and 4 deletions

View File

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

View File

@ -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