X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/5ca425fcc9c50f37da67347bf904ff4aff4db4b7..7182a9a69285a1faf79c72e382ac2e3017bd7317:/lisp/calculator.el diff --git a/lisp/calculator.el b/lisp/calculator.el index a19d84b527..76ff4053c7 100644 --- a/lisp/calculator.el +++ b/lisp/calculator.el @@ -1,10 +1,10 @@ -;;; calculator.el --- A [not so] simple calculator for Emacs. +;;; calculator.el --- a [not so] simple calculator for Emacs -;; Copyright (C) 1998, 2000 by Free Software Foundation, Inc. +;; Copyright (C) 1998, 2000, 2001 by Free Software Foundation, Inc. -;; Author: Eli Barzilay +;; Author: Eli Barzilay ;; Keywords: tools, convenience -;; Time-stamp: <2000-11-07 15:04:06 eli> +;; Time-stamp: <2002-07-13 01:14:35 eli> ;; This file is part of GNU Emacs. @@ -23,7 +23,7 @@ ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, ;; MA 02111-1307, USA. -;;;============================================================================ +;;;===================================================================== ;;; Commentary: ;; ;; A calculator for Emacs. @@ -41,13 +41,17 @@ ;; ;; For latest version, check ;; http://www.barzilay.org/misc/calculator.el +;; + +;;; History: +;; I hate history. (eval-and-compile (if (fboundp 'defgroup) nil (defmacro defgroup (&rest forms) nil) (defmacro defcustom (s v d &rest r) (list 'defvar s v d)))) -;;;============================================================================ +;;;===================================================================== ;;; Customization: (defgroup calculator nil @@ -94,9 +98,23 @@ documentation for `calculator-mode'." Used by the `calculator-standard-display' function - it will use the format string \"%.NC\" where this number is N and C is a character given at runtime." - :type 'string + :type 'integer :group 'calculator) +(defcustom calculator-radix-grouping-mode t + "*Use digit grouping in radix output mode. +If this is set, chunks of `calculator-radix-grouping-digits' characters +will be separated by `calculator-radix-grouping-separator' when in radix +output mode is active (determined by `calculator-output-radix').") + +(defcustom calculator-radix-grouping-digits 4 + "*The number of digits used for grouping display in radix modes. +See `calculator-radix-grouping-mode'.") + +(defcustom calculator-radix-grouping-separator "'" + "*The separator used in radix grouping display. +See `calculator-radix-grouping-mode'.") + (defcustom calculator-remove-zeros t "*Non-nil value means delete all redundant zero decimal digits. If this value is not t, and not nil, redundant zeros are removed except @@ -125,14 +143,14 @@ character, in this case the `calculator-standard-displayer' function will be used with this character for a format string.") (defcustom calculator-displayers - '(((std ?n) "Standard dislpay, decimal point or scientific") + '(((std ?n) "Standard display, decimal point or scientific") (calculator-eng-display "Eng display") ((std ?f) "Standard display, decimal point") - ((std ?e) "Standard dislpay, scientific") + ((std ?e) "Standard display, scientific") ("%S" "Emacs printer")) "*A list of displayers. Each element is a list of a displayer and a description string. The -first element is the one which is curently used, this is for the display +first element is the one which is currently used, this is for the display of result values not values in expressions. A displayer specification is the same as the values that can be stored in `calculator-displayer'. @@ -147,6 +165,12 @@ floats, otherwise the Emacs reader will fail on them." :type 'boolean :group 'calculator) +(defcustom calculator-copy-displayer nil + "*If non-nil, this is any value that can be used for +`calculator-displayer', to format a string before copying it with +`calculator-copy'. If nil, then `calculator-displayer's normal value is +used.") + (defcustom calculator-2s-complement nil "*If non-nil, show negative numbers in 2s complement in radix modes. Otherwise show as a negative number." @@ -154,7 +178,11 @@ Otherwise show as a negative number." :group 'calculator) (defcustom calculator-mode-hook nil - "*List of hook functions for `calculator-mode' to run." + "*List of hook functions for `calculator-mode' to run. +Note: if `calculator-electric-mode' is on, then this hook will get +activated in the minibuffer - in that case it should not do much more +than local key settings and other effects that will change things +outside the scope of calculator related code." :type 'hook :group 'calculator) @@ -209,10 +237,10 @@ Examples: :type '(repeat (list string symbol sexp integer integer)) :group 'calculator) -;;;============================================================================ +;;;===================================================================== ;;; Code: -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Variables (defvar calculator-initial-operators @@ -346,7 +374,7 @@ Used for repeating operations in calculator-repR/L.") (defvar calculator-restart-other-mode nil "Used to hack restarting with the electric mode changed.") -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Key bindings (defvar calculator-mode-map nil @@ -378,8 +406,8 @@ Used for repeating operations in calculator-repR/L.") "oD" "oH" "oX" "oO" "oB") (calculator-rotate-displayer "'") (calculator-rotate-displayer-back "\"") - (calculator-displayer-left "{") - (calculator-displayer-right "}") + (calculator-displayer-prev "{") + (calculator-displayer-next "}") (calculator-saved-up [up] [?\C-p]) (calculator-saved-down [down] [?\C-n]) (calculator-quit "q" [?\C-g]) @@ -389,10 +417,11 @@ Used for repeating operations in calculator-repR/L.") (calculator-clear-saved [?\C-c] [(control delete)]) (calculator-save-and-quit [(control return)] [(control kp-enter)]) - (calculator-paste [insert] [(shift insert)] [mouse-2]) + (calculator-paste [insert] [(shift insert)] + [paste] [mouse-2] [?\C-y]) (calculator-clear [delete] [?\C-?] [?\C-d]) (calculator-help [?h] [??] [f1] [help]) - (calculator-copy [(control insert)]) + (calculator-copy [(control insert)] [copy]) (calculator-backspace [backspace]) ))) (while p @@ -526,21 +555,21 @@ Used for repeating operations in calculator-repR/L.") ,@(mapcar (lambda (x) (nth 1 x)) radix-selectors) "---" ,@(mapcar (lambda (x) (nth 2 x)) radix-selectors))) - ("Decimal Dislpay" + ("Decimal Display" ,@(mapcar (lambda (d) (vector (cadr d) ;; Note: inserts actual object here `(calculator-rotate-displayer ',d))) calculator-displayers) "---" - ["Change Display Left" calculator-displayer-left] - ["Change Display Right" calculator-displayer-right]) + ["Change Prev Display" calculator-displayer-prev] + ["Change Next Display" calculator-displayer-next]) "---" ["Copy+Quit" calculator-save-and-quit] ["Quit" calculator-quit])))) (setq calculator-mode-map map))) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Startup and mode stuff (defun calculator-mode () @@ -601,10 +630,11 @@ The prompt indicates the current modes: * \"=?\": (? is B/O/H) the display radix (when input is decimal); * \"??\": (? is D/B/O/H) 1st char for input radix, 2nd for display. -Also, the quote character can be used to switch display modes for -decimal numbers (double-quote rotates back), and the two brace -characters (\"{\" and \"}\" change display parameters that these -displayers use (if they handle such). +Also, the quote key can be used to switch display modes for decimal +numbers (double-quote rotates back), and the two brace characters +\(\"{\" and \"}\" change display parameters that these displayers use (if +they handle such). If output is using any radix mode, then these keys +toggle digit grouping mode and the chunk size. Values can be saved for future reference in either a list of saved values, or in registers. @@ -658,14 +688,13 @@ See the documentation for `calculator-mode' for more information." ;; don't change this since it is a customization variable, ;; its set function will add any new operators (calculator-add-operators calculator-user-operators))) + (setq calculator-buffer (get-buffer-create "*calculator*")) (if calculator-electric-mode (save-window-excursion (progn (require 'electric) (message nil)) ; hide load message (let (old-g-map old-l-map (echo-keystrokes 0) (garbage-collection-messages nil)) ; no gc msg when electric - ;; strange behavior in FSF: doesn't always select correct - ;; minibuffer. I have no idea how to fix this - (setq calculator-buffer (window-buffer (minibuffer-window))) + (set-window-buffer (minibuffer-window) calculator-buffer) (select-window (minibuffer-window)) (calculator-reset) (calculator-update-display) @@ -674,6 +703,7 @@ See the documentation for `calculator-mode' for more information." (setq calculator-saved-global-map (current-global-map)) (use-local-map nil) (use-global-map calculator-mode-map) + (run-hooks 'calculator-mode-hook) (unwind-protect (catch 'calculator-done (Electric-command-loop @@ -687,28 +717,20 @@ See the documentation for `calculator-mode' for more information." (use-local-map old-l-map) (use-global-map old-g-map)))) (progn - (setq calculator-buffer - (or (and (bufferp calculator-buffer) - (buffer-live-p calculator-buffer) - calculator-buffer) - (if calculator-electric-mode - (get-buffer-create "*calculator*") - (let ((split-window-keep-point nil) - (window-min-height 2)) - (select-window - ;; maybe leave two lines for our window because - ;; of the normal `raised' modeline in Emacs 21 - (split-window-vertically - (- (window-height) - (if (and - (fboundp 'face-attr-construct) - (plist-get (face-attr-construct 'modeline) - :box)) - 3 - 2)))) - (switch-to-buffer - (get-buffer-create "*calculator*")))))) - (set-buffer calculator-buffer) + (cond + ((not (get-buffer-window calculator-buffer)) + (let ((split-window-keep-point nil) + (window-min-height 2)) + ;; maybe leave two lines for our window because of the normal + ;; `raised' modeline in Emacs 21 + (select-window + (split-window-vertically + (if (and (fboundp 'face-attr-construct) + (plist-get (face-attr-construct 'modeline) :box)) + -3 -2))) + (switch-to-buffer calculator-buffer))) + ((not (eq (current-buffer) calculator-buffer)) + (select-window (get-buffer-window calculator-buffer)))) (calculator-mode) (setq buffer-read-only t) (calculator-reset) @@ -716,8 +738,14 @@ See the documentation for `calculator-mode' for more information." (if (and calculator-restart-other-mode calculator-electric-mode) (calculator))) -;;;---------------------------------------------------------------------------- -;;; Operatos +(defun calculator-message (string &rest arguments) + "Same as `message', but special handle of electric mode." + (apply 'message string arguments) + (if calculator-electric-mode + (progn (sit-for 1) (message nil)))) + +;;;--------------------------------------------------------------------- +;;; Operators (defun calculator-op-arity (op) "Return OP's arity, 2, +1 or -1." @@ -759,7 +787,7 @@ Adds MORE-OPS to `calculator-operator', called initially to handle (setq calculator-operators (append (nreverse added-ops) calculator-operators)))) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Display stuff (defun calculator-reset () @@ -817,82 +845,116 @@ The string is set not to exceed the screen width." (concat calculator-prompt (substring prompt (+ trim (length calculator-prompt))))))) -(defun calculator-curnum-value () - "Get the numeric value of the displayed number string as a float." +(defun calculator-string-to-number (str) + "Convert the given STR to a number, according to the value of +`calculator-input-radix'." (if calculator-input-radix (let ((radix (cdr (assq calculator-input-radix '((bin . 2) (oct . 8) (hex . 16))))) - (i -1) (value 0)) - ;; assume valid input (upcased & characters in range) - (while (< (setq i (1+ i)) (length calculator-curnum)) - (setq value - (+ (let ((ch (aref calculator-curnum i))) - (- ch (if (<= ch ?9) ?0 (- ?A 10)))) - (* radix value)))) + (i -1) (value 0) (new-value 0)) + ;; assume mostly valid input (e.g., characters in range) + (while (< (setq i (1+ i)) (length str)) + (setq new-value + (let* ((ch (upcase (aref str i))) + (n (cond ((< ch ?0) nil) + ((<= ch ?9) (- ch ?0)) + ((< ch ?A) nil) + ((<= ch ?Z) (- ch (- ?A 10))) + (t nil)))) + (if (and n (<= 0 n) (< n radix)) + (+ n (* radix value)) + (progn + (calculator-message + "Warning: Ignoring bad input character `%c'." ch) + (sit-for 1) + value)))) + (if (if (< new-value 0) (> value 0) (< value 0)) + (calculator-message "Warning: Overflow in input.")) + (setq value new-value)) value) - (car - (read-from-string - (cond - ((equal "." calculator-curnum) - "0.0") - ((string-match "[eE][+-]?$" calculator-curnum) - (concat calculator-curnum "0")) - ((string-match "\\.[0-9]\\|[eE]" calculator-curnum) - calculator-curnum) - ((string-match "\\." calculator-curnum) - ;; do this because Emacs reads "23." as an integer - (concat calculator-curnum "0")) - ((stringp calculator-curnum) - (concat calculator-curnum ".0")) - (t "0.0")))))) + (car (read-from-string + (cond ((equal "." str) "0.0") + ((string-match "[eE][+-]?$" str) (concat str "0")) + ((string-match "\\.[0-9]\\|[eE]" str) str) + ((string-match "\\." str) + ;; do this because Emacs reads "23." as an integer + (concat str "0")) + ((stringp str) (concat str ".0")) + (t "0.0")))))) + +(defun calculator-curnum-value () + "Get the numeric value of the displayed number string as a float." + (calculator-string-to-number calculator-curnum)) (defun calculator-rotate-displayer (&optional new-disp) "Switch to the next displayer on the `calculator-displayers' list. Can be called with an optional argument NEW-DISP to force rotation to -that argument." +that argument. +If radix output mode is active, toggle digit grouping." (interactive) - (setq calculator-displayers - (if (and new-disp (memq new-disp calculator-displayers)) - (let ((tmp nil)) - (while (not (eq (car calculator-displayers) new-disp)) - (setq tmp (cons (car calculator-displayers) tmp)) - (setq calculator-displayers (cdr calculator-displayers))) - (setq calculator-displayers - (nconc calculator-displayers (nreverse tmp)))) - (nconc (cdr calculator-displayers) - (list (car calculator-displayers))))) - (message "Using %s." (cadr (car calculator-displayers))) - (if calculator-electric-mode - (progn (sit-for 1) (message nil))) + (cond + (calculator-output-radix + (setq calculator-radix-grouping-mode + (not calculator-radix-grouping-mode)) + (calculator-message + "Digit grouping mode %s." + (if calculator-radix-grouping-mode "ON" "OFF"))) + (t + (setq calculator-displayers + (if (and new-disp (memq new-disp calculator-displayers)) + (let ((tmp nil)) + (while (not (eq (car calculator-displayers) new-disp)) + (setq tmp (cons (car calculator-displayers) tmp)) + (setq calculator-displayers + (cdr calculator-displayers))) + (setq calculator-displayers + (nconc calculator-displayers (nreverse tmp)))) + (nconc (cdr calculator-displayers) + (list (car calculator-displayers))))) + (calculator-message + "Using %s." (cadr (car calculator-displayers))))) (calculator-enter)) (defun calculator-rotate-displayer-back () - "Like `calculator-rotate-displayer', but rotates modes back." + "Like `calculator-rotate-displayer', but rotates modes back. +If radix output mode is active, toggle digit grouping." (interactive) (calculator-rotate-displayer (car (last calculator-displayers)))) -(defun calculator-displayer-left () +(defun calculator-displayer-prev () "Send the current displayer function a 'left argument. This is used to modify display arguments (if the current displayer -function supports this)." +function supports this). +If radix output mode is active, increase the grouping size." (interactive) - (and (car calculator-displayers) - (let ((disp (caar calculator-displayers))) - (cond ((symbolp disp) (funcall disp 'left)) - ((and (consp disp) (eq 'std (car disp))) - (calculator-standard-displayer 'left (cadr disp))))))) - -(defun calculator-displayer-right () + (if calculator-output-radix + (progn (setq calculator-radix-grouping-digits + (1+ calculator-radix-grouping-digits)) + (calculator-enter)) + (and (car calculator-displayers) + (let ((disp (caar calculator-displayers))) + (cond + ((symbolp disp) (funcall disp 'left)) + ((and (consp disp) (eq 'std (car disp))) + (calculator-standard-displayer 'left (cadr disp)))))))) + +(defun calculator-displayer-next () "Send the current displayer function a 'right argument. This is used to modify display arguments (if the current displayer -function supports this)." +function supports this). +If radix output mode is active, decrease the grouping size." (interactive) - (and (car calculator-displayers) - (let ((disp (caar calculator-displayers))) - (cond ((symbolp disp) (funcall disp 'right)) - ((and (consp disp) (eq 'std (car disp))) - (calculator-standard-displayer 'right (cadr disp))))))) + (if calculator-output-radix + (progn (setq calculator-radix-grouping-digits + (max 2 (1- calculator-radix-grouping-digits))) + (calculator-enter)) + (and (car calculator-displayers) + (let ((disp (caar calculator-displayers))) + (cond + ((symbolp disp) (funcall disp 'right)) + ((and (consp disp) (eq 'std (car disp))) + (calculator-standard-displayer 'right (cadr disp)))))))) (defun calculator-remove-zeros (numstr) "Get a number string NUMSTR and remove unnecessary zeroes. @@ -937,14 +999,16 @@ It will also remove redundant zeros from the result." (setq calculator-number-digits (1+ calculator-number-digits)) (calculator-enter))) - (let ((str (format - (concat "%." - (number-to-string calculator-number-digits) - (if (eq char ?n) - (let ((n (abs num))) - (if (or (< n 0.001) (> n 1e8)) "e" "f")) - (string char))) - num))) + (let ((str (if (zerop num) + "0" + (format + (concat "%." + (number-to-string calculator-number-digits) + (if (eq char ?n) + (let ((n (abs num))) + (if (or (< n 0.001) (> n 1e8)) "e" "f")) + (string char))) + num)))) (calculator-remove-zeros str)))) (defun calculator-eng-display (num) @@ -982,7 +1046,9 @@ the 'left or 'right when one of the standard modes is used." (setq num (/ num 1000.0)) (setq exp (+ exp 3)) (setq i (1+ i))))))) (or calculator-eng-tmp-show (setq calculator-eng-extra nil)) - (let ((str (format (concat "%." calculator-number-digits "f") + (let ((str (format (concat "%." (number-to-string + calculator-number-digits) + "f") num))) (concat (let ((calculator-remove-zeros ;; make sure we don't leave integers @@ -990,7 +1056,7 @@ the 'left or 'right when one of the standard modes is used." (calculator-remove-zeros str)) "e" (number-to-string exp)))))) -(defun calculator-num-to-string (num) +(defun calculator-number-to-string (num) "Convert NUM to a displayable string." (cond ((and (numberp num) calculator-output-radix) @@ -1010,23 +1076,30 @@ the 'left or 'right when one of the standard modes is used." (?6 . "110") (?7 . "111"))))))) (string-match "^0*\\(.+\\)" s) (setq str (match-string 1 s)))) + (if calculator-radix-grouping-mode + (let ((d (/ (length str) calculator-radix-grouping-digits)) + (r (% (length str) calculator-radix-grouping-digits))) + (while (>= (setq d (1- d)) (if (zerop r) 1 0)) + (let ((i (+ r (* d calculator-radix-grouping-digits)))) + (setq str (concat (substring str 0 i) + calculator-radix-grouping-separator + (substring str i))))))) (upcase (if (and (not calculator-2s-complement) (< num 0)) (concat "-" str) str)))) - ((and (numberp num) (car calculator-displayers)) - (let ((disp (if (= 1 (length calculator-stack)) - ;; customizable display for a single value - (caar calculator-displayers) - calculator-displayer))) - (cond ((stringp disp) (format disp num)) - ((symbolp disp) (funcall disp num)) - ((and (consp disp) - (eq 'std (car disp))) - (calculator-standard-displayer - num (cadr disp))) - ((listp disp) (eval disp)) - (t (prin1-to-string num t))))) + ((and (numberp num) calculator-displayer) + (cond + ((stringp calculator-displayer) + (format calculator-displayer num)) + ((symbolp calculator-displayer) + (funcall calculator-displayer num)) + ((and (consp calculator-displayer) + (eq 'std (car calculator-displayer))) + (calculator-standard-displayer num (cadr calculator-displayer))) + ((listp calculator-displayer) + (eval calculator-displayer)) + (t (prin1-to-string num t)))) ;; operators are printed here (t (prin1-to-string (nth 1 num) t)))) @@ -1041,9 +1114,15 @@ If optional argument FORCE is non-nil, don't use the cached string." (cons calculator-stack (if calculator-stack (concat - (mapconcat 'calculator-num-to-string - (reverse calculator-stack) - " ") + (let ((calculator-displayer + (if (and calculator-displayers + (= 1 (length calculator-stack))) + ;; customizable display for a single value + (caar calculator-displayers) + calculator-displayer))) + (mapconcat 'calculator-number-to-string + (reverse calculator-stack) + " ")) " " (and calculator-display-fragile calculator-saved-list @@ -1065,7 +1144,7 @@ If optional argument FORCE is non-nil, don't use the cached string." (goto-char (1+ (length calculator-prompt))) (goto-char (1- (point))))) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Stack computations (defun calculator-reduce-stack (prec) @@ -1195,7 +1274,7 @@ arguments." (or (fboundp 'key-press-event-p) (defun key-press-event-p (&rest _) nil))) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Input interaction (defun calculator-last-input (&optional keys) @@ -1309,9 +1388,8 @@ Optional string argument KEYS will force using it as the keys entered." (if (not (and op (= -1 (calculator-op-arity op)))) ;;(error "Binary operator without a first operand") (progn - (message "Binary operator without a first operand") - (if calculator-electric-mode - (progn (sit-for 1) (message nil))) + (calculator-message + "Binary operator without a first operand") (throw 'op-error nil))))) (calculator-reduce-stack (cond ((eq (nth 1 op) '\() 10) @@ -1324,9 +1402,7 @@ Optional string argument KEYS will force using it as the keys entered." (not (numberp (car calculator-stack))))) ;;(error "Unterminated expression") (progn - (message "Unterminated expression") - (if calculator-electric-mode - (progn (sit-for 1) (message nil))) + (calculator-message "Unterminated expression") (throw 'op-error nil))) (setq calculator-stack (cons op calculator-stack)) (calculator-reduce-stack (calculator-op-prec op)) @@ -1356,7 +1432,7 @@ operators)." (calculator-digit) (calculator-op))) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Input/output modes (not display) (defun calculator-dec/deg-mode () @@ -1408,7 +1484,7 @@ Optional string argument KEYS will force using it as the keys entered." calculator-char-radix)))) (calculator-update-display t)) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Saved values list (defun calculator-save-on-list () @@ -1451,7 +1527,7 @@ Optional string argument KEYS will force using it as the keys entered." (interactive) (calculator-saved-move -1)) -;;;---------------------------------------------------------------------------- +;;;--------------------------------------------------------------------- ;;; Misc functions (defun calculator-open-paren () @@ -1509,12 +1585,17 @@ Optional string argument KEYS will force using it as the keys entered." (defun calculator-copy () "Copy current number to the `kill-ring'." (interactive) - (calculator-enter) - ;; remove trailing spaces and and an index - (let ((s (cdr calculator-stack-display))) - (if (string-match "^\\([^ ]+\\) *\\(\\[[0-9/]+\\]\\)? *$" s) - (setq s (match-string 1 s))) - (kill-new s))) + (let ((calculator-displayer + (or calculator-copy-displayer calculator-displayer)) + (calculator-displayers + (if calculator-copy-displayer nil calculator-displayers))) + (calculator-enter) + ;; remove trailing spaces and and an index + (let ((s (cdr calculator-stack-display))) + (and s + (if (string-match "^\\([^ ]+\\) *\\(\\[[0-9/]+\\]\\)? *$" s) + (setq s (match-string 1 s))) + (kill-new s))))) (defun calculator-set-register (reg) "Set a register value for REG." @@ -1525,7 +1606,7 @@ Optional string argument KEYS will force using it as the keys entered." (setcdr as val) (setq calculator-registers (cons (cons reg val) calculator-registers))) - (message (format "[%c] := %S" reg val)))) + (calculator-message "[%c] := %S" reg val))) (defun calculator-put-value (val) "Paste VAL as if entered. @@ -1536,24 +1617,27 @@ Used by `calculator-paste' and `get-register'." (not (numberp (car calculator-stack))))) (progn (calculator-clear-fragile) - (setq calculator-curnum (calculator-num-to-string val)) + (setq calculator-curnum (let ((calculator-displayer "%S")) + (calculator-number-to-string val))) (calculator-update-display)))) (defun calculator-paste () "Paste a value from the `kill-ring'." (interactive) (calculator-put-value - (let ((str (current-kill 0))) - (if calculator-paste-decimals - (progn - (string-match "\\([0-9]+\\)\\(\\.[0-9]+\\)?\\(e[0-9]+\\)?" str) - (if (or (match-string 1 str) - (match-string 2 str) - (match-string 3 str)) - (setq str (concat (match-string 1 str) - (or (match-string 2 str) ".0") - (match-string 3 str)))))) - (condition-case nil (car (read-from-string str)) + (let ((str (replace-regexp-in-string + "^ *\\(.+[^ ]\\) *$" "\\1" (current-kill 0)))) + (and (not calculator-input-radix) + calculator-paste-decimals + (string-match "\\([0-9]+\\)\\(\\.[0-9]+\\)?\\(e[0-9]+\\)?" + str) + (or (match-string 1 str) + (match-string 2 str) + (match-string 3 str)) + (setq str (concat (or (match-string 1 str) "0") + (or (match-string 2 str) ".0") + (or (match-string 3 str) "")))) + (condition-case nil (calculator-string-to-number str) (error nil))))) (defun calculator-get-register (reg) @@ -1588,7 +1672,11 @@ Used by `calculator-paste' and `get-register'." (require 'ehelp) (if calculator-electric-mode (use-global-map calculator-saved-global-map)) - (electric-describe-mode) + (if (or (not calculator-electric-mode) + ;; XEmacs has a problem with electric-describe-mode + (string-match "XEmacs" (emacs-version))) + (describe-mode) + (electric-describe-mode)) (if calculator-electric-mode (use-global-map g-map)) (select-window win) ; these are for XEmacs (also below) @@ -1658,7 +1746,7 @@ To use this, apply a binary operator (evaluate it), then call this." (while (> x 0) (setq r (* r (truncate x))) (setq x (1- x))) - r)) + (+ 0.0 r))) (defun calculator-truncate (n) "Truncate N, return 0 in case of overflow." @@ -1667,4 +1755,5 @@ To use this, apply a binary operator (evaluate it), then call this." (provide 'calculator) +;;; arch-tag: a1b9766c-af8a-4a74-b466-65ad8eeb0c73 ;;; calculator.el ends here