emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] lisp/org-table.el: Allow named columns on lhs
@ 2023-07-19  2:36 Gavin Downard
  2023-07-19  7:44 ` Ihor Radchenko
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Gavin Downard @ 2023-07-19  2:36 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 314 bytes --]

This patch does prioritize named columns over named fields, which can
break compatibility in tables with a named column and named field with
the same name. Alternatively, we could prioritize named fields to
preserve compatibility, but since named columns are prioritized on the
rhs, it could be pretty confusing.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-org-table.el-Allow-named-columns-on-lhs.patch --]
[-- Type: text/x-patch, Size: 3840 bytes --]

From c6ebbf02e0cb89839606338e8bbc4032810ea398 Mon Sep 17 00:00:00 2001
From: Gavin Downard <gavin.downard@runbox.com>
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 |   |
 <point>#+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 |
+<point>#+TBLFM: $lhs=$rhs"
       (org-table-calc-current-TBLFM)
       (buffer-string)))))
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2023-08-31 17:18 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-19  2:36 [PATCH] lisp/org-table.el: Allow named columns on lhs Gavin Downard
2023-07-19  7:44 ` Ihor Radchenko
2023-07-21 15:15   ` Max Nikulin
2023-07-21 18:33     ` Gavin Downard
2023-07-22  2:12       ` Max Nikulin
2023-07-22  7:25         ` Ihor Radchenko
2023-07-22 13:16           ` Max Nikulin
2023-07-22 13:25             ` Ihor Radchenko
2023-07-22 12:45 ` Max Nikulin
2023-07-22 18:21   ` Gavin Downard
2023-07-23  6:50     ` Ihor Radchenko
2023-07-24  3:25       ` Gavin Downard
2023-07-24  7:23         ` Ihor Radchenko
2023-07-24 15:17 ` Max Nikulin
2023-07-24 20:29   ` Gavin Downard
2023-07-25 15:01     ` Max Nikulin
2023-07-26 21:50       ` Gavin Downard
2023-07-27  7:36         ` Ihor Radchenko
2023-08-29  9:58 ` Ihor Radchenko
2023-08-31 14:19   ` Gavin Downard

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).