Browse Source

org-capture: Small fix to table line capture

* lisp/org-capture.el (org-capture-place-table-line): Handle abort
  table line insertion more gracefully.
* testing/lisp/test-org-capture.el (test-org-capture/abort): Add tests
Nicolas Goaziou 6 years ago
parent
commit
ad0fce50eb
2 changed files with 22 additions and 12 deletions
  1. 13 12
      lisp/org-capture.el
  2. 9 0
      testing/lisp/test-org-capture.el

+ 13 - 12
lisp/org-capture.el

@@ -1271,18 +1271,19 @@ may have been stored before."
      (t
       (goto-char (org-table-end))))
     ;; Insert text and position point according to template.
-    (unless (bolp) (insert "\n"))
-    (let ((beg (point))
-	  (end (save-excursion
-		 (insert text)
-		 (point))))
-      (org-capture-position-for-last-stored 'table-line)
-      (org-capture-mark-kill-region beg end)
-      (org-capture-narrow beg end)
-      (when (or (re-search-backward "%\\?" beg t)
-		(re-search-forward "%\\?" end t))
-	(replace-match "")))
-    (org-table-align)))
+    (let ((origin (point)))
+      (unless (bolp) (insert "\n"))
+      (let ((beg (point))
+	    (end (save-excursion
+		   (insert text)
+		   (point))))
+	(org-capture-position-for-last-stored 'table-line)
+	(org-capture-mark-kill-region origin end)
+	(org-capture-narrow beg end)
+	(when (or (search-backward "%?" beg t)
+		  (search-forward "%?" end t))
+	  (replace-match "")))
+      (org-table-align))))
 
 (defun org-capture-place-plain-text ()
   "Place the template plainly.

+ 9 - 0
testing/lisp/test-org-capture.el

@@ -161,6 +161,15 @@
 	(insert "Capture text")
 	(org-capture-kill))
       (buffer-string))))
+  (should
+   (equal "| a |\n| b |"
+	  (org-test-with-temp-text-in-file "| a |\n| b |"
+	    (let* ((file (buffer-file-name))
+		   (org-capture-templates
+		    `(("t" "Table" table-line (file ,file) "| x |"))))
+	      (org-capture nil "t")
+	      (org-capture-kill))
+	    (buffer-string))))
   ;; Test aborting a capture that split the line.
   (should
    (equal