Use forward-line rather than goto-line.
[bpt/emacs.git] / lisp / calc / calc.el
index 5ff4f9a..8e6af0d 100644 (file)
@@ -1,12 +1,11 @@
 ;;; calc.el --- the GNU Emacs calculator
 
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007, 2008, 2009  Free Software Foundation, Inc.
 
 ;; Author: David Gillespie <daveg@synaptics.com>
 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
 ;; Keywords: convenience, extensions
-;; Version: 2.1
 
 ;; This file is part of GNU Emacs.
 
 ;;;###autoload
 (defcustom calc-settings-file
   (convert-standard-filename "~/.calc.el")
-  "*File in which to record permanent settings."
+  "File in which to record permanent settings."
   :group 'calc
   :type '(file))
 
     (fortran-mode . fortran)
     (f90-mode . fortran)
     (texinfo-mode . calc-normal-language))
-  "*Alist of major modes with appropriate Calc languages."
+  "Alist of major modes with appropriate Calc languages."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
                 :value-type (symbol :tag "Calc language")))
 
 (defcustom calc-embedded-announce-formula
   "%Embed\n\\(% .*\n\\)*"
-  "*A regular expression which is sure to be followed by a calc-embedded formula."
+  "A regular expression which is sure to be followed by a calc-embedded formula."
   :group 'calc
   :type '(regexp))
 
     (sgml-mode    . "<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
     (xml-mode     . "<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
     (texinfo-mode . "@c Embed\n\\(@c .*\n\\)*"))
-  "*Alist of major modes with appropriate values for `calc-embedded-announce-formula'."
+  "Alist of major modes with appropriate values for `calc-embedded-announce-formula'."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
                 :value-type (regexp :tag "Regexp to announce formula")))
 
 (defcustom calc-embedded-open-formula
   "\\`\\|^\n\\|\\$\\$?\\|\\\\\\[\\|^\\\\begin[^{].*\n\\|^\\\\begin{.*[^x]}.*\n\\|^@.*\n\\|^\\.EQ.*\n\\|\\\\(\\|^%\n\\|^\\.\\\\\"\n"
-  "*A regular expression for the opening delimiter of a formula used by calc-embedded."
+  "A regular expression for the opening delimiter of a formula used by calc-embedded."
   :group 'calc
   :type '(regexp))
 
 (defcustom calc-embedded-close-formula
   "\\'\\|\n$\\|\\$\\$?\\|\\\\]\\|^\\\\end[^{].*\n\\|^\\\\end{.*[^x]}.*\n\\|^@.*\n\\|^\\.EN.*\n\\|\\\\)\\|\n%\n\\|^\\.\\\\\"\n"
-  "*A regular expression for the closing delimiter of a formula used by calc-embedded."
+  "A regular expression for the closing delimiter of a formula used by calc-embedded."
   :group 'calc
   :type '(regexp))
 
 (defcustom calc-embedded-open-close-formula-alist
   nil
-  "*Alist of major modes with pairs of formula delimiters used by calc-embedded."
+  "Alist of major modes with pairs of formula delimiters used by calc-embedded."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
                 :value-type (list (regexp :tag "Opening formula delimiter")
                                   (regexp :tag "Closing formula delimiter"))))
 
-(defcustom calc-embedded-open-word
-  "^\\|[^-+0-9.eE]"
-  "*A regular expression for the opening delimiter of a formula used by calc-embedded-word."
+(defcustom calc-embedded-word-regexp
+  "[-+]?[0-9]+\\(\\.[0-9]+\\)?\\([eE][-+]?[0-9]+\\)?"
+  "A regular expression determining a word for calc-embedded-word."
   :group 'calc
   :type '(regexp))
 
-(defcustom calc-embedded-close-word
-  "$\\|[^-+0-9.eE]"
-  "*A regular expression for the closing delimiter of a formula used by calc-embedded-word."
-  :group 'calc
-  :type '(regexp))
-
-(defcustom calc-embedded-open-close-word-alist
+(defcustom calc-embedded-word-regexp-alist
   nil
-  "*Alist of major modes with pairs of word delimiters used by calc-embedded."
+  "Alist of major modes with word regexps used by calc-embedded-word."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
-                :value-type (list (regexp :tag "Opening word delimiter")
-                                  (regexp :tag "Closing word delimiter"))))
+                :value-type (regexp :tag "Regexp for word")))
 
 (defcustom calc-embedded-open-plain
   "%%% "
-  "*A string which is the opening delimiter for a \"plain\" formula.
+  "A string which is the opening delimiter for a \"plain\" formula.
 If calc-show-plain mode is enabled, this is inserted at the front of
 each formula."
   :group 'calc
@@ -325,7 +317,7 @@ each formula."
 
 (defcustom calc-embedded-close-plain
   " %%%\n"
-  "*A string which is the closing delimiter for a \"plain\" formula.
+  "A string which is the closing delimiter for a \"plain\" formula.
 See calc-embedded-open-plain."
   :group 'calc
   :type '(string))
@@ -342,7 +334,7 @@ See calc-embedded-open-plain."
     (sgml-mode     "<!-- %% " " %% -->\n")
     (xml-mode     "<!-- %% " " %% -->\n")
     (texinfo-mode "@c %% "   " %%\n"))
-  "*Alist of major modes with pairs of delimiters for \"plain\" formulas."
+  "Alist of major modes with pairs of delimiters for \"plain\" formulas."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
                 :value-type (list (string :tag "Opening \"plain\" delimiter")
@@ -350,19 +342,19 @@ See calc-embedded-open-plain."
 
 (defcustom calc-embedded-open-new-formula
   "\n\n"
-  "*A string which is inserted at front of formula by calc-embedded-new-formula."
+  "A string which is inserted at front of formula by calc-embedded-new-formula."
   :group 'calc
   :type '(string))
 
 (defcustom calc-embedded-close-new-formula
   "\n\n"
-  "*A string which is inserted at end of formula by calc-embedded-new-formula."
+  "A string which is inserted at end of formula by calc-embedded-new-formula."
   :group 'calc
   :type '(string))
 
 (defcustom calc-embedded-open-close-new-formula-alist
   nil
-  "*Alist of major modes with pairs of new formula delimiters used by calc-embedded."
+  "Alist of major modes with pairs of new formula delimiters used by calc-embedded."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
                 :value-type (list (string :tag "Opening new formula delimiter")
@@ -370,14 +362,14 @@ See calc-embedded-open-plain."
 
 (defcustom calc-embedded-open-mode
   "% "
-  "*A string which should precede calc-embedded mode annotations.
+  "A string which should precede calc-embedded mode annotations.
 This is not required to be present for user-written mode annotations."
   :group 'calc
   :type '(string))
 
 (defcustom calc-embedded-close-mode
   "\n"
-  "*A string which should follow calc-embedded mode annotations.
+  "A string which should follow calc-embedded mode annotations.
 This is not required to be present for user-written mode annotations."
   :group 'calc
   :type '(string))
@@ -394,37 +386,43 @@ This is not required to be present for user-written mode annotations."
     (sgml-mode    "<!-- " " -->\n")
     (xml-mode     "<!-- " " -->\n")
     (texinfo-mode "@c "   "\n"))
-  "*Alist of major modes with pairs of strings to delimit annotations."
+  "Alist of major modes with pairs of strings to delimit annotations."
   :group 'calc
   :type '(alist :key-type (symbol :tag "Major mode")
                 :value-type (list (string :tag "Opening annotation delimiter")
                                   (string :tag "Closing annotation delimiter"))))
 
 (defcustom calc-gnuplot-name
-  "gnuplot"
-  "*Name of GNUPLOT program, for calc-graph features."
+  (if (eq system-type 'windows-nt) "pgnuplot" "gnuplot")
+  "Name of GNUPLOT program, for calc-graph features."
   :group 'calc
   :type '(string))
 
 (defcustom calc-gnuplot-plot-command
   nil
-  "*Name of command for displaying GNUPLOT output; %s = file name to print."
+  "Name of command for displaying GNUPLOT output; %s = file name to print."
   :group 'calc
   :type '(choice (string) (sexp)))
 
 (defcustom calc-gnuplot-print-command
   "lp %s"
-  "*Name of command for printing GNUPLOT output; %s = file name to print."
+  "Name of command for printing GNUPLOT output; %s = file name to print."
   :group 'calc
   :type '(choice (string) (sexp)))
 
 (defcustom calc-multiplication-has-precedence
   t
-  "*If non-nil, multiplication has precedence over division
+  "If non-nil, multiplication has precedence over division
 in normal mode."
   :group 'calc
   :type 'boolean)
 
+(defcustom calc-undo-length 
+  100
+  "The number of undo steps that will be preserved when Calc is quit."
+  :group 'calc
+  :type 'integer)
+
 (defvar calc-bug-address "jay.p.belanger@gmail.com"
   "Address of the maintainer of Calc, for use by `report-calc-bug'.")
 
@@ -901,7 +899,6 @@ Used by `calc-user-invocation'.")
 (put 'math-underflow 'error-conditions '(error math-underflow calc-error))
 (put 'math-underflow 'error-message "Floating-point underflow occurred")
 
-(defconst calc-version "2.1")
 (defvar calc-trail-pointer nil
   "The \"current\" entry in trail buffer.")
 (defvar calc-trail-overlay nil
@@ -912,6 +909,8 @@ Used by `calc-user-invocation'.")
   "The list of recent undo operations.")
 (defvar calc-main-buffer nil
   "A pointer to Calculator buffer.")
+(defvar calc-buffer-list nil
+  "A list of all Calc buffers.")
 (defvar calc-trail-buffer nil
   "A pointer to Calc Trail buffer.")
 (defvar calc-why nil
@@ -1023,17 +1022,21 @@ Used by `calc-user-invocation'.")
     (define-key map "\"" 'calc-auto-algebraic-entry)
     (define-key map "\t" 'calc-roll-down)
     (define-key map "\M-\t" 'calc-roll-up)
+    (define-key map "\C-x\C-t" 'calc-transpose-lines)
     (define-key map "\C-m" 'calc-enter)
     (define-key map "\M-\C-m" 'calc-last-args-stub)
     (define-key map "\C-j" 'calc-over)
     (define-key map "\C-y" 'calc-yank)
     (define-key map [mouse-2] 'calc-yank)
-      
+
     (mapc (lambda (x) (define-key map (char-to-string x) 'undefined))
           "lOW")
     (mapc (lambda (x) (define-key map (char-to-string x) 'calc-missing-key))
           (concat "ABCDEFGHIJKLMNPQRSTUVXZabcdfghjkmoprstuvwxyz"
-                  ":\\|!()[]<>{},;=~`\C-k\M-k\C-w\M-w\C-_"))
+                  ":\\|!()[]<>{},;=~`\C-k\C-w\C-_"))
+    (define-key map "\M-w" 'calc-missing-key)
+    (define-key map "\M-k" 'calc-missing-key)
+    (define-key map "\M-\C-w" 'calc-missing-key)
     (mapc (lambda (x) (define-key map (char-to-string x) 'calcDigit-start))
           "_0123456789.#@")
     map)
@@ -1095,11 +1098,17 @@ Used by `calc-user-invocation'.")
 (defvar calc-dispatch-map
   (let ((map (make-keymap)))
     (mapc (lambda (x)
-            (define-key map (char-to-string (car x)) (cdr x))
-            (when (string-match "abcdefhijklnopqrstuwxyz"
-                                (char-to-string (car x)))
-              (define-key map (char-to-string (- (car x) ?a -1)) (cdr x)))
-            (define-key map (format "\e%c" (car x)) (cdr x)))
+           (let* ((x-chr (car x))
+                  (x-str (char-to-string x-chr))
+                  (x-def (cdr x)))
+             (define-key map x-str x-def)
+             (when (string-match "[a-z]" x-str)
+               ;; Map upper case char to same definition.
+               (define-key map (upcase x-str) x-def)
+               (unless (string-match "[gmv]" x-str)
+                 ;; Map control prefixed char to same definition.
+                 (define-key map (vector (list 'control x-chr)) x-def)))
+             (define-key map (format "\e%c" x-chr) x-def)))
           '( ( ?a . calc-embedded-activate )
              ( ?b . calc-big-or-small )
              ( ?c . calc )
@@ -1201,10 +1210,66 @@ Used by `calc-user-invocation'.")
 
 (defvar calc-alg-map) ; Defined in calc-ext.el
 
-(defun calc-version ()
-  "Return version of this version of Calc."
-  (interactive)
-  (message "Calc version %s" calc-version))
+
+(defvar calc-embedded-modes) ; Defined in calc-embed.el
+(defvar calc-override-minor-modes) ; Defined in calc-embed.el
+(defun calc-kill-stack-buffer ()
+  "Check to see if user wants to kill the Calc stack buffer.
+This will look for buffers using the Calc buffer for embedded mode,
+and inform the user if there are any.
+If the user wants to kill the Calc buffer, this will remove
+embedded information from the appropriate buffers and tidy up
+the trail buffer."
+  (let ((cb (current-buffer))
+        (info-list nil)
+        (buflist)
+;        (plural nil)
+        (cea calc-embedded-active))
+    ;; Get a list of all buffers using this buffer for
+    ;; embedded Calc.
+    (while cea
+      (when (and (eq cb (aref (nth 1 (car cea)) 1))
+                 (buffer-name (car (car cea))))
+        (setq info-list (cons (car cea) info-list)))
+      (setq cea (cdr cea)))
+    ;; Eventually, prompt user with a list of buffers using embedded mode.
+    (when (and
+           info-list
+           (yes-or-no-p 
+            (concat "This Calc stack is being used for embedded mode. Kill anyway?")))
+      (while info-list
+        (with-current-buffer (car (car info-list))
+          (when calc-embedded-info
+            (setq calc-embedded-info nil
+                  mode-line-buffer-identification (car calc-embedded-modes)
+                  truncate-lines (nth 2 calc-embedded-modes)
+                  buffer-read-only nil)
+            (use-local-map (nth 1 calc-embedded-modes))
+            (setq minor-mode-overriding-map-alist
+                  (remq calc-override-minor-modes minor-mode-overriding-map-alist))
+            (let ((str mode-line-buffer-identification))
+              (setq mode-line-buffer-identification str))
+            (set-buffer-modified-p (buffer-modified-p))))
+        (setq calc-embedded-active
+              (delete (car info-list) calc-embedded-active))
+        (setq info-list (cdr info-list))))
+    (if (not info-list)
+        (progn
+          (setq calc-buffer-list (delete cb calc-buffer-list))
+          (with-current-buffer calc-trail-buffer
+            (if (eq cb calc-main-buffer)
+                ;; If there are other Calc stacks, make another one
+                ;; the calc-main-buffer ...
+                (if calc-buffer-list
+                    (setq calc-main-buffer (car calc-buffer-list))
+                  ;; ... otherwise kill the trail and its windows.
+                  (let ((wl (get-buffer-window-list calc-trail-buffer)))
+                    (while wl
+                      (delete-window (car wl))
+                      (setq wl (cdr wl))))
+                  (kill-buffer calc-trail-buffer)
+                  (setq calc-trail-buffer nil))))
+          t))))
 
 (defun calc-mode ()
   "Calculator major mode.
@@ -1247,6 +1312,9 @@ Notations:  3.14e6     3.14 * 10^6
   (make-local-variable 'overlay-arrow-position)
   (make-local-variable 'overlay-arrow-string)
   (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
+  (add-hook 'kill-buffer-query-functions
+            'calc-kill-stack-buffer
+            t t)
   (setq truncate-lines t)
   (setq buffer-read-only t)
   (setq major-mode 'calc-mode)
@@ -1273,7 +1341,9 @@ Notations:  3.14e6     3.14 * 10^6
   (run-mode-hooks 'calc-mode-hook)
   (calc-refresh t)
   (calc-set-mode-line)
-  (calc-check-defines))
+  (calc-check-defines)
+  (if calc-buffer-list (setq calc-stack (copy-sequence calc-stack)))
+  (add-to-list 'calc-buffer-list (current-buffer) t))
 
 (defvar calc-check-defines 'calc-check-defines)  ; suitable for run-hooks
 (defun calc-check-defines ()
@@ -1422,21 +1492,31 @@ commands given here will actually operate on the *Calculator* stack."
       (unless (eq major-mode 'calc-mode)
         (calc-create-buffer))
       (run-hooks 'calc-end-hook)
-      (setq calc-undo-list nil calc-redo-list nil)
+      (if (integerp calc-undo-length)
+          (cond
+           ((= calc-undo-length 0)
+            (setq calc-undo-list nil calc-redo-list nil))
+           ((> calc-undo-length 0)
+            (let ((tail (nthcdr (1- calc-undo-length) calc-undo-list)))
+              (if tail (setcdr tail nil)))
+            (setq calc-redo-list nil))))
       (mapc (function (lambda (v) (set-default v (symbol-value v))))
            calc-local-var-list)
       (let ((buf (current-buffer))
             (win (get-buffer-window (current-buffer)))
             (kbuf (get-buffer "*Calc Keypad*")))
         (delete-windows-on (calc-trail-buffer))
+        ;; The next few lines will set `calc-window-height' so that the
+        ;; next time Calc is called, the window will be the same size
+        ;; as the current window.
         (if (and win
                  (< (window-height win) (1- (frame-height)))
-                 (= (window-width win) (frame-width))  ; avoid calc-keypad
+                 (window-full-width-p win) ; avoid calc-keypad
                  (not (get-buffer-window "*Calc Keypad*")))
             (setq calc-window-height (- (window-height win) 2)))
         (progn
           (delete-windows-on buf)
-          (delete-windows-on kbuf))
+          (and kbuf (delete-windows-on kbuf)))
         (bury-buffer buf)
         (bury-buffer calc-trail-buffer)
         (and kbuf (bury-buffer kbuf))))))
@@ -1535,11 +1615,13 @@ See calc-keypad for details."
       (and (memq 'position-point calc-command-flags)
           (if (eq major-mode 'calc-mode)
               (progn
-                (goto-line calc-final-point-line)
+                (goto-char (point-min))
+                (forward-line (1- calc-final-point-line))
                 (move-to-column calc-final-point-column))
             (save-current-buffer
               (calc-select-buffer)
-              (goto-line calc-final-point-line)
+              (goto-char (point-min))
+              (forward-line (1- calc-final-point-line))
               (move-to-column calc-final-point-column))))
       (unless (memq 'keep-flags calc-command-flags)
        (save-excursion
@@ -1592,7 +1674,9 @@ See calc-keypad for details."
           (figs (nth 1 calc-float-format))
           (new-mode-string
            (format "Calc%s%s: %d %s %-14s"
-                   (if calc-embedded-info "Embed" "")
+                   (if (and calc-embedded-info
+                             (eq (aref calc-embedded-info 1) (current-buffer)))
+                        "Embed" "")
                    (if (and (> (length (buffer-name)) 12)
                             (equal (substring (buffer-name) 0 12)
                                    "*Calculator*"))
@@ -1937,7 +2021,8 @@ See calc-keypad for details."
           (eq (marker-buffer calc-trail-pointer) calc-trail-buffer))
       (save-excursion
        (set-buffer calc-trail-buffer)
-       (goto-line 2)
+       (goto-char (point-min))
+       (forward-line 1)
        (setq calc-trail-pointer (point-marker))))
   calc-trail-buffer)
 
@@ -2149,7 +2234,7 @@ See calc-keypad for details."
   (interactive)
   (calc-wrapper
    (if (or calc-algebraic-mode
-          (and (> calc-number-radix 14) (eq last-command-char ?e)))
+          (and (> calc-number-radix 14) (eq last-command-event ?e)))
        (calc-alg-digit-entry)
      (calc-unread-command)
      (setq calc-aborted-prefix nil)
@@ -2198,11 +2283,11 @@ See calc-keypad for details."
       (progn
        (beep)
        (calc-temp-minibuffer-message " [Bad format]"))
-    (or (memq last-command-char '(32 13))
+    (or (memq last-command-event '(32 13))
        (progn (setq prefix-arg current-prefix-arg)
-              (calc-unread-command (if (and (eq last-command-char 27)
-                                            (>= last-input-char 128))
-                                       last-input-char
+              (calc-unread-command (if (and (eq last-command-event 27)
+                                            (>= last-input-event 128))
+                                       last-input-event
                                      nil))))
     (exit-minibuffer)))
 
@@ -2215,38 +2300,38 @@ See calc-keypad for details."
 (defun calcDigit-key ()
   (interactive)
   (goto-char (point-max))
-  (if (or (and (memq last-command-char '(?+ ?-))
+  (if (or (and (memq last-command-event '(?+ ?-))
               (> (buffer-size) 0)
               (/= (preceding-char) ?e))
-         (and (memq last-command-char '(?m ?s))
+         (and (memq last-command-event '(?m ?s))
               (not (calc-minibuffer-contains "[-+]?[0-9]+\\.?0*[@oh].*"))
               (not (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*"))))
       (calcDigit-nondigit)
     (if (calc-minibuffer-contains "\\([-+]?\\|.* \\)\\'")
-       (cond ((memq last-command-char '(?. ?@)) (insert "0"))
-             ((and (memq last-command-char '(?o ?h ?m))
+       (cond ((memq last-command-event '(?. ?@)) (insert "0"))
+             ((and (memq last-command-event '(?o ?h ?m))
                    (not (calc-minibuffer-contains ".*#.*"))) (insert "0"))
-             ((memq last-command-char '(?: ?e)) (insert "1"))
-             ((eq last-command-char ?#)
+             ((memq last-command-event '(?: ?e)) (insert "1"))
+             ((eq last-command-event ?#)
               (insert (int-to-string calc-number-radix)))))
     (if (and (calc-minibuffer-contains "\\([-+]?[0-9]+#\\|[^:]*:\\)\\'")
-            (eq last-command-char ?:))
+            (eq last-command-event ?:))
        (insert "1"))
     (if (and (calc-minibuffer-contains "[-+]?[0-9]+#\\'")
-            (eq last-command-char ?.))
+            (eq last-command-event ?.))
        (insert "0"))
     (if (and (calc-minibuffer-contains "[-+]?0*\\([2-9]\\|1[0-4]\\)#\\'")
-            (eq last-command-char ?e))
+            (eq last-command-event ?e))
        (insert "1"))
-    (if (or (and (memq last-command-char '(?h ?o ?m ?s ?p))
+    (if (or (and (memq last-command-event '(?h ?o ?m ?s ?p))
                 (calc-minibuffer-contains ".*#.*"))
-           (and (eq last-command-char ?e)
+           (and (eq last-command-event ?e)
                 (calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
-           (and (eq last-command-char ?n)
+           (and (eq last-command-event ?n)
                 (calc-minibuffer-contains "[-+]?\\(2[4-9]\\|[3-9][0-9]\\)#.*")))
-       (setq last-command-char (upcase last-command-char)))
+       (setq last-command-event (upcase last-command-event)))
     (cond
-     ((memq last-command-char '(?_ ?n))
+     ((memq last-command-event '(?_ ?n))
       (goto-char (minibuffer-prompt-end))
       (if (and (search-forward " +/- " nil t)
               (not (search-forward "e" nil t)))
@@ -2259,7 +2344,7 @@ See calc-keypad for details."
            (delete-char 1)
          (insert "-")))
       (goto-char (point-max)))
-     ((eq last-command-char ?p)
+     ((eq last-command-event ?p)
       (if (or (calc-minibuffer-contains ".*\\+/-.*")
              (calc-minibuffer-contains ".*mod.*")
              (calc-minibuffer-contains ".*#.*")
@@ -2268,7 +2353,7 @@ See calc-keypad for details."
        (if (not (calc-minibuffer-contains ".* \\'"))
            (insert " "))
        (insert "+/- ")))
-     ((and (eq last-command-char ?M)
+     ((and (eq last-command-event ?M)
           (not (calc-minibuffer-contains
                 "[-+]?\\(2[3-9]\\|[3-9][0-9]\\)#.*")))
       (if (or (calc-minibuffer-contains ".*\\+/-.*")
@@ -2283,25 +2368,25 @@ See calc-keypad for details."
              (insert " "))
          (insert "mod "))))
      (t
-      (insert (char-to-string last-command-char))
+      (insert (char-to-string last-command-event))
       (if (or (and (calc-minibuffer-contains "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9][0-9]?\\)#[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\)?\\|.[0-9a-zA-Z]*\\(e[-+]?[0-9]*\\)?\\)?\\'")
                   (let ((radix (string-to-number
                                 (buffer-substring
                                  (match-beginning 2) (match-end 2)))))
                     (and (>= radix 2)
                          (<= radix 36)
-                         (or (memq last-command-char '(?# ?: ?. ?e ?+ ?-))
+                         (or (memq last-command-event '(?# ?: ?. ?e ?+ ?-))
                              (let ((dig (math-read-radix-digit
-                                         (upcase last-command-char))))
+                                         (upcase last-command-event))))
                                (and dig
                                     (< dig radix)))))))
              (calc-minibuffer-contains
               "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9]+\\.?0*[@oh] *\\)?\\([0-9]+\\.?0*['m] *\\)?[0-9]*\\(\\.?[0-9]*\\(e[-+]?[0-3]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?\\)?\\|[0-9]:\\([0-9]+:\\)?[0-9]*\\)?[\"s]?\\'"))
-         (if (and (memq last-command-char '(?@ ?o ?h ?\' ?m))
+         (if (and (memq last-command-event '(?@ ?o ?h ?\' ?m))
                   (string-match " " calc-hms-format))
              (insert " "))
        (if (and (eq this-command last-command)
-                (eq last-command-char ?.))
+                (eq last-command-event ?.))
            (progn
              (require 'calc-ext)
              (calc-digit-dots))
@@ -2309,7 +2394,7 @@ See calc-keypad for details."
          (beep)
          (calc-temp-minibuffer-message " [Bad format]"))))))
   (setq calc-prev-prev-char calc-prev-char
-       calc-prev-char last-command-char))
+       calc-prev-char last-command-event))
 
 
 (defun calcDigit-backspace ()
@@ -2326,7 +2411,7 @@ See calc-keypad for details."
        (t (backward-delete-char 1)))
   (if (= (calc-minibuffer-size) 0)
       (progn
-       (setq last-command-char 13)
+       (setq last-command-event 13)
        (calcDigit-nondigit))))
 
 
@@ -3452,7 +3537,7 @@ largest Emacs integer.")
 
 
 ;;; Parse a simple number in string form.   [N X] [Public]
-(defun math-read-number (s)
+(defun math-read-number (s &optional decimal)
   "Convert the string S into a Calc number."
   (math-normalize
    (cond
@@ -3460,9 +3545,10 @@ largest Emacs integer.")
     ;; Integers (most common case)
     ((string-match "\\` *\\([0-9]+\\) *\\'" s)
      (let ((digs (math-match-substring s 1)))
-       (if (and (eq calc-language 'c)
+       (if (and (memq calc-language calc-lang-c-type-hex)
                (> (length digs) 1)
-               (eq (aref digs 0) ?0))
+               (eq (aref digs 0) ?0)
+                (null decimal))
           (math-read-number (concat "8#" digs))
         (if (<= (length digs) (* 2 math-bignum-digit-length))
             (string-to-number digs)
@@ -3489,8 +3575,8 @@ largest Emacs integer.")
           (frac (math-match-substring s 2)))
        (let ((ilen (length int))
             (flen (length frac)))
-        (let ((int (if (> ilen 0) (math-read-number int) 0))
-              (frac (if (> flen 0) (math-read-number frac) 0)))
+        (let ((int (if (> ilen 0) (math-read-number int t) 0))
+              (frac (if (> flen 0) (math-read-number frac t) 0)))
           (and int frac (or (> ilen 0) (> flen 0))
                (list 'float
                      (math-add (math-scale-int int flen) frac)
@@ -3500,7 +3586,7 @@ largest Emacs integer.")
     ((string-match "^\\(.*\\)[eE]\\([-+]?[0-9]+\\)$" s)
      (let ((mant (math-match-substring s 1))
           (exp (math-match-substring s 2)))
-       (let ((mant (if (> (length mant) 0) (math-read-number mant) 1))
+       (let ((mant (if (> (length mant) 0) (math-read-number mant t) 1))
             (exp (if (<= (length exp) (if (memq (aref exp 0) '(?+ ?-)) 8 7))
                      (string-to-number exp))))
         (and mant exp (Math-realp mant) (> exp -4000000) (< exp 4000000)
@@ -3660,6 +3746,17 @@ Also looks for the equivalent TeX words, \\gets and \\evalto."
 
 ;;;###autoload
 (defmacro defmath (func args &rest body)   ;  [Public]
+  "Define Calc function.
+
+Like `defun' except that code in the body of the definition can
+make use of the full range of Calc data types and the usual
+arithmetic operations are converted to their Calc equivalents.
+
+The prefix `calcFunc-' is added to the specified name to get the
+actual Lisp function name.
+
+See Info node `(calc)Defining Functions'."
+  (declare (doc-string 3))
   (require 'calc-ext)
   (math-do-defmath func args body))