diff options
| author | Jay Belanger | 2007-08-14 05:24:35 +0000 |
|---|---|---|
| committer | Jay Belanger | 2007-08-14 05:24:35 +0000 |
| commit | 5360ea16a48631a9c7e1265e82e935d96286bd74 (patch) | |
| tree | b332ce1e1a1bf27b919f10dbc5e237a52d2ba719 | |
| parent | cdf4e301b04110022b0b8cd8b0ea68c4b0beb710 (diff) | |
| download | emacs-5360ea16a48631a9c7e1265e82e935d96286bd74.tar.gz emacs-5360ea16a48631a9c7e1265e82e935d96286bd74.zip | |
(math-get-standard-units,math-get-units,math-make-unit-string)
(math-get-default-units,math-put-default-units): New functions.
(math-default-units-table): New variable.
(calc-convert-units, calc-convert-temperature): Add machinery to
supply default values.
| -rw-r--r-- | lisp/calc/calc-units.el | 101 |
1 files changed, 89 insertions, 12 deletions
diff --git a/lisp/calc/calc-units.el b/lisp/calc/calc-units.el index e823a57aef0..e225d2d0b09 100644 --- a/lisp/calc/calc-units.el +++ b/lisp/calc/calc-units.el | |||
| @@ -321,13 +321,65 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 321 | (math-simplify-units | 321 | (math-simplify-units |
| 322 | (math-mul expr (nth pos units)))))))) | 322 | (math-mul expr (nth pos units)))))))) |
| 323 | 323 | ||
| 324 | (defun math-get-standard-units (expr) | ||
| 325 | "Return the standard units in EXPR." | ||
| 326 | (math-simplify-units | ||
| 327 | (math-extract-units | ||
| 328 | (math-to-standard-units expr nil)))) | ||
| 329 | |||
| 330 | (defun math-get-units (expr) | ||
| 331 | "Return the units in EXPR." | ||
| 332 | (math-simplify-units | ||
| 333 | (math-extract-units expr))) | ||
| 334 | |||
| 335 | (defun math-make-unit-string (expr) | ||
| 336 | "Return EXPR in string form. | ||
| 337 | If EXPR is nil, return nil." | ||
| 338 | (if expr | ||
| 339 | (let ((cexpr (math-compose-expr expr 0))) | ||
| 340 | (if (stringp cexpr) | ||
| 341 | cexpr | ||
| 342 | (math-composition-to-string cexpr))))) | ||
| 343 | |||
| 344 | (defvar math-default-units-table | ||
| 345 | (make-hash-table :test 'equal) | ||
| 346 | "A table storing previously converted units.") | ||
| 347 | |||
| 348 | (defun math-get-default-units (expr) | ||
| 349 | "Get default units to use when converting the units in EXPR." | ||
| 350 | (let* ((units (math-get-units expr)) | ||
| 351 | (standard-units (math-get-standard-units expr)) | ||
| 352 | (default-units (gethash | ||
| 353 | standard-units | ||
| 354 | math-default-units-table))) | ||
| 355 | (if (equal units (car default-units)) | ||
| 356 | (math-make-unit-string (cadr default-units)) | ||
| 357 | (math-make-unit-string (car default-units))))) | ||
| 358 | |||
| 359 | (defun math-put-default-units (expr) | ||
| 360 | "Put the units in EXPR in the default units table." | ||
| 361 | (let* ((units (math-get-units expr)) | ||
| 362 | (standard-units (math-get-standard-units expr)) | ||
| 363 | (default-units (gethash | ||
| 364 | standard-units | ||
| 365 | math-default-units-table))) | ||
| 366 | (cond | ||
| 367 | ((not default-units) | ||
| 368 | (puthash standard-units (list units) math-default-units-table)) | ||
| 369 | ((not (equal units (car default-units))) | ||
| 370 | (puthash standard-units | ||
| 371 | (list units (car default-units)) | ||
| 372 | math-default-units-table))))) | ||
| 373 | |||
| 374 | |||
| 324 | (defun calc-convert-units (&optional old-units new-units) | 375 | (defun calc-convert-units (&optional old-units new-units) |
| 325 | (interactive) | 376 | (interactive) |
| 326 | (calc-slow-wrapper | 377 | (calc-slow-wrapper |
| 327 | (let ((expr (calc-top-n 1)) | 378 | (let ((expr (calc-top-n 1)) |
| 328 | (uoldname nil) | 379 | (uoldname nil) |
| 329 | unew | 380 | unew |
| 330 | units) | 381 | units |
| 382 | defunits) | ||
| 331 | (unless (math-units-in-expr-p expr t) | 383 | (unless (math-units-in-expr-p expr t) |
| 332 | (let ((uold (or old-units | 384 | (let ((uold (or old-units |
| 333 | (progn | 385 | (progn |
| @@ -343,16 +395,31 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 343 | (error "Bad format in units expression: %s" (nth 1 uold))) | 395 | (error "Bad format in units expression: %s" (nth 1 uold))) |
| 344 | (setq expr (math-mul expr uold)))) | 396 | (setq expr (math-mul expr uold)))) |
| 345 | (unless new-units | 397 | (unless new-units |
| 346 | (setq new-units (read-string (if uoldname | 398 | (setq defunits (math-get-default-units expr)) |
| 347 | (concat "Old units: " | 399 | (setq new-units |
| 348 | uoldname | 400 | (read-string (concat |
| 349 | ", new units: ") | 401 | (if uoldname |
| 350 | "New units: ")))) | 402 | (concat "Old units: " |
| 403 | uoldname | ||
| 404 | ", new units") | ||
| 405 | "New units") | ||
| 406 | (if defunits | ||
| 407 | (concat | ||
| 408 | " (default: " | ||
| 409 | defunits | ||
| 410 | "): ") | ||
| 411 | ": ")))) | ||
| 412 | |||
| 413 | (if (and | ||
| 414 | (string= new-units "") | ||
| 415 | defunits) | ||
| 416 | (setq new-units defunits))) | ||
| 351 | (when (string-match "\\` */" new-units) | 417 | (when (string-match "\\` */" new-units) |
| 352 | (setq new-units (concat "1" new-units))) | 418 | (setq new-units (concat "1" new-units))) |
| 353 | (setq units (math-read-expr new-units)) | 419 | (setq units (math-read-expr new-units)) |
| 354 | (when (eq (car-safe units) 'error) | 420 | (when (eq (car-safe units) 'error) |
| 355 | (error "Bad format in units expression: %s" (nth 2 units))) | 421 | (error "Bad format in units expression: %s" (nth 2 units))) |
| 422 | (math-put-default-units units) | ||
| 356 | (let ((unew (math-units-in-expr-p units t)) | 423 | (let ((unew (math-units-in-expr-p units t)) |
| 357 | (std (and (eq (car-safe units) 'var) | 424 | (std (and (eq (car-safe units) 'var) |
| 358 | (assq (nth 1 units) math-standard-units-systems)))) | 425 | (assq (nth 1 units) math-standard-units-systems)))) |
| @@ -381,7 +448,8 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 381 | (let ((expr (calc-top-n 1)) | 448 | (let ((expr (calc-top-n 1)) |
| 382 | (uold nil) | 449 | (uold nil) |
| 383 | (uoldname nil) | 450 | (uoldname nil) |
| 384 | unew) | 451 | unew |
| 452 | defunits) | ||
| 385 | (setq uold (or old-units | 453 | (setq uold (or old-units |
| 386 | (let ((units (math-single-units-in-expr-p expr))) | 454 | (let ((units (math-single-units-in-expr-p expr))) |
| 387 | (if units | 455 | (if units |
| @@ -398,15 +466,24 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 398 | (error "Bad format in units expression: %s" (nth 2 uold))) | 466 | (error "Bad format in units expression: %s" (nth 2 uold))) |
| 399 | (or (math-units-in-expr-p expr nil) | 467 | (or (math-units-in-expr-p expr nil) |
| 400 | (setq expr (math-mul expr uold))) | 468 | (setq expr (math-mul expr uold))) |
| 469 | (setq defunits (math-get-default-units expr)) | ||
| 401 | (setq unew (or new-units | 470 | (setq unew (or new-units |
| 402 | (math-read-expr | 471 | (math-read-expr |
| 403 | (read-string (if uoldname | 472 | (read-string |
| 404 | (concat "Old temperature units: " | 473 | (concat |
| 405 | uoldname | 474 | (if uoldname |
| 406 | ", new units: ") | 475 | (concat "Old temperature units: " |
| 407 | "New temperature units: "))))) | 476 | uoldname |
| 477 | ", new units") | ||
| 478 | "New temperature units") | ||
| 479 | (if defunits | ||
| 480 | (concat " (default: " | ||
| 481 | defunits | ||
| 482 | "): ") | ||
| 483 | ": ")))))) | ||
| 408 | (when (eq (car-safe unew) 'error) | 484 | (when (eq (car-safe unew) 'error) |
| 409 | (error "Bad format in units expression: %s" (nth 2 unew))) | 485 | (error "Bad format in units expression: %s" (nth 2 unew))) |
| 486 | (math-put-default-units unew) | ||
| 410 | (calc-enter-result 1 "cvtm" (math-simplify-units | 487 | (calc-enter-result 1 "cvtm" (math-simplify-units |
| 411 | (math-convert-temperature expr uold unew | 488 | (math-convert-temperature expr uold unew |
| 412 | uoldname)))))) | 489 | uoldname)))))) |