aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Moellmann2001-09-25 08:37:33 +0000
committerGerd Moellmann2001-09-25 08:37:33 +0000
commit4351784fd89d5272a0464699c05ee44a3dc461ca (patch)
tree9b2ce0a572ad1defac0e36518db9fe0726c5620c
parent452294c2bf34b33ea8c2fa9d9b750a7c33599d6d (diff)
downloademacs-4351784fd89d5272a0464699c05ee44a3dc461ca.tar.gz
emacs-4351784fd89d5272a0464699c05ee44a3dc461ca.zip
(calculator-copy-displayer): New user-option.
(calculator-displayer-prev, calculator-displayer-next): Renamed from calculator-displayed-{left,right}. (calculator, calculator-standard-displayer) (calculator-num-to-string, calculator-update-display) (calculator-copy, calculator-put-value): Bug and display fixes.
-rw-r--r--lisp/calculator.el138
1 files changed, 77 insertions, 61 deletions
diff --git a/lisp/calculator.el b/lisp/calculator.el
index 7d6ec114307..b0ca5b4f449 100644
--- a/lisp/calculator.el
+++ b/lisp/calculator.el
@@ -1,10 +1,10 @@
1;;; calculator.el --- a [not so] simple calculator for Emacs 1;;; calculator.el --- a [not so] simple calculator for Emacs
2 2
3;; Copyright (C) 1998, 2000 by Free Software Foundation, Inc. 3;; Copyright (C) 1998, 2000, 2001 by Free Software Foundation, Inc.
4 4
5;; Author: Eli Barzilay <eli@barzilay.org> 5;; Author: Eli Barzilay <eli@barzilay.org>
6;; Keywords: tools, convenience 6;; Keywords: tools, convenience
7;; Time-stamp: <2001-07-15 11:04:11 pavel> 7;; Time-stamp: <2001-09-23 02:24:35 eli>
8 8
9;; This file is part of GNU Emacs. 9;; This file is part of GNU Emacs.
10 10
@@ -41,6 +41,10 @@
41;; 41;;
42;; For latest version, check 42;; For latest version, check
43;; http://www.barzilay.org/misc/calculator.el 43;; http://www.barzilay.org/misc/calculator.el
44;;
45
46;;; History:
47;; I hate history.
44 48
45(eval-and-compile 49(eval-and-compile
46 (if (fboundp 'defgroup) nil 50 (if (fboundp 'defgroup) nil
@@ -147,6 +151,12 @@ floats, otherwise the Emacs reader will fail on them."
147 :type 'boolean 151 :type 'boolean
148 :group 'calculator) 152 :group 'calculator)
149 153
154(defcustom calculator-copy-displayer nil
155 "*If non-nil, this is any value that can be used for
156`calculator-displayer', to format a string before copying it with
157`calculator-copy'. If nil, then `calculator-displayer's normal value is
158used.")
159
150(defcustom calculator-2s-complement nil 160(defcustom calculator-2s-complement nil
151 "*If non-nil, show negative numbers in 2s complement in radix modes. 161 "*If non-nil, show negative numbers in 2s complement in radix modes.
152Otherwise show as a negative number." 162Otherwise show as a negative number."
@@ -378,8 +388,8 @@ Used for repeating operations in calculator-repR/L.")
378 "oD" "oH" "oX" "oO" "oB") 388 "oD" "oH" "oX" "oO" "oB")
379 (calculator-rotate-displayer "'") 389 (calculator-rotate-displayer "'")
380 (calculator-rotate-displayer-back "\"") 390 (calculator-rotate-displayer-back "\"")
381 (calculator-displayer-left "{") 391 (calculator-displayer-pref "{")
382 (calculator-displayer-right "}") 392 (calculator-displayer-next "}")
383 (calculator-saved-up [up] [?\C-p]) 393 (calculator-saved-up [up] [?\C-p])
384 (calculator-saved-down [down] [?\C-n]) 394 (calculator-saved-down [down] [?\C-n])
385 (calculator-quit "q" [?\C-g]) 395 (calculator-quit "q" [?\C-g])
@@ -534,8 +544,8 @@ Used for repeating operations in calculator-repR/L.")
534 `(calculator-rotate-displayer ',d))) 544 `(calculator-rotate-displayer ',d)))
535 calculator-displayers) 545 calculator-displayers)
536 "---" 546 "---"
537 ["Change Display Left" calculator-displayer-left] 547 ["Change Prev Display" calculator-displayer-prev]
538 ["Change Display Right" calculator-displayer-right]) 548 ["Change Next Display" calculator-displayer-next])
539 "---" 549 "---"
540 ["Copy+Quit" calculator-save-and-quit] 550 ["Copy+Quit" calculator-save-and-quit]
541 ["Quit" calculator-quit])))) 551 ["Quit" calculator-quit]))))
@@ -688,28 +698,21 @@ See the documentation for `calculator-mode' for more information."
688 (use-local-map old-l-map) 698 (use-local-map old-l-map)
689 (use-global-map old-g-map)))) 699 (use-global-map old-g-map))))
690 (progn 700 (progn
691 (setq calculator-buffer 701 (setq calculator-buffer (get-buffer-create "*calculator*"))
692 (or (and (bufferp calculator-buffer) 702 (cond
693 (buffer-live-p calculator-buffer) 703 ((not (get-buffer-window calculator-buffer))
694 calculator-buffer) 704 (let ((split-window-keep-point nil)
695 (if calculator-electric-mode 705 (window-min-height 2))
696 (get-buffer-create "*calculator*") 706 ;; maybe leave two lines for our window because of the normal
697 (let ((split-window-keep-point nil) 707 ;; `raised' modeline in Emacs 21
698 (window-min-height 2)) 708 (select-window
699 (select-window 709 (split-window-vertically
700 ;; maybe leave two lines for our window because 710 (if (and (fboundp 'face-attr-construct)
701 ;; of the normal `raised' modeline in Emacs 21 711 (plist-get (face-attr-construct 'modeline) :box))
702 (split-window-vertically 712 -3 -2)))
703 (- (window-height) 713 (switch-to-buffer calculator-buffer)))
704 (if (and 714 ((not (eq (current-buffer) calculator-buffer))
705 (fboundp 'face-attr-construct) 715 (select-window (get-buffer-window calculator-buffer))))
706 (plist-get (face-attr-construct 'modeline)
707 :box))
708 3
709 2))))
710 (switch-to-buffer
711 (get-buffer-create "*calculator*"))))))
712 (set-buffer calculator-buffer)
713 (calculator-mode) 716 (calculator-mode)
714 (setq buffer-read-only t) 717 (setq buffer-read-only t)
715 (calculator-reset) 718 (calculator-reset)
@@ -873,7 +876,7 @@ that argument."
873 (interactive) 876 (interactive)
874 (calculator-rotate-displayer (car (last calculator-displayers)))) 877 (calculator-rotate-displayer (car (last calculator-displayers))))
875 878
876(defun calculator-displayer-left () 879(defun calculator-displayer-prev ()
877 "Send the current displayer function a 'left argument. 880 "Send the current displayer function a 'left argument.
878This is used to modify display arguments (if the current displayer 881This is used to modify display arguments (if the current displayer
879function supports this)." 882function supports this)."
@@ -884,7 +887,7 @@ function supports this)."
884 ((and (consp disp) (eq 'std (car disp))) 887 ((and (consp disp) (eq 'std (car disp)))
885 (calculator-standard-displayer 'left (cadr disp))))))) 888 (calculator-standard-displayer 'left (cadr disp)))))))
886 889
887(defun calculator-displayer-right () 890(defun calculator-displayer-next ()
888 "Send the current displayer function a 'right argument. 891 "Send the current displayer function a 'right argument.
889This is used to modify display arguments (if the current displayer 892This is used to modify display arguments (if the current displayer
890function supports this)." 893function supports this)."
@@ -938,14 +941,16 @@ It will also remove redundant zeros from the result."
938 (setq calculator-number-digits 941 (setq calculator-number-digits
939 (1+ calculator-number-digits)) 942 (1+ calculator-number-digits))
940 (calculator-enter))) 943 (calculator-enter)))
941 (let ((str (format 944 (let ((str (if (zerop num)
942 (concat "%." 945 "0"
943 (number-to-string calculator-number-digits) 946 (format
944 (if (eq char ?n) 947 (concat "%."
945 (let ((n (abs num))) 948 (number-to-string calculator-number-digits)
946 (if (or (< n 0.001) (> n 1e8)) "e" "f")) 949 (if (eq char ?n)
947 (string char))) 950 (let ((n (abs num)))
948 num))) 951 (if (or (< n 0.001) (> n 1e8)) "e" "f"))
952 (string char)))
953 num))))
949 (calculator-remove-zeros str)))) 954 (calculator-remove-zeros str))))
950 955
951(defun calculator-eng-display (num) 956(defun calculator-eng-display (num)
@@ -1015,19 +1020,18 @@ the 'left or 'right when one of the standard modes is used."
1015 (if (and (not calculator-2s-complement) (< num 0)) 1020 (if (and (not calculator-2s-complement) (< num 0))
1016 (concat "-" str) 1021 (concat "-" str)
1017 str)))) 1022 str))))
1018 ((and (numberp num) (car calculator-displayers)) 1023 ((and (numberp num) calculator-displayer)
1019 (let ((disp (if (= 1 (length calculator-stack)) 1024 (cond
1020 ;; customizable display for a single value 1025 ((stringp calculator-displayer)
1021 (caar calculator-displayers) 1026 (format calculator-displayer num))
1022 calculator-displayer))) 1027 ((symbolp calculator-displayer)
1023 (cond ((stringp disp) (format disp num)) 1028 (funcall calculator-displayer num))
1024 ((symbolp disp) (funcall disp num)) 1029 ((and (consp calculator-displayer)
1025 ((and (consp disp) 1030 (eq 'std (car calculator-displayer)))
1026 (eq 'std (car disp))) 1031 (calculator-standard-displayer num (cadr calculator-displayer)))
1027 (calculator-standard-displayer 1032 ((listp calculator-displayer)
1028 num (cadr disp))) 1033 (eval calculator-displayer))
1029 ((listp disp) (eval disp)) 1034 (t (prin1-to-string num t))))
1030 (t (prin1-to-string num t)))))
1031 ;; operators are printed here 1035 ;; operators are printed here
1032 (t (prin1-to-string (nth 1 num) t)))) 1036 (t (prin1-to-string (nth 1 num) t))))
1033 1037
@@ -1042,9 +1046,15 @@ If optional argument FORCE is non-nil, don't use the cached string."
1042 (cons calculator-stack 1046 (cons calculator-stack
1043 (if calculator-stack 1047 (if calculator-stack
1044 (concat 1048 (concat
1045 (mapconcat 'calculator-num-to-string 1049 (let ((calculator-displayer
1046 (reverse calculator-stack) 1050 (if (and calculator-displayers
1047 " ") 1051 (= 1 (length calculator-stack)))
1052 ;; customizable display for a single value
1053 (caar calculator-displayers)
1054 calculator-displayer)))
1055 (mapconcat 'calculator-num-to-string
1056 (reverse calculator-stack)
1057 " "))
1048 " " 1058 " "
1049 (and calculator-display-fragile 1059 (and calculator-display-fragile
1050 calculator-saved-list 1060 calculator-saved-list
@@ -1510,12 +1520,17 @@ Optional string argument KEYS will force using it as the keys entered."
1510(defun calculator-copy () 1520(defun calculator-copy ()
1511 "Copy current number to the `kill-ring'." 1521 "Copy current number to the `kill-ring'."
1512 (interactive) 1522 (interactive)
1513 (calculator-enter) 1523 (let ((calculator-displayer
1514 ;; remove trailing spaces and and an index 1524 (or calculator-copy-displayer calculator-displayer))
1515 (let ((s (cdr calculator-stack-display))) 1525 (calculator-displayers
1516 (if (string-match "^\\([^ ]+\\) *\\(\\[[0-9/]+\\]\\)? *$" s) 1526 (if calculator-copy-displayer nil calculator-displayers)))
1517 (setq s (match-string 1 s))) 1527 (calculator-enter)
1518 (kill-new s))) 1528 ;; remove trailing spaces and and an index
1529 (let ((s (cdr calculator-stack-display)))
1530 (and s
1531 (if (string-match "^\\([^ ]+\\) *\\(\\[[0-9/]+\\]\\)? *$" s)
1532 (setq s (match-string 1 s)))
1533 (kill-new s)))))
1519 1534
1520(defun calculator-set-register (reg) 1535(defun calculator-set-register (reg)
1521 "Set a register value for REG." 1536 "Set a register value for REG."
@@ -1537,7 +1552,8 @@ Used by `calculator-paste' and `get-register'."
1537 (not (numberp (car calculator-stack))))) 1552 (not (numberp (car calculator-stack)))))
1538 (progn 1553 (progn
1539 (calculator-clear-fragile) 1554 (calculator-clear-fragile)
1540 (setq calculator-curnum (calculator-num-to-string val)) 1555 (setq calculator-curnum (let ((calculator-displayer "%S"))
1556 (calculator-num-to-string val)))
1541 (calculator-update-display)))) 1557 (calculator-update-display))))
1542 1558
1543(defun calculator-paste () 1559(defun calculator-paste ()