浏览代码

org-table: Fix bug with "$>" reference

* lisp/org-table.el (org-table-analyze): Fix incorrect replacement for
  "$>" reference when the table ends on a hline.
* testing/lisp/test-org-table.el (test-org-table/end-on-hline): New
  test.
Nicolas Goaziou 9 年之前
父节点
当前提交
57029084b5
共有 2 个文件被更改,包括 39 次插入15 次删除
  1. 18 15
      lisp/org-table.el
  2. 21 0
      testing/lisp/test-org-table.el

+ 18 - 15
lisp/org-table.el

@@ -2534,24 +2534,27 @@ This function sets up the following dynamically scoped variables:
 	(push 'hline types) ; Add an imaginary extra hline to the end.
 	(setq org-table-current-line-types (apply #'vector (nreverse types)))
 	(setq org-table-dlines (apply #'vector (cons nil (nreverse dlines))))
-	(setq org-table-hlines (apply #'vector (cons nil (nreverse hlines))))
-	(forward-line -1)
-	(let* ((last-dline (car dlines))
-	       (fields (org-split-string
-			(buffer-substring (line-beginning-position)
-					  (line-end-position))
-			"[ \t]*|[ \t]*"))
-	       (nfields (length fields))
-	       al al2)
-	  (setq org-table-current-ncol nfields)
+	(setq org-table-hlines (apply #'vector (cons nil (nreverse hlines)))))
+      ;; Get the number of columns from the first data line in table.
+      (goto-char beg)
+      (forward-line (aref org-table-dlines 0))
+      (let* ((fields
+	      (org-split-string
+	       (buffer-substring (line-beginning-position) (line-end-position))
+	       "[ \t]*|[ \t]*"))
+	     (nfields (length fields))
+	     al al2)
+	(setq org-table-current-ncol nfields)
+	(let ((last-dline
+	       (aref org-table-dlines (1- (length org-table-dlines)))))
 	  (dotimes (i nfields)
 	    (let ((column (1+ i)))
 	      (push (list (format "LR%d" column) last-dline column) al)
-	      (push (cons (format "LR%d" column) (nth i fields)) al2)))
-	  (setq org-table-named-field-locations
-		(append org-table-named-field-locations al))
-	  (setq org-table-local-parameters
-		(append org-table-local-parameters al2)))))))
+	      (push (cons (format "LR%d" column) (nth i fields)) al2))))
+	(setq org-table-named-field-locations
+	      (append org-table-named-field-locations al))
+	(setq org-table-local-parameters
+	      (append org-table-local-parameters al2))))))
 
 (defun org-table-goto-field (ref &optional create-column-p)
   "Move point to a specific field in the current table.

+ 21 - 0
testing/lisp/test-org-table.el

@@ -1767,6 +1767,27 @@ is t, then new columns should be added as needed"
 		   (org-table-calc-current-TBLFM)
 		   (buffer-string)))))
 
+(ert-deftest test-org-table/end-on-hline ()
+  "Test with a table ending on a hline."
+  (should
+   (equal
+    (org-test-with-temp-text
+	"
+| 1 | 2 | 3 |
+| 4 | 5 | 6 |
+|   |   |   |
+|---+---+---|
+<point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
+      (org-table-calc-current-TBLFM)
+      (buffer-string))
+    "
+| 1 | 2 | 3 |
+| 4 | 5 | 6 |
+|   | 7 | 9 |
+|---+---+---|
+#+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
+
+
 (provide 'test-org-table)
 
 ;;; test-org-table.el ends here