Merge from emacs-23 branch, up to 2010-05-20T22:16:19Z!juri@jurta.org.
[bpt/emacs.git] / lisp / calculator.el
index c843a3d..1e9bb68 100644 (file)
@@ -1,7 +1,7 @@
 ;;; calculator.el --- a [not so] simple calculator for Emacs
 
 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,  2005, 2006, 2007,
-;;   2008 Free Software Foundation, Inc.
+;;   2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
 ;; Author: Eli Barzilay <eli@barzilay.org>
 ;; Keywords: tools, convenience
   :prefix "calculator"
   :version "21.1"
   :group 'tools
-  :group 'convenience)
+  :group 'applications)
 
 (defcustom calculator-electric-mode nil
-  "*Run `calculator' electrically, in the echo area.
+  "Run `calculator' electrically, in the echo area.
 Electric mode saves some place but changes the way you interact with the
 calculator."
   :type  'boolean
   :group 'calculator)
 
 (defcustom calculator-use-menu t
-  "*Make `calculator' create a menu.
+  "Make `calculator' create a menu.
 Note that this requires easymenu.  Must be set before loading."
   :type  'boolean
   :group 'calculator)
 
 (defcustom calculator-bind-escape nil
-  "*If non-nil, set escape to exit the calculator."
+  "If non-nil, set escape to exit the calculator."
   :type  'boolean
   :group 'calculator)
 
 (defcustom calculator-unary-style 'postfix
-  "*Value is either 'prefix or 'postfix.
+  "Value is either 'prefix or 'postfix.
 This determines the default behavior of unary operators."
   :type    '(choice (const prefix) (const postfix))
   :group   'calculator)
 
 (defcustom calculator-prompt "Calc=%s> "
-  "*The prompt used by the Emacs calculator.
+  "The prompt used by the Emacs calculator.
 It should contain a \"%s\" somewhere that will indicate the i/o radixes;
 this will be a two-character string as described in the documentation
 for `calculator-mode'."
@@ -89,7 +89,7 @@ for `calculator-mode'."
   :group 'calculator)
 
 (defcustom calculator-number-digits 3
-  "*The calculator's number of digits used for standard display.
+  "The calculator's number of digits used for standard display.
 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."
@@ -97,7 +97,7 @@ at runtime."
   :group 'calculator)
 
 (defcustom calculator-radix-grouping-mode t
-  "*Use digit grouping in radix output mode.
+  "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')."
@@ -105,19 +105,19 @@ output mode is active (determined by `calculator-output-radix')."
   :group 'calculator)
 
 (defcustom calculator-radix-grouping-digits 4
-  "*The number of digits used for grouping display in radix modes.
+  "The number of digits used for grouping display in radix modes.
 See `calculator-radix-grouping-mode'."
   :type  'integer
   :group 'calculator)
 
 (defcustom calculator-radix-grouping-separator "'"
-  "*The separator used in radix grouping display.
+  "The separator used in radix grouping display.
 See `calculator-radix-grouping-mode'."
   :type  'string
   :group 'calculator)
 
 (defcustom calculator-remove-zeros t
-  "*Non-nil value means delete all redundant zero decimal digits.
+  "Non-nil value means delete all redundant zero decimal digits.
 If this value is not t, and not nil, redundant zeros are removed except
 for one and if it is nil, nothing is removed.
 Used by the `calculator-remove-zeros' function."
@@ -125,7 +125,7 @@ Used by the `calculator-remove-zeros' function."
   :group 'calculator)
 
 (defcustom calculator-displayer '(std ?n)
-  "*A displayer specification for numerical values.
+  "A displayer specification for numerical values.
 This is the displayer used to show all numbers in an expression.  Result
 values will be displayed according to the first element of
 `calculator-displayers'.
@@ -150,7 +150,7 @@ will be used with this character for a format string."
     ((std ?f) "Standard display, decimal point")
     ((std ?e) "Standard display, scientific")
     ("%S"     "Emacs printer"))
-  "*A list of displayers.
+  "A list of displayers.
 Each element is a list of a displayer and a description string.  The
 first element is the one which is currently used, this is for the display
 of result values not values in expressions.  A displayer specification
@@ -161,14 +161,14 @@ is the same as the values that can be stored in `calculator-displayer'.
   :group 'calculator)
 
 (defcustom calculator-paste-decimals t
-  "*If non-nil, convert pasted integers so they have a decimal point.
+  "If non-nil, convert pasted integers so they have a decimal point.
 This makes it possible to paste big integers since they will be read as
 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
+  "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."
@@ -176,13 +176,13 @@ used."
   :group 'calculator)
 
 (defcustom calculator-2s-complement nil
-  "*If non-nil, show negative numbers in 2s complement in radix modes.
+  "If non-nil, show negative numbers in 2s complement in radix modes.
 Otherwise show as a negative number."
   :type  'boolean
   :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
@@ -191,7 +191,7 @@ outside the scope of calculator related code."
   :group 'calculator)
 
 (defcustom calculator-user-registers nil
-  "*An association list of user-defined register bindings.
+  "An association list of user-defined register bindings.
 Each element in this list is a list of a character and a number that
 will be stored in that character's register.
 
@@ -207,7 +207,7 @@ before you load calculator."
   :group 'calculator)
 
 (defcustom calculator-user-operators nil
-  "*A list of additional operators.
+  "A list of additional operators.
 This is a list in the same format as specified in the documentation for
 `calculator-operators', that you can use to bind additional calculator
 operators.  It is probably not a good idea to modify this value with
@@ -369,7 +369,8 @@ documentation for an example.")
 Used for repeating operations in calculator-repR/L.")
 
 (defvar calculator-registers ; use user-bindings first
-  (append calculator-user-registers (list (cons ?e e) (cons ?p pi)))
+  (append calculator-user-registers
+          (list (cons ?e float-e) (cons ?p float-pi)))
   "The association list of calculator register values.")
 
 (defvar calculator-saved-global-map nil
@@ -381,10 +382,7 @@ Used for repeating operations in calculator-repR/L.")
 ;;;---------------------------------------------------------------------
 ;;; Key bindings
 
-(defvar calculator-mode-map nil
-  "The calculator key map.")
-
-(or calculator-mode-map
+(defvar calculator-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map t)
     (define-key map "i" nil)
@@ -470,113 +468,114 @@ Used for repeating operations in calculator-repR/L.")
                        ("Binary"      bin "B")
                        ("Octal"       oct "O")
                        ("Hexadecimal" hex "H"))))
-            (op '(lambda (name key)
-                        `[,name (calculator-op ,key) :keys ,key])))
+            (op (lambda (name key)
+                  `[,name (calculator-op ,key) :keys ,key])))
         (easy-menu-define
-         calculator-menu map "Calculator menu."
-         `("Calculator"
-           ["Help"
-            (let ((last-command 'calculator-help)) (calculator-help))
-            :keys "?"]
-           "---"
-           ["Copy"  calculator-copy]
-           ["Paste" calculator-paste]
-           "---"
-           ["Electric mode"
-            (progn (calculator-quit)
-                   (setq calculator-restart-other-mode t)
-                   (run-with-timer 0.1 nil '(lambda () (message nil)))
-                   ;; the message from the menu will be visible,
-                   ;; couldn't make it go away...
-                   (calculator))
-            :active (not calculator-electric-mode)]
-           ["Normal mode"
-            (progn (setq calculator-restart-other-mode t)
-                   (calculator-quit))
-            :active calculator-electric-mode]
-           "---"
-           ("Functions"
-            ,(funcall op "Repeat-right" ">")
-            ,(funcall op "Repeat-left"  "<")
-            "------General------"
-            ,(funcall op "Reciprocal"   ";")
-            ,(funcall op "Log"          "L")
-            ,(funcall op "Square-root"  "Q")
-            ,(funcall op "Factorial"    "!")
-            "------Trigonometric------"
-            ,(funcall op "Sinus"        "S")
-            ,(funcall op "Cosine"       "C")
-            ,(funcall op "Tangent"      "T")
-            ,(funcall op "Inv-Sinus"    "IS")
-            ,(funcall op "Inv-Cosine"   "IC")
-            ,(funcall op "Inv-Tangent"  "IT")
-            "------Bitwise------"
-            ,(funcall op "Or"           "|")
-            ,(funcall op "Xor"          "#")
-            ,(funcall op "And"          "&")
-            ,(funcall op "Not"          "~"))
-           ("Saved List"
-            ["Eval+Save"      calculator-save-on-list]
-            ["Prev number"    calculator-saved-up]
-            ["Next number"    calculator-saved-down]
-            ["Delete current" calculator-clear
-             :active (and calculator-display-fragile
-                          calculator-saved-list
-                          (= (car calculator-stack)
-                             (nth calculator-saved-ptr
-                                  calculator-saved-list)))]
-            ["Delete all" calculator-clear-saved]
+          calculator-menu map "Calculator menu."
+          `("Calculator"
+            ["Help"
+             (let ((last-command 'calculator-help)) (calculator-help))
+             :keys "?"]
             "---"
-            ,(funcall op "List-total"   "l")
-            ,(funcall op "List-average" "v"))
-           ("Registers"
-            ["Get register" calculator-get-register]
-            ["Set register" calculator-set-register])
-           ("Modes"
-            ["Radians"
-             (progn
-               (and (or calculator-input-radix calculator-output-radix)
-                    (calculator-radix-mode "D"))
-               (and calculator-deg (calculator-dec/deg-mode)))
-             :keys "D"
-             :style radio
-             :selected (not (or calculator-input-radix
-                                calculator-output-radix
-                                calculator-deg))]
-            ["Degrees"
-             (progn
-               (and (or calculator-input-radix calculator-output-radix)
-                    (calculator-radix-mode "D"))
-               (or calculator-deg (calculator-dec/deg-mode)))
-             :keys "D"
-             :style radio
-             :selected (and calculator-deg
-                            (not (or calculator-input-radix
-                                     calculator-output-radix)))]
+            ["Copy"  calculator-copy]
+            ["Paste" calculator-paste]
             "---"
-            ,@(mapcar 'car radix-selectors)
-            ("Separate I/O"
-             ,@(mapcar (lambda (x) (nth 1 x)) radix-selectors)
+            ["Electric mode"
+             (progn (calculator-quit)
+                    (setq calculator-restart-other-mode t)
+                    (run-with-timer 0.1 nil '(lambda () (message nil)))
+                    ;; the message from the menu will be visible,
+                    ;; couldn't make it go away...
+                    (calculator))
+             :active (not calculator-electric-mode)]
+            ["Normal mode"
+             (progn (setq calculator-restart-other-mode t)
+                    (calculator-quit))
+             :active calculator-electric-mode]
+            "---"
+            ("Functions"
+             ,(funcall op "Repeat-right" ">")
+             ,(funcall op "Repeat-left"  "<")
+             "------General------"
+             ,(funcall op "Reciprocal"   ";")
+             ,(funcall op "Log"          "L")
+             ,(funcall op "Square-root"  "Q")
+             ,(funcall op "Factorial"    "!")
+             "------Trigonometric------"
+             ,(funcall op "Sinus"        "S")
+             ,(funcall op "Cosine"       "C")
+             ,(funcall op "Tangent"      "T")
+             ,(funcall op "Inv-Sinus"    "IS")
+             ,(funcall op "Inv-Cosine"   "IC")
+             ,(funcall op "Inv-Tangent"  "IT")
+             "------Bitwise------"
+             ,(funcall op "Or"           "|")
+             ,(funcall op "Xor"          "#")
+             ,(funcall op "And"          "&")
+             ,(funcall op "Not"          "~"))
+            ("Saved List"
+             ["Eval+Save"      calculator-save-on-list]
+             ["Prev number"    calculator-saved-up]
+             ["Next number"    calculator-saved-down]
+             ["Delete current" calculator-clear
+              :active (and calculator-display-fragile
+                           calculator-saved-list
+                           (= (car calculator-stack)
+                              (nth calculator-saved-ptr
+                                   calculator-saved-list)))]
+             ["Delete all" calculator-clear-saved]
+             "---"
+             ,(funcall op "List-total"   "l")
+             ,(funcall op "List-average" "v"))
+            ("Registers"
+             ["Get register" calculator-get-register]
+             ["Set register" calculator-set-register])
+            ("Modes"
+             ["Radians"
+              (progn
+                (and (or calculator-input-radix calculator-output-radix)
+                     (calculator-radix-mode "D"))
+                (and calculator-deg (calculator-dec/deg-mode)))
+              :keys "D"
+              :style radio
+              :selected (not (or calculator-input-radix
+                                 calculator-output-radix
+                                 calculator-deg))]
+             ["Degrees"
+              (progn
+                (and (or calculator-input-radix calculator-output-radix)
+                     (calculator-radix-mode "D"))
+                (or calculator-deg (calculator-dec/deg-mode)))
+              :keys "D"
+              :style radio
+              :selected (and calculator-deg
+                             (not (or calculator-input-radix
+                                      calculator-output-radix)))]
              "---"
-             ,@(mapcar (lambda (x) (nth 2 x)) radix-selectors)))
-           ("Decimal Display"
-            ,@(mapcar (lambda (d)
-                        (vector (cadr d)
-                                ;; Note: inserts actual object here
-                                `(calculator-rotate-displayer ',d)))
-                      calculator-displayers)
+             ,@(mapcar 'car radix-selectors)
+             ("Separate I/O"
+              ,@(mapcar (lambda (x) (nth 1 x)) radix-selectors)
+              "---"
+              ,@(mapcar (lambda (x) (nth 2 x)) radix-selectors)))
+            ("Decimal Display"
+             ,@(mapcar (lambda (d)
+                         (vector (cadr d)
+                                 ;; Note: inserts actual object here
+                                 `(calculator-rotate-displayer ',d)))
+                       calculator-displayers)
+             "---"
+             ["Change Prev Display" calculator-displayer-prev]
+             ["Change Next Display" calculator-displayer-next])
             "---"
-            ["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)))
+            ["Copy+Quit" calculator-save-and-quit]
+            ["Quit"      calculator-quit]))))
+    map)
+  "The calculator key map.")
 
 ;;;---------------------------------------------------------------------
 ;;; Startup and mode stuff
 
-(defun calculator-mode ()
+(define-derived-mode calculator-mode fundamental-mode "Calculator"
   ;; this help is also used as the major help screen
   "A [not so] simple calculator for Emacs.
 
@@ -670,13 +669,7 @@ Some interesting customization variables are:
 See the documentation for these variables, and \"calculator.el\" for
 more information.
 
-\\{calculator-mode-map}"
-  (interactive)
-  (kill-all-local-variables)
-  (setq major-mode 'calculator-mode)
-  (setq mode-name "Calculator")
-  (use-local-map calculator-mode-map)
-  (run-mode-hooks 'calculator-mode-hook))
+\\{calculator-mode-map}")
 
 (eval-when-compile (require 'electric) (require 'ehelp))
 
@@ -724,8 +717,7 @@ See the documentation for `calculator-mode' for more information."
     (progn
       (cond
         ((not (get-buffer-window calculator-buffer))
-         (let ((split-window-keep-point nil)
-               (window-min-height 2))
+         (let ((window-min-height 2))
            ;; maybe leave two lines for our window because of the normal
            ;; `raised' modeline in Emacs 21
            (select-window
@@ -1301,7 +1293,7 @@ arguments."
                       (calculator-funcall __f__ x y))))
           (fset 'D (function
                     (lambda (x)
-                      (if calculator-deg (/ (* x 180) pi) x))))
+                      (if calculator-deg (/ (* x 180) float-pi) x))))
           (unwind-protect (eval f)
             (if Fbound (fset 'F Fsave) (fmakunbound 'F))
             (if Dbound (fset 'D Dsave) (fmakunbound 'D)))))
@@ -1832,5 +1824,4 @@ 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