(toplevel): Require calc-macs during compilation.
[bpt/emacs.git] / lisp / calc / calc-aent.el
1 ;; Calculator for GNU Emacs, part I [calc-aent.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
21
22
23
24 ;; This file is autoloaded from calc.el.
25 (require 'calc)
26
27 (require 'calc-macs)
28 (eval-when-compile '(require calc-macs))
29
30 (defun calc-Need-calc-aent () nil)
31
32
33 (defun calc-do-quick-calc ()
34 (calc-check-defines)
35 (if (eq major-mode 'calc-mode)
36 (calc-algebraic-entry t)
37 (let (buf shortbuf)
38 (save-excursion
39 (calc-create-buffer)
40 (let* ((calc-command-flags nil)
41 (calc-dollar-values calc-quick-prev-results)
42 (calc-dollar-used 0)
43 (enable-recursive-minibuffers t)
44 (calc-language (if (memq calc-language '(nil big))
45 'flat calc-language))
46 (entry (calc-do-alg-entry "" "Quick calc: " t))
47 (alg-exp (mapcar (function
48 (lambda (x)
49 (if (and (not calc-extensions-loaded)
50 calc-previous-alg-entry
51 (string-match
52 "\\`[-0-9._+*/^() ]+\\'"
53 calc-previous-alg-entry))
54 (calc-normalize x)
55 (calc-extensions)
56 (math-evaluate-expr x))))
57 entry)))
58 (if (and (= (length alg-exp) 1)
59 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
60 (= (length (car alg-exp)) 3)
61 (eq (car-safe (nth 1 (car alg-exp))) 'var))
62 (progn
63 (calc-extensions)
64 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
65 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
66 (setq alg-exp (list (nth 2 (car alg-exp))))))
67 (setq calc-quick-prev-results alg-exp
68 buf (mapconcat (function (lambda (x)
69 (math-format-value x 1000)))
70 alg-exp
71 " ")
72 shortbuf buf)
73 (if (and (= (length alg-exp) 1)
74 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
75 (< (length buf) 20)
76 (= calc-number-radix 10))
77 (setq buf (concat buf " ("
78 (let ((calc-number-radix 16))
79 (math-format-value (car alg-exp) 1000))
80 ", "
81 (let ((calc-number-radix 8))
82 (math-format-value (car alg-exp) 1000))
83 (if (and (integerp (car alg-exp))
84 (> (car alg-exp) 0)
85 (< (car alg-exp) 127))
86 (format ", \"%c\"" (car alg-exp))
87 "")
88 ")")))
89 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
90 calc-extensions-loaded)
91 (let ((long (concat (math-format-value (car entry) 1000)
92 " => " buf)))
93 (if (<= (length long) (- (frame-width) 8))
94 (setq buf long))))
95 (calc-handle-whys)
96 (message "Result: %s" buf)))
97 (if (eq last-command-char 10)
98 (insert shortbuf)
99 (setq kill-ring (cons shortbuf kill-ring))
100 (if (> (length kill-ring) kill-ring-max)
101 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
102 (setq kill-ring-yank-pointer kill-ring))))
103 )
104
105 (defun calc-do-calc-eval (str separator args)
106 (calc-check-defines)
107 (catch 'calc-error
108 (save-excursion
109 (calc-create-buffer)
110 (cond
111 ((and (consp str) (not (symbolp (car str))))
112 (let ((calc-language nil)
113 (math-expr-opers math-standard-opers)
114 (calc-internal-prec 12)
115 (calc-word-size 32)
116 (calc-symbolic-mode nil)
117 (calc-matrix-mode nil)
118 (calc-angle-mode 'deg)
119 (calc-number-radix 10)
120 (calc-leading-zeros nil)
121 (calc-group-digits nil)
122 (calc-point-char ".")
123 (calc-frac-format '(":" nil))
124 (calc-prefer-frac nil)
125 (calc-hms-format "%s@ %s' %s\"")
126 (calc-date-format '((H ":" mm C SS pp " ")
127 Www " " Mmm " " D ", " YYYY))
128 (calc-float-format '(float 0))
129 (calc-full-float-format '(float 0))
130 (calc-complex-format nil)
131 (calc-matrix-just nil)
132 (calc-full-vectors t)
133 (calc-break-vectors nil)
134 (calc-vector-commas ",")
135 (calc-vector-brackets "[]")
136 (calc-matrix-brackets '(R O))
137 (calc-complex-mode 'cplx)
138 (calc-infinite-mode nil)
139 (calc-display-strings nil)
140 (calc-simplify-mode nil)
141 (calc-display-working-message 'lots)
142 (strp (cdr str)))
143 (while strp
144 (set (car strp) (nth 1 strp))
145 (setq strp (cdr (cdr strp))))
146 (calc-do-calc-eval (car str) separator args)))
147 ((eq separator 'eval)
148 (eval str))
149 ((eq separator 'macro)
150 (calc-extensions)
151 (let* ((calc-buffer (current-buffer))
152 (calc-window (get-buffer-window calc-buffer))
153 (save-window (selected-window)))
154 (if calc-window
155 (unwind-protect
156 (progn
157 (select-window calc-window)
158 (calc-execute-kbd-macro str nil (car args)))
159 (and (window-point save-window)
160 (select-window save-window)))
161 (save-window-excursion
162 (select-window (get-largest-window))
163 (switch-to-buffer calc-buffer)
164 (calc-execute-kbd-macro str nil (car args)))))
165 nil)
166 ((eq separator 'pop)
167 (or (not (integerp str))
168 (= str 0)
169 (calc-pop (min str (calc-stack-size))))
170 (calc-stack-size))
171 ((eq separator 'top)
172 (and (integerp str)
173 (> str 0)
174 (<= str (calc-stack-size))
175 (math-format-value (calc-top-n str (car args)) 1000)))
176 ((eq separator 'rawtop)
177 (and (integerp str)
178 (> str 0)
179 (<= str (calc-stack-size))
180 (calc-top-n str (car args))))
181 (t
182 (let* ((calc-command-flags nil)
183 (calc-next-why nil)
184 (calc-language (if (memq calc-language '(nil big))
185 'flat calc-language))
186 (calc-dollar-values (mapcar
187 (function
188 (lambda (x)
189 (if (stringp x)
190 (progn
191 (setq x (math-read-exprs x))
192 (if (eq (car-safe x)
193 'error)
194 (throw 'calc-error
195 (calc-eval-error
196 (cdr x)))
197 (car x)))
198 x)))
199 args))
200 (calc-dollar-used 0)
201 (res (if (stringp str)
202 (math-read-exprs str)
203 (list str)))
204 buf)
205 (if (eq (car res) 'error)
206 (calc-eval-error (cdr res))
207 (setq res (mapcar 'calc-normalize res))
208 (and (memq 'clear-message calc-command-flags)
209 (message ""))
210 (cond ((eq separator 'pred)
211 (calc-extensions)
212 (if (= (length res) 1)
213 (math-is-true (car res))
214 (calc-eval-error '(0 "Single value expected"))))
215 ((eq separator 'raw)
216 (if (= (length res) 1)
217 (car res)
218 (calc-eval-error '(0 "Single value expected"))))
219 ((eq separator 'list)
220 res)
221 ((memq separator '(num rawnum))
222 (if (= (length res) 1)
223 (if (math-constp (car res))
224 (if (eq separator 'num)
225 (math-format-value (car res) 1000)
226 (car res))
227 (calc-eval-error
228 (list 0
229 (if calc-next-why
230 (calc-explain-why (car calc-next-why))
231 "Number expected"))))
232 (calc-eval-error '(0 "Single value expected"))))
233 ((eq separator 'push)
234 (calc-push-list res)
235 nil)
236 (t (while res
237 (setq buf (concat buf
238 (and buf (or separator ", "))
239 (math-format-value (car res) 1000))
240 res (cdr res)))
241 buf))))))))
242 )
243
244 (defun calc-eval-error (msg)
245 (if (and (boundp 'calc-eval-error)
246 calc-eval-error)
247 (if (eq calc-eval-error 'string)
248 (nth 1 msg)
249 (error "%s" (nth 1 msg)))
250 msg)
251 )
252
253
254 ;;;; Reading an expression in algebraic form.
255
256 (defun calc-auto-algebraic-entry (&optional prefix)
257 (interactive "P")
258 (calc-algebraic-entry prefix t)
259 )
260
261 (defun calc-algebraic-entry (&optional prefix auto)
262 (interactive "P")
263 (calc-wrapper
264 (let ((calc-language (if prefix nil calc-language))
265 (math-expr-opers (if prefix math-standard-opers math-expr-opers)))
266 (calc-alg-entry (and auto (char-to-string last-command-char)))))
267 )
268
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
291 "")
292 (list (car nvals)))
293 (setq alg-exp (cdr alg-exp)
294 nvals (cdr nvals)
295 calc-dollar-used 0)))
296 (calc-handle-whys)))
297 )
298
299 (defun calc-do-alg-entry (&optional initial prompt no-normalize)
300 (let* ((calc-buffer (current-buffer))
301 (blink-paren-hook 'calcAlg-blink-matching-open)
302 (alg-exp 'error))
303 (if (boundp 'calc-alg-ent-map)
304 ()
305 (setq calc-alg-ent-map (copy-keymap minibuffer-local-map))
306 (define-key calc-alg-ent-map "'" 'calcAlg-previous)
307 (define-key calc-alg-ent-map "`" 'calcAlg-edit)
308 (define-key calc-alg-ent-map "\C-m" 'calcAlg-enter)
309 (define-key calc-alg-ent-map "\C-j" 'calcAlg-enter)
310 (or calc-emacs-type-19
311 (let ((i 33))
312 (setq calc-alg-ent-esc-map (copy-sequence esc-map))
313 (while (< i 127)
314 (aset calc-alg-ent-esc-map i 'calcAlg-escape)
315 (setq i (1+ i))))))
316 (or calc-emacs-type-19
317 (define-key calc-alg-ent-map "\e" nil))
318 (if (eq calc-algebraic-mode 'total)
319 (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
320 (define-key calc-alg-ent-map "\ep" 'calcAlg-plus-minus)
321 (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
322 (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
323 (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
324 (define-key calc-alg-ent-map "\e%" 'self-insert-command))
325 (setq calc-aborted-prefix nil)
326 (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
327 (or initial "")
328 calc-alg-ent-map nil)))
329 (if (eq alg-exp 'error)
330 (if (eq (car-safe (setq alg-exp (math-read-exprs buf))) 'error)
331 (setq alg-exp nil)))
332 (setq calc-aborted-prefix "alg'")
333 (or no-normalize
334 (and alg-exp (setq alg-exp (mapcar 'calc-normalize alg-exp))))
335 alg-exp))
336 )
337
338 (defun calcAlg-plus-minus ()
339 (interactive)
340 (if (calc-minibuffer-contains ".* \\'")
341 (insert "+/- ")
342 (insert " +/- "))
343 )
344
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 "))
354 )
355
356 (defun calcAlg-previous ()
357 (interactive)
358 (if (calc-minibuffer-contains "\\`\\'")
359 (if calc-previous-alg-entry
360 (insert calc-previous-alg-entry)
361 (beep))
362 (insert "'"))
363 )
364
365 (defun calcAlg-equals ()
366 (interactive)
367 (unwind-protect
368 (calcAlg-enter)
369 (if (consp alg-exp)
370 (progn (setq prefix-arg (length alg-exp))
371 (calc-unread-command ?=))))
372 )
373
374 (defun calcAlg-escape ()
375 (interactive)
376 (calc-unread-command)
377 (save-excursion
378 (calc-select-buffer)
379 (use-local-map calc-mode-map))
380 (calcAlg-enter)
381 )
382
383 (defun calcAlg-edit ()
384 (interactive)
385 (if (or (not calc-plain-entry)
386 (calc-minibuffer-contains
387 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
388 (insert "`")
389 (setq alg-exp (minibuffer-contents))
390 (and (> (length alg-exp) 0) (setq calc-previous-alg-entry alg-exp))
391 (exit-minibuffer))
392 )
393 (setq calc-plain-entry nil)
394
395 (defun calcAlg-enter ()
396 (interactive)
397 (let* ((str (minibuffer-contents))
398 (exp (and (> (length str) 0)
399 (save-excursion
400 (set-buffer calc-buffer)
401 (math-read-exprs str)))))
402 (if (eq (car-safe exp) 'error)
403 (progn
404 (goto-char (minibuffer-prompt-end))
405 (forward-char (nth 1 exp))
406 (beep)
407 (calc-temp-minibuffer-message
408 (concat " [" (or (nth 2 exp) "Error") "]"))
409 (calc-clear-unread-commands))
410 (setq alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
411 '((incomplete vec))
412 exp))
413 (and (> (length str) 0) (setq calc-previous-alg-entry str))
414 (exit-minibuffer)))
415 )
416
417 (defun calcAlg-blink-matching-open ()
418 (let ((oldpos (point))
419 (blinkpos nil))
420 (save-excursion
421 (condition-case ()
422 (setq blinkpos (scan-sexps oldpos -1))
423 (error nil)))
424 (if (and blinkpos
425 (> oldpos (1+ (point-min)))
426 (or (and (= (char-after (1- oldpos)) ?\))
427 (= (char-after blinkpos) ?\[))
428 (and (= (char-after (1- oldpos)) ?\])
429 (= (char-after blinkpos) ?\()))
430 (save-excursion
431 (goto-char blinkpos)
432 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
433 (let ((saved (aref (syntax-table) (char-after blinkpos))))
434 (unwind-protect
435 (progn
436 (aset (syntax-table) (char-after blinkpos)
437 (+ (logand saved 255)
438 (lsh (char-after (1- oldpos)) 8)))
439 (blink-matching-open))
440 (aset (syntax-table) (char-after blinkpos) saved)))
441 (blink-matching-open)))
442 )
443
444
445 (defun calc-alg-digit-entry ()
446 (calc-alg-entry
447 (cond ((eq last-command-char ?e)
448 (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e"))
449 ((eq last-command-char ?#) (format "%d#" calc-number-radix))
450 ((eq last-command-char ?_) "-")
451 ((eq last-command-char ?@) "0@ ")
452 (t (char-to-string last-command-char))))
453 )
454
455 (defun calcDigit-algebraic ()
456 (interactive)
457 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
458 (calcDigit-key)
459 (setq calc-digit-value (minibuffer-contents))
460 (exit-minibuffer))
461 )
462
463 (defun calcDigit-edit ()
464 (interactive)
465 (calc-unread-command)
466 (setq calc-digit-value (minibuffer-contents))
467 (exit-minibuffer)
468 )
469
470
471 ;;; Algebraic expression parsing. [Public]
472
473 (defun math-read-exprs (exp-str)
474 (let ((exp-pos 0)
475 (exp-old-pos 0)
476 (exp-keep-spaces nil)
477 exp-token exp-data)
478 (if calc-language-input-filter
479 (setq exp-str (funcall calc-language-input-filter exp-str)))
480 (while (setq exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" exp-str))
481 (setq exp-str (concat (substring exp-str 0 exp-token) "\\dots"
482 (substring exp-str (+ exp-token 2)))))
483 (math-build-parse-table)
484 (math-read-token)
485 (let ((val (catch 'syntax (math-read-expr-list))))
486 (if (stringp val)
487 (list 'error exp-old-pos val)
488 (if (equal exp-token 'end)
489 val
490 (list 'error exp-old-pos "Syntax error")))))
491 )
492
493 (defun math-read-expr-list ()
494 (let* ((exp-keep-spaces nil)
495 (val (list (math-read-expr-level 0)))
496 (last val))
497 (while (equal exp-data ",")
498 (math-read-token)
499 (let ((rest (list (math-read-expr-level 0))))
500 (setcdr last rest)
501 (setq last rest)))
502 val)
503 )
504
505 (setq calc-user-parse-table nil)
506 (setq calc-last-main-parse-table nil)
507 (setq calc-last-lang-parse-table nil)
508 (setq calc-user-tokens nil)
509 (setq calc-user-token-chars nil)
510
511 (defun math-build-parse-table ()
512 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
513 (ltab (cdr (assq calc-language calc-user-parse-tables))))
514 (or (and (eq mtab calc-last-main-parse-table)
515 (eq ltab calc-last-lang-parse-table))
516 (let ((p (append mtab ltab))
517 (toks nil))
518 (setq calc-user-parse-table p)
519 (setq calc-user-token-chars nil)
520 (while p
521 (math-find-user-tokens (car (car p)))
522 (setq p (cdr p)))
523 (setq calc-user-tokens (mapconcat 'identity
524 (sort (mapcar 'car toks)
525 (function (lambda (x y)
526 (> (length x)
527 (length y)))))
528 "\\|")
529 calc-last-main-parse-table mtab
530 calc-last-lang-parse-table ltab))))
531 )
532
533 (defun math-find-user-tokens (p) ; uses "toks"
534 (while p
535 (cond ((and (stringp (car p))
536 (or (> (length (car p)) 1) (equal (car p) "$")
537 (equal (car p) "\""))
538 (string-match "[^a-zA-Z0-9]" (car p)))
539 (let ((s (regexp-quote (car p))))
540 (if (string-match "\\`[a-zA-Z0-9]" s)
541 (setq s (concat "\\<" s)))
542 (if (string-match "[a-zA-Z0-9]\\'" s)
543 (setq s (concat s "\\>")))
544 (or (assoc s toks)
545 (progn
546 (setq toks (cons (list s) toks))
547 (or (memq (aref (car p) 0) calc-user-token-chars)
548 (setq calc-user-token-chars
549 (cons (aref (car p) 0)
550 calc-user-token-chars)))))))
551 ((consp (car p))
552 (math-find-user-tokens (nth 1 (car p)))
553 (or (eq (car (car p)) '\?)
554 (math-find-user-tokens (nth 2 (car p))))))
555 (setq p (cdr p)))
556 )
557
558 (defun math-read-token ()
559 (if (>= exp-pos (length exp-str))
560 (setq exp-old-pos exp-pos
561 exp-token 'end
562 exp-data "\000")
563 (let ((ch (aref exp-str exp-pos)))
564 (setq exp-old-pos exp-pos)
565 (cond ((memq ch '(32 10 9))
566 (setq exp-pos (1+ exp-pos))
567 (if exp-keep-spaces
568 (setq exp-token 'space
569 exp-data " ")
570 (math-read-token)))
571 ((and (memq ch calc-user-token-chars)
572 (let ((case-fold-search nil))
573 (eq (string-match calc-user-tokens exp-str exp-pos)
574 exp-pos)))
575 (setq exp-token 'punc
576 exp-data (math-match-substring exp-str 0)
577 exp-pos (match-end 0)))
578 ((or (and (>= ch ?a) (<= ch ?z))
579 (and (>= ch ?A) (<= ch ?Z)))
580 (string-match (if (memq calc-language '(c fortran pascal maple))
581 "[a-zA-Z0-9_#]*"
582 "[a-zA-Z0-9'#]*")
583 exp-str exp-pos)
584 (setq exp-token 'symbol
585 exp-pos (match-end 0)
586 exp-data (math-restore-dashes
587 (math-match-substring exp-str 0)))
588 (if (eq calc-language 'eqn)
589 (let ((code (assoc exp-data math-eqn-ignore-words)))
590 (cond ((null code))
591 ((null (cdr code))
592 (math-read-token))
593 ((consp (nth 1 code))
594 (math-read-token)
595 (if (assoc exp-data (cdr code))
596 (setq exp-data (format "%s %s"
597 (car code) exp-data))))
598 ((eq (nth 1 code) 'punc)
599 (setq exp-token 'punc
600 exp-data (nth 2 code)))
601 (t
602 (math-read-token)
603 (math-read-token))))))
604 ((or (and (>= ch ?0) (<= ch ?9))
605 (and (eq ch '?\.)
606 (eq (string-match "\\.[0-9]" exp-str exp-pos) exp-pos))
607 (and (eq ch '?_)
608 (eq (string-match "_\\.?[0-9]" exp-str exp-pos) exp-pos)
609 (or (eq exp-pos 0)
610 (and (memq calc-language '(nil flat big unform
611 tex eqn))
612 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
613 exp-str (1- exp-pos))
614 (1- exp-pos))))))
615 (or (and (eq calc-language 'c)
616 (string-match "0[xX][0-9a-fA-F]+" exp-str exp-pos))
617 (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))
618 (setq exp-token 'number
619 exp-data (math-match-substring exp-str 0)
620 exp-pos (match-end 0)))
621 ((eq ch ?\$)
622 (if (and (eq calc-language 'pascal)
623 (eq (string-match
624 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
625 exp-str exp-pos)
626 exp-pos))
627 (setq exp-token 'number
628 exp-data (math-match-substring exp-str 1)
629 exp-pos (match-end 1))
630 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" exp-str exp-pos)
631 exp-pos)
632 (setq exp-data (- (string-to-int (math-match-substring
633 exp-str 1))))
634 (string-match "\\$+" exp-str exp-pos)
635 (setq exp-data (- (match-end 0) (match-beginning 0))))
636 (setq exp-token 'dollar
637 exp-pos (match-end 0))))
638 ((eq ch ?\#)
639 (if (eq (string-match "#\\([1-9][0-9]*\\)" exp-str exp-pos)
640 exp-pos)
641 (setq exp-data (string-to-int
642 (math-match-substring exp-str 1))
643 exp-pos (match-end 0))
644 (setq exp-data 1
645 exp-pos (1+ exp-pos)))
646 (setq exp-token 'hash))
647 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
648 exp-str exp-pos)
649 exp-pos)
650 (setq exp-token 'punc
651 exp-data (math-match-substring exp-str 0)
652 exp-pos (match-end 0)))
653 ((and (eq ch ?\")
654 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)" exp-str exp-pos))
655 (if (eq calc-language 'eqn)
656 (progn
657 (setq exp-str (copy-sequence exp-str))
658 (aset exp-str (match-beginning 1) ?\{)
659 (if (< (match-end 1) (length exp-str))
660 (aset exp-str (match-end 1) ?\}))
661 (math-read-token))
662 (setq exp-token 'string
663 exp-data (math-match-substring exp-str 1)
664 exp-pos (match-end 0))))
665 ((and (= ch ?\\) (eq calc-language 'tex)
666 (< exp-pos (1- (length exp-str))))
667 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}" exp-str exp-pos)
668 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)" exp-str exp-pos))
669 (setq exp-token 'symbol
670 exp-pos (match-end 0)
671 exp-data (math-restore-dashes
672 (math-match-substring exp-str 1)))
673 (let ((code (assoc exp-data math-tex-ignore-words)))
674 (cond ((null code))
675 ((null (cdr code))
676 (math-read-token))
677 ((eq (nth 1 code) 'punc)
678 (setq exp-token 'punc
679 exp-data (nth 2 code)))
680 ((and (eq (nth 1 code) 'mat)
681 (string-match " *{" exp-str exp-pos))
682 (setq exp-pos (match-end 0)
683 exp-token 'punc
684 exp-data "[")
685 (let ((right (string-match "}" exp-str exp-pos)))
686 (and right
687 (setq exp-str (copy-sequence exp-str))
688 (aset exp-str right ?\])))))))
689 ((and (= ch ?\.) (eq calc-language 'fortran)
690 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
691 exp-str exp-pos) exp-pos))
692 (setq exp-token 'punc
693 exp-data (upcase (math-match-substring exp-str 0))
694 exp-pos (match-end 0)))
695 ((and (eq calc-language 'math)
696 (eq (string-match "\\[\\[\\|->\\|:>" exp-str exp-pos)
697 exp-pos))
698 (setq exp-token 'punc
699 exp-data (math-match-substring exp-str 0)
700 exp-pos (match-end 0)))
701 ((and (eq calc-language 'eqn)
702 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
703 exp-str exp-pos)
704 exp-pos))
705 (setq exp-token 'punc
706 exp-data (math-match-substring exp-str 0)
707 exp-pos (match-end 0))
708 (and (eq (string-match "\\\\dots\\." exp-str exp-pos) exp-pos)
709 (setq exp-pos (match-end 0)))
710 (if (memq (aref exp-data 0) '(?~ ?^))
711 (math-read-token)))
712 ((eq (string-match "%%.*$" exp-str exp-pos) exp-pos)
713 (setq exp-pos (match-end 0))
714 (math-read-token))
715 (t
716 (if (and (eq ch ?\{) (memq calc-language '(tex eqn)))
717 (setq ch ?\())
718 (if (and (eq ch ?\}) (memq calc-language '(tex eqn)))
719 (setq ch ?\)))
720 (if (and (eq ch ?\&) (eq calc-language 'tex))
721 (setq ch ?\,))
722 (setq exp-token 'punc
723 exp-data (char-to-string ch)
724 exp-pos (1+ exp-pos))))))
725 )
726
727
728 (defun math-read-expr-level (exp-prec &optional exp-term)
729 (let* ((x (math-read-factor)) (first t) op op2)
730 (while (and (or (and calc-user-parse-table
731 (setq op (calc-check-user-syntax x exp-prec))
732 (setq x op
733 op '("2x" ident 999999 -1)))
734 (and (setq op (assoc exp-data math-expr-opers))
735 (/= (nth 2 op) -1)
736 (or (and (setq op2 (assoc
737 exp-data
738 (cdr (memq op math-expr-opers))))
739 (eq (= (nth 3 op) -1)
740 (/= (nth 3 op2) -1))
741 (eq (= (nth 3 op2) -1)
742 (not (math-factor-after)))
743 (setq op op2))
744 t))
745 (and (or (eq (nth 2 op) -1)
746 (memq exp-token '(symbol number dollar hash))
747 (equal exp-data "(")
748 (and (equal exp-data "[")
749 (not (eq calc-language 'math))
750 (not (and exp-keep-spaces
751 (eq (car-safe x) 'vec)))))
752 (or (not (setq op (assoc exp-data math-expr-opers)))
753 (/= (nth 2 op) -1))
754 (or (not calc-user-parse-table)
755 (not (eq exp-token 'symbol))
756 (let ((p calc-user-parse-table))
757 (while (and p
758 (or (not (integerp
759 (car (car (car p)))))
760 (not (equal
761 (nth 1 (car (car p)))
762 exp-data))))
763 (setq p (cdr p)))
764 (not p)))
765 (setq op (assoc "2x" math-expr-opers))))
766 (not (and exp-term (equal exp-data exp-term)))
767 (>= (nth 2 op) exp-prec))
768 (if (not (equal (car op) "2x"))
769 (math-read-token))
770 (and (memq (nth 1 op) '(sdev mod))
771 (calc-extensions))
772 (setq x (cond ((consp (nth 1 op))
773 (funcall (car (nth 1 op)) x op))
774 ((eq (nth 3 op) -1)
775 (if (eq (nth 1 op) 'ident)
776 x
777 (if (eq (nth 1 op) 'closing)
778 (if (eq (nth 2 op) exp-prec)
779 (progn
780 (setq exp-prec 1000)
781 x)
782 (throw 'syntax "Mismatched delimiters"))
783 (list (nth 1 op) x))))
784 ((and (not first)
785 (memq (nth 1 op) math-alg-inequalities)
786 (memq (car-safe x) math-alg-inequalities))
787 (calc-extensions)
788 (math-composite-inequalities x op))
789 (t (list (nth 1 op)
790 x
791 (math-read-expr-level (nth 3 op) exp-term))))
792 first nil))
793 x)
794 )
795
796 (defun calc-check-user-syntax (&optional x prec)
797 (let ((p calc-user-parse-table)
798 (matches nil)
799 match rule)
800 (while (and p
801 (or (not (progn
802 (setq rule (car (car p)))
803 (if x
804 (and (integerp (car rule))
805 (>= (car rule) prec)
806 (equal exp-data
807 (car (setq rule (cdr rule)))))
808 (equal exp-data (car rule)))))
809 (let ((save-exp-pos exp-pos)
810 (save-exp-old-pos exp-old-pos)
811 (save-exp-token exp-token)
812 (save-exp-data exp-data))
813 (or (not (listp
814 (setq matches (calc-match-user-syntax rule))))
815 (let ((args (progn
816 (calc-extensions)
817 calc-arg-values))
818 (conds nil)
819 temp)
820 (if x
821 (setq matches (cons x matches)))
822 (setq match (cdr (car p)))
823 (while (and (eq (car-safe match)
824 'calcFunc-condition)
825 (= (length match) 3))
826 (setq conds (append (math-flatten-lands
827 (nth 2 match))
828 conds)
829 match (nth 1 match)))
830 (while (and conds match)
831 (calc-extensions)
832 (cond ((eq (car-safe (car conds))
833 'calcFunc-let)
834 (setq temp (car conds))
835 (or (= (length temp) 3)
836 (and (= (length temp) 2)
837 (eq (car-safe (nth 1 temp))
838 'calcFunc-assign)
839 (= (length (nth 1 temp)) 3)
840 (setq temp (nth 1 temp)))
841 (setq match nil))
842 (setq matches (cons
843 (math-normalize
844 (math-multi-subst
845 (nth 2 temp)
846 args matches))
847 matches)
848 args (cons (nth 1 temp)
849 args)))
850 ((and (eq (car-safe (car conds))
851 'calcFunc-matches)
852 (= (length (car conds)) 3))
853 (setq temp (calcFunc-vmatches
854 (math-multi-subst
855 (nth 1 (car conds))
856 args matches)
857 (nth 2 (car conds))))
858 (if (eq temp 0)
859 (setq match nil)
860 (while (setq temp (cdr temp))
861 (setq matches (cons (nth 2 (car temp))
862 matches)
863 args (cons (nth 1 (car temp))
864 args)))))
865 (t
866 (or (math-is-true (math-simplify
867 (math-multi-subst
868 (car conds)
869 args matches)))
870 (setq match nil))))
871 (setq conds (cdr conds)))
872 (if match
873 (not (setq match (math-multi-subst
874 match args matches)))
875 (setq exp-old-pos save-exp-old-pos
876 exp-token save-exp-token
877 exp-data save-exp-data
878 exp-pos save-exp-pos)))))))
879 (setq p (cdr p)))
880 (and p match))
881 )
882
883 (defun calc-match-user-syntax (p &optional term)
884 (let ((matches nil)
885 (save-exp-pos exp-pos)
886 (save-exp-old-pos exp-old-pos)
887 (save-exp-token exp-token)
888 (save-exp-data exp-data))
889 (while (and p
890 (cond ((stringp (car p))
891 (and (equal exp-data (car p))
892 (progn
893 (math-read-token)
894 t)))
895 ((integerp (car p))
896 (and (setq m (catch 'syntax
897 (math-read-expr-level
898 (car p)
899 (if (cdr p)
900 (if (consp (nth 1 p))
901 (car (nth 1 (nth 1 p)))
902 (nth 1 p))
903 term))))
904 (not (stringp m))
905 (setq matches (nconc matches (list m)))))
906 ((eq (car (car p)) '\?)
907 (setq m (calc-match-user-syntax (nth 1 (car p))))
908 (or (nth 2 (car p))
909 (setq matches
910 (nconc matches
911 (list
912 (cons 'vec (and (listp m) m))))))
913 (or (listp m) (not (nth 2 (car p)))
914 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
915 (eq exp-token 'end)))
916 (t
917 (setq m (calc-match-user-syntax (nth 1 (car p))
918 (car (nth 2 (car p)))))
919 (if (listp m)
920 (let ((vec (cons 'vec m))
921 opos mm)
922 (while (and (listp
923 (setq opos exp-pos
924 mm (calc-match-user-syntax
925 (or (nth 2 (car p))
926 (nth 1 (car p)))
927 (car (nth 2 (car p))))))
928 (> exp-pos opos))
929 (setq vec (nconc vec mm)))
930 (setq matches (nconc matches (list vec))))
931 (and (eq (car (car p)) '*)
932 (setq matches (nconc matches (list '(vec)))))))))
933 (setq p (cdr p)))
934 (if p
935 (setq exp-pos save-exp-pos
936 exp-old-pos save-exp-old-pos
937 exp-token save-exp-token
938 exp-data save-exp-data
939 matches "Failed"))
940 matches)
941 )
942
943 (defconst math-alg-inequalities
944 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
945 calcFunc-eq calcFunc-neq))
946
947 (defun math-remove-dashes (x)
948 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
949 (math-remove-dashes
950 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
951 x)
952 )
953
954 (defun math-restore-dashes (x)
955 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
956 (math-restore-dashes
957 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
958 x)
959 )
960
961 (defun math-read-if (cond op)
962 (let ((then (math-read-expr-level 0)))
963 (or (equal exp-data ":")
964 (throw 'syntax "Expected ':'"))
965 (math-read-token)
966 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op))))
967 )
968
969 (defun math-factor-after ()
970 (let ((exp-pos exp-pos)
971 exp-old-pos exp-token exp-data)
972 (math-read-token)
973 (or (memq exp-token '(number symbol dollar hash string))
974 (and (assoc exp-data '(("-") ("+") ("!") ("|") ("/")))
975 (assoc (concat "u" exp-data) math-expr-opers))
976 (eq (nth 2 (assoc exp-data math-expr-opers)) -1)
977 (assoc exp-data '(("(") ("[") ("{")))))
978 )
979
980 (defun math-read-factor ()
981 (let (op)
982 (cond ((eq exp-token 'number)
983 (let ((num (math-read-number exp-data)))
984 (if (not num)
985 (progn
986 (setq exp-old-pos exp-pos)
987 (throw 'syntax "Bad format")))
988 (math-read-token)
989 (if (and math-read-expr-quotes
990 (consp num))
991 (list 'quote num)
992 num)))
993 ((and calc-user-parse-table
994 (setq op (calc-check-user-syntax)))
995 op)
996 ((or (equal exp-data "-")
997 (equal exp-data "+")
998 (equal exp-data "!")
999 (equal exp-data "|")
1000 (equal exp-data "/"))
1001 (setq exp-data (concat "u" exp-data))
1002 (math-read-factor))
1003 ((and (setq op (assoc exp-data math-expr-opers))
1004 (eq (nth 2 op) -1))
1005 (if (consp (nth 1 op))
1006 (funcall (car (nth 1 op)) op)
1007 (math-read-token)
1008 (let ((val (math-read-expr-level (nth 3 op))))
1009 (cond ((eq (nth 1 op) 'ident)
1010 val)
1011 ((and (Math-numberp val)
1012 (equal (car op) "u-"))
1013 (math-neg val))
1014 (t (list (nth 1 op) val))))))
1015 ((eq exp-token 'symbol)
1016 (let ((sym (intern exp-data)))
1017 (math-read-token)
1018 (if (equal exp-data calc-function-open)
1019 (let ((f (assq sym math-expr-function-mapping)))
1020 (math-read-token)
1021 (if (consp (cdr f))
1022 (funcall (car (cdr f)) f sym)
1023 (let ((args (if (or (equal exp-data calc-function-close)
1024 (eq exp-token 'end))
1025 nil
1026 (math-read-expr-list))))
1027 (if (not (or (equal exp-data calc-function-close)
1028 (eq exp-token 'end)))
1029 (throw 'syntax "Expected `)'"))
1030 (math-read-token)
1031 (if (and (eq calc-language 'fortran) args
1032 (calc-extensions)
1033 (let ((calc-matrix-mode 'scalar))
1034 (math-known-matrixp
1035 (list 'var sym
1036 (intern
1037 (concat "var-"
1038 (symbol-name sym)))))))
1039 (math-parse-fortran-subscr sym args)
1040 (if f
1041 (setq sym (cdr f))
1042 (and (= (aref (symbol-name sym) 0) ?\\)
1043 (< (prefix-numeric-value calc-language-option)
1044 0)
1045 (setq sym (intern (substring (symbol-name sym)
1046 1))))
1047 (or (string-match "-" (symbol-name sym))
1048 (setq sym (intern
1049 (concat "calcFunc-"
1050 (symbol-name sym))))))
1051 (cons sym args)))))
1052 (if math-read-expr-quotes
1053 sym
1054 (let ((val (list 'var
1055 (intern (math-remove-dashes
1056 (symbol-name sym)))
1057 (if (string-match "-" (symbol-name sym))
1058 sym
1059 (intern (concat "var-"
1060 (symbol-name sym)))))))
1061 (let ((v (assq (nth 1 val) math-expr-variable-mapping)))
1062 (and v (setq val (if (consp (cdr v))
1063 (funcall (car (cdr v)) v val)
1064 (list 'var
1065 (intern
1066 (substring (symbol-name (cdr v))
1067 4))
1068 (cdr v))))))
1069 (while (and (memq calc-language '(c pascal maple))
1070 (equal exp-data "["))
1071 (math-read-token)
1072 (setq val (append (list 'calcFunc-subscr val)
1073 (math-read-expr-list)))
1074 (if (equal exp-data "]")
1075 (math-read-token)
1076 (throw 'syntax "Expected ']'")))
1077 val)))))
1078 ((eq exp-token 'dollar)
1079 (let ((abs (if (> exp-data 0) exp-data (- exp-data))))
1080 (if (>= (length calc-dollar-values) abs)
1081 (let ((num exp-data))
1082 (math-read-token)
1083 (setq calc-dollar-used (max calc-dollar-used num))
1084 (math-check-complete (nth (1- abs) calc-dollar-values)))
1085 (throw 'syntax (if calc-dollar-values
1086 "Too many $'s"
1087 "$'s not allowed in this context")))))
1088 ((eq exp-token 'hash)
1089 (or calc-hashes-used
1090 (throw 'syntax "#'s not allowed in this context"))
1091 (calc-extensions)
1092 (if (<= exp-data (length calc-arg-values))
1093 (let ((num exp-data))
1094 (math-read-token)
1095 (setq calc-hashes-used (max calc-hashes-used num))
1096 (nth (1- num) calc-arg-values))
1097 (throw 'syntax "Too many # arguments")))
1098 ((equal exp-data "(")
1099 (let* ((exp (let ((exp-keep-spaces nil))
1100 (math-read-token)
1101 (if (or (equal exp-data "\\dots")
1102 (equal exp-data "\\ldots"))
1103 '(neg (var inf var-inf))
1104 (math-read-expr-level 0)))))
1105 (let ((exp-keep-spaces nil))
1106 (cond
1107 ((equal exp-data ",")
1108 (progn
1109 (math-read-token)
1110 (let ((exp2 (math-read-expr-level 0)))
1111 (setq exp
1112 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1113 (math-normalize (list 'cplx exp exp2))
1114 (list '+ exp (list '* exp2 '(var i var-i))))))))
1115 ((equal exp-data ";")
1116 (progn
1117 (math-read-token)
1118 (let ((exp2 (math-read-expr-level 0)))
1119 (setq exp (if (and exp2 (Math-realp exp)
1120 (Math-anglep exp2))
1121 (math-normalize (list 'polar exp exp2))
1122 (calc-extensions)
1123 (list '* exp
1124 (list 'calcFunc-exp
1125 (list '*
1126 (math-to-radians-2 exp2)
1127 '(var i var-i)))))))))
1128 ((or (equal exp-data "\\dots")
1129 (equal exp-data "\\ldots"))
1130 (progn
1131 (math-read-token)
1132 (let ((exp2 (if (or (equal exp-data ")")
1133 (equal exp-data "]")
1134 (eq exp-token 'end))
1135 '(var inf var-inf)
1136 (math-read-expr-level 0))))
1137 (setq exp
1138 (list 'intv
1139 (if (equal exp-data ")") 0 1)
1140 exp
1141 exp2)))))))
1142 (if (not (or (equal exp-data ")")
1143 (and (equal exp-data "]") (eq (car-safe exp) 'intv))
1144 (eq exp-token 'end)))
1145 (throw 'syntax "Expected `)'"))
1146 (math-read-token)
1147 exp))
1148 ((eq exp-token 'string)
1149 (calc-extensions)
1150 (math-read-string))
1151 ((equal exp-data "[")
1152 (calc-extensions)
1153 (math-read-brackets t "]"))
1154 ((equal exp-data "{")
1155 (calc-extensions)
1156 (math-read-brackets nil "}"))
1157 ((equal exp-data "<")
1158 (calc-extensions)
1159 (math-read-angle-brackets))
1160 (t (throw 'syntax "Expected a number"))))
1161 )
1162
1163
1164