Merge from emacs--rel--22
[bpt/emacs.git] / lisp / calc / calc-aent.el
index 54d1757..e640eb5 100644 (file)
@@ -1,42 +1,42 @@
 ;;; calc-aent.el --- algebraic entry functions for Calc
 
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007 Free Software Foundation, Inc.
 
 ;; Author: Dave Gillespie <daveg@synaptics.com>
-;; Maintainers: D. Goel <deego@gnufans.org>
-;;              Colin Walters <walters@debian.org>
+;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
 
 ;; This file is part of GNU Emacs.
 
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+
 ;; GNU Emacs is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY.  No author or distributor
-;; accepts responsibility to anyone for the consequences of using it
-;; or for whether it serves any particular purpose or works at all,
-;; unless he says so in writing.  Refer to the GNU Emacs General Public
-;; License for full details.
-
-;; Everyone is granted permission to copy, modify and redistribute
-;; GNU Emacs, but only under the conditions described in the
-;; GNU Emacs General Public License.   A copy of this license is
-;; supposed to have been given to you along with GNU Emacs so you
-;; can know your rights and responsibilities.  It should be in a
-;; file named COPYING.  Among other things, the copyright notice
-;; and this notice must be preserved on all copies.
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 ;;; Code:
 
 ;; This file is autoloaded from calc.el.
