From c6ebbf02e0cb89839606338e8bbc4032810ea398 Mon Sep 17 00:00:00 2001 From: Gavin Downard Date: Sat, 1 Jul 2023 13:26:46 -0700 Subject: [PATCH] lisp/org-table.el: Allow named columns on lhs * lisp/org-table.el (org-table-recalculate): Add support for named columns on the lhs of spreadsheet formulas, prioritizing named columns over named fields if there is a conflict. (org-table-edit-formulas): Modify category name to include column formulas with field formulas. (org-table-get-stored-formulas): Remove comment mentioning lack of named columns in the lhs. * testing/lisp/test-org-table.el (test-org-table/named-column): Add test case for named columns. * etc/ORG-NEWS (Spreadsheets now support named columns on the lhs): Document the change This change breaks compatibility in tables with a named field and named column with the same name, when that name is used on the lhs of a formula. --- etc/ORG-NEWS | 4 ++++ lisp/org-table.el | 9 +++++---- testing/lisp/test-org-table.el | 9 +++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index a4725ae8c..42f39fd45 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -534,6 +534,10 @@ special repeaters ~++~ and ~.+~ are skipped. A capture template can target ~(here)~ which is the equivalent of invoking a capture template with a zero prefix. +*** Spreadsheets now support named columns on the lhs + +Spreadsheet formulas can now use named column references on the lhs. + ** New functions and changes in function arguments *** =TYPES= argument in ~org-element-lineage~ can now be a symbol diff --git a/lisp/org-table.el b/lisp/org-table.el index c5efe8f0c..34b0a562e 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -2253,8 +2253,7 @@ LOCATION is a buffer position, consider the formulas there." ((not (match-end 2)) m) ;; Is it a column reference? ((string-match-p "\\`\\$\\([0-9]+\\|[<>]+\\)\\'" m) m) - ;; Since named columns are not possible in - ;; LHS, assume this is a named field. + ;; This is either a named field or column. (t (match-string 2 string))))) (rhs (match-string 3 string))) (push (cons lhs rhs) eq-alist) @@ -2963,7 +2962,9 @@ existing formula for column %s" (t old-lhs))))) (if (string-match-p "\\`\\$[0-9]+\\'" lhs) (push (cons lhs rhs) eqlcol) - (push (cons lhs rhs) eqlfield)))) + (if-let ((named-column (assoc lhs org-table-column-names))) + (push (cons (concat "$" (cdr named-column)) rhs) eqlcol) + (push (cons lhs rhs) eqlfield))))) (setq eqlcol (nreverse eqlcol)) ;; Expand ranges in lhs of formulas (setq eqlfield (org-table-expand-lhs-ranges (nreverse eqlfield))) @@ -3355,7 +3356,7 @@ Parameters get priority." (sel-win (selected-window)) (titles '((column . "# Column Formulas\n") (field . "# Field and Range Formulas\n") - (named . "# Named Field Formulas\n")))) + (named . "# Named Field and Named Column Formulas\n")))) (org-switch-to-buffer-other-window "*Edit Formulas*") (erase-buffer) ;; Keep global-font-lock-mode from turning on font-lock-mode diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 27aeb5ab3..8cd01049a 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -2158,6 +2158,15 @@ See also `test-org-table/copy-field'." | ! | name | | | | 1 | | #+TBLFM: @2$3=$name" + (org-table-calc-current-TBLFM) + (buffer-string)))) + (should + (string-match-p + "| +# +| +1 +| +1 +|" + (org-test-with-temp-text " +| ! | lhs | rhs | +| # | | 1 | +#+TBLFM: $lhs=$rhs" (org-table-calc-current-TBLFM) (buffer-string))))) -- 2.40.1