1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
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-opers
)
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 NIL means return a list containing the character position of error.
236 STRING means return error message as string rather than list.
237 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-opers math-expr-opers
)))
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* ((x (math-read-factor)) (first t
) op op2
)
880 (while (and (or (and calc-user-parse-table
881 (setq op
(calc-check-user-syntax x exp-prec
))
883 op
'("2x" ident
999999 -
1)))
884 (and (setq op
(assoc math-expr-data math-expr-opers
))
886 (or (and (setq op2
(assoc
888 (cdr (memq op math-expr-opers
))))
889 (eq (= (nth 3 op
) -
1)
891 (eq (= (nth 3 op2
) -
1)
892 (not (math-factor-after)))
895 (and (or (eq (nth 2 op
) -
1)
896 (memq math-exp-token
'(symbol number dollar hash
))
897 (equal math-expr-data
"(")
898 (and (equal math-expr-data
"[")
899 (not (eq calc-language
'math
))
900 (not (and math-exp-keep-spaces
901 (eq (car-safe x
) 'vec
)))))
902 (or (not (setq op
(assoc math-expr-data math-expr-opers
)))
904 (or (not calc-user-parse-table
)
905 (not (eq math-exp-token
'symbol
))
906 (let ((p calc-user-parse-table
))
909 (car (car (car p
)))))
911 (nth 1 (car (car p
)))
915 (setq op
(assoc "2x" math-expr-opers
))))
916 (not (and exp-term
(equal math-expr-data exp-term
)))
917 (>= (nth 2 op
) exp-prec
))
918 (if (not (equal (car op
) "2x"))
920 (and (memq (nth 1 op
) '(sdev mod
))
922 (setq x
(cond ((consp (nth 1 op
))
923 (funcall (car (nth 1 op
)) x op
))
925 (if (eq (nth 1 op
) 'ident
)
927 (if (eq (nth 1 op
) 'closing
)
928 (if (eq (nth 2 op
) exp-prec
)
932 (throw 'syntax
"Mismatched delimiters"))
933 (list (nth 1 op
) x
))))
935 (memq (nth 1 op
) math-alg-inequalities
)
936 (memq (car-safe x
) math-alg-inequalities
))
938 (math-composite-inequalities x op
))
941 (math-read-expr-level (nth 3 op
) exp-term
))))
945 ;; calc-arg-values is defined in calc-ext.el, but is used here.
946 (defvar calc-arg-values
)
948 (defun calc-check-user-syntax (&optional x prec
)
949 (let ((p calc-user-parse-table
)
954 (setq rule
(car (car p
)))
956 (and (integerp (car rule
))
958 (equal math-expr-data
959 (car (setq rule
(cdr rule
)))))
960 (equal math-expr-data
(car rule
)))))
961 (let ((save-exp-pos math-exp-pos
)
962 (save-exp-old-pos math-exp-old-pos
)
963 (save-exp-token math-exp-token
)
964 (save-exp-data math-expr-data
))
966 (setq matches
(calc-match-user-syntax rule
))))
973 (setq matches
(cons x matches
)))
974 (setq match
(cdr (car p
)))
975 (while (and (eq (car-safe match
)
977 (= (length match
) 3))
978 (setq conds
(append (math-flatten-lands
981 match
(nth 1 match
)))
982 (while (and conds match
)
984 (cond ((eq (car-safe (car conds
))
986 (setq temp
(car conds
))
987 (or (= (length temp
) 3)
988 (and (= (length temp
) 2)
989 (eq (car-safe (nth 1 temp
))
991 (= (length (nth 1 temp
)) 3)
992 (setq temp
(nth 1 temp
)))
1000 args
(cons (nth 1 temp
)
1002 ((and (eq (car-safe (car conds
))
1004 (= (length (car conds
)) 3))
1005 (setq temp
(calcFunc-vmatches
1009 (nth 2 (car conds
))))
1012 (while (setq temp
(cdr temp
))
1013 (setq matches
(cons (nth 2 (car temp
))
1015 args
(cons (nth 1 (car temp
))
1018 (or (math-is-true (math-simplify
1023 (setq conds
(cdr conds
)))
1025 (not (setq match
(math-multi-subst
1026 match args matches
)))
1027 (setq math-exp-old-pos save-exp-old-pos
1028 math-exp-token save-exp-token
1029 math-expr-data save-exp-data
1030 math-exp-pos save-exp-pos
)))))))
1034 (defun calc-match-user-syntax (p &optional term
)
1036 (save-exp-pos math-exp-pos
)
1037 (save-exp-old-pos math-exp-old-pos
)
1038 (save-exp-token math-exp-token
)
1039 (save-exp-data math-expr-data
)
1042 (cond ((stringp (car p
))
1043 (and (equal math-expr-data
(car p
))
1048 (and (setq m
(catch 'syntax
1049 (math-read-expr-level
1052 (if (consp (nth 1 p
))
1053 (car (nth 1 (nth 1 p
)))
1057 (setq matches
(nconc matches
(list m
)))))
1058 ((eq (car (car p
)) '\?)
1059 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
1064 (cons 'vec
(and (listp m
) m
))))))
1065 (or (listp m
) (not (nth 2 (car p
)))
1066 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
1067 (eq math-exp-token
'end
)))
1069 (setq m
(calc-match-user-syntax (nth 1 (car p
))
1070 (car (nth 2 (car p
)))))
1072 (let ((vec (cons 'vec m
))
1075 (setq opos math-exp-pos
1076 mm
(calc-match-user-syntax
1079 (car (nth 2 (car p
))))))
1080 (> math-exp-pos opos
))
1081 (setq vec
(nconc vec mm
)))
1082 (setq matches
(nconc matches
(list vec
))))
1083 (and (eq (car (car p
)) '*)
1084 (setq matches
(nconc matches
(list '(vec)))))))))
1087 (setq math-exp-pos save-exp-pos
1088 math-exp-old-pos save-exp-old-pos
1089 math-exp-token save-exp-token
1090 math-expr-data save-exp-data
1094 (defun math-remove-dashes (x)
1095 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
1097 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
1100 (defun math-restore-dashes (x)
1101 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
1102 (math-restore-dashes
1103 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
1106 (defun math-read-if (cond op
)
1107 (let ((then (math-read-expr-level 0)))
1108 (or (equal math-expr-data
":")
1109 (throw 'syntax
"Expected ':'"))
1111 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
1113 (defun math-factor-after ()
1114 (let ((math-exp-pos math-exp-pos
)
1115 math-exp-old-pos math-exp-token math-expr-data
)
1117 (or (memq math-exp-token
'(number symbol dollar hash string
))
1118 (and (assoc math-expr-data
'(("-") ("+") ("!") ("|") ("/")))
1119 (assoc (concat "u" math-expr-data
) math-expr-opers
))
1120 (eq (nth 2 (assoc math-expr-data math-expr-opers
)) -
1)
1121 (assoc math-expr-data
'(("(") ("[") ("{"))))))
1123 (defun math-read-factor ()
1125 (cond ((eq math-exp-token
'number
)
1126 (let ((num (math-read-number math-expr-data
)))
1129 (setq math-exp-old-pos math-exp-pos
)
1130 (throw 'syntax
"Bad format")))
1132 (if (and math-read-expr-quotes
1136 ((and calc-user-parse-table
1137 (setq op
(calc-check-user-syntax)))
1139 ((or (equal math-expr-data
"-")
1140 (equal math-expr-data
"+")
1141 (equal math-expr-data
"!")
1142 (equal math-expr-data
"|")
1143 (equal math-expr-data
"/"))
1144 (setq math-expr-data
(concat "u" math-expr-data
))
1146 ((and (setq op
(assoc math-expr-data math-expr-opers
))
1148 (if (consp (nth 1 op
))
1149 (funcall (car (nth 1 op
)) op
)
1151 (let ((val (math-read-expr-level (nth 3 op
))))
1152 (cond ((eq (nth 1 op
) 'ident
)
1154 ((and (Math-numberp val
)
1155 (equal (car op
) "u-"))
1157 (t (list (nth 1 op
) val
))))))
1158 ((eq math-exp-token
'symbol
)
1159 (let ((sym (intern math-expr-data
)))
1161 (if (equal math-expr-data calc-function-open
)
1162 (let ((f (assq sym math-expr-function-mapping
)))
1165 (funcall (car (cdr f
)) f sym
)
1166 (let ((args (if (or (equal math-expr-data calc-function-close
)
1167 (eq math-exp-token
'end
))
1169 (math-read-expr-list))))
1170 (if (not (or (equal math-expr-data calc-function-close
)
1171 (eq math-exp-token
'end
)))
1172 (throw 'syntax
"Expected `)'"))
1174 (if (and (eq calc-language
'fortran
) args
1176 (let ((calc-matrix-mode 'scalar
))
1181 (symbol-name sym
)))))))
1182 (math-parse-fortran-subscr sym args
)
1185 (and (= (aref (symbol-name sym
) 0) ?
\\)
1186 (< (prefix-numeric-value calc-language-option
)
1188 (setq sym
(intern (substring (symbol-name sym
)
1190 (or (string-match "-" (symbol-name sym
))
1193 (symbol-name sym
))))))
1195 (if math-read-expr-quotes
1197 (let ((val (list 'var
1198 (intern (math-remove-dashes
1200 (if (string-match "-" (symbol-name sym
))
1202 (intern (concat "var-"
1203 (symbol-name sym
)))))))
1204 (let ((v (assq (nth 1 val
) math-expr-variable-mapping
)))
1205 (and v
(setq val
(if (consp (cdr v
))
1206 (funcall (car (cdr v
)) v val
)
1209 (substring (symbol-name (cdr v
))
1212 (while (and (memq calc-language
'(c pascal maple
))
1213 (equal math-expr-data
"["))
1215 (setq val
(append (list 'calcFunc-subscr val
)
1216 (math-read-expr-list)))
1217 (if (equal math-expr-data
"]")
1219 (throw 'syntax
"Expected ']'")))
1221 ((eq math-exp-token
'dollar
)
1222 (let ((abs (if (> math-expr-data
0) math-expr-data
(- math-expr-data
))))
1223 (if (>= (length calc-dollar-values
) abs
)
1224 (let ((num math-expr-data
))
1226 (setq calc-dollar-used
(max calc-dollar-used num
))
1227 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1228 (throw 'syntax
(if calc-dollar-values
1230 "$'s not allowed in this context")))))
1231 ((eq math-exp-token
'hash
)
1232 (or calc-hashes-used
1233 (throw 'syntax
"#'s not allowed in this context"))
1235 (if (<= math-expr-data
(length calc-arg-values
))
1236 (let ((num math-expr-data
))
1238 (setq calc-hashes-used
(max calc-hashes-used num
))
1239 (nth (1- num
) calc-arg-values
))
1240 (throw 'syntax
"Too many # arguments")))
1241 ((equal math-expr-data
"(")
1242 (let* ((exp (let ((math-exp-keep-spaces nil
))
1244 (if (or (equal math-expr-data
"\\dots")
1245 (equal math-expr-data
"\\ldots"))
1246 '(neg (var inf var-inf
))
1247 (math-read-expr-level 0)))))
1248 (let ((math-exp-keep-spaces nil
))
1250 ((equal math-expr-data
",")
1253 (let ((exp2 (math-read-expr-level 0)))
1255 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1256 (math-normalize (list 'cplx exp exp2
))
1257 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1258 ((equal math-expr-data
";")
1261 (let ((exp2 (math-read-expr-level 0)))
1262 (setq exp
(if (and exp2
(Math-realp exp
)
1264 (math-normalize (list 'polar exp exp2
))
1269 (math-to-radians-2 exp2
)
1270 '(var i var-i
)))))))))
1271 ((or (equal math-expr-data
"\\dots")
1272 (equal math-expr-data
"\\ldots"))
1275 (let ((exp2 (if (or (equal math-expr-data
")")
1276 (equal math-expr-data
"]")
1277 (eq math-exp-token
'end
))
1279 (math-read-expr-level 0))))
1282 (if (equal math-expr-data
")") 0 1)
1285 (if (not (or (equal math-expr-data
")")
1286 (and (equal math-expr-data
"]") (eq (car-safe exp
) 'intv
))
1287 (eq math-exp-token
'end
)))
1288 (throw 'syntax
"Expected `)'"))
1291 ((eq math-exp-token
'string
)
1294 ((equal math-expr-data
"[")
1296 (math-read-brackets t
"]"))
1297 ((equal math-expr-data
"{")
1299 (math-read-brackets nil
"}"))
1300 ((equal math-expr-data
"<")
1302 (math-read-angle-brackets))
1303 (t (throw 'syntax
"Expected a number")))))
1305 (provide 'calc-aent
)
1307 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1308 ;;; calc-aent.el ends here