Browse Source

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.
Daniele Nicolodi 1 month ago
parent
commit
15469774dd
4 changed files with 28 additions and 2 deletions
  1. 8 0
      doc/org-manual.org
  2. 5 0
      etc/ORG-NEWS
  3. 3 2
      lisp/org-table.el
  4. 12 0
      testing/lisp/test-org-table.el

+ 8 - 0
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]].

+ 5 - 0
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
 

+ 3 - 2
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)

+ 12 - 0
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