2ae23cd5aa92c315b4a087f360ce6df69a040444
[bpt/emacs.git] / lisp / calc / calc-lang.el
1 ;;; calc-lang.el --- calc language functions
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8
9 ;; This file is part of GNU Emacs.
10
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
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; This file is autoloaded from calc-ext.el.
31
32 (require 'calc-ext)
33 (require 'calc-macs)
34
35
36 ;; Declare functions which are defined elsewhere.
37 (declare-function math-compose-vector "calccomp" (a sep prec))
38 (declare-function math-compose-var "calccomp" (a))
39 (declare-function math-tex-expr-is-flat "calccomp" (a))
40 (declare-function math-read-factor "calc-aent" ())
41 (declare-function math-read-expr-level "calc-aent" (exp-prec &optional exp-term))
42
43 ;; Declare variables which are defined elsewhere.
44 (defvar calc-lang-slash-idiv)
45 (defvar calc-lang-allow-underscores)
46 (defvar math-comp-left-bracket)
47 (defvar math-comp-right-bracket)
48 (defvar math-comp-comma)
49 (defvar math-comp-vector-prec)
50
51 ;;; Alternate entry/display languages.
52
53 (defun calc-set-language (lang &optional option no-refresh)
54 (setq math-expr-opers (or (get lang 'math-oper-table) (math-standard-ops))
55 math-expr-function-mapping (get lang 'math-function-table)
56 math-expr-variable-mapping (get lang 'math-variable-table)
57 calc-language-input-filter (get lang 'math-input-filter)
58 calc-language-output-filter (get lang 'math-output-filter)
59 calc-vector-brackets (or (get lang 'math-vector-brackets) "[]")
60 calc-complex-format (get lang 'math-complex-format)
61 calc-radix-formatter (get lang 'math-radix-formatter)
62 calc-function-open (or (get lang 'math-function-open) "(")
63 calc-function-close (or (get lang 'math-function-close) ")"))
64 (if no-refresh
65 (setq calc-language lang
66 calc-language-option option)
67 (calc-change-mode '(calc-language calc-language-option)
68 (list lang option) t)))
69
70 (defun calc-normal-language ()
71 (interactive)
72 (calc-wrapper
73 (calc-set-language nil)
74 (message "Normal language mode")))
75
76 (defun calc-flat-language ()
77 (interactive)
78 (calc-wrapper
79 (calc-set-language 'flat)
80 (message "Flat language mode (all stack entries shown on one line)")))
81
82 (defun calc-big-language ()
83 (interactive)
84 (calc-wrapper
85 (calc-set-language 'big)
86 (message "\"Big\" language mode")))
87
88 (defun calc-unformatted-language ()
89 (interactive)
90 (calc-wrapper
91 (calc-set-language 'unform)
92 (message "Unformatted language mode")))
93
94
95 (defun calc-c-language ()
96 (interactive)
97 (calc-wrapper
98 (calc-set-language 'c)
99 (message "`C' language mode")))
100
101 (put 'c 'math-oper-table
102 '( ( "u!" calcFunc-lnot -1 1000 )
103 ( "~" calcFunc-not -1 1000 )
104 ( "u+" ident -1 197 )
105 ( "u-" neg -1 197 )
106 ( "*" * 190 191 )
107 ( "/" / 190 191 )
108 ( "%" % 190 191 )
109 ( "+" + 180 181 )
110 ( "-" - 180 181 )
111 ( "<<" calcFunc-lsh 170 171 )
112 ( ">>" calcFunc-rsh 170 171 )
113 ( "<" calcFunc-lt 160 161 )
114 ( ">" calcFunc-gt 160 161 )
115 ( "<=" calcFunc-leq 160 161 )
116 ( ">=" calcFunc-geq 160 161 )
117 ( "==" calcFunc-eq 150 151 )
118 ( "!=" calcFunc-neq 150 151 )
119 ( "&" calcFunc-and 140 141 )
120 ( "^" calcFunc-xor 131 130 )
121 ( "|" calcFunc-or 120 121 )
122 ( "&&" calcFunc-land 110 111 )
123 ( "||" calcFunc-lor 100 101 )
124 ( "?" (math-read-if) 91 90 )
125 ( "!!!" calcFunc-pnot -1 88 )
126 ( "&&&" calcFunc-pand 85 86 )
127 ( "|||" calcFunc-por 75 76 )
128 ( "=" calcFunc-assign 51 50 )
129 ( ":=" calcFunc-assign 51 50 )
130 ( "::" calcFunc-condition 45 46 ))) ; should support full assignments
131
132 (put 'c 'math-function-table
133 '( ( acos . calcFunc-arccos )
134 ( acosh . calcFunc-arccosh )
135 ( asin . calcFunc-arcsin )
136 ( asinh . calcFunc-arcsinh )
137 ( atan . calcFunc-arctan )
138 ( atan2 . calcFunc-arctan2 )
139 ( atanh . calcFunc-arctanh )))
140
141 (put 'c 'math-variable-table
142 '( ( M_PI . var-pi )
143 ( M_E . var-e )))
144
145 (put 'c 'math-vector-brackets "{}")
146
147 (put 'c 'math-radix-formatter
148 (function (lambda (r s)
149 (if (= r 16) (format "0x%s" s)
150 (if (= r 8) (format "0%s" s)
151 (format "%d#%s" r s))))))
152
153 (put 'c 'math-compose-subscr
154 (function
155 (lambda (a)
156 (let ((args (cdr (cdr a))))
157 (list 'horiz
158 (math-compose-expr (nth 1 a) 1000)
159 "["
160 (math-compose-vector args ", " 0)
161 "]")))))
162
163 (add-to-list 'calc-lang-slash-idiv 'c)
164 (add-to-list 'calc-lang-allow-underscores 'c)
165 (add-to-list 'calc-lang-c-type-hex 'c)
166 (add-to-list 'calc-lang-brackets-are-subscripts 'c)
167
168 (defun calc-pascal-language (n)
169 (interactive "P")
170 (calc-wrapper
171 (and n (setq n (prefix-numeric-value n)))
172 (calc-set-language 'pascal n)
173 (message (if (and n (/= n 0))
174 (if (> n 0)
175 "Pascal language mode (all uppercase)"
176 "Pascal language mode (all lowercase)")
177 "Pascal language mode"))))
178
179 (put 'pascal 'math-oper-table
180 '( ( "not" calcFunc-lnot -1 1000 )
181 ( "*" * 190 191 )
182 ( "/" / 190 191 )
183 ( "and" calcFunc-and 190 191 )
184 ( "div" calcFunc-idiv 190 191 )
185 ( "mod" % 190 191 )
186 ( "u+" ident -1 185 )
187 ( "u-" neg -1 185 )
188 ( "+" + 180 181 )
189 ( "-" - 180 181 )
190 ( "or" calcFunc-or 180 181 )
191 ( "xor" calcFunc-xor 180 181 )
192 ( "shl" calcFunc-lsh 180 181 )
193 ( "shr" calcFunc-rsh 180 181 )
194 ( "in" calcFunc-in 160 161 )
195 ( "<" calcFunc-lt 160 161 )
196 ( ">" calcFunc-gt 160 161 )
197 ( "<=" calcFunc-leq 160 161 )
198 ( ">=" calcFunc-geq 160 161 )
199 ( "=" calcFunc-eq 160 161 )
200 ( "<>" calcFunc-neq 160 161 )
201 ( "!!!" calcFunc-pnot -1 85 )
202 ( "&&&" calcFunc-pand 80 81 )
203 ( "|||" calcFunc-por 75 76 )
204 ( ":=" calcFunc-assign 51 50 )
205 ( "::" calcFunc-condition 45 46 )))
206
207 (put 'pascal 'math-input-filter 'calc-input-case-filter)
208 (put 'pascal 'math-output-filter 'calc-output-case-filter)
209
210 (put 'pascal 'math-radix-formatter
211 (function (lambda (r s)
212 (if (= r 16) (format "$%s" s)
213 (format "%d#%s" r s)))))
214
215 (put 'pascal 'math-lang-read-symbol
216 '((?\$
217 (eq (string-match
218 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
219 math-exp-str math-exp-pos)
220 math-exp-pos)
221 (setq math-exp-token 'number
222 math-expr-data (math-match-substring math-exp-str 1)
223 math-exp-pos (match-end 1)))))
224
225 (put 'pascal 'math-compose-subscr
226 (function
227 (lambda (a)
228 (let ((args (cdr (cdr a))))
229 (while (eq (car-safe (nth 1 a)) 'calcFunc-subscr)
230 (setq args (append (cdr (cdr (nth 1 a))) args)
231 a (nth 1 a)))
232 (list 'horiz
233 (math-compose-expr (nth 1 a) 1000)
234 "["
235 (math-compose-vector args ", " 0)
236 "]")))))
237
238 (add-to-list 'calc-lang-allow-underscores 'pascal)
239 (add-to-list 'calc-lang-brackets-are-subscripts 'pascal)
240
241 (defun calc-input-case-filter (str)
242 (cond ((or (null calc-language-option) (= calc-language-option 0))
243 str)
244 (t
245 (downcase str))))
246
247 (defun calc-output-case-filter (str)
248 (cond ((or (null calc-language-option) (= calc-language-option 0))
249 str)
250 ((> calc-language-option 0)
251 (upcase str))
252 (t
253 (downcase str))))
254
255
256 (defun calc-fortran-language (n)
257 (interactive "P")
258 (calc-wrapper
259 (and n (setq n (prefix-numeric-value n)))
260 (calc-set-language 'fortran n)
261 (message (if (and n (/= n 0))
262 (if (> n 0)
263 "FORTRAN language mode (all uppercase)"
264 "FORTRAN language mode (all lowercase)")
265 "FORTRAN language mode"))))
266
267 (put 'fortran 'math-oper-table
268 '( ( "u/" (math-parse-fortran-vector) -1 1 )
269 ( "/" (math-parse-fortran-vector-end) 1 -1 )
270 ( "**" ^ 201 200 )
271 ( "u+" ident -1 191 )
272 ( "u-" neg -1 191 )
273 ( "*" * 190 191 )
274 ( "/" / 190 191 )
275 ( "+" + 180 181 )
276 ( "-" - 180 181 )
277 ( ".LT." calcFunc-lt 160 161 )
278 ( ".GT." calcFunc-gt 160 161 )
279 ( ".LE." calcFunc-leq 160 161 )
280 ( ".GE." calcFunc-geq 160 161 )
281 ( ".EQ." calcFunc-eq 160 161 )
282 ( ".NE." calcFunc-neq 160 161 )
283 ( ".NOT." calcFunc-lnot -1 121 )
284 ( ".AND." calcFunc-land 110 111 )
285 ( ".OR." calcFunc-lor 100 101 )
286 ( "!!!" calcFunc-pnot -1 85 )
287 ( "&&&" calcFunc-pand 80 81 )
288 ( "|||" calcFunc-por 75 76 )
289 ( "=" calcFunc-assign 51 50 )
290 ( ":=" calcFunc-assign 51 50 )
291 ( "::" calcFunc-condition 45 46 )))
292
293 (put 'fortran 'math-vector-brackets "//")
294
295 (put 'fortran 'math-function-table
296 '( ( acos . calcFunc-arccos )
297 ( acosh . calcFunc-arccosh )
298 ( aimag . calcFunc-im )
299 ( aint . calcFunc-ftrunc )
300 ( asin . calcFunc-arcsin )
301 ( asinh . calcFunc-arcsinh )
302 ( atan . calcFunc-arctan )
303 ( atan2 . calcFunc-arctan2 )
304 ( atanh . calcFunc-arctanh )
305 ( conjg . calcFunc-conj )
306 ( log . calcFunc-ln )
307 ( nint . calcFunc-round )
308 ( real . calcFunc-re )))
309
310 (put 'fortran 'math-input-filter 'calc-input-case-filter)
311
312 (put 'fortran 'math-output-filter 'calc-output-case-filter)
313
314 (put 'fortran 'math-lang-read-symbol
315 '((?\.
316 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
317 math-exp-str math-exp-pos) math-exp-pos)
318 (setq math-exp-token 'punc
319 math-expr-data (upcase (math-match-substring math-exp-str 0))
320 math-exp-pos (match-end 0)))))
321
322 (put 'fortran 'math-compose-subscr
323 (function
324 (lambda (a)
325 (let ((args (cdr (cdr a))))
326 (while (eq (car-safe (nth 1 a)) 'calcFunc-subscr)
327 (setq args (append (cdr (cdr (nth 1 a))) args)
328 a (nth 1 a)))
329 (list 'horiz
330 (math-compose-expr (nth 1 a) 1000)
331 "("
332 (math-compose-vector args ", " 0)
333 ")")))))
334
335 (add-to-list 'calc-lang-slash-idiv 'fortran)
336 (add-to-list 'calc-lang-allow-underscores 'fortran)
337 (add-to-list 'calc-lang-parens-are-subscripts 'fortran)
338
339 ;; The next few variables are local to math-read-exprs in calc-aent.el
340 ;; and math-read-expr in calc-ext.el, but are set in functions they call.
341
342 (defvar math-exp-token)
343 (defvar math-expr-data)
344 (defvar math-exp-old-pos)
345
346 (defvar math-parsing-fortran-vector nil)
347 (defun math-parse-fortran-vector (op)
348 (let ((math-parsing-fortran-vector '(end . "\000")))
349 (prog1
350 (math-read-brackets t "]")
351 (setq math-exp-token (car math-parsing-fortran-vector)
352 math-expr-data (cdr math-parsing-fortran-vector)))))
353
354 (defun math-parse-fortran-vector-end (x op)
355 (if math-parsing-fortran-vector
356 (progn
357 (setq math-parsing-fortran-vector (cons math-exp-token math-expr-data)
358 math-exp-token 'end
359 math-expr-data "\000")
360 x)
361 (throw 'syntax "Unmatched closing `/'")))
362
363 (defun math-parse-fortran-subscr (sym args)
364 (setq sym (math-build-var-name sym))
365 (while args
366 (setq sym (list 'calcFunc-subscr sym (car args))
367 args (cdr args)))
368 sym)
369
370
371 (defun calc-tex-language (n)
372 (interactive "P")
373 (calc-wrapper
374 (and n (setq n (prefix-numeric-value n)))
375 (calc-set-language 'tex n)
376 (cond ((not n)
377 (message "TeX language mode"))
378 ((= n 0)
379 (message "TeX language mode with multiline matrices"))
380 ((= n 1)
381 (message "TeX language mode with \\hbox{func}(\\hbox{var})"))
382 ((> n 1)
383 (message
384 "TeX language mode with \\hbox{func}(\\hbox{var}) and multiline matrices"))
385 ((= n -1)
386 (message "TeX language mode with \\func(\\hbox{var})"))
387 ((< n -1)
388 (message
389 "TeX language mode with \\func(\\hbox{var}) and multiline matrices")))))
390
391 (defun calc-latex-language (n)
392 (interactive "P")
393 (calc-wrapper
394 (and n (setq n (prefix-numeric-value n)))
395 (calc-set-language 'latex n)
396 (cond ((not n)
397 (message "LaTeX language mode"))
398 ((= n 0)
399 (message "LaTeX language mode with multiline matrices"))
400 ((= n 1)
401 (message "LaTeX language mode with \\text{func}(\\text{var})"))
402 ((> n 1)
403 (message
404 "LaTeX language mode with \\text{func}(\\text{var}) and multiline matrices"))
405 ((= n -1)
406 (message "LaTeX language mode with \\func(\\text{var})"))
407 ((< n -1)
408 (message
409 "LaTeX language mode with \\func(\\text{var}) and multiline matrices")))))
410
411 (put 'tex 'math-oper-table
412 '( ( "\\hat" calcFunc-hat -1 950 )
413 ( "\\check" calcFunc-check -1 950 )
414 ( "\\tilde" calcFunc-tilde -1 950 )
415 ( "\\acute" calcFunc-acute -1 950 )
416 ( "\\grave" calcFunc-grave -1 950 )
417 ( "\\dot" calcFunc-dot -1 950 )
418 ( "\\ddot" calcFunc-dotdot -1 950 )
419 ( "\\breve" calcFunc-breve -1 950 )
420 ( "\\bar" calcFunc-bar -1 950 )
421 ( "\\vec" calcFunc-Vec -1 950 )
422 ( "\\underline" calcFunc-under -1 950 )
423 ( "u|" calcFunc-abs -1 0 )
424 ( "|" closing 0 -1 )
425 ( "\\lfloor" calcFunc-floor -1 0 )
426 ( "\\rfloor" closing 0 -1 )
427 ( "\\lceil" calcFunc-ceil -1 0 )
428 ( "\\rceil" closing 0 -1 )
429 ( "\\pm" sdev 300 300 )
430 ( "!" calcFunc-fact 210 -1 )
431 ( "^" ^ 201 200 )
432 ( "_" calcFunc-subscr 201 200 )
433 ( "u+" ident -1 197 )
434 ( "u-" neg -1 197 )
435 ( "\\times" * 191 190 )
436 ( "*" * 191 190 )
437 ( "2x" * 191 190 )
438 ( "+" + 180 181 )
439 ( "-" - 180 181 )
440 ( "\\over" / 170 171 )
441 ( "/" / 170 171 )
442 ( "\\choose" calcFunc-choose 170 171 )
443 ( "\\mod" % 170 171 )
444 ( "<" calcFunc-lt 160 161 )
445 ( ">" calcFunc-gt 160 161 )
446 ( "\\leq" calcFunc-leq 160 161 )
447 ( "\\geq" calcFunc-geq 160 161 )
448 ( "=" calcFunc-eq 160 161 )
449 ( "\\neq" calcFunc-neq 160 161 )
450 ( "\\ne" calcFunc-neq 160 161 )
451 ( "\\lnot" calcFunc-lnot -1 121 )
452 ( "\\land" calcFunc-land 110 111 )
453 ( "\\lor" calcFunc-lor 100 101 )
454 ( "?" (math-read-if) 91 90 )
455 ( "!!!" calcFunc-pnot -1 85 )
456 ( "&&&" calcFunc-pand 80 81 )
457 ( "|||" calcFunc-por 75 76 )
458 ( "\\gets" calcFunc-assign 51 50 )
459 ( ":=" calcFunc-assign 51 50 )
460 ( "::" calcFunc-condition 45 46 )
461 ( "\\to" calcFunc-evalto 40 41 )
462 ( "\\to" calcFunc-evalto 40 -1 )
463 ( "=>" calcFunc-evalto 40 41 )
464 ( "=>" calcFunc-evalto 40 -1 )))
465
466 (put 'tex 'math-function-table
467 '( ( \\arccos . calcFunc-arccos )
468 ( \\arcsin . calcFunc-arcsin )
469 ( \\arctan . calcFunc-arctan )
470 ( \\arg . calcFunc-arg )
471 ( \\cos . calcFunc-cos )
472 ( \\cosh . calcFunc-cosh )
473 ( \\cot . calcFunc-cot )
474 ( \\coth . calcFunc-coth )
475 ( \\csc . calcFunc-csc )
476 ( \\det . calcFunc-det )
477 ( \\exp . calcFunc-exp )
478 ( \\gcd . calcFunc-gcd )
479 ( \\ln . calcFunc-ln )
480 ( \\log . calcFunc-log10 )
481 ( \\max . calcFunc-max )
482 ( \\min . calcFunc-min )
483 ( \\sec . calcFunc-sec )
484 ( \\sin . calcFunc-sin )
485 ( \\sinh . calcFunc-sinh )
486 ( \\sqrt . calcFunc-sqrt )
487 ( \\tan . calcFunc-tan )
488 ( \\tanh . calcFunc-tanh )
489 ( \\phi . calcFunc-totient )
490 ( \\mu . calcFunc-moebius )))
491
492 (put 'tex 'math-special-function-table
493 '((calcFunc-sum . (math-compose-tex-sum "\\sum"))
494 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
495 (intv . math-compose-tex-intv)))
496
497 (put 'tex 'math-variable-table
498 '(
499 ;; The Greek letters
500 ( \\alpha . var-alpha )
501 ( \\beta . var-beta )
502 ( \\gamma . var-gamma )
503 ( \\Gamma . var-Gamma )
504 ( \\delta . var-delta )
505 ( \\Delta . var-Delta )
506 ( \\epsilon . var-epsilon )
507 ( \\varepsilon . var-varepsilon)
508 ( \\zeta . var-zeta )
509 ( \\eta . var-eta )
510 ( \\theta . var-theta )
511 ( \\vartheta . var-vartheta )
512 ( \\Theta . var-Theta )
513 ( \\iota . var-iota )
514 ( \\kappa . var-kappa )
515 ( \\lambda . var-lambda )
516 ( \\Lambda . var-Lambda )
517 ( \\mu . var-mu )
518 ( \\nu . var-nu )
519 ( \\xi . var-xi )
520 ( \\Xi . var-Xi )
521 ( \\pi . var-pi )
522 ( \\varpi . var-varpi )
523 ( \\Pi . var-Pi )
524 ( \\rho . var-rho )
525 ( \\varrho . var-varrho )
526 ( \\sigma . var-sigma )
527 ( \\sigma . var-varsigma )
528 ( \\Sigma . var-Sigma )
529 ( \\tau . var-tau )
530 ( \\upsilon . var-upsilon )
531 ( \\Upsilon . var-Upsilon )
532 ( \\phi . var-phi )
533 ( \\varphi . var-varphi )
534 ( \\Phi . var-Phi )
535 ( \\chi . var-chi )
536 ( \\psi . var-psi )
537 ( \\Psi . var-Psi )
538 ( \\omega . var-omega )
539 ( \\Omega . var-Omega )
540 ;; Others
541 ( \\ell . var-ell )
542 ( \\infty . var-inf )
543 ( \\infty . var-uinf )
544 ( \\sum . (math-parse-tex-sum calcFunc-sum) )
545 ( \\prod . (math-parse-tex-sum calcFunc-prod) )))
546
547 (put 'tex 'math-punc-table
548 '((?\{ . ?\()
549 (?\} . ?\))
550 (?\& . ?\,)))
551
552 (put 'tex 'math-complex-format 'i)
553
554 (put 'tex 'math-input-filter 'math-tex-input-filter)
555
556 (put 'tex 'math-matrix-formatter
557 (function
558 (lambda (a)
559 (if (and (integerp calc-language-option)
560 (or (= calc-language-option 0)
561 (> calc-language-option 1)
562 (< calc-language-option -1)))
563 (append '(vleft 0 "\\matrix{")
564 (math-compose-tex-matrix (cdr a))
565 '("}"))
566 (append '(horiz "\\matrix{ ")
567 (math-compose-tex-matrix (cdr a))
568 '(" }"))))))
569
570 (put 'tex 'math-var-formatter 'math-compose-tex-var)
571
572 (put 'tex 'math-func-formatter 'math-compose-tex-func)
573
574 (put 'tex 'math-dots "\\ldots")
575
576 (put 'tex 'math-big-parens '("\\left( " . " \\right)"))
577
578 (put 'tex 'math-evalto '("\\evalto " . " \\to "))
579
580 (defconst math-tex-ignore-words
581 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right")
582 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ")
583 ("\\quad") ("\\qquad") ("\\hfil") ("\\hfill")
584 ("\\displaystyle") ("\\textstyle") ("\\dsize") ("\\tsize")
585 ("\\scriptstyle") ("\\scriptscriptstyle") ("\\ssize") ("\\sssize")
586 ("\\rm") ("\\bf") ("\\it") ("\\sl")
587 ("\\roman") ("\\bold") ("\\italic") ("\\slanted")
588 ("\\cal") ("\\mit") ("\\Cal") ("\\Bbb") ("\\frak") ("\\goth")
589 ("\\evalto")
590 ("\\matrix" mat) ("\\bmatrix" mat) ("\\pmatrix" mat)
591 ("\\begin" begenv)
592 ("\\cr" punc ";") ("\\\\" punc ";") ("\\*" punc "*")
593 ("\\{" punc "[") ("\\}" punc "]")))
594
595 (defconst math-latex-ignore-words
596 (append math-tex-ignore-words
597 '(("\\begin" begenv))))
598
599 (put 'tex 'math-lang-read-symbol
600 '((?\\
601 (< math-exp-pos (1- (length math-exp-str)))
602 (progn
603 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
604 math-exp-str math-exp-pos)
605 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
606 math-exp-str math-exp-pos))
607 (setq math-exp-token 'symbol
608 math-exp-pos (match-end 0)
609 math-expr-data (math-restore-dashes
610 (math-match-substring math-exp-str 1)))
611 (let ((code (assoc math-expr-data math-latex-ignore-words)))
612 (cond ((null code))
613 ((null (cdr code))
614 (math-read-token))
615 ((eq (nth 1 code) 'punc)
616 (setq math-exp-token 'punc
617 math-expr-data (nth 2 code)))
618 ((and (eq (nth 1 code) 'mat)
619 (string-match " *{" math-exp-str math-exp-pos))
620 (setq math-exp-pos (match-end 0)
621 math-exp-token 'punc
622 math-expr-data "[")
623 (let ((right (string-match "}" math-exp-str math-exp-pos)))
624 (and right
625 (setq math-exp-str (copy-sequence math-exp-str))
626 (aset math-exp-str right ?\]))))))))))
627
628 (defun math-compose-tex-matrix (a &optional ltx)
629 (if (cdr a)
630 (cons (append (math-compose-vector (cdr (car a)) " & " 0)
631 (if ltx '(" \\\\ ") '(" \\cr ")))
632 (math-compose-tex-matrix (cdr a) ltx))
633 (list (math-compose-vector (cdr (car a)) " & " 0))))
634
635 (defun math-compose-tex-sum (a fn)
636 (cond
637 ((nth 4 a)
638 (list 'horiz (nth 1 fn)
639 "_{" (math-compose-expr (nth 2 a) 0)
640 "=" (math-compose-expr (nth 3 a) 0)
641 "}^{" (math-compose-expr (nth 4 a) 0)
642 "}{" (math-compose-expr (nth 1 a) 0) "}"))
643 ((nth 3 a)
644 (list 'horiz (nth 1 fn)
645 "_{" (math-compose-expr (nth 2 a) 0)
646 "=" (math-compose-expr (nth 3 a) 0)
647 "}{" (math-compose-expr (nth 1 a) 0) "}"))
648 (t
649 (list 'horiz (nth 1 fn)
650 "_{" (math-compose-expr (nth 2 a) 0)
651 "}{" (math-compose-expr (nth 1 a) 0) "}"))))
652
653 (defun math-parse-tex-sum (f val)
654 (let (low high save)
655 (or (equal math-expr-data "_") (throw 'syntax "Expected `_'"))
656 (math-read-token)
657 (setq save math-exp-old-pos)
658 (setq low (math-read-factor))
659 (or (eq (car-safe low) 'calcFunc-eq)
660 (progn
661 (setq math-exp-old-pos (1+ save))
662 (throw 'syntax "Expected equation")))
663 (or (equal math-expr-data "^") (throw 'syntax "Expected `^'"))
664 (math-read-token)
665 (setq high (math-read-factor))
666 (list (nth 2 f) (math-read-factor) (nth 1 low) (nth 2 low) high)))
667
668 (defun math-tex-input-filter (str) ; allow parsing of 123\,456\,789.
669 (while (string-match "[0-9]\\\\,[0-9]" str)
670 (setq str (concat (substring str 0 (1+ (match-beginning 0)))
671 (substring str (1- (match-end 0))))))
672 str)
673
674 ;(defun math-tex-print-sqrt (a)
675 ; (list 'horiz
676 ; "\\sqrt{"
677 ; (math-compose-expr (nth 1 a) 0)
678 ; "}"))
679
680 (defun math-compose-tex-intv (a)
681 (list 'horiz
682 (if (memq (nth 1 a) '(0 1)) "(" "[")
683 (math-compose-expr (nth 2 a) 0)
684 " \\ldots "
685 (math-compose-expr (nth 3 a) 0)
686 (if (memq (nth 1 a) '(0 2)) ")" "]")))
687
688 (defun math-compose-tex-var (a prec)
689 (if (and calc-language-option
690 (not (= calc-language-option 0))
691 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
692 (symbol-name (nth 1 a))))
693 (if (eq calc-language 'latex)
694 (format "\\text{%s}" (symbol-name (nth 1 a)))
695 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
696 (math-compose-var a)))
697
698 (defun math-compose-tex-func (func a)
699 (let (left right)
700 (if (and calc-language-option
701 (not (= calc-language-option 0))
702 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
703 (if (< (prefix-numeric-value calc-language-option) 0)
704 (setq func (format "\\%s" func))
705 (setq func (if (eq calc-language 'latex)
706 (format "\\text{%s}" func)
707 (format "\\hbox{%s}" func)))))
708 (cond ((or (> (length a) 2)
709 (not (math-tex-expr-is-flat (nth 1 a))))
710 (setq left "\\left( "
711 right " \\right)"))
712 ((and (eq (aref func 0) ?\\)
713 (not (or
714 (string-match "\\hbox{" func)
715 (string-match "\\text{" func)))
716 (= (length a) 2)
717 (or (Math-realp (nth 1 a))
718 (memq (car (nth 1 a)) '(var *))))
719 (setq left "{" right "}"))
720 (t (setq left calc-function-open
721 right calc-function-close)))
722 (list 'horiz func
723 left
724 (math-compose-vector (cdr a) ", " 0)
725 right)))
726
727 (put 'latex 'math-oper-table
728 (append (get 'tex 'math-oper-table)
729 '(( "\\Hat" calcFunc-Hat -1 950 )
730 ( "\\Check" calcFunc-Check -1 950 )
731 ( "\\Tilde" calcFunc-Tilde -1 950 )
732 ( "\\Acute" calcFunc-Acute -1 950 )
733 ( "\\Grave" calcFunc-Grave -1 950 )
734 ( "\\Dot" calcFunc-Dot -1 950 )
735 ( "\\Ddot" calcFunc-Dotdot -1 950 )
736 ( "\\Breve" calcFunc-Breve -1 950 )
737 ( "\\Bar" calcFunc-Bar -1 950 )
738 ( "\\Vec" calcFunc-VEC -1 950 )
739 ( "\\dddot" calcFunc-dddot -1 950 )
740 ( "\\ddddot" calcFunc-ddddot -1 950 )
741 ( "\\div" / 170 171 )
742 ( "\\le" calcFunc-leq 160 161 )
743 ( "\\leqq" calcFunc-leq 160 161 )
744 ( "\\leqsland" calcFunc-leq 160 161 )
745 ( "\\ge" calcFunc-geq 160 161 )
746 ( "\\geqq" calcFunc-geq 160 161 )
747 ( "\\geqslant" calcFunc-geq 160 161 )
748 ( "=" calcFunc-eq 160 161 )
749 ( "\\neq" calcFunc-neq 160 161 )
750 ( "\\ne" calcFunc-neq 160 161 )
751 ( "\\lnot" calcFunc-lnot -1 121 )
752 ( "\\land" calcFunc-land 110 111 )
753 ( "\\lor" calcFunc-lor 100 101 )
754 ( "?" (math-read-if) 91 90 )
755 ( "!!!" calcFunc-pnot -1 85 )
756 ( "&&&" calcFunc-pand 80 81 )
757 ( "|||" calcFunc-por 75 76 )
758 ( "\\gets" calcFunc-assign 51 50 )
759 ( ":=" calcFunc-assign 51 50 )
760 ( "::" calcFunc-condition 45 46 )
761 ( "\\to" calcFunc-evalto 40 41 )
762 ( "\\to" calcFunc-evalto 40 -1 )
763 ( "=>" calcFunc-evalto 40 41 )
764 ( "=>" calcFunc-evalto 40 -1 ))))
765
766 (put 'latex 'math-function-table
767 (append
768 (get 'tex 'math-function-table)
769 '(( \\frac . (math-latex-parse-frac))
770 ( \\tfrac . (math-latex-parse-frac))
771 ( \\dfrac . (math-latex-parse-frac))
772 ( \\binom . (math-latex-parse-two-args calcFunc-choose))
773 ( \\tbinom . (math-latex-parse-two-args calcFunc-choose))
774 ( \\dbinom . (math-latex-parse-two-args calcFunc-choose))
775 ( \\phi . calcFunc-totient )
776 ( \\mu . calcFunc-moebius ))))
777
778 (put 'latex 'math-special-function-table
779 '((/ . (math-compose-latex-frac "\\frac"))
780 (calcFunc-choose . (math-compose-latex-frac "\\binom"))
781 (calcFunc-sum . (math-compose-tex-sum "\\sum"))
782 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
783 (intv . math-compose-tex-intv)))
784
785 (put 'latex 'math-variable-table
786 (get 'tex 'math-variable-table))
787
788 (put 'latex 'math-punc-table
789 '((?\{ . ?\()
790 (?\} . ?\))
791 (?\& . ?\,)))
792
793 (put 'latex 'math-complex-format 'i)
794
795 (put 'latex 'math-matrix-formatter
796 (function
797 (lambda (a)
798 (if (and (integerp calc-language-option)
799 (or (= calc-language-option 0)
800 (> calc-language-option 1)
801 (< calc-language-option -1)))
802 (append '(vleft 0 "\\begin{pmatrix}")
803 (math-compose-tex-matrix (cdr a) t)
804 '("\\end{pmatrix}"))
805 (append '(horiz "\\begin{pmatrix} ")
806 (math-compose-tex-matrix (cdr a) t)
807 '(" \\end{pmatrix}"))))))
808
809 (put 'latex 'math-var-formatter 'math-compose-tex-var)
810
811 (put 'latex 'math-func-formatter 'math-compose-tex-func)
812
813 (put 'latex 'math-dots "\\ldots")
814
815 (put 'latex 'math-big-parens '("\\left( " . " \\right)"))
816
817 (put 'latex 'math-evalto '("\\evalto " . " \\to "))
818
819 (put 'latex 'math-lang-read-symbol
820 '((?\\
821 (< math-exp-pos (1- (length math-exp-str)))
822 (progn
823 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
824 math-exp-str math-exp-pos)
825 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
826 math-exp-str math-exp-pos)
827 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
828 math-exp-str math-exp-pos))
829 (setq math-exp-token 'symbol
830 math-exp-pos (match-end 0)
831 math-expr-data (math-restore-dashes
832 (math-match-substring math-exp-str 1)))
833 (let ((code (assoc math-expr-data math-tex-ignore-words))
834 envname)
835 (cond ((null code))
836 ((null (cdr code))
837 (math-read-token))
838 ((eq (nth 1 code) 'punc)
839 (setq math-exp-token 'punc
840 math-expr-data (nth 2 code)))
841 ((and (eq (nth 1 code) 'begenv)
842 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
843 (setq math-exp-pos (match-end 0)
844 envname (match-string 1 math-exp-str)
845 math-exp-token 'punc
846 math-expr-data "[")
847 (cond ((or (string= envname "matrix")
848 (string= envname "bmatrix")
849 (string= envname "smallmatrix")
850 (string= envname "pmatrix"))
851 (if (string-match (concat "\\\\end{" envname "}")
852 math-exp-str math-exp-pos)
853 (setq math-exp-str
854 (replace-match "]" t t math-exp-str))
855 (error "%s" (concat "No closing \\end{" envname "}"))))))
856 ((and (eq (nth 1 code) 'mat)
857 (string-match " *{" math-exp-str math-exp-pos))
858 (setq math-exp-pos (match-end 0)
859 math-exp-token 'punc
860 math-expr-data "[")
861 (let ((right (string-match "}" math-exp-str math-exp-pos)))
862 (and right
863 (setq math-exp-str (copy-sequence math-exp-str))
864 (aset math-exp-str right ?\]))))))))))
865
866 (defun math-latex-parse-frac (f val)
867 (let (numer denom)
868 (setq numer (car (math-read-expr-list)))
869 (math-read-token)
870 (setq denom (math-read-factor))
871 (if (and (Math-num-integerp numer)
872 (Math-num-integerp denom))
873 (list 'frac numer denom)
874 (list '/ numer denom))))
875
876 (defun math-latex-parse-two-args (f val)
877 (let (first second)
878 (setq first (car (math-read-expr-list)))
879 (math-read-token)
880 (setq second (math-read-factor))
881 (list (nth 2 f) first second)))
882
883 (defun math-compose-latex-frac (a fn)
884 (list 'horiz (nth 1 fn) "{" (math-compose-expr (nth 1 a) -1)
885 "}{"
886 (math-compose-expr (nth 2 a) -1)
887 "}"))
888
889 (put 'latex 'math-input-filter 'math-tex-input-filter)
890
891 (defun calc-eqn-language (n)
892 (interactive "P")
893 (calc-wrapper
894 (calc-set-language 'eqn)
895 (message "Eqn language mode")))
896
897 (put 'eqn 'math-oper-table
898 '( ( "prime" (math-parse-eqn-prime) 950 -1 )
899 ( "prime" calcFunc-Prime 950 -1 )
900 ( "dot" calcFunc-dot 950 -1 )
901 ( "dotdot" calcFunc-dotdot 950 -1 )
902 ( "hat" calcFunc-hat 950 -1 )
903 ( "tilde" calcFunc-tilde 950 -1 )
904 ( "vec" calcFunc-Vec 950 -1 )
905 ( "dyad" calcFunc-dyad 950 -1 )
906 ( "bar" calcFunc-bar 950 -1 )
907 ( "under" calcFunc-under 950 -1 )
908 ( "sub" calcFunc-subscr 931 930 )
909 ( "sup" ^ 921 920 )
910 ( "sqrt" calcFunc-sqrt -1 910 )
911 ( "over" / 900 901 )
912 ( "u|" calcFunc-abs -1 0 )
913 ( "|" closing 0 -1 )
914 ( "left floor" calcFunc-floor -1 0 )
915 ( "right floor" closing 0 -1 )
916 ( "left ceil" calcFunc-ceil -1 0 )
917 ( "right ceil" closing 0 -1 )
918 ( "+-" sdev 300 300 )
919 ( "!" calcFunc-fact 210 -1 )
920 ( "u+" ident -1 197 )
921 ( "u-" neg -1 197 )
922 ( "times" * 191 190 )
923 ( "*" * 191 190 )
924 ( "2x" * 191 190 )
925 ( "/" / 180 181 )
926 ( "%" % 180 181 )
927 ( "+" + 170 171 )
928 ( "-" - 170 171 )
929 ( "<" calcFunc-lt 160 161 )
930 ( ">" calcFunc-gt 160 161 )
931 ( "<=" calcFunc-leq 160 161 )
932 ( ">=" calcFunc-geq 160 161 )
933 ( "=" calcFunc-eq 160 161 )
934 ( "==" calcFunc-eq 160 161 )
935 ( "!=" calcFunc-neq 160 161 )
936 ( "u!" calcFunc-lnot -1 121 )
937 ( "&&" calcFunc-land 110 111 )
938 ( "||" calcFunc-lor 100 101 )
939 ( "?" (math-read-if) 91 90 )
940 ( "!!!" calcFunc-pnot -1 85 )
941 ( "&&&" calcFunc-pand 80 81 )
942 ( "|||" calcFunc-por 75 76 )
943 ( "<-" calcFunc-assign 51 50 )
944 ( ":=" calcFunc-assign 51 50 )
945 ( "::" calcFunc-condition 45 46 )
946 ( "->" calcFunc-evalto 40 41 )
947 ( "->" calcFunc-evalto 40 -1 )
948 ( "=>" calcFunc-evalto 40 41 )
949 ( "=>" calcFunc-evalto 40 -1 )))
950
951 (put 'eqn 'math-function-table
952 '( ( arc\ cos . calcFunc-arccos )
953 ( arc\ cosh . calcFunc-arccosh )
954 ( arc\ sin . calcFunc-arcsin )
955 ( arc\ sinh . calcFunc-arcsinh )
956 ( arc\ tan . calcFunc-arctan )
957 ( arc\ tanh . calcFunc-arctanh )
958 ( GAMMA . calcFunc-gamma )
959 ( phi . calcFunc-totient )
960 ( mu . calcFunc-moebius )
961 ( matrix . (math-parse-eqn-matrix) )))
962
963 (put 'eqn 'math-special-function-table
964 '((intv . math-compose-eqn-intv)))
965
966 (put 'eqn 'math-punc-table
967 '((?\{ . ?\()
968 (?\} . ?\))))
969
970 (put 'eqn 'math-variable-table
971 '( ( inf . var-uinf )))
972
973 (put 'eqn 'math-complex-format 'i)
974
975 (put 'eqn 'math-big-parens '("{left ( " . " right )}"))
976
977 (put 'eqn 'math-evalto '("evalto " . " -> "))
978
979 (put 'eqn 'math-matrix-formatter
980 (function
981 (lambda (a)
982 (append '(horiz "matrix { ")
983 (math-compose-eqn-matrix
984 (cdr (math-transpose a)))
985 '("}")))))
986
987 (put 'eqn 'math-var-formatter
988 (function
989 (lambda (a prec)
990 (let (v)
991 (if (and math-compose-hash-args
992 (let ((p calc-arg-values))
993 (setq v 1)
994 (while (and p (not (equal (car p) a)))
995 (setq p (and (eq math-compose-hash-args t) (cdr p))
996 v (1+ v)))
997 p))
998 (if (eq math-compose-hash-args 1)
999 "#"
1000 (format "#%d" v))
1001 (if (string-match ".'\\'" (symbol-name (nth 2 a)))
1002 (math-compose-expr
1003 (list 'calcFunc-Prime
1004 (list
1005 'var
1006 (intern (substring (symbol-name (nth 1 a)) 0 -1))
1007 (intern (substring (symbol-name (nth 2 a)) 0 -1))))
1008 prec)
1009 (symbol-name (nth 1 a))))))))
1010
1011 (defconst math-eqn-special-funcs
1012 '( calcFunc-log
1013 calcFunc-ln calcFunc-exp
1014 calcFunc-sin calcFunc-cos calcFunc-tan
1015 calcFunc-sec calcFunc-csc calcFunc-cot
1016 calcFunc-sinh calcFunc-cosh calcFunc-tanh
1017 calcFunc-sech calcFunc-csch calcFunc-coth
1018 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
1019 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh))
1020
1021 (put 'eqn 'math-func-formatter
1022 (function
1023 (lambda (func a)
1024 (let (left right)
1025 (if (string-match "[^']'+\\'" func)
1026 (let ((n (- (length func) (match-beginning 0) 1)))
1027 (setq func (substring func 0 (- n)))
1028 (while (>= (setq n (1- n)) 0)
1029 (setq func (concat func " prime")))))
1030 (cond ((or (> (length a) 2)
1031 (not (math-tex-expr-is-flat (nth 1 a))))
1032 (setq left "{left ( "
1033 right " right )}"))
1034
1035 ((and
1036 (memq (car a) math-eqn-special-funcs)
1037 (= (length a) 2)
1038 (or (Math-realp (nth 1 a))
1039 (memq (car (nth 1 a)) '(var *))))
1040 (setq left "~{" right "}"))
1041 (t
1042 (setq left " ( "
1043 right " )")))
1044 (list 'horiz func left
1045 (math-compose-vector (cdr a) " , " 0)
1046 right)))))
1047
1048 (put 'eqn 'math-lang-read-symbol
1049 '((?\"
1050 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
1051 math-exp-str math-exp-pos)
1052 (progn
1053 (setq math-exp-str (copy-sequence math-exp-str))
1054 (aset math-exp-str (match-beginning 1) ?\{)
1055 (if (< (match-end 1) (length math-exp-str))
1056 (aset math-exp-str (match-end 1) ?\}))
1057 (math-read-token)))))
1058
1059 (defconst math-eqn-ignore-words
1060 '( ("roman") ("bold") ("italic") ("mark") ("lineup") ("evalto")
1061 ("left" ("floor") ("ceil"))
1062 ("right" ("floor") ("ceil"))
1063 ("arc" ("sin") ("cos") ("tan") ("sinh") ("cosh") ("tanh"))
1064 ("size" n) ("font" n) ("fwd" n) ("back" n) ("up" n) ("down" n)
1065 ("above" punc ",")))
1066
1067 (put 'eqn 'math-lang-adjust-words
1068 (function
1069 (lambda ()
1070 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
1071 (cond ((null code))
1072 ((null (cdr code))
1073 (math-read-token))
1074 ((consp (nth 1 code))
1075 (math-read-token)
1076 (if (assoc math-expr-data (cdr code))
1077 (setq math-expr-data (format "%s %s"
1078 (car code) math-expr-data))))
1079 ((eq (nth 1 code) 'punc)
1080 (setq math-exp-token 'punc
1081 math-expr-data (nth 2 code)))
1082 (t
1083 (math-read-token)
1084 (math-read-token)))))))
1085
1086 (put 'eqn 'math-lang-read
1087 '((eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
1088 math-exp-str math-exp-pos)
1089 math-exp-pos)
1090 (progn
1091 (setq math-exp-token 'punc
1092 math-expr-data (math-match-substring math-exp-str 0)
1093 math-exp-pos (match-end 0))
1094 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
1095 math-exp-pos)
1096 (setq math-exp-pos (match-end 0)))
1097 (if (memq (aref math-expr-data 0) '(?~ ?^))
1098 (math-read-token)))))
1099
1100
1101 (defun math-compose-eqn-matrix (a)
1102 (if a
1103 (cons
1104 (cond ((eq calc-matrix-just 'right) "rcol ")
1105 ((eq calc-matrix-just 'center) "ccol ")
1106 (t "lcol "))
1107 (cons
1108 (list 'break math-compose-level)
1109 (cons
1110 "{ "
1111 (cons
1112 (let ((math-compose-level (1+ math-compose-level)))
1113 (math-compose-vector (cdr (car a)) " above " 1000))
1114 (cons
1115 " } "
1116 (math-compose-eqn-matrix (cdr a)))))))
1117 nil))
1118
1119 (defun math-parse-eqn-matrix (f sym)
1120 (let ((vec nil))
1121 (while (assoc math-expr-data '(("ccol") ("lcol") ("rcol")))
1122 (math-read-token)
1123 (or (equal math-expr-data calc-function-open)
1124 (throw 'syntax "Expected `{'"))
1125 (math-read-token)
1126 (setq vec (cons (cons 'vec (math-read-expr-list)) vec))
1127 (or (equal math-expr-data calc-function-close)
1128 (throw 'syntax "Expected `}'"))
1129 (math-read-token))
1130 (or (equal math-expr-data calc-function-close)
1131 (throw 'syntax "Expected `}'"))
1132 (math-read-token)
1133 (math-transpose (cons 'vec (nreverse vec)))))
1134
1135 (defun math-parse-eqn-prime (x sym)
1136 (if (eq (car-safe x) 'var)
1137 (if (equal math-expr-data calc-function-open)
1138 (progn
1139 (math-read-token)
1140 (let ((args (if (or (equal math-expr-data calc-function-close)
1141 (eq math-exp-token 'end))
1142 nil
1143 (math-read-expr-list))))
1144 (if (not (or (equal math-expr-data calc-function-close)
1145 (eq math-exp-token 'end)))
1146 (throw 'syntax "Expected `)'"))
1147 (math-read-token)
1148 (cons (intern (format "calcFunc-%s'" (nth 1 x))) args)))
1149 (list 'var
1150 (intern (concat (symbol-name (nth 1 x)) "'"))
1151 (intern (concat (symbol-name (nth 2 x)) "'"))))
1152 (list 'calcFunc-Prime x)))
1153
1154 (defun math-compose-eqn-intv (a)
1155 (list 'horiz
1156 (if (memq (nth 1 a) '(0 1)) "(" "[")
1157 (math-compose-expr (nth 2 a) 0)
1158 " ... "
1159 (math-compose-expr (nth 3 a) 0)
1160 (if (memq (nth 1 a) '(0 2)) ")" "]")))
1161
1162
1163 (defun calc-mathematica-language ()
1164 (interactive)
1165 (calc-wrapper
1166 (calc-set-language 'math)
1167 (message "Mathematica language mode")))
1168
1169 (put 'math 'math-oper-table
1170 '( ( "[[" (math-read-math-subscr) 250 -1 )
1171 ( "!" calcFunc-fact 210 -1 )
1172 ( "!!" calcFunc-dfact 210 -1 )
1173 ( "^" ^ 201 200 )
1174 ( "u+" ident -1 197 )
1175 ( "u-" neg -1 197 )
1176 ( "/" / 195 196 )
1177 ( "*" * 190 191 )
1178 ( "2x" * 190 191 )
1179 ( "+" + 180 181 )
1180 ( "-" - 180 181 )
1181 ( "<" calcFunc-lt 160 161 )
1182 ( ">" calcFunc-gt 160 161 )
1183 ( "<=" calcFunc-leq 160 161 )
1184 ( ">=" calcFunc-geq 160 161 )
1185 ( "==" calcFunc-eq 150 151 )
1186 ( "!=" calcFunc-neq 150 151 )
1187 ( "u!" calcFunc-lnot -1 121 )
1188 ( "&&" calcFunc-land 110 111 )
1189 ( "||" calcFunc-lor 100 101 )
1190 ( "!!!" calcFunc-pnot -1 85 )
1191 ( "&&&" calcFunc-pand 80 81 )
1192 ( "|||" calcFunc-por 75 76 )
1193 ( ":=" calcFunc-assign 51 50 )
1194 ( "=" calcFunc-assign 51 50 )
1195 ( "->" calcFunc-assign 51 50 )
1196 ( ":>" calcFunc-assign 51 50 )
1197 ( "::" calcFunc-condition 45 46 )
1198 ))
1199
1200 (put 'math 'math-function-table
1201 '( ( Abs . calcFunc-abs )
1202 ( ArcCos . calcFunc-arccos )
1203 ( ArcCosh . calcFunc-arccosh )
1204 ( ArcSin . calcFunc-arcsin )
1205 ( ArcSinh . calcFunc-arcsinh )
1206 ( ArcTan . calcFunc-arctan )
1207 ( ArcTanh . calcFunc-arctanh )
1208 ( Arg . calcFunc-arg )
1209 ( Binomial . calcFunc-choose )
1210 ( Ceiling . calcFunc-ceil )
1211 ( Conjugate . calcFunc-conj )
1212 ( Cos . calcFunc-cos )
1213 ( Cosh . calcFunc-cosh )
1214 ( Cot . calcFunc-cot )
1215 ( Coth . calcFunc-coth )
1216 ( Csc . calcFunc-csc )
1217 ( Csch . calcFunc-csch )
1218 ( D . calcFunc-deriv )
1219 ( Dt . calcFunc-tderiv )
1220 ( Det . calcFunc-det )
1221 ( Exp . calcFunc-exp )
1222 ( EulerPhi . calcFunc-totient )
1223 ( Floor . calcFunc-floor )
1224 ( Gamma . calcFunc-gamma )
1225 ( GCD . calcFunc-gcd )
1226 ( If . calcFunc-if )
1227 ( Im . calcFunc-im )
1228 ( Inverse . calcFunc-inv )
1229 ( Integrate . calcFunc-integ )
1230 ( Join . calcFunc-vconcat )
1231 ( LCM . calcFunc-lcm )
1232 ( Log . calcFunc-ln )
1233 ( Max . calcFunc-max )
1234 ( Min . calcFunc-min )
1235 ( Mod . calcFunc-mod )
1236 ( MoebiusMu . calcFunc-moebius )
1237 ( Random . calcFunc-random )
1238 ( Round . calcFunc-round )
1239 ( Re . calcFunc-re )
1240 ( Sec . calcFunc-sec )
1241 ( Sech . calcFunc-sech )
1242 ( Sign . calcFunc-sign )
1243 ( Sin . calcFunc-sin )
1244 ( Sinh . calcFunc-sinh )
1245 ( Sqrt . calcFunc-sqrt )
1246 ( Tan . calcFunc-tan )
1247 ( Tanh . calcFunc-tanh )
1248 ( Transpose . calcFunc-trn )
1249 ( Length . calcFunc-vlen )
1250 ))
1251
1252 (put 'math 'math-variable-table
1253 '( ( I . var-i )
1254 ( Pi . var-pi )
1255 ( E . var-e )
1256 ( GoldenRatio . var-phi )
1257 ( EulerGamma . var-gamma )
1258 ( Infinity . var-inf )
1259 ( ComplexInfinity . var-uinf )
1260 ( Indeterminate . var-nan )
1261 ))
1262
1263 (put 'math 'math-vector-brackets "{}")
1264 (put 'math 'math-complex-format 'I)
1265 (put 'math 'math-function-open "[")
1266 (put 'math 'math-function-close "]")
1267
1268 (put 'math 'math-radix-formatter
1269 (function (lambda (r s) (format "%d^^%s" r s))))
1270
1271 (put 'math 'math-lang-read
1272 '((eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
1273 math-exp-pos)
1274 (setq math-exp-token 'punc
1275 math-expr-data (math-match-substring math-exp-str 0)
1276 math-exp-pos (match-end 0))))
1277
1278 (put 'math 'math-compose-subscr
1279 (function
1280 (lambda (a)
1281 (list 'horiz
1282 (math-compose-expr (nth 1 a) 1000)
1283 "[["
1284 (math-compose-expr (nth 2 a) 0)
1285 "]]"))))
1286
1287 (defun math-read-math-subscr (x op)
1288 (let ((idx (math-read-expr-level 0)))
1289 (or (and (equal math-expr-data "]")
1290 (progn
1291 (math-read-token)
1292 (equal math-expr-data "]")))
1293 (throw 'syntax "Expected ']]'"))
1294 (math-read-token)
1295 (list 'calcFunc-subscr x idx)))
1296
1297
1298 (defun calc-maple-language ()
1299 (interactive)
1300 (calc-wrapper
1301 (calc-set-language 'maple)
1302 (message "Maple language mode")))
1303
1304 (put 'maple 'math-oper-table
1305 '( ( "matrix" ident -1 300 )
1306 ( "MATRIX" ident -1 300 )
1307 ( "!" calcFunc-fact 210 -1 )
1308 ( "^" ^ 201 200 )
1309 ( "**" ^ 201 200 )
1310 ( "u+" ident -1 197 )
1311 ( "u-" neg -1 197 )
1312 ( "/" / 191 192 )
1313 ( "*" * 191 192 )
1314 ( "intersect" calcFunc-vint 191 192 )
1315 ( "+" + 180 181 )
1316 ( "-" - 180 181 )
1317 ( "union" calcFunc-vunion 180 181 )
1318 ( "minus" calcFunc-vdiff 180 181 )
1319 ( "mod" % 170 170 )
1320 ( ".." (math-read-maple-dots) 165 165 )
1321 ( "\\dots" (math-read-maple-dots) 165 165 )
1322 ( "<" calcFunc-lt 160 160 )
1323 ( ">" calcFunc-gt 160 160 )
1324 ( "<=" calcFunc-leq 160 160 )
1325 ( ">=" calcFunc-geq 160 160 )
1326 ( "=" calcFunc-eq 160 160 )
1327 ( "<>" calcFunc-neq 160 160 )
1328 ( "not" calcFunc-lnot -1 121 )
1329 ( "and" calcFunc-land 110 111 )
1330 ( "or" calcFunc-lor 100 101 )
1331 ( "!!!" calcFunc-pnot -1 85 )
1332 ( "&&&" calcFunc-pand 80 81 )
1333 ( "|||" calcFunc-por 75 76 )
1334 ( ":=" calcFunc-assign 51 50 )
1335 ( "::" calcFunc-condition 45 46 )
1336 ))
1337
1338 (put 'maple 'math-function-table
1339 '( ( bernoulli . calcFunc-bern )
1340 ( binomial . calcFunc-choose )
1341 ( diff . calcFunc-deriv )
1342 ( GAMMA . calcFunc-gamma )
1343 ( ifactor . calcFunc-prfac )
1344 ( igcd . calcFunc-gcd )
1345 ( ilcm . calcFunc-lcm )
1346 ( int . calcFunc-integ )
1347 ( modp . % )
1348 ( irem . % )
1349 ( iquo . calcFunc-idiv )
1350 ( isprime . calcFunc-prime )
1351 ( length . calcFunc-vlen )
1352 ( member . calcFunc-in )
1353 ( crossprod . calcFunc-cross )
1354 ( inverse . calcFunc-inv )
1355 ( trace . calcFunc-tr )
1356 ( transpose . calcFunc-trn )
1357 ( vectdim . calcFunc-vlen )
1358 ))
1359
1360 (put 'maple 'math-special-function-table
1361 '((intv . math-compose-maple-intv)))
1362
1363 (put 'maple 'math-variable-table
1364 '( ( I . var-i )
1365 ( Pi . var-pi )
1366 ( E . var-e )
1367 ( infinity . var-inf )
1368 ( infinity . var-uinf )
1369 ( infinity . var-nan )
1370 ))
1371
1372 (put 'maple 'math-complex-format 'I)
1373
1374 (put 'maple 'math-matrix-formatter
1375 (function
1376 (lambda (a)
1377 (list 'horiz
1378 "matrix("
1379 math-comp-left-bracket
1380 (math-compose-vector (cdr a)
1381 (concat math-comp-comma " ")
1382 math-comp-vector-prec)
1383 math-comp-right-bracket
1384 ")"))))
1385
1386 (put 'maple 'math-compose-subscr
1387 (function
1388 (lambda (a)
1389 (let ((args (cdr (cdr a))))
1390 (list 'horiz
1391 (math-compose-expr (nth 1 a) 1000)
1392 "["
1393 (math-compose-vector args ", " 0)
1394 "]")))))
1395
1396 (add-to-list 'calc-lang-allow-underscores 'maple)
1397 (add-to-list 'calc-lang-brackets-are-subscripts 'maple)
1398
1399 (defun math-compose-maple-intv (a)
1400 (list 'horiz
1401 (math-compose-expr (nth 2 a) 0)
1402 " .. "
1403 (math-compose-expr (nth 3 a) 0)))
1404
1405 (defun math-read-maple-dots (x op)
1406 (list 'intv 3 x (math-read-expr-level (nth 3 op))))
1407
1408
1409 ;; The variable math-read-big-lines is local to math-read-big-expr in
1410 ;; calc-ext.el, but is used by math-read-big-rec, math-read-big-char,
1411 ;; math-read-big-emptyp, math-read-big-error and math-read-big-balance,
1412 ;; which are called (directly and indirectly) by math-read-big-expr.
1413 ;; It is also local to math-read-big-bigp in calc-ext.el, which calls
1414 ;; math-read-big-balance.
1415 (defvar math-read-big-lines)
1416
1417 ;; The variables math-read-big-baseline and math-read-big-h2 are
1418 ;; local to math-read-big-expr in calc-ext.el, but used by
1419 ;; math-read-big-rec.
1420 (defvar math-read-big-baseline)
1421 (defvar math-read-big-h2)
1422
1423 ;; The variables math-rb-h1, math-rb-h2, math-rb-v1 and math-rb-v2
1424 ;; are local to math-read-big-rec, but are used by math-read-big-char,
1425 ;; math-read-big-emptyp and math-read-big-balance which are called by
1426 ;; math-read-big-rec.
1427 ;; math-rb-h2 is also local to math-read-big-bigp in calc-ext.el,
1428 ;; which calls math-read-big-balance.
1429 (defvar math-rb-h1)
1430 (defvar math-rb-h2)
1431 (defvar math-rb-v1)
1432 (defvar math-rb-v2)
1433
1434 (defun math-read-big-rec (math-rb-h1 math-rb-v1 math-rb-h2 math-rb-v2
1435 &optional baseline prec short)
1436 (or prec (setq prec 0))
1437
1438 ;; Clip whitespace above or below.
1439 (while (and (< math-rb-v1 math-rb-v2)
1440 (math-read-big-emptyp math-rb-h1 math-rb-v1 math-rb-h2 (1+ math-rb-v1)))
1441 (setq math-rb-v1 (1+ math-rb-v1)))
1442 (while (and (< math-rb-v1 math-rb-v2)
1443 (math-read-big-emptyp math-rb-h1 (1- math-rb-v2) math-rb-h2 math-rb-v2))
1444 (setq math-rb-v2 (1- math-rb-v2)))
1445
1446 ;; If formula is a single line high, normal parser can handle it.
1447 (if (<= math-rb-v2 (1+ math-rb-v1))
1448 (if (or (<= math-rb-v2 math-rb-v1)
1449 (> math-rb-h1 (length (setq math-rb-v2
1450 (nth math-rb-v1 math-read-big-lines)))))
1451 (math-read-big-error math-rb-h1 math-rb-v1)
1452 (setq math-read-big-baseline math-rb-v1
1453 math-read-big-h2 math-rb-h2
1454 math-rb-v2 (nth math-rb-v1 math-read-big-lines)
1455 math-rb-h2 (math-read-expr
1456 (substring math-rb-v2 math-rb-h1
1457 (min math-rb-h2 (length math-rb-v2)))))
1458 (if (eq (car-safe math-rb-h2) 'error)
1459 (math-read-big-error (+ math-rb-h1 (nth 1 math-rb-h2))
1460 math-rb-v1 (nth 2 math-rb-h2))
1461 math-rb-h2))
1462
1463 ;; Clip whitespace at left or right.
1464 (while (and (< math-rb-h1 math-rb-h2)
1465 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) math-rb-v2))
1466 (setq math-rb-h1 (1+ math-rb-h1)))
1467 (while (and (< math-rb-h1 math-rb-h2)
1468 (math-read-big-emptyp (1- math-rb-h2) math-rb-v1 math-rb-h2 math-rb-v2))
1469 (setq math-rb-h2 (1- math-rb-h2)))
1470
1471 ;; Scan to find widest left-justified "----" in the region.
1472 (let* ((widest nil)
1473 (widest-h2 0)
1474 (lines-v1 (nthcdr math-rb-v1 math-read-big-lines))
1475 (p lines-v1)
1476 (v math-rb-v1)
1477 (other-v nil)
1478 other-char line len h)
1479 (while (< v math-rb-v2)
1480 (setq line (car p)
1481 len (min math-rb-h2 (length line)))
1482 (and (< math-rb-h1 len)
1483 (/= (aref line math-rb-h1) ?\ )
1484 (if (and (= (aref line math-rb-h1) ?\-)
1485 ;; Make sure it's not a minus sign.
1486 (or (and (< (1+ math-rb-h1) len)
1487 (= (aref line (1+ math-rb-h1)) ?\-))
1488 (/= (math-read-big-char math-rb-h1 (1- v)) ?\ )
1489 (/= (math-read-big-char math-rb-h1 (1+ v)) ?\ )))
1490 (progn
1491 (setq h math-rb-h1)
1492 (while (and (< (setq h (1+ h)) len)
1493 (= (aref line h) ?\-)))
1494 (if (> h widest-h2)
1495 (setq widest v
1496 widest-h2 h)))
1497 (or other-v (setq other-v v other-char (aref line math-rb-h1)))))
1498 (setq v (1+ v)
1499 p (cdr p)))
1500
1501 (cond ((not (setq v other-v))
1502 (math-read-big-error math-rb-h1 math-rb-v1)) ; Should never happen!
1503
1504 ;; Quotient.
1505 (widest
1506 (setq h widest-h2
1507 v widest)
1508 (let ((num (math-read-big-rec math-rb-h1 math-rb-v1 h v))
1509 (den (math-read-big-rec math-rb-h1 (1+ v) h math-rb-v2)))
1510 (setq p (if (and (math-integerp num) (math-integerp den))
1511 (math-make-frac num den)
1512 (list '/ num den)))))
1513
1514 ;; Big radical sign.
1515 ((= other-char ?\\)
1516 (or (= (math-read-big-char (1+ math-rb-h1) v) ?\|)
1517 (math-read-big-error (1+ math-rb-h1) v "Malformed root sign"))
1518 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1519 (while (= (math-read-big-char (1+ math-rb-h1) (setq v (1- v))) ?\|))
1520 (or (= (math-read-big-char (setq h (+ math-rb-h1 2)) v) ?\_)
1521 (math-read-big-error h v "Malformed root sign"))
1522 (while (= (math-read-big-char (setq h (1+ h)) v) ?\_))
1523 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1524 (math-read-big-emptyp math-rb-h1 (1+ other-v) h math-rb-v2 nil t)
1525 (setq p (list 'calcFunc-sqrt (math-read-big-rec
1526 (+ math-rb-h1 2) (1+ v)
1527 h (1+ other-v) baseline))
1528 v math-read-big-baseline))
1529
1530 ;; Small radical sign.
1531 ((and (= other-char ?V)
1532 (= (math-read-big-char (1+ math-rb-h1) (1- v)) ?\_))
1533 (setq h (1+ math-rb-h1))
1534 (math-read-big-emptyp math-rb-h1 math-rb-v1 h (1- v) nil t)
1535 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1536 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1537 (while (= (math-read-big-char (setq h (1+ h)) (1- v)) ?\_))
1538 (setq p (list 'calcFunc-sqrt (math-read-big-rec
1539 (1+ math-rb-h1) v h (1+ v) t))
1540 v math-read-big-baseline))
1541
1542 ;; Binomial coefficient.
1543 ((and (= other-char ?\()
1544 (= (math-read-big-char (1+ math-rb-h1) v) ?\ )
1545 (= (string-match "( *)" (nth v math-read-big-lines)
1546 math-rb-h1) math-rb-h1))
1547 (setq h (match-end 0))
1548 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1549 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1550 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1551 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1552 (setq p (list 'calcFunc-choose
1553 (math-read-big-rec (1+ math-rb-h1) math-rb-v1 (1- h) v)
1554 (math-read-big-rec (1+ math-rb-h1) (1+ v)
1555 (1- h) math-rb-v2))))
1556
1557 ;; Minus sign.
1558 ((= other-char ?\-)
1559 (setq p (list 'neg (math-read-big-rec (1+ math-rb-h1) math-rb-v1
1560 math-rb-h2 math-rb-v2 v 250 t))
1561 v math-read-big-baseline
1562 h math-read-big-h2))
1563
1564 ;; Parentheses.
1565 ((= other-char ?\()
1566 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1567 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1568 (setq h (math-read-big-balance (1+ math-rb-h1) v "(" t))
1569 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1570 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1571 (let ((sep (math-read-big-char (1- h) v))
1572 hmid)
1573 (if (= sep ?\.)
1574 (setq h (1+ h)))
1575 (if (= sep ?\])
1576 (math-read-big-error (1- h) v "Expected `)'"))
1577 (if (= sep ?\))
1578 (setq p (math-read-big-rec
1579 (1+ math-rb-h1) math-rb-v1 (1- h) math-rb-v2 v))
1580 (setq hmid (math-read-big-balance h v "(")
1581 p (list p
1582 (math-read-big-rec h math-rb-v1 (1- hmid) math-rb-v2 v))
1583 h hmid)
1584 (cond ((= sep ?\.)
1585 (setq p (cons 'intv (cons (if (= (math-read-big-char
1586 (1- h) v)
1587 ?\))
1588 0 1)
1589 p))))
1590 ((= (math-read-big-char (1- h) v) ?\])
1591 (math-read-big-error (1- h) v "Expected `)'"))
1592 ((= sep ?\,)
1593 (or (and (math-realp (car p)) (math-realp (nth 1 p)))
1594 (math-read-big-error
1595 math-rb-h1 v "Complex components must be real"))
1596 (setq p (cons 'cplx p)))
1597 ((= sep ?\;)
1598 (or (and (math-realp (car p)) (math-anglep (nth 1 p)))
1599 (math-read-big-error
1600 math-rb-h1 v "Complex components must be real"))
1601 (setq p (cons 'polar p)))))))
1602
1603 ;; Matrix.
1604 ((and (= other-char ?\[)
1605 (or (= (math-read-big-char (setq h math-rb-h1) (1+ v)) ?\[)
1606 (= (math-read-big-char (setq h (1+ h)) v) ?\[)
1607 (and (= (math-read-big-char h v) ?\ )
1608 (= (math-read-big-char (setq h (1+ h)) v) ?\[)))
1609 (= (math-read-big-char h (1+ v)) ?\[))
1610 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1611 (let ((vtop v)
1612 (hleft h)
1613 (hright nil))
1614 (setq p nil)
1615 (while (progn
1616 (setq h (math-read-big-balance (1+ hleft) v "["))
1617 (if hright
1618 (or (= h hright)
1619 (math-read-big-error hright v "Expected `]'"))
1620 (setq hright h))
1621 (setq p (cons (math-read-big-rec
1622 hleft v h (1+ v)) p))
1623 (and (memq (math-read-big-char h v) '(?\ ?\,))
1624 (= (math-read-big-char hleft (1+ v)) ?\[)))
1625 (setq v (1+ v)))
1626 (or (= hleft math-rb-h1)
1627 (progn
1628 (if (= (math-read-big-char h v) ?\ )
1629 (setq h (1+ h)))
1630 (and (= (math-read-big-char h v) ?\])
1631 (setq h (1+ h))))
1632 (math-read-big-error (1- h) v "Expected `]'"))
1633 (if (= (math-read-big-char h vtop) ?\,)
1634 (setq h (1+ h)))
1635 (math-read-big-emptyp math-rb-h1 (1+ v) (1- h) math-rb-v2 nil t)
1636 (setq v (+ vtop (/ (- v vtop) 2))
1637 p (cons 'vec (nreverse p)))))
1638
1639 ;; Square brackets.
1640 ((= other-char ?\[)
1641 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1642 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1643 (setq p nil
1644 h (1+ math-rb-h1))
1645 (while (progn
1646 (setq widest (math-read-big-balance h v "[" t))
1647 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1648 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1649 (setq p (cons (math-read-big-rec
1650 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1651 h widest)
1652 (= (math-read-big-char (1- h) v) ?\,)))
1653 (setq widest (math-read-big-char (1- h) v))
1654 (if (or (memq widest '(?\; ?\)))
1655 (and (eq widest ?\.) (cdr p)))
1656 (math-read-big-error (1- h) v "Expected `]'"))
1657 (if (= widest ?\.)
1658 (setq h (1+ h)
1659 widest (math-read-big-balance h v "[")
1660 p (nconc p (list (math-read-big-rec
1661 h math-rb-v1 (1- widest) math-rb-v2 v)))
1662 h widest
1663 p (cons 'intv (cons (if (= (math-read-big-char (1- h) v)
1664 ?\])
1665 3 2)
1666 p)))
1667 (setq p (cons 'vec (nreverse p)))))
1668
1669 ;; Date form.
1670 ((= other-char ?\<)
1671 (setq line (nth v math-read-big-lines))
1672 (string-match ">" line math-rb-h1)
1673 (setq h (match-end 0))
1674 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1675 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1676 (setq p (math-read-big-rec math-rb-h1 v h (1+ v) v)))
1677
1678 ;; Variable name or function call.
1679 ((or (and (>= other-char ?a) (<= other-char ?z))
1680 (and (>= other-char ?A) (<= other-char ?Z)))
1681 (setq line (nth v math-read-big-lines))
1682 (string-match "\\([a-zA-Z'_]+\\) *" line math-rb-h1)
1683 (setq h (match-end 1)
1684 widest (match-end 0)
1685 p (math-match-substring line 1))
1686 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1687 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1688 (if (= (math-read-big-char widest v) ?\()
1689 (progn
1690 (setq line (if (string-match "-" p)
1691 (intern p)
1692 (intern (concat "calcFunc-" p)))
1693 h (1+ widest)
1694 p nil)
1695 (math-read-big-emptyp widest math-rb-v1 h v nil t)
1696 (math-read-big-emptyp widest (1+ v) h math-rb-v2 nil t)
1697 (while (progn
1698 (setq widest (math-read-big-balance h v "(" t))
1699 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1700 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1701 (setq p (cons (math-read-big-rec
1702 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1703 h widest)
1704 (= (math-read-big-char (1- h) v) ?\,)))
1705 (or (= (math-read-big-char (1- h) v) ?\))
1706 (math-read-big-error (1- h) v "Expected `)'"))
1707 (setq p (cons line (nreverse p))))
1708 (setq p (list 'var
1709 (intern (math-remove-dashes p))
1710 (if (string-match "-" p)
1711 (intern p)
1712 (intern (concat "var-" p)))))))
1713
1714 ;; Number.
1715 (t
1716 (setq line (nth v math-read-big-lines))
1717 (or (= (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\([0-9]+\\(#\\|\\^\\^\\)[0-9a-zA-Z:]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?" line math-rb-h1) math-rb-h1)
1718 (math-read-big-error h v "Expected a number"))
1719 (setq h (match-end 0)
1720 p (math-read-number (math-match-substring line 0)))
1721 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1722 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)))
1723
1724 ;; Now left term is bounded by math-rb-h1, math-rb-v1, h, math-rb-v2;
1725 ;; baseline = v.
1726 (if baseline
1727 (or (= v baseline)
1728 (math-read-big-error math-rb-h1 v "Inconsistent baseline in formula"))
1729 (setq baseline v))
1730
1731 ;; Look for superscripts or subscripts.
1732 (setq line (nth baseline math-read-big-lines)
1733 len (min math-rb-h2 (length line))
1734 widest h)
1735 (while (and (< widest len)
1736 (= (aref line widest) ?\ ))
1737 (setq widest (1+ widest)))
1738 (and (>= widest len) (setq widest math-rb-h2))
1739 (if (math-read-big-emptyp h v widest math-rb-v2)
1740 (if (math-read-big-emptyp h math-rb-v1 widest v)
1741 (setq h widest)
1742 (setq p (list '^ p (math-read-big-rec h math-rb-v1 widest v))
1743 h widest))
1744 (if (math-read-big-emptyp h math-rb-v1 widest v)
1745 (setq p (list 'calcFunc-subscr p
1746 (math-read-big-rec h v widest math-rb-v2))
1747 h widest)))
1748
1749 ;; Look for an operator name and grab additional terms.
1750 (while (and (< h len)
1751 (if (setq widest (and (math-read-big-emptyp
1752 h math-rb-v1 (1+ h) v)
1753 (math-read-big-emptyp
1754 h (1+ v) (1+ h) math-rb-v2)
1755 (string-match "<=\\|>=\\|\\+/-\\|!=\\|&&\\|||\\|:=\\|=>\\|." line h)
1756 (assoc (math-match-substring line 0)
1757 (math-standard-ops))))
1758 (and (>= (nth 2 widest) prec)
1759 (setq h (match-end 0)))
1760 (and (not (eq (string-match ",\\|;\\|\\.\\.\\|)\\|\\]\\|:" line h)
1761 h))
1762 (setq widest '("2x" * 196 195)))))
1763 (cond ((eq (nth 3 widest) -1)
1764 (setq p (list (nth 1 widest) p)))
1765 ((equal (car widest) "?")
1766 (let ((y (math-read-big-rec h math-rb-v1 math-rb-h2
1767 math-rb-v2 baseline nil t)))
1768 (or (= (math-read-big-char math-read-big-h2 baseline) ?\:)
1769 (math-read-big-error math-read-big-h2 baseline "Expected `:'"))
1770 (setq p (list (nth 1 widest) p y
1771 (math-read-big-rec
1772 (1+ math-read-big-h2) math-rb-v1 math-rb-h2 math-rb-v2
1773 baseline (nth 3 widest) t))
1774 h math-read-big-h2)))
1775 (t
1776 (setq p (list (nth 1 widest) p
1777 (math-read-big-rec h math-rb-v1 math-rb-h2 math-rb-v2
1778 baseline (nth 3 widest) t))
1779 h math-read-big-h2))))
1780
1781 ;; Return all relevant information to caller.
1782 (setq math-read-big-baseline baseline
1783 math-read-big-h2 h)
1784 (or short (= math-read-big-h2 math-rb-h2)
1785 (math-read-big-error h baseline))
1786 p)))
1787
1788 (defun math-read-big-char (h v)
1789 (or (and (>= h math-rb-h1)
1790 (< h math-rb-h2)
1791 (>= v math-rb-v1)
1792 (< v math-rb-v2)
1793 (let ((line (nth v math-read-big-lines)))
1794 (and line
1795 (< h (length line))
1796 (aref line h))))
1797 ?\ ))
1798
1799 (defun math-read-big-emptyp (eh1 ev1 eh2 ev2 &optional what error)
1800 (and (< ev1 math-rb-v1) (setq ev1 math-rb-v1))
1801 (and (< eh1 math-rb-h1) (setq eh1 math-rb-h1))
1802 (and (> ev2 math-rb-v2) (setq ev2 math-rb-v2))
1803 (and (> eh2 math-rb-h2) (setq eh2 math-rb-h2))
1804 (or what (setq what ?\ ))
1805 (let ((p (nthcdr ev1 math-read-big-lines))
1806 h)
1807 (while (and (< ev1 ev2)
1808 (progn
1809 (setq h (min eh2 (length (car p))))
1810 (while (and (>= (setq h (1- h)) eh1)
1811 (= (aref (car p) h) what)))
1812 (and error (>= h eh1)
1813 (math-read-big-error h ev1 (if (stringp error)
1814 error
1815 "Whitespace expected")))
1816 (< h eh1)))
1817 (setq ev1 (1+ ev1)
1818 p (cdr p)))
1819 (>= ev1 ev2)))
1820
1821 ;; math-read-big-err-msg is local to math-read-big-expr in calc-ext.el,
1822 ;; but is used by math-read-big-error which is called (indirectly) by
1823 ;; math-read-big-expr.
1824 (defvar math-read-big-err-msg)
1825
1826 (defun math-read-big-error (h v &optional msg)
1827 (let ((pos 0)
1828 (p math-read-big-lines))
1829 (while (> v 0)
1830 (setq pos (+ pos 1 (length (car p)))
1831 p (cdr p)
1832 v (1- v)))
1833 (setq h (+ pos (min h (length (car p))))
1834 math-read-big-err-msg (list 'error h (or msg "Syntax error")))
1835 (throw 'syntax nil)))
1836
1837 (defun math-read-big-balance (h v what &optional commas)
1838 (let* ((line (nth v math-read-big-lines))
1839 (len (min math-rb-h2 (length line)))
1840 (count 1))
1841 (while (> count 0)
1842 (if (>= h len)
1843 (if what
1844 (math-read-big-error nil v (format "Unmatched `%s'" what))
1845 (setq count 0))
1846 (if (memq (aref line h) '(?\( ?\[))
1847 (setq count (1+ count))
1848 (if (if (and commas (= count 1))
1849 (or (memq (aref line h) '(?\) ?\] ?\, ?\;))
1850 (and (eq (aref line h) ?\.)
1851 (< (1+ h) len)
1852 (eq (aref line (1+ h)) ?\.)))
1853 (memq (aref line h) '(?\) ?\])))
1854 (setq count (1- count))))
1855 (setq h (1+ h))))
1856 h))
1857
1858 (provide 'calc-lang)
1859
1860 ;;; arch-tag: 483bfe15-f290-4fef-bb7d-ce65be687f2e
1861 ;;; calc-lang.el ends here