summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2017-02-19 15:27:09 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-02-19 15:37:12 +0100
commit7d7c38c6b1255a3a1ddd03f32849232bf86200e3 (patch)
treee78d077dd0f87d4efb013fafe10ade27829c9f10
parentc5ab7958b7074ae8da734e9e6180666cca4c2c62 (diff)
downloadorg-mode-7d7c38c6b1255a3a1ddd03f32849232bf86200e3.tar.gz
org-capture: Fix `org-capture-refile'
* lisp/org-capture.el (org-capture-refile): Preserve location of point when refiling. * testing/lisp/test-org-capture.el (test-org-capture/refile): New test. Reported-by: Liu Hui <liuhui1610@gmail.com> <http://permalink.gmane.org/gmane.emacs.orgmode/112202>
-rw-r--r--lisp/org-capture.el34
-rw-r--r--testing/lisp/test-org-capture.el36
2 files changed, 49 insertions, 21 deletions
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index ae5ee50..71aff0d 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -820,20 +820,28 @@ Refiling is done from the base buffer, because the indirect buffer is then
already gone. Any prefix argument will be passed to the refile command."
(interactive)
(unless (eq (org-capture-get :type 'local) 'entry)
- (error
- "Refiling from a capture buffer makes only sense for `entry'-type templates"))
- (let ((pos (point))
- (base (buffer-base-buffer (current-buffer)))
- (org-capture-is-refiling t)
- (kill-buffer (org-capture-get :kill-buffer 'local)))
+ (user-error "Refiling from a capture buffer makes only sense \
+for `entry'-type templates"))
+ (let* ((base (or (buffer-base-buffer) (current-buffer)))
+ (pos (make-marker))
+ (org-capture-is-refiling t)
+ (kill-buffer (org-capture-get :kill-buffer 'local)))
+ ;; Since `org-capture-finalize' may alter buffer contents (e.g.,
+ ;; empty lines) around entry, use a marker to refer to the
+ ;; headline to be refiled. Place the marker in the base buffer,
+ ;; as the current indirect one is going to be killed.
+ (set-marker pos (save-excursion (org-back-to-heading t)) base)
(org-capture-put :kill-buffer nil)
- (org-capture-finalize)
- (save-window-excursion
- (with-current-buffer (or base (current-buffer))
- (org-with-wide-buffer
- (goto-char pos)
- (call-interactively 'org-refile))))
- (when kill-buffer (kill-buffer base))))
+ (unwind-protect
+ (progn
+ (org-capture-finalize)
+ (save-window-excursion
+ (with-current-buffer base
+ (org-with-wide-buffer
+ (goto-char pos)
+ (call-interactively 'org-refile))))
+ (when kill-buffer (kill-buffer base)))
+ (set-marker pos nil))))
(defun org-capture-kill ()
"Abort the current capture process."
diff --git a/testing/lisp/test-org-capture.el b/testing/lisp/test-org-capture.el
index 6b86410..3f477b2 100644
--- a/testing/lisp/test-org-capture.el
+++ b/testing/lisp/test-org-capture.el
@@ -87,14 +87,34 @@
;; %(sexp) placeholder with an input containing the traps %, " and )
;; all at once which is complicated to parse.
(should
- (equal
- "5 % Less (See Item \"3)\" Somewhere)\n"
- (let ((org-store-link-plist nil))
- (org-capture-fill-template
- "%(capitalize \"%i\")"
- "5 % less (see item \"3)\" somewhere)")))))
-
-
+ (equal "5 % Less (See Item \"3)\" Somewhere)\n"
+ (let ((org-store-link-plist nil))
+ (org-capture-fill-template
+ "%(capitalize \"%i\")"
+ "5 % less (see item \"3)\" somewhere)")))))
+
+(ert-deftest test-org-capture/refile ()
+ "Test `org-capture-refile' specifications."
+ ;; When refiling, make sure the headline being refiled is the one
+ ;; being captured. In particular, empty lines after the entry may
+ ;; be removed, and we don't want to shift onto the next heading.
+ (should
+ (string-prefix-p
+ "** H1"
+ (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")
+ (insert "\n")
+ (cl-letf (((symbol-function 'org-refile)
+ (lambda ()
+ (interactive)
+ (throw :return
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position))))))
+ (catch :return (org-capture-refile))))))))
(provide 'test-org-capture)