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