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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
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.
34 (eval-when-compile '(require calc-macs
))
36 (defun calc-Need-calc-aent () nil
)
39 (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
))
53 (alg-exp (mapcar (function
55 (if (and (not calc-extensions-loaded
)
56 calc-previous-alg-entry
58 "\\`[-0-9._+*/^() ]+\\'"
59 calc-previous-alg-entry
))
62 (math-evaluate-expr x
))))
64 (when (and (= (length alg-exp
) 1)
65 (eq (car-safe (car alg-exp
)) 'calcFunc-assign
)
66 (= (length (car alg-exp
)) 3)
67 (eq (car-safe (nth 1 (car alg-exp
))) 'var
))
69 (set (nth 2 (nth 1 (car alg-exp
))) (nth 2 (car alg-exp
)))
70 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp
))))
71 (setq alg-exp
(list (nth 2 (car alg-exp
)))))
72 (setq calc-quick-prev-results alg-exp
73 buf
(mapconcat (function (lambda (x)
74 (math-format-value x
1000)))
78 (if (and (= (length alg-exp
) 1)
79 (memq (car-safe (car alg-exp
)) '(nil bigpos bigneg
))
81 (= calc-number-radix
10))
82 (setq buf
(concat buf
" ("
83 (let ((calc-number-radix 16))
84 (math-format-value (car alg-exp
) 1000))
86 (let ((calc-number-radix 8))
87 (math-format-value (car alg-exp
) 1000))
88 (if (and (integerp (car alg-exp
))
90 (< (car alg-exp
) 127))
91 (format ", \"%c\"" (car alg-exp
))
94 (if (and (< (length buf
) (frame-width)) (= (length entry
) 1)
95 calc-extensions-loaded
)
96 (let ((long (concat (math-format-value (car entry
) 1000)
98 (if (<= (length long
) (- (frame-width) 8))
101 (message "Result: %s" buf
)))
102 (if (eq last-command-char
10)
104 (setq kill-ring
(cons shortbuf kill-ring
))
105 (when (> (length kill-ring
) kill-ring-max
)
106 (setcdr (nthcdr (1- kill-ring-max
) kill-ring
) nil
))
107 (setq kill-ring-yank-pointer kill-ring
)))))
109 (defun calc-do-calc-eval (str separator args
)
115 ((and (consp str
) (not (symbolp (car str
))))
116 (let ((calc-language nil
)
117 (math-expr-opers math-standard-opers
)
118 (calc-internal-prec 12)
120 (calc-symbolic-mode nil
)
121 (calc-matrix-mode nil
)
122 (calc-angle-mode 'deg
)
123 (calc-number-radix 10)
124 (calc-leading-zeros nil
)
125 (calc-group-digits nil
)
126 (calc-point-char ".")
127 (calc-frac-format '(":" nil
))
128 (calc-prefer-frac nil
)
129 (calc-hms-format "%s@ %s' %s\"")
130 (calc-date-format '((H ":" mm C SS pp
" ")
131 Www
" " Mmm
" " D
", " YYYY
))
132 (calc-float-format '(float 0))
133 (calc-full-float-format '(float 0))
134 (calc-complex-format nil
)
135 (calc-matrix-just nil
)
136 (calc-full-vectors t
)
137 (calc-break-vectors nil
)
138 (calc-vector-commas ",")
139 (calc-vector-brackets "[]")
140 (calc-matrix-brackets '(R O
))
141 (calc-complex-mode 'cplx
)
142 (calc-infinite-mode nil
)
143 (calc-display-strings nil
)
144 (calc-simplify-mode nil
)
145 (calc-display-working-message 'lots
)
148 (set (car strp
) (nth 1 strp
))
149 (setq strp
(cdr (cdr strp
))))
150 (calc-do-calc-eval (car str
) separator args
)))
151 ((eq separator
'eval
)
153 ((eq separator
'macro
)
155 (let* ((calc-buffer (current-buffer))
156 (calc-window (get-buffer-window calc-buffer
))
157 (save-window (selected-window)))
161 (select-window calc-window
)
162 (calc-execute-kbd-macro str nil
(car args
)))
163 (and (window-point save-window
)
164 (select-window save-window
)))
165 (save-window-excursion
166 (select-window (get-largest-window))
167 (switch-to-buffer calc-buffer
)
168 (calc-execute-kbd-macro str nil
(car args
)))))
171 (or (not (integerp str
))
173 (calc-pop (min str
(calc-stack-size))))
178 (<= str
(calc-stack-size))
179 (math-format-value (calc-top-n str
(car args
)) 1000)))
180 ((eq separator
'rawtop
)
183 (<= str
(calc-stack-size))
184 (calc-top-n str
(car args
))))
186 (let* ((calc-command-flags nil
)
188 (calc-language (if (memq calc-language
'(nil big
))
189 'flat calc-language
))
190 (calc-dollar-values (mapcar
195 (setq x
(math-read-exprs x
))
205 (res (if (stringp str
)
206 (math-read-exprs str
)
209 (if (eq (car res
) 'error
)
210 (calc-eval-error (cdr res
))
211 (setq res
(mapcar 'calc-normalize res
))
212 (and (memq 'clear-message calc-command-flags
)
214 (cond ((eq separator
'pred
)
216 (if (= (length res
) 1)
217 (math-is-true (car res
))
218 (calc-eval-error '(0 "Single value expected"))))
220 (if (= (length res
) 1)
222 (calc-eval-error '(0 "Single value expected"))))
223 ((eq separator
'list
)
225 ((memq separator
'(num rawnum
))
226 (if (= (length res
) 1)
227 (if (math-constp (car res
))
228 (if (eq separator
'num
)
229 (math-format-value (car res
) 1000)
234 (calc-explain-why (car calc-next-why
))
235 "Number expected"))))
236 (calc-eval-error '(0 "Single value expected"))))
237 ((eq separator
'push
)
241 (setq buf
(concat buf
242 (and buf
(or separator
", "))
243 (math-format-value (car res
) 1000))
247 (defun calc-eval-error (msg)
248 (if (and (boundp 'calc-eval-error
)
250 (if (eq calc-eval-error
'string
)
252 (error "%s" (nth 1 msg
)))
256 ;;;; Reading an expression in algebraic form.
258 (defun calc-auto-algebraic-entry (&optional prefix
)
260 (calc-algebraic-entry prefix t
))
262 (defun calc-algebraic-entry (&optional prefix auto
)
265 (let ((calc-language (if prefix nil calc-language
))
266 (math-expr-opers (if prefix math-standard-opers math-expr-opers
)))
267 (calc-alg-entry (and auto
(char-to-string last-command-char
))))))
269 (defun calc-alg-entry (&optional initial prompt
)
270 (let* ((sel-mode nil
)
271 (calc-dollar-values (mapcar 'calc-get-stack-element
272 (nthcdr calc-stack-top calc-stack
)))
275 (alg-exp (calc-do-alg-entry initial prompt t
)))
276 (if (stringp alg-exp
)
279 (calc-alg-edit alg-exp
))
280 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j
)
283 (nvals (mapcar 'calc-normalize alg-exp
)))
285 (calc-record (if calc-extensions-loaded
(car alg-exp
) (car nvals
))
287 (calc-pop-push-record-list calc-dollar-used
288 (and (not (equal (car alg-exp
)
290 calc-extensions-loaded
293 (setq alg-exp
(cdr alg-exp
)
295 calc-dollar-used
0)))
296 (calc-handle-whys))))
298 (defvar calc-alg-ent-map nil
299 "The keymap used for algebraic entry.")
301 (defvar calc-alg-ent-esc-map nil
302 "The keymap used for escapes in algebraic entry.")
304 (defun calc-do-alg-entry (&optional initial prompt no-normalize
)
305 (let* ((calc-buffer (current-buffer))
306 (blink-paren-function 'calcAlg-blink-matching-open
)
308 (unless calc-alg-ent-map
309 (setq calc-alg-ent-map
(copy-keymap minibuffer-local-map
))
310 (define-key calc-alg-ent-map
"'" 'calcAlg-previous
)
311 (define-key calc-alg-ent-map
"`" 'calcAlg-edit
)
312 (define-key calc-alg-ent-map
"\C-m" 'calcAlg-enter
)
313 (define-key calc-alg-ent-map
"\C-j" 'calcAlg-enter
)
315 (setq calc-alg-ent-esc-map
(copy-keymap esc-map
))
317 (aset (nth 1 calc-alg-ent-esc-map
) i
'calcAlg-escape
)
319 (define-key calc-alg-ent-map
"\e" nil
)
320 (if (eq calc-algebraic-mode
'total
)
321 (define-key calc-alg-ent-map
"\e" calc-alg-ent-esc-map
)
322 (define-key calc-alg-ent-map
"\ep" 'calcAlg-plus-minus
)
323 (define-key calc-alg-ent-map
"\em" 'calcAlg-mod
)
324 (define-key calc-alg-ent-map
"\e=" 'calcAlg-equals
)
325 (define-key calc-alg-ent-map
"\e\r" 'calcAlg-equals
)
326 (define-key calc-alg-ent-map
"\e%" 'self-insert-command
))
327 (setq calc-aborted-prefix nil
)
328 (let ((buf (read-from-minibuffer (or prompt
"Algebraic: ")
330 calc-alg-ent-map nil
)))
331 (when (eq alg-exp
'error
)
332 (when (eq (car-safe (setq alg-exp
(math-read-exprs buf
))) 'error
)
334 (setq calc-aborted-prefix
"alg'")
336 (and alg-exp
(setq alg-exp
(mapcar 'calc-normalize alg-exp
))))
339 (defun calcAlg-plus-minus ()
341 (if (calc-minibuffer-contains ".* \\'")
345 (defun calcAlg-mod ()
347 (if (not (calc-minibuffer-contains ".* \\'"))
349 (if (calc-minibuffer-contains ".* mod +\\'")
350 (if calc-previous-modulo
351 (insert (math-format-flat-expr calc-previous-modulo
0))
355 (defun calcAlg-previous ()
357 (if (calc-minibuffer-contains "\\'")
358 (if calc-previous-alg-entry
359 (insert calc-previous-alg-entry
)
363 (defun calcAlg-equals ()
368 (progn (setq prefix-arg
(length alg-exp
))
369 (calc-unread-command ?
=)))))
371 (defun calcAlg-escape ()
373 (calc-unread-command)
376 (use-local-map calc-mode-map
))
379 (defvar calc-plain-entry nil
)
380 (defun calcAlg-edit ()
382 (if (or (not calc-plain-entry
)
383 (calc-minibuffer-contains
384 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
386 (setq alg-exp
(minibuffer-contents))
387 (and (> (length alg-exp
) 0) (setq calc-previous-alg-entry alg-exp
))
390 (defun calcAlg-enter ()
392 (let* ((str (minibuffer-contents))
393 (exp (and (> (length str
) 0)
395 (set-buffer calc-buffer
)
396 (math-read-exprs str
)))))
397 (if (eq (car-safe exp
) 'error
)
399 (goto-char (minibuffer-prompt-end))
400 (forward-char (nth 1 exp
))
402 (calc-temp-minibuffer-message
403 (concat " [" (or (nth 2 exp
) "Error") "]"))
404 (calc-clear-unread-commands))
405 (setq alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
408 (and (> (length str
) 0) (setq calc-previous-alg-entry str
))
411 (defun calcAlg-blink-matching-open ()
412 (let ((oldpos (point))
416 (setq blinkpos
(scan-sexps oldpos -
1))
419 (> oldpos
(1+ (point-min)))
420 (or (and (= (char-after (1- oldpos
)) ?\
))
421 (= (char-after blinkpos
) ?\
[))
422 (and (= (char-after (1- oldpos
)) ?\
])
423 (= (char-after blinkpos
) ?\
()))
426 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
427 (let ((saved (aref (syntax-table) (char-after blinkpos
))))
430 (aset (syntax-table) (char-after blinkpos
)
431 (+ (logand saved
255)
432 (lsh (char-after (1- oldpos
)) 8)))
433 (blink-matching-open))
434 (aset (syntax-table) (char-after blinkpos
) saved
)))
435 (blink-matching-open))))
438 (defun calc-alg-digit-entry ()
440 (cond ((eq last-command-char ?e
)
441 (if (> calc-number-radix
14) (format "%d.^" calc-number-radix
) "1e"))
442 ((eq last-command-char ?
#) (format "%d#" calc-number-radix
))
443 ((eq last-command-char ?_
) "-")
444 ((eq last-command-char ?
@) "0@ ")
445 (t (char-to-string last-command-char
)))))
447 (defun calcDigit-algebraic ()
449 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
451 (setq calc-digit-value
(minibuffer-contents))
454 (defun calcDigit-edit ()
456 (calc-unread-command)
457 (setq calc-digit-value
(minibuffer-contents))
461 ;;; Algebraic expression parsing. [Public]
463 (defun math-read-exprs (exp-str)
466 (exp-keep-spaces nil
)
468 (if calc-language-input-filter
469 (setq exp-str
(funcall calc-language-input-filter exp-str
)))
470 (while (setq exp-token
(string-match "\\.\\.\\([^.]\\|.[^.]\\)" exp-str
))
471 (setq exp-str
(concat (substring exp-str
0 exp-token
) "\\dots"
472 (substring exp-str
(+ exp-token
2)))))
473 (math-build-parse-table)
475 (let ((val (catch 'syntax
(math-read-expr-list))))
477 (list 'error exp-old-pos val
)
478 (if (equal exp-token
'end
)
480 (list 'error exp-old-pos
"Syntax error"))))))
482 (defun math-read-expr-list ()
483 (let* ((exp-keep-spaces nil
)
484 (val (list (math-read-expr-level 0)))
486 (while (equal exp-data
",")
488 (let ((rest (list (math-read-expr-level 0))))
493 (defvar calc-user-parse-table nil
)
494 (defvar calc-last-main-parse-table nil
)
495 (defvar calc-last-lang-parse-table nil
)
496 (defvar calc-user-tokens nil
)
497 (defvar calc-user-token-chars nil
)
499 (defun math-build-parse-table ()
500 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
501 (ltab (cdr (assq calc-language calc-user-parse-tables
))))
502 (or (and (eq mtab calc-last-main-parse-table
)
503 (eq ltab calc-last-lang-parse-table
))
504 (let ((p (append mtab ltab
))
506 (setq calc-user-parse-table p
)
507 (setq calc-user-token-chars nil
)
509 (math-find-user-tokens (car (car p
)))
511 (setq calc-user-tokens
(mapconcat 'identity
512 (sort (mapcar 'car toks
)
513 (function (lambda (x y
)
517 calc-last-main-parse-table mtab
518 calc-last-lang-parse-table ltab
)))))
520 (defun math-find-user-tokens (p) ; uses "toks"
522 (cond ((and (stringp (car p
))
523 (or (> (length (car p
)) 1) (equal (car p
) "$")
524 (equal (car p
) "\""))
525 (string-match "[^a-zA-Z0-9]" (car p
)))
526 (let ((s (regexp-quote (car p
))))
527 (if (string-match "\\`[a-zA-Z0-9]" s
)
528 (setq s
(concat "\\<" s
)))
529 (if (string-match "[a-zA-Z0-9]\\'" s
)
530 (setq s
(concat s
"\\>")))
533 (setq toks
(cons (list s
) toks
))
534 (or (memq (aref (car p
) 0) calc-user-token-chars
)
535 (setq calc-user-token-chars
536 (cons (aref (car p
) 0)
537 calc-user-token-chars
)))))))
539 (math-find-user-tokens (nth 1 (car p
)))
540 (or (eq (car (car p
)) '\?)
541 (math-find-user-tokens (nth 2 (car p
))))))
544 (defun math-read-token ()
545 (if (>= exp-pos
(length exp-str
))
546 (setq exp-old-pos exp-pos
549 (let ((ch (aref exp-str exp-pos
)))
550 (setq exp-old-pos exp-pos
)
551 (cond ((memq ch
'(32 10 9))
552 (setq exp-pos
(1+ exp-pos
))
554 (setq exp-token
'space
557 ((and (memq ch calc-user-token-chars
)
558 (let ((case-fold-search nil
))
559 (eq (string-match calc-user-tokens exp-str exp-pos
)
561 (setq exp-token
'punc
562 exp-data
(math-match-substring exp-str
0)
563 exp-pos
(match-end 0)))
564 ((or (and (>= ch ?a
) (<= ch ?z
))
565 (and (>= ch ?A
) (<= ch ?Z
)))
566 (string-match (if (memq calc-language
'(c fortran pascal maple
))
570 (setq exp-token
'symbol
571 exp-pos
(match-end 0)
572 exp-data
(math-restore-dashes
573 (math-match-substring exp-str
0)))
574 (if (eq calc-language
'eqn
)
575 (let ((code (assoc exp-data math-eqn-ignore-words
)))
579 ((consp (nth 1 code
))
581 (if (assoc exp-data
(cdr code
))
582 (setq exp-data
(format "%s %s"
583 (car code
) exp-data
))))
584 ((eq (nth 1 code
) 'punc
)
585 (setq exp-token
'punc
586 exp-data
(nth 2 code
)))
589 (math-read-token))))))
590 ((or (and (>= ch ?
0) (<= ch ?
9))
592 (eq (string-match "\\.[0-9]" exp-str exp-pos
) exp-pos
))
594 (eq (string-match "_\\.?[0-9]" exp-str exp-pos
) exp-pos
)
596 (and (memq calc-language
'(nil flat big unform
598 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
599 exp-str
(1- exp-pos
))
601 (or (and (eq calc-language
'c
)
602 (string-match "0[xX][0-9a-fA-F]+" exp-str exp-pos
))
603 (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\(0*\\([2-9]\\|1[0-4]\\)\\(#\\|\\^\\^\\)[0-9a-dA-D.]+[eE][-+_]?[0-9]+\\|0*\\([2-9]\\|[0-2][0-9]\\|3[0-6]\\)\\(#\\|\\^\\^\\)[0-9a-zA-Z:.]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?" exp-str exp-pos
))
604 (setq exp-token
'number
605 exp-data
(math-match-substring exp-str
0)
606 exp-pos
(match-end 0)))
608 (if (and (eq calc-language
'pascal
)
610 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
613 (setq exp-token
'number
614 exp-data
(math-match-substring exp-str
1)
615 exp-pos
(match-end 1))
616 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" exp-str exp-pos
)
618 (setq exp-data
(- (string-to-int (math-match-substring
620 (string-match "\\$+" exp-str exp-pos
)
621 (setq exp-data
(- (match-end 0) (match-beginning 0))))
622 (setq exp-token
'dollar
623 exp-pos
(match-end 0))))
625 (if (eq (string-match "#\\([1-9][0-9]*\\)" exp-str exp-pos
)
627 (setq exp-data
(string-to-int
628 (math-match-substring exp-str
1))
629 exp-pos
(match-end 0))
631 exp-pos
(1+ exp-pos
)))
632 (setq exp-token
'hash
))
633 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
636 (setq exp-token
'punc
637 exp-data
(math-match-substring exp-str
0)
638 exp-pos
(match-end 0)))
640 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)" exp-str exp-pos
))
641 (if (eq calc-language
'eqn
)
643 (setq exp-str
(copy-sequence exp-str
))
644 (aset exp-str
(match-beginning 1) ?\
{)
645 (if (< (match-end 1) (length exp-str
))
646 (aset exp-str
(match-end 1) ?\
}))
648 (setq exp-token
'string
649 exp-data
(math-match-substring exp-str
1)
650 exp-pos
(match-end 0))))
651 ((and (= ch ?
\\) (eq calc-language
'tex
)
652 (< exp-pos
(1- (length exp-str
))))
653 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}" exp-str exp-pos
)
654 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)" exp-str exp-pos
))
655 (setq exp-token
'symbol
656 exp-pos
(match-end 0)
657 exp-data
(math-restore-dashes
658 (math-match-substring exp-str
1)))
659 (let ((code (assoc exp-data math-tex-ignore-words
)))
663 ((eq (nth 1 code
) 'punc
)
664 (setq exp-token
'punc
665 exp-data
(nth 2 code
)))
666 ((and (eq (nth 1 code
) 'mat
)
667 (string-match " *{" exp-str exp-pos
))
668 (setq exp-pos
(match-end 0)
671 (let ((right (string-match "}" exp-str exp-pos
)))
673 (setq exp-str
(copy-sequence exp-str
))
674 (aset exp-str right ?\
])))))))
675 ((and (= ch ?\.
) (eq calc-language
'fortran
)
676 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
677 exp-str exp-pos
) exp-pos
))
678 (setq exp-token
'punc
679 exp-data
(upcase (math-match-substring exp-str
0))
680 exp-pos
(match-end 0)))
681 ((and (eq calc-language
'math
)
682 (eq (string-match "\\[\\[\\|->\\|:>" exp-str exp-pos
)
684 (setq exp-token
'punc
685 exp-data
(math-match-substring exp-str
0)
686 exp-pos
(match-end 0)))
687 ((and (eq calc-language
'eqn
)
688 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
691 (setq exp-token
'punc
692 exp-data
(math-match-substring exp-str
0)
693 exp-pos
(match-end 0))
694 (and (eq (string-match "\\\\dots\\." exp-str exp-pos
) exp-pos
)
695 (setq exp-pos
(match-end 0)))
696 (if (memq (aref exp-data
0) '(?~ ?^
))
698 ((eq (string-match "%%.*$" exp-str exp-pos
) exp-pos
)
699 (setq exp-pos
(match-end 0))
702 (if (and (eq ch ?\
{) (memq calc-language
'(tex eqn
)))
704 (if (and (eq ch ?\
}) (memq calc-language
'(tex eqn
)))
706 (if (and (eq ch ?\
&) (eq calc-language
'tex
))
708 (setq exp-token
'punc
709 exp-data
(char-to-string ch
)
710 exp-pos
(1+ exp-pos
)))))))
713 (defun math-read-expr-level (exp-prec &optional exp-term
)
714 (let* ((x (math-read-factor)) (first t
) op op2
)
715 (while (and (or (and calc-user-parse-table
716 (setq op
(calc-check-user-syntax x exp-prec
))
718 op
'("2x" ident
999999 -
1)))
719 (and (setq op
(assoc exp-data math-expr-opers
))
721 (or (and (setq op2
(assoc
723 (cdr (memq op math-expr-opers
))))
724 (eq (= (nth 3 op
) -
1)
726 (eq (= (nth 3 op2
) -
1)
727 (not (math-factor-after)))
730 (and (or (eq (nth 2 op
) -
1)
731 (memq exp-token
'(symbol number dollar hash
))
733 (and (equal exp-data
"[")
734 (not (eq calc-language
'math
))
735 (not (and exp-keep-spaces
736 (eq (car-safe x
) 'vec
)))))
737 (or (not (setq op
(assoc exp-data math-expr-opers
)))
739 (or (not calc-user-parse-table
)
740 (not (eq exp-token
'symbol
))
741 (let ((p calc-user-parse-table
))
744 (car (car (car p
)))))
746 (nth 1 (car (car p
)))
750 (setq op
(assoc "2x" math-expr-opers
))))
751 (not (and exp-term
(equal exp-data exp-term
)))
752 (>= (nth 2 op
) exp-prec
))
753 (if (not (equal (car op
) "2x"))
755 (and (memq (nth 1 op
) '(sdev mod
))
757 (setq x
(cond ((consp (nth 1 op
))
758 (funcall (car (nth 1 op
)) x op
))
760 (if (eq (nth 1 op
) 'ident
)
762 (if (eq (nth 1 op
) 'closing
)
763 (if (eq (nth 2 op
) exp-prec
)
767 (throw 'syntax
"Mismatched delimiters"))
768 (list (nth 1 op
) x
))))
770 (memq (nth 1 op
) math-alg-inequalities
)
771 (memq (car-safe x
) math-alg-inequalities
))
773 (math-composite-inequalities x op
))
776 (math-read-expr-level (nth 3 op
) exp-term
))))
780 (defun calc-check-user-syntax (&optional x prec
)
781 (let ((p calc-user-parse-table
)
786 (setq rule
(car (car p
)))
788 (and (integerp (car rule
))
791 (car (setq rule
(cdr rule
)))))
792 (equal exp-data
(car rule
)))))
793 (let ((save-exp-pos exp-pos
)
794 (save-exp-old-pos exp-old-pos
)
795 (save-exp-token exp-token
)
796 (save-exp-data exp-data
))
798 (setq matches
(calc-match-user-syntax rule
))))
805 (setq matches
(cons x matches
)))
806 (setq match
(cdr (car p
)))
807 (while (and (eq (car-safe match
)
809 (= (length match
) 3))
810 (setq conds
(append (math-flatten-lands
813 match
(nth 1 match
)))
814 (while (and conds match
)
816 (cond ((eq (car-safe (car conds
))
818 (setq temp
(car conds
))
819 (or (= (length temp
) 3)
820 (and (= (length temp
) 2)
821 (eq (car-safe (nth 1 temp
))
823 (= (length (nth 1 temp
)) 3)
824 (setq temp
(nth 1 temp
)))
832 args
(cons (nth 1 temp
)
834 ((and (eq (car-safe (car conds
))
836 (= (length (car conds
)) 3))
837 (setq temp
(calcFunc-vmatches
841 (nth 2 (car conds
))))
844 (while (setq temp
(cdr temp
))
845 (setq matches
(cons (nth 2 (car temp
))
847 args
(cons (nth 1 (car temp
))
850 (or (math-is-true (math-simplify
855 (setq conds
(cdr conds
)))
857 (not (setq match
(math-multi-subst
858 match args matches
)))
859 (setq exp-old-pos save-exp-old-pos
860 exp-token save-exp-token
861 exp-data save-exp-data
862 exp-pos save-exp-pos
)))))))
866 (defun calc-match-user-syntax (p &optional term
)
868 (save-exp-pos exp-pos
)
869 (save-exp-old-pos exp-old-pos
)
870 (save-exp-token exp-token
)
871 (save-exp-data exp-data
))
873 (cond ((stringp (car p
))
874 (and (equal exp-data
(car p
))
879 (and (setq m
(catch 'syntax
880 (math-read-expr-level
883 (if (consp (nth 1 p
))
884 (car (nth 1 (nth 1 p
)))
888 (setq matches
(nconc matches
(list m
)))))
889 ((eq (car (car p
)) '\?)
890 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
895 (cons 'vec
(and (listp m
) m
))))))
896 (or (listp m
) (not (nth 2 (car p
)))
897 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
898 (eq exp-token
'end
)))
900 (setq m
(calc-match-user-syntax (nth 1 (car p
))
901 (car (nth 2 (car p
)))))
903 (let ((vec (cons 'vec m
))
907 mm
(calc-match-user-syntax
910 (car (nth 2 (car p
))))))
912 (setq vec
(nconc vec mm
)))
913 (setq matches
(nconc matches
(list vec
))))
914 (and (eq (car (car p
)) '*)
915 (setq matches
(nconc matches
(list '(vec)))))))))
918 (setq exp-pos save-exp-pos
919 exp-old-pos save-exp-old-pos
920 exp-token save-exp-token
921 exp-data save-exp-data
925 (defconst math-alg-inequalities
926 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
927 calcFunc-eq calcFunc-neq
))
929 (defun math-remove-dashes (x)
930 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
932 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
935 (defun math-restore-dashes (x)
936 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
938 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
941 (defun math-read-if (cond op
)
942 (let ((then (math-read-expr-level 0)))
943 (or (equal exp-data
":")
944 (throw 'syntax
"Expected ':'"))
946 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
948 (defun math-factor-after ()
949 (let ((exp-pos exp-pos
)
950 exp-old-pos exp-token exp-data
)
952 (or (memq exp-token
'(number symbol dollar hash string
))
953 (and (assoc exp-data
'(("-") ("+") ("!") ("|") ("/")))
954 (assoc (concat "u" exp-data
) math-expr-opers
))
955 (eq (nth 2 (assoc exp-data math-expr-opers
)) -
1)
956 (assoc exp-data
'(("(") ("[") ("{"))))))
958 (defun math-read-factor ()
960 (cond ((eq exp-token
'number
)
961 (let ((num (math-read-number exp-data
)))
964 (setq exp-old-pos exp-pos
)
965 (throw 'syntax
"Bad format")))
967 (if (and math-read-expr-quotes
971 ((and calc-user-parse-table
972 (setq op
(calc-check-user-syntax)))
974 ((or (equal exp-data
"-")
978 (equal exp-data
"/"))
979 (setq exp-data
(concat "u" exp-data
))
981 ((and (setq op
(assoc exp-data math-expr-opers
))
983 (if (consp (nth 1 op
))
984 (funcall (car (nth 1 op
)) op
)
986 (let ((val (math-read-expr-level (nth 3 op
))))
987 (cond ((eq (nth 1 op
) 'ident
)
989 ((and (Math-numberp val
)
990 (equal (car op
) "u-"))
992 (t (list (nth 1 op
) val
))))))
993 ((eq exp-token
'symbol
)
994 (let ((sym (intern exp-data
)))
996 (if (equal exp-data calc-function-open
)
997 (let ((f (assq sym math-expr-function-mapping
)))
1000 (funcall (car (cdr f
)) f sym
)
1001 (let ((args (if (or (equal exp-data calc-function-close
)
1002 (eq exp-token
'end
))
1004 (math-read-expr-list))))
1005 (if (not (or (equal exp-data calc-function-close
)
1006 (eq exp-token
'end
)))
1007 (throw 'syntax
"Expected `)'"))
1009 (if (and (eq calc-language
'fortran
) args
1011 (let ((calc-matrix-mode 'scalar
))
1016 (symbol-name sym
)))))))
1017 (math-parse-fortran-subscr sym args
)
1020 (and (= (aref (symbol-name sym
) 0) ?
\\)
1021 (< (prefix-numeric-value calc-language-option
)
1023 (setq sym
(intern (substring (symbol-name sym
)
1025 (or (string-match "-" (symbol-name sym
))
1028 (symbol-name sym
))))))
1030 (if math-read-expr-quotes
1032 (let ((val (list 'var
1033 (intern (math-remove-dashes
1035 (if (string-match "-" (symbol-name sym
))
1037 (intern (concat "var-"
1038 (symbol-name sym
)))))))
1039 (let ((v (assq (nth 1 val
) math-expr-variable-mapping
)))
1040 (and v
(setq val
(if (consp (cdr v
))
1041 (funcall (car (cdr v
)) v val
)
1044 (substring (symbol-name (cdr v
))
1047 (while (and (memq calc-language
'(c pascal maple
))
1048 (equal exp-data
"["))
1050 (setq val
(append (list 'calcFunc-subscr val
)
1051 (math-read-expr-list)))
1052 (if (equal exp-data
"]")
1054 (throw 'syntax
"Expected ']'")))
1056 ((eq exp-token
'dollar
)
1057 (let ((abs (if (> exp-data
0) exp-data
(- exp-data
))))
1058 (if (>= (length calc-dollar-values
) abs
)
1059 (let ((num exp-data
))
1061 (setq calc-dollar-used
(max calc-dollar-used num
))
1062 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1063 (throw 'syntax
(if calc-dollar-values
1065 "$'s not allowed in this context")))))
1066 ((eq exp-token
'hash
)
1067 (or calc-hashes-used
1068 (throw 'syntax
"#'s not allowed in this context"))
1070 (if (<= exp-data
(length calc-arg-values
))
1071 (let ((num exp-data
))
1073 (setq calc-hashes-used
(max calc-hashes-used num
))
1074 (nth (1- num
) calc-arg-values
))
1075 (throw 'syntax
"Too many # arguments")))
1076 ((equal exp-data
"(")
1077 (let* ((exp (let ((exp-keep-spaces nil
))
1079 (if (or (equal exp-data
"\\dots")
1080 (equal exp-data
"\\ldots"))
1081 '(neg (var inf var-inf
))
1082 (math-read-expr-level 0)))))
1083 (let ((exp-keep-spaces nil
))
1085 ((equal exp-data
",")
1088 (let ((exp2 (math-read-expr-level 0)))
1090 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1091 (math-normalize (list 'cplx exp exp2
))
1092 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1093 ((equal exp-data
";")
1096 (let ((exp2 (math-read-expr-level 0)))
1097 (setq exp
(if (and exp2
(Math-realp exp
)
1099 (math-normalize (list 'polar exp exp2
))
1104 (math-to-radians-2 exp2
)
1105 '(var i var-i
)))))))))
1106 ((or (equal exp-data
"\\dots")
1107 (equal exp-data
"\\ldots"))
1110 (let ((exp2 (if (or (equal exp-data
")")
1111 (equal exp-data
"]")
1112 (eq exp-token
'end
))
1114 (math-read-expr-level 0))))
1117 (if (equal exp-data
")") 0 1)
1120 (if (not (or (equal exp-data
")")
1121 (and (equal exp-data
"]") (eq (car-safe exp
) 'intv
))
1122 (eq exp-token
'end
)))
1123 (throw 'syntax
"Expected `)'"))
1126 ((eq exp-token
'string
)
1129 ((equal exp-data
"[")
1131 (math-read-brackets t
"]"))
1132 ((equal exp-data
"{")
1134 (math-read-brackets nil
"}"))
1135 ((equal exp-data
"<")
1137 (math-read-angle-brackets))
1138 (t (throw 'syntax
"Expected a number")))))
1140 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1141 ;;; calc-aent.el ends here