Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / calc / calc-aent.el
CommitLineData
3132f345
CW
1;;; calc-aent.el --- algebraic entry functions for Calc
2
acaf905b 3;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
3132f345
CW
4
5;; Author: Dave Gillespie <daveg@synaptics.com>
e8fff8ed 6;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9
EZ
7
8;; This file is part of GNU Emacs.
9
662c9c64 10;; GNU Emacs is free software: you can redistribute it and/or modify
7c671b23 11;; it under the terms of the GNU General Public License as published by
662c9c64
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
7c671b23 14
136211a9 15;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
662c9c64 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
136211a9 22
3132f345 23;;; Commentary:
136211a9 24
3132f345 25;;; Code:
136211a9
EZ
26
27;; This file is autoloaded from calc.el.
136211a9 28
5eb3eeda 29(require 'calc)
136211a9
EZ
30(require 'calc-macs)
31
2f78f904
JB
32;; Declare functions which are defined elsewhere.
33(declare-function calc-refresh-evaltos "calc-ext" (&optional which-var))
34(declare-function calc-execute-kbd-macro "calc-prog" (mac arg &rest prefix))
35(declare-function math-is-true "calc-ext" (expr))
36(declare-function calc-explain-why "calc-stuff" (why &optional more))
37(declare-function calc-alg-edit "calc-yank" (str))
38(declare-function math-composite-inequalities "calc-prog" (x op))
39(declare-function math-flatten-lands "calc-rewr" (expr))
40(declare-function math-multi-subst "calc-map" (expr olds news))
41(declare-function calcFunc-vmatches "calc-rewr" (expr pat))
42(declare-function math-simplify "calc-alg" (top-expr))
43(declare-function math-known-matrixp "calc-arith" (a))
44(declare-function math-parse-fortran-subscr "calc-lang" (sym args))
45(declare-function math-to-radians-2 "calc-math" (a))
46(declare-function math-read-string "calc-ext" ())
47(declare-function math-read-brackets "calc-vec" (space-sep math-rb-close))
48(declare-function math-read-angle-brackets "calc-forms" ())
d039d4d8 49(declare-function math-to-percentsigns "calccomp" (x))
2f78f904 50
0b504d64
JB
51(defvar calc-quick-calc-history nil
52 "The history list for quick-calc.")
53
2378f044 54;;;###autoload
136211a9 55(defun calc-do-quick-calc ()
0b504d64 56 (require 'calc-ext)
136211a9
EZ
57 (calc-check-defines)
58 (if (eq major-mode 'calc-mode)
59 (calc-algebraic-entry t)
60 (let (buf shortbuf)
61 (save-excursion
62 (calc-create-buffer)
63 (let* ((calc-command-flags nil)
64 (calc-dollar-values calc-quick-prev-results)
65 (calc-dollar-used 0)
66 (enable-recursive-minibuffers t)
67 (calc-language (if (memq calc-language '(nil big))
68 'flat calc-language))
0b504d64
JB
69 (entry (calc-do-alg-entry "" "Quick calc: " t 'calc-quick-calc-history))
70 (alg-exp (mapcar 'math-evaluate-expr entry)))
3132f345
CW
71 (when (and (= (length alg-exp) 1)
72 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
73 (= (length (car alg-exp)) 3)
74 (eq (car-safe (nth 1 (car alg-exp))) 'var))
3132f345
CW
75 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
76 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
77 (setq alg-exp (list (nth 2 (car alg-exp)))))
136211a9
EZ
78 (setq calc-quick-prev-results alg-exp
79 buf (mapconcat (function (lambda (x)
80 (math-format-value x 1000)))
81 alg-exp
82 " ")
83 shortbuf buf)
84 (if (and (= (length alg-exp) 1)
85 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
86 (< (length buf) 20)
87 (= calc-number-radix 10))
88 (setq buf (concat buf " ("
89 (let ((calc-number-radix 16))
90 (math-format-value (car alg-exp) 1000))
91 ", "
92 (let ((calc-number-radix 8))
93 (math-format-value (car alg-exp) 1000))
8f26ce73
JB
94 ", "
95 (let ((calc-number-radix 2))
96 (math-format-value (car alg-exp) 1000))
136211a9
EZ
97 (if (and (integerp (car alg-exp))
98 (> (car alg-exp) 0)
99 (< (car alg-exp) 127))
100 (format ", \"%c\"" (car alg-exp))
101 "")
102 ")")))
f20be8fe 103 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
5eb3eeda 104 (featurep 'calc-ext))
136211a9
EZ
105 (let ((long (concat (math-format-value (car entry) 1000)
106 " => " buf)))
f20be8fe 107 (if (<= (length long) (- (frame-width) 8))
136211a9
EZ
108 (setq buf long))))
109 (calc-handle-whys)
110 (message "Result: %s" buf)))
e93c003e 111 (if (eq last-command-event 10)
136211a9 112 (insert shortbuf)
3cedbf72 113 (kill-new shortbuf)))))
136211a9 114
2378f044 115;;;###autoload
136211a9
EZ
116(defun calc-do-calc-eval (str separator args)
117 (calc-check-defines)
118 (catch 'calc-error
119 (save-excursion
120 (calc-create-buffer)
121 (cond
122 ((and (consp str) (not (symbolp (car str))))
123 (let ((calc-language nil)
16819e48 124 (math-expr-opers (math-standard-ops))
136211a9
EZ
125 (calc-internal-prec 12)
126 (calc-word-size 32)
127 (calc-symbolic-mode nil)
128 (calc-matrix-mode nil)
129 (calc-angle-mode 'deg)
130 (calc-number-radix 10)
8768b132 131 (calc-twos-complement-mode nil)
136211a9
EZ
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
2378f044 271;;;###autoload
136211a9
EZ
272(defun calc-auto-algebraic-entry (&optional prefix)
273 (interactive "P")
07ff2bc8 274 (calc-algebraic-entry prefix t))
136211a9 275
2378f044 276;;;###autoload
136211a9
EZ
277(defun calc-algebraic-entry (&optional prefix auto)
278 (interactive "P")
279 (calc-wrapper
280 (let ((calc-language (if prefix nil calc-language))
16819e48 281 (math-expr-opers (if prefix (math-standard-ops) (math-expr-ops))))
e93c003e 282 (calc-alg-entry (and auto (char-to-string last-command-event))))))
136211a9 283
0b504d64
JB
284(defvar calc-alg-entry-history nil
285 "History for algebraic entry.")
286
2378f044 287;;;###autoload
136211a9
EZ
288(defun calc-alg-entry (&optional initial prompt)
289 (let* ((sel-mode nil)
290 (calc-dollar-values (mapcar 'calc-get-stack-element
291 (nthcdr calc-stack-top calc-stack)))
292 (calc-dollar-used 0)
293 (calc-plain-entry t)
0b504d64 294 (alg-exp (calc-do-alg-entry initial prompt t 'calc-alg-entry-history)))
136211a9
EZ
295 (if (stringp alg-exp)
296 (progn
5eb3eeda 297 (require 'calc-ext)
136211a9 298 (calc-alg-edit alg-exp))
e93c003e 299 (let* ((calc-simplify-mode (if (eq last-command-event ?\C-j)
136211a9
EZ
300 'none
301 calc-simplify-mode))
302 (nvals (mapcar 'calc-normalize alg-exp)))
303 (while alg-exp
5eb3eeda 304 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
136211a9
EZ
305 "alg'")
306 (calc-pop-push-record-list calc-dollar-used
307 (and (not (equal (car alg-exp)
308 (car nvals)))
5eb3eeda 309 (featurep 'calc-ext)
136211a9
EZ
310 "")
311 (list (car nvals)))
312 (setq alg-exp (cdr alg-exp)
313 nvals (cdr nvals)
314 calc-dollar-used 0)))
07ff2bc8 315 (calc-handle-whys))))
136211a9 316
0da20854
SM
317(defvar calc-alg-ent-map
318 (let ((map (make-sparse-keymap)))
319 (set-keymap-parent map minibuffer-local-map)
320 (define-key map "'" 'calcAlg-previous)
321 (define-key map "`" 'calcAlg-edit)
322 (define-key map "\C-m" 'calcAlg-enter)
323 (define-key map "\C-j" 'calcAlg-enter)
324 map)
1480882c
JB
325 "The keymap used for algebraic entry.")
326
0da20854
SM
327(defvar calc-alg-ent-esc-map
328 (let ((map (make-keymap))
329 (i 33))
330 (set-keymap-parent map esc-map)
331 (while (< i 127)
332 (define-key map (vector i) 'calcAlg-escape)
333 (setq i (1+ i)))
334 map)
1480882c
JB
335 "The keymap used for escapes in algebraic entry.")
336
3cedbf72
JB
337(defvar calc-alg-exp)
338
2378f044 339;;;###autoload
0b504d64 340(defun calc-do-alg-entry (&optional initial prompt no-normalize history)
136211a9 341 (let* ((calc-buffer (current-buffer))
0da20854 342 (blink-matching-check-function 'calcAlg-blink-matching-check)
3cedbf72 343 (calc-alg-exp 'error))
f1625eaa 344 (define-key calc-alg-ent-map "\e" nil)
136211a9
EZ
345 (if (eq calc-algebraic-mode 'total)
346 (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
0b504d64 347 (define-key calc-alg-ent-map "\e+" 'calcAlg-plus-minus)
136211a9
EZ
348 (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
349 (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
350 (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
0b504d64
JB
351 (define-key calc-alg-ent-map "\ep" 'previous-history-element)
352 (define-key calc-alg-ent-map "\en" 'next-history-element)
136211a9
EZ
353 (define-key calc-alg-ent-map "\e%" 'self-insert-command))
354 (setq calc-aborted-prefix nil)
355 (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
356 (or initial "")
0b504d64 357 calc-alg-ent-map nil history)))
3cedbf72
JB
358 (when (eq calc-alg-exp 'error)
359 (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
360 (setq calc-alg-exp nil)))
136211a9
EZ
361 (setq calc-aborted-prefix "alg'")
362 (or no-normalize
3cedbf72
JB
363 (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
364 calc-alg-exp)))
136211a9
EZ
365
366(defun calcAlg-plus-minus ()
367 (interactive)
368 (if (calc-minibuffer-contains ".* \\'")
369 (insert "+/- ")
07ff2bc8 370 (insert " +/- ")))
136211a9
EZ
371
372(defun calcAlg-mod ()
373 (interactive)
374 (if (not (calc-minibuffer-contains ".* \\'"))
375 (insert " "))
376 (if (calc-minibuffer-contains ".* mod +\\'")
377 (if calc-previous-modulo
378 (insert (math-format-flat-expr calc-previous-modulo 0))
379 (beep))
07ff2bc8 380 (insert "mod ")))
136211a9
EZ
381
382(defun calcAlg-previous ()
383 (interactive)
aa9208fb 384 (if (calc-minibuffer-contains "\\'")
0b504d64 385 (previous-history-element 1)
07ff2bc8 386 (insert "'")))
136211a9
EZ
387
388(defun calcAlg-equals ()
389 (interactive)
390 (unwind-protect
391 (calcAlg-enter)
3cedbf72
JB
392 (if (consp calc-alg-exp)
393 (progn (setq prefix-arg (length calc-alg-exp))
07ff2bc8 394 (calc-unread-command ?=)))))
136211a9
EZ
395
396(defun calcAlg-escape ()
397 (interactive)
398 (calc-unread-command)
399 (save-excursion
400 (calc-select-buffer)
401 (use-local-map calc-mode-map))
07ff2bc8 402 (calcAlg-enter))
136211a9 403
3132f345 404(defvar calc-plain-entry nil)
136211a9
EZ
405(defun calcAlg-edit ()
406 (interactive)
407 (if (or (not calc-plain-entry)
408 (calc-minibuffer-contains
409 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
410 (insert "`")
3cedbf72 411 (setq calc-alg-exp (minibuffer-contents))
07ff2bc8 412 (exit-minibuffer)))
136211a9 413
d37f2979
JB
414(defvar calc-buffer)
415
136211a9
EZ
416(defun calcAlg-enter ()
417 (interactive)
f20be8fe 418 (let* ((str (minibuffer-contents))
136211a9 419 (exp (and (> (length str) 0)
6df9b6d7 420 (with-current-buffer calc-buffer
136211a9
EZ
421 (math-read-exprs str)))))
422 (if (eq (car-safe exp) 'error)
423 (progn
f20be8fe 424 (goto-char (minibuffer-prompt-end))
136211a9
EZ
425 (forward-char (nth 1 exp))
426 (beep)
427 (calc-temp-minibuffer-message
428 (concat " [" (or (nth 2 exp) "Error") "]"))
429 (calc-clear-unread-commands))
3cedbf72 430 (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
136211a9
EZ
431 '((incomplete vec))
432 exp))
07ff2bc8 433 (exit-minibuffer))))
136211a9 434
0da20854
SM
435(defun calcAlg-blink-matching-check (leftpt rightpt)
436 (let ((rightchar (char-before rightpt))
437 (leftchar (if leftpt (char-after leftpt))))
a1a6b428
JB
438 (if (and leftpt
439 (or (and (= rightchar ?\))
440 (= leftchar ?\[))
441 (and (= rightchar ?\])
442 (= leftchar ?\()))
443 (save-excursion
444 (goto-char leftpt)
445 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
0da20854
SM
446 ;; [2..5) perfectly valid!
447 nil
448 (blink-matching-check-mismatch leftpt rightpt))))
136211a9 449
2378f044 450;;;###autoload
136211a9 451(defun calc-alg-digit-entry ()
a1506d29 452 (calc-alg-entry
e93c003e 453 (cond ((eq last-command-event ?e)
136211a9 454 (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e"))
e93c003e
GM
455 ((eq last-command-event ?#) (format "%d#" calc-number-radix))
456 ((eq last-command-event ?_) "-")
457 ((eq last-command-event ?@) "0@ ")
458 (t (char-to-string last-command-event)))))
136211a9 459
d37f2979
JB
460;; The variable calc-digit-value is initially declared in calc.el,
461;; but can be set by calcDigit-algebraic and calcDigit-edit.
462(defvar calc-digit-value)
463
2378f044 464;;;###autoload
136211a9
EZ
465(defun calcDigit-algebraic ()
466 (interactive)
467 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
468 (calcDigit-key)
f20be8fe 469 (setq calc-digit-value (minibuffer-contents))
07ff2bc8 470 (exit-minibuffer)))
136211a9 471
2378f044 472;;;###autoload
136211a9
EZ
473(defun calcDigit-edit ()
474 (interactive)
475 (calc-unread-command)
f20be8fe 476 (setq calc-digit-value (minibuffer-contents))
07ff2bc8 477 (exit-minibuffer))
136211a9
EZ
478
479
480;;; Algebraic expression parsing. [Public]
481
c59d10e0
JB
482(defvar math-read-replacement-list
483 '(;; Misc symbols
484 ("±" "+/-") ; plus or minus
485 ("×" "*") ; multiplication sign
486 ("÷" ":") ; division sign
487 ("−" "-") ; subtraction sign
488 ("∕" "/") ; division sign
489 ("∗" "*") ; asterisk multiplication
490 ("∞" "inf") ; infinity symbol
491 ("≤" "<=")
492 ("≥" ">=")
493 ("≦" "<=")
494 ("≧" ">=")
ae6bc504 495 ("µ" "μ")
c59d10e0
JB
496 ;; fractions
497 ("¼" "(1:4)") ; 1/4
498 ("½" "(1:2)") ; 1/2
499 ("¾" "(3:4)") ; 3/4
500 ("⅓" "(1:3)") ; 1/3
501 ("⅔" "(2:3)") ; 2/3
502 ("⅕" "(1:5)") ; 1/5
503 ("⅖" "(2:5)") ; 2/5
504 ("⅗" "(3:5)") ; 3/5
505 ("⅘" "(4:5)") ; 4/5
506 ("⅙" "(1:6)") ; 1/6
507 ("⅚" "(5:6)") ; 5/6
508 ("⅛" "(1:8)") ; 1/8
509 ("⅜" "(3:8)") ; 3/8
510 ("⅝" "(5:8)") ; 5/8
511 ("⅞" "(7:8)") ; 7/8
512 ("⅟" "1:") ; 1/...
513 ;; superscripts
514 ("⁰" "0") ; 0
515 ("¹" "1") ; 1
516 ("²" "2") ; 2
517 ("³" "3") ; 3
518 ("⁴" "4") ; 4
519 ("⁵" "5") ; 5
520 ("⁶" "6") ; 6
521 ("⁷" "7") ; 7
522 ("⁸" "8") ; 8
523 ("⁹" "9") ; 9
524 ("⁺" "+") ; +
525 ("⁻" "-") ; -
526 ("⁽" "(") ; (
527 ("⁾" ")") ; )
528 ("ⁿ" "n") ; n
bf7bae80
JB
529 ("ⁱ" "i") ; i
530 ;; subscripts
531 ("₀" "0") ; 0
532 ("₁" "1") ; 1
533 ("₂" "2") ; 2
534 ("₃" "3") ; 3
535 ("₄" "4") ; 4
536 ("₅" "5") ; 5
537 ("₆" "6") ; 6
538 ("₇" "7") ; 7
539 ("₈" "8") ; 8
540 ("₉" "9") ; 9
541 ("₊" "+") ; +
542 ("₋" "-") ; -
543 ("₍" "(") ; (
544 ("₎" ")")) ; )
c59d10e0
JB
545 "A list whose elements (old new) indicate replacements to make
546in Calc algebraic input.")
547
548(defvar math-read-superscripts
549 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
550 "A string consisting of the superscripts allowed by Calc.")
551
bf7bae80
JB
552(defvar math-read-subscripts
553 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
554 "A string consisting of the subscripts allowed by Calc.")
555
2378f044 556;;;###autoload
c59d10e0
JB
557(defun math-read-preprocess-string (str)
558 "Replace some substrings of STR by Calc equivalents."
559 (setq str
560 (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
561 "^(\\&)" str))
bf7bae80
JB
562 (setq str
563 (replace-regexp-in-string (concat "[" math-read-subscripts "]+")
564 "_(\\&)" str))
c59d10e0
JB
565 (let ((rep-list math-read-replacement-list))
566 (while rep-list
567 (setq str
568 (replace-regexp-in-string (nth 0 (car rep-list))
569 (nth 1 (car rep-list)) str))
570 (setq rep-list (cdr rep-list))))
571 str)
572
d37f2979
JB
573;; The next few variables are local to math-read-exprs (and math-read-expr
574;; in calc-ext.el), but are set in functions they call.
3cedbf72
JB
575
576(defvar math-exp-pos)
577(defvar math-exp-str)
578(defvar math-exp-old-pos)
579(defvar math-exp-token)
580(defvar math-exp-keep-spaces)
d37f2979 581(defvar math-expr-data)
3cedbf72 582
2378f044 583;;;###autoload
3cedbf72
JB
584(defun math-read-exprs (math-exp-str)
585 (let ((math-exp-pos 0)
586 (math-exp-old-pos 0)
587 (math-exp-keep-spaces nil)
588 math-exp-token math-expr-data)
c4446fc2 589 (setq math-exp-str (math-read-preprocess-string math-exp-str))
d039d4d8
JB
590 (if (memq calc-language calc-lang-allow-percentsigns)
591 (setq math-exp-str (math-remove-percentsigns math-exp-str)))
136211a9 592 (if calc-language-input-filter
3cedbf72 593 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
be19ef0b 594 (while (setq math-exp-token
d039d4d8 595 (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
be19ef0b 596 (setq math-exp-str
d039d4d8 597 (concat (substring math-exp-str 0 math-exp-token) "\\dots"
3cedbf72 598 (substring math-exp-str (+ math-exp-token 2)))))
136211a9
EZ
599 (math-build-parse-table)
600 (math-read-token)
601 (let ((val (catch 'syntax (math-read-expr-list))))
602 (if (stringp val)
3cedbf72
JB
603 (list 'error math-exp-old-pos val)
604 (if (equal math-exp-token 'end)
136211a9 605 val
3cedbf72 606 (list 'error math-exp-old-pos "Syntax error"))))))
136211a9 607
2378f044 608;;;###autoload
136211a9 609(defun math-read-expr-list ()
3cedbf72 610 (let* ((math-exp-keep-spaces nil)
136211a9
EZ
611 (val (list (math-read-expr-level 0)))
612 (last val))
abd880c3 613 (while (equal math-expr-data ",")
136211a9
EZ
614 (math-read-token)
615 (let ((rest (list (math-read-expr-level 0))))
616 (setcdr last rest)
617 (setq last rest)))
07ff2bc8 618 val))
136211a9 619
3132f345
CW
620(defvar calc-user-parse-table nil)
621(defvar calc-last-main-parse-table nil)
b2e7f0f0 622(defvar calc-last-user-lang-parse-table nil)
3132f345
CW
623(defvar calc-last-lang-parse-table nil)
624(defvar calc-user-tokens nil)
625(defvar calc-user-token-chars nil)
136211a9 626
3cedbf72
JB
627(defvar math-toks nil
628 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
629
2378f044 630;;;###autoload
136211a9
EZ
631(defun math-build-parse-table ()
632 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
b2e7f0f0
JB
633 (ltab (cdr (assq calc-language calc-user-parse-tables)))
634 (lltab (get calc-language 'math-parse-table)))
136211a9 635 (or (and (eq mtab calc-last-main-parse-table)
b2e7f0f0
JB
636 (eq ltab calc-last-user-lang-parse-table)
637 (eq lltab calc-last-lang-parse-table))
638 (let ((p (append mtab ltab lltab))
3cedbf72 639 (math-toks nil))
136211a9
EZ
640 (setq calc-user-parse-table p)
641 (setq calc-user-token-chars nil)
642 (while p
643 (math-find-user-tokens (car (car p)))
644 (setq p (cdr p)))
645 (setq calc-user-tokens (mapconcat 'identity
3cedbf72 646 (sort (mapcar 'car math-toks)
136211a9
EZ
647 (function (lambda (x y)
648 (> (length x)
649 (length y)))))
650 "\\|")
651 calc-last-main-parse-table mtab
b2e7f0f0
JB
652 calc-last-user-lang-parse-table ltab
653 calc-last-lang-parse-table lltab)))))
136211a9 654
2378f044 655;;;###autoload
3cedbf72 656(defun math-find-user-tokens (p)
136211a9
EZ
657 (while p
658 (cond ((and (stringp (car p))
659 (or (> (length (car p)) 1) (equal (car p) "$")
660 (equal (car p) "\""))
ae6bc504 661 (string-match "[^a-zA-Zα-ωΑ-Ω0-9]" (car p)))
136211a9 662 (let ((s (regexp-quote (car p))))
ae6bc504 663 (if (string-match "\\`[a-zA-Zα-ωΑ-Ω0-9]" s)
136211a9 664 (setq s (concat "\\<" s)))
ae6bc504 665 (if (string-match "[a-zA-Zα-ωΑ-Ω0-9]\\'" s)
136211a9 666 (setq s (concat s "\\>")))
3cedbf72 667 (or (assoc s math-toks)
136211a9 668 (progn
3cedbf72 669 (setq math-toks (cons (list s) math-toks))
136211a9
EZ
670 (or (memq (aref (car p) 0) calc-user-token-chars)
671 (setq calc-user-token-chars
672 (cons (aref (car p) 0)
673 calc-user-token-chars)))))))
674 ((consp (car p))
675 (math-find-user-tokens (nth 1 (car p)))
676 (or (eq (car (car p)) '\?)
677 (math-find-user-tokens (nth 2 (car p))))))
07ff2bc8 678 (setq p (cdr p))))
136211a9 679
2378f044 680;;;###autoload
136211a9 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))
be19ef0b 697 (eq (string-match
d039d4d8 698 calc-user-tokens math-exp-str math-exp-pos)
3cedbf72
JB
699 math-exp-pos)))
700 (setq math-exp-token 'punc
701 math-expr-data (math-match-substring math-exp-str 0)
702 math-exp-pos (match-end 0)))
136211a9 703 ((or (and (>= ch ?a) (<= ch ?z))
ae6bc504
JB
704 (and (>= ch ?A) (<= ch ?Z))
705 (and (>= ch ?α) (<= ch ?ω))
706 (and (>= ch ?Α) (<= ch ?Ω)))
be19ef0b 707 (string-match
d039d4d8
JB
708 (cond
709 ((and (memq calc-language calc-lang-allow-underscores)
710 (memq calc-language calc-lang-allow-percentsigns))
ae6bc504 711 "[a-zA-Zα-ωΑ-Ω0-9_'#]*")
d039d4d8 712 ((memq calc-language calc-lang-allow-underscores)
ae6bc504
JB
713 "[a-zA-Zα-ωΑ-Ω0-9_#]*")
714 (t "[a-zA-Zα-ωΑ-Ω0-9'#]*"))
d039d4d8 715 math-exp-str math-exp-pos)
3cedbf72
JB
716 (setq math-exp-token 'symbol
717 math-exp-pos (match-end 0)
abd880c3 718 math-expr-data (math-restore-dashes
3cedbf72 719 (math-match-substring math-exp-str 0)))
2807e8e4
JB
720 (if (setq adfn (get calc-language 'math-lang-adjust-words))
721 (funcall adfn)))
136211a9
EZ
722 ((or (and (>= ch ?0) (<= ch ?9))
723 (and (eq ch '?\.)
8f2904a5 724 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
3cedbf72 725 math-exp-pos))
136211a9 726 (and (eq ch '?_)
8f2904a5 727 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
3cedbf72
JB
728 math-exp-pos)
729 (or (eq math-exp-pos 0)
be19ef0b 730 (and (not (memq calc-language
2807e8e4 731 calc-lang-allow-underscores))
ae6bc504 732 (eq (string-match "[^])}\"a-zA-Zα-ωΑ-Ω0-9'$]_"
3cedbf72
JB
733 math-exp-str (1- math-exp-pos))
734 (1- math-exp-pos))))))
2807e8e4 735 (or (and (memq calc-language calc-lang-c-type-hex)
3cedbf72 736 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
ae6bc504 737 (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
738 math-exp-str math-exp-pos))
739 (setq math-exp-token 'number
740 math-expr-data (math-match-substring math-exp-str 0)
741 math-exp-pos (match-end 0)))
be19ef0b 742 ((and (setq adfn
2807e8e4
JB
743 (assq ch (get calc-language 'math-lang-read-symbol)))
744 (eval (nth 1 adfn)))
745 (eval (nth 2 adfn)))
136211a9 746 ((eq ch ?\$)
2807e8e4
JB
747 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
748 math-exp-pos)
749 (setq math-expr-data (- (string-to-number (math-match-substring
750 math-exp-str 1))))
751 (string-match "\\$+" math-exp-str math-exp-pos)
752 (setq math-expr-data (- (match-end 0) (match-beginning 0))))
753 (setq math-exp-token 'dollar
754 math-exp-pos (match-end 0)))
136211a9 755 ((eq ch ?\#)
3cedbf72
JB
756 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
757 math-exp-pos)
36692c4a 758 (setq math-expr-data (string-to-number
3cedbf72
JB
759 (math-match-substring math-exp-str 1))
760 math-exp-pos (match-end 0))
abd880c3 761 (setq math-expr-data 1
3cedbf72
JB
762 math-exp-pos (1+ math-exp-pos)))
763 (setq math-exp-token 'hash))
136211a9 764 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
3cedbf72
JB
765 math-exp-str math-exp-pos)
766 math-exp-pos)
767 (setq math-exp-token 'punc
768 math-expr-data (math-match-substring math-exp-str 0)
769 math-exp-pos (match-end 0)))
136211a9 770 ((and (eq ch ?\")
8f2904a5 771 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
3cedbf72 772 math-exp-str math-exp-pos))
2807e8e4
JB
773 (setq math-exp-token 'string
774 math-expr-data (math-match-substring math-exp-str 1)
775 math-exp-pos (match-end 0)))
5fb4539b 776 ((and (setq adfn (get calc-language 'math-lang-read))
2807e8e4 777 (eval (nth 0 adfn))
5fb4539b 778 (eval (nth 1 adfn))))
3cedbf72
JB
779 ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
780 (setq math-exp-pos (match-end 0))
136211a9
EZ
781 (math-read-token))
782 (t
2807e8e4
JB
783 (if (setq adfn (assq ch (get calc-language 'math-punc-table)))
784 (setq ch (cdr adfn)))
3cedbf72 785 (setq math-exp-token 'punc
abd880c3 786 math-expr-data (char-to-string ch)
3cedbf72 787 math-exp-pos (1+ math-exp-pos)))))))
136211a9 788
d37f2979
JB
789(defconst math-alg-inequalities
790 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
791 calcFunc-eq calcFunc-neq))
136211a9
EZ
792
793(defun math-read-expr-level (exp-prec &optional exp-term)
16819e48 794 (let* ((math-expr-opers (math-expr-ops))
be19ef0b
GM
795 (x (math-read-factor))
796 (first t)
16819e48 797 op op2)
136211a9
EZ
798 (while (and (or (and calc-user-parse-table
799 (setq op (calc-check-user-syntax x exp-prec))
800 (setq x op
801 op '("2x" ident 999999 -1)))
abd880c3 802 (and (setq op (assoc math-expr-data math-expr-opers))
136211a9
EZ
803 (/= (nth 2 op) -1)
804 (or (and (setq op2 (assoc
abd880c3 805 math-expr-data
136211a9
EZ
806 (cdr (memq op math-expr-opers))))
807 (eq (= (nth 3 op) -1)
808 (/= (nth 3 op2) -1))
809 (eq (= (nth 3 op2) -1)
810 (not (math-factor-after)))
811 (setq op op2))
812 t))
813 (and (or (eq (nth 2 op) -1)
3cedbf72 814 (memq math-exp-token '(symbol number dollar hash))
abd880c3
JB
815 (equal math-expr-data "(")
816 (and (equal math-expr-data "[")
be19ef0b
GM
817 (not (equal
818 (get calc-language
2807e8e4 819 'math-function-open) "["))
3cedbf72 820 (not (and math-exp-keep-spaces
136211a9 821 (eq (car-safe x) 'vec)))))
abd880c3 822 (or (not (setq op (assoc math-expr-data math-expr-opers)))
136211a9
EZ
823 (/= (nth 2 op) -1))
824 (or (not calc-user-parse-table)
3cedbf72 825 (not (eq math-exp-token 'symbol))
136211a9
EZ
826 (let ((p calc-user-parse-table))
827 (while (and p
828 (or (not (integerp
829 (car (car (car p)))))
830 (not (equal
831 (nth 1 (car (car p)))
abd880c3 832 math-expr-data))))
136211a9
EZ
833 (setq p (cdr p)))
834 (not p)))
835 (setq op (assoc "2x" math-expr-opers))))
abd880c3 836 (not (and exp-term (equal math-expr-data exp-term)))
136211a9
EZ
837 (>= (nth 2 op) exp-prec))
838 (if (not (equal (car op) "2x"))
839 (math-read-token))
840 (and (memq (nth 1 op) '(sdev mod))
5eb3eeda 841 (require 'calc-ext))
136211a9
EZ
842 (setq x (cond ((consp (nth 1 op))
843 (funcall (car (nth 1 op)) x op))
844 ((eq (nth 3 op) -1)
845 (if (eq (nth 1 op) 'ident)
846 x
847 (if (eq (nth 1 op) 'closing)
848 (if (eq (nth 2 op) exp-prec)
849 (progn
850 (setq exp-prec 1000)
851 x)
852 (throw 'syntax "Mismatched delimiters"))
853 (list (nth 1 op) x))))
854 ((and (not first)
855 (memq (nth 1 op) math-alg-inequalities)
856 (memq (car-safe x) math-alg-inequalities))
5eb3eeda 857 (require 'calc-ext)
136211a9
EZ
858 (math-composite-inequalities x op))
859 (t (list (nth 1 op)
860 x
861 (math-read-expr-level (nth 3 op) exp-term))))
862 first nil))
07ff2bc8 863 x))
136211a9 864
35cdbd30
JB
865;; calc-arg-values is defined in calc-ext.el, but is used here.
866(defvar calc-arg-values)
867
2378f044 868;;;###autoload
136211a9
EZ
869(defun calc-check-user-syntax (&optional x prec)
870 (let ((p calc-user-parse-table)
871 (matches nil)
872 match rule)
873 (while (and p
874 (or (not (progn
875 (setq rule (car (car p)))
876 (if x
877 (and (integerp (car rule))
878 (>= (car rule) prec)
abd880c3 879 (equal math-expr-data
136211a9 880 (car (setq rule (cdr rule)))))
abd880c3 881 (equal math-expr-data (car rule)))))
3cedbf72
JB
882 (let ((save-exp-pos math-exp-pos)
883 (save-exp-old-pos math-exp-old-pos)
884 (save-exp-token math-exp-token)
abd880c3 885 (save-exp-data math-expr-data))
136211a9
EZ
886 (or (not (listp
887 (setq matches (calc-match-user-syntax rule))))
888 (let ((args (progn
5eb3eeda 889 (require 'calc-ext)
136211a9
EZ
890 calc-arg-values))
891 (conds nil)
892 temp)
893 (if x
894 (setq matches (cons x matches)))
895 (setq match (cdr (car p)))
896 (while (and (eq (car-safe match)
897 'calcFunc-condition)
898 (= (length match) 3))
899 (setq conds (append (math-flatten-lands
900 (nth 2 match))
901 conds)
902 match (nth 1 match)))
903 (while (and conds match)
5eb3eeda 904 (require 'calc-ext)
136211a9
EZ
905 (cond ((eq (car-safe (car conds))
906 'calcFunc-let)
907 (setq temp (car conds))
908 (or (= (length temp) 3)
909 (and (= (length temp) 2)
910 (eq (car-safe (nth 1 temp))
911 'calcFunc-assign)
912 (= (length (nth 1 temp)) 3)
913 (setq temp (nth 1 temp)))
914 (setq match nil))
915 (setq matches (cons
916 (math-normalize
917 (math-multi-subst
918 (nth 2 temp)
919 args matches))
920 matches)
921 args (cons (nth 1 temp)
922 args)))
923 ((and (eq (car-safe (car conds))
924 'calcFunc-matches)
925 (= (length (car conds)) 3))
926 (setq temp (calcFunc-vmatches
927 (math-multi-subst
928 (nth 1 (car conds))
929 args matches)
930 (nth 2 (car conds))))
931 (if (eq temp 0)
932 (setq match nil)
933 (while (setq temp (cdr temp))
934 (setq matches (cons (nth 2 (car temp))
935 matches)
936 args (cons (nth 1 (car temp))
937 args)))))
938 (t
939 (or (math-is-true (math-simplify
940 (math-multi-subst
941 (car conds)
942 args matches)))
943 (setq match nil))))
944 (setq conds (cdr conds)))
945 (if match
946 (not (setq match (math-multi-subst
947 match args matches)))
3cedbf72
JB
948 (setq math-exp-old-pos save-exp-old-pos
949 math-exp-token save-exp-token
abd880c3 950 math-expr-data save-exp-data
3cedbf72 951 math-exp-pos save-exp-pos)))))))
136211a9 952 (setq p (cdr p)))
07ff2bc8 953 (and p match)))
136211a9 954
2378f044 955;;;###autoload
136211a9
EZ
956(defun calc-match-user-syntax (p &optional term)
957 (let ((matches nil)
3cedbf72
JB
958 (save-exp-pos math-exp-pos)
959 (save-exp-old-pos math-exp-old-pos)
960 (save-exp-token math-exp-token)
961 (save-exp-data math-expr-data)
962 m)
136211a9
EZ
963 (while (and p
964 (cond ((stringp (car p))
abd880c3 965 (and (equal math-expr-data (car p))
136211a9
EZ
966 (progn
967 (math-read-token)
968 t)))
969 ((integerp (car p))
970 (and (setq m (catch 'syntax
971 (math-read-expr-level
972 (car p)
973 (if (cdr p)
974 (if (consp (nth 1 p))
975 (car (nth 1 (nth 1 p)))
976 (nth 1 p))
977 term))))
978 (not (stringp m))
979 (setq matches (nconc matches (list m)))))
980 ((eq (car (car p)) '\?)
981 (setq m (calc-match-user-syntax (nth 1 (car p))))
982 (or (nth 2 (car p))
983 (setq matches
984 (nconc matches
985 (list
986 (cons 'vec (and (listp m) m))))))
987 (or (listp m) (not (nth 2 (car p)))
988 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
3cedbf72 989 (eq math-exp-token 'end)))
136211a9
EZ
990 (t
991 (setq m (calc-match-user-syntax (nth 1 (car p))
992 (car (nth 2 (car p)))))
993 (if (listp m)
994 (let ((vec (cons 'vec m))
995 opos mm)
996 (while (and (listp
3cedbf72 997 (setq opos math-exp-pos
136211a9
EZ
998 mm (calc-match-user-syntax
999 (or (nth 2 (car p))
1000 (nth 1 (car p)))
1001 (car (nth 2 (car p))))))
3cedbf72 1002 (> math-exp-pos opos))
136211a9
EZ
1003 (setq vec (nconc vec mm)))
1004 (setq matches (nconc matches (list vec))))
1005 (and (eq (car (car p)) '*)
1006 (setq matches (nconc matches (list '(vec)))))))))
1007 (setq p (cdr p)))
1008 (if p
3cedbf72
JB
1009 (setq math-exp-pos save-exp-pos
1010 math-exp-old-pos save-exp-old-pos
1011 math-exp-token save-exp-token
abd880c3 1012 math-expr-data save-exp-data
136211a9 1013 matches "Failed"))
07ff2bc8 1014 matches))
136211a9 1015
2378f044 1016;;;###autoload
136211a9
EZ
1017(defun math-remove-dashes (x)
1018 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
1019 (math-remove-dashes
1020 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
07ff2bc8 1021 x))
136211a9 1022
d039d4d8 1023(defun math-remove-percentsigns (x)
d039d4d8
JB
1024 (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x)
1025 (math-remove-percentsigns
c0db5755 1026 (concat (math-match-substring x 1) "o'o" (math-match-substring x 2)))
d039d4d8
JB
1027 x))
1028
136211a9
EZ
1029(defun math-restore-dashes (x)
1030 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
1031 (math-restore-dashes
1032 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
07ff2bc8 1033 x))
136211a9 1034
d039d4d8
JB
1035(defun math-restore-placeholders (x)
1036 "Replace placeholders by the proper characters in the symbol x.
1037This includes `#' for `_' and `'' for `%'.
1038If the current Calc language does not use placeholders, return nil."
1039 (if (or (memq calc-language calc-lang-allow-underscores)
1040 (memq calc-language calc-lang-allow-percentsigns))
1041 (let ((sx (symbol-name x)))
1042 (when (memq calc-language calc-lang-allow-percentsigns)
1043 (require 'calccomp)
1044 (setq sx (math-to-percentsigns sx)))
1045 (if (memq calc-language calc-lang-allow-underscores)
1046 (setq sx (math-string-restore-underscores sx)))
1047 (intern-soft sx))))
57f8977d
JB
1048
1049(defun math-string-restore-underscores (x)
1050 "Replace pound signs by underscores in the string x."
1051 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x)
1052 (math-string-restore-underscores
1053 (concat (math-match-substring x 1) "_" (math-match-substring x 2)))
1054 x))
1055
2378f044 1056;;;###autoload
136211a9
EZ
1057(defun math-read-if (cond op)
1058 (let ((then (math-read-expr-level 0)))
abd880c3 1059 (or (equal math-expr-data ":")
136211a9
EZ
1060 (throw 'syntax "Expected ':'"))
1061 (math-read-token)
07ff2bc8 1062 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
136211a9
EZ
1063
1064(defun math-factor-after ()
3cedbf72
JB
1065 (let ((math-exp-pos math-exp-pos)
1066 math-exp-old-pos math-exp-token math-expr-data)
136211a9 1067 (math-read-token)
3cedbf72 1068 (or (memq math-exp-token '(number symbol dollar hash string))
abd880c3
JB
1069 (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
1070 (assoc (concat "u" math-expr-data) math-expr-opers))
1071 (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
1072 (assoc math-expr-data '(("(") ("[") ("{"))))))
136211a9
EZ
1073
1074(defun math-read-factor ()
16819e48
JB
1075 (let ((math-expr-opers (math-expr-ops))
1076 op)
3cedbf72 1077 (cond ((eq math-exp-token 'number)
abd880c3 1078 (let ((num (math-read-number math-expr-data)))
136211a9
EZ
1079 (if (not num)
1080 (progn
3cedbf72 1081 (setq math-exp-old-pos math-exp-pos)
136211a9
EZ
1082 (throw 'syntax "Bad format")))
1083 (math-read-token)
1084 (if (and math-read-expr-quotes
1085 (consp num))
1086 (list 'quote num)
1087 num)))
1088 ((and calc-user-parse-table
1089 (setq op (calc-check-user-syntax)))
1090 op)
abd880c3
JB
1091 ((or (equal math-expr-data "-")
1092 (equal math-expr-data "+")
1093 (equal math-expr-data "!")
1094 (equal math-expr-data "|")
1095 (equal math-expr-data "/"))
1096 (setq math-expr-data (concat "u" math-expr-data))
136211a9 1097 (math-read-factor))
abd880c3 1098 ((and (setq op (assoc math-expr-data math-expr-opers))
136211a9
EZ
1099 (eq (nth 2 op) -1))
1100 (if (consp (nth 1 op))
1101 (funcall (car (nth 1 op)) op)
1102 (math-read-token)
1103 (let ((val (math-read-expr-level (nth 3 op))))
1104 (cond ((eq (nth 1 op) 'ident)
1105 val)
1106 ((and (Math-numberp val)
1107 (equal (car op) "u-"))
1108 (math-neg val))
1109 (t (list (nth 1 op) val))))))
3cedbf72 1110 ((eq math-exp-token 'symbol)
abd880c3 1111 (let ((sym (intern math-expr-data)))
136211a9 1112 (math-read-token)
abd880c3 1113 (if (equal math-expr-data calc-function-open)
136211a9
EZ
1114 (let ((f (assq sym math-expr-function-mapping)))
1115 (math-read-token)
1116 (if (consp (cdr f))
1117 (funcall (car (cdr f)) f sym)
abd880c3 1118 (let ((args (if (or (equal math-expr-data calc-function-close)
3cedbf72 1119 (eq math-exp-token 'end))
136211a9
EZ
1120 nil
1121 (math-read-expr-list))))
abd880c3 1122 (if (not (or (equal math-expr-data calc-function-close)
3cedbf72 1123 (eq math-exp-token 'end)))
136211a9
EZ
1124 (throw 'syntax "Expected `)'"))
1125 (math-read-token)
be19ef0b
GM
1126 (if (and (memq calc-language
1127 calc-lang-parens-are-subscripts)
2807e8e4 1128 args
5eb3eeda 1129 (require 'calc-ext)
136211a9
EZ
1130 (let ((calc-matrix-mode 'scalar))
1131 (math-known-matrixp
1132 (list 'var sym
1133 (intern
1134 (concat "var-"
1135 (symbol-name sym)))))))
1136 (math-parse-fortran-subscr sym args)
1137 (if f
1138 (setq sym (cdr f))
1139 (and (= (aref (symbol-name sym) 0) ?\\)
1140 (< (prefix-numeric-value calc-language-option)
1141 0)
1142 (setq sym (intern (substring (symbol-name sym)
1143 1))))
1144 (or (string-match "-" (symbol-name sym))
1145 (setq sym (intern
1146 (concat "calcFunc-"
1147 (symbol-name sym))))))
1148 (cons sym args)))))
1149 (if math-read-expr-quotes
1150 sym
1151 (let ((val (list 'var
1152 (intern (math-remove-dashes
1153 (symbol-name sym)))
1154 (if (string-match "-" (symbol-name sym))
1155 sym
1156 (intern (concat "var-"
1157 (symbol-name sym)))))))
57f8977d
JB
1158 (let ((v (or
1159 (assq (nth 1 val) math-expr-variable-mapping)
d039d4d8 1160 (assq (math-restore-placeholders (nth 1 val))
57f8977d 1161 math-expr-variable-mapping))))
136211a9
EZ
1162 (and v (setq val (if (consp (cdr v))
1163 (funcall (car (cdr v)) v val)
1164 (list 'var
1165 (intern
1166 (substring (symbol-name (cdr v))
1167 4))
1168 (cdr v))))))
be19ef0b 1169 (while (and (memq calc-language
2807e8e4 1170 calc-lang-brackets-are-subscripts)
abd880c3 1171 (equal math-expr-data "["))
136211a9 1172 (math-read-token)
2807e8e4
JB
1173 (let ((el (math-read-expr-list)))
1174 (while el
1175 (setq val (append (list 'calcFunc-subscr val)
1176 (list (car el))))
1177 (setq el (cdr el))))
abd880c3 1178 (if (equal math-expr-data "]")
136211a9
EZ
1179 (math-read-token)
1180 (throw 'syntax "Expected ']'")))
1181 val)))))
3cedbf72 1182 ((eq math-exp-token 'dollar)
abd880c3 1183 (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
136211a9 1184 (if (>= (length calc-dollar-values) abs)
abd880c3 1185 (let ((num math-expr-data))
136211a9
EZ
1186 (math-read-token)
1187 (setq calc-dollar-used (max calc-dollar-used num))
1188 (math-check-complete (nth (1- abs) calc-dollar-values)))
1189 (throw 'syntax (if calc-dollar-values
1190 "Too many $'s"
1191 "$'s not allowed in this context")))))
3cedbf72 1192 ((eq math-exp-token 'hash)
136211a9
EZ
1193 (or calc-hashes-used
1194 (throw 'syntax "#'s not allowed in this context"))
5eb3eeda 1195 (require 'calc-ext)
abd880c3
JB
1196 (if (<= math-expr-data (length calc-arg-values))
1197 (let ((num math-expr-data))
136211a9
EZ
1198 (math-read-token)
1199 (setq calc-hashes-used (max calc-hashes-used num))
1200 (nth (1- num) calc-arg-values))
1201 (throw 'syntax "Too many # arguments")))
abd880c3 1202 ((equal math-expr-data "(")
3cedbf72 1203 (let* ((exp (let ((math-exp-keep-spaces nil))
136211a9 1204 (math-read-token)
abd880c3
JB
1205 (if (or (equal math-expr-data "\\dots")
1206 (equal math-expr-data "\\ldots"))
136211a9
EZ
1207 '(neg (var inf var-inf))
1208 (math-read-expr-level 0)))))
3cedbf72 1209 (let ((math-exp-keep-spaces nil))
136211a9 1210 (cond
abd880c3 1211 ((equal math-expr-data ",")
136211a9
EZ
1212 (progn
1213 (math-read-token)
1214 (let ((exp2 (math-read-expr-level 0)))
1215 (setq exp
1216 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1217 (math-normalize (list 'cplx exp exp2))
1218 (list '+ exp (list '* exp2 '(var i var-i))))))))
abd880c3 1219 ((equal math-expr-data ";")
136211a9
EZ
1220 (progn
1221 (math-read-token)
1222 (let ((exp2 (math-read-expr-level 0)))
1223 (setq exp (if (and exp2 (Math-realp exp)
1224 (Math-anglep exp2))
1225 (math-normalize (list 'polar exp exp2))
5eb3eeda 1226 (require 'calc-ext)
136211a9
EZ
1227 (list '* exp
1228 (list 'calcFunc-exp
1229 (list '*
1230 (math-to-radians-2 exp2)
1231 '(var i var-i)))))))))
abd880c3
JB
1232 ((or (equal math-expr-data "\\dots")
1233 (equal math-expr-data "\\ldots"))
136211a9
EZ
1234 (progn
1235 (math-read-token)
abd880c3
JB
1236 (let ((exp2 (if (or (equal math-expr-data ")")
1237 (equal math-expr-data "]")
3cedbf72 1238 (eq math-exp-token 'end))
136211a9
EZ
1239 '(var inf var-inf)
1240 (math-read-expr-level 0))))
1241 (setq exp
1242 (list 'intv
abd880c3 1243 (if (equal math-expr-data ")") 0 1)
136211a9
EZ
1244 exp
1245 exp2)))))))
abd880c3
JB
1246 (if (not (or (equal math-expr-data ")")
1247 (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
3cedbf72 1248 (eq math-exp-token 'end)))
136211a9
EZ
1249 (throw 'syntax "Expected `)'"))
1250 (math-read-token)
1251 exp))
3cedbf72 1252 ((eq math-exp-token 'string)
5eb3eeda 1253 (require 'calc-ext)
136211a9 1254 (math-read-string))
abd880c3 1255 ((equal math-expr-data "[")
5eb3eeda 1256 (require 'calc-ext)
136211a9 1257 (math-read-brackets t "]"))
abd880c3 1258 ((equal math-expr-data "{")
5eb3eeda 1259 (require 'calc-ext)
136211a9 1260 (math-read-brackets nil "}"))
abd880c3 1261 ((equal math-expr-data "<")
5eb3eeda 1262 (require 'calc-ext)
136211a9 1263 (math-read-angle-brackets))
07ff2bc8 1264 (t (throw 'syntax "Expected a number")))))
136211a9 1265
47233535
JB
1266(provide 'calc-aent)
1267
2378f044 1268;; Local variables:
be19ef0b 1269;; coding: utf-8
2378f044
SM
1270;; generated-autoload-file: "calc-loaddefs.el"
1271;; End:
1272
07ff2bc8 1273;;; calc-aent.el ends here