aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorJay Belanger2012-05-18 22:00:48 -0500
committerJay Belanger2012-05-18 22:00:48 -0500
commitd14b00297f7968ee861500dbf79f79ad8c77e494 (patch)
treedbe2d8374682a9619db07e238c5511af44bfa928 /lisp
parent6d1a623ec4125ba74f482c4610abe696abd97c5e (diff)
downloademacs-d14b00297f7968ee861500dbf79f79ad8c77e494.tar.gz
emacs-d14b00297f7968ee861500dbf79f79ad8c77e494.zip
* calc/calc.el (calc-ensure-consistent-units): New variable.
* calc/calc-units.el (math-consistent-units-p, math-check-unit-consistency): New functions. (calc-quick-units, calc-convert-units): Use `math-check-unit-consistency' when `calc-ensure-consistent-units' is non-nil. (calc-extract-units): Fix typo. * doc/misc/calc.texi (Basic Operations on Units, Customizing Calc): Mention `calc-ensure-consistent-units'.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/ChangeLog10
-rw-r--r--lisp/calc/calc-units.el22
-rw-r--r--lisp/calc/calc.el7
3 files changed, 36 insertions, 3 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index ed24a1eb176..91778032966 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,13 @@
12012-05-19 Jay Belanger <jay.p.belanger@gmail.com>
2
3 * calc/calc.el (calc-ensure-consistent-units): New variable.
4
5 * calc/calc-units.el (math-consistent-units-p, math-check-unit-consistency):
6 New functions.
7 (calc-quick-units, calc-convert-units): Use `math-check-unit-consistency' when
8 `calc-ensure-consistent-units' is non-nil.
9 (calc-extract-units): Fix typo.
10
12012-05-18 Stefan Monnier <monnier@iro.umontreal.ca> 112012-05-18 Stefan Monnier <monnier@iro.umontreal.ca>
2 12
3 * vc/vc-bzr.el (vc-bzr-state-heuristic): Save match-data around sha1. 13 * vc/vc-bzr.el (vc-bzr-state-heuristic): Save match-data around sha1.
diff --git a/lisp/calc/calc-units.el b/lisp/calc/calc-units.el
index 9d2583085a2..f4100f38831 100644
--- a/lisp/calc/calc-units.el
+++ b/lisp/calc/calc-units.el
@@ -370,8 +370,11 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
370 (unless (< pos (length units)) 370 (unless (< pos (length units))
371 (error "Unit number %d not defined" pos)) 371 (error "Unit number %d not defined" pos))
372 (if (math-units-in-expr-p expr nil) 372 (if (math-units-in-expr-p expr nil)
373 (calc-enter-result 1 (format "cun%d" num) 373 (progn
374 (math-convert-units expr (nth pos units))) 374 (if calc-ensure-consistent-units
375 (math-check-unit-consistency expr units))
376 (calc-enter-result 1 (format "cun%d" num)
377 (math-convert-units expr (nth pos units))))
375 (calc-enter-result 1 (format "*un%d" num) 378 (calc-enter-result 1 (format "*un%d" num)
376 (math-simplify-units 379 (math-simplify-units
377 (math-mul expr (nth pos units)))))))) 380 (math-mul expr (nth pos units))))))))
@@ -477,6 +480,8 @@ If EXPR is nil, return nil."
477 (setq units (math-read-expr new-units)) 480 (setq units (math-read-expr new-units))
478 (when (eq (car-safe units) 'error) 481 (when (eq (car-safe units) 'error)
479 (error "Bad format in units expression: %s" (nth 2 units))) 482 (error "Bad format in units expression: %s" (nth 2 units)))
483 (if calc-ensure-consistent-units
484 (math-check-unit-consistency expr units))
480 (math-put-default-units units) 485 (math-put-default-units units)
481 (let ((unew (math-units-in-expr-p units t)) 486 (let ((unew (math-units-in-expr-p units t))
482 (std (and (eq (car-safe units) 'var) 487 (std (and (eq (car-safe units) 'var)
@@ -560,7 +565,7 @@ If EXPR is nil, return nil."
560(defun calc-extract-units () 565(defun calc-extract-units ()
561 (interactive) 566 (interactive)
562 (calc-slow-wrapper 567 (calc-slow-wrapper
563 (calc-enter-result 1 "rmun" (math-simplify-units 568 (calc-enter-result 1 "exun" (math-simplify-units
564 (math-extract-units (calc-top-n 1)))))) 569 (math-extract-units (calc-top-n 1))))))
565 570
566;; The variables calc-num-units and calc-den-units are local to 571;; The variables calc-num-units and calc-den-units are local to
@@ -914,6 +919,17 @@ If EXPR is nil, return nil."
914 (math-single-units-in-expr-p (nth 1 expr)))) 919 (math-single-units-in-expr-p (nth 1 expr))))
915 (t 'wrong))) 920 (t 'wrong)))
916 921
922(defun math-consistent-units-p (expr1 expr2)
923 "Non-nil if EXPR1 and EXPR2 have consistent units."
924 (math-numberp (math-get-units (list '/ expr1 expr2))))
925
926(defun math-check-unit-consistency (expr units)
927 "Give an error if EXPR and UNITS do not have consistent units."
928 (unless (math-consistent-units-p expr units)
929 (error "New units (%s) are inconsistent with current units (%s)"
930 (math-format-value units)
931 (math-format-value (math-get-units expr)))))
932
917(defun math-check-unit-name (v) 933(defun math-check-unit-name (v)
918 (and (eq (car-safe v) 'var) 934 (and (eq (car-safe v) 'var)
919 (or (assq (nth 1 v) (or math-units-table (math-build-units-table))) 935 (or (assq (nth 1 v) (or math-units-table (math-build-units-table)))
diff --git a/lisp/calc/calc.el b/lisp/calc/calc.el
index c7cea5f0aef..4d64209dd36 100644
--- a/lisp/calc/calc.el
+++ b/lisp/calc/calc.el
@@ -418,6 +418,13 @@ in normal mode."
418 :group 'calc 418 :group 'calc
419 :type 'boolean) 419 :type 'boolean)
420 420
421(defcustom calc-ensure-consistent-units
422 nil
423 "If non-nil, make sure new units are consistent with current units
424when converting units."
425 :group 'calc
426 :type 'boolean)
427
421(defcustom calc-undo-length 428(defcustom calc-undo-length
422 100 429 100
423 "The number of undo steps that will be preserved when Calc is quit." 430 "The number of undo steps that will be preserved when Calc is quit."