summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Nicolodi <daniele@grinta.net>2020-11-24 00:49:16 +0100
committerKyle Meyer <kyle@kyleam.com>2020-11-24 20:59:39 -0500
commit15469774dd2063b79df66e61441008396a95a491 (patch)
tree1bcfcc9b23195b668edec0c9eef88484ee258e48
parentabd994943170c2f1bc2f71d8b99c48588499e4e2 (diff)
downloadorg-mode-15469774dd2063b79df66e61441008396a95a491.tar.gz
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.
-rw-r--r--doc/org-manual.org8
-rw-r--r--etc/ORG-NEWS5
-rw-r--r--lisp/org-table.el5
-rw-r--r--testing/lisp/test-org-table.el12
4 files changed, 28 insertions, 2 deletions
diff --git a/doc/org-manual.org b/doc/org-manual.org
index be69996..2f7f5f8 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 a unit,
+ represented with numerals followed by a 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 889eb4a..0d08a93 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 cf1bfa3..c138190 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 fb9d83f..1c930c8 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