Re: [O] [PATCH] org-capture: cannot abort cleanly after inserting at end of buffer

2017-11-04 Thread Xavier Martinez-Hidalgo
On Fri, Nov 3, 2017 at 10:22 PM, Nicolas Goaziou <> wrote:

> Applied, with a slight twist. Thank you!

It does look much cleaner using 'copy-marker'. Thanks a lot for the review!

Xavier Martinez-Hidalgo

[O] [PATCH] org-capture: cannot abort cleanly after inserting at end of buffer

2017-11-02 Thread Xavier Martinez-Hidalgo

After the following steps:

- Start a capture.
- Move to end of capture buffer (M->).
- Type some text.
- Abort capture (C-c C-k).

the capture is aborted but its contents are not removed from the
target buffer and the following message is shown:

  "Capture process aborted, but target buffer could not be cleaned up

I am a attaching a patch that adds a test case showing up the problem
and includes a proposed fix (setting the capture kill end marker
insertion type).

I am not sure whether the fix might affect something else.  Does the
fix makes sense?
From 24130d37652acda6e3554d83b1506e93efc42a14 Mon Sep 17 00:00:00 2001
From: Xavier Martinez-Hidalgo <>
Date: Thu, 2 Nov 2017 11:47:19 +0100
Subject: [PATCH] org-capture: Fix aborting after inserting text at end of

* lisp/org-capture.el (org-capture-mark-kill-region): Set end marker
  insertion type so that it advances when inserting text at the end of
  the capture buffer.

* testing/lisp/test-org-capture.el (test-org-capture/insert-at-end-abort):
  Add test case.

Inserting text at the end of the capture buffer does not advance the
end kill marker.  This causes the narrowing region and the kill
markers to get out of sync, which prevents 'org-capture-finalize' from
correctly cleaning the target buffer when aborting the capture.

Setting the kill end marker insertion type fixes this.

 lisp/org-capture.el  |  1 +
 testing/lisp/test-org-capture.el | 13 +
 2 files changed, 14 insertions(+)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 25af674b8..59561d666 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1313,6 +1313,7 @@ Of course, if exact position has been required, just put it there."
   "Mark the region that will have to be killed when aborting capture."
   (let ((m1 (move-marker (make-marker) beg))
 	(m2 (move-marker (make-marker) end)))
+(set-marker-insertion-type m2 t)
 (org-capture-put :begin-marker m1)
 (org-capture-put :end-marker m2)))
diff --git a/testing/lisp/test-org-capture.el b/testing/lisp/test-org-capture.el
index 4d5dfb713..5e41f972b 100644
--- a/testing/lisp/test-org-capture.el
+++ b/testing/lisp/test-org-capture.el
@@ -146,5 +146,18 @@
 	 (list file1 file2 (buffer-file-name)
+(ert-deftest test-org-capture/insert-at-end-abort ()
+  "Test that capture can be aborted after inserting at end of capture buffer."
+  (org-test-with-temp-text-in-file "* A\n* B\n"
+  (let* ((file (buffer-file-name))
+	 (org-capture-templates
+	  `(("t" "Todo" entry (file+headline ,file "A") "** H1 %?"
+	(org-capture nil "t")
+	(goto-char (point-max))
+	(insert "Capture text")
+	(org-capture-kill))
+  (should (string= "* A\n* B\n" (buffer-string)
 (provide 'test-org-capture)
 ;;; test-org-capture.el ends here