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