-(require 'calc)
 
+(require 'calc)
 (require 'calc-macs)
-(eval-when-compile '(require calc-macs))
-
-(defun calc-Need-calc-aent () nil)
 
+(defvar calc-quick-calc-history nil
+  "The history list for quick-calc.")
 
 (defun calc-do-quick-calc ()
+  (require 'calc-ext)
   (calc-check-defines)
   (if (eq major-mode 'calc-mode)
       (calc-algebraic-entry t)
               (enable-recursive-minibuffers t)
               (calc-language (if (memq calc-language '(nil big))
                                  'flat calc-language))
-              (entry (calc-do-alg-entry "" "Quick calc: " t))
-              (alg-exp (mapcar (function
-                                (lambda (x)
-                                  (if (and (not calc-extensions-loaded)
-                                           calc-previous-alg-entry
-                                           (string-match
-                                            "\\`[-0-9._+*/^() ]+\\'"
-                                            calc-previous-alg-entry))
-                                      (calc-normalize x)
-                                    (calc-extensions)
-                                    (math-evaluate-expr x))))
-                               entry)))
+              (entry (calc-do-alg-entry "" "Quick calc: " t 'calc-quick-calc-history))
+              (alg-exp (mapcar 'math-evaluate-expr entry)))
          (when (and (= (length alg-exp) 1)
                     (eq (car-safe (car alg-exp)) 'calcFunc-assign)
                     (= (length (car alg-exp)) 3)
                     (eq (car-safe (nth 1 (car alg-exp))) 'var))
-           (calc-extensions)
            (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
            (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
            (setq alg-exp (list (nth 2 (car alg-exp)))))
@@ -92,7 +81,7 @@
                                  "")
                                ")")))
          (if (and (< (length buf) (frame-width)) (= (length entry) 1)
-                  calc-extensions-loaded)
+                  (featurep 'calc-ext))
              (let ((long (concat (math-format-value (car entry) 1000)
                                  " =>  " buf)))
                (if (<= (length long) (- (frame-width) 8))
          (message "Result: %s" buf)))
       (if (eq last-command-char 10)
          (insert shortbuf)
-       (setq kill-ring (cons shortbuf kill-ring))
-       (when (> (length kill-ring) kill-ring-max)
-         (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
-       (setq kill-ring-yank-pointer kill-ring)))))
+        (kill-new shortbuf)))))
 
 (defun calc-do-calc-eval (str separator args)
   (calc-check-defines)
       (cond
        ((and (consp str) (not (symbolp (car str))))
        (let ((calc-language nil)
-             (math-expr-opers math-standard-opers)
+             (math-expr-opers (math-standard-ops))
              (calc-internal-prec 12)
              (calc-word-size 32)
              (calc-symbolic-mode nil)
        ((eq separator 'eval)
        (eval str))
        ((eq separator 'macro)
-       (calc-extensions)
+       (require 'calc-ext)
        (let* ((calc-buffer (current-buffer))
               (calc-window (get-buffer-window calc-buffer))
               (save-window (selected-window)))
            (and (memq 'clear-message calc-command-flags)
                 (message ""))
            (cond ((eq separator 'pred)
-                  (calc-extensions)
+                  (require 'calc-ext)
                   (if (= (length res) 1)
                       (math-is-true (car res))
                     (calc-eval-error '(0 "Single value expected"))))
                             res (cdr res)))
                     buf)))))))))
 
+(defvar calc-eval-error nil
+  "Determines how calc handles errors.
+If nil, return a list containing the character position of error.
+STRING means return error message as string rather than list.
+The value t means abort and give an error message.")
+
 (defun calc-eval-error (msg)
-  (if (and (boundp 'calc-eval-error)
-          calc-eval-error)
+  (if calc-eval-error
       (if (eq calc-eval-error 'string)
          (nth 1 msg)
        (error "%s" (nth 1 msg)))
   (interactive "P")
   (calc-wrapper
    (let ((calc-language (if prefix nil calc-language))
-        (math-expr-opers (if prefix math-standard-opers math-expr-opers)))
+        (math-expr-opers (if prefix (math-standard-ops) (math-expr-ops))))
      (calc-alg-entry (and auto (char-to-string last-command-char))))))
 
+(defvar calc-alg-entry-history nil
+  "History for algebraic entry.")
+
 (defun calc-alg-entry (&optional initial prompt)
   (let* ((sel-mode nil)
         (calc-dollar-values (mapcar 'calc-get-stack-element
                                     (nthcdr calc-stack-top calc-stack)))
         (calc-dollar-used 0)
         (calc-plain-entry t)
-        (alg-exp (calc-do-alg-entry initial prompt t)))
+        (alg-exp (calc-do-alg-entry initial prompt t 'calc-alg-entry-history)))
     (if (stringp alg-exp)
        (progn
-         (calc-extensions)
+         (require 'calc-ext)
          (calc-alg-edit alg-exp))
       (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
                                     'none
                                   calc-simplify-mode))
             (nvals (mapcar 'calc-normalize alg-exp)))
        (while alg-exp
-         (calc-record (if calc-extensions-loaded (car alg-exp) (car nvals))
+         (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
                       "alg'")
          (calc-pop-push-record-list calc-dollar-used
                                     (and (not (equal (car alg-exp)
                                                      (car nvals)))
-                                         calc-extensions-loaded
+                                         (featurep 'calc-ext)
                                          "")
                                     (list (car nvals)))
          (setq alg-exp (cdr alg-exp)
                calc-dollar-used 0)))
       (calc-handle-whys))))
 
-(defun calc-do-alg-entry (&optional initial prompt no-normalize)
+(defvar calc-alg-ent-map nil
+  "The keymap used for algebraic entry.")
+
+(defvar calc-alg-ent-esc-map nil
+  "The keymap used for escapes in algebraic entry.")
+
+(defvar calc-alg-exp)
+
+(defun calc-do-alg-entry (&optional initial prompt no-normalize history)
   (let* ((calc-buffer (current-buffer))
         (blink-paren-function 'calcAlg-blink-matching-open)
-        (alg-exp 'error))
-    (unless (boundp 'calc-alg-ent-map)
+        (calc-alg-exp 'error))
+    (unless calc-alg-ent-map
       (setq calc-alg-ent-map (copy-keymap minibuffer-local-map))
       (define-key calc-alg-ent-map "'" 'calcAlg-previous)
       (define-key calc-alg-ent-map "`" 'calcAlg-edit)
     (define-key calc-alg-ent-map "\e" nil)
     (if (eq calc-algebraic-mode 'total)
        (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
-      (define-key calc-alg-ent-map "\ep" 'calcAlg-plus-minus)
+      (define-key calc-alg-ent-map "\e+" 'calcAlg-plus-minus)
       (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
       (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
       (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
+      (define-key calc-alg-ent-map "\ep" 'previous-history-element)
+      (define-key calc-alg-ent-map "\en" 'next-history-element)
       (define-key calc-alg-ent-map "\e%" 'self-insert-command))
     (setq calc-aborted-prefix nil)
     (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
                                     (or initial "")
-                                    calc-alg-ent-map nil)))
-      (when (eq alg-exp 'error)
-       (when (eq (car-safe (setq alg-exp (math-read-exprs buf))) 'error)
-         (setq alg-exp nil)))
+                                    calc-alg-ent-map nil history)))
+      (when (eq calc-alg-exp 'error)
+       (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
+         (setq calc-alg-exp nil)))
       (setq calc-aborted-prefix "alg'")
       (or no-normalize
-         (and alg-exp (setq alg-exp (mapcar 'calc-normalize alg-exp))))
-      alg-exp)))
+         (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
+      calc-alg-exp)))
 
 (defun calcAlg-plus-minus ()
   (interactive)
 
 (defun calcAlg-previous ()
   (interactive)
-  (if (calc-minibuffer-contains "\\`\\'")
-      (if calc-previous-alg-entry
-         (insert calc-previous-alg-entry)
-       (beep))
+  (if (calc-minibuffer-contains "\\'")
+      (previous-history-element 1)
     (insert "'")))
 
 (defun calcAlg-equals ()
   (interactive)
   (unwind-protect
       (calcAlg-enter)
-    (if (consp alg-exp)
-       (progn (setq prefix-arg (length alg-exp))
+    (if (consp calc-alg-exp)
+       (progn (setq prefix-arg (length calc-alg-exp))
               (calc-unread-command ?=)))))
 
 (defun calcAlg-escape ()
          (calc-minibuffer-contains
           "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
       (insert "`")
-    (setq alg-exp (minibuffer-contents))
-    (and (> (length alg-exp) 0) (setq calc-previous-alg-entry alg-exp))
+    (setq calc-alg-exp (minibuffer-contents))
     (exit-minibuffer)))
 
+(defvar calc-buffer)
+
 (defun calcAlg-enter ()
   (interactive)
   (let* ((str (minibuffer-contents))
          (calc-temp-minibuffer-message
           (concat " [" (or (nth 2 exp) "Error") "]"))
          (calc-clear-unread-commands))
-      (setq alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
+      (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
                        '((incomplete vec))
                      exp))
-      (and (> (length str) 0) (setq calc-previous-alg-entry str))
       (exit-minibuffer))))
 
 (defun calcAlg-blink-matching-open ()
-  (let ((oldpos (point))
-       (blinkpos nil))
+  (let ((rightpt (point))
+       (leftpt nil)
+        (rightchar (preceding-char))
+        leftchar
+        rightsyntax
+        leftsyntax)
     (save-excursion
       (condition-case ()
-         (setq blinkpos (scan-sexps oldpos -1))
-       (error nil)))
-    (if (and blinkpos
-            (> oldpos (1+ (point-min)))
-            (or (and (= (char-after (1- oldpos)) ?\))
-                     (= (char-after blinkpos) ?\[))
-                (and (= (char-after (1- oldpos)) ?\])
-                     (= (char-after blinkpos) ?\()))
-            (save-excursion
-              (goto-char blinkpos)
-              (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
-       (let ((saved (aref (syntax-table) (char-after blinkpos))))
-         (unwind-protect
-             (progn
-               (aset (syntax-table) (char-after blinkpos)
-                     (+ (logand saved 255)
-                        (lsh (char-after (1- oldpos)) 8)))
-               (blink-matching-open))
-           (aset (syntax-table) (char-after blinkpos) saved)))
+         (setq leftpt (scan-sexps rightpt -1)
+                leftchar (char-after leftpt))
+       (error nil)))
+    (if (and leftpt
+            (or (and (= rightchar ?\))
+                     (= leftchar ?\[))
+                (and (= rightchar ?\])
+                     (= leftchar ?\()))
+            (save-excursion
+              (goto-char leftpt)
+              (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
+       (let ((leftsaved (aref (syntax-table) leftchar))
+              (rightsaved (aref (syntax-table) rightchar)))
+         (unwind-protect
+             (progn
+                (cond ((= leftchar ?\[)
+                       (aset (syntax-table) leftchar (cons 4 ?\)))
+                       (aset (syntax-table) rightchar (cons 5 ?\[)))
+                      (t
+                       (aset (syntax-table) leftchar (cons 4 ?\]))
+                       (aset (syntax-table) rightchar (cons 5 ?\())))
+               (blink-matching-open))
+            (aset (syntax-table) leftchar leftsaved)
+            (aset (syntax-table) rightchar rightsaved)))
       (blink-matching-open))))
 
-
 (defun calc-alg-digit-entry ()
   (calc-alg-entry
    (cond ((eq last-command-char ?e)
         ((eq last-command-char ?@) "0@ ")
         (t (char-to-string last-command-char)))))
 
+;; The variable calc-digit-value is initially declared in calc.el,
+;; but can be set by calcDigit-algebraic and calcDigit-edit.
+(defvar calc-digit-value)
+
 (defun calcDigit-algebraic ()
   (interactive)
   (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
 
 ;;; Algebraic expression parsing.   [Public]
 
-(defun math-read-exprs (exp-str)
-  (let ((exp-pos 0)
-       (exp-old-pos 0)
-       (exp-keep-spaces nil)
-       exp-token exp-data)
+(defvar math-read-replacement-list
+  '(;; Misc symbols
+    ("±" "+/-")  ; plus or minus
+    ("×" "*")    ; multiplication sign
+    ("÷" ":")    ; division sign
+    ("−" "-")    ; subtraction sign
+    ("∕" "/")    ; division sign
+    ("∗" "*")    ; asterisk multiplication
+    ("∞" "inf")  ; infinity symbol
+    ("≤" "<=")
+    ("≥" ">=")
+    ("≦" "<=")
+    ("≧" ">=")
+    ;; fractions
+    ("¼" "(1:4)") ; 1/4
+    ("½" "(1:2)") ; 1/2
+    ("¾" "(3:4)") ; 3/4
+    ("⅓" "(1:3)") ; 1/3
+    ("⅔" "(2:3)") ; 2/3
+    ("⅕" "(1:5)") ; 1/5
+    ("⅖" "(2:5)") ; 2/5
+    ("⅗" "(3:5)") ; 3/5
+    ("⅘" "(4:5)") ; 4/5
+    ("⅙" "(1:6)") ; 1/6
+    ("⅚" "(5:6)") ; 5/6
+    ("⅛" "(1:8)") ; 1/8
+    ("⅜" "(3:8)") ; 3/8
+    ("⅝" "(5:8)") ; 5/8
+    ("⅞" "(7:8)") ; 7/8
+    ("⅟" "1:")    ; 1/...
+    ;; superscripts
+    ("⁰" "0")  ; 0
+    ("¹" "1")  ; 1
+    ("²" "2")  ; 2
+    ("³" "3")  ; 3
+    ("⁴" "4")  ; 4
+    ("⁵" "5")  ; 5
+    ("⁶" "6")  ; 6
+    ("⁷" "7")  ; 7
+    ("⁸" "8")  ; 8
+    ("⁹" "9")  ; 9
+    ("⁺" "+")  ; +
+    ("⁻" "-")  ; -
+    ("⁽" "(")  ; (
+    ("⁾" ")")  ; )
+    ("ⁿ" "n")  ; n
+    ("ⁱ" "i")  ; i
+    ;; subscripts
+    ("₀"  "0")  ; 0
+    ("₁"  "1")  ; 1
+    ("₂"  "2")  ; 2
+    ("₃"  "3")  ; 3
+    ("₄"  "4")  ; 4
+    ("₅"  "5")  ; 5
+    ("₆"  "6")  ; 6
+    ("₇"  "7")  ; 7
+    ("₈"  "8")  ; 8
+    ("₉"  "9")  ; 9
+    ("₊"  "+")  ; +
+    ("₋"  "-")  ; -
+    ("₍"  "(")  ; (
+    ("₎"  ")"))  ; )
+  "A list whose elements (old new) indicate replacements to make
+in Calc algebraic input.")
+
+(defvar math-read-superscripts
+  "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
+  "A string consisting of the superscripts allowed by Calc.")
+
+(defvar math-read-subscripts
+  "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
+  "A string consisting of the subscripts allowed by Calc.")
+
+(defun math-read-preprocess-string (str)
+  "Replace some substrings of STR by Calc equivalents."
+  (setq str
+        (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
+                                  "^(\\&)" str))
+  (setq str
+        (replace-regexp-in-string (concat "[" math-read-subscripts "]+")
+                                  "_(\\&)" str))
+  (let ((rep-list math-read-replacement-list))
+    (while rep-list
+      (setq str
+            (replace-regexp-in-string (nth 0 (car rep-list))
+                                      (nth 1 (car rep-list)) str))
+      (setq rep-list (cdr rep-list))))
+  str)
+
+;; The next few variables are local to math-read-exprs (and math-read-expr
+;; in calc-ext.el), but are set in functions they call.
+
+(defvar math-exp-pos)
+(defvar math-exp-str)
+(defvar math-exp-old-pos)
+(defvar math-exp-token)
+(defvar math-exp-keep-spaces)
+(defvar math-expr-data)
+
+(defun math-read-exprs (math-exp-str)
+  (let ((math-exp-pos 0)
+       (math-exp-old-pos 0)
+       (math-exp-keep-spaces nil)
+       math-exp-token math-expr-data)
+    (setq math-exp-str (math-read-preprocess-string math-exp-str))
     (if calc-language-input-filter
-       (setq exp-str (funcall calc-language-input-filter exp-str)))
-    (while (setq exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" exp-str))
-      (setq exp-str (concat (substring exp-str 0 exp-token) "\\dots"
-                           (substring exp-str (+ exp-token 2)))))
+       (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
+    (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
+      (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
+                           (substring math-exp-str (+ math-exp-token 2)))))
     (math-build-parse-table)
     (math-read-token)
     (let ((val (catch 'syntax (math-read-expr-list))))
       (if (stringp val)
-         (list 'error exp-old-pos val)
-       (if (equal exp-token 'end)
+         (list 'error math-exp-old-pos val)
+       (if (equal math-exp-token 'end)
            val
-         (list 'error exp-old-pos "Syntax error"))))))
+         (list 'error math-exp-old-pos "Syntax error"))))))
 
 (defun math-read-expr-list ()
-  (let* ((exp-keep-spaces nil)
+  (let* ((math-exp-keep-spaces nil)
         (val (list (math-read-expr-level 0)))
         (last val))
-    (while (equal exp-data ",")
+    (while (equal math-expr-data ",")
       (math-read-token)
       (let ((rest (list (math-read-expr-level 0))))
        (setcdr last rest)
 (defvar calc-user-tokens nil)
 (defvar calc-user-token-chars nil)
 
+(defvar math-toks nil
+  "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
+
 (defun math-build-parse-table ()
   (let ((mtab (cdr (assq nil calc-user-parse-tables)))
        (ltab (cdr (assq calc-language calc-user-parse-tables))))
     (or (and (eq mtab calc-last-main-parse-table)
             (eq ltab calc-last-lang-parse-table))
        (let ((p (append mtab ltab))
-             (toks nil))
+             (math-toks nil))
          (setq calc-user-parse-table p)
          (setq calc-user-token-chars nil)
          (while p
            (math-find-user-tokens (car (car p)))
            (setq p (cdr p)))
          (setq calc-user-tokens (mapconcat 'identity
-                                           (sort (mapcar 'car toks)
+                                           (sort (mapcar 'car math-toks)
                                                  (function (lambda (x y)
                                                              (> (length x)
                                                                 (length y)))))
                calc-last-main-parse-table mtab
                calc-last-lang-parse-table ltab)))))
 
-(defun math-find-user-tokens (p)   ; uses "toks"
+(defun math-find-user-tokens (p)
   (while p
     (cond ((and (stringp (car p))
                (or (> (length (car p)) 1) (equal (car p) "$")
                 (setq s (concat "\\<" s)))
             (if (string-match "[a-zA-Z0-9]\\'" s)
                 (setq s (concat s "\\>")))
-            (or (assoc s toks)
+            (or (assoc s math-toks)
                 (progn
-                  (setq toks (cons (list s) toks))
+                  (setq math-toks (cons (list s) math-toks))
                   (or (memq (aref (car p) 0) calc-user-token-chars)
                       (setq calc-user-token-chars
                             (cons (aref (car p) 0)
     (setq p (cdr p))))
 
 (defun math-read-token ()
-  (if (>= exp-pos (length exp-str))
-      (setq exp-old-pos exp-pos
-           exp-token 'end
-           exp-data "\000")
-    (let ((ch (aref exp-str exp-pos)))
-      (setq exp-old-pos exp-pos)
+  (if (>= math-exp-pos (length math-exp-str))
+      (setq math-exp-old-pos math-exp-pos
+           math-exp-token 'end
+           math-expr-data "\000")
+    (let ((ch (aref math-exp-str math-exp-pos)))
+      (setq math-exp-old-pos math-exp-pos)
       (cond ((memq ch '(32 10 9))
-            (setq exp-pos (1+ exp-pos))
-            (if exp-keep-spaces
-                (setq exp-token 'space
-                      exp-data " ")
+            (setq math-exp-pos (1+ math-exp-pos))
+            (if math-exp-keep-spaces
+                (setq math-exp-token 'space
+                      math-expr-data " ")
               (math-read-token)))
            ((and (memq ch calc-user-token-chars)
                  (let ((case-fold-search nil))
-                   (eq (string-match calc-user-tokens exp-str exp-pos)
-                       exp-pos)))
-            (setq exp-token 'punc
-                  exp-data (math-match-substring exp-str 0)
-                  exp-pos (match-end 0)))
+                   (eq (string-match calc-user-tokens math-exp-str math-exp-pos)
+                       math-exp-pos)))
+            (setq math-exp-token 'punc
+                  math-expr-data (math-match-substring math-exp-str 0)
+                  math-exp-pos (match-end 0)))
            ((or (and (>= ch ?a) (<= ch ?z))
                 (and (>= ch ?A) (<= ch ?Z)))
             (string-match (if (memq calc-language '(c fortran pascal maple))
                               "[a-zA-Z0-9_#]*"
                             "[a-zA-Z0-9'#]*")
-                          exp-str exp-pos)
-            (setq exp-token 'symbol
-                  exp-pos (match-end 0)
-                  exp-data (math-restore-dashes
-                            (math-match-substring exp-str 0)))
+                          math-exp-str math-exp-pos)
+            (setq math-exp-token 'symbol
+                  math-exp-pos (match-end 0)
+                  math-expr-data (math-restore-dashes
+                            (math-match-substring math-exp-str 0)))
             (if (eq calc-language 'eqn)
-                (let ((code (assoc exp-data math-eqn-ignore-words)))
+                (let ((code (assoc math-expr-data math-eqn-ignore-words)))
                   (cond ((null code))
                         ((null (cdr code))
                          (math-read-token))
                         ((consp (nth 1 code))
                          (math-read-token)
-                         (if (assoc exp-data (cdr code))
-                             (setq exp-data (format "%s %s"
-                                                    (car code) exp-data))))
+                         (if (assoc math-expr-data (cdr code))
+                             (setq math-expr-data (format "%s %s"
+                                                    (car code) math-expr-data))))
                         ((eq (nth 1 code) 'punc)
-                         (setq exp-token 'punc
-                               exp-data (nth 2 code)))
+                         (setq math-exp-token 'punc
+                               math-expr-data (nth 2 code)))
                         (t
                          (math-read-token)
                          (math-read-token))))))
            ((or (and (>= ch ?0) (<= ch ?9))
                 (and (eq ch '?\.)
-                     (eq (string-match "\\.[0-9]" exp-str exp-pos) exp-pos))
+                     (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
+                          math-exp-pos))
                 (and (eq ch '?_)
-                     (eq (string-match "_\\.?[0-9]" exp-str exp-pos) exp-pos)
-                     (or (eq exp-pos 0)
+                     (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
+                          math-exp-pos)
+                     (or (eq math-exp-pos 0)
                          (and (memq calc-language '(nil flat big unform
-                                                        tex eqn))
+                                                        tex latex eqn))
                               (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
-                                                exp-str (1- exp-pos))
-                                  (1- exp-pos))))))
+                                                math-exp-str (1- math-exp-pos))
+                                  (1- math-exp-pos))))))
             (or (and (eq calc-language 'c)
-                     (string-match "0[xX][0-9a-fA-F]+" exp-str exp-pos))
-                (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\(0*\\([2-9]\\|1[0-4]\\)\\(#\\|\\^\\^\\)[0-9a-dA-D.]+[eE][-+_]?[0-9]+\\|0*\\([2-9]\\|[0-2][0-9]\\|3[0-6]\\)\\(#\\|\\^\\^\\)[0-9a-zA-Z:.]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?" exp-str exp-pos))
-            (setq exp-token 'number
-                  exp-data (math-match-substring exp-str 0)
-                  exp-pos (match-end 0)))
+                     (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
+                (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\(0*\\([2-9]\\|1[0-4]\\)\\(#\\|\\^\\^\\)[0-9a-dA-D.]+[eE][-+_]?[0-9]+\\|0*\\([2-9]\\|[0-2][0-9]\\|3[0-6]\\)\\(#\\|\\^\\^\\)[0-9a-zA-Z:.]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?"
+                               math-exp-str math-exp-pos))
+            (setq math-exp-token 'number
+                  math-expr-data (math-match-substring math-exp-str 0)
+                  math-exp-pos (match-end 0)))
            ((eq ch ?\$)
             (if (and (eq calc-language 'pascal)
                      (eq (string-match
                           "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
-                          exp-str exp-pos)
-                         exp-pos))
-                (setq exp-token 'number
-                      exp-data (math-match-substring exp-str 1)
-                      exp-pos (match-end 1))
-              (if (eq (string-match "\\$\\([1-9][0-9]*\\)" exp-str exp-pos)
-                      exp-pos)
-                  (setq exp-data (- (string-to-int (math-match-substring
-                                                    exp-str 1))))
-                (string-match "\\$+" exp-str exp-pos)
-                (setq exp-data (- (match-end 0) (match-beginning 0))))
-              (setq exp-token 'dollar
-                    exp-pos (match-end 0))))
+                          math-exp-str math-exp-pos)
+                         math-exp-pos))
+                (setq math-exp-token 'number
+                      math-expr-data (math-match-substring math-exp-str 1)
+                      math-exp-pos (match-end 1))
+              (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
+                      math-exp-pos)
+                  (setq math-expr-data (- (string-to-number (math-match-substring
+                                                    math-exp-str 1))))
+                (string-match "\\$+" math-exp-str math-exp-pos)
+                (setq math-expr-data (- (match-end 0) (match-beginning 0))))
+              (setq math-exp-token 'dollar
+                    math-exp-pos (match-end 0))))
            ((eq ch ?\#)
-            (if (eq (string-match "#\\([1-9][0-9]*\\)" exp-str exp-pos)
-                    exp-pos)
-                (setq exp-data (string-to-int
-                                (math-match-substring exp-str 1))
-                      exp-pos (match-end 0))
-              (setq exp-data 1
-                    exp-pos (1+ exp-pos)))
-            (setq exp-token 'hash))
+            (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
+                    math-exp-pos)
+                (setq math-expr-data (string-to-number
+                                (math-match-substring math-exp-str 1))
+                      math-exp-pos (match-end 0))
+              (setq math-expr-data 1
+                    math-exp-pos (1+ math-exp-pos)))
+            (setq math-exp-token 'hash))
            ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
-                              exp-str exp-pos)
-                exp-pos)
-            (setq exp-token 'punc
-                  exp-data (math-match-substring exp-str 0)
-                  exp-pos (match-end 0)))
+                              math-exp-str math-exp-pos)
+                math-exp-pos)
+            (setq math-exp-token 'punc
+                  math-expr-data (math-match-substring math-exp-str 0)
+                  math-exp-pos (match-end 0)))
            ((and (eq ch ?\")
-                 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)" exp-str exp-pos))
+                 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
+                                math-exp-str math-exp-pos))
             (if (eq calc-language 'eqn)
                 (progn
-                  (setq exp-str (copy-sequence exp-str))
-                  (aset exp-str (match-beginning 1) ?\{)
-                  (if (< (match-end 1) (length exp-str))
-                      (aset exp-str (match-end 1) ?\}))
+                  (setq math-exp-str (copy-sequence math-exp-str))
+                  (aset math-exp-str (match-beginning 1) ?\{)
+                  (if (< (match-end 1) (length math-exp-str))
+                      (aset math-exp-str (match-end 1) ?\}))
                   (math-read-token))
-              (setq exp-token 'string
-                    exp-data (math-match-substring exp-str 1)
-                    exp-pos (match-end 0))))
+              (setq math-exp-token 'string
+                    math-expr-data (math-match-substring math-exp-str 1)
+                    math-exp-pos (match-end 0))))
            ((and (= ch ?\\) (eq calc-language 'tex)
-                 (< exp-pos (1- (length exp-str))))
-            (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}" exp-str exp-pos)
-                (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)" exp-str exp-pos))
-            (setq exp-token 'symbol
-                  exp-pos (match-end 0)
-                  exp-data (math-restore-dashes
-                            (math-match-substring exp-str 1)))
-            (let ((code (assoc exp-data math-tex-ignore-words)))
+                 (< math-exp-pos (1- (length math-exp-str))))
+            (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
+                               math-exp-str math-exp-pos)
+                (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
+                               math-exp-str math-exp-pos))
+            (setq math-exp-token 'symbol
+                  math-exp-pos (match-end 0)
+                  math-expr-data (math-restore-dashes
+                            (math-match-substring math-exp-str 1)))
+            (let ((code (assoc math-expr-data math-latex-ignore-words)))
               (cond ((null code))
                     ((null (cdr code))
                      (math-read-token))
                     ((eq (nth 1 code) 'punc)
-                     (setq exp-token 'punc
-                           exp-data (nth 2 code)))
-                    ((and (eq (nth 1 code) 'mat)
-                          (string-match " *{" exp-str exp-pos))
-                     (setq exp-pos (match-end 0)
-                           exp-token 'punc
-                           exp-data "[")
-                     (let ((right (string-match "}" exp-str exp-pos)))
+                     (setq math-exp-token 'punc
+                           math-expr-data (nth 2 code)))
+                      ((and (eq (nth 1 code) 'mat)
+                            (string-match " *{" math-exp-str math-exp-pos))
+                     (setq math-exp-pos (match-end 0)
+                           math-exp-token 'punc
+                           math-expr-data "[")
+                     (let ((right (string-match "}" math-exp-str math-exp-pos)))
                        (and right
-                            (setq exp-str (copy-sequence exp-str))
-                            (aset exp-str right ?\])))))))
+                            (setq math-exp-str (copy-sequence math-exp-str))
+                            (aset math-exp-str right ?\])))))))
+           ((and (= ch ?\\) (eq calc-language 'latex)
+                 (< math-exp-pos (1- (length math-exp-str))))
+            (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
+                               math-exp-str math-exp-pos)
+                 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
+                               math-exp-str math-exp-pos)
+                (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
+                               math-exp-str math-exp-pos))
+            (setq math-exp-token 'symbol
+                  math-exp-pos (match-end 0)
+                  math-expr-data (math-restore-dashes
+                            (math-match-substring math-exp-str 1)))
+            (let ((code (assoc math-expr-data math-tex-ignore-words))
+                   envname)
+              (cond ((null code))
+                    ((null (cdr code))
+                     (math-read-token))
+                    ((eq (nth 1 code) 'punc)
+                     (setq math-exp-token 'punc
+                           math-expr-data (nth 2 code)))
+                     ((and (eq (nth 1 code) 'begenv)
+                           (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
+                      (setq math-exp-pos (match-end 0)
+                            envname (match-string 1 math-exp-str)
+                            math-exp-token 'punc
+                            math-expr-data "[")
+                      (cond ((or (string= envname "matrix")
+                                 (string= envname "bmatrix")
+                                 (string= envname "smallmatrix")
+                                 (string= envname "pmatrix"))
+                             (if (string-match (concat "\\\\end{" envname "}")
+                                               math-exp-str math-exp-pos)
+                                 (setq math-exp-str
+                                       (replace-match "]" t t math-exp-str))
+                               (error "%s" (concat "No closing \\end{" envname "}"))))))
+                      ((and (eq (nth 1 code) 'mat)
+                            (string-match " *{" math-exp-str math-exp-pos))
+                     (setq math-exp-pos (match-end 0)
+                           math-exp-token 'punc
+                           math-expr-data "[")
+                     (let ((right (string-match "}" math-exp-str math-exp-pos)))
+                       (and right
+                            (setq math-exp-str (copy-sequence math-exp-str))
+                            (aset math-exp-str right ?\])))))))
            ((and (= ch ?\.) (eq calc-language 'fortran)
                  (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
-                                   exp-str exp-pos) exp-pos))
-            (setq exp-token 'punc
-                  exp-data (upcase (math-match-substring exp-str 0))
-                  exp-pos (match-end 0)))
+                                   math-exp-str math-exp-pos) math-exp-pos))
+            (setq math-exp-token 'punc
+                  math-expr-data (upcase (math-match-substring math-exp-str 0))
+                  math-exp-pos (match-end 0)))
            ((and (eq calc-language 'math)
-                 (eq (string-match "\\[\\[\\|->\\|:>" exp-str exp-pos)
-                     exp-pos))
-            (setq exp-token 'punc
-                  exp-data (math-match-substring exp-str 0)
-                  exp-pos (match-end 0)))
+                 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
+                     math-exp-pos))
+            (setq math-exp-token 'punc
+                  math-expr-data (math-match-substring math-exp-str 0)
+                  math-exp-pos (match-end 0)))
            ((and (eq calc-language 'eqn)
                  (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
-                                   exp-str exp-pos)
-                     exp-pos))
-            (setq exp-token 'punc
-                  exp-data (math-match-substring exp-str 0)
-                  exp-pos (match-end 0))
-            (and (eq (string-match "\\\\dots\\." exp-str exp-pos) exp-pos)
-                 (setq exp-pos (match-end 0)))
-            (if (memq (aref exp-data 0) '(?~ ?^))
+                                   math-exp-str math-exp-pos)
+                     math-exp-pos))
+            (setq math-exp-token 'punc
+                  math-expr-data (math-match-substring math-exp-str 0)
+                  math-exp-pos (match-end 0))
+            (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
+                      math-exp-pos)
+                 (setq math-exp-pos (match-end 0)))
+            (if (memq (aref math-expr-data 0) '(?~ ?^))
                 (math-read-token)))
-           ((eq (string-match "%%.*$" exp-str exp-pos) exp-pos)
-            (setq exp-pos (match-end 0))
+           ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
+            (setq math-exp-pos (match-end 0))
             (math-read-token))
            (t
-            (if (and (eq ch ?\{) (memq calc-language '(tex eqn)))
+            (if (and (eq ch ?\{) (memq calc-language '(tex latex eqn)))
                 (setq ch ?\())
-            (if (and (eq ch ?\}) (memq calc-language '(tex eqn)))
+            (if (and (eq ch ?\}) (memq calc-language '(tex latex eqn)))
                 (setq ch ?\)))
-            (if (and (eq ch ?\&) (eq calc-language 'tex))
+            (if (and (eq ch ?\&) (memq calc-language '(tex latex)))
                 (setq ch ?\,))
-            (setq exp-token 'punc
-                  exp-data (char-to-string ch)
-                  exp-pos (1+ exp-pos)))))))
+            (setq math-exp-token 'punc
+                  math-expr-data (char-to-string ch)
+                  math-exp-pos (1+ math-exp-pos)))))))
 
+(defconst math-alg-inequalities
+  '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
+               calcFunc-eq calcFunc-neq))
 
 (defun math-read-expr-level (exp-prec &optional exp-term)
-  (let* ((x (math-read-factor)) (first t) op op2)
+  (let* ((math-expr-opers (math-expr-ops))
+         (x (math-read-factor)) 
+         (first t) 
+         op op2)
     (while (and (or (and calc-user-parse-table
                         (setq op (calc-check-user-syntax x exp-prec))
                         (setq x op
                               op '("2x" ident 999999 -1)))
-                   (and (setq op (assoc exp-data math-expr-opers))
+                   (and (setq op (assoc math-expr-data math-expr-opers))
                         (/= (nth 2 op) -1)
                         (or (and (setq op2 (assoc
-                                            exp-data
+                                            math-expr-data
                                             (cdr (memq op math-expr-opers))))
                                  (eq (= (nth 3 op) -1)
                                      (/= (nth 3 op2) -1))
                                  (setq op op2))
                             t))
                    (and (or (eq (nth 2 op) -1)
-                            (memq exp-token '(symbol number dollar hash))
-                            (equal exp-data "(")
-                            (and (equal exp-data "[")
+                            (memq math-exp-token '(symbol number dollar hash))
+                            (equal math-expr-data "(")
+                            (and (equal math-expr-data "[")
                                  (not (eq calc-language 'math))
-                                 (not (and exp-keep-spaces
+                                 (not (and math-exp-keep-spaces
                                            (eq (car-safe x) 'vec)))))
-                        (or (not (setq op (assoc exp-data math-expr-opers)))
+                        (or (not (setq op (assoc math-expr-data math-expr-opers)))
                             (/= (nth 2 op) -1))
                         (or (not calc-user-parse-table)
-                            (not (eq exp-token 'symbol))
+                            (not (eq math-exp-token 'symbol))
                             (let ((p calc-user-parse-table))
                               (while (and p
                                           (or (not (integerp
                                                     (car (car (car p)))))
                                               (not (equal
                                                     (nth 1 (car (car p)))
-                                                    exp-data))))
+                                                    math-expr-data))))
                                 (setq p (cdr p)))
                               (not p)))
                         (setq op (assoc "2x" math-expr-opers))))
-               (not (and exp-term (equal exp-data exp-term)))
+               (not (and exp-term (equal math-expr-data exp-term)))
                (>= (nth 2 op) exp-prec))
       (if (not (equal (car op) "2x"))
          (math-read-token))
       (and (memq (nth 1 op) '(sdev mod))
-          (calc-extensions))
+          (require 'calc-ext))
       (setq x (cond ((consp (nth 1 op))
                     (funcall (car (nth 1 op)) x op))
                    ((eq (nth 3 op) -1)
                    ((and (not first)
                          (memq (nth 1 op) math-alg-inequalities)
                          (memq (car-safe x) math-alg-inequalities))
-                    (calc-extensions)
+                    (require 'calc-ext)
                     (math-composite-inequalities x op))
                    (t (list (nth 1 op)
                             x
            first nil))
     x))
 
+;; calc-arg-values is defined in calc-ext.el, but is used here.
+(defvar calc-arg-values)
+
 (defun calc-check-user-syntax (&optional x prec)
   (let ((p calc-user-parse-table)
        (matches nil)
                           (if x
                               (and (integerp (car rule))
                                    (>= (car rule) prec)
-                                   (equal exp-data
+                                   (equal math-expr-data
                                           (car (setq rule (cdr rule)))))
-                            (equal exp-data (car rule)))))
-                   (let ((save-exp-pos exp-pos)
-                         (save-exp-old-pos exp-old-pos)
-                         (save-exp-token exp-token)
-                         (save-exp-data exp-data))
+                            (equal math-expr-data (car rule)))))
+                   (let ((save-exp-pos math-exp-pos)
+                         (save-exp-old-pos math-exp-old-pos)
+                         (save-exp-token math-exp-token)
+                         (save-exp-data math-expr-data))
                      (or (not (listp
                                (setq matches (calc-match-user-syntax rule))))
                          (let ((args (progn
-                                       (calc-extensions)
+                                       (require 'calc-ext)
                                        calc-arg-values))
                                (conds nil)
                                temp)
                                                  conds)
                                    match (nth 1 match)))
                            (while (and conds match)
-                             (calc-extensions)
+                             (require 'calc-ext)
                              (cond ((eq (car-safe (car conds))
                                         'calcFunc-let)
                                     (setq temp (car conds))
                            (if match
                                (not (setq match (math-multi-subst
                                                  match args matches)))
-                             (setq exp-old-pos save-exp-old-pos
-                                   exp-token save-exp-token
-                                   exp-data save-exp-data
-                                   exp-pos save-exp-pos)))))))
+                             (setq math-exp-old-pos save-exp-old-pos
+                                   math-exp-token save-exp-token
+                                   math-expr-data save-exp-data
+                                   math-exp-pos save-exp-pos)))))))
       (setq p (cdr p)))
     (and p match)))
 
 (defun calc-match-user-syntax (p &optional term)
   (let ((matches nil)
-       (save-exp-pos exp-pos)
-       (save-exp-old-pos exp-old-pos)
-       (save-exp-token exp-token)
-       (save-exp-data exp-data))
+       (save-exp-pos math-exp-pos)
+       (save-exp-old-pos math-exp-old-pos)
+       (save-exp-token math-exp-token)
+       (save-exp-data math-expr-data)
+        m)
     (while (and p
                (cond ((stringp (car p))
-                      (and (equal exp-data (car p))
+                      (and (equal math-expr-data (car p))
                            (progn
                              (math-read-token)
                              t)))
                                         (cons 'vec (and (listp m) m))))))
                       (or (listp m) (not (nth 2 (car p)))
                           (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
-                          (eq exp-token 'end)))
+                          (eq math-exp-token 'end)))
                      (t
                       (setq m (calc-match-user-syntax (nth 1 (car p))
                                                       (car (nth 2 (car p)))))
                           (let ((vec (cons 'vec m))
                                 opos mm)
                             (while (and (listp
-                                         (setq opos exp-pos
+                                         (setq opos math-exp-pos
                                                mm (calc-match-user-syntax
                                                    (or (nth 2 (car p))
                                                        (nth 1 (car p)))
                                                    (car (nth 2 (car p))))))
-                                        (> exp-pos opos))
+                                        (> math-exp-pos opos))
                               (setq vec (nconc vec mm)))
                             (setq matches (nconc matches (list vec))))
                         (and (eq (car (car p)) '*)
                              (setq matches (nconc matches (list '(vec)))))))))
       (setq p (cdr p)))
     (if p
-       (setq exp-pos save-exp-pos
-             exp-old-pos save-exp-old-pos
-             exp-token save-exp-token
-             exp-data save-exp-data
+       (setq math-exp-pos save-exp-pos
+             math-exp-old-pos save-exp-old-pos
+             math-exp-token save-exp-token
+             math-expr-data save-exp-data
              matches "Failed"))
     matches))
 
-(defconst math-alg-inequalities
-  '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
-               calcFunc-eq calcFunc-neq))
-
 (defun math-remove-dashes (x)
   (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
       (math-remove-dashes
 
 (defun math-read-if (cond op)
   (let ((then (math-read-expr-level 0)))
-    (or (equal exp-data ":")
+    (or (equal math-expr-data ":")
        (throw 'syntax "Expected ':'"))
     (math-read-token)
     (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
 
 (defun math-factor-after ()
-  (let ((exp-pos exp-pos)
-       exp-old-pos exp-token exp-data)
+  (let ((math-exp-pos math-exp-pos)
+       math-exp-old-pos math-exp-token math-expr-data)
     (math-read-token)
-    (or (memq exp-token '(number symbol dollar hash string))
-       (and (assoc exp-data '(("-") ("+") ("!") ("|") ("/")))
-            (assoc (concat "u" exp-data) math-expr-opers))
-       (eq (nth 2 (assoc exp-data math-expr-opers)) -1)
-       (assoc exp-data '(("(") ("[") ("{"))))))
+    (or (memq math-exp-token '(number symbol dollar hash string))
+       (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
+            (assoc (concat "u" math-expr-data) math-expr-opers))
+       (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
+       (assoc math-expr-data '(("(") ("[") ("{"))))))
 
 (defun math-read-factor ()
-  (let (op)
-    (cond ((eq exp-token 'number)
-          (let ((num (math-read-number exp-data)))
+  (let ((math-expr-opers (math-expr-ops))
+        op)
+    (cond ((eq math-exp-token 'number)
+          (let ((num (math-read-number math-expr-data)))
             (if (not num)
                 (progn
-                  (setq exp-old-pos exp-pos)
+                  (setq math-exp-old-pos math-exp-pos)
                   (throw 'syntax "Bad format")))
             (math-read-token)
             (if (and math-read-expr-quotes
          ((and calc-user-parse-table
                (setq op (calc-check-user-syntax)))
           op)
-         ((or (equal exp-data "-")
-              (equal exp-data "+")
-              (equal exp-data "!")
-              (equal exp-data "|")
-              (equal exp-data "/"))
-          (setq exp-data (concat "u" exp-data))
+         ((or (equal math-expr-data "-")
+              (equal math-expr-data "+")
+              (equal math-expr-data "!")
+              (equal math-expr-data "|")
+              (equal math-expr-data "/"))
+          (setq math-expr-data (concat "u" math-expr-data))
           (math-read-factor))
-         ((and (setq op (assoc exp-data math-expr-opers))
+         ((and (setq op (assoc math-expr-data math-expr-opers))
                (eq (nth 2 op) -1))
           (if (consp (nth 1 op))
               (funcall (car (nth 1 op)) op)
                           (equal (car op) "u-"))
                      (math-neg val))
                     (t (list (nth 1 op) val))))))
-         ((eq exp-token 'symbol)
-          (let ((sym (intern exp-data)))
+         ((eq math-exp-token 'symbol)
+          (let ((sym (intern math-expr-data)))
             (math-read-token)
-            (if (equal exp-data calc-function-open)
+            (if (equal math-expr-data calc-function-open)
                 (let ((f (assq sym math-expr-function-mapping)))
                   (math-read-token)
                   (if (consp (cdr f))
                       (funcall (car (cdr f)) f sym)
-                    (let ((args (if (or (equal exp-data calc-function-close)
-                                        (eq exp-token 'end))
+                    (let ((args (if (or (equal math-expr-data calc-function-close)
+                                        (eq math-exp-token 'end))
                                     nil
                                   (math-read-expr-list))))
-                      (if (not (or (equal exp-data calc-function-close)
-                                   (eq exp-token 'end)))
+                      (if (not (or (equal math-expr-data calc-function-close)
+                                   (eq math-exp-token 'end)))
                           (throw 'syntax "Expected `)'"))
                       (math-read-token)
                       (if (and (eq calc-language 'fortran) args
-                               (calc-extensions)
+                               (require 'calc-ext)
                                (let ((calc-matrix-mode 'scalar))
                                  (math-known-matrixp
                                   (list 'var sym
                                                          4))
                                              (cdr v))))))
                   (while (and (memq calc-language '(c pascal maple))
-                              (equal exp-data "["))
+                              (equal math-expr-data "["))
                     (math-read-token)
                     (setq val (append (list 'calcFunc-subscr val)
                                       (math-read-expr-list)))
-                    (if (equal exp-data "]")
+                    (if (equal math-expr-data "]")
                         (math-read-token)
                       (throw 'syntax "Expected ']'")))
                   val)))))
-         ((eq exp-token 'dollar)
-          (let ((abs (if (> exp-data 0) exp-data (- exp-data))))
+         ((eq math-exp-token 'dollar)
+          (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
             (if (>= (length calc-dollar-values) abs)
-                (let ((num exp-data))
+                (let ((num math-expr-data))
                   (math-read-token)
                   (setq calc-dollar-used (max calc-dollar-used num))
                   (math-check-complete (nth (1- abs) calc-dollar-values)))
               (throw 'syntax (if calc-dollar-values
                                  "Too many $'s"
                                "$'s not allowed in this context")))))
-         ((eq exp-token 'hash)
+         ((eq math-exp-token 'hash)
           (or calc-hashes-used
               (throw 'syntax "#'s not allowed in this context"))
-          (calc-extensions)
-          (if (<= exp-data (length calc-arg-values))
-              (let ((num exp-data))
+          (require 'calc-ext)
+          (if (<= math-expr-data (length calc-arg-values))
+              (let ((num math-expr-data))
                 (math-read-token)
                 (setq calc-hashes-used (max calc-hashes-used num))
                 (nth (1- num) calc-arg-values))
             (throw 'syntax "Too many # arguments")))
-         ((equal exp-data "(")
-          (let* ((exp (let ((exp-keep-spaces nil))
+         ((equal math-expr-data "(")
+          (let* ((exp (let ((math-exp-keep-spaces nil))
                         (math-read-token)
-                        (if (or (equal exp-data "\\dots")
-                                (equal exp-data "\\ldots"))
+                        (if (or (equal math-expr-data "\\dots")
+                                (equal math-expr-data "\\ldots"))
                             '(neg (var inf var-inf))
                           (math-read-expr-level 0)))))
-            (let ((exp-keep-spaces nil))
+            (let ((math-exp-keep-spaces nil))
               (cond
-               ((equal exp-data ",")
+               ((equal math-expr-data ",")
                 (progn
                   (math-read-token)
                   (let ((exp2 (math-read-expr-level 0)))
                           (if (and exp2 (Math-realp exp) (Math-realp exp2))
                               (math-normalize (list 'cplx exp exp2))
                             (list '+ exp (list '* exp2 '(var i var-i))))))))
-               ((equal exp-data ";")
+               ((equal math-expr-data ";")
                 (progn
                   (math-read-token)
                   (let ((exp2 (math-read-expr-level 0)))
                     (setq exp (if (and exp2 (Math-realp exp)
                                        (Math-anglep exp2))
                                   (math-normalize (list 'polar exp exp2))
-                                (calc-extensions)
+                                (require 'calc-ext)
                                 (list '* exp
                                       (list 'calcFunc-exp
                                             (list '*
                                                   (math-to-radians-2 exp2)
                                                   '(var i var-i)))))))))
-               ((or (equal exp-data "\\dots")
-                    (equal exp-data "\\ldots"))
+               ((or (equal math-expr-data "\\dots")
+                    (equal math-expr-data "\\ldots"))
                 (progn
                   (math-read-token)
-                  (let ((exp2 (if (or (equal exp-data ")")
-                                      (equal exp-data "]")
-                                      (eq exp-token 'end))
+                  (let ((exp2 (if (or (equal math-expr-data ")")
+                                      (equal math-expr-data "]")
+                                      (eq math-exp-token 'end))
                                   '(var inf var-inf)
                                 (math-read-expr-level 0))))
                     (setq exp
                           (list 'intv
-                                (if (equal exp-data ")") 0 1)
+                                (if (equal math-expr-data ")") 0 1)
                                 exp
                                 exp2)))))))
-            (if (not (or (equal exp-data ")")
-                         (and (equal exp-data "]") (eq (car-safe exp) 'intv))
-                         (eq exp-token 'end)))
+            (if (not (or (equal math-expr-data ")")
+                         (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
+                         (eq math-exp-token 'end)))
                 (throw 'syntax "Expected `)'"))
             (math-read-token)
             exp))
-         ((eq exp-token 'string)
-          (calc-extensions)
+         ((eq math-exp-token 'string)
+          (require 'calc-ext)
           (math-read-string))
-         ((equal exp-data "[")
-          (calc-extensions)
+         ((equal math-expr-data "[")
+          (require 'calc-ext)
           (math-read-brackets t "]"))
-         ((equal exp-data "{")
-          (calc-extensions)
+         ((equal math-expr-data "{")
+          (require 'calc-ext)
           (math-read-brackets nil "}"))
-         ((equal exp-data "<")
-          (calc-extensions)
+         ((equal math-expr-data "<")
+          (require 'calc-ext)
           (math-read-angle-brackets))
          (t (throw 'syntax "Expected a number")))))
 
+(provide 'calc-aent)
+
 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
 ;;; calc-aent.el ends here