1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc.el.
35 (defvar calc-quick-calc-history nil
36 "The history list for quick-calc.")
38 (defun calc-do-quick-calc ()
41 (if (eq major-mode 'calc-mode)
42 (calc-algebraic-entry t)
46 (let* ((calc-command-flags nil)
47 (calc-dollar-values calc-quick-prev-results)
49 (enable-recursive-minibuffers t)
50 (calc-language (if (memq calc-language '(nil big))
52 (entry (calc-do-alg-entry "" "Quick calc: " t 'calc-quick-calc-history))
53 (alg-exp (mapcar 'math-evaluate-expr entry)))
54 (when (and (= (length alg-exp) 1)
55 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
56 (= (length (car alg-exp)) 3)
57 (eq (car-safe (nth 1 (car alg-exp))) 'var))
58 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
59 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
60 (setq alg-exp (list (nth 2 (car alg-exp)))))
61 (setq calc-quick-prev-results alg-exp
62 buf (mapconcat (function (lambda (x)
63 (math-format-value x 1000)))
67 (if (and (= (length alg-exp) 1)
68 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
70 (= calc-number-radix 10))
71 (setq buf (concat buf " ("
72 (let ((calc-number-radix 16))
73 (math-format-value (car alg-exp) 1000))
75 (let ((calc-number-radix 8))
76 (math-format-value (car alg-exp) 1000))
77 (if (and (integerp (car alg-exp))
79 (< (car alg-exp) 127))
80 (format ", \"%c\"" (car alg-exp))
83 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
85 (let ((long (concat (math-format-value (car entry) 1000)
87 (if (<= (length long) (- (frame-width) 8))
90 (message "Result: %s" buf)))
91 (if (eq last-command-char 10)
93 (kill-new shortbuf)))))
95 (defun calc-do-calc-eval (str separator args)
101 ((and (consp str) (not (symbolp (car str))))
102 (let ((calc-language nil)
103 (math-expr-opers (math-standard-ops))
104 (calc-internal-prec 12)
106 (calc-symbolic-mode nil)
107 (calc-matrix-mode nil)
108 (calc-angle-mode 'deg)
109 (calc-number-radix 10)
110 (calc-leading-zeros nil)
111 (calc-group-digits nil)
112 (calc-point-char ".")
113 (calc-frac-format '(":" nil))
114 (calc-prefer-frac nil)
115 (calc-hms-format "%s@ %s' %s\"")
116 (calc-date-format '((H ":" mm C SS pp " ")
117 Www " " Mmm " " D ", " YYYY))
118 (calc-float-format '(float 0))
119 (calc-full-float-format '(float 0))
120 (calc-complex-format nil)
121 (calc-matrix-just nil)
122 (calc-full-vectors t)
123 (calc-break-vectors nil)
124 (calc-vector-commas ",")
125 (calc-vector-brackets "[]")
126 (calc-matrix-brackets '(R O))
127 (calc-complex-mode 'cplx)
128 (calc-infinite-mode nil)
129 (calc-display-strings nil)
130 (calc-simplify-mode nil)
131 (calc-display-working-message 'lots)
134 (set (car strp) (nth 1 strp))
135 (setq strp (cdr (cdr strp))))
136 (calc-do-calc-eval (car str) separator args)))
137 ((eq separator 'eval)
139 ((eq separator 'macro)
141 (let* ((calc-buffer (current-buffer))
142 (calc-window (get-buffer-window calc-buffer))
143 (save-window (selected-window)))
147 (select-window calc-window)
148 (calc-execute-kbd-macro str nil (car args)))
149 (and (window-point save-window)
150 (select-window save-window)))
151 (save-window-excursion
152 (select-window (get-largest-window))
153 (switch-to-buffer calc-buffer)
154 (calc-execute-kbd-macro str nil (car args)))))
157 (or (not (integerp str))
159 (calc-pop (min str (calc-stack-size))))
164 (<= str (calc-stack-size))
165 (math-format-value (calc-top-n str (car args)) 1000)))
166 ((eq separator 'rawtop)
169 (<= str (calc-stack-size))
170 (calc-top-n str (car args))))
172 (let* ((calc-command-flags nil)
174 (calc-language (if (memq calc-language '(nil big))
175 'flat calc-language))
176 (calc-dollar-values (mapcar
181 (setq x (math-read-exprs x))
191 (res (if (stringp str)
192 (math-read-exprs str)
195 (if (eq (car res) 'error)
196 (calc-eval-error (cdr res))
197 (setq res (mapcar 'calc-normalize res))
198 (and (memq 'clear-message calc-command-flags)
200 (cond ((eq separator 'pred)
202 (if (= (length res) 1)
203 (math-is-true (car res))
204 (calc-eval-error '(0 "Single value expected"))))
206 (if (= (length res) 1)
208 (calc-eval-error '(0 "Single value expected"))))
209 ((eq separator 'list)
211 ((memq separator '(num rawnum))
212 (if (= (length res) 1)
213 (if (math-constp (car res))
214 (if (eq separator 'num)
215 (math-format-value (car res) 1000)
220 (calc-explain-why (car calc-next-why))
221 "Number expected"))))
222 (calc-eval-error '(0 "Single value expected"))))
223 ((eq separator 'push)
227 (setq buf (concat buf
228 (and buf (or separator ", "))
229 (math-format-value (car res) 1000))
233 (defvar calc-eval-error nil
234 "Determines how calc handles errors.
235 If nil, return a list containing the character position of error.
236 STRING means return error message as string rather than list.
237 The value t means abort and give an error message.")
239 (defun calc-eval-error (msg)
241 (if (eq calc-eval-error 'string)
243 (error "%s" (nth 1 msg)))
247 ;;;; Reading an expression in algebraic form.
249 (defun calc-auto-algebraic-entry (&optional prefix)
251 (calc-algebraic-entry prefix t))
253 (defun calc-algebraic-entry (&optional prefix auto)
256 (let ((calc-language (if prefix nil calc-language))
257 (math-expr-opers (if prefix (math-standard-ops) (math-expr-ops))))
258 (calc-alg-entry (and auto (char-to-string last-command-char))))))
260 (defvar calc-alg-entry-history nil
261 "History for algebraic entry.")
263 (defun calc-alg-entry (&optional initial prompt)
264 (let* ((sel-mode nil)
265 (calc-dollar-values (mapcar 'calc-get-stack-element
266 (nthcdr calc-stack-top calc-stack)))
269 (alg-exp (calc-do-alg-entry initial prompt t 'calc-alg-entry-history)))
270 (if (stringp alg-exp)
273 (calc-alg-edit alg-exp))
274 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
277 (nvals (mapcar 'calc-normalize alg-exp)))
279 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
281 (calc-pop-push-record-list calc-dollar-used
282 (and (not (equal (car alg-exp)
287 (setq alg-exp (cdr alg-exp)
289 calc-dollar-used 0)))
290 (calc-handle-whys))))
292 (defvar calc-alg-ent-map nil
293 "The keymap used for algebraic entry.")
295 (defvar calc-alg-ent-esc-map nil
296 "The keymap used for escapes in algebraic entry.")
298 (defvar calc-alg-exp)
300 (defun calc-do-alg-entry (&optional initial prompt no-normalize history)
301 (let* ((calc-buffer (current-buffer))
302 (blink-paren-function 'calcAlg-blink-matching-open)
303 (calc-alg-exp 'error))
304 (unless calc-alg-ent-map
305 (setq calc-alg-ent-map (copy-keymap minibuffer-local-map))
306 (define-key calc-alg-ent-map "'" 'calcAlg-previous)
307 (define-key calc-alg-ent-map "`" 'calcAlg-edit)
308 (define-key calc-alg-ent-map "\C-m" 'calcAlg-enter)
309 (define-key calc-alg-ent-map "\C-j" 'calcAlg-enter)
311 (setq calc-alg-ent-esc-map (copy-keymap esc-map))
313 (aset (nth 1 calc-alg-ent-esc-map) i 'calcAlg-escape)
315 (define-key calc-alg-ent-map "\e" nil)
316 (if (eq calc-algebraic-mode 'total)
317 (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
318 (define-key calc-alg-ent-map "\e+" 'calcAlg-plus-minus)
319 (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
320 (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
321 (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
322 (define-key calc-alg-ent-map "\ep" 'previous-history-element)
323 (define-key calc-alg-ent-map "\en" 'next-history-element)
324 (define-key calc-alg-ent-map "\e%" 'self-insert-command))
325 (setq calc-aborted-prefix nil)
326 (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
328 calc-alg-ent-map nil history)))
329 (when (eq calc-alg-exp 'error)
330 (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
331 (setq calc-alg-exp nil)))
332 (setq calc-aborted-prefix "alg'")
334 (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
337 (defun calcAlg-plus-minus ()
339 (if (calc-minibuffer-contains ".* \\'")
343 (defun calcAlg-mod ()
345 (if (not (calc-minibuffer-contains ".* \\'"))
347 (if (calc-minibuffer-contains ".* mod +\\'")
348 (if calc-previous-modulo
349 (insert (math-format-flat-expr calc-previous-modulo 0))
353 (defun calcAlg-previous ()
355 (if (calc-minibuffer-contains "\\'")
356 (previous-history-element 1)
359 (defun calcAlg-equals ()
363 (if (consp calc-alg-exp)
364 (progn (setq prefix-arg (length calc-alg-exp))
365 (calc-unread-command ?=)))))
367 (defun calcAlg-escape ()
369 (calc-unread-command)
372 (use-local-map calc-mode-map))
375 (defvar calc-plain-entry nil)
376 (defun calcAlg-edit ()
378 (if (or (not calc-plain-entry)
379 (calc-minibuffer-contains
380 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
382 (setq calc-alg-exp (minibuffer-contents))
387 (defun calcAlg-enter ()
389 (let* ((str (minibuffer-contents))
390 (exp (and (> (length str) 0)
392 (set-buffer calc-buffer)
393 (math-read-exprs str)))))
394 (if (eq (car-safe exp) 'error)
396 (goto-char (minibuffer-prompt-end))
397 (forward-char (nth 1 exp))
399 (calc-temp-minibuffer-message
400 (concat " [" (or (nth 2 exp) "Error") "]"))
401 (calc-clear-unread-commands))
402 (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
407 (defun calcAlg-blink-matching-open ()
408 (let ((rightpt (point))
410 (rightchar (preceding-char))
416 (setq leftpt (scan-sexps rightpt -1)
417 leftchar (char-after leftpt))
420 (or (and (= rightchar ?\))
422 (and (= rightchar ?\])
426 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
427 (let ((leftsaved (aref (syntax-table) leftchar))
428 (rightsaved (aref (syntax-table) rightchar)))
431 (cond ((= leftchar ?\[)
432 (aset (syntax-table) leftchar (cons 4 ?\)))
433 (aset (syntax-table) rightchar (cons 5 ?\[)))
435 (aset (syntax-table) leftchar (cons 4 ?\]))
436 (aset (syntax-table) rightchar (cons 5 ?\())))
437 (blink-matching-open))
438 (aset (syntax-table) leftchar leftsaved)
439 (aset (syntax-table) rightchar rightsaved)))
440 (blink-matching-open))))
442 (defun calc-alg-digit-entry ()
444 (cond ((eq last-command-char ?e)
445 (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e"))
446 ((eq last-command-char ?#) (format "%d#" calc-number-radix))
447 ((eq last-command-char ?_) "-")
448 ((eq last-command-char ?@) "0@ ")
449 (t (char-to-string last-command-char)))))
451 ;; The variable calc-digit-value is initially declared in calc.el,
452 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
453 (defvar calc-digit-value)
455 (defun calcDigit-algebraic ()
457 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
459 (setq calc-digit-value (minibuffer-contents))
462 (defun calcDigit-edit ()
464 (calc-unread-command)
465 (setq calc-digit-value (minibuffer-contents))
469 ;;; Algebraic expression parsing. [Public]
471 (defvar math-read-replacement-list
473 ("±" "+/-") ; plus or minus
474 ("×" "*") ; multiplication sign
475 ("÷" ":") ; division sign
476 ("−" "-") ; subtraction sign
477 ("∕" "/") ; division sign
478 ("∗" "*") ; asterisk multiplication
479 ("∞" "inf") ; infinity symbol
533 "A list whose elements (old new) indicate replacements to make
534 in Calc algebraic input.")
536 (defvar math-read-superscripts
537 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
538 "A string consisting of the superscripts allowed by Calc.")
540 (defvar math-read-subscripts
541 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
542 "A string consisting of the subscripts allowed by Calc.")
544 (defun math-read-preprocess-string (str)
545 "Replace some substrings of STR by Calc equivalents."
547 (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
550 (replace-regexp-in-string (concat "[" math-read-subscripts "]+")
552 (let ((rep-list math-read-replacement-list))
555 (replace-regexp-in-string (nth 0 (car rep-list))
556 (nth 1 (car rep-list)) str))
557 (setq rep-list (cdr rep-list))))
560 ;; The next few variables are local to math-read-exprs (and math-read-expr
561 ;; in calc-ext.el), but are set in functions they call.
563 (defvar math-exp-pos)
564 (defvar math-exp-str)
565 (defvar math-exp-old-pos)
566 (defvar math-exp-token)
567 (defvar math-exp-keep-spaces)
568 (defvar math-expr-data)
570 (defun math-read-exprs (math-exp-str)
571 (let ((math-exp-pos 0)
573 (math-exp-keep-spaces nil)
574 math-exp-token math-expr-data)
575 (setq math-exp-str (math-read-preprocess-string math-exp-str))
576 (if calc-language-input-filter
577 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
578 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
579 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
580 (substring math-exp-str (+ math-exp-token 2)))))
581 (math-build-parse-table)
583 (let ((val (catch 'syntax (math-read-expr-list))))
585 (list 'error math-exp-old-pos val)
586 (if (equal math-exp-token 'end)
588 (list 'error math-exp-old-pos "Syntax error"))))))
590 (defun math-read-expr-list ()
591 (let* ((math-exp-keep-spaces nil)
592 (val (list (math-read-expr-level 0)))
594 (while (equal math-expr-data ",")
596 (let ((rest (list (math-read-expr-level 0))))
601 (defvar calc-user-parse-table nil)
602 (defvar calc-last-main-parse-table nil)
603 (defvar calc-last-lang-parse-table nil)
604 (defvar calc-user-tokens nil)
605 (defvar calc-user-token-chars nil)
607 (defvar math-toks nil
608 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
610 (defun math-build-parse-table ()
611 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
612 (ltab (cdr (assq calc-language calc-user-parse-tables))))
613 (or (and (eq mtab calc-last-main-parse-table)
614 (eq ltab calc-last-lang-parse-table))
615 (let ((p (append mtab ltab))
617 (setq calc-user-parse-table p)
618 (setq calc-user-token-chars nil)
620 (math-find-user-tokens (car (car p)))
622 (setq calc-user-tokens (mapconcat 'identity
623 (sort (mapcar 'car math-toks)
624 (function (lambda (x y)
628 calc-last-main-parse-table mtab
629 calc-last-lang-parse-table ltab)))))
631 (defun math-find-user-tokens (p)
633 (cond ((and (stringp (car p))
634 (or (> (length (car p)) 1) (equal (car p) "$")
635 (equal (car p) "\""))
636 (string-match "[^a-zA-Z0-9]" (car p)))
637 (let ((s (regexp-quote (car p))))
638 (if (string-match "\\`[a-zA-Z0-9]" s)
639 (setq s (concat "\\<" s)))
640 (if (string-match "[a-zA-Z0-9]\\'" s)
641 (setq s (concat s "\\>")))
642 (or (assoc s math-toks)
644 (setq math-toks (cons (list s) math-toks))
645 (or (memq (aref (car p) 0) calc-user-token-chars)
646 (setq calc-user-token-chars
647 (cons (aref (car p) 0)
648 calc-user-token-chars)))))))
650 (math-find-user-tokens (nth 1 (car p)))
651 (or (eq (car (car p)) '\?)
652 (math-find-user-tokens (nth 2 (car p))))))
655 (defun math-read-token ()
656 (if (>= math-exp-pos (length math-exp-str))
657 (setq math-exp-old-pos math-exp-pos
659 math-expr-data "\000")
660 (let ((ch (aref math-exp-str math-exp-pos)))
661 (setq math-exp-old-pos math-exp-pos)
662 (cond ((memq ch '(32 10 9))
663 (setq math-exp-pos (1+ math-exp-pos))
664 (if math-exp-keep-spaces
665 (setq math-exp-token 'space
668 ((and (memq ch calc-user-token-chars)
669 (let ((case-fold-search nil))
670 (eq (string-match calc-user-tokens math-exp-str math-exp-pos)
672 (setq math-exp-token 'punc
673 math-expr-data (math-match-substring math-exp-str 0)
674 math-exp-pos (match-end 0)))
675 ((or (and (>= ch ?a) (<= ch ?z))
676 (and (>= ch ?A) (<= ch ?Z)))
677 (string-match (if (memq calc-language '(c fortran pascal maple))
680 math-exp-str math-exp-pos)
681 (setq math-exp-token 'symbol
682 math-exp-pos (match-end 0)
683 math-expr-data (math-restore-dashes
684 (math-match-substring math-exp-str 0)))
685 (if (eq calc-language 'eqn)
686 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
690 ((consp (nth 1 code))
692 (if (assoc math-expr-data (cdr code))
693 (setq math-expr-data (format "%s %s"
694 (car code) math-expr-data))))
695 ((eq (nth 1 code) 'punc)
696 (setq math-exp-token 'punc
697 math-expr-data (nth 2 code)))
700 (math-read-token))))))
701 ((or (and (>= ch ?0) (<= ch ?9))
703 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
706 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
708 (or (eq math-exp-pos 0)
709 (and (memq calc-language '(nil flat big unform
711 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
712 math-exp-str (1- math-exp-pos))
713 (1- math-exp-pos))))))
714 (or (and (eq calc-language 'c)
715 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
716 (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]+\\)?\"?\\)?"
717 math-exp-str math-exp-pos))
718 (setq math-exp-token 'number
719 math-expr-data (math-match-substring math-exp-str 0)
720 math-exp-pos (match-end 0)))
722 (if (and (eq calc-language 'pascal)
724 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
725 math-exp-str math-exp-pos)
727 (setq math-exp-token 'number
728 math-expr-data (math-match-substring math-exp-str 1)
729 math-exp-pos (match-end 1))
730 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
732 (setq math-expr-data (- (string-to-number (math-match-substring
734 (string-match "\\$+" math-exp-str math-exp-pos)
735 (setq math-expr-data (- (match-end 0) (match-beginning 0))))
736 (setq math-exp-token 'dollar
737 math-exp-pos (match-end 0))))
739 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
741 (setq math-expr-data (string-to-number
742 (math-match-substring math-exp-str 1))
743 math-exp-pos (match-end 0))
744 (setq math-expr-data 1
745 math-exp-pos (1+ math-exp-pos)))
746 (setq math-exp-token 'hash))
747 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
748 math-exp-str math-exp-pos)
750 (setq math-exp-token 'punc
751 math-expr-data (math-match-substring math-exp-str 0)
752 math-exp-pos (match-end 0)))
754 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
755 math-exp-str math-exp-pos))
756 (if (eq calc-language 'eqn)
758 (setq math-exp-str (copy-sequence math-exp-str))
759 (aset math-exp-str (match-beginning 1) ?\{)
760 (if (< (match-end 1) (length math-exp-str))
761 (aset math-exp-str (match-end 1) ?\}))
763 (setq math-exp-token 'string
764 math-expr-data (math-match-substring math-exp-str 1)
765 math-exp-pos (match-end 0))))
766 ((and (= ch ?\\) (eq calc-language 'tex)
767 (< math-exp-pos (1- (length math-exp-str))))
768 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
769 math-exp-str math-exp-pos)
770 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
771 math-exp-str math-exp-pos))
772 (setq math-exp-token 'symbol
773 math-exp-pos (match-end 0)
774 math-expr-data (math-restore-dashes
775 (math-match-substring math-exp-str 1)))
776 (let ((code (assoc math-expr-data math-latex-ignore-words)))
780 ((eq (nth 1 code) 'punc)
781 (setq math-exp-token 'punc
782 math-expr-data (nth 2 code)))
783 ((and (eq (nth 1 code) 'mat)
784 (string-match " *{" math-exp-str math-exp-pos))
785 (setq math-exp-pos (match-end 0)
788 (let ((right (string-match "}" math-exp-str math-exp-pos)))
790 (setq math-exp-str (copy-sequence math-exp-str))
791 (aset math-exp-str right ?\])))))))
792 ((and (= ch ?\\) (eq calc-language 'latex)
793 (< math-exp-pos (1- (length math-exp-str))))
794 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
795 math-exp-str math-exp-pos)
796 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
797 math-exp-str math-exp-pos)
798 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
799 math-exp-str math-exp-pos))
800 (setq math-exp-token 'symbol
801 math-exp-pos (match-end 0)
802 math-expr-data (math-restore-dashes
803 (math-match-substring math-exp-str 1)))
804 (let ((code (assoc math-expr-data math-tex-ignore-words))
809 ((eq (nth 1 code) 'punc)
810 (setq math-exp-token 'punc
811 math-expr-data (nth 2 code)))
812 ((and (eq (nth 1 code) 'begenv)
813 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
814 (setq math-exp-pos (match-end 0)
815 envname (match-string 1 math-exp-str)
818 (cond ((or (string= envname "matrix")
819 (string= envname "bmatrix")
820 (string= envname "smallmatrix")
821 (string= envname "pmatrix"))
822 (if (string-match (concat "\\\\end{" envname "}")
823 math-exp-str math-exp-pos)
825 (replace-match "]" t t math-exp-str))
826 (error "%s" (concat "No closing \\end{" envname "}"))))))
827 ((and (eq (nth 1 code) 'mat)
828 (string-match " *{" math-exp-str math-exp-pos))
829 (setq math-exp-pos (match-end 0)
832 (let ((right (string-match "}" math-exp-str math-exp-pos)))
834 (setq math-exp-str (copy-sequence math-exp-str))
835 (aset math-exp-str right ?\])))))))
836 ((and (= ch ?\.) (eq calc-language 'fortran)
837 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
838 math-exp-str math-exp-pos) math-exp-pos))
839 (setq math-exp-token 'punc
840 math-expr-data (upcase (math-match-substring math-exp-str 0))
841 math-exp-pos (match-end 0)))
842 ((and (eq calc-language 'math)
843 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
845 (setq math-exp-token 'punc
846 math-expr-data (math-match-substring math-exp-str 0)
847 math-exp-pos (match-end 0)))
848 ((and (eq calc-language 'eqn)
849 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
850 math-exp-str math-exp-pos)
852 (setq math-exp-token 'punc
853 math-expr-data (math-match-substring math-exp-str 0)
854 math-exp-pos (match-end 0))
855 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
857 (setq math-exp-pos (match-end 0)))
858 (if (memq (aref math-expr-data 0) '(?~ ?^))
860 ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
861 (setq math-exp-pos (match-end 0))
864 (if (and (eq ch ?\{) (memq calc-language '(tex latex eqn)))
866 (if (and (eq ch ?\}) (memq calc-language '(tex latex eqn)))
868 (if (and (eq ch ?\&) (memq calc-language '(tex latex)))
870 (setq math-exp-token 'punc
871 math-expr-data (char-to-string ch)
872 math-exp-pos (1+ math-exp-pos)))))))
874 (defconst math-alg-inequalities
875 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
876 calcFunc-eq calcFunc-neq))
878 (defun math-read-expr-level (exp-prec &optional exp-term)
879 (let* ((math-expr-opers (math-expr-ops))
880 (x (math-read-factor))
883 (while (and (or (and calc-user-parse-table
884 (setq op (calc-check-user-syntax x exp-prec))
886 op '("2x" ident 999999 -1)))
887 (and (setq op (assoc math-expr-data math-expr-opers))
889 (or (and (setq op2 (assoc
891 (cdr (memq op math-expr-opers))))
892 (eq (= (nth 3 op) -1)
894 (eq (= (nth 3 op2) -1)
895 (not (math-factor-after)))
898 (and (or (eq (nth 2 op) -1)
899 (memq math-exp-token '(symbol number dollar hash))
900 (equal math-expr-data "(")
901 (and (equal math-expr-data "[")
902 (not (eq calc-language 'math))
903 (not (and math-exp-keep-spaces
904 (eq (car-safe x) 'vec)))))
905 (or (not (setq op (assoc math-expr-data math-expr-opers)))
907 (or (not calc-user-parse-table)
908 (not (eq math-exp-token 'symbol))
909 (let ((p calc-user-parse-table))
912 (car (car (car p)))))
914 (nth 1 (car (car p)))
918 (setq op (assoc "2x" math-expr-opers))))
919 (not (and exp-term (equal math-expr-data exp-term)))
920 (>= (nth 2 op) exp-prec))
921 (if (not (equal (car op) "2x"))
923 (and (memq (nth 1 op) '(sdev mod))
925 (setq x (cond ((consp (nth 1 op))
926 (funcall (car (nth 1 op)) x op))
928 (if (eq (nth 1 op) 'ident)
930 (if (eq (nth 1 op) 'closing)
931 (if (eq (nth 2 op) exp-prec)
935 (throw 'syntax "Mismatched delimiters"))
936 (list (nth 1 op) x))))
938 (memq (nth 1 op) math-alg-inequalities)
939 (memq (car-safe x) math-alg-inequalities))
941 (math-composite-inequalities x op))
944 (math-read-expr-level (nth 3 op) exp-term))))
948 ;; calc-arg-values is defined in calc-ext.el, but is used here.
949 (defvar calc-arg-values)
951 (defun calc-check-user-syntax (&optional x prec)
952 (let ((p calc-user-parse-table)
957 (setq rule (car (car p)))
959 (and (integerp (car rule))
961 (equal math-expr-data
962 (car (setq rule (cdr rule)))))
963 (equal math-expr-data (car rule)))))
964 (let ((save-exp-pos math-exp-pos)
965 (save-exp-old-pos math-exp-old-pos)
966 (save-exp-token math-exp-token)
967 (save-exp-data math-expr-data))
969 (setq matches (calc-match-user-syntax rule))))
976 (setq matches (cons x matches)))
977 (setq match (cdr (car p)))
978 (while (and (eq (car-safe match)
980 (= (length match) 3))
981 (setq conds (append (math-flatten-lands
984 match (nth 1 match)))
985 (while (and conds match)
987 (cond ((eq (car-safe (car conds))
989 (setq temp (car conds))
990 (or (= (length temp) 3)
991 (and (= (length temp) 2)
992 (eq (car-safe (nth 1 temp))
994 (= (length (nth 1 temp)) 3)
995 (setq temp (nth 1 temp)))
1003 args (cons (nth 1 temp)
1005 ((and (eq (car-safe (car conds))
1007 (= (length (car conds)) 3))
1008 (setq temp (calcFunc-vmatches
1012 (nth 2 (car conds))))
1015 (while (setq temp (cdr temp))
1016 (setq matches (cons (nth 2 (car temp))
1018 args (cons (nth 1 (car temp))
1021 (or (math-is-true (math-simplify
1026 (setq conds (cdr conds)))
1028 (not (setq match (math-multi-subst
1029 match args matches)))
1030 (setq math-exp-old-pos save-exp-old-pos
1031 math-exp-token save-exp-token
1032 math-expr-data save-exp-data
1033 math-exp-pos save-exp-pos)))))))
1037 (defun calc-match-user-syntax (p &optional term)
1039 (save-exp-pos math-exp-pos)
1040 (save-exp-old-pos math-exp-old-pos)
1041 (save-exp-token math-exp-token)
1042 (save-exp-data math-expr-data)
1045 (cond ((stringp (car p))
1046 (and (equal math-expr-data (car p))
1051 (and (setq m (catch 'syntax
1052 (math-read-expr-level
1055 (if (consp (nth 1 p))
1056 (car (nth 1 (nth 1 p)))
1060 (setq matches (nconc matches (list m)))))
1061 ((eq (car (car p)) '\?)
1062 (setq m (calc-match-user-syntax (nth 1 (car p))))
1067 (cons 'vec (and (listp m) m))))))
1068 (or (listp m) (not (nth 2 (car p)))
1069 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
1070 (eq math-exp-token 'end)))
1072 (setq m (calc-match-user-syntax (nth 1 (car p))
1073 (car (nth 2 (car p)))))
1075 (let ((vec (cons 'vec m))
1078 (setq opos math-exp-pos
1079 mm (calc-match-user-syntax
1082 (car (nth 2 (car p))))))
1083 (> math-exp-pos opos))
1084 (setq vec (nconc vec mm)))
1085 (setq matches (nconc matches (list vec))))
1086 (and (eq (car (car p)) '*)
1087 (setq matches (nconc matches (list '(vec)))))))))
1090 (setq math-exp-pos save-exp-pos
1091 math-exp-old-pos save-exp-old-pos
1092 math-exp-token save-exp-token
1093 math-expr-data save-exp-data
1097 (defun math-remove-dashes (x)
1098 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
1100 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
1103 (defun math-restore-dashes (x)
1104 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
1105 (math-restore-dashes
1106 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
1109 (defun math-read-if (cond op)
1110 (let ((then (math-read-expr-level 0)))
1111 (or (equal math-expr-data ":")
1112 (throw 'syntax "Expected ':'"))
1114 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
1116 (defun math-factor-after ()
1117 (let ((math-exp-pos math-exp-pos)
1118 math-exp-old-pos math-exp-token math-expr-data)
1120 (or (memq math-exp-token '(number symbol dollar hash string))
1121 (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
1122 (assoc (concat "u" math-expr-data) math-expr-opers))
1123 (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
1124 (assoc math-expr-data '(("(") ("[") ("{"))))))
1126 (defun math-read-factor ()
1127 (let ((math-expr-opers (math-expr-ops))
1129 (cond ((eq math-exp-token 'number)
1130 (let ((num (math-read-number math-expr-data)))
1133 (setq math-exp-old-pos math-exp-pos)
1134 (throw 'syntax "Bad format")))
1136 (if (and math-read-expr-quotes
1140 ((and calc-user-parse-table
1141 (setq op (calc-check-user-syntax)))
1143 ((or (equal math-expr-data "-")
1144 (equal math-expr-data "+")
1145 (equal math-expr-data "!")
1146 (equal math-expr-data "|")
1147 (equal math-expr-data "/"))
1148 (setq math-expr-data (concat "u" math-expr-data))
1150 ((and (setq op (assoc math-expr-data math-expr-opers))
1152 (if (consp (nth 1 op))
1153 (funcall (car (nth 1 op)) op)
1155 (let ((val (math-read-expr-level (nth 3 op))))
1156 (cond ((eq (nth 1 op) 'ident)
1158 ((and (Math-numberp val)
1159 (equal (car op) "u-"))
1161 (t (list (nth 1 op) val))))))
1162 ((eq math-exp-token 'symbol)
1163 (let ((sym (intern math-expr-data)))
1165 (if (equal math-expr-data calc-function-open)
1166 (let ((f (assq sym math-expr-function-mapping)))
1169 (funcall (car (cdr f)) f sym)
1170 (let ((args (if (or (equal math-expr-data calc-function-close)
1171 (eq math-exp-token 'end))
1173 (math-read-expr-list))))
1174 (if (not (or (equal math-expr-data calc-function-close)
1175 (eq math-exp-token 'end)))
1176 (throw 'syntax "Expected `)'"))
1178 (if (and (eq calc-language 'fortran) args
1180 (let ((calc-matrix-mode 'scalar))
1185 (symbol-name sym)))))))
1186 (math-parse-fortran-subscr sym args)
1189 (and (= (aref (symbol-name sym) 0) ?\\)
1190 (< (prefix-numeric-value calc-language-option)
1192 (setq sym (intern (substring (symbol-name sym)
1194 (or (string-match "-" (symbol-name sym))
1197 (symbol-name sym))))))
1199 (if math-read-expr-quotes
1201 (let ((val (list 'var
1202 (intern (math-remove-dashes
1204 (if (string-match "-" (symbol-name sym))
1206 (intern (concat "var-"
1207 (symbol-name sym)))))))
1208 (let ((v (assq (nth 1 val) math-expr-variable-mapping)))
1209 (and v (setq val (if (consp (cdr v))
1210 (funcall (car (cdr v)) v val)
1213 (substring (symbol-name (cdr v))
1216 (while (and (memq calc-language '(c pascal maple))
1217 (equal math-expr-data "["))
1219 (setq val (append (list 'calcFunc-subscr val)
1220 (math-read-expr-list)))
1221 (if (equal math-expr-data "]")
1223 (throw 'syntax "Expected ']'")))
1225 ((eq math-exp-token 'dollar)
1226 (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
1227 (if (>= (length calc-dollar-values) abs)
1228 (let ((num math-expr-data))
1230 (setq calc-dollar-used (max calc-dollar-used num))
1231 (math-check-complete (nth (1- abs) calc-dollar-values)))
1232 (throw 'syntax (if calc-dollar-values
1234 "$'s not allowed in this context")))))
1235 ((eq math-exp-token 'hash)
1236 (or calc-hashes-used
1237 (throw 'syntax "#'s not allowed in this context"))
1239 (if (<= math-expr-data (length calc-arg-values))
1240 (let ((num math-expr-data))
1242 (setq calc-hashes-used (max calc-hashes-used num))
1243 (nth (1- num) calc-arg-values))
1244 (throw 'syntax "Too many # arguments")))
1245 ((equal math-expr-data "(")
1246 (let* ((exp (let ((math-exp-keep-spaces nil))
1248 (if (or (equal math-expr-data "\\dots")
1249 (equal math-expr-data "\\ldots"))
1250 '(neg (var inf var-inf))
1251 (math-read-expr-level 0)))))
1252 (let ((math-exp-keep-spaces nil))
1254 ((equal math-expr-data ",")
1257 (let ((exp2 (math-read-expr-level 0)))
1259 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1260 (math-normalize (list 'cplx exp exp2))
1261 (list '+ exp (list '* exp2 '(var i var-i))))))))
1262 ((equal math-expr-data ";")
1265 (let ((exp2 (math-read-expr-level 0)))
1266 (setq exp (if (and exp2 (Math-realp exp)
1268 (math-normalize (list 'polar exp exp2))
1273 (math-to-radians-2 exp2)
1274 '(var i var-i)))))))))
1275 ((or (equal math-expr-data "\\dots")
1276 (equal math-expr-data "\\ldots"))
1279 (let ((exp2 (if (or (equal math-expr-data ")")
1280 (equal math-expr-data "]")
1281 (eq math-exp-token 'end))
1283 (math-read-expr-level 0))))
1286 (if (equal math-expr-data ")") 0 1)
1289 (if (not (or (equal math-expr-data ")")
1290 (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
1291 (eq math-exp-token 'end)))
1292 (throw 'syntax "Expected `)'"))
1295 ((eq math-exp-token 'string)
1298 ((equal math-expr-data "[")
1300 (math-read-brackets t "]"))
1301 ((equal math-expr-data "{")
1303 (math-read-brackets nil "}"))
1304 ((equal math-expr-data "<")
1306 (math-read-angle-brackets))
1307 (t (throw 'syntax "Expected a number")))))
1309 (provide 'calc-aent)
1311 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1312 ;;; calc-aent.el ends here