From 57029084b54736180c7ef98705a0c67ed780d38f Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 11 Nov 2015 00:37:57 +0100 Subject: [PATCH] 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. --- lisp/org-table.el | 33 ++++++++++++++++++--------------- testing/lisp/test-org-table.el | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index b953b0d66..df06e58b6 100644 --- a/lisp/org-table.el +++ b/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. diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 154c2a678..95ac7f469 100644 --- a/testing/lisp/test-org-table.el +++ b/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 | +| | | | +|---+---+---| +#+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