From: Jay Belanger Date: Sat, 19 May 2012 03:00:48 +0000 (-0500) Subject: * calc/calc.el (calc-ensure-consistent-units): New variable. X-Git-Tag: emacs-24.2.90~471^2~16 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d14b00297f7968ee861500dbf79f79ad8c77e494;p=emacs.git * 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'. --- diff --git a/doc/misc/ChangeLog b/doc/misc/ChangeLog index c7dd1d3fa82..420fde36b2f 100644 --- a/doc/misc/ChangeLog +++ b/doc/misc/ChangeLog @@ -1,3 +1,9 @@ +2012-05-19 Jay Belanger + + * doc/misc/calc.texi + (Basic Operations on Units, Customizing Calc): + Mention `calc-ensure-consistent-units'. + 2012-05-14 Andreas Schwab * cc-mode.texi: Avoid space before macro in 4th argument of cross diff --git a/doc/misc/calc.texi b/doc/misc/calc.texi index 48252ceb327..98b5c44abf8 100644 --- a/doc/misc/calc.texi +++ b/doc/misc/calc.texi @@ -27778,6 +27778,11 @@ acres per meter-second.) Remainder units are expressed in terms of ``fundamental'' units like @samp{m} and @samp{s}, regardless of the input units. +If you want to disallow using inconsistent units, you can set the customizable variable +@code{calc-ensure-consistent-units} to @code{t} (@pxref{Customizing Calc}). In this case, +if you request units which are inconsistent with the original units, you will be warned about +it and no conversion will occur. + One special exception is that if you specify a single unit name, and a compatible unit appears somewhere in the units expression, then that compatible unit will be converted to the new unit and the @@ -35591,6 +35596,19 @@ as @samp{a/(b*c)}. If @code{calc-multiplication-has-precedence} is of @code{calc-multiplication-has-precedence} is @code{t}. @end defvar +@defvar calc-ensure-consistent-units +When converting units, the variable @code{calc-ensure-consistent-units} +determines whether or not the target units need to be consistent with the +original units. If @code{calc-ensure-consistent-units} is @code{nil}, then +the target units don't need to have the same dimensions as the original units; +for example, converting @samp{100 ft/s} to @samp{m} will produce @samp{30.48 m/s}. +If @code{calc-ensure-consistent-units} is non-@code{nil}, then the target units +need to have the same dimensions as the original units; for example, converting +@samp{100 ft/s} to @samp{m} will result in an error, since @samp{ft/s} and @samp{m} +have different dimensions. The default value of @code{calc-ensure-consistent-units} +is @code{nil}. +@end defvar + @defvar calc-undo-length The variable @code{calc-undo-length} determines the number of undo steps that Calc will keep track of when @code{calc-quit} is called. diff --git a/lisp/ChangeLog b/lisp/ChangeLog index ed24a1eb176..91778032966 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,13 @@ +2012-05-19 Jay Belanger + + * 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. + 2012-05-18 Stefan Monnier * 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).") (unless (< pos (length units)) (error "Unit number %d not defined" pos)) (if (math-units-in-expr-p expr nil) - (calc-enter-result 1 (format "cun%d" num) - (math-convert-units expr (nth pos units))) + (progn + (if calc-ensure-consistent-units + (math-check-unit-consistency expr units)) + (calc-enter-result 1 (format "cun%d" num) + (math-convert-units expr (nth pos units)))) (calc-enter-result 1 (format "*un%d" num) (math-simplify-units (math-mul expr (nth pos units)))))))) @@ -477,6 +480,8 @@ If EXPR is nil, return nil." (setq units (math-read-expr new-units)) (when (eq (car-safe units) 'error) (error "Bad format in units expression: %s" (nth 2 units))) + (if calc-ensure-consistent-units + (math-check-unit-consistency expr units)) (math-put-default-units units) (let ((unew (math-units-in-expr-p units t)) (std (and (eq (car-safe units) 'var) @@ -560,7 +565,7 @@ If EXPR is nil, return nil." (defun calc-extract-units () (interactive) (calc-slow-wrapper - (calc-enter-result 1 "rmun" (math-simplify-units + (calc-enter-result 1 "exun" (math-simplify-units (math-extract-units (calc-top-n 1)))))) ;; The variables calc-num-units and calc-den-units are local to @@ -914,6 +919,17 @@ If EXPR is nil, return nil." (math-single-units-in-expr-p (nth 1 expr)))) (t 'wrong))) +(defun math-consistent-units-p (expr1 expr2) + "Non-nil if EXPR1 and EXPR2 have consistent units." + (math-numberp (math-get-units (list '/ expr1 expr2)))) + +(defun math-check-unit-consistency (expr units) + "Give an error if EXPR and UNITS do not have consistent units." + (unless (math-consistent-units-p expr units) + (error "New units (%s) are inconsistent with current units (%s)" + (math-format-value units) + (math-format-value (math-get-units expr))))) + (defun math-check-unit-name (v) (and (eq (car-safe v) 'var) (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." :group 'calc :type 'boolean) +(defcustom calc-ensure-consistent-units + nil + "If non-nil, make sure new units are consistent with current units +when converting units." + :group 'calc + :type 'boolean) + (defcustom calc-undo-length 100 "The number of undo steps that will be preserved when Calc is quit."