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 (kill-new shortbuf
)))))
106 (defun calc-do-calc-eval (str separator args
)
112 ((and (consp str
) (not (symbolp (car str
))))
113 (let ((calc-language nil
)
114 (math-expr-opers math-standard-opers
)
115 (calc-internal-prec 12)
117 (calc-symbolic-mode nil
)
118 (calc-matrix-mode nil
)
119 (calc-angle-mode 'deg
)
120 (calc-number-radix 10)
121 (calc-leading-zeros nil
)
122 (calc-group-digits nil
)
123 (calc-point-char ".")
124 (calc-frac-format '(":" nil
))
125 (calc-prefer-frac nil
)
126 (calc-hms-format "%s@ %s' %s\"")
127 (calc-date-format '((H ":" mm C SS pp
" ")
128 Www
" " Mmm
" " D
", " YYYY
))
129 (calc-float-format '(float 0))
130 (calc-full-float-format '(float 0))
131 (calc-complex-format nil
)
132 (calc-matrix-just nil
)
133 (calc-full-vectors t
)
134 (calc-break-vectors nil
)
135 (calc-vector-commas ",")
136 (calc-vector-brackets "[]")
137 (calc-matrix-brackets '(R O
))
138 (calc-complex-mode 'cplx
)
139 (calc-infinite-mode nil
)
140 (calc-display-strings nil
)
141 (calc-simplify-mode nil
)
142 (calc-display-working-message 'lots
)
145 (set (car strp
) (nth 1 strp
))
146 (setq strp
(cdr (cdr strp
))))
147 (calc-do-calc-eval (car str
) separator args
)))
148 ((eq separator
'eval
)
150 ((eq separator
'macro
)
152 (let* ((calc-buffer (current-buffer))
153 (calc-window (get-buffer-window calc-buffer
))
154 (save-window (selected-window)))
158 (select-window calc-window
)
159 (calc-execute-kbd-macro str nil
(car args
)))
160 (and (window-point save-window
)
161 (select-window save-window
)))
162 (save-window-excursion
163 (select-window (get-largest-window))
164 (switch-to-buffer calc-buffer
)
165 (calc-execute-kbd-macro str nil
(car args
)))))
168 (or (not (integerp str
))
170 (calc-pop (min str
(calc-stack-size))))
175 (<= str
(calc-stack-size))
176 (math-format-value (calc-top-n str
(car args
)) 1000)))
177 ((eq separator
'rawtop
)
180 (<= str
(calc-stack-size))
181 (calc-top-n str
(car args
))))
183 (let* ((calc-command-flags nil
)
185 (calc-language (if (memq calc-language
'(nil big
))
186 'flat calc-language
))
187 (calc-dollar-values (mapcar
192 (setq x
(math-read-exprs x
))
202 (res (if (stringp str
)
203 (math-read-exprs str
)
206 (if (eq (car res
) 'error
)
207 (calc-eval-error (cdr res
))
208 (setq res
(mapcar 'calc-normalize res
))
209 (and (memq 'clear-message calc-command-flags
)
211 (cond ((eq separator
'pred
)
213 (if (= (length res
) 1)
214 (math-is-true (car res
))
215 (calc-eval-error '(0 "Single value expected"))))
217 (if (= (length res
) 1)
219 (calc-eval-error '(0 "Single value expected"))))
220 ((eq separator
'list
)
222 ((memq separator
'(num rawnum
))
223 (if (= (length res
) 1)
224 (if (math-constp (car res
))
225 (if (eq separator
'num
)
226 (math-format-value (car res
) 1000)
231 (calc-explain-why (car calc-next-why
))
232 "Number expected"))))
233 (calc-eval-error '(0 "Single value expected"))))
234 ((eq separator
'push
)
238 (setq buf
(concat buf
239 (and buf
(or separator
", "))
240 (math-format-value (car res
) 1000))
244 (defun calc-eval-error (msg)
245 (if (and (boundp 'calc-eval-error
)
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 calc-extensions-loaded
(car alg-exp
) (car nvals
))
284 (calc-pop-push-record-list calc-dollar-used
285 (and (not (equal (car alg-exp
)
287 calc-extensions-loaded
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
))
389 (defun calcAlg-enter ()
391 (let* ((str (minibuffer-contents))
392 (exp (and (> (length str
) 0)
394 (set-buffer calc-buffer
)
395 (math-read-exprs str
)))))
396 (if (eq (car-safe exp
) 'error
)
398 (goto-char (minibuffer-prompt-end))
399 (forward-char (nth 1 exp
))
401 (calc-temp-minibuffer-message
402 (concat " [" (or (nth 2 exp
) "Error") "]"))
403 (calc-clear-unread-commands))
404 (setq calc-alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
407 (and (> (length str
) 0) (setq calc-previous-alg-entry str
))
410 (defun calcAlg-blink-matching-open ()
411 (let ((oldpos (point))
415 (setq blinkpos
(scan-sexps oldpos -
1))
418 (> oldpos
(1+ (point-min)))
419 (or (and (= (char-after (1- oldpos
)) ?\
))
420 (= (char-after blinkpos
) ?\
[))
421 (and (= (char-after (1- oldpos
)) ?\
])
422 (= (char-after blinkpos
) ?\
()))
425 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
426 (let ((saved (aref (syntax-table) (char-after blinkpos
))))
429 (aset (syntax-table) (char-after blinkpos
)
430 (+ (logand saved
255)
431 (lsh (char-after (1- oldpos
)) 8)))
432 (blink-matching-open))
433 (aset (syntax-table) (char-after blinkpos
) saved
)))
434 (blink-matching-open))))
437 (defun calc-alg-digit-entry ()
439 (cond ((eq last-command-char ?e
)
440 (if (> calc-number-radix
14) (format "%d.^" calc-number-radix
) "1e"))
441 ((eq last-command-char ?
#) (format "%d#" calc-number-radix
))
442 ((eq last-command-char ?_
) "-")
443 ((eq last-command-char ?
@) "0@ ")
444 (t (char-to-string last-command-char
)))))
446 (defun calcDigit-algebraic ()
448 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
450 (setq calc-digit-value
(minibuffer-contents))
453 (defun calcDigit-edit ()
455 (calc-unread-command)
456 (setq calc-digit-value
(minibuffer-contents))
460 ;;; Algebraic expression parsing. [Public]
462 ;;; The next few variables are local to math-read-exprs (and math-read-expr)
463 ;;; but are set in functions they call.
465 (defvar math-exp-pos
)
466 (defvar math-exp-str
)
467 (defvar math-exp-old-pos
)
468 (defvar math-exp-token
)
469 (defvar math-exp-keep-spaces
)
471 (defun math-read-exprs (math-exp-str)
472 (let ((math-exp-pos 0)
474 (math-exp-keep-spaces nil
)
475 math-exp-token math-expr-data
)
476 (if calc-language-input-filter
477 (setq math-exp-str
(funcall calc-language-input-filter math-exp-str
)))
478 (while (setq math-exp-token
(string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str
))
479 (setq math-exp-str
(concat (substring math-exp-str
0 math-exp-token
) "\\dots"
480 (substring math-exp-str
(+ math-exp-token
2)))))
481 (math-build-parse-table)
483 (let ((val (catch 'syntax
(math-read-expr-list))))
485 (list 'error math-exp-old-pos val
)
486 (if (equal math-exp-token
'end
)
488 (list 'error math-exp-old-pos
"Syntax error"))))))
490 (defun math-read-expr-list ()
491 (let* ((math-exp-keep-spaces nil
)
492 (val (list (math-read-expr-level 0)))
494 (while (equal math-expr-data
",")
496 (let ((rest (list (math-read-expr-level 0))))
501 (defvar calc-user-parse-table nil
)
502 (defvar calc-last-main-parse-table nil
)
503 (defvar calc-last-lang-parse-table nil
)
504 (defvar calc-user-tokens nil
)
505 (defvar calc-user-token-chars nil
)
507 (defvar math-toks nil
508 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
510 (defun math-build-parse-table ()
511 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
512 (ltab (cdr (assq calc-language calc-user-parse-tables
))))
513 (or (and (eq mtab calc-last-main-parse-table
)
514 (eq ltab calc-last-lang-parse-table
))
515 (let ((p (append mtab ltab
))
517 (setq calc-user-parse-table p
)
518 (setq calc-user-token-chars nil
)
520 (math-find-user-tokens (car (car p
)))
522 (setq calc-user-tokens
(mapconcat 'identity
523 (sort (mapcar 'car math-toks
)
524 (function (lambda (x y
)
528 calc-last-main-parse-table mtab
529 calc-last-lang-parse-table ltab
)))))
531 (defun math-find-user-tokens (p)
533 (cond ((and (stringp (car p
))
534 (or (> (length (car p
)) 1) (equal (car p
) "$")
535 (equal (car p
) "\""))
536 (string-match "[^a-zA-Z0-9]" (car p
)))
537 (let ((s (regexp-quote (car p
))))
538 (if (string-match "\\`[a-zA-Z0-9]" s
)
539 (setq s
(concat "\\<" s
)))
540 (if (string-match "[a-zA-Z0-9]\\'" s
)
541 (setq s
(concat s
"\\>")))
542 (or (assoc s math-toks
)
544 (setq math-toks
(cons (list s
) math-toks
))
545 (or (memq (aref (car p
) 0) calc-user-token-chars
)
546 (setq calc-user-token-chars
547 (cons (aref (car p
) 0)
548 calc-user-token-chars
)))))))
550 (math-find-user-tokens (nth 1 (car p
)))
551 (or (eq (car (car p
)) '\?)
552 (math-find-user-tokens (nth 2 (car p
))))))
555 (defun math-read-token ()
556 (if (>= math-exp-pos
(length math-exp-str
))
557 (setq math-exp-old-pos math-exp-pos
559 math-expr-data
"\000")
560 (let ((ch (aref math-exp-str math-exp-pos
)))
561 (setq math-exp-old-pos math-exp-pos
)
562 (cond ((memq ch
'(32 10 9))
563 (setq math-exp-pos
(1+ math-exp-pos
))
564 (if math-exp-keep-spaces
565 (setq math-exp-token
'space
568 ((and (memq ch calc-user-token-chars
)
569 (let ((case-fold-search nil
))
570 (eq (string-match calc-user-tokens math-exp-str math-exp-pos
)
572 (setq math-exp-token
'punc
573 math-expr-data
(math-match-substring math-exp-str
0)
574 math-exp-pos
(match-end 0)))
575 ((or (and (>= ch ?a
) (<= ch ?z
))
576 (and (>= ch ?A
) (<= ch ?Z
)))
577 (string-match (if (memq calc-language
'(c fortran pascal maple
))
580 math-exp-str math-exp-pos
)
581 (setq math-exp-token
'symbol
582 math-exp-pos
(match-end 0)
583 math-expr-data
(math-restore-dashes
584 (math-match-substring math-exp-str
0)))
585 (if (eq calc-language
'eqn
)
586 (let ((code (assoc math-expr-data math-eqn-ignore-words
)))
590 ((consp (nth 1 code
))
592 (if (assoc math-expr-data
(cdr code
))
593 (setq math-expr-data
(format "%s %s"
594 (car code
) math-expr-data
))))
595 ((eq (nth 1 code
) 'punc
)
596 (setq math-exp-token
'punc
597 math-expr-data
(nth 2 code
)))
600 (math-read-token))))))
601 ((or (and (>= ch ?
0) (<= ch ?
9))
603 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos
)
606 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos
)
608 (or (eq math-exp-pos
0)
609 (and (memq calc-language
'(nil flat big unform
611 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
612 math-exp-str
(1- math-exp-pos
))
613 (1- math-exp-pos
))))))
614 (or (and (eq calc-language
'c
)
615 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos
))
616 (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]+\\)?\"?\\)?"
617 math-exp-str math-exp-pos
))
618 (setq math-exp-token
'number
619 math-expr-data
(math-match-substring math-exp-str
0)
620 math-exp-pos
(match-end 0)))
622 (if (and (eq calc-language
'pascal
)
624 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
625 math-exp-str math-exp-pos
)
627 (setq math-exp-token
'number
628 math-expr-data
(math-match-substring math-exp-str
1)
629 math-exp-pos
(match-end 1))
630 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
632 (setq math-expr-data
(- (string-to-int (math-match-substring
634 (string-match "\\$+" math-exp-str math-exp-pos
)
635 (setq math-expr-data
(- (match-end 0) (match-beginning 0))))
636 (setq math-exp-token
'dollar
637 math-exp-pos
(match-end 0))))
639 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
641 (setq math-expr-data
(string-to-int
642 (math-match-substring math-exp-str
1))
643 math-exp-pos
(match-end 0))
644 (setq math-expr-data
1
645 math-exp-pos
(1+ math-exp-pos
)))
646 (setq math-exp-token
'hash
))
647 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
648 math-exp-str math-exp-pos
)
650 (setq math-exp-token
'punc
651 math-expr-data
(math-match-substring math-exp-str
0)
652 math-exp-pos
(match-end 0)))
654 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
655 math-exp-str math-exp-pos
))
656 (if (eq calc-language
'eqn
)
658 (setq math-exp-str
(copy-sequence math-exp-str
))
659 (aset math-exp-str
(match-beginning 1) ?\
{)
660 (if (< (match-end 1) (length math-exp-str
))
661 (aset math-exp-str
(match-end 1) ?\
}))
663 (setq math-exp-token
'string
664 math-expr-data
(math-match-substring math-exp-str
1)
665 math-exp-pos
(match-end 0))))
666 ((and (= ch ?
\\) (eq calc-language
'tex
)
667 (< math-exp-pos
(1- (length math-exp-str
))))
668 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
669 math-exp-str math-exp-pos
)
670 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
671 math-exp-str math-exp-pos
))
672 (setq math-exp-token
'symbol
673 math-exp-pos
(match-end 0)
674 math-expr-data
(math-restore-dashes
675 (math-match-substring math-exp-str
1)))
676 (let ((code (assoc math-expr-data math-tex-ignore-words
)))
680 ((eq (nth 1 code
) 'punc
)
681 (setq math-exp-token
'punc
682 math-expr-data
(nth 2 code
)))
683 ((and (eq (nth 1 code
) 'mat
)
684 (string-match " *{" math-exp-str math-exp-pos
))
685 (setq math-exp-pos
(match-end 0)
688 (let ((right (string-match "}" math-exp-str math-exp-pos
)))
690 (setq math-exp-str
(copy-sequence math-exp-str
))
691 (aset math-exp-str right ?\
])))))))
692 ((and (= ch ?\.
) (eq calc-language
'fortran
)
693 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
694 math-exp-str math-exp-pos
) math-exp-pos
))
695 (setq math-exp-token
'punc
696 math-expr-data
(upcase (math-match-substring math-exp-str
0))
697 math-exp-pos
(match-end 0)))
698 ((and (eq calc-language
'math
)
699 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos
)
701 (setq math-exp-token
'punc
702 math-expr-data
(math-match-substring math-exp-str
0)
703 math-exp-pos
(match-end 0)))
704 ((and (eq calc-language
'eqn
)
705 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
706 math-exp-str math-exp-pos
)
708 (setq math-exp-token
'punc
709 math-expr-data
(math-match-substring math-exp-str
0)
710 math-exp-pos
(match-end 0))
711 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos
)
713 (setq math-exp-pos
(match-end 0)))
714 (if (memq (aref math-expr-data
0) '(?~ ?^
))
716 ((eq (string-match "%%.*$" math-exp-str math-exp-pos
) math-exp-pos
)
717 (setq math-exp-pos
(match-end 0))
720 (if (and (eq ch ?\
{) (memq calc-language
'(tex eqn
)))
722 (if (and (eq ch ?\
}) (memq calc-language
'(tex eqn
)))
724 (if (and (eq ch ?\
&) (eq calc-language
'tex
))
726 (setq math-exp-token
'punc
727 math-expr-data
(char-to-string ch
)
728 math-exp-pos
(1+ math-exp-pos
)))))))
731 (defun math-read-expr-level (exp-prec &optional exp-term
)
732 (let* ((x (math-read-factor)) (first t
) op op2
)
733 (while (and (or (and calc-user-parse-table
734 (setq op
(calc-check-user-syntax x exp-prec
))
736 op
'("2x" ident
999999 -
1)))
737 (and (setq op
(assoc math-expr-data math-expr-opers
))
739 (or (and (setq op2
(assoc
741 (cdr (memq op math-expr-opers
))))
742 (eq (= (nth 3 op
) -
1)
744 (eq (= (nth 3 op2
) -
1)
745 (not (math-factor-after)))
748 (and (or (eq (nth 2 op
) -
1)
749 (memq math-exp-token
'(symbol number dollar hash
))
750 (equal math-expr-data
"(")
751 (and (equal math-expr-data
"[")
752 (not (eq calc-language
'math
))
753 (not (and math-exp-keep-spaces
754 (eq (car-safe x
) 'vec
)))))
755 (or (not (setq op
(assoc math-expr-data math-expr-opers
)))
757 (or (not calc-user-parse-table
)
758 (not (eq math-exp-token
'symbol
))
759 (let ((p calc-user-parse-table
))
762 (car (car (car p
)))))
764 (nth 1 (car (car p
)))
768 (setq op
(assoc "2x" math-expr-opers
))))
769 (not (and exp-term
(equal math-expr-data exp-term
)))
770 (>= (nth 2 op
) exp-prec
))
771 (if (not (equal (car op
) "2x"))
773 (and (memq (nth 1 op
) '(sdev mod
))
775 (setq x
(cond ((consp (nth 1 op
))
776 (funcall (car (nth 1 op
)) x op
))
778 (if (eq (nth 1 op
) 'ident
)
780 (if (eq (nth 1 op
) 'closing
)
781 (if (eq (nth 2 op
) exp-prec
)
785 (throw 'syntax
"Mismatched delimiters"))
786 (list (nth 1 op
) x
))))
788 (memq (nth 1 op
) math-alg-inequalities
)
789 (memq (car-safe x
) math-alg-inequalities
))
791 (math-composite-inequalities x op
))
794 (math-read-expr-level (nth 3 op
) exp-term
))))
798 (defun calc-check-user-syntax (&optional x prec
)
799 (let ((p calc-user-parse-table
)
804 (setq rule
(car (car p
)))
806 (and (integerp (car rule
))
808 (equal math-expr-data
809 (car (setq rule
(cdr rule
)))))
810 (equal math-expr-data
(car rule
)))))
811 (let ((save-exp-pos math-exp-pos
)
812 (save-exp-old-pos math-exp-old-pos
)
813 (save-exp-token math-exp-token
)
814 (save-exp-data math-expr-data
))
816 (setq matches
(calc-match-user-syntax rule
))))
823 (setq matches
(cons x matches
)))
824 (setq match
(cdr (car p
)))
825 (while (and (eq (car-safe match
)
827 (= (length match
) 3))
828 (setq conds
(append (math-flatten-lands
831 match
(nth 1 match
)))
832 (while (and conds match
)
834 (cond ((eq (car-safe (car conds
))
836 (setq temp
(car conds
))
837 (or (= (length temp
) 3)
838 (and (= (length temp
) 2)
839 (eq (car-safe (nth 1 temp
))
841 (= (length (nth 1 temp
)) 3)
842 (setq temp
(nth 1 temp
)))
850 args
(cons (nth 1 temp
)
852 ((and (eq (car-safe (car conds
))
854 (= (length (car conds
)) 3))
855 (setq temp
(calcFunc-vmatches
859 (nth 2 (car conds
))))
862 (while (setq temp
(cdr temp
))
863 (setq matches
(cons (nth 2 (car temp
))
865 args
(cons (nth 1 (car temp
))
868 (or (math-is-true (math-simplify
873 (setq conds
(cdr conds
)))
875 (not (setq match
(math-multi-subst
876 match args matches
)))
877 (setq math-exp-old-pos save-exp-old-pos
878 math-exp-token save-exp-token
879 math-expr-data save-exp-data
880 math-exp-pos save-exp-pos
)))))))
884 (defun calc-match-user-syntax (p &optional term
)
886 (save-exp-pos math-exp-pos
)
887 (save-exp-old-pos math-exp-old-pos
)
888 (save-exp-token math-exp-token
)
889 (save-exp-data math-expr-data
)
892 (cond ((stringp (car p
))
893 (and (equal math-expr-data
(car p
))
898 (and (setq m
(catch 'syntax
899 (math-read-expr-level
902 (if (consp (nth 1 p
))
903 (car (nth 1 (nth 1 p
)))
907 (setq matches
(nconc matches
(list m
)))))
908 ((eq (car (car p
)) '\?)
909 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
914 (cons 'vec
(and (listp m
) m
))))))
915 (or (listp m
) (not (nth 2 (car p
)))
916 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
917 (eq math-exp-token
'end
)))
919 (setq m
(calc-match-user-syntax (nth 1 (car p
))
920 (car (nth 2 (car p
)))))
922 (let ((vec (cons 'vec m
))
925 (setq opos math-exp-pos
926 mm
(calc-match-user-syntax
929 (car (nth 2 (car p
))))))
930 (> math-exp-pos opos
))
931 (setq vec
(nconc vec mm
)))
932 (setq matches
(nconc matches
(list vec
))))
933 (and (eq (car (car p
)) '*)
934 (setq matches
(nconc matches
(list '(vec)))))))))
937 (setq math-exp-pos save-exp-pos
938 math-exp-old-pos save-exp-old-pos
939 math-exp-token save-exp-token
940 math-expr-data save-exp-data
944 (defconst math-alg-inequalities
945 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
946 calcFunc-eq calcFunc-neq
))
948 (defun math-remove-dashes (x)
949 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
951 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
954 (defun math-restore-dashes (x)
955 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
957 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
960 (defun math-read-if (cond op
)
961 (let ((then (math-read-expr-level 0)))
962 (or (equal math-expr-data
":")
963 (throw 'syntax
"Expected ':'"))
965 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
967 (defun math-factor-after ()
968 (let ((math-exp-pos math-exp-pos
)
969 math-exp-old-pos math-exp-token math-expr-data
)
971 (or (memq math-exp-token
'(number symbol dollar hash string
))
972 (and (assoc math-expr-data
'(("-") ("+") ("!") ("|") ("/")))
973 (assoc (concat "u" math-expr-data
) math-expr-opers
))
974 (eq (nth 2 (assoc math-expr-data math-expr-opers
)) -
1)
975 (assoc math-expr-data
'(("(") ("[") ("{"))))))
977 (defun math-read-factor ()
979 (cond ((eq math-exp-token
'number
)
980 (let ((num (math-read-number math-expr-data
)))
983 (setq math-exp-old-pos math-exp-pos
)
984 (throw 'syntax
"Bad format")))
986 (if (and math-read-expr-quotes
990 ((and calc-user-parse-table
991 (setq op
(calc-check-user-syntax)))
993 ((or (equal math-expr-data
"-")
994 (equal math-expr-data
"+")
995 (equal math-expr-data
"!")
996 (equal math-expr-data
"|")
997 (equal math-expr-data
"/"))
998 (setq math-expr-data
(concat "u" math-expr-data
))
1000 ((and (setq op
(assoc math-expr-data math-expr-opers
))
1002 (if (consp (nth 1 op
))
1003 (funcall (car (nth 1 op
)) op
)
1005 (let ((val (math-read-expr-level (nth 3 op
))))
1006 (cond ((eq (nth 1 op
) 'ident
)
1008 ((and (Math-numberp val
)
1009 (equal (car op
) "u-"))
1011 (t (list (nth 1 op
) val
))))))
1012 ((eq math-exp-token
'symbol
)
1013 (let ((sym (intern math-expr-data
)))
1015 (if (equal math-expr-data calc-function-open
)
1016 (let ((f (assq sym math-expr-function-mapping
)))
1019 (funcall (car (cdr f
)) f sym
)
1020 (let ((args (if (or (equal math-expr-data calc-function-close
)
1021 (eq math-exp-token
'end
))
1023 (math-read-expr-list))))
1024 (if (not (or (equal math-expr-data calc-function-close
)
1025 (eq math-exp-token
'end
)))
1026 (throw 'syntax
"Expected `)'"))
1028 (if (and (eq calc-language
'fortran
) args
1030 (let ((calc-matrix-mode 'scalar
))
1035 (symbol-name sym
)))))))
1036 (math-parse-fortran-subscr sym args
)
1039 (and (= (aref (symbol-name sym
) 0) ?
\\)
1040 (< (prefix-numeric-value calc-language-option
)
1042 (setq sym
(intern (substring (symbol-name sym
)
1044 (or (string-match "-" (symbol-name sym
))
1047 (symbol-name sym
))))))
1049 (if math-read-expr-quotes
1051 (let ((val (list 'var
1052 (intern (math-remove-dashes
1054 (if (string-match "-" (symbol-name sym
))
1056 (intern (concat "var-"
1057 (symbol-name sym
)))))))
1058 (let ((v (assq (nth 1 val
) math-expr-variable-mapping
)))
1059 (and v
(setq val
(if (consp (cdr v
))
1060 (funcall (car (cdr v
)) v val
)
1063 (substring (symbol-name (cdr v
))
1066 (while (and (memq calc-language
'(c pascal maple
))
1067 (equal math-expr-data
"["))
1069 (setq val
(append (list 'calcFunc-subscr val
)
1070 (math-read-expr-list)))
1071 (if (equal math-expr-data
"]")
1073 (throw 'syntax
"Expected ']'")))
1075 ((eq math-exp-token
'dollar
)
1076 (let ((abs (if (> math-expr-data
0) math-expr-data
(- math-expr-data
))))
1077 (if (>= (length calc-dollar-values
) abs
)
1078 (let ((num math-expr-data
))
1080 (setq calc-dollar-used
(max calc-dollar-used num
))
1081 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1082 (throw 'syntax
(if calc-dollar-values
1084 "$'s not allowed in this context")))))
1085 ((eq math-exp-token
'hash
)
1086 (or calc-hashes-used
1087 (throw 'syntax
"#'s not allowed in this context"))
1089 (if (<= math-expr-data
(length calc-arg-values
))
1090 (let ((num math-expr-data
))
1092 (setq calc-hashes-used
(max calc-hashes-used num
))
1093 (nth (1- num
) calc-arg-values
))
1094 (throw 'syntax
"Too many # arguments")))
1095 ((equal math-expr-data
"(")
1096 (let* ((exp (let ((math-exp-keep-spaces nil
))
1098 (if (or (equal math-expr-data
"\\dots")
1099 (equal math-expr-data
"\\ldots"))
1100 '(neg (var inf var-inf
))
1101 (math-read-expr-level 0)))))
1102 (let ((math-exp-keep-spaces nil
))
1104 ((equal math-expr-data
",")
1107 (let ((exp2 (math-read-expr-level 0)))
1109 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1110 (math-normalize (list 'cplx exp exp2
))
1111 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1112 ((equal math-expr-data
";")
1115 (let ((exp2 (math-read-expr-level 0)))
1116 (setq exp
(if (and exp2
(Math-realp exp
)
1118 (math-normalize (list 'polar exp exp2
))
1123 (math-to-radians-2 exp2
)
1124 '(var i var-i
)))))))))
1125 ((or (equal math-expr-data
"\\dots")
1126 (equal math-expr-data
"\\ldots"))
1129 (let ((exp2 (if (or (equal math-expr-data
")")
1130 (equal math-expr-data
"]")
1131 (eq math-exp-token
'end
))
1133 (math-read-expr-level 0))))
1136 (if (equal math-expr-data
")") 0 1)
1139 (if (not (or (equal math-expr-data
")")
1140 (and (equal math-expr-data
"]") (eq (car-safe exp
) 'intv
))
1141 (eq math-exp-token
'end
)))
1142 (throw 'syntax
"Expected `)'"))
1145 ((eq math-exp-token
'string
)
1148 ((equal math-expr-data
"[")
1150 (math-read-brackets t
"]"))
1151 ((equal math-expr-data
"{")
1153 (math-read-brackets nil
"}"))
1154 ((equal math-expr-data
"<")
1156 (math-read-angle-brackets))
1157 (t (throw 'syntax
"Expected a number")))))
1159 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1160 ;;; calc-aent.el ends here