diff options
| author | Gerd Moellmann | 2000-02-29 09:34:42 +0000 |
|---|---|---|
| committer | Gerd Moellmann | 2000-02-29 09:34:42 +0000 |
| commit | 86f1e1ece391bf8b38ee554d5bb1b0a4dbe1536c (patch) | |
| tree | f54de65178fba00cafc77fd33dfeff88d9b98033 | |
| parent | 948709770a216c647187999070dbb327f8832269 (diff) | |
| download | emacs-86f1e1ece391bf8b38ee554d5bb1b0a4dbe1536c.tar.gz emacs-86f1e1ece391bf8b38ee554d5bb1b0a4dbe1536c.zip | |
(calculator-use-menu): New option.
(calculator-initial-bindings): Changed some bindings to work as
macros.
(calculator-forced-input): Removed.
(calculator-restart-other-mode): New variable.
(calculator-mode-map): Set up menu.
| -rw-r--r-- | lisp/calculator.el | 336 |
1 files changed, 235 insertions, 101 deletions
diff --git a/lisp/calculator.el b/lisp/calculator.el index fdca294df1d..cfe459341f8 100644 --- a/lisp/calculator.el +++ b/lisp/calculator.el | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | ;; Author: Eli Barzilay <eli@lambda.cs.cornell.edu> | 5 | ;; Author: Eli Barzilay <eli@lambda.cs.cornell.edu> |
| 6 | ;; Keywords: tools, convenience | 6 | ;; Keywords: tools, convenience |
| 7 | ;; Time-stamp: <2000-02-01 20:12:16 eli> | 7 | ;; Time-stamp: <2000-02-16 21:07:54 eli> |
| 8 | 8 | ||
| 9 | ;; This file is part of GNU Emacs. | 9 | ;; This file is part of GNU Emacs. |
| 10 | 10 | ||
| @@ -61,6 +61,12 @@ conventional help keys." | |||
| 61 | :type 'boolean | 61 | :type 'boolean |
| 62 | :group 'calculator) | 62 | :group 'calculator) |
| 63 | 63 | ||
| 64 | (defcustom calculator-use-menu t | ||
| 65 | "*Make `calculator' create a menu. | ||
| 66 | Note that this requires easymenu. Must be set before loading." | ||
| 67 | :type 'boolean | ||
| 68 | :group 'calculator) | ||
| 69 | |||
| 64 | (defcustom calculator-bind-escape nil | 70 | (defcustom calculator-bind-escape nil |
| 65 | "*If non-nil, set escape to exit the calculator." | 71 | "*If non-nil, set escape to exit the calculator." |
| 66 | :type 'boolean | 72 | :type 'boolean |
| @@ -178,14 +184,14 @@ Examples: | |||
| 178 | ;;; Code: | 184 | ;;; Code: |
| 179 | 185 | ||
| 180 | (defvar calculator-initial-operators | 186 | (defvar calculator-initial-operators |
| 181 | '(;; these have keybindings of themselves, not calculator-ops | 187 | '(;; "+"/"-" have keybindings of themselves, not calculator-ops |
| 182 | (nobind "=" = identity 1 -1) | 188 | ("=" = identity 1 -1) |
| 183 | (nobind "+" + + 2 4) | 189 | (nobind "+" + + 2 4) |
| 184 | (nobind "-" - - 2 4) | 190 | (nobind "-" - - 2 4) |
| 185 | (nobind "+" + + -1 9) | 191 | (nobind "+" + + -1 9) |
| 186 | (nobind "-" - - -1 9) | 192 | (nobind "-" - - -1 9) |
| 187 | (nobind "(" \( identity -1 -1) | 193 | ("(" \( identity -1 -1) |
| 188 | (nobind ")" \) identity +1 10) | 194 | (")" \) identity +1 10) |
| 189 | ;; normal keys | 195 | ;; normal keys |
| 190 | ("|" or (logior TX TY) 2 2) | 196 | ("|" or (logior TX TY) 2 2) |
| 191 | ("#" xor (logxor TX TY) 2 2) | 197 | ("#" xor (logxor TX TY) 2 2) |
| @@ -288,9 +294,6 @@ documentation for an example.") | |||
| 288 | (defvar calculator-buffer nil | 294 | (defvar calculator-buffer nil |
| 289 | "The current calculator buffer.") | 295 | "The current calculator buffer.") |
| 290 | 296 | ||
| 291 | (defvar calculator-forced-input nil | ||
| 292 | "Used to make alias events, e.g., make Return equivalent to `='.") | ||
| 293 | |||
| 294 | (defvar calculator-last-opXY nil | 297 | (defvar calculator-last-opXY nil |
| 295 | "The last binary operation and its arguments. | 298 | "The last binary operation and its arguments. |
| 296 | Used for repeating operations in calculator-repR/L.") | 299 | Used for repeating operations in calculator-repR/L.") |
| @@ -302,52 +305,58 @@ Used for repeating operations in calculator-repR/L.") | |||
| 302 | (defvar calculator-saved-global-map nil | 305 | (defvar calculator-saved-global-map nil |
| 303 | "Saved global key map.") | 306 | "Saved global key map.") |
| 304 | 307 | ||
| 308 | (defvar calculator-restart-other-mode nil | ||
| 309 | "Used to hack restarting with the mode electric mode changed.") | ||
| 310 | |||
| 305 | (defvar calculator-mode-map nil | 311 | (defvar calculator-mode-map nil |
| 306 | "The calculator key map.") | 312 | "The calculator key map.") |
| 307 | 313 | ||
| 308 | (or calculator-mode-map | 314 | (or calculator-mode-map |
| 309 | (let ((map (make-sparse-keymap "Calculator"))) | 315 | (let ((map (make-sparse-keymap))) |
| 310 | (suppress-keymap map t) | 316 | (suppress-keymap map t) |
| 311 | (define-key map "i" nil) | 317 | (define-key map "i" nil) |
| 312 | (define-key map "o" nil) | 318 | (define-key map "o" nil) |
| 313 | (let ((p '(calculator-open-paren "(" "[" "{" | 319 | (let ((p |
| 314 | calculator-close-paren ")" "]" "}" | 320 | '(("(" "[" "{") |
| 315 | calculator-op-or-exp "+" "-" [kp-add] [kp-subtract] | 321 | (")" "]" "}") |
| 316 | calculator-digit "0" "1" "2" "3" "4" "5" "6" "7" | 322 | (calculator-op-or-exp "+" "-" [kp-add] [kp-subtract]) |
| 317 | "8" "9" "a" "b" "c" "d" "f" | 323 | (calculator-digit "0" "1" "2" "3" "4" "5" "6" "7" "8" |
| 318 | [kp-0] [kp-1] [kp-2] [kp-3] [kp-4] | 324 | "9" "a" "b" "c" "d" "f" |
| 319 | [kp-5] [kp-6] [kp-7] [kp-8] [kp-9] | 325 | [kp-0] [kp-1] [kp-2] [kp-3] [kp-4] |
| 320 | calculator-op [kp-divide] [kp-multiply] | 326 | [kp-5] [kp-6] [kp-7] [kp-8] [kp-9]) |
| 321 | calculator-decimal "." [kp-decimal] | 327 | (calculator-op [kp-divide] [kp-multiply]) |
| 322 | calculator-exp "e" | 328 | (calculator-decimal "." [kp-decimal]) |
| 323 | calculator-dec/deg-mode "D" | 329 | (calculator-exp "e") |
| 324 | calculator-set-register "s" | 330 | (calculator-dec/deg-mode "D") |
| 325 | calculator-get-register "g" | 331 | (calculator-set-register "s") |
| 326 | calculator-radix-mode "H" "X" "O" "B" | 332 | (calculator-get-register "g") |
| 327 | calculator-radix-input-mode "id" "ih" "ix" "io" "ib" | 333 | (calculator-radix-mode "H" "X" "O" "B") |
| 328 | "iD" "iH" "iX" "iO" "iB" | 334 | (calculator-radix-input-mode "id" "ih" "ix" "io" "ib" |
| 329 | calculator-radix-output-mode "od" "oh" "ox" "oo" "ob" | 335 | "iD" "iH" "iX" "iO" "iB") |
| 330 | "oD" "oH" "oX" "oO" "oB" | 336 | (calculator-radix-output-mode "od" "oh" "ox" "oo" "ob" |
| 331 | calculator-saved-up [?\C-p] [up] | 337 | "oD" "oH" "oX" "oO" "oB") |
| 332 | calculator-saved-down [?\C-n] [down] | 338 | (calculator-saved-up [up] [?\C-p]) |
| 333 | calculator-quit "q" [?\C-g] | 339 | (calculator-saved-down [down] [?\C-n]) |
| 334 | calculator-enter [enter] [linefeed] [kp-enter] | 340 | (calculator-quit "q" [?\C-g]) |
| 335 | [?\r] [?\n] | 341 | ("=" [enter] [linefeed] [kp-enter] |
| 336 | calculator-save-on-list " " [space] | 342 | [?\r] [?\n]) |
| 337 | calculator-clear-saved [?\C-c] [(control delete)] | 343 | (calculator-save-on-list " " [space]) |
| 338 | calculator-save-and-quit [(control return)] | 344 | (calculator-clear-saved [?\C-c] [(control delete)]) |
| 339 | [(control kp-enter)] | 345 | (calculator-save-and-quit [(control return)] |
| 340 | calculator-paste [insert] [(shift insert)] | 346 | [(control kp-enter)]) |
| 341 | calculator-clear [delete] [?\C-?] [?\C-d] | 347 | (calculator-paste [insert] [(shift insert)]) |
| 342 | calculator-help [?h] [??] [f1] [help] | 348 | (calculator-clear [delete] [?\C-?] [?\C-d]) |
| 343 | calculator-copy [(control insert)] | 349 | (calculator-help [?h] [??] [f1] [help]) |
| 344 | calculator-backspace [backspace] | 350 | (calculator-copy [(control insert)]) |
| 345 | )) | 351 | (calculator-backspace [backspace]) |
| 346 | (f nil)) | 352 | ))) |
| 347 | (while p | 353 | (while p |
| 348 | (cond | 354 | ;; reverse the keys so first defs come last - makes the more |
| 349 | ((symbolp (car p)) (setq f (car p))) | 355 | ;; sensible bindings visible in the menu |
| 350 | (p (define-key map (car p) f))) | 356 | (let ((func (car (car p))) (keys (reverse (cdr (car p))))) |
| 357 | (while keys | ||
| 358 | (define-key map (car keys) func) | ||
| 359 | (setq keys (cdr keys)))) | ||
| 351 | (setq p (cdr p)))) | 360 | (setq p (cdr p)))) |
| 352 | (if calculator-bind-escape | 361 | (if calculator-bind-escape |
| 353 | (progn (define-key map [?\e] 'calculator-quit) | 362 | (progn (define-key map [?\e] 'calculator-quit) |
| @@ -355,6 +364,126 @@ Used for repeating operations in calculator-repR/L.") | |||
| 355 | (define-key map [?\e ?\e ?\e] 'calculator-quit)) | 364 | (define-key map [?\e ?\e ?\e] 'calculator-quit)) |
| 356 | ;; make C-h work in text-mode | 365 | ;; make C-h work in text-mode |
| 357 | (or window-system (define-key map [?\C-h] 'calculator-backspace)) | 366 | (or window-system (define-key map [?\C-h] 'calculator-backspace)) |
| 367 | ;; set up a menu | ||
| 368 | (if (and calculator-use-menu (not (boundp 'calculator-menu))) | ||
| 369 | (let ((radix-selectors | ||
| 370 | (mapcar (lambda (x) | ||
| 371 | `([,(nth 0 x) | ||
| 372 | (calculator-radix-mode ,(nth 2 x)) | ||
| 373 | :style radio | ||
| 374 | :keys ,(nth 2 x) | ||
| 375 | :selected | ||
| 376 | (and | ||
| 377 | (eq calculator-input-radix ',(nth 1 x)) | ||
| 378 | (eq calculator-output-radix ',(nth 1 x)))] | ||
| 379 | [,(concat (nth 0 x) " Input") | ||
| 380 | (calculator-radix-input-mode ,(nth 2 x)) | ||
| 381 | :keys ,(concat "i" (downcase (nth 2 x))) | ||
| 382 | :style radio | ||
| 383 | :selected | ||
| 384 | (eq calculator-input-radix ',(nth 1 x))] | ||
| 385 | [,(concat (nth 0 x) " Output") | ||
| 386 | (calculator-radix-output-mode ,(nth 2 x)) | ||
| 387 | :keys ,(concat "o" (downcase (nth 2 x))) | ||
| 388 | :style radio | ||
| 389 | :selected | ||
| 390 | (eq calculator-output-radix ',(nth 1 x))])) | ||
| 391 | '(("Decimal" nil "D") | ||
| 392 | ("Binary" bin "B") | ||
| 393 | ("Octal" oct "O") | ||
| 394 | ("Hexadecimal" hex "H")))) | ||
| 395 | (op '(lambda (name key) | ||
| 396 | `[,name (calculator-op ,key) :keys ,key]))) | ||
| 397 | (easy-menu-define | ||
| 398 | calculator-menu map "Calculator menu." | ||
| 399 | `("Calculator" | ||
| 400 | ["Help" | ||
| 401 | (let ((last-command 'calculator-help)) (calculator-help)) | ||
| 402 | :keys "?"] | ||
| 403 | "---" | ||
| 404 | ["Copy" calculator-copy] | ||
| 405 | ["Paste" calculator-paste] | ||
| 406 | "---" | ||
| 407 | ["Electric mode" | ||
| 408 | (progn (calculator-quit) | ||
| 409 | (setq calculator-restart-other-mode t) | ||
| 410 | (run-with-timer 0.1 nil '(lambda () (message nil))) | ||
| 411 | ;; the message from the menu will be visible, | ||
| 412 | ;; couldn't make it go away... | ||
| 413 | (calculator)) | ||
| 414 | :active (not calculator-electric-mode)] | ||
| 415 | ["Normal mode" | ||
| 416 | (progn (setq calculator-restart-other-mode t) | ||
| 417 | (calculator-quit)) | ||
| 418 | :active calculator-electric-mode] | ||
| 419 | "---" | ||
| 420 | ("Functions" | ||
| 421 | ,(funcall op "Repeat-right" ">") | ||
| 422 | ,(funcall op "Repeat-left" "<") | ||
| 423 | "------General------" | ||
| 424 | ,(funcall op "Reciprocal" ";") | ||
| 425 | ,(funcall op "Log" "L") | ||
| 426 | ,(funcall op "Square-root" "Q") | ||
| 427 | ,(funcall op "Factorial" "!") | ||
| 428 | "------Trigonometric------" | ||
| 429 | ,(funcall op "Sinus" "S") | ||
| 430 | ,(funcall op "Cosine" "C") | ||
| 431 | ,(funcall op "Tangent" "T") | ||
| 432 | ,(funcall op "Inv-Sinus" "IS") | ||
| 433 | ,(funcall op "Inv-Cosine" "IC") | ||
| 434 | ,(funcall op "Inv-Tangent" "IT") | ||
| 435 | "------Bitwise------" | ||
| 436 | ,(funcall op "Or" "|") | ||
| 437 | ,(funcall op "Xor" "#") | ||
| 438 | ,(funcall op "And" "&") | ||
| 439 | ,(funcall op "Not" "~")) | ||
| 440 | ("Saved List" | ||
| 441 | ["Eval+Save" calculator-save-on-list] | ||
| 442 | ["Prev number" calculator-saved-up] | ||
| 443 | ["Next number" calculator-saved-down] | ||
| 444 | ["Delete current" calculator-clear | ||
| 445 | :active (and calculator-display-fragile | ||
| 446 | calculator-saved-list | ||
| 447 | (= (car calculator-stack) | ||
| 448 | (nth calculator-saved-ptr | ||
| 449 | calculator-saved-list)))] | ||
| 450 | ["Delete all" calculator-clear-saved] | ||
| 451 | "---" | ||
| 452 | ,(funcall op "List-total" "l") | ||
| 453 | ,(funcall op "List-average" "v")) | ||
| 454 | ("Registers" | ||
| 455 | ["Get register" calculator-get-register] | ||
| 456 | ["Set register" calculator-set-register]) | ||
| 457 | ("Modes" | ||
| 458 | ["Radians" | ||
| 459 | (progn | ||
| 460 | (and (or calculator-input-radix calculator-output-radix) | ||
| 461 | (calculator-radix-mode "D")) | ||
| 462 | (and calculator-deg (calculator-dec/deg-mode))) | ||
| 463 | :keys "D" | ||
| 464 | :style radio | ||
| 465 | :selected (not (or calculator-input-radix | ||
| 466 | calculator-output-radix | ||
| 467 | calculator-deg))] | ||
| 468 | ["Degrees" | ||
| 469 | (progn | ||
| 470 | (and (or calculator-input-radix calculator-output-radix) | ||
| 471 | (calculator-radix-mode "D")) | ||
| 472 | (or calculator-deg (calculator-dec/deg-mode))) | ||
| 473 | :keys "D" | ||
| 474 | :style radio | ||
| 475 | :selected (and calculator-deg | ||
| 476 | (not (or calculator-input-radix | ||
| 477 | calculator-output-radix)))] | ||
| 478 | "---" | ||
| 479 | ,@(mapcar 'car radix-selectors) | ||
| 480 | ("Seperate I/O" | ||
| 481 | ,@(mapcar (lambda (x) (nth 1 x)) radix-selectors) | ||
| 482 | "---" | ||
| 483 | ,@(mapcar (lambda (x) (nth 2 x)) radix-selectors))) | ||
| 484 | "---" | ||
| 485 | ["Copy+Quit" calculator-save-and-quit] | ||
| 486 | ["Quit" calculator-quit])))) | ||
| 358 | (setq calculator-mode-map map))) | 487 | (setq calculator-mode-map map))) |
| 359 | 488 | ||
| 360 | (defun calculator-mode () | 489 | (defun calculator-mode () |
| @@ -375,7 +504,7 @@ will be the hexadecimal digit). | |||
| 375 | Here are the editing keys: | 504 | Here are the editing keys: |
| 376 | * `RET' `=' evaluate the current expression | 505 | * `RET' `=' evaluate the current expression |
| 377 | * `C-insert' copy the whole current expression to the `kill-ring' | 506 | * `C-insert' copy the whole current expression to the `kill-ring' |
| 378 | * `C-enter' evaluate, save result the `kill-ring' and exit | 507 | * `C-return' evaluate, save result the `kill-ring' and exit |
| 379 | * `insert' paste a number if the one was copied (normally) | 508 | * `insert' paste a number if the one was copied (normally) |
| 380 | * `delete' `C-d' clear last argument or whole expression (hit twice) | 509 | * `delete' `C-d' clear last argument or whole expression (hit twice) |
| 381 | * `backspace' delete a digit or a previous expression element | 510 | * `backspace' delete a digit or a previous expression element |
| @@ -456,37 +585,19 @@ more information. | |||
| 456 | "Run the pocket calculator. | 585 | "Run the pocket calculator. |
| 457 | See the documentation for `calculator-mode' for more information." | 586 | See the documentation for `calculator-mode' for more information." |
| 458 | (interactive) | 587 | (interactive) |
| 459 | (if calculator-electric-mode | 588 | (if calculator-restart-other-mode |
| 460 | (progn (require 'electric) | 589 | (setq calculator-electric-mode (not calculator-electric-mode))) |
| 461 | (message nil))) ; hide load message | ||
| 462 | (setq calculator-buffer | ||
| 463 | (or (and (bufferp calculator-buffer) | ||
| 464 | (buffer-live-p calculator-buffer) | ||
| 465 | calculator-buffer) | ||
| 466 | (if calculator-electric-mode | ||
| 467 | (get-buffer-create "*calculator*") | ||
| 468 | (let ((split-window-keep-point nil) | ||
| 469 | (window-min-height 2)) | ||
| 470 | (select-window | ||
| 471 | (split-window-vertically (- (window-height) 2))) | ||
| 472 | (switch-to-buffer | ||
| 473 | (get-buffer-create "*calculator*")))))) | ||
| 474 | (set-buffer calculator-buffer) | ||
| 475 | (calculator-mode) | ||
| 476 | (setq buffer-read-only t) | ||
| 477 | (if calculator-initial-operators | 590 | (if calculator-initial-operators |
| 478 | (progn (calculator-add-operators calculator-initial-operators) | 591 | (progn (calculator-add-operators calculator-initial-operators) |
| 479 | (setq calculator-initial-operators nil) | 592 | (setq calculator-initial-operators nil) |
| 480 | ;; don't change this since it is a customization variable, | 593 | ;; don't change this since it is a customization variable, |
| 481 | ;; its set function will add any new operators. | 594 | ;; its set function will add any new operators. |
| 482 | (calculator-add-operators calculator-user-operators))) | 595 | (calculator-add-operators calculator-user-operators))) |
| 483 | (calculator-reset) | ||
| 484 | (calculator-update-display) | ||
| 485 | (if calculator-electric-mode | 596 | (if calculator-electric-mode |
| 486 | (save-window-excursion | 597 | (save-window-excursion |
| 598 | (progn (require 'electric) (message nil)) ; hide load message | ||
| 487 | (let (old-g-map old-l-map (echo-keystrokes 0) | 599 | (let (old-g-map old-l-map (echo-keystrokes 0) |
| 488 | (garbage-collection-messages nil)) ; no gc msg when electric | 600 | (garbage-collection-messages nil)) ; no gc msg when electric |
| 489 | (kill-buffer calculator-buffer) | ||
| 490 | ;; strange behavior in FSF: doesn't always select correct | 601 | ;; strange behavior in FSF: doesn't always select correct |
| 491 | ;; minibuffer. I have no idea how to fix this | 602 | ;; minibuffer. I have no idea how to fix this |
| 492 | (setq calculator-buffer (window-buffer (minibuffer-window))) | 603 | (setq calculator-buffer (window-buffer (minibuffer-window))) |
| @@ -496,7 +607,7 @@ See the documentation for `calculator-mode' for more information." | |||
| 496 | (setq old-l-map (current-local-map)) | 607 | (setq old-l-map (current-local-map)) |
| 497 | (setq old-g-map (current-global-map)) | 608 | (setq old-g-map (current-global-map)) |
| 498 | (setq calculator-saved-global-map (current-global-map)) | 609 | (setq calculator-saved-global-map (current-global-map)) |
| 499 | (use-local-map calculator-mode-map) | 610 | (use-local-map nil) |
| 500 | (use-global-map calculator-mode-map) | 611 | (use-global-map calculator-mode-map) |
| 501 | (unwind-protect | 612 | (unwind-protect |
| 502 | (catch 'calculator-done | 613 | (catch 'calculator-done |
| @@ -505,13 +616,31 @@ See the documentation for `calculator-mode' for more information." | |||
| 505 | ;; can't use 'noprompt, bug in electric.el | 616 | ;; can't use 'noprompt, bug in electric.el |
| 506 | '(lambda () 'noprompt) | 617 | '(lambda () 'noprompt) |
| 507 | nil | 618 | nil |
| 508 | (lambda (x y) | 619 | (lambda (x y) (calculator-update-display)))) |
| 509 | (calculator-update-display)))) | ||
| 510 | (and calculator-buffer | 620 | (and calculator-buffer |
| 511 | (catch 'calculator-done (calculator-quit))) | 621 | (catch 'calculator-done (calculator-quit))) |
| 512 | (use-local-map old-l-map) | 622 | (use-local-map old-l-map) |
| 513 | (use-global-map old-g-map)))) | 623 | (use-global-map old-g-map)))) |
| 514 | (message "Hit `?' For a quick help screen."))) | 624 | (progn |
| 625 | (setq calculator-buffer | ||
| 626 | (or (and (bufferp calculator-buffer) | ||
| 627 | (buffer-live-p calculator-buffer) | ||
| 628 | calculator-buffer) | ||
| 629 | (if calculator-electric-mode | ||
| 630 | (get-buffer-create "*calculator*") | ||
| 631 | (let ((split-window-keep-point nil) | ||
| 632 | (window-min-height 2)) | ||
| 633 | (select-window | ||
| 634 | (split-window-vertically (- (window-height) 2))) | ||
| 635 | (switch-to-buffer | ||
| 636 | (get-buffer-create "*calculator*")))))) | ||
| 637 | (set-buffer calculator-buffer) | ||
| 638 | (calculator-mode) | ||
| 639 | (setq buffer-read-only t) | ||
| 640 | (calculator-reset) | ||
| 641 | (message "Hit `?' For a quick help screen."))) | ||
| 642 | (if (and calculator-restart-other-mode calculator-electric-mode) | ||
| 643 | (calculator))) | ||
| 515 | 644 | ||
| 516 | (defun calculator-op-arity (op) | 645 | (defun calculator-op-arity (op) |
| 517 | "Return OP's arity, 2, +1 or -1." | 646 | "Return OP's arity, 2, +1 or -1." |
| @@ -555,10 +684,12 @@ Adds MORE-OPS to `calculator-operator', called initially to handle | |||
| 555 | 684 | ||
| 556 | (defun calculator-reset () | 685 | (defun calculator-reset () |
| 557 | "Reset calculator variables." | 686 | "Reset calculator variables." |
| 558 | (setq calculator-stack nil | 687 | (or calculator-restart-other-mode |
| 559 | calculator-curnum nil | 688 | (setq calculator-stack nil |
| 560 | calculator-stack-display nil | 689 | calculator-curnum nil |
| 561 | calculator-display-fragile nil) | 690 | calculator-stack-display nil |
| 691 | calculator-display-fragile nil)) | ||
| 692 | (setq calculator-restart-other-mode nil) | ||
| 562 | (calculator-update-display)) | 693 | (calculator-update-display)) |
| 563 | 694 | ||
| 564 | (defun calculator-get-prompt () | 695 | (defun calculator-get-prompt () |
| @@ -803,9 +934,10 @@ PREC is a precedence - reduce everything with higher precedence." | |||
| 803 | (or (fboundp 'key-press-event-p) | 934 | (or (fboundp 'key-press-event-p) |
| 804 | (defun key-press-event-p (&rest _) nil))) | 935 | (defun key-press-event-p (&rest _) nil))) |
| 805 | 936 | ||
| 806 | (defun calculator-last-input () | 937 | (defun calculator-last-input (&optional keys) |
| 807 | "Last char (or event or event sequence) that was read." | 938 | "Last char (or event or event sequence) that was read. |
| 808 | (let ((inp (or calculator-forced-input (this-command-keys)))) | 939 | Optional string argument KEYS will force using it as the keys entered." |
| 940 | (let ((inp (or keys (this-command-keys)))) | ||
| 809 | (if (or (stringp inp) (not (arrayp inp))) | 941 | (if (or (stringp inp) (not (arrayp inp))) |
| 810 | inp | 942 | inp |
| 811 | ;; this translates kp-x to x and [tries to] create a string to | 943 | ;; this translates kp-x to x and [tries to] create a string to |
| @@ -889,10 +1021,11 @@ OP is the operator (if any) that caused this call." | |||
| 889 | (setq calculator-curnum (concat (or calculator-curnum "1") "e")) | 1021 | (setq calculator-curnum (concat (or calculator-curnum "1") "e")) |
| 890 | (calculator-update-display))))) | 1022 | (calculator-update-display))))) |
| 891 | 1023 | ||
| 892 | (defun calculator-op () | 1024 | (defun calculator-op (&optional keys) |
| 893 | "Enter an operator on the stack, doing all necessary reductions." | 1025 | "Enter an operator on the stack, doing all necessary reductions. |
| 1026 | Optional string argument KEYS will force using it as the keys entered." | ||
| 894 | (interactive) | 1027 | (interactive) |
| 895 | (let* ((last-inp (calculator-last-input)) | 1028 | (let* ((last-inp (calculator-last-input keys)) |
| 896 | (op (assoc last-inp calculator-operators))) | 1029 | (op (assoc last-inp calculator-operators))) |
| 897 | (calculator-clear-fragile op) | 1030 | (calculator-clear-fragile op) |
| 898 | (if (and calculator-curnum (/= (calculator-op-arity op) 0)) | 1031 | (if (and calculator-curnum (/= (calculator-op-arity op) 0)) |
| @@ -960,34 +1093,37 @@ Used with +/- for entering them as digits in numbers like 1e-3." | |||
| 960 | (setq calculator-deg (not calculator-deg))) | 1093 | (setq calculator-deg (not calculator-deg))) |
| 961 | (calculator-update-display t)) | 1094 | (calculator-update-display t)) |
| 962 | 1095 | ||
| 963 | (defun calculator-radix-mode () | 1096 | (defun calculator-radix-mode (&optional keys) |
| 964 | "Set input and display radix modes." | 1097 | "Set input and display radix modes. |
| 1098 | Optional string argument KEYS will force using it as the keys entered." | ||
| 965 | (interactive) | 1099 | (interactive) |
| 966 | (calculator-radix-input-mode) | 1100 | (calculator-radix-input-mode keys) |
| 967 | (calculator-radix-output-mode)) | 1101 | (calculator-radix-output-mode keys)) |
| 968 | 1102 | ||
| 969 | (defun calculator-radix-input-mode () | 1103 | (defun calculator-radix-input-mode (&optional keys) |
| 970 | "Set input radix modes." | 1104 | "Set input radix modes. |
| 1105 | Optional string argument KEYS will force using it as the keys entered." | ||
| 971 | (interactive) | 1106 | (interactive) |
| 972 | (if calculator-curnum | 1107 | (if calculator-curnum |
| 973 | (setq calculator-stack | 1108 | (setq calculator-stack |
| 974 | (cons (calculator-curnum-value) calculator-stack))) | 1109 | (cons (calculator-curnum-value) calculator-stack))) |
| 975 | (setq calculator-curnum nil) | 1110 | (setq calculator-curnum nil) |
| 976 | (setq calculator-input-radix | 1111 | (setq calculator-input-radix |
| 977 | (let ((inp (calculator-last-input))) | 1112 | (let ((inp (calculator-last-input keys))) |
| 978 | (cdr (assq (upcase (aref inp (1- (length inp)))) | 1113 | (cdr (assq (upcase (aref inp (1- (length inp)))) |
| 979 | calculator-char-radix)))) | 1114 | calculator-char-radix)))) |
| 980 | (calculator-update-display)) | 1115 | (calculator-update-display)) |
| 981 | 1116 | ||
| 982 | (defun calculator-radix-output-mode () | 1117 | (defun calculator-radix-output-mode (&optional keys) |
| 983 | "Set display radix modes." | 1118 | "Set display radix modes. |
| 1119 | Optional string argument KEYS will force using it as the keys entered." | ||
| 984 | (interactive) | 1120 | (interactive) |
| 985 | (if calculator-curnum | 1121 | (if calculator-curnum |
| 986 | (setq calculator-stack | 1122 | (setq calculator-stack |
| 987 | (cons (calculator-curnum-value) calculator-stack))) | 1123 | (cons (calculator-curnum-value) calculator-stack))) |
| 988 | (setq calculator-curnum nil) | 1124 | (setq calculator-curnum nil) |
| 989 | (setq calculator-output-radix | 1125 | (setq calculator-output-radix |
| 990 | (let ((inp (calculator-last-input))) | 1126 | (let ((inp (calculator-last-input keys))) |
| 991 | (cdr (assq (upcase (aref inp (1- (length inp)))) | 1127 | (cdr (assq (upcase (aref inp (1- (length inp)))) |
| 992 | calculator-char-radix)))) | 1128 | calculator-char-radix)))) |
| 993 | (calculator-update-display t)) | 1129 | (calculator-update-display t)) |
| @@ -1018,7 +1154,8 @@ Used with +/- for entering them as digits in numbers like 1e-3." | |||
| 1018 | (setq calculator-stack | 1154 | (setq calculator-stack |
| 1019 | (list (nth calculator-saved-ptr calculator-saved-list)) | 1155 | (list (nth calculator-saved-ptr calculator-saved-list)) |
| 1020 | calculator-display-fragile t) | 1156 | calculator-display-fragile t) |
| 1021 | (calculator-reset))))) | 1157 | (calculator-reset)) |
| 1158 | (calculator-update-display)))) | ||
| 1022 | 1159 | ||
| 1023 | (defun calculator-saved-up () | 1160 | (defun calculator-saved-up () |
| 1024 | "Go up the list of saved values." | 1161 | "Go up the list of saved values." |
| @@ -1033,20 +1170,17 @@ Used with +/- for entering them as digits in numbers like 1e-3." | |||
| 1033 | (defun calculator-open-paren () | 1170 | (defun calculator-open-paren () |
| 1034 | "Equivalents of `(' use this." | 1171 | "Equivalents of `(' use this." |
| 1035 | (interactive) | 1172 | (interactive) |
| 1036 | (let ((calculator-forced-input "(")) | 1173 | (calculator-op "(")) |
| 1037 | (calculator-op))) | ||
| 1038 | 1174 | ||
| 1039 | (defun calculator-close-paren () | 1175 | (defun calculator-close-paren () |
| 1040 | "Equivalents of `)' use this." | 1176 | "Equivalents of `)' use this." |
| 1041 | (interactive) | 1177 | (interactive) |
| 1042 | (let ((calculator-forced-input ")")) | 1178 | (calculator-op ")")) |
| 1043 | (calculator-op))) | ||
| 1044 | 1179 | ||
| 1045 | (defun calculator-enter () | 1180 | (defun calculator-enter () |
| 1046 | "Make Enter equivalent to `='." | 1181 | "Evaluate current expression." |
| 1047 | (interactive) | 1182 | (interactive) |
| 1048 | (let ((calculator-forced-input "=")) | 1183 | (calculator-op "=")) |
| 1049 | (calculator-op))) | ||
| 1050 | 1184 | ||
| 1051 | (defun calculator-backspace () | 1185 | (defun calculator-backspace () |
| 1052 | "Backward delete a single digit or a stack element." | 1186 | "Backward delete a single digit or a stack element." |
| @@ -1144,7 +1278,7 @@ Used by `calculator-paste' and `get-register'." | |||
| 1144 | * enter/= - evaluate current expr. * s/g - set/get a register | 1278 | * enter/= - evaluate current expr. * s/g - set/get a register |
| 1145 | * space - evaluate & save on list * l/v - list total/average | 1279 | * space - evaluate & save on list * l/v - list total/average |
| 1146 | * up/down/C-p/C-n - browse saved * C-delete - clear all saved | 1280 | * up/down/C-p/C-n - browse saved * C-delete - clear all saved |
| 1147 | * C-insert - copy whole expr. * C-enter - evaluate, copy, exit | 1281 | * C-insert - copy whole expr. * C-return - evaluate, copy, exit |
| 1148 | * insert - paste a number * backspace- delete backwards | 1282 | * insert - paste a number * backspace- delete backwards |
| 1149 | * delete - clear argument or list value or whole expression (twice) | 1283 | * delete - clear argument or list value or whole expression (twice) |
| 1150 | * escape/q - exit." | 1284 | * escape/q - exit." |