Browse Source

org-capture: Fix row insertion in a table with a formula

* lisp/org-capture.el (org-capture-place-table-line): Insert new row
  above the formula, not below.
* testing/lisp/test-org-capture.el (test-org-capture/table-line): Add
  test.
Nicolas Goaziou 1 year ago
parent
commit
547b431e70
2 changed files with 14 additions and 3 deletions
  1. 4 3
      lisp/org-capture.el
  2. 10 0
      testing/lisp/test-org-capture.el

+ 4 - 3
lisp/org-capture.el

@@ -1220,10 +1220,11 @@ may have been stored before."
       (while (re-search-forward org-table-dataline-regexp end t)
 	(pcase (org-element-lineage (org-element-at-point) '(table) t)
 	  (`nil nil)
+	  ((and table
+		(guard (eq 'table.el (org-element-property :type table))))
+	   nil)
 	  (table
-	   (goto-char (org-element-property :end table))
-	   (skip-chars-backward " \r\t\n")
-	   (forward-line)
+	   (goto-char (org-element-property :contents-end table))
 	   (narrow-to-region (org-element-property :post-affiliated table)
 			     (point))
 	   (throw :found t))))

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

@@ -233,6 +233,16 @@
 		       :immediate-finish t))))
 	      (org-capture nil "t"))
 	    (buffer-string))))
+  ;; Properly insert row with formulas.
+  (should
+   (equal "| 1 |\n| 2 |\n#+TBLFM: "
+	  (org-test-with-temp-text-in-file "| 1 |\n#+TBLFM: "
+	    (let* ((file (buffer-file-name))
+		   (org-capture-templates
+		    `(("t" "Table" table-line (file ,file)
+		       "| 2 |" :immediate-finish t))))
+	      (org-capture nil "t"))
+	    (buffer-string))))
   ;; When `:prepend' is nil, add the row at the end of the table.
   (should
    (equal "| a |\n| x |\n"