From 2842d6f0a10ee83d8d500cd3e4c3827a32418eef Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Tue, 24 Nov 2020 00:49:16 +0100 Subject: [PATCH 3/3] org-table: Add mode flag to enable Calc units simplification mode * org-table.el (org-table-eval-formula): Add the `u` mode flag to enable Calc's units simplification mode. * test-org-table.el (test-org-table/mode-string-u): Add Unit test for the new mode flag. * org-manual.org: Document new mode flag. --- doc/org-manual.org | 8 ++++++++ etc/ORG-NEWS | 5 +++++ lisp/org-table.el | 5 +++-- testing/lisp/test-org-table.el | 12 ++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index be69996d5..d9de5e633 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -2075,6 +2075,14 @@ variable ~org-calc-default-modes~. Fraction and symbolic modes of Calc. +- =u= :: + + Units simplification mode of Calc. Calc is also a symbolic + calculator and is capable of working with values having an unit, + represented with numerals followed by an unit string in Org table + cells. This mode instructs Calc to simplify the units in the + computed expression before returning the result. + - =T=, =t=, =U= :: Duration computations in Calc or Lisp, [[*Durations and time values]]. diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 889eb4aab..0d08a939c 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -112,6 +112,11 @@ package, to convert pandas Dataframes into orgmode tables: | 2 | 3 | 6 | #+end_src +*** New =u= table formula flag to enable Calc units simplification mode + +A new =u= mode flag for Calc formulas in Org tables has been added to +enable Calc units simplification mode. + ** Miscellaneous *** =org-goto-first-child= now works before first heading diff --git a/lisp/org-table.el b/lisp/org-table.el index cf1bfa31c..c138190ef 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -2442,7 +2442,7 @@ location of point." (?e (setf (cl-getf calc-modes 'calc-float-format) (list 'eng n))))) ;; Remove matched flags from the mode string. (setq fmt (replace-match "" t t fmt))) - (while (string-match "\\([tTUNLEDRFS]\\)" fmt) + (while (string-match "\\([tTUNLEDRFSu]\\)" fmt) (let ((c (string-to-char (match-string 1 fmt)))) (cl-case c (?t (setq duration t numbers t @@ -2455,7 +2455,8 @@ location of point." (?D (setf (cl-getf calc-modes 'calc-angle-mode) 'deg)) (?R (setf (cl-getf calc-modes 'calc-angle-mode) 'rad)) (?F (setf (cl-getf calc-modes 'calc-prefer-frac) t)) - (?S (setf (cl-getf calc-modes 'calc-symbolic-mode) t)))) + (?S (setf (cl-getf calc-modes 'calc-symbolic-mode) t)) + (?u (setf (cl-getf calc-modes 'calc-simplify-mode) 'units)))) ;; Remove matched flags from the mode string. (setq fmt (replace-match "" t t fmt))) (unless (string-match "\\S-" fmt) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index fb9d83f95..1c930c8d0 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -380,6 +380,18 @@ reference (with row). Mode string N." " 1 calc))) +(ert-deftest test-org-table/mode-string-u () + "Basic: verify that mode string u results in units +simplification mode applied to Calc formulas." + (org-test-table-target-expect + " +| 1.5 A/B | 2.0 B | | +" + " +| 1.5 A/B | 2.0 B | 3. A | +" + 1 "#+TBLFM: $3=$1*$2;u")) + (ert-deftest test-org-table/lisp-return-value () "Basic: Return value of Lisp formulas." (org-test-table-target-expect -- 2.29.2