Load ediff-*.el files silently.
[bpt/emacs.git] / lisp / calc / calc-aent.el
CommitLineData
3132f345
CW
1;;; calc-aent.el --- algebraic entry functions for Calc
2
ed805bee 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
f0fa15c5 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
3132f345
CW
5
6;; Author: Dave Gillespie <daveg@synaptics.com>
e8fff8ed 7;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9
EZ
8
9;; This file is part of GNU Emacs.
10
7c671b23
GM
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
075969b4 13;; the Free Software Foundation; either version 3, or (at your option)
7c671b23
GM
14;; any later version.
15
136211a9 16;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
136211a9 25
3132f345 26;;; Commentary:
136211a9 27
3132f345 28;;; Code:
136211a9
EZ
29
30;; This file is autoloaded from calc.el.
136211a9 31
5eb3eeda 32(require 'calc)
136211a9
EZ
33(require 'calc-macs)
34
0b504d64
JB
35(defvar calc-quick-calc-history nil
36 "The history list for quick-calc.")
37
136211a9 38(defun calc-do-quick-calc ()
0b504d64 39 (require 'calc-ext)
136211a9
EZ
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))
0b504d64
JB
52 (entry (calc-do-alg-entry "" "Quick calc: " t 'calc-quick-calc-history))
53 (alg-exp (mapcar 'math-evaluate-expr entry)))
3132f345
CW
54 (when (and (= (length alg-exp) 1)
55 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
56 (= (length (car alg-exp)) 3)
57 (eq (car-safe (nth 1 (car alg-exp))) 'var))
3132f345
CW
58 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
59 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
60 (setq alg-exp (list (nth 2 (car alg-exp)))))
136211a9
EZ
61 (setq calc-quick-prev-results alg-exp
62 buf (mapconcat (function (lambda (x)
63 (math-format-value x 1000)))
64 alg-exp
65 " ")
66 shortbuf buf)
67 (if (and (= (length alg-exp) 1)
68 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
69 (< (length buf) 20)
70 (= calc-number-radix 10))
71 (setq buf (concat buf " ("
72 (let ((calc-number-radix 16))
73 (math-format-value (car alg-exp) 1000))
74 ", "
75 (let ((calc-number-radix 8))
76 (math-format-value (car alg-exp) 1000))
8f26ce73
JB
77 ", "
78 (let ((calc-number-radix 2))
79 (math-format-value (car alg-exp) 1000))
136211a9
EZ
80 (if (and (integerp (car alg-exp))
81 (> (car alg-exp) 0)
82 (< (car alg-exp) 127))
83 (format ", \"%c\"" (car alg-exp))
84 "")
85 ")")))
f20be8fe 86 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
5eb3eeda 87 (featurep 'calc-ext))
136211a9
EZ
88 (let ((long (concat (math-format-value (car entry) 1000)
89 " => " buf)))
f20be8fe 90 (if (<= (length long) (- (frame-width) 8))
136211a9
EZ
91 (setq buf long))))
92 (calc-handle-whys)
93 (message "Result: %s" buf)))
94 (if (eq last-command-char 10)
95 (insert shortbuf)
3cedbf72 96 (kill-new shortbuf)))))
136211a9
EZ
97
98(defun calc-do-calc-eval (str separator args)
99 (calc-check-defines)
100 (catch 'calc-error
101 (save-excursion
102 (calc-create-buffer)
103 (cond
104 ((and (consp str) (not (symbolp (car str))))
105 (let ((calc-language nil)
16819e48 106 (math-expr-opers (math-standard-ops))
136211a9
EZ
107 (calc-internal-prec 12)
108 (calc-word-size 32)
109 (calc-symbolic-mode nil)
110 (calc-matrix-mode nil)
111 (calc-angle-mode 'deg)
112 (calc-number-radix 10)
113 (calc-leading-zeros nil)
114 (calc-group-digits nil)
115 (calc-point-char ".")
116 (calc-frac-format '(":" nil))
117 (calc-prefer-frac nil)
118 (calc-hms-format "%s@ %s' %s\"")
119 (calc-date-format '((H ":" mm C SS pp " ")
120 Www " " Mmm " " D ", " YYYY))
121 (calc-float-format '(float 0))
122 (calc-full-float-format '(float 0))
123 (calc-complex-format nil)
124 (calc-matrix-just nil)
125 (calc-full-vectors t)
126 (calc-break-vectors nil)
127 (calc-vector-commas ",")
128 (calc-vector-brackets "[]")
129 (calc-matrix-brackets '(R O))
130 (calc-complex-mode 'cplx)
131 (calc-infinite-mode nil)
132 (calc-display-strings nil)
133 (calc-simplify-mode nil)
134 (calc-display-working-message 'lots)
135 (strp (cdr str)))
136 (while strp
137 (set (car strp) (nth 1 strp))
138 (setq strp (cdr (cdr strp))))
139 (calc-do-calc-eval (car str) separator args)))
140 ((eq separator 'eval)
141 (eval str))
142 ((eq separator 'macro)
5eb3eeda 143 (require 'calc-ext)
136211a9
EZ
144 (let* ((calc-buffer (current-buffer))
145 (calc-window (get-buffer-window calc-buffer))
146 (save-window (selected-window)))
147 (if calc-window
148 (unwind-protect
149 (progn
150 (select-window calc-window)
151 (calc-execute-kbd-macro str nil (car args)))
152 (and (window-point save-window)
153 (select-window save-window)))
154 (save-window-excursion
155 (select-window (get-largest-window))
156 (switch-to-buffer calc-buffer)
157 (calc-execute-kbd-macro str nil (car args)))))
158 nil)
159 ((eq separator 'pop)
160 (or (not (integerp str))
161 (= str 0)
162 (calc-pop (min str (calc-stack-size))))
163 (calc-stack-size))
164 ((eq separator 'top)
165 (and (integerp str)
166 (> str 0)
167 (<= str (calc-stack-size))
168 (math-format-value (calc-top-n str (car args)) 1000)))
169 ((eq separator 'rawtop)
170 (and (integerp str)
171 (> str 0)
172 (<= str (calc-stack-size))
173 (calc-top-n str (car args))))
174 (t
175 (let* ((calc-command-flags nil)
176 (calc-next-why nil)
177 (calc-language (if (memq calc-language '(nil big))
178 'flat calc-language))
179 (calc-dollar-values (mapcar
180 (function
181 (lambda (x)
182 (if (stringp x)
183 (progn
184 (setq x (math-read-exprs x))
185 (if (eq (car-safe x)
186 'error)
187 (throw 'calc-error
188 (calc-eval-error
189 (cdr x)))
190 (car x)))
191 x)))
192 args))
193 (calc-dollar-used 0)
194 (res (if (stringp str)
195 (math-read-exprs str)
196 (list str)))
197 buf)
198 (if (eq (car res) 'error)
199 (calc-eval-error (cdr res))
200 (setq res (mapcar 'calc-normalize res))
201 (and (memq 'clear-message calc-command-flags)
202 (message ""))
203 (cond ((eq separator 'pred)
5eb3eeda 204 (require 'calc-ext)
136211a9
EZ
205 (if (= (length res) 1)
206 (math-is-true (car res))
207 (calc-eval-error '(0 "Single value expected"))))
208 ((eq separator 'raw)
209 (if (= (length res) 1)
210 (car res)
211 (calc-eval-error '(0 "Single value expected"))))
212 ((eq separator 'list)
213 res)
214 ((memq separator '(num rawnum))
215 (if (= (length res) 1)
216 (if (math-constp (car res))
217 (if (eq separator 'num)
218 (math-format-value (car res) 1000)
219 (car res))
220 (calc-eval-error
221 (list 0
222 (if calc-next-why
223 (calc-explain-why (car calc-next-why))
224 "Number expected"))))
225 (calc-eval-error '(0 "Single value expected"))))
226 ((eq separator 'push)
227 (calc-push-list res)
228 nil)
229 (t (while res
230 (setq buf (concat buf
231 (and buf (or separator ", "))
232 (math-format-value (car res) 1000))
233 res (cdr res)))
07ff2bc8 234 buf)))))))))
136211a9 235
d37f2979
JB
236(defvar calc-eval-error nil
237 "Determines how calc handles errors.
8f2904a5 238If nil, return a list containing the character position of error.
d37f2979 239STRING means return error message as string rather than list.
8f2904a5 240The value t means abort and give an error message.")
d37f2979 241
136211a9 242(defun calc-eval-error (msg)
d37f2979 243 (if calc-eval-error
136211a9
EZ
244 (if (eq calc-eval-error 'string)
245 (nth 1 msg)
246 (error "%s" (nth 1 msg)))
07ff2bc8 247 msg))
136211a9
EZ
248
249
250;;;; Reading an expression in algebraic form.
251
252(defun calc-auto-algebraic-entry (&optional prefix)
253 (interactive "P")
07ff2bc8 254 (calc-algebraic-entry prefix t))
136211a9
EZ
255
256(defun calc-algebraic-entry (&optional prefix auto)
257 (interactive "P")
258 (calc-wrapper
259 (let ((calc-language (if prefix nil calc-language))
16819e48 260 (math-expr-opers (if prefix (math-standard-ops) (math-expr-ops))))
07ff2bc8 261 (calc-alg-entry (and auto (char-to-string last-command-char))))))
136211a9 262
0b504d64
JB
263(defvar calc-alg-entry-history nil
264 "History for algebraic entry.")
265
136211a9
EZ
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)))
270 (calc-dollar-used 0)
271 (calc-plain-entry t)
0b504d64 272 (alg-exp (calc-do-alg-entry initial prompt t 'calc-alg-entry-history)))
136211a9
EZ
273 (if (stringp alg-exp)
274 (progn
5eb3eeda 275 (require 'calc-ext)
136211a9
EZ
276 (calc-alg-edit alg-exp))
277 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
278 'none
279 calc-simplify-mode))
280 (nvals (mapcar 'calc-normalize alg-exp)))
281 (while alg-exp
5eb3eeda 282 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
136211a9
EZ
283 "alg'")
284 (calc-pop-push-record-list calc-dollar-used
285 (and (not (equal (car alg-exp)
286 (car nvals)))
5eb3eeda 287 (featurep 'calc-ext)
136211a9
EZ
288 "")
289 (list (car nvals)))
290 (setq alg-exp (cdr alg-exp)
291 nvals (cdr nvals)
292 calc-dollar-used 0)))
07ff2bc8 293 (calc-handle-whys))))
136211a9 294
1480882c
JB
295(defvar calc-alg-ent-map nil
296 "The keymap used for algebraic entry.")
297
298(defvar calc-alg-ent-esc-map nil
299 "The keymap used for escapes in algebraic entry.")
300
3cedbf72
JB
301(defvar calc-alg-exp)
302
0b504d64 303(defun calc-do-alg-entry (&optional initial prompt no-normalize history)
136211a9 304 (let* ((calc-buffer (current-buffer))
4db56602 305 (blink-paren-function 'calcAlg-blink-matching-open)
3cedbf72 306 (calc-alg-exp 'error))
1480882c 307 (unless calc-alg-ent-map
136211a9
EZ
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)
f1625eaa
JB
313 (let ((i 33))
314 (setq calc-alg-ent-esc-map (copy-keymap esc-map))
315 (while (< i 127)
316 (aset (nth 1 calc-alg-ent-esc-map) i 'calcAlg-escape)
317 (setq i (1+ i)))))
318 (define-key calc-alg-ent-map "\e" nil)
136211a9
EZ
319 (if (eq calc-algebraic-mode 'total)
320 (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
0b504d64 321 (define-key calc-alg-ent-map "\e+" 'calcAlg-plus-minus)
136211a9
EZ
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)
0b504d64
JB
325 (define-key calc-alg-ent-map "\ep" 'previous-history-element)
326 (define-key calc-alg-ent-map "\en" 'next-history-element)
136211a9
EZ
327 (define-key calc-alg-ent-map "\e%" 'self-insert-command))
328 (setq calc-aborted-prefix nil)
329 (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
330 (or initial "")
0b504d64 331 calc-alg-ent-map nil history)))
3cedbf72
JB
332 (when (eq calc-alg-exp 'error)
333 (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
334 (setq calc-alg-exp nil)))
136211a9
EZ
335 (setq calc-aborted-prefix "alg'")
336 (or no-normalize
3cedbf72
JB
337 (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
338 calc-alg-exp)))
136211a9
EZ
339
340(defun calcAlg-plus-minus ()
341 (interactive)
342 (if (calc-minibuffer-contains ".* \\'")
343 (insert "+/- ")
07ff2bc8 344 (insert " +/- ")))
136211a9
EZ
345
346(defun calcAlg-mod ()
347 (interactive)
348 (if (not (calc-minibuffer-contains ".* \\'"))
349 (insert " "))
350 (if (calc-minibuffer-contains ".* mod +\\'")
351 (if calc-previous-modulo
352 (insert (math-format-flat-expr calc-previous-modulo 0))
353 (beep))
07ff2bc8 354 (insert "mod ")))
136211a9
EZ
355
356(defun calcAlg-previous ()
357 (interactive)
aa9208fb 358 (if (calc-minibuffer-contains "\\'")
0b504d64 359 (previous-history-element 1)
07ff2bc8 360 (insert "'")))
136211a9
EZ
361
362(defun calcAlg-equals ()
363 (interactive)
364 (unwind-protect
365 (calcAlg-enter)
3cedbf72
JB
366 (if (consp calc-alg-exp)
367 (progn (setq prefix-arg (length calc-alg-exp))
07ff2bc8 368 (calc-unread-command ?=)))))
136211a9
EZ
369
370(defun calcAlg-escape ()
371 (interactive)
372 (calc-unread-command)
373 (save-excursion
374 (calc-select-buffer)
375 (use-local-map calc-mode-map))
07ff2bc8 376 (calcAlg-enter))
136211a9 377
3132f345 378(defvar calc-plain-entry nil)
136211a9
EZ
379(defun calcAlg-edit ()
380 (interactive)
381 (if (or (not calc-plain-entry)
382 (calc-minibuffer-contains
383 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
384 (insert "`")
3cedbf72 385 (setq calc-alg-exp (minibuffer-contents))
07ff2bc8 386 (exit-minibuffer)))
136211a9 387
d37f2979
JB
388(defvar calc-buffer)
389
136211a9
EZ
390(defun calcAlg-enter ()
391 (interactive)
f20be8fe 392 (let* ((str (minibuffer-contents))
136211a9
EZ
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
f20be8fe 399 (goto-char (minibuffer-prompt-end))
136211a9
EZ
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))
3cedbf72 405 (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
136211a9
EZ
406 '((incomplete vec))
407 exp))
07ff2bc8 408 (exit-minibuffer))))
136211a9
EZ
409
410(defun calcAlg-blink-matching-open ()
a1a6b428
JB
411 (let ((rightpt (point))
412 (leftpt nil)
413 (rightchar (preceding-char))
414 leftchar
415 rightsyntax
416 leftsyntax)
136211a9
EZ
417 (save-excursion
418 (condition-case ()
a1a6b428
JB
419 (setq leftpt (scan-sexps rightpt -1)
420 leftchar (char-after leftpt))
421 (error nil)))
422 (if (and leftpt
423 (or (and (= rightchar ?\))
424 (= leftchar ?\[))
425 (and (= rightchar ?\])
426 (= leftchar ?\()))
427 (save-excursion
428 (goto-char leftpt)
429 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
430 (let ((leftsaved (aref (syntax-table) leftchar))
431 (rightsaved (aref (syntax-table) rightchar)))
432 (unwind-protect
433 (progn
434 (cond ((= leftchar ?\[)
435 (aset (syntax-table) leftchar (cons 4 ?\)))
436 (aset (syntax-table) rightchar (cons 5 ?\[)))
437 (t
438 (aset (syntax-table) leftchar (cons 4 ?\]))
439 (aset (syntax-table) rightchar (cons 5 ?\())))
440 (blink-matching-open))
441 (aset (syntax-table) leftchar leftsaved)
442 (aset (syntax-table) rightchar rightsaved)))
07ff2bc8 443 (blink-matching-open))))
136211a9 444
136211a9 445(defun calc-alg-digit-entry ()
a1506d29 446 (calc-alg-entry
136211a9
EZ
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@ ")
07ff2bc8 452 (t (char-to-string last-command-char)))))
136211a9 453
d37f2979
JB
454;; The variable calc-digit-value is initially declared in calc.el,
455;; but can be set by calcDigit-algebraic and calcDigit-edit.
456(defvar calc-digit-value)
457
136211a9
EZ
458(defun calcDigit-algebraic ()
459 (interactive)
460 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
461 (calcDigit-key)
f20be8fe 462 (setq calc-digit-value (minibuffer-contents))
07ff2bc8 463 (exit-minibuffer)))
136211a9
EZ
464
465(defun calcDigit-edit ()
466 (interactive)
467 (calc-unread-command)
f20be8fe 468 (setq calc-digit-value (minibuffer-contents))
07ff2bc8 469 (exit-minibuffer))
136211a9
EZ
470
471
472;;; Algebraic expression parsing. [Public]
473
c59d10e0
JB
474(defvar math-read-replacement-list
475 '(;; Misc symbols
476 ("±" "+/-") ; plus or minus
477 ("×" "*") ; multiplication sign
478 ("÷" ":") ; division sign
479 ("−" "-") ; subtraction sign
480 ("∕" "/") ; division sign
481 ("∗" "*") ; asterisk multiplication
482 ("∞" "inf") ; infinity symbol
483 ("≤" "<=")
484 ("≥" ">=")
485 ("≦" "<=")
486 ("≧" ">=")
487 ;; fractions
488 ("¼" "(1:4)") ; 1/4
489 ("½" "(1:2)") ; 1/2
490 ("¾" "(3:4)") ; 3/4
491 ("⅓" "(1:3)") ; 1/3
492 ("⅔" "(2:3)") ; 2/3
493 ("⅕" "(1:5)") ; 1/5
494 ("⅖" "(2:5)") ; 2/5
495 ("⅗" "(3:5)") ; 3/5
496 ("⅘" "(4:5)") ; 4/5
497 ("⅙" "(1:6)") ; 1/6
498 ("⅚" "(5:6)") ; 5/6
499 ("⅛" "(1:8)") ; 1/8
500 ("⅜" "(3:8)") ; 3/8
501 ("⅝" "(5:8)") ; 5/8
502 ("⅞" "(7:8)") ; 7/8
503 ("⅟" "1:") ; 1/...
504 ;; superscripts
505 ("⁰" "0") ; 0
506 ("¹" "1") ; 1
507 ("²" "2") ; 2
508 ("³" "3") ; 3
509 ("⁴" "4") ; 4
510 ("⁵" "5") ; 5
511 ("⁶" "6") ; 6
512 ("⁷" "7") ; 7
513 ("⁸" "8") ; 8
514 ("⁹" "9") ; 9
515 ("⁺" "+") ; +
516 ("⁻" "-") ; -
517 ("⁽" "(") ; (
518 ("⁾" ")") ; )
519 ("ⁿ" "n") ; n
bf7bae80
JB
520 ("ⁱ" "i") ; i
521 ;; subscripts
522 ("₀" "0") ; 0
523 ("₁" "1") ; 1
524 ("₂" "2") ; 2
525 ("₃" "3") ; 3
526 ("₄" "4") ; 4
527 ("₅" "5") ; 5
528 ("₆" "6") ; 6
529 ("₇" "7") ; 7
530 ("₈" "8") ; 8
531 ("₉" "9") ; 9
532 ("₊" "+") ; +
533 ("₋" "-") ; -
534 ("₍" "(") ; (
535 ("₎" ")")) ; )
c59d10e0
JB
536 "A list whose elements (old new) indicate replacements to make
537in Calc algebraic input.")
538
539(defvar math-read-superscripts
540 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
541 "A string consisting of the superscripts allowed by Calc.")
542
bf7bae80
JB
543(defvar math-read-subscripts
544 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
545 "A string consisting of the subscripts allowed by Calc.")
546
c59d10e0
JB
547(defun math-read-preprocess-string (str)
548 "Replace some substrings of STR by Calc equivalents."
549 (setq str
550 (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
551 "^(\\&)" str))
bf7bae80
JB
552 (setq str
553 (replace-regexp-in-string (concat "[" math-read-subscripts "]+")
554 "_(\\&)" str))
c59d10e0
JB
555 (let ((rep-list math-read-replacement-list))
556 (while rep-list
557 (setq str
558 (replace-regexp-in-string (nth 0 (car rep-list))
559 (nth 1 (car rep-list)) str))
560 (setq rep-list (cdr rep-list))))
561 str)
562
d37f2979
JB
563;; The next few variables are local to math-read-exprs (and math-read-expr
564;; in calc-ext.el), but are set in functions they call.
3cedbf72
JB
565
566(defvar math-exp-pos)
567(defvar math-exp-str)
568(defvar math-exp-old-pos)
569(defvar math-exp-token)
570(defvar math-exp-keep-spaces)
d37f2979 571(defvar math-expr-data)
3cedbf72
JB
572
573(defun math-read-exprs (math-exp-str)
574 (let ((math-exp-pos 0)
575 (math-exp-old-pos 0)
576 (math-exp-keep-spaces nil)
577 math-exp-token math-expr-data)
c4446fc2 578 (setq math-exp-str (math-read-preprocess-string math-exp-str))
136211a9 579 (if calc-language-input-filter
3cedbf72
JB
580 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
581 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
582 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
583 (substring math-exp-str (+ math-exp-token 2)))))
136211a9
EZ
584 (math-build-parse-table)
585 (math-read-token)
586 (let ((val (catch 'syntax (math-read-expr-list))))
587 (if (stringp val)
3cedbf72
JB
588 (list 'error math-exp-old-pos val)
589 (if (equal math-exp-token 'end)
136211a9 590 val
3cedbf72 591 (list 'error math-exp-old-pos "Syntax error"))))))
136211a9
EZ
592
593(defun math-read-expr-list ()
3cedbf72 594 (let* ((math-exp-keep-spaces nil)
136211a9
EZ
595 (val (list (math-read-expr-level 0)))
596 (last val))
abd880c3 597 (while (equal math-expr-data ",")
136211a9
EZ
598 (math-read-token)
599 (let ((rest (list (math-read-expr-level 0))))
600 (setcdr last rest)
601 (setq last rest)))
07ff2bc8 602 val))
136211a9 603
3132f345
CW
604(defvar calc-user-parse-table nil)
605(defvar calc-last-main-parse-table nil)
b2e7f0f0 606(defvar calc-last-user-lang-parse-table nil)
3132f345
CW
607(defvar calc-last-lang-parse-table nil)
608(defvar calc-user-tokens nil)
609(defvar calc-user-token-chars nil)
136211a9 610
3cedbf72
JB
611(defvar math-toks nil
612 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
613
136211a9
EZ
614(defun math-build-parse-table ()
615 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
b2e7f0f0
JB
616 (ltab (cdr (assq calc-language calc-user-parse-tables)))
617 (lltab (get calc-language 'math-parse-table)))
136211a9 618 (or (and (eq mtab calc-last-main-parse-table)
b2e7f0f0
JB
619 (eq ltab calc-last-user-lang-parse-table)
620 (eq lltab calc-last-lang-parse-table))
621 (let ((p (append mtab ltab lltab))
3cedbf72 622 (math-toks nil))
136211a9
EZ
623 (setq calc-user-parse-table p)
624 (setq calc-user-token-chars nil)
625 (while p
626 (math-find-user-tokens (car (car p)))
627 (setq p (cdr p)))
628 (setq calc-user-tokens (mapconcat 'identity
3cedbf72 629 (sort (mapcar 'car math-toks)
136211a9
EZ
630 (function (lambda (x y)
631 (> (length x)
632 (length y)))))
633 "\\|")
634 calc-last-main-parse-table mtab
b2e7f0f0
JB
635 calc-last-user-lang-parse-table ltab
636 calc-last-lang-parse-table lltab)))))
136211a9 637
3cedbf72 638(defun math-find-user-tokens (p)
136211a9
EZ
639 (while p
640 (cond ((and (stringp (car p))
641 (or (> (length (car p)) 1) (equal (car p) "$")
642 (equal (car p) "\""))
643 (string-match "[^a-zA-Z0-9]" (car p)))
644 (let ((s (regexp-quote (car p))))
645 (if (string-match "\\`[a-zA-Z0-9]" s)
646 (setq s (concat "\\<" s)))
647 (if (string-match "[a-zA-Z0-9]\\'" s)
648 (setq s (concat s "\\>")))
3cedbf72 649 (or (assoc s math-toks)
136211a9 650 (progn
3cedbf72 651 (setq math-toks (cons (list s) math-toks))
136211a9
EZ
652 (or (memq (aref (car p) 0) calc-user-token-chars)
653 (setq calc-user-token-chars
654 (cons (aref (car p) 0)
655 calc-user-token-chars)))))))
656 ((consp (car p))
657 (math-find-user-tokens (nth 1 (car p)))
658 (or (eq (car (car p)) '\?)
659 (math-find-user-tokens (nth 2 (car p))))))
07ff2bc8 660 (setq p (cdr p))))
136211a9
EZ
661
662(defun math-read-token ()
3cedbf72
JB
663 (if (>= math-exp-pos (length math-exp-str))
664 (setq math-exp-old-pos math-exp-pos
665 math-exp-token 'end
abd880c3 666 math-expr-data "\000")
3cedbf72
JB
667 (let ((ch (aref math-exp-str math-exp-pos)))
668 (setq math-exp-old-pos math-exp-pos)
136211a9 669 (cond ((memq ch '(32 10 9))
3cedbf72
JB
670 (setq math-exp-pos (1+ math-exp-pos))
671 (if math-exp-keep-spaces
672 (setq math-exp-token 'space
abd880c3 673 math-expr-data " ")
136211a9
EZ
674 (math-read-token)))
675 ((and (memq ch calc-user-token-chars)
676 (let ((case-fold-search nil))
3cedbf72
JB
677 (eq (string-match calc-user-tokens math-exp-str math-exp-pos)
678 math-exp-pos)))
679 (setq math-exp-token 'punc
680 math-expr-data (math-match-substring math-exp-str 0)
681 math-exp-pos (match-end 0)))
136211a9
EZ
682 ((or (and (>= ch ?a) (<= ch ?z))
683 (and (>= ch ?A) (<= ch ?Z)))
684 (string-match (if (memq calc-language '(c fortran pascal maple))
685 "[a-zA-Z0-9_#]*"
686 "[a-zA-Z0-9'#]*")
3cedbf72
JB
687 math-exp-str math-exp-pos)
688 (setq math-exp-token 'symbol
689 math-exp-pos (match-end 0)
abd880c3 690 math-expr-data (math-restore-dashes
3cedbf72 691 (math-match-substring math-exp-str 0)))
136211a9 692 (if (eq calc-language 'eqn)
abd880c3 693 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
136211a9
EZ
694 (cond ((null code))
695 ((null (cdr code))
696 (math-read-token))
697 ((consp (nth 1 code))
698 (math-read-token)
abd880c3
JB
699 (if (assoc math-expr-data (cdr code))
700 (setq math-expr-data (format "%s %s"
701 (car code) math-expr-data))))
136211a9 702 ((eq (nth 1 code) 'punc)
3cedbf72 703 (setq math-exp-token 'punc
abd880c3 704 math-expr-data (nth 2 code)))
136211a9
EZ
705 (t
706 (math-read-token)
707 (math-read-token))))))
708 ((or (and (>= ch ?0) (<= ch ?9))
709 (and (eq ch '?\.)
8f2904a5 710 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
3cedbf72 711 math-exp-pos))
136211a9 712 (and (eq ch '?_)
8f2904a5 713 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
3cedbf72
JB
714 math-exp-pos)
715 (or (eq math-exp-pos 0)
136211a9 716 (and (memq calc-language '(nil flat big unform
434e6899 717 tex latex eqn))
136211a9 718 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
3cedbf72
JB
719 math-exp-str (1- math-exp-pos))
720 (1- math-exp-pos))))))
136211a9 721 (or (and (eq calc-language 'c)
3cedbf72 722 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
8f2904a5 723 (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]+\\)?\"?\\)?"
3cedbf72
JB
724 math-exp-str math-exp-pos))
725 (setq math-exp-token 'number
726 math-expr-data (math-match-substring math-exp-str 0)
727 math-exp-pos (match-end 0)))
136211a9
EZ
728 ((eq ch ?\$)
729 (if (and (eq calc-language 'pascal)
730 (eq (string-match
731 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
3cedbf72
JB
732 math-exp-str math-exp-pos)
733 math-exp-pos))
734 (setq math-exp-token 'number
735 math-expr-data (math-match-substring math-exp-str 1)
736 math-exp-pos (match-end 1))
737 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
738 math-exp-pos)
36692c4a 739 (setq math-expr-data (- (string-to-number (math-match-substring
3cedbf72
JB
740 math-exp-str 1))))
741 (string-match "\\$+" math-exp-str math-exp-pos)
abd880c3 742 (setq math-expr-data (- (match-end 0) (match-beginning 0))))
3cedbf72
JB
743 (setq math-exp-token 'dollar
744 math-exp-pos (match-end 0))))
136211a9 745 ((eq ch ?\#)
3cedbf72
JB
746 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
747 math-exp-pos)
36692c4a 748 (setq math-expr-data (string-to-number
3cedbf72
JB
749 (math-match-substring math-exp-str 1))
750 math-exp-pos (match-end 0))
abd880c3 751 (setq math-expr-data 1
3cedbf72
JB
752 math-exp-pos (1+ math-exp-pos)))
753 (setq math-exp-token 'hash))
136211a9 754 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
3cedbf72
JB
755 math-exp-str math-exp-pos)
756 math-exp-pos)
757 (setq math-exp-token 'punc
758 math-expr-data (math-match-substring math-exp-str 0)
759 math-exp-pos (match-end 0)))
136211a9 760 ((and (eq ch ?\")
8f2904a5 761 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
3cedbf72 762 math-exp-str math-exp-pos))
136211a9
EZ
763 (if (eq calc-language 'eqn)
764 (progn
3cedbf72
JB
765 (setq math-exp-str (copy-sequence math-exp-str))
766 (aset math-exp-str (match-beginning 1) ?\{)
767 (if (< (match-end 1) (length math-exp-str))
768 (aset math-exp-str (match-end 1) ?\}))
136211a9 769 (math-read-token))
3cedbf72
JB
770 (setq math-exp-token 'string
771 math-expr-data (math-match-substring math-exp-str 1)
772 math-exp-pos (match-end 0))))
a6ab3125 773 ((and (= ch ?\\) (eq calc-language 'tex)
3cedbf72 774 (< math-exp-pos (1- (length math-exp-str))))
8f2904a5 775 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
3cedbf72 776 math-exp-str math-exp-pos)
8f2904a5 777 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
3cedbf72
JB
778 math-exp-str math-exp-pos))
779 (setq math-exp-token 'symbol
780 math-exp-pos (match-end 0)
abd880c3 781 math-expr-data (math-restore-dashes
3cedbf72 782 (math-match-substring math-exp-str 1)))
a6ab3125
JB
783 (let ((code (assoc math-expr-data math-latex-ignore-words)))
784 (cond ((null code))
785 ((null (cdr code))
786 (math-read-token))
787 ((eq (nth 1 code) 'punc)
788 (setq math-exp-token 'punc
789 math-expr-data (nth 2 code)))
790 ((and (eq (nth 1 code) 'mat)
791 (string-match " *{" math-exp-str math-exp-pos))
792 (setq math-exp-pos (match-end 0)
793 math-exp-token 'punc
794 math-expr-data "[")
795 (let ((right (string-match "}" math-exp-str math-exp-pos)))
796 (and right
797 (setq math-exp-str (copy-sequence math-exp-str))
798 (aset math-exp-str right ?\])))))))
799 ((and (= ch ?\\) (eq calc-language 'latex)
800 (< math-exp-pos (1- (length math-exp-str))))
8f2904a5 801 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
a6ab3125 802 math-exp-str math-exp-pos)
8f2904a5 803 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
a6ab3125 804 math-exp-str math-exp-pos)
8f2904a5 805 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
a6ab3125
JB
806 math-exp-str math-exp-pos))
807 (setq math-exp-token 'symbol
808 math-exp-pos (match-end 0)
809 math-expr-data (math-restore-dashes
810 (math-match-substring math-exp-str 1)))
dba763ee
JB
811 (let ((code (assoc math-expr-data math-tex-ignore-words))
812 envname)
136211a9
EZ
813 (cond ((null code))
814 ((null (cdr code))
815 (math-read-token))
816 ((eq (nth 1 code) 'punc)
3cedbf72 817 (setq math-exp-token 'punc
abd880c3 818 math-expr-data (nth 2 code)))
8d5cd172
JB
819 ((and (eq (nth 1 code) 'begenv)
820 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
821 (setq math-exp-pos (match-end 0)
822 envname (match-string 1 math-exp-str)
823 math-exp-token 'punc
824 math-expr-data "[")
825 (cond ((or (string= envname "matrix")
826 (string= envname "bmatrix")
84d77b1e 827 (string= envname "smallmatrix")
8d5cd172 828 (string= envname "pmatrix"))
dba763ee
JB
829 (if (string-match (concat "\\\\end{" envname "}")
830 math-exp-str math-exp-pos)
8f2904a5 831 (setq math-exp-str
8d5cd172
JB
832 (replace-match "]" t t math-exp-str))
833 (error "%s" (concat "No closing \\end{" envname "}"))))))
834 ((and (eq (nth 1 code) 'mat)
835 (string-match " *{" math-exp-str math-exp-pos))
3cedbf72
JB
836 (setq math-exp-pos (match-end 0)
837 math-exp-token 'punc
abd880c3 838 math-expr-data "[")
3cedbf72 839 (let ((right (string-match "}" math-exp-str math-exp-pos)))
136211a9 840 (and right
3cedbf72
JB
841 (setq math-exp-str (copy-sequence math-exp-str))
842 (aset math-exp-str right ?\])))))))
136211a9
EZ
843 ((and (= ch ?\.) (eq calc-language 'fortran)
844 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
3cedbf72
JB
845 math-exp-str math-exp-pos) math-exp-pos))
846 (setq math-exp-token 'punc
847 math-expr-data (upcase (math-match-substring math-exp-str 0))
848 math-exp-pos (match-end 0)))
136211a9 849 ((and (eq calc-language 'math)
3cedbf72
JB
850 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
851 math-exp-pos))
852 (setq math-exp-token 'punc
853 math-expr-data (math-match-substring math-exp-str 0)
854 math-exp-pos (match-end 0)))
136211a9
EZ
855 ((and (eq calc-language 'eqn)
856 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
3cedbf72
JB
857 math-exp-str math-exp-pos)
858 math-exp-pos))
859 (setq math-exp-token 'punc
860 math-expr-data (math-match-substring math-exp-str 0)
861 math-exp-pos (match-end 0))
8f2904a5 862 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
3cedbf72
JB
863 math-exp-pos)
864 (setq math-exp-pos (match-end 0)))
abd880c3 865 (if (memq (aref math-expr-data 0) '(?~ ?^))
136211a9 866 (math-read-token)))
3cedbf72
JB
867 ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
868 (setq math-exp-pos (match-end 0))
136211a9
EZ
869 (math-read-token))
870 (t
434e6899 871 (if (and (eq ch ?\{) (memq calc-language '(tex latex eqn)))
136211a9 872 (setq ch ?\())
434e6899 873 (if (and (eq ch ?\}) (memq calc-language '(tex latex eqn)))
136211a9 874 (setq ch ?\)))
434e6899 875 (if (and (eq ch ?\&) (memq calc-language '(tex latex)))
136211a9 876 (setq ch ?\,))
3cedbf72 877 (setq math-exp-token 'punc
abd880c3 878 math-expr-data (char-to-string ch)
3cedbf72 879 math-exp-pos (1+ math-exp-pos)))))))
136211a9 880
d37f2979
JB
881(defconst math-alg-inequalities
882 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
883 calcFunc-eq calcFunc-neq))
136211a9
EZ
884
885(defun math-read-expr-level (exp-prec &optional exp-term)
16819e48
JB
886 (let* ((math-expr-opers (math-expr-ops))
887 (x (math-read-factor))
888 (first t)
889 op op2)
136211a9
EZ
890 (while (and (or (and calc-user-parse-table
891 (setq op (calc-check-user-syntax x exp-prec))
892 (setq x op
893 op '("2x" ident 999999 -1)))
abd880c3 894 (and (setq op (assoc math-expr-data math-expr-opers))
136211a9
EZ
895 (/= (nth 2 op) -1)
896 (or (and (setq op2 (assoc
abd880c3 897 math-expr-data
136211a9
EZ
898 (cdr (memq op math-expr-opers))))
899 (eq (= (nth 3 op) -1)
900 (/= (nth 3 op2) -1))
901 (eq (= (nth 3 op2) -1)
902 (not (math-factor-after)))
903 (setq op op2))
904 t))
905 (and (or (eq (nth 2 op) -1)
3cedbf72 906 (memq math-exp-token '(symbol number dollar hash))
abd880c3
JB
907 (equal math-expr-data "(")
908 (and (equal math-expr-data "[")
136211a9 909 (not (eq calc-language 'math))
3cedbf72 910 (not (and math-exp-keep-spaces
136211a9 911 (eq (car-safe x) 'vec)))))
abd880c3 912 (or (not (setq op (assoc math-expr-data math-expr-opers)))
136211a9
EZ
913 (/= (nth 2 op) -1))
914 (or (not calc-user-parse-table)
3cedbf72 915 (not (eq math-exp-token 'symbol))
136211a9
EZ
916 (let ((p calc-user-parse-table))
917 (while (and p
918 (or (not (integerp
919 (car (car (car p)))))
920 (not (equal
921 (nth 1 (car (car p)))
abd880c3 922 math-expr-data))))
136211a9
EZ
923 (setq p (cdr p)))
924 (not p)))
925 (setq op (assoc "2x" math-expr-opers))))
abd880c3 926 (not (and exp-term (equal math-expr-data exp-term)))
136211a9
EZ
927 (>= (nth 2 op) exp-prec))
928 (if (not (equal (car op) "2x"))
929 (math-read-token))
930 (and (memq (nth 1 op) '(sdev mod))
5eb3eeda 931 (require 'calc-ext))
136211a9
EZ
932 (setq x (cond ((consp (nth 1 op))
933 (funcall (car (nth 1 op)) x op))
934 ((eq (nth 3 op) -1)
935 (if (eq (nth 1 op) 'ident)
936 x
937 (if (eq (nth 1 op) 'closing)
938 (if (eq (nth 2 op) exp-prec)
939 (progn
940 (setq exp-prec 1000)
941 x)
942 (throw 'syntax "Mismatched delimiters"))
943 (list (nth 1 op) x))))
944 ((and (not first)
945 (memq (nth 1 op) math-alg-inequalities)
946 (memq (car-safe x) math-alg-inequalities))
5eb3eeda 947 (require 'calc-ext)
136211a9
EZ
948 (math-composite-inequalities x op))
949 (t (list (nth 1 op)
950 x
951 (math-read-expr-level (nth 3 op) exp-term))))
952 first nil))
07ff2bc8 953 x))
136211a9 954
35cdbd30
JB
955;; calc-arg-values is defined in calc-ext.el, but is used here.
956(defvar calc-arg-values)
957
136211a9
EZ
958(defun calc-check-user-syntax (&optional x prec)
959 (let ((p calc-user-parse-table)
960 (matches nil)
961 match rule)
962 (while (and p
963 (or (not (progn
964 (setq rule (car (car p)))
965 (if x
966 (and (integerp (car rule))
967 (>= (car rule) prec)
abd880c3 968 (equal math-expr-data
136211a9 969 (car (setq rule (cdr rule)))))
abd880c3 970 (equal math-expr-data (car rule)))))
3cedbf72
JB
971 (let ((save-exp-pos math-exp-pos)
972 (save-exp-old-pos math-exp-old-pos)
973 (save-exp-token math-exp-token)
abd880c3 974 (save-exp-data math-expr-data))
136211a9
EZ
975 (or (not (listp
976 (setq matches (calc-match-user-syntax rule))))
977 (let ((args (progn
5eb3eeda 978 (require 'calc-ext)
136211a9
EZ
979 calc-arg-values))
980 (conds nil)
981 temp)
982 (if x
983 (setq matches (cons x matches)))
984 (setq match (cdr (car p)))
985 (while (and (eq (car-safe match)
986 'calcFunc-condition)
987 (= (length match) 3))
988 (setq conds (append (math-flatten-lands
989 (nth 2 match))
990 conds)
991 match (nth 1 match)))
992 (while (and conds match)
5eb3eeda 993 (require 'calc-ext)
136211a9
EZ
994 (cond ((eq (car-safe (car conds))
995 'calcFunc-let)
996 (setq temp (car conds))
997 (or (= (length temp) 3)
998 (and (= (length temp) 2)
999 (eq (car-safe (nth 1 temp))
1000 'calcFunc-assign)
1001 (= (length (nth 1 temp)) 3)
1002 (setq temp (nth 1 temp)))
1003 (setq match nil))
1004 (setq matches (cons
1005 (math-normalize
1006 (math-multi-subst
1007 (nth 2 temp)
1008 args matches))
1009 matches)
1010 args (cons (nth 1 temp)
1011 args)))
1012 ((and (eq (car-safe (car conds))
1013 'calcFunc-matches)
1014 (= (length (car conds)) 3))
1015 (setq temp (calcFunc-vmatches
1016 (math-multi-subst
1017 (nth 1 (car conds))
1018 args matches)
1019 (nth 2 (car conds))))
1020 (if (eq temp 0)
1021 (setq match nil)
1022 (while (setq temp (cdr temp))
1023 (setq matches (cons (nth 2 (car temp))
1024 matches)
1025 args (cons (nth 1 (car temp))
1026 args)))))
1027 (t
1028 (or (math-is-true (math-simplify
1029 (math-multi-subst
1030 (car conds)
1031 args matches)))
1032 (setq match nil))))
1033 (setq conds (cdr conds)))
1034 (if match
1035 (not (setq match (math-multi-subst
1036 match args matches)))
3cedbf72
JB
1037 (setq math-exp-old-pos save-exp-old-pos
1038 math-exp-token save-exp-token
abd880c3 1039 math-expr-data save-exp-data
3cedbf72 1040 math-exp-pos save-exp-pos)))))))
136211a9 1041 (setq p (cdr p)))
07ff2bc8 1042 (and p match)))
136211a9
EZ
1043
1044(defun calc-match-user-syntax (p &optional term)
1045 (let ((matches nil)
3cedbf72
JB
1046 (save-exp-pos math-exp-pos)
1047 (save-exp-old-pos math-exp-old-pos)
1048 (save-exp-token math-exp-token)
1049 (save-exp-data math-expr-data)
1050 m)
136211a9
EZ
1051 (while (and p
1052 (cond ((stringp (car p))
abd880c3 1053 (and (equal math-expr-data (car p))
136211a9
EZ
1054 (progn
1055 (math-read-token)
1056 t)))
1057 ((integerp (car p))
1058 (and (setq m (catch 'syntax
1059 (math-read-expr-level
1060 (car p)
1061 (if (cdr p)
1062 (if (consp (nth 1 p))
1063 (car (nth 1 (nth 1 p)))
1064 (nth 1 p))
1065 term))))
1066 (not (stringp m))
1067 (setq matches (nconc matches (list m)))))
1068 ((eq (car (car p)) '\?)
1069 (setq m (calc-match-user-syntax (nth 1 (car p))))
1070 (or (nth 2 (car p))
1071 (setq matches
1072 (nconc matches
1073 (list
1074 (cons 'vec (and (listp m) m))))))
1075 (or (listp m) (not (nth 2 (car p)))
1076 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
3cedbf72 1077 (eq math-exp-token 'end)))
136211a9
EZ
1078 (t
1079 (setq m (calc-match-user-syntax (nth 1 (car p))
1080 (car (nth 2 (car p)))))
1081 (if (listp m)
1082 (let ((vec (cons 'vec m))
1083 opos mm)
1084 (while (and (listp
3cedbf72 1085 (setq opos math-exp-pos
136211a9
EZ
1086 mm (calc-match-user-syntax
1087 (or (nth 2 (car p))
1088 (nth 1 (car p)))
1089 (car (nth 2 (car p))))))
3cedbf72 1090 (> math-exp-pos opos))
136211a9
EZ
1091 (setq vec (nconc vec mm)))
1092 (setq matches (nconc matches (list vec))))
1093 (and (eq (car (car p)) '*)
1094 (setq matches (nconc matches (list '(vec)))))))))
1095 (setq p (cdr p)))
1096 (if p
3cedbf72
JB
1097 (setq math-exp-pos save-exp-pos
1098 math-exp-old-pos save-exp-old-pos
1099 math-exp-token save-exp-token
abd880c3 1100 math-expr-data save-exp-data
136211a9 1101 matches "Failed"))
07ff2bc8 1102 matches))
136211a9 1103
136211a9
EZ
1104(defun math-remove-dashes (x)
1105 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
1106 (math-remove-dashes
1107 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
07ff2bc8 1108 x))
136211a9
EZ
1109
1110(defun math-restore-dashes (x)
1111 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
1112 (math-restore-dashes
1113 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
07ff2bc8 1114 x))
136211a9
EZ
1115
1116(defun math-read-if (cond op)
1117 (let ((then (math-read-expr-level 0)))
abd880c3 1118 (or (equal math-expr-data ":")
136211a9
EZ
1119 (throw 'syntax "Expected ':'"))
1120 (math-read-token)
07ff2bc8 1121 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
136211a9
EZ
1122
1123(defun math-factor-after ()
3cedbf72
JB
1124 (let ((math-exp-pos math-exp-pos)
1125 math-exp-old-pos math-exp-token math-expr-data)
136211a9 1126 (math-read-token)
3cedbf72 1127 (or (memq math-exp-token '(number symbol dollar hash string))
abd880c3
JB
1128 (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
1129 (assoc (concat "u" math-expr-data) math-expr-opers))
1130 (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
1131 (assoc math-expr-data '(("(") ("[") ("{"))))))
136211a9
EZ
1132
1133(defun math-read-factor ()
16819e48
JB
1134 (let ((math-expr-opers (math-expr-ops))
1135 op)
3cedbf72 1136 (cond ((eq math-exp-token 'number)
abd880c3 1137 (let ((num (math-read-number math-expr-data)))
136211a9
EZ
1138 (if (not num)
1139 (progn
3cedbf72 1140 (setq math-exp-old-pos math-exp-pos)
136211a9
EZ
1141 (throw 'syntax "Bad format")))
1142 (math-read-token)
1143 (if (and math-read-expr-quotes
1144 (consp num))
1145 (list 'quote num)
1146 num)))
1147 ((and calc-user-parse-table
1148 (setq op (calc-check-user-syntax)))
1149 op)
abd880c3
JB
1150 ((or (equal math-expr-data "-")
1151 (equal math-expr-data "+")
1152 (equal math-expr-data "!")
1153 (equal math-expr-data "|")
1154 (equal math-expr-data "/"))
1155 (setq math-expr-data (concat "u" math-expr-data))
136211a9 1156 (math-read-factor))
abd880c3 1157 ((and (setq op (assoc math-expr-data math-expr-opers))
136211a9
EZ
1158 (eq (nth 2 op) -1))
1159 (if (consp (nth 1 op))
1160 (funcall (car (nth 1 op)) op)
1161 (math-read-token)
1162 (let ((val (math-read-expr-level (nth 3 op))))
1163 (cond ((eq (nth 1 op) 'ident)
1164 val)
1165 ((and (Math-numberp val)
1166 (equal (car op) "u-"))
1167 (math-neg val))
1168 (t (list (nth 1 op) val))))))
3cedbf72 1169 ((eq math-exp-token 'symbol)
abd880c3 1170 (let ((sym (intern math-expr-data)))
136211a9 1171 (math-read-token)
abd880c3 1172 (if (equal math-expr-data calc-function-open)
136211a9
EZ
1173 (let ((f (assq sym math-expr-function-mapping)))
1174 (math-read-token)
1175 (if (consp (cdr f))
1176 (funcall (car (cdr f)) f sym)
abd880c3 1177 (let ((args (if (or (equal math-expr-data calc-function-close)
3cedbf72 1178 (eq math-exp-token 'end))
136211a9
EZ
1179 nil
1180 (math-read-expr-list))))
abd880c3 1181 (if (not (or (equal math-expr-data calc-function-close)
3cedbf72 1182 (eq math-exp-token 'end)))
136211a9
EZ
1183 (throw 'syntax "Expected `)'"))
1184 (math-read-token)
1185 (if (and (eq calc-language 'fortran) args
5eb3eeda 1186 (require 'calc-ext)
136211a9
EZ
1187 (let ((calc-matrix-mode 'scalar))
1188 (math-known-matrixp
1189 (list 'var sym
1190 (intern
1191 (concat "var-"
1192 (symbol-name sym)))))))
1193 (math-parse-fortran-subscr sym args)
1194 (if f
1195 (setq sym (cdr f))
1196 (and (= (aref (symbol-name sym) 0) ?\\)
1197 (< (prefix-numeric-value calc-language-option)
1198 0)
1199 (setq sym (intern (substring (symbol-name sym)
1200 1))))
1201 (or (string-match "-" (symbol-name sym))
1202 (setq sym (intern
1203 (concat "calcFunc-"
1204 (symbol-name sym))))))
1205 (cons sym args)))))
1206 (if math-read-expr-quotes
1207 sym
1208 (let ((val (list 'var
1209 (intern (math-remove-dashes
1210 (symbol-name sym)))
1211 (if (string-match "-" (symbol-name sym))
1212 sym
1213 (intern (concat "var-"
1214 (symbol-name sym)))))))
1215 (let ((v (assq (nth 1 val) math-expr-variable-mapping)))
1216 (and v (setq val (if (consp (cdr v))
1217 (funcall (car (cdr v)) v val)
1218 (list 'var
1219 (intern
1220 (substring (symbol-name (cdr v))
1221 4))
1222 (cdr v))))))
1223 (while (and (memq calc-language '(c pascal maple))
abd880c3 1224 (equal math-expr-data "["))
136211a9
EZ
1225 (math-read-token)
1226 (setq val (append (list 'calcFunc-subscr val)
1227 (math-read-expr-list)))
abd880c3 1228 (if (equal math-expr-data "]")
136211a9
EZ
1229 (math-read-token)
1230 (throw 'syntax "Expected ']'")))
1231 val)))))
3cedbf72 1232 ((eq math-exp-token 'dollar)
abd880c3 1233 (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
136211a9 1234 (if (>= (length calc-dollar-values) abs)
abd880c3 1235 (let ((num math-expr-data))
136211a9
EZ
1236 (math-read-token)
1237 (setq calc-dollar-used (max calc-dollar-used num))
1238 (math-check-complete (nth (1- abs) calc-dollar-values)))
1239 (throw 'syntax (if calc-dollar-values
1240 "Too many $'s"
1241 "$'s not allowed in this context")))))
3cedbf72 1242 ((eq math-exp-token 'hash)
136211a9
EZ
1243 (or calc-hashes-used
1244 (throw 'syntax "#'s not allowed in this context"))
5eb3eeda 1245 (require 'calc-ext)
abd880c3
JB
1246 (if (<= math-expr-data (length calc-arg-values))
1247 (let ((num math-expr-data))
136211a9
EZ
1248 (math-read-token)
1249 (setq calc-hashes-used (max calc-hashes-used num))
1250 (nth (1- num) calc-arg-values))
1251 (throw 'syntax "Too many # arguments")))
abd880c3 1252 ((equal math-expr-data "(")
3cedbf72 1253 (let* ((exp (let ((math-exp-keep-spaces nil))
136211a9 1254 (math-read-token)
abd880c3
JB
1255 (if (or (equal math-expr-data "\\dots")
1256 (equal math-expr-data "\\ldots"))
136211a9
EZ
1257 '(neg (var inf var-inf))
1258 (math-read-expr-level 0)))))
3cedbf72 1259 (let ((math-exp-keep-spaces nil))
136211a9 1260 (cond
abd880c3 1261 ((equal math-expr-data ",")
136211a9
EZ
1262 (progn
1263 (math-read-token)
1264 (let ((exp2 (math-read-expr-level 0)))
1265 (setq exp
1266 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1267 (math-normalize (list 'cplx exp exp2))
1268 (list '+ exp (list '* exp2 '(var i var-i))))))))
abd880c3 1269 ((equal math-expr-data ";")
136211a9
EZ
1270 (progn
1271 (math-read-token)
1272 (let ((exp2 (math-read-expr-level 0)))
1273 (setq exp (if (and exp2 (Math-realp exp)
1274 (Math-anglep exp2))
1275 (math-normalize (list 'polar exp exp2))
5eb3eeda 1276 (require 'calc-ext)
136211a9
EZ
1277 (list '* exp
1278 (list 'calcFunc-exp
1279 (list '*
1280 (math-to-radians-2 exp2)
1281 '(var i var-i)))))))))
abd880c3
JB
1282 ((or (equal math-expr-data "\\dots")
1283 (equal math-expr-data "\\ldots"))
136211a9
EZ
1284 (progn
1285 (math-read-token)
abd880c3
JB
1286 (let ((exp2 (if (or (equal math-expr-data ")")
1287 (equal math-expr-data "]")
3cedbf72 1288 (eq math-exp-token 'end))
136211a9
EZ
1289 '(var inf var-inf)
1290 (math-read-expr-level 0))))
1291 (setq exp
1292 (list 'intv
abd880c3 1293 (if (equal math-expr-data ")") 0 1)
136211a9
EZ
1294 exp
1295 exp2)))))))
abd880c3
JB
1296 (if (not (or (equal math-expr-data ")")
1297 (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
3cedbf72 1298 (eq math-exp-token 'end)))
136211a9
EZ
1299 (throw 'syntax "Expected `)'"))
1300 (math-read-token)
1301 exp))
3cedbf72 1302 ((eq math-exp-token 'string)
5eb3eeda 1303 (require 'calc-ext)
136211a9 1304 (math-read-string))
abd880c3 1305 ((equal math-expr-data "[")
5eb3eeda 1306 (require 'calc-ext)
136211a9 1307 (math-read-brackets t "]"))
abd880c3 1308 ((equal math-expr-data "{")
5eb3eeda 1309 (require 'calc-ext)
136211a9 1310 (math-read-brackets nil "}"))
abd880c3 1311 ((equal math-expr-data "<")
5eb3eeda 1312 (require 'calc-ext)
136211a9 1313 (math-read-angle-brackets))
07ff2bc8 1314 (t (throw 'syntax "Expected a number")))))
136211a9 1315
47233535
JB
1316(provide 'calc-aent)
1317
ab5796a9 1318;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
07ff2bc8 1319;;; calc-aent.el ends here