From d85ddfba0d07ceb303d915cd3240f26921204b57 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Thu, 26 Nov 2015 22:41:14 +0100 Subject: [PATCH] org-table: Fix c651e150cc8fb230fca99dfff27caedfddced8ff * lisp/org-table.el (org-table-get-stored-formulas): Correctly detect named fields in formulas' LHS. * testing/lisp/test-org-table.el (test-org-table/named-field): (test-org-table/named-column): New tests. Reported-by: "Stefan-W. Hahn" --- lisp/org-table.el | 17 +++++++++++++---- testing/lisp/test-org-table.el | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index 638a284cf..09f4473ef 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -2297,10 +2297,19 @@ column formula? " )) " *:: *")) eq-alist seen) (dolist (string strings (nreverse eq-alist)) - (when (string-match "\\`\\(@[-+I<>0-9.$@]+\\|@?[0-9]+\\|\ -\\$\\([a-zA-Z0-9]+\\|[<>]+\\)\\) *= *\\(.*[^ \t]\\)" string) - (let* ((lhs (match-string 1 string)) - (rhs (match-string 3 string))) + (when (string-match "\\`\\(@[-+I<>0-9.$@]+\\|\\$\\([_a-zA-Z0-9]+\\|\ +[<>]+\\)\\) *= *\\(.*[^ \t]\\)" + string) + (let ((lhs + (let ((m (match-string 1 string))) + (cond + ((not (match-end 2)) m) + ;; Is it a column reference? + ((org-string-match-p "\\`$\\([0-9]+\\|[<>]+\\)\\'" m) m) + ;; Since named columns are not possible in + ;; LHS, assume this is a named field. + (t (match-string 2 string))))) + (rhs (match-string 3 string))) (push (cons lhs rhs) eq-alist) (cond ((not (member lhs seen)) (push lhs seen)) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 95ac7f469..ff8978dc0 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -1787,6 +1787,38 @@ is t, then new columns should be added as needed" |---+---+---| #+TBLFM: @3$2..@3$>=vsum(@1..@2)"))) +(ert-deftest test-org-table/named-field () + "Test formula with a named field." + (should + (org-string-match-p + "| +| +1 +|" + (org-test-with-temp-text " +| | | +| ^ | name | +#+TBLFM: $name=1" + (org-table-calc-current-TBLFM) + (buffer-string)))) + (should + (org-string-match-p + "| +| +1 +|" + (org-test-with-temp-text " +| _ | name | +| | | +#+TBLFM: $name=1" + (org-table-calc-current-TBLFM) + (buffer-string))))) + +(ert-deftest test-org-table/named-column () + "Test formula with a named field." + (should + (org-string-match-p + "| +| +1 +| +1 +|" + (org-test-with-temp-text " +| ! | name | | +| | 1 | | +#+TBLFM: @2$3=$name" + (org-table-calc-current-TBLFM) + (buffer-string))))) (provide 'test-org-table)