1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc.el.
34 (defun calc-do-quick-calc ()
36 (if (eq major-mode 'calc-mode)
37 (calc-algebraic-entry t)
41 (let* ((calc-command-flags nil)
42 (calc-dollar-values calc-quick-prev-results)
44 (enable-recursive-minibuffers t)
45 (calc-language (if (memq calc-language '(nil big))
47 (entry (calc-do-alg-entry "" "Quick calc: " t))
48 (alg-exp (mapcar (function
50 (if (and (not (featurep 'calc-ext))
51 calc-previous-alg-entry
53 "\\`[-0-9._+*/^() ]+\\'"
54 calc-previous-alg-entry))
57 (math-evaluate-expr x))))
59 (when (and (= (length alg-exp) 1)
60 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
61 (= (length (car alg-exp)) 3)
62 (eq (car-safe (nth 1 (car alg-exp))) 'var))
64 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
65 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
66 (setq alg-exp (list (nth 2 (car alg-exp)))))
67 (setq calc-quick-prev-results alg-exp
68 buf (mapconcat (function (lambda (x)
69 (math-format-value x 1000)))
73 (if (and (= (length alg-exp) 1)
74 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
76 (= calc-number-radix 10))
77 (setq buf (concat buf " ("
78 (let ((calc-number-radix 16))
79 (math-format-value (car alg-exp) 1000))
81 (let ((calc-number-radix 8))
82 (math-format-value (car alg-exp) 1000))
83 (if (and (integerp (car alg-exp))
85 (< (car alg-exp) 127))
86 (format ", \"%c\"" (car alg-exp))
89 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
91 (let ((long (concat (math-format-value (car entry) 1000)
93 (if (<= (length long) (- (frame-width) 8))
96 (message "Result: %s" buf)))
97 (if (eq last-command-char 10)
99 (kill-new shortbuf)))))
101 (defun calc-do-calc-eval (str separator args)
107 ((and (consp str) (not (symbolp (car str))))
108 (let ((calc-language nil)
109 (math-expr-opers math-standard-opers)
110 (calc-internal-prec 12)
112 (calc-symbolic-mode nil)
113 (calc-matrix-mode nil)
114 (calc-angle-mode 'deg)
115 (calc-number-radix 10)
116 (calc-leading-zeros nil)
117 (calc-group-digits nil)
118 (calc-point-char ".")
119 (calc-frac-format '(":" nil))
120 (calc-prefer-frac nil)
121 (calc-hms-format "%s@ %s' %s\"")
122 (calc-date-format '((H ":" mm C SS pp " ")
123 Www " " Mmm " " D ", " YYYY))
124 (calc-float-format '(float 0))
125 (calc-full-float-format '(float 0))
126 (calc-complex-format nil)
127 (calc-matrix-just nil)
128 (calc-full-vectors t)
129 (calc-break-vectors nil)
130 (calc-vector-commas ",")
131 (calc-vector-brackets "[]")
132 (calc-matrix-brackets '(R O))
133 (calc-complex-mode 'cplx)
134 (calc-infinite-mode nil)
135 (calc-display-strings nil)
136 (calc-simplify-mode nil)
137 (calc-display-working-message 'lots)
140 (set (car strp) (nth 1 strp))
141 (setq strp (cdr (cdr strp))))
142 (calc-do-calc-eval (car str) separator args)))
143 ((eq separator 'eval)
145 ((eq separator 'macro)
147 (let* ((calc-buffer (current-buffer))
148 (calc-window (get-buffer-window calc-buffer))
149 (save-window (selected-window)))
153 (select-window calc-window)
154 (calc-execute-kbd-macro str nil (car args)))
155 (and (window-point save-window)
156 (select-window save-window)))
157 (save-window-excursion
158 (select-window (get-largest-window))
159 (switch-to-buffer calc-buffer)
160 (calc-execute-kbd-macro str nil (car args)))))
163 (or (not (integerp str))
165 (calc-pop (min str (calc-stack-size))))
170 (<= str (calc-stack-size))
171 (math-format-value (calc-top-n str (car args)) 1000)))
172 ((eq separator 'rawtop)
175 (<= str (calc-stack-size))
176 (calc-top-n str (car args))))
178 (let* ((calc-command-flags nil)
180 (calc-language (if (memq calc-language '(nil big))
181 'flat calc-language))
182 (calc-dollar-values (mapcar
187 (setq x (math-read-exprs x))
197 (res (if (stringp str)
198 (math-read-exprs str)
201 (if (eq (car res) 'error)
202 (calc-eval-error (cdr res))
203 (setq res (mapcar 'calc-normalize res))
204 (and (memq 'clear-message calc-command-flags)
206 (cond ((eq separator 'pred)
208 (if (= (length res) 1)
209 (math-is-true (car res))
210 (calc-eval-error '(0 "Single value expected"))))
212 (if (= (length res) 1)
214 (calc-eval-error '(0 "Single value expected"))))
215 ((eq separator 'list)
217 ((memq separator '(num rawnum))
218 (if (= (length res) 1)
219 (if (math-constp (car res))
220 (if (eq separator 'num)
221 (math-format-value (car res) 1000)
226 (calc-explain-why (car calc-next-why))
227 "Number expected"))))
228 (calc-eval-error '(0 "Single value expected"))))
229 ((eq separator 'push)
233 (setq buf (concat buf
234 (and buf (or separator ", "))
235 (math-format-value (car res) 1000))
239 (defvar calc-eval-error nil
240 "Determines how calc handles errors.
241 NIL means return a list containing the character position of error.
242 STRING means return error message as string rather than list.
243 T means abort and give an error message.")
245 (defun calc-eval-error (msg)
247 (if (eq calc-eval-error 'string)
249 (error "%s" (nth 1 msg)))
253 ;;;; Reading an expression in algebraic form.
255 (defun calc-auto-algebraic-entry (&optional prefix)
257 (calc-algebraic-entry prefix t))
259 (defun calc-algebraic-entry (&optional prefix auto)
262 (let ((calc-language (if prefix nil calc-language))
263 (math-expr-opers (if prefix math-standard-opers math-expr-opers)))
264 (calc-alg-entry (and auto (char-to-string last-command-char))))))
266 (defun calc-alg-entry (&optional initial prompt)
267 (let* ((sel-mode nil)
268 (calc-dollar-values (mapcar 'calc-get-stack-element
269 (nthcdr calc-stack-top calc-stack)))
272 (alg-exp (calc-do-alg-entry initial prompt t)))
273 (if (stringp alg-exp)
276 (calc-alg-edit alg-exp))
277 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
280 (nvals (mapcar 'calc-normalize alg-exp)))
282 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
284 (calc-pop-push-record-list calc-dollar-used
285 (and (not (equal (car alg-exp)
290 (setq alg-exp (cdr alg-exp)
292 calc-dollar-used 0)))
293 (calc-handle-whys))))
295 (defvar calc-alg-ent-map nil
296 "The keymap used for algebraic entry.")
298 (defvar calc-alg-ent-esc-map nil
299 "The keymap used for escapes in algebraic entry.")
301 (defvar calc-alg-exp)
303 (defun calc-do-alg-entry (&optional initial prompt no-normalize)
304 (let* ((calc-buffer (current-buffer))
305 (blink-paren-function 'calcAlg-blink-matching-open)
306 (calc-alg-exp 'error))
307 (unless calc-alg-ent-map
308 (setq calc-alg-ent-map (copy-keymap minibuffer-local-map))
309 (define-key calc-alg-ent-map "'" 'calcAlg-previous)
310 (define-key calc-alg-ent-map "`" 'calcAlg-edit)
311 (define-key calc-alg-ent-map "\C-m" 'calcAlg-enter)
312 (define-key calc-alg-ent-map "\C-j" 'calcAlg-enter)
314 (setq calc-alg-ent-esc-map (copy-keymap esc-map))
316 (aset (nth 1 calc-alg-ent-esc-map) i 'calcAlg-escape)
318 (define-key calc-alg-ent-map "\e" nil)
319 (if (eq calc-algebraic-mode 'total)
320 (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
321 (define-key calc-alg-ent-map "\ep" 'calcAlg-plus-minus)
322 (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
323 (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
324 (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
325 (define-key calc-alg-ent-map "\e%" 'self-insert-command))
326 (setq calc-aborted-prefix nil)
327 (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
329 calc-alg-ent-map nil)))
330 (when (eq calc-alg-exp 'error)
331 (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
332 (setq calc-alg-exp nil)))
333 (setq calc-aborted-prefix "alg'")
335 (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
338 (defun calcAlg-plus-minus ()
340 (if (calc-minibuffer-contains ".* \\'")
344 (defun calcAlg-mod ()
346 (if (not (calc-minibuffer-contains ".* \\'"))
348 (if (calc-minibuffer-contains ".* mod +\\'")
349 (if calc-previous-modulo
350 (insert (math-format-flat-expr calc-previous-modulo 0))
354 (defun calcAlg-previous ()
356 (if (calc-minibuffer-contains "\\'")
357 (if calc-previous-alg-entry
358 (insert calc-previous-alg-entry)
362 (defun calcAlg-equals ()
366 (if (consp calc-alg-exp)
367 (progn (setq prefix-arg (length calc-alg-exp))
368 (calc-unread-command ?=)))))
370 (defun calcAlg-escape ()
372 (calc-unread-command)
375 (use-local-map calc-mode-map))
378 (defvar calc-plain-entry nil)
379 (defun calcAlg-edit ()
381 (if (or (not calc-plain-entry)
382 (calc-minibuffer-contains
383 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
385 (setq calc-alg-exp (minibuffer-contents))
386 (and (> (length calc-alg-exp) 0) (setq calc-previous-alg-entry calc-alg-exp))
391 (defun calcAlg-enter ()
393 (let* ((str (minibuffer-contents))
394 (exp (and (> (length str) 0)
396 (set-buffer calc-buffer)
397 (math-read-exprs str)))))
398 (if (eq (car-safe exp) 'error)
400 (goto-char (minibuffer-prompt-end))
401 (forward-char (nth 1 exp))
403 (calc-temp-minibuffer-message
404 (concat " [" (or (nth 2 exp) "Error") "]"))
405 (calc-clear-unread-commands))
406 (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
409 (and (> (length str) 0) (setq calc-previous-alg-entry str))
412 (defun calcAlg-blink-matching-open ()
413 (let ((rightpt (point))
415 (rightchar (preceding-char))
421 (setq leftpt (scan-sexps rightpt -1)
422 leftchar (char-after leftpt))
425 (or (and (= rightchar ?\))
427 (and (= rightchar ?\])
431 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
432 (let ((leftsaved (aref (syntax-table) leftchar))
433 (rightsaved (aref (syntax-table) rightchar)))
436 (cond ((= leftchar ?\[)
437 (aset (syntax-table) leftchar (cons 4 ?\)))
438 (aset (syntax-table) rightchar (cons 5 ?\[)))
440 (aset (syntax-table) leftchar (cons 4 ?\]))
441 (aset (syntax-table) rightchar (cons 5 ?\())))
442 (blink-matching-open))
443 (aset (syntax-table) leftchar leftsaved)
444 (aset (syntax-table) rightchar rightsaved)))
445 (blink-matching-open))))
447 (defun calc-alg-digit-entry ()
449 (cond ((eq last-command-char ?e)
450 (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e"))
451 ((eq last-command-char ?#) (format "%d#" calc-number-radix))
452 ((eq last-command-char ?_) "-")
453 ((eq last-command-char ?@) "0@ ")
454 (t (char-to-string last-command-char)))))
456 ;; The variable calc-digit-value is initially declared in calc.el,
457 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
458 (defvar calc-digit-value)
460 (defun calcDigit-algebraic ()
462 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
464 (setq calc-digit-value (minibuffer-contents))
467 (defun calcDigit-edit ()
469 (calc-unread-command)
470 (setq calc-digit-value (minibuffer-contents))
474 ;;; Algebraic expression parsing. [Public]
476 (defvar math-read-replacement-list
478 ("±" "+/-") ; plus or minus
479 ("×" "*") ; multiplication sign
480 ("÷" ":") ; division sign
481 ("−" "-") ; subtraction sign
482 ("∕" "/") ; division sign
483 ("∗" "*") ; asterisk multiplication
484 ("∞" "inf") ; infinity symbol
523 "A list whose elements (old new) indicate replacements to make
524 in Calc algebraic input.")
526 (defvar math-read-superscripts
527 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
528 "A string consisting of the superscripts allowed by Calc.")
530 (defun math-read-preprocess-string (str)
531 "Replace some substrings of STR by Calc equivalents."
533 (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
535 (let ((rep-list math-read-replacement-list))
538 (replace-regexp-in-string (nth 0 (car rep-list))
539 (nth 1 (car rep-list)) str))
540 (setq rep-list (cdr rep-list))))
543 ;; The next few variables are local to math-read-exprs (and math-read-expr
544 ;; in calc-ext.el), but are set in functions they call.
546 (defvar math-exp-pos)
547 (defvar math-exp-str)
548 (defvar math-exp-old-pos)
549 (defvar math-exp-token)
550 (defvar math-exp-keep-spaces)
551 (defvar math-expr-data)
553 (defun math-read-exprs (math-exp-str)
554 (let ((math-exp-pos 0)
556 (math-exp-keep-spaces nil)
557 math-exp-token math-expr-data)
558 (setq math-exp-str (math-read-preprocess-string math-exp-str))
559 (if calc-language-input-filter
560 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
561 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
562 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
563 (substring math-exp-str (+ math-exp-token 2)))))
564 (math-build-parse-table)
566 (let ((val (catch 'syntax (math-read-expr-list))))
568 (list 'error math-exp-old-pos val)
569 (if (equal math-exp-token 'end)
571 (list 'error math-exp-old-pos "Syntax error"))))))
573 (defun math-read-expr-list ()
574 (let* ((math-exp-keep-spaces nil)
575 (val (list (math-read-expr-level 0)))
577 (while (equal math-expr-data ",")
579 (let ((rest (list (math-read-expr-level 0))))
584 (defvar calc-user-parse-table nil)
585 (defvar calc-last-main-parse-table nil)
586 (defvar calc-last-lang-parse-table nil)
587 (defvar calc-user-tokens nil)
588 (defvar calc-user-token-chars nil)
590 (defvar math-toks nil
591 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
593 (defun math-build-parse-table ()
594 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
595 (ltab (cdr (assq calc-language calc-user-parse-tables))))
596 (or (and (eq mtab calc-last-main-parse-table)
597 (eq ltab calc-last-lang-parse-table))
598 (let ((p (append mtab ltab))
600 (setq calc-user-parse-table p)
601 (setq calc-user-token-chars nil)
603 (math-find-user-tokens (car (car p)))
605 (setq calc-user-tokens (mapconcat 'identity
606 (sort (mapcar 'car math-toks)
607 (function (lambda (x y)
611 calc-last-main-parse-table mtab
612 calc-last-lang-parse-table ltab)))))
614 (defun math-find-user-tokens (p)
616 (cond ((and (stringp (car p))
617 (or (> (length (car p)) 1) (equal (car p) "$")
618 (equal (car p) "\""))
619 (string-match "[^a-zA-Z0-9]" (car p)))
620 (let ((s (regexp-quote (car p))))
621 (if (string-match "\\`[a-zA-Z0-9]" s)
622 (setq s (concat "\\<" s)))
623 (if (string-match "[a-zA-Z0-9]\\'" s)
624 (setq s (concat s "\\>")))
625 (or (assoc s math-toks)
627 (setq math-toks (cons (list s) math-toks))
628 (or (memq (aref (car p) 0) calc-user-token-chars)
629 (setq calc-user-token-chars
630 (cons (aref (car p) 0)
631 calc-user-token-chars)))))))
633 (math-find-user-tokens (nth 1 (car p)))
634 (or (eq (car (car p)) '\?)
635 (math-find-user-tokens (nth 2 (car p))))))
638 (defun math-read-token ()
639 (if (>= math-exp-pos (length math-exp-str))
640 (setq math-exp-old-pos math-exp-pos
642 math-expr-data "\000")
643 (let ((ch (aref math-exp-str math-exp-pos)))
644 (setq math-exp-old-pos math-exp-pos)
645 (cond ((memq ch '(32 10 9))
646 (setq math-exp-pos (1+ math-exp-pos))
647 (if math-exp-keep-spaces
648 (setq math-exp-token 'space
651 ((and (memq ch calc-user-token-chars)
652 (let ((case-fold-search nil))
653 (eq (string-match calc-user-tokens math-exp-str math-exp-pos)
655 (setq math-exp-token 'punc
656 math-expr-data (math-match-substring math-exp-str 0)
657 math-exp-pos (match-end 0)))
658 ((or (and (>= ch ?a) (<= ch ?z))
659 (and (>= ch ?A) (<= ch ?Z)))
660 (string-match (if (memq calc-language '(c fortran pascal maple))
663 math-exp-str math-exp-pos)
664 (setq math-exp-token 'symbol
665 math-exp-pos (match-end 0)
666 math-expr-data (math-restore-dashes
667 (math-match-substring math-exp-str 0)))
668 (if (eq calc-language 'eqn)
669 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
673 ((consp (nth 1 code))
675 (if (assoc math-expr-data (cdr code))
676 (setq math-expr-data (format "%s %s"
677 (car code) math-expr-data))))
678 ((eq (nth 1 code) 'punc)
679 (setq math-exp-token 'punc
680 math-expr-data (nth 2 code)))
683 (math-read-token))))))
684 ((or (and (>= ch ?0) (<= ch ?9))
686 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
689 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
691 (or (eq math-exp-pos 0)
692 (and (memq calc-language '(nil flat big unform
694 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
695 math-exp-str (1- math-exp-pos))
696 (1- math-exp-pos))))))
697 (or (and (eq calc-language 'c)
698 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
699 (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]+\\)?\"?\\)?"
700 math-exp-str math-exp-pos))
701 (setq math-exp-token 'number
702 math-expr-data (math-match-substring math-exp-str 0)
703 math-exp-pos (match-end 0)))
705 (if (and (eq calc-language 'pascal)
707 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
708 math-exp-str math-exp-pos)
710 (setq math-exp-token 'number
711 math-expr-data (math-match-substring math-exp-str 1)
712 math-exp-pos (match-end 1))
713 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
715 (setq math-expr-data (- (string-to-int (math-match-substring
717 (string-match "\\$+" math-exp-str math-exp-pos)
718 (setq math-expr-data (- (match-end 0) (match-beginning 0))))
719 (setq math-exp-token 'dollar
720 math-exp-pos (match-end 0))))
722 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
724 (setq math-expr-data (string-to-int
725 (math-match-substring math-exp-str 1))
726 math-exp-pos (match-end 0))
727 (setq math-expr-data 1
728 math-exp-pos (1+ math-exp-pos)))
729 (setq math-exp-token 'hash))
730 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
731 math-exp-str math-exp-pos)
733 (setq math-exp-token 'punc
734 math-expr-data (math-match-substring math-exp-str 0)
735 math-exp-pos (match-end 0)))
737 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
738 math-exp-str math-exp-pos))
739 (if (eq calc-language 'eqn)
741 (setq math-exp-str (copy-sequence math-exp-str))
742 (aset math-exp-str (match-beginning 1) ?\{)
743 (if (< (match-end 1) (length math-exp-str))
744 (aset math-exp-str (match-end 1) ?\}))
746 (setq math-exp-token 'string
747 math-expr-data (math-match-substring math-exp-str 1)
748 math-exp-pos (match-end 0))))
749 ((and (= ch ?\\) (eq calc-language 'tex)
750 (< math-exp-pos (1- (length math-exp-str))))
751 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
752 math-exp-str math-exp-pos)
753 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
754 math-exp-str math-exp-pos))
755 (setq math-exp-token 'symbol
756 math-exp-pos (match-end 0)
757 math-expr-data (math-restore-dashes
758 (math-match-substring math-exp-str 1)))
759 (let ((code (assoc math-expr-data math-tex-ignore-words)))
763 ((eq (nth 1 code) 'punc)
764 (setq math-exp-token 'punc
765 math-expr-data (nth 2 code)))
766 ((and (eq (nth 1 code) 'mat)
767 (string-match " *{" math-exp-str math-exp-pos))
768 (setq math-exp-pos (match-end 0)
771 (let ((right (string-match "}" math-exp-str math-exp-pos)))
773 (setq math-exp-str (copy-sequence math-exp-str))
774 (aset math-exp-str right ?\])))))))
775 ((and (= ch ?\.) (eq calc-language 'fortran)
776 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
777 math-exp-str math-exp-pos) math-exp-pos))
778 (setq math-exp-token 'punc
779 math-expr-data (upcase (math-match-substring math-exp-str 0))
780 math-exp-pos (match-end 0)))
781 ((and (eq calc-language 'math)
782 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
784 (setq math-exp-token 'punc
785 math-expr-data (math-match-substring math-exp-str 0)
786 math-exp-pos (match-end 0)))
787 ((and (eq calc-language 'eqn)
788 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
789 math-exp-str math-exp-pos)
791 (setq math-exp-token 'punc
792 math-expr-data (math-match-substring math-exp-str 0)
793 math-exp-pos (match-end 0))
794 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
796 (setq math-exp-pos (match-end 0)))
797 (if (memq (aref math-expr-data 0) '(?~ ?^))
799 ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
800 (setq math-exp-pos (match-end 0))
803 (if (and (eq ch ?\{) (memq calc-language '(tex eqn)))
805 (if (and (eq ch ?\}) (memq calc-language '(tex eqn)))
807 (if (and (eq ch ?\&) (eq calc-language 'tex))
809 (setq math-exp-token 'punc
810 math-expr-data (char-to-string ch)
811 math-exp-pos (1+ math-exp-pos)))))))
813 (defconst math-alg-inequalities
814 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
815 calcFunc-eq calcFunc-neq))
817 (defun math-read-expr-level (exp-prec &optional exp-term)
818 (let* ((x (math-read-factor)) (first t) op op2)
819 (while (and (or (and calc-user-parse-table
820 (setq op (calc-check-user-syntax x exp-prec))
822 op '("2x" ident 999999 -1)))
823 (and (setq op (assoc math-expr-data math-expr-opers))
825 (or (and (setq op2 (assoc
827 (cdr (memq op math-expr-opers))))
828 (eq (= (nth 3 op) -1)
830 (eq (= (nth 3 op2) -1)
831 (not (math-factor-after)))
834 (and (or (eq (nth 2 op) -1)
835 (memq math-exp-token '(symbol number dollar hash))
836 (equal math-expr-data "(")
837 (and (equal math-expr-data "[")
838 (not (eq calc-language 'math))
839 (not (and math-exp-keep-spaces
840 (eq (car-safe x) 'vec)))))
841 (or (not (setq op (assoc math-expr-data math-expr-opers)))
843 (or (not calc-user-parse-table)
844 (not (eq math-exp-token 'symbol))
845 (let ((p calc-user-parse-table))
848 (car (car (car p)))))
850 (nth 1 (car (car p)))
854 (setq op (assoc "2x" math-expr-opers))))
855 (not (and exp-term (equal math-expr-data exp-term)))
856 (>= (nth 2 op) exp-prec))
857 (if (not (equal (car op) "2x"))
859 (and (memq (nth 1 op) '(sdev mod))
861 (setq x (cond ((consp (nth 1 op))
862 (funcall (car (nth 1 op)) x op))
864 (if (eq (nth 1 op) 'ident)
866 (if (eq (nth 1 op) 'closing)
867 (if (eq (nth 2 op) exp-prec)
871 (throw 'syntax "Mismatched delimiters"))
872 (list (nth 1 op) x))))
874 (memq (nth 1 op) math-alg-inequalities)
875 (memq (car-safe x) math-alg-inequalities))
877 (math-composite-inequalities x op))
880 (math-read-expr-level (nth 3 op) exp-term))))
884 (defun calc-check-user-syntax (&optional x prec)
885 (let ((p calc-user-parse-table)
890 (setq rule (car (car p)))
892 (and (integerp (car rule))
894 (equal math-expr-data
895 (car (setq rule (cdr rule)))))
896 (equal math-expr-data (car rule)))))
897 (let ((save-exp-pos math-exp-pos)
898 (save-exp-old-pos math-exp-old-pos)
899 (save-exp-token math-exp-token)
900 (save-exp-data math-expr-data))
902 (setq matches (calc-match-user-syntax rule))))
909 (setq matches (cons x matches)))
910 (setq match (cdr (car p)))
911 (while (and (eq (car-safe match)
913 (= (length match) 3))
914 (setq conds (append (math-flatten-lands
917 match (nth 1 match)))
918 (while (and conds match)
920 (cond ((eq (car-safe (car conds))
922 (setq temp (car conds))
923 (or (= (length temp) 3)
924 (and (= (length temp) 2)
925 (eq (car-safe (nth 1 temp))
927 (= (length (nth 1 temp)) 3)
928 (setq temp (nth 1 temp)))
936 args (cons (nth 1 temp)
938 ((and (eq (car-safe (car conds))
940 (= (length (car conds)) 3))
941 (setq temp (calcFunc-vmatches
945 (nth 2 (car conds))))
948 (while (setq temp (cdr temp))
949 (setq matches (cons (nth 2 (car temp))
951 args (cons (nth 1 (car temp))
954 (or (math-is-true (math-simplify
959 (setq conds (cdr conds)))
961 (not (setq match (math-multi-subst
962 match args matches)))
963 (setq math-exp-old-pos save-exp-old-pos
964 math-exp-token save-exp-token
965 math-expr-data save-exp-data
966 math-exp-pos save-exp-pos)))))))
970 (defun calc-match-user-syntax (p &optional term)
972 (save-exp-pos math-exp-pos)
973 (save-exp-old-pos math-exp-old-pos)
974 (save-exp-token math-exp-token)
975 (save-exp-data math-expr-data)
978 (cond ((stringp (car p))
979 (and (equal math-expr-data (car p))
984 (and (setq m (catch 'syntax
985 (math-read-expr-level
988 (if (consp (nth 1 p))
989 (car (nth 1 (nth 1 p)))
993 (setq matches (nconc matches (list m)))))
994 ((eq (car (car p)) '\?)
995 (setq m (calc-match-user-syntax (nth 1 (car p))))
1000 (cons 'vec (and (listp m) m))))))
1001 (or (listp m) (not (nth 2 (car p)))
1002 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
1003 (eq math-exp-token 'end)))
1005 (setq m (calc-match-user-syntax (nth 1 (car p))
1006 (car (nth 2 (car p)))))
1008 (let ((vec (cons 'vec m))
1011 (setq opos math-exp-pos
1012 mm (calc-match-user-syntax
1015 (car (nth 2 (car p))))))
1016 (> math-exp-pos opos))
1017 (setq vec (nconc vec mm)))
1018 (setq matches (nconc matches (list vec))))
1019 (and (eq (car (car p)) '*)
1020 (setq matches (nconc matches (list '(vec)))))))))
1023 (setq math-exp-pos save-exp-pos
1024 math-exp-old-pos save-exp-old-pos
1025 math-exp-token save-exp-token
1026 math-expr-data save-exp-data
1030 (defun math-remove-dashes (x)
1031 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
1033 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
1036 (defun math-restore-dashes (x)
1037 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
1038 (math-restore-dashes
1039 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
1042 (defun math-read-if (cond op)
1043 (let ((then (math-read-expr-level 0)))
1044 (or (equal math-expr-data ":")
1045 (throw 'syntax "Expected ':'"))
1047 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
1049 (defun math-factor-after ()
1050 (let ((math-exp-pos math-exp-pos)
1051 math-exp-old-pos math-exp-token math-expr-data)
1053 (or (memq math-exp-token '(number symbol dollar hash string))
1054 (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
1055 (assoc (concat "u" math-expr-data) math-expr-opers))
1056 (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
1057 (assoc math-expr-data '(("(") ("[") ("{"))))))
1059 (defun math-read-factor ()
1061 (cond ((eq math-exp-token 'number)
1062 (let ((num (math-read-number math-expr-data)))
1065 (setq math-exp-old-pos math-exp-pos)
1066 (throw 'syntax "Bad format")))
1068 (if (and math-read-expr-quotes
1072 ((and calc-user-parse-table
1073 (setq op (calc-check-user-syntax)))
1075 ((or (equal math-expr-data "-")
1076 (equal math-expr-data "+")
1077 (equal math-expr-data "!")
1078 (equal math-expr-data "|")
1079 (equal math-expr-data "/"))
1080 (setq math-expr-data (concat "u" math-expr-data))
1082 ((and (setq op (assoc math-expr-data math-expr-opers))
1084 (if (consp (nth 1 op))
1085 (funcall (car (nth 1 op)) op)
1087 (let ((val (math-read-expr-level (nth 3 op))))
1088 (cond ((eq (nth 1 op) 'ident)
1090 ((and (Math-numberp val)
1091 (equal (car op) "u-"))
1093 (t (list (nth 1 op) val))))))
1094 ((eq math-exp-token 'symbol)
1095 (let ((sym (intern math-expr-data)))
1097 (if (equal math-expr-data calc-function-open)
1098 (let ((f (assq sym math-expr-function-mapping)))
1101 (funcall (car (cdr f)) f sym)
1102 (let ((args (if (or (equal math-expr-data calc-function-close)
1103 (eq math-exp-token 'end))
1105 (math-read-expr-list))))
1106 (if (not (or (equal math-expr-data calc-function-close)
1107 (eq math-exp-token 'end)))
1108 (throw 'syntax "Expected `)'"))
1110 (if (and (eq calc-language 'fortran) args
1112 (let ((calc-matrix-mode 'scalar))
1117 (symbol-name sym)))))))
1118 (math-parse-fortran-subscr sym args)
1121 (and (= (aref (symbol-name sym) 0) ?\\)
1122 (< (prefix-numeric-value calc-language-option)
1124 (setq sym (intern (substring (symbol-name sym)
1126 (or (string-match "-" (symbol-name sym))
1129 (symbol-name sym))))))
1131 (if math-read-expr-quotes
1133 (let ((val (list 'var
1134 (intern (math-remove-dashes
1136 (if (string-match "-" (symbol-name sym))
1138 (intern (concat "var-"
1139 (symbol-name sym)))))))
1140 (let ((v (assq (nth 1 val) math-expr-variable-mapping)))
1141 (and v (setq val (if (consp (cdr v))
1142 (funcall (car (cdr v)) v val)
1145 (substring (symbol-name (cdr v))
1148 (while (and (memq calc-language '(c pascal maple))
1149 (equal math-expr-data "["))
1151 (setq val (append (list 'calcFunc-subscr val)
1152 (math-read-expr-list)))
1153 (if (equal math-expr-data "]")
1155 (throw 'syntax "Expected ']'")))
1157 ((eq math-exp-token 'dollar)
1158 (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
1159 (if (>= (length calc-dollar-values) abs)
1160 (let ((num math-expr-data))
1162 (setq calc-dollar-used (max calc-dollar-used num))
1163 (math-check-complete (nth (1- abs) calc-dollar-values)))
1164 (throw 'syntax (if calc-dollar-values
1166 "$'s not allowed in this context")))))
1167 ((eq math-exp-token 'hash)
1168 (or calc-hashes-used
1169 (throw 'syntax "#'s not allowed in this context"))
1171 (if (<= math-expr-data (length calc-arg-values))
1172 (let ((num math-expr-data))
1174 (setq calc-hashes-used (max calc-hashes-used num))
1175 (nth (1- num) calc-arg-values))
1176 (throw 'syntax "Too many # arguments")))
1177 ((equal math-expr-data "(")
1178 (let* ((exp (let ((math-exp-keep-spaces nil))
1180 (if (or (equal math-expr-data "\\dots")
1181 (equal math-expr-data "\\ldots"))
1182 '(neg (var inf var-inf))
1183 (math-read-expr-level 0)))))
1184 (let ((math-exp-keep-spaces nil))
1186 ((equal math-expr-data ",")
1189 (let ((exp2 (math-read-expr-level 0)))
1191 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1192 (math-normalize (list 'cplx exp exp2))
1193 (list '+ exp (list '* exp2 '(var i var-i))))))))
1194 ((equal math-expr-data ";")
1197 (let ((exp2 (math-read-expr-level 0)))
1198 (setq exp (if (and exp2 (Math-realp exp)
1200 (math-normalize (list 'polar exp exp2))
1205 (math-to-radians-2 exp2)
1206 '(var i var-i)))))))))
1207 ((or (equal math-expr-data "\\dots")
1208 (equal math-expr-data "\\ldots"))
1211 (let ((exp2 (if (or (equal math-expr-data ")")
1212 (equal math-expr-data "]")
1213 (eq math-exp-token 'end))
1215 (math-read-expr-level 0))))
1218 (if (equal math-expr-data ")") 0 1)
1221 (if (not (or (equal math-expr-data ")")
1222 (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
1223 (eq math-exp-token 'end)))
1224 (throw 'syntax "Expected `)'"))
1227 ((eq math-exp-token 'string)
1230 ((equal math-expr-data "[")
1232 (math-read-brackets t "]"))
1233 ((equal math-expr-data "{")
1235 (math-read-brackets nil "}"))
1236 ((equal math-expr-data "<")
1238 (math-read-angle-brackets))
1239 (t (throw 'syntax "Expected a number")))))
1241 (provide 'calc-aent)
1243 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1244 ;;; calc-aent.el ends here