(show-paren-function): Change calls to syntax-after
[emacs.git] / lisp / calc / calc-aent.el
blob2db722ccb2dd7274a2774f3b2a39b80512ec5c23
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.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This file is autoloaded from calc.el.
31 (require 'calc)
33 (require 'calc-macs)
34 (eval-when-compile '(require calc-macs))
36 (defun calc-Need-calc-aent () nil)
39 (defun calc-do-quick-calc ()
40 (calc-check-defines)
41 (if (eq major-mode 'calc-mode)
42 (calc-algebraic-entry t)
43 (let (buf shortbuf)
44 (save-excursion
45 (calc-create-buffer)
46 (let* ((calc-command-flags nil)
47 (calc-dollar-values calc-quick-prev-results)
48 (calc-dollar-used 0)
49 (enable-recursive-minibuffers t)
50 (calc-language (if (memq calc-language '(nil big))
51 'flat calc-language))
52 (entry (calc-do-alg-entry "" "Quick calc: " t))
53 (alg-exp (mapcar (function
54 (lambda (x)
55 (if (and (not calc-extensions-loaded)
56 calc-previous-alg-entry
57 (string-match
58 "\\`[-0-9._+*/^() ]+\\'"
59 calc-previous-alg-entry))
60 (calc-normalize x)
61 (calc-extensions)
62 (math-evaluate-expr x))))
63 entry)))
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))
68 (calc-extensions)
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)))
75 alg-exp
76 " ")
77 shortbuf buf)
78 (if (and (= (length alg-exp) 1)
79 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
80 (< (length buf) 20)
81 (= calc-number-radix 10))
82 (setq buf (concat buf " ("
83 (let ((calc-number-radix 16))
84 (math-format-value (car alg-exp) 1000))
85 ", "
86 (let ((calc-number-radix 8))
87 (math-format-value (car alg-exp) 1000))
88 (if (and (integerp (car alg-exp))
89 (> (car alg-exp) 0)
90 (< (car alg-exp) 127))
91 (format ", \"%c\"" (car alg-exp))
92 "")
93 ")")))
94 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
95 calc-extensions-loaded)
96 (let ((long (concat (math-format-value (car entry) 1000)
97 " => " buf)))
98 (if (<= (length long) (- (frame-width) 8))
99 (setq buf long))))
100 (calc-handle-whys)
101 (message "Result: %s" buf)))
102 (if (eq last-command-char 10)
103 (insert shortbuf)
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)
110 (calc-check-defines)
111 (catch 'calc-error
112 (save-excursion
113 (calc-create-buffer)
114 (cond
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)
119 (calc-word-size 32)
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)
146 (strp (cdr str)))
147 (while strp
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)
152 (eval str))
153 ((eq separator 'macro)
154 (calc-extensions)
155 (let* ((calc-buffer (current-buffer))
156 (calc-window (get-buffer-window calc-buffer))
157 (save-window (selected-window)))
158 (if calc-window
159 (unwind-protect
160 (progn
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)))))
169 nil)
170 ((eq separator 'pop)
171 (or (not (integerp str))
172 (= str 0)
173 (calc-pop (min str (calc-stack-size))))
174 (calc-stack-size))
175 ((eq separator 'top)
176 (and (integerp str)
177 (> str 0)
178 (<= str (calc-stack-size))
179 (math-format-value (calc-top-n str (car args)) 1000)))
180 ((eq separator 'rawtop)
181 (and (integerp str)
182 (> str 0)
183 (<= str (calc-stack-size))
184 (calc-top-n str (car args))))
186 (let* ((calc-command-flags nil)
187 (calc-next-why nil)
188 (calc-language (if (memq calc-language '(nil big))
189 'flat calc-language))
190 (calc-dollar-values (mapcar
191 (function
192 (lambda (x)
193 (if (stringp x)
194 (progn
195 (setq x (math-read-exprs x))
196 (if (eq (car-safe x)
197 'error)
198 (throw 'calc-error
199 (calc-eval-error
200 (cdr x)))
201 (car x)))
202 x)))
203 args))
204 (calc-dollar-used 0)
205 (res (if (stringp str)
206 (math-read-exprs str)
207 (list str)))
208 buf)
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)
213 (message ""))
214 (cond ((eq separator 'pred)
215 (calc-extensions)
216 (if (= (length res) 1)
217 (math-is-true (car res))
218 (calc-eval-error '(0 "Single value expected"))))
219 ((eq separator 'raw)
220 (if (= (length res) 1)
221 (car res)
222 (calc-eval-error '(0 "Single value expected"))))
223 ((eq separator 'list)
224 res)
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)
230 (car res))
231 (calc-eval-error
232 (list 0
233 (if calc-next-why
234 (calc-explain-why (car calc-next-why))
235 "Number expected"))))
236 (calc-eval-error '(0 "Single value expected"))))
237 ((eq separator 'push)
238 (calc-push-list res)
239 nil)
240 (t (while res
241 (setq buf (concat buf
242 (and buf (or separator ", "))
243 (math-format-value (car res) 1000))
244 res (cdr res)))
245 buf)))))))))
247 (defun calc-eval-error (msg)
248 (if (and (boundp 'calc-eval-error)
249 calc-eval-error)
250 (if (eq calc-eval-error 'string)
251 (nth 1 msg)
252 (error "%s" (nth 1 msg)))
253 msg))
256 ;;;; Reading an expression in algebraic form.
258 (defun calc-auto-algebraic-entry (&optional prefix)
259 (interactive "P")
260 (calc-algebraic-entry prefix t))
262 (defun calc-algebraic-entry (&optional prefix auto)
263 (interactive "P")
264 (calc-wrapper
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)))
273 (calc-dollar-used 0)
274 (calc-plain-entry t)
275 (alg-exp (calc-do-alg-entry initial prompt t)))
276 (if (stringp alg-exp)
277 (progn
278 (calc-extensions)
279 (calc-alg-edit alg-exp))
280 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
281 'none
282 calc-simplify-mode))
283 (nvals (mapcar 'calc-normalize alg-exp)))
284 (while alg-exp
285 (calc-record (if calc-extensions-loaded (car alg-exp) (car nvals))
286 "alg'")
287 (calc-pop-push-record-list calc-dollar-used
288 (and (not (equal (car alg-exp)
289 (car nvals)))
290 calc-extensions-loaded
292 (list (car nvals)))
293 (setq alg-exp (cdr alg-exp)
294 nvals (cdr nvals)
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)
307 (alg-exp 'error))
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)
314 (let ((i 33))
315 (setq calc-alg-ent-esc-map (copy-keymap esc-map))
316 (while (< i 127)
317 (aset (nth 1 calc-alg-ent-esc-map) i 'calcAlg-escape)
318 (setq i (1+ i)))))
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: ")
329 (or initial "")
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)
333 (setq alg-exp nil)))
334 (setq calc-aborted-prefix "alg'")
335 (or no-normalize
336 (and alg-exp (setq alg-exp (mapcar 'calc-normalize alg-exp))))
337 alg-exp)))
339 (defun calcAlg-plus-minus ()
340 (interactive)
341 (if (calc-minibuffer-contains ".* \\'")
342 (insert "+/- ")
343 (insert " +/- ")))
345 (defun calcAlg-mod ()
346 (interactive)
347 (if (not (calc-minibuffer-contains ".* \\'"))
348 (insert " "))
349 (if (calc-minibuffer-contains ".* mod +\\'")
350 (if calc-previous-modulo
351 (insert (math-format-flat-expr calc-previous-modulo 0))
352 (beep))
353 (insert "mod ")))
355 (defun calcAlg-previous ()
356 (interactive)
357 (if (calc-minibuffer-contains "\\'")
358 (if calc-previous-alg-entry
359 (insert calc-previous-alg-entry)
360 (beep))
361 (insert "'")))
363 (defun calcAlg-equals ()
364 (interactive)
365 (unwind-protect
366 (calcAlg-enter)
367 (if (consp alg-exp)
368 (progn (setq prefix-arg (length alg-exp))
369 (calc-unread-command ?=)))))
371 (defun calcAlg-escape ()
372 (interactive)
373 (calc-unread-command)
374 (save-excursion
375 (calc-select-buffer)
376 (use-local-map calc-mode-map))
377 (calcAlg-enter))
379 (defvar calc-plain-entry nil)
380 (defun calcAlg-edit ()
381 (interactive)
382 (if (or (not calc-plain-entry)
383 (calc-minibuffer-contains
384 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
385 (insert "`")
386 (setq alg-exp (minibuffer-contents))
387 (and (> (length alg-exp) 0) (setq calc-previous-alg-entry alg-exp))
388 (exit-minibuffer)))
390 (defun calcAlg-enter ()
391 (interactive)
392 (let* ((str (minibuffer-contents))
393 (exp (and (> (length str) 0)
394 (save-excursion
395 (set-buffer calc-buffer)
396 (math-read-exprs str)))))
397 (if (eq (car-safe exp) 'error)
398 (progn
399 (goto-char (minibuffer-prompt-end))
400 (forward-char (nth 1 exp))
401 (beep)
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 "\\` *\\[ *\\'")
406 '((incomplete vec))
407 exp))
408 (and (> (length str) 0) (setq calc-previous-alg-entry str))
409 (exit-minibuffer))))
411 (defun calcAlg-blink-matching-open ()
412 (let ((oldpos (point))
413 (blinkpos nil))
414 (save-excursion
415 (condition-case ()
416 (setq blinkpos (scan-sexps oldpos -1))
417 (error nil)))
418 (if (and blinkpos
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) ?\()))
424 (save-excursion
425 (goto-char blinkpos)
426 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
427 (let ((saved (aref (syntax-table) (char-after blinkpos))))
428 (unwind-protect
429 (progn
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 ()
439 (calc-alg-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 ()
448 (interactive)
449 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
450 (calcDigit-key)
451 (setq calc-digit-value (minibuffer-contents))
452 (exit-minibuffer)))
454 (defun calcDigit-edit ()
455 (interactive)
456 (calc-unread-command)
457 (setq calc-digit-value (minibuffer-contents))
458 (exit-minibuffer))
461 ;;; Algebraic expression parsing. [Public]
463 (defun math-read-exprs (exp-str)
464 (let ((exp-pos 0)
465 (exp-old-pos 0)
466 (exp-keep-spaces nil)
467 exp-token exp-data)
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)
474 (math-read-token)
475 (let ((val (catch 'syntax (math-read-expr-list))))
476 (if (stringp val)
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)))
485 (last val))
486 (while (equal exp-data ",")
487 (math-read-token)
488 (let ((rest (list (math-read-expr-level 0))))
489 (setcdr last rest)
490 (setq last rest)))
491 val))
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))
505 (toks nil))
506 (setq calc-user-parse-table p)
507 (setq calc-user-token-chars nil)
508 (while p
509 (math-find-user-tokens (car (car p)))
510 (setq p (cdr p)))
511 (setq calc-user-tokens (mapconcat 'identity
512 (sort (mapcar 'car toks)
513 (function (lambda (x y)
514 (> (length x)
515 (length y)))))
516 "\\|")
517 calc-last-main-parse-table mtab
518 calc-last-lang-parse-table ltab)))))
520 (defun math-find-user-tokens (p) ; uses "toks"
521 (while p
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 "\\>")))
531 (or (assoc s toks)
532 (progn
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)))))))
538 ((consp (car p))
539 (math-find-user-tokens (nth 1 (car p)))
540 (or (eq (car (car p)) '\?)
541 (math-find-user-tokens (nth 2 (car p))))))
542 (setq p (cdr p))))
544 (defun math-read-token ()
545 (if (>= exp-pos (length exp-str))
546 (setq exp-old-pos exp-pos
547 exp-token 'end
548 exp-data "\000")
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))
553 (if exp-keep-spaces
554 (setq exp-token 'space
555 exp-data " ")
556 (math-read-token)))
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)
560 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))
567 "[a-zA-Z0-9_#]*"
568 "[a-zA-Z0-9'#]*")
569 exp-str exp-pos)
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)))
576 (cond ((null code))
577 ((null (cdr code))
578 (math-read-token))
579 ((consp (nth 1 code))
580 (math-read-token)
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)))
588 (math-read-token)
589 (math-read-token))))))
590 ((or (and (>= ch ?0) (<= ch ?9))
591 (and (eq ch '?\.)
592 (eq (string-match "\\.[0-9]" exp-str exp-pos) exp-pos))
593 (and (eq ch '?_)
594 (eq (string-match "_\\.?[0-9]" exp-str exp-pos) exp-pos)
595 (or (eq exp-pos 0)
596 (and (memq calc-language '(nil flat big unform
597 tex eqn))
598 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
599 exp-str (1- exp-pos))
600 (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)))
607 ((eq ch ?\$)
608 (if (and (eq calc-language 'pascal)
609 (eq (string-match
610 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
611 exp-str exp-pos)
612 exp-pos))
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)
617 exp-pos)
618 (setq exp-data (- (string-to-int (math-match-substring
619 exp-str 1))))
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))))
624 ((eq ch ?\#)
625 (if (eq (string-match "#\\([1-9][0-9]*\\)" exp-str exp-pos)
626 exp-pos)
627 (setq exp-data (string-to-int
628 (math-match-substring exp-str 1))
629 exp-pos (match-end 0))
630 (setq exp-data 1
631 exp-pos (1+ exp-pos)))
632 (setq exp-token 'hash))
633 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
634 exp-str exp-pos)
635 exp-pos)
636 (setq exp-token 'punc
637 exp-data (math-match-substring exp-str 0)
638 exp-pos (match-end 0)))
639 ((and (eq ch ?\")
640 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)" exp-str exp-pos))
641 (if (eq calc-language 'eqn)
642 (progn
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) ?\}))
647 (math-read-token))
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)))
660 (cond ((null code))
661 ((null (cdr code))
662 (math-read-token))
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)
669 exp-token 'punc
670 exp-data "[")
671 (let ((right (string-match "}" exp-str exp-pos)))
672 (and right
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)
683 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\\|~\\|\\^"
689 exp-str exp-pos)
690 exp-pos))
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) '(?~ ?^))
697 (math-read-token)))
698 ((eq (string-match "%%.*$" exp-str exp-pos) exp-pos)
699 (setq exp-pos (match-end 0))
700 (math-read-token))
702 (if (and (eq ch ?\{) (memq calc-language '(tex eqn)))
703 (setq ch ?\())
704 (if (and (eq ch ?\}) (memq calc-language '(tex eqn)))
705 (setq ch ?\)))
706 (if (and (eq ch ?\&) (eq calc-language 'tex))
707 (setq ch ?\,))
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))
717 (setq x op
718 op '("2x" ident 999999 -1)))
719 (and (setq op (assoc exp-data math-expr-opers))
720 (/= (nth 2 op) -1)
721 (or (and (setq op2 (assoc
722 exp-data
723 (cdr (memq op math-expr-opers))))
724 (eq (= (nth 3 op) -1)
725 (/= (nth 3 op2) -1))
726 (eq (= (nth 3 op2) -1)
727 (not (math-factor-after)))
728 (setq op op2))
730 (and (or (eq (nth 2 op) -1)
731 (memq exp-token '(symbol number dollar hash))
732 (equal exp-data "(")
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)))
738 (/= (nth 2 op) -1))
739 (or (not calc-user-parse-table)
740 (not (eq exp-token 'symbol))
741 (let ((p calc-user-parse-table))
742 (while (and p
743 (or (not (integerp
744 (car (car (car p)))))
745 (not (equal
746 (nth 1 (car (car p)))
747 exp-data))))
748 (setq p (cdr p)))
749 (not 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"))
754 (math-read-token))
755 (and (memq (nth 1 op) '(sdev mod))
756 (calc-extensions))
757 (setq x (cond ((consp (nth 1 op))
758 (funcall (car (nth 1 op)) x op))
759 ((eq (nth 3 op) -1)
760 (if (eq (nth 1 op) 'ident)
762 (if (eq (nth 1 op) 'closing)
763 (if (eq (nth 2 op) exp-prec)
764 (progn
765 (setq exp-prec 1000)
767 (throw 'syntax "Mismatched delimiters"))
768 (list (nth 1 op) x))))
769 ((and (not first)
770 (memq (nth 1 op) math-alg-inequalities)
771 (memq (car-safe x) math-alg-inequalities))
772 (calc-extensions)
773 (math-composite-inequalities x op))
774 (t (list (nth 1 op)
776 (math-read-expr-level (nth 3 op) exp-term))))
777 first nil))
780 (defun calc-check-user-syntax (&optional x prec)
781 (let ((p calc-user-parse-table)
782 (matches nil)
783 match rule)
784 (while (and p
785 (or (not (progn
786 (setq rule (car (car p)))
787 (if x
788 (and (integerp (car rule))
789 (>= (car rule) prec)
790 (equal exp-data
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))
797 (or (not (listp
798 (setq matches (calc-match-user-syntax rule))))
799 (let ((args (progn
800 (calc-extensions)
801 calc-arg-values))
802 (conds nil)
803 temp)
804 (if x
805 (setq matches (cons x matches)))
806 (setq match (cdr (car p)))
807 (while (and (eq (car-safe match)
808 'calcFunc-condition)
809 (= (length match) 3))
810 (setq conds (append (math-flatten-lands
811 (nth 2 match))
812 conds)
813 match (nth 1 match)))
814 (while (and conds match)
815 (calc-extensions)
816 (cond ((eq (car-safe (car conds))
817 'calcFunc-let)
818 (setq temp (car conds))
819 (or (= (length temp) 3)
820 (and (= (length temp) 2)
821 (eq (car-safe (nth 1 temp))
822 'calcFunc-assign)
823 (= (length (nth 1 temp)) 3)
824 (setq temp (nth 1 temp)))
825 (setq match nil))
826 (setq matches (cons
827 (math-normalize
828 (math-multi-subst
829 (nth 2 temp)
830 args matches))
831 matches)
832 args (cons (nth 1 temp)
833 args)))
834 ((and (eq (car-safe (car conds))
835 'calcFunc-matches)
836 (= (length (car conds)) 3))
837 (setq temp (calcFunc-vmatches
838 (math-multi-subst
839 (nth 1 (car conds))
840 args matches)
841 (nth 2 (car conds))))
842 (if (eq temp 0)
843 (setq match nil)
844 (while (setq temp (cdr temp))
845 (setq matches (cons (nth 2 (car temp))
846 matches)
847 args (cons (nth 1 (car temp))
848 args)))))
850 (or (math-is-true (math-simplify
851 (math-multi-subst
852 (car conds)
853 args matches)))
854 (setq match nil))))
855 (setq conds (cdr conds)))
856 (if match
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)))))))
863 (setq p (cdr p)))
864 (and p match)))
866 (defun calc-match-user-syntax (p &optional term)
867 (let ((matches nil)
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))
872 (while (and p
873 (cond ((stringp (car p))
874 (and (equal exp-data (car p))
875 (progn
876 (math-read-token)
877 t)))
878 ((integerp (car p))
879 (and (setq m (catch 'syntax
880 (math-read-expr-level
881 (car p)
882 (if (cdr p)
883 (if (consp (nth 1 p))
884 (car (nth 1 (nth 1 p)))
885 (nth 1 p))
886 term))))
887 (not (stringp m))
888 (setq matches (nconc matches (list m)))))
889 ((eq (car (car p)) '\?)
890 (setq m (calc-match-user-syntax (nth 1 (car p))))
891 (or (nth 2 (car p))
892 (setq matches
893 (nconc matches
894 (list
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)))))
902 (if (listp m)
903 (let ((vec (cons 'vec m))
904 opos mm)
905 (while (and (listp
906 (setq opos exp-pos
907 mm (calc-match-user-syntax
908 (or (nth 2 (car p))
909 (nth 1 (car p)))
910 (car (nth 2 (car p))))))
911 (> exp-pos opos))
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)))))))))
916 (setq p (cdr p)))
917 (if p
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
922 matches "Failed"))
923 matches))
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)
931 (math-remove-dashes
932 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
935 (defun math-restore-dashes (x)
936 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
937 (math-restore-dashes
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 ':'"))
945 (math-read-token)
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)
951 (math-read-token)
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 ()
959 (let (op)
960 (cond ((eq exp-token 'number)
961 (let ((num (math-read-number exp-data)))
962 (if (not num)
963 (progn
964 (setq exp-old-pos exp-pos)
965 (throw 'syntax "Bad format")))
966 (math-read-token)
967 (if (and math-read-expr-quotes
968 (consp num))
969 (list 'quote num)
970 num)))
971 ((and calc-user-parse-table
972 (setq op (calc-check-user-syntax)))
974 ((or (equal exp-data "-")
975 (equal exp-data "+")
976 (equal exp-data "!")
977 (equal exp-data "|")
978 (equal exp-data "/"))
979 (setq exp-data (concat "u" exp-data))
980 (math-read-factor))
981 ((and (setq op (assoc exp-data math-expr-opers))
982 (eq (nth 2 op) -1))
983 (if (consp (nth 1 op))
984 (funcall (car (nth 1 op)) op)
985 (math-read-token)
986 (let ((val (math-read-expr-level (nth 3 op))))
987 (cond ((eq (nth 1 op) 'ident)
988 val)
989 ((and (Math-numberp val)
990 (equal (car op) "u-"))
991 (math-neg val))
992 (t (list (nth 1 op) val))))))
993 ((eq exp-token 'symbol)
994 (let ((sym (intern exp-data)))
995 (math-read-token)
996 (if (equal exp-data calc-function-open)
997 (let ((f (assq sym math-expr-function-mapping)))
998 (math-read-token)
999 (if (consp (cdr f))
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 `)'"))
1008 (math-read-token)
1009 (if (and (eq calc-language 'fortran) args
1010 (calc-extensions)
1011 (let ((calc-matrix-mode 'scalar))
1012 (math-known-matrixp
1013 (list 'var sym
1014 (intern
1015 (concat "var-"
1016 (symbol-name sym)))))))
1017 (math-parse-fortran-subscr sym args)
1018 (if f
1019 (setq sym (cdr f))
1020 (and (= (aref (symbol-name sym) 0) ?\\)
1021 (< (prefix-numeric-value calc-language-option)
1023 (setq sym (intern (substring (symbol-name sym)
1024 1))))
1025 (or (string-match "-" (symbol-name sym))
1026 (setq sym (intern
1027 (concat "calcFunc-"
1028 (symbol-name sym))))))
1029 (cons sym args)))))
1030 (if math-read-expr-quotes
1032 (let ((val (list 'var
1033 (intern (math-remove-dashes
1034 (symbol-name sym)))
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)
1042 (list 'var
1043 (intern
1044 (substring (symbol-name (cdr v))
1046 (cdr v))))))
1047 (while (and (memq calc-language '(c pascal maple))
1048 (equal exp-data "["))
1049 (math-read-token)
1050 (setq val (append (list 'calcFunc-subscr val)
1051 (math-read-expr-list)))
1052 (if (equal exp-data "]")
1053 (math-read-token)
1054 (throw 'syntax "Expected ']'")))
1055 val)))))
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))
1060 (math-read-token)
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
1064 "Too many $'s"
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"))
1069 (calc-extensions)
1070 (if (<= exp-data (length calc-arg-values))
1071 (let ((num exp-data))
1072 (math-read-token)
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))
1078 (math-read-token)
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))
1084 (cond
1085 ((equal exp-data ",")
1086 (progn
1087 (math-read-token)
1088 (let ((exp2 (math-read-expr-level 0)))
1089 (setq exp
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 ";")
1094 (progn
1095 (math-read-token)
1096 (let ((exp2 (math-read-expr-level 0)))
1097 (setq exp (if (and exp2 (Math-realp exp)
1098 (Math-anglep exp2))
1099 (math-normalize (list 'polar exp exp2))
1100 (calc-extensions)
1101 (list '* exp
1102 (list 'calcFunc-exp
1103 (list '*
1104 (math-to-radians-2 exp2)
1105 '(var i var-i)))))))))
1106 ((or (equal exp-data "\\dots")
1107 (equal exp-data "\\ldots"))
1108 (progn
1109 (math-read-token)
1110 (let ((exp2 (if (or (equal exp-data ")")
1111 (equal exp-data "]")
1112 (eq exp-token 'end))
1113 '(var inf var-inf)
1114 (math-read-expr-level 0))))
1115 (setq exp
1116 (list 'intv
1117 (if (equal exp-data ")") 0 1)
1119 exp2)))))))
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 `)'"))
1124 (math-read-token)
1125 exp))
1126 ((eq exp-token 'string)
1127 (calc-extensions)
1128 (math-read-string))
1129 ((equal exp-data "[")
1130 (calc-extensions)
1131 (math-read-brackets t "]"))
1132 ((equal exp-data "{")
1133 (calc-extensions)
1134 (math-read-brackets nil "}"))
1135 ((equal exp-data "<")
1136 (calc-extensions)
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