(math-eqn-special-funcs): Add functions to list.
[bpt/emacs.git] / lisp / calc / calccomp.el
1 ;;; calccomp.el --- composition functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
16
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; This file is autoloaded from calc-ext.el.
30
31 (require 'calc-ext)
32 (require 'calc-macs)
33
34 (defconst math-eqn-special-funcs
35 '( calcFunc-log
36 calcFunc-ln calcFunc-exp
37 calcFunc-sin calcFunc-cos calcFunc-tan
38 calcFunc-sec calcFunc-csc calcFunc-cot
39 calcFunc-sinh calcFunc-cosh calcFunc-tanh
40 calcFunc-sech calcFunc-csch calcFunc-coth
41 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
42 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh))
43
44 ;;; A "composition" has one of the following forms:
45 ;;;
46 ;;; "string" A literal string
47 ;;;
48 ;;; (horiz C1 C2 ...) Horizontally abutted sub-compositions
49 ;;;
50 ;;; (set LEVEL OFF) Set left margin + offset for line-break level
51 ;;; (break LEVEL) A potential line-break point
52 ;;;
53 ;;; (vleft N C1 C2 ...) Vertically stacked, left-justified sub-comps
54 ;;; (vcent N C1 C2 ...) Vertically stacked, centered sub-comps
55 ;;; (vright N C1 C2 ...) Vertically stacked, right-justified sub-comps
56 ;;; N specifies baseline of the stack, 0=top line.
57 ;;;
58 ;;; (supscr C1 C2) Composition C1 with superscript C2
59 ;;; (subscr C1 C2) Composition C1 with subscript C2
60 ;;; (rule X) Horizontal line of X, full width of enclosing comp
61 ;;;
62 ;;; (tag X C) Composition C corresponds to sub-expression X
63
64 ;; math-comp-just and math-comp-comma-spc are local to
65 ;; math-compose-expr, but are used by math-compose-matrix, which is
66 ;; called by math-compose-expr
67 (defvar math-comp-just)
68 (defvar math-comp-comma-spc)
69
70 ;; math-comp-vector-prec is local to math-compose-expr, but is used by
71 ;; math-compose-matrix and math-compose-rows, which are called by
72 ;; math-compose-expr.
73 (defvar math-comp-vector-prec)
74
75 ;; math-comp-left-bracket, math-comp-right-bracket and math-comp-comma are
76 ;; local to math-compose-expr, but are used by math-compose-rows, which is
77 ;; called by math-compose-expr.
78 (defvar math-comp-left-bracket)
79 (defvar math-comp-right-bracket)
80 (defvar math-comp-comma)
81
82
83 (defun math-compose-expr (a prec)
84 (let ((math-compose-level (1+ math-compose-level))
85 spfn)
86 (cond
87 ((or (and (eq a math-comp-selected) a)
88 (and math-comp-tagged
89 (not (eq math-comp-tagged a))))
90 (let ((math-comp-selected nil))
91 (and math-comp-tagged (setq math-comp-tagged a))
92 (list 'tag a (math-compose-expr a prec))))
93 ((and (not (consp a)) (not (integerp a)))
94 (concat "'" (prin1-to-string a)))
95 ((setq spfn (assq (car-safe a) math-expr-special-function-mapping))
96 (setq spfn (cdr spfn))
97 (funcall (car spfn) a spfn))
98 ((math-scalarp a)
99 (if (or (eq (car-safe a) 'frac)
100 (and (nth 1 calc-frac-format) (Math-integerp a)))
101 (if (memq calc-language '(tex latex eqn math maple c fortran pascal))
102 (let ((aa (math-adjust-fraction a))
103 (calc-frac-format nil))
104 (math-compose-expr (list '/
105 (if (memq calc-language '(c fortran))
106 (math-float (nth 1 aa))
107 (nth 1 aa))
108 (nth 2 aa)) prec))
109 (if (and (eq calc-language 'big)
110 (= (length (car calc-frac-format)) 1))
111 (let* ((aa (math-adjust-fraction a))
112 (calc-frac-format nil)
113 (math-radix-explicit-format nil)
114 (c (list 'horiz
115 (if (math-negp (nth 1 aa))
116 "- " "")
117 (list 'vcent 1
118 (math-format-number
119 (math-abs (nth 1 aa)))
120 '(rule ?-)
121 (math-format-number (nth 2 aa))))))
122 (if (= calc-number-radix 10)
123 c
124 (list 'horiz "(" c
125 (list 'subscr ")"
126 (int-to-string calc-number-radix)))))
127 (math-format-number a)))
128 (if (not (eq calc-language 'big))
129 (math-format-number a prec)
130 (if (memq (car-safe a) '(cplx polar))
131 (if (math-zerop (nth 2 a))
132 (math-compose-expr (nth 1 a) prec)
133 (list 'horiz "("
134 (math-compose-expr (nth 1 a) 0)
135 (if (eq (car a) 'cplx) ", " "; ")
136 (math-compose-expr (nth 2 a) 0) ")"))
137 (if (or (= calc-number-radix 10)
138 (not (Math-realp a))
139 (and calc-group-digits
140 (not (assoc calc-group-char '((",") (" "))))))
141 (math-format-number a prec)
142 (let ((s (math-format-number a prec))
143 (c nil))
144 (while (string-match (if (> calc-number-radix 14)
145 "\\([0-9]+\\)#\\([0-9a-zA-Z., ]+\\)"
146 "\\([0-9]+\\)#\\([0-9a-dA-D., ]+\\)")
147 s)
148 (setq c (nconc c (list (substring s 0 (match-beginning 0))
149 (list 'subscr
150 (math-match-substring s 2)
151 (math-match-substring s 1))))
152 s (substring s (match-end 0))))
153 (if (string-match
154 "\\*\\([0-9.]+\\)\\^\\(-?[0-9]+\\)\\()?\\)\\'" s)
155 (setq s (list 'horiz
156 (substring s 0 (match-beginning 0)) " "
157 (list 'supscr
158 (math-match-substring s 1)
159 (math-match-substring s 2))
160 (math-match-substring s 3))))
161 (if c (cons 'horiz (nconc c (list s))) s)))))))
162 ((and (get (car a) 'math-compose-forms)
163 (not (eq calc-language 'unform))
164 (let ((comps (get (car a) 'math-compose-forms))
165 temp temp2)
166 (or (and (setq temp (assq calc-language comps))
167 (or (and (setq temp2 (assq (1- (length a)) (cdr temp)))
168 (setq temp (apply (cdr temp2) (cdr a)))
169 (math-compose-expr temp prec))
170 (and (setq temp2 (assq nil (cdr temp)))
171 (funcall (cdr temp2) a))))
172 (and (setq temp (assq nil comps))
173 (or (and (setq temp2 (assq (1- (length a)) (cdr temp)))
174 (setq temp (apply (cdr temp2) (cdr a)))
175 (math-compose-expr temp prec))
176 (and (setq temp2 (assq nil (cdr temp)))
177 (funcall (cdr temp2) a))))))))
178 ((eq (car a) 'vec)
179 (let* ((math-comp-left-bracket (if calc-vector-brackets
180 (substring calc-vector-brackets 0 1) ""))
181 (math-comp-right-bracket (if calc-vector-brackets
182 (substring calc-vector-brackets 1 2) ""))
183 (inner-brackets (memq 'R calc-matrix-brackets))
184 (outer-brackets (memq 'O calc-matrix-brackets))
185 (row-commas (memq 'C calc-matrix-brackets))
186 (math-comp-comma-spc (or calc-vector-commas " "))
187 (math-comp-comma (or calc-vector-commas ""))
188 (math-comp-vector-prec (if (or (and calc-vector-commas
189 (math-vector-no-parens a))
190 (memq 'P calc-matrix-brackets)) 0 1000))
191 (math-comp-just (cond ((eq calc-matrix-just 'right) 'vright)
192 ((eq calc-matrix-just 'center) 'vcent)
193 (t 'vleft)))
194 (break calc-break-vectors))
195 (if (and (memq calc-language '(nil big))
196 (not calc-break-vectors)
197 (math-matrixp a) (not (math-matrixp (nth 1 a)))
198 (or calc-full-vectors
199 (and (< (length a) 7) (< (length (nth 1 a)) 7))
200 (progn (setq break t) nil)))
201 (if (progn
202 (setq math-comp-vector-prec (if (or (and calc-vector-commas
203 (math-vector-no-parens
204 (nth 1 a)))
205 (memq 'P calc-matrix-brackets))
206 0 1000))
207 (= (length a) 2))
208 (list 'horiz
209 (concat math-comp-left-bracket math-comp-left-bracket " ")
210 (math-compose-vector (cdr (nth 1 a)) (concat math-comp-comma " ")
211 math-comp-vector-prec)
212 (concat " " math-comp-right-bracket math-comp-right-bracket))
213 (let* ((rows (1- (length a)))
214 (cols (1- (length (nth 1 a))))
215 (base (/ (1- rows) 2))
216 (calc-language 'flat))
217 (append '(horiz)
218 (list (append '(vleft)
219 (list base)
220 (list (concat (and outer-brackets
221 (concat math-comp-left-bracket
222 " "))
223 (and inner-brackets
224 (concat math-comp-left-bracket
225 " "))))
226 (make-list (1- rows)
227 (concat (and outer-brackets
228 " ")
229 (and inner-brackets
230 (concat
231 math-comp-left-bracket
232 " "))))))
233 (math-compose-matrix (cdr a) 1 cols base)
234 (list (append '(vleft)
235 (list base)
236 (make-list (1- rows)
237 (if inner-brackets
238 (concat " "
239 math-comp-right-bracket
240 (and row-commas
241 math-comp-comma))
242 (if (and outer-brackets
243 row-commas)
244 ";" "")))
245 (list (concat
246 (and inner-brackets
247 (concat " "
248 math-comp-right-bracket))
249 (and outer-brackets
250 (concat
251 " "
252 math-comp-right-bracket)))))))))
253 (if (and calc-display-strings
254 (cdr a)
255 (math-vector-is-string a))
256 (math-vector-to-string a t)
257 (if (and break (cdr a)
258 (not (eq calc-language 'flat)))
259 (let* ((full (or calc-full-vectors (< (length a) 7)))
260 (rows (if full (1- (length a)) 5))
261 (base (/ (1- rows) 2))
262 (calc-break-vectors nil))
263 (list 'horiz
264 (cons 'vleft (cons base
265 (math-compose-rows
266 (cdr a)
267 (if full rows 3) t)))))
268 (if (or calc-full-vectors (< (length a) 7))
269 (if (and (eq calc-language 'tex)
270 (math-matrixp a))
271 (if (and (integerp calc-language-option)
272 (or (= calc-language-option 0)
273 (> calc-language-option 1)
274 (< calc-language-option -1)))
275 (append '(vleft 0 "\\matrix{")
276 (math-compose-tex-matrix (cdr a))
277 '("}"))
278 (append '(horiz "\\matrix{ ")
279 (math-compose-tex-matrix (cdr a))
280 '(" }")))
281 (if (and (eq calc-language 'latex)
282 (math-matrixp a))
283 (if (and (integerp calc-language-option)
284 (or (= calc-language-option 0)
285 (> calc-language-option 1)
286 (< calc-language-option -1)))
287 (append '(vleft 0 "\\begin{pmatrix}")
288 (math-compose-tex-matrix (cdr a))
289 '("\\end{pmatrix}"))
290 (append '(horiz "\\begin{pmatrix} ")
291 (math-compose-tex-matrix (cdr a))
292 '(" \\end{pmatrix}")))
293 (if (and (eq calc-language 'eqn)
294 (math-matrixp a))
295 (append '(horiz "matrix { ")
296 (math-compose-eqn-matrix
297 (cdr (math-transpose a)))
298 '("}"))
299 (if (and (eq calc-language 'maple)
300 (math-matrixp a))
301 (list 'horiz
302 "matrix("
303 math-comp-left-bracket
304 (math-compose-vector (cdr a)
305 (concat math-comp-comma " ")
306 math-comp-vector-prec)
307 math-comp-right-bracket
308 ")")
309 (list 'horiz
310 math-comp-left-bracket
311 (math-compose-vector (cdr a)
312 (concat math-comp-comma " ")
313 math-comp-vector-prec)
314 math-comp-right-bracket)))))
315 (list 'horiz
316 math-comp-left-bracket
317 (math-compose-vector (list (nth 1 a) (nth 2 a) (nth 3 a))
318 (concat math-comp-comma " ")
319 math-comp-vector-prec)
320 math-comp-comma (if (memq calc-language '(tex latex))
321 " \\ldots" " ...")
322 math-comp-comma " "
323 (list 'break math-compose-level)
324 (math-compose-expr (nth (1- (length a)) a)
325 (if (equal math-comp-comma "") 1000 0))
326 math-comp-right-bracket)))))))
327 ((eq (car a) 'incomplete)
328 (if (cdr (cdr a))
329 (cond ((eq (nth 1 a) 'vec)
330 (list 'horiz "["
331 (math-compose-vector (cdr (cdr a)) ", " 0)
332 " ..."))
333 ((eq (nth 1 a) 'cplx)
334 (list 'horiz "("
335 (math-compose-vector (cdr (cdr a)) ", " 0)
336 ", ..."))
337 ((eq (nth 1 a) 'polar)
338 (list 'horiz "("
339 (math-compose-vector (cdr (cdr a)) "; " 0)
340 "; ..."))
341 ((eq (nth 1 a) 'intv)
342 (list 'horiz
343 (if (memq (nth 2 a) '(0 1)) "(" "[")
344 (math-compose-vector (cdr (cdr (cdr a))) " .. " 0)
345 " .. ..."))
346 (t (format "%s" a)))
347 (cond ((eq (nth 1 a) 'vec) "[ ...")
348 ((eq (nth 1 a) 'intv)
349 (if (memq (nth 2 a) '(0 1)) "( ..." "[ ..."))
350 (t "( ..."))))
351 ((eq (car a) 'var)
352 (let ((v (rassq (nth 2 a) math-expr-variable-mapping)))
353 (if v
354 (symbol-name (car v))
355 (if (and (memq calc-language '(tex latex))
356 calc-language-option
357 (not (= calc-language-option 0))
358 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
359 (symbol-name (nth 1 a))))
360 (if (eq calc-language 'latex)
361 (format "\\text{%s}" (symbol-name (nth 1 a)))
362 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
363 (if (and math-compose-hash-args
364 (let ((p calc-arg-values))
365 (setq v 1)
366 (while (and p (not (equal (car p) a)))
367 (setq p (and (eq math-compose-hash-args t) (cdr p))
368 v (1+ v)))
369 p))
370 (if (eq math-compose-hash-args 1)
371 "#"
372 (format "#%d" v))
373 (if (memq calc-language '(c fortran pascal maple))
374 (math-to-underscores (symbol-name (nth 1 a)))
375 (if (and (eq calc-language 'eqn)
376 (string-match ".'\\'" (symbol-name (nth 2 a))))
377 (math-compose-expr
378 (list 'calcFunc-Prime
379 (list
380 'var
381 (intern (substring (symbol-name (nth 1 a)) 0 -1))
382 (intern (substring (symbol-name (nth 2 a)) 0 -1))))
383 prec)
384 (symbol-name (nth 1 a)))))))))
385 ((eq (car a) 'intv)
386 (list 'horiz
387 (if (eq calc-language 'maple) ""
388 (if (memq (nth 1 a) '(0 1)) "(" "["))
389 (math-compose-expr (nth 2 a) 0)
390 (if (memq calc-language '(tex latex)) " \\ldots "
391 (if (eq calc-language 'eqn) " ... " " .. "))
392 (math-compose-expr (nth 3 a) 0)
393 (if (eq calc-language 'maple) ""
394 (if (memq (nth 1 a) '(0 2)) ")" "]"))))
395 ((eq (car a) 'date)
396 (if (eq (car calc-date-format) 'X)
397 (math-format-date a)
398 (concat "<" (math-format-date a) ">")))
399 ((and (eq (car a) 'calcFunc-subscr) (cdr (cdr a))
400 (memq calc-language '(c pascal fortran maple)))
401 (let ((args (cdr (cdr a))))
402 (while (and (memq calc-language '(pascal fortran))
403 (eq (car-safe (nth 1 a)) 'calcFunc-subscr))
404 (setq args (append (cdr (cdr (nth 1 a))) args)
405 a (nth 1 a)))
406 (list 'horiz
407 (math-compose-expr (nth 1 a) 1000)
408 (if (eq calc-language 'fortran) "(" "[")
409 (math-compose-vector args ", " 0)
410 (if (eq calc-language 'fortran) ")" "]"))))
411 ((and (eq (car a) 'calcFunc-subscr) (= (length a) 3)
412 (eq calc-language 'big))
413 (let* ((a1 (math-compose-expr (nth 1 a) 1000))
414 (calc-language 'flat)
415 (a2 (math-compose-expr (nth 2 a) 0)))
416 (if (or (eq (car-safe a1) 'subscr)
417 (and (eq (car-safe a1) 'tag)
418 (eq (car-safe (nth 2 a1)) 'subscr)
419 (setq a1 (nth 2 a1))))
420 (list 'subscr
421 (nth 1 a1)
422 (list 'horiz
423 (nth 2 a1)
424 ", "
425 a2))
426 (list 'subscr a1 a2))))
427 ((and (eq (car a) 'calcFunc-subscr) (= (length a) 3)
428 (eq calc-language 'math))
429 (list 'horiz
430 (math-compose-expr (nth 1 a) 1000)
431 "[["
432 (math-compose-expr (nth 2 a) 0)
433 "]]"))
434 ((and (eq (car a) 'calcFunc-sqrt)
435 (memq calc-language '(tex latex)))
436 (list 'horiz
437 "\\sqrt{"
438 (math-compose-expr (nth 1 a) 0)
439 "}"))
440 ((and nil (eq (car a) 'calcFunc-sqrt)
441 (eq calc-language 'eqn))
442 (list 'horiz
443 "sqrt {"
444 (math-compose-expr (nth 1 a) -1)
445 "}"))
446 ((and (eq (car a) '^)
447 (eq calc-language 'big))
448 (list 'supscr
449 (if (or (math-looks-negp (nth 1 a))
450 (memq (car-safe (nth 1 a)) '(^ / frac calcFunc-sqrt))
451 (and (eq (car-safe (nth 1 a)) 'cplx)
452 (math-negp (nth 1 (nth 1 a)))
453 (eq (nth 2 (nth 1 a)) 0)))
454 (list 'horiz "(" (math-compose-expr (nth 1 a) 0) ")")
455 (math-compose-expr (nth 1 a) 201))
456 (let ((calc-language 'flat)
457 (calc-number-radix 10))
458 (math-compose-expr (nth 2 a) 0))))
459 ((and (eq (car a) '/)
460 (eq calc-language 'big))
461 (let ((a1 (let ((calc-language (if (memq (car-safe (nth 1 a)) '(/ frac))
462 'flat 'big)))
463 (math-compose-expr (nth 1 a) 0)))
464 (a2 (let ((calc-language (if (memq (car-safe (nth 2 a)) '(/ frac))
465 'flat 'big)))
466 (math-compose-expr (nth 2 a) 0))))
467 (list 'vcent
468 (math-comp-height a1)
469 a1 '(rule ?-) a2)))
470 ((and (memq (car a) '(calcFunc-sum calcFunc-prod))
471 (memq calc-language '(tex latex))
472 (= (length a) 5))
473 (list 'horiz (if (eq (car a) 'calcFunc-sum) "\\sum" "\\prod")
474 "_{" (math-compose-expr (nth 2 a) 0)
475 "=" (math-compose-expr (nth 3 a) 0)
476 "}^{" (math-compose-expr (nth 4 a) 0)
477 "}{" (math-compose-expr (nth 1 a) 0) "}"))
478 ((and (eq (car a) 'calcFunc-lambda)
479 (> (length a) 2)
480 (memq calc-language '(nil flat big)))
481 (let ((p (cdr a))
482 (ap calc-arg-values)
483 (math-compose-hash-args (if (= (length a) 3) 1 t)))
484 (while (and (cdr p) (equal (car p) (car ap)))
485 (setq p (cdr p) ap (cdr ap)))
486 (append '(horiz "<")
487 (if (cdr p)
488 (list (math-compose-vector
489 (nreverse (cdr (reverse (cdr a)))) ", " 0)
490 " : ")
491 nil)
492 (list (math-compose-expr (nth (1- (length a)) a) 0)
493 ">"))))
494 ((and (eq (car a) 'calcFunc-string)
495 (= (length a) 2)
496 (math-vectorp (nth 1 a))
497 (math-vector-is-string (nth 1 a)))
498 (if (eq calc-language 'unform)
499 (concat "string(" (math-vector-to-string (nth 1 a) t) ")")
500 (math-vector-to-string (nth 1 a) nil)))
501 ((and (eq (car a) 'calcFunc-bstring)
502 (= (length a) 2)
503 (math-vectorp (nth 1 a))
504 (math-vector-is-string (nth 1 a)))
505 (if (eq calc-language 'unform)
506 (concat "bstring(" (math-vector-to-string (nth 1 a) t) ")")
507 (let ((c nil)
508 (s (math-vector-to-string (nth 1 a) nil))
509 p)
510 (while (string-match "[^ ] +[^ ]" s)
511 (setq p (1- (match-end 0))
512 c (cons (list 'break math-compose-level)
513 (cons (substring s 0 p)
514 c))
515 s (substring s p)))
516 (setq c (nreverse (cons s c)))
517 (or (= prec -123)
518 (setq c (cons (list 'set math-compose-level 2) c)))
519 (cons 'horiz c))))
520 ((and (eq (car a) 'calcFunc-cprec)
521 (not (eq calc-language 'unform))
522 (= (length a) 3)
523 (integerp (nth 2 a)))
524 (let ((c (math-compose-expr (nth 1 a) -1)))
525 (if (> prec (nth 2 a))
526 (if (memq calc-language '(tex latex))
527 (list 'horiz "\\left( " c " \\right)")
528 (if (eq calc-language 'eqn)
529 (list 'horiz "{left ( " c " right )}")
530 (list 'horiz "(" c ")")))
531 c)))
532 ((and (eq (car a) 'calcFunc-choriz)
533 (not (eq calc-language 'unform))
534 (memq (length a) '(2 3 4))
535 (math-vectorp (nth 1 a))
536 (if (integerp (nth 2 a))
537 (or (null (nth 3 a))
538 (and (math-vectorp (nth 3 a))
539 (math-vector-is-string (nth 3 a))))
540 (or (null (nth 2 a))
541 (and (math-vectorp (nth 2 a))
542 (math-vector-is-string (nth 2 a))))))
543 (let* ((cprec (and (integerp (nth 2 a)) (nth 2 a)))
544 (sep (nth (if cprec 3 2) a))
545 (bprec nil))
546 (if sep
547 (math-compose-vector (cdr (nth 1 a))
548 (math-vector-to-string sep nil)
549 (or cprec prec))
550 (cons 'horiz (mapcar (function
551 (lambda (x)
552 (if (eq (car-safe x) 'calcFunc-bstring)
553 (prog1
554 (math-compose-expr
555 x (or bprec cprec prec))
556 (setq bprec -123))
557 (math-compose-expr x (or cprec prec)))))
558 (cdr (nth 1 a)))))))
559 ((and (memq (car a) '(calcFunc-cvert calcFunc-clvert calcFunc-crvert))
560 (not (eq calc-language 'unform))
561 (memq (length a) '(2 3))
562 (math-vectorp (nth 1 a))
563 (or (null (nth 2 a))
564 (integerp (nth 2 a))))
565 (let* ((base 0)
566 (v 0)
567 (prec (or (nth 2 a) prec))
568 (c (mapcar (function
569 (lambda (x)
570 (let ((b nil) (cc nil) a d)
571 (if (and (memq (car-safe x) '(calcFunc-cbase
572 calcFunc-ctbase
573 calcFunc-cbbase))
574 (memq (length x) '(1 2)))
575 (setq b (car x)
576 x (nth 1 x)))
577 (if (and (eq (car-safe x) 'calcFunc-crule)
578 (memq (length x) '(1 2))
579 (or (null (nth 1 x))
580 (and (math-vectorp (nth 1 x))
581 (= (length (nth 1 x)) 2)
582 (math-vector-is-string
583 (nth 1 x)))
584 (and (natnump (nth 1 x))
585 (<= (nth 1 x) 255))))
586 (setq cc (list
587 'rule
588 (if (math-vectorp (nth 1 x))
589 (aref (math-vector-to-string
590 (nth 1 x) nil) 0)
591 (or (nth 1 x) ?-))))
592 (or (and (memq (car-safe x) '(calcFunc-cvspace
593 calcFunc-ctspace
594 calcFunc-cbspace))
595 (memq (length x) '(2 3))
596 (eq (nth 1 x) 0))
597 (null x)
598 (setq cc (math-compose-expr x prec))))
599 (setq a (if cc (math-comp-ascent cc) 0)
600 d (if cc (math-comp-descent cc) 0))
601 (if (eq b 'calcFunc-cbase)
602 (setq base (+ v a -1))
603 (if (eq b 'calcFunc-ctbase)
604 (setq base v)
605 (if (eq b 'calcFunc-cbbase)
606 (setq base (+ v a d -1)))))
607 (setq v (+ v a d))
608 cc)))
609 (cdr (nth 1 a)))))
610 (setq c (delq nil c))
611 (if c
612 (cons (if (eq (car a) 'calcFunc-cvert) 'vcent
613 (if (eq (car a) 'calcFunc-clvert) 'vleft 'vright))
614 (cons base c))
615 " ")))
616 ((and (memq (car a) '(calcFunc-csup calcFunc-csub))
617 (not (eq calc-language 'unform))
618 (memq (length a) '(3 4))
619 (or (null (nth 3 a))
620 (integerp (nth 3 a))))
621 (list (if (eq (car a) 'calcFunc-csup) 'supscr 'subscr)
622 (math-compose-expr (nth 1 a) (or (nth 3 a) 0))
623 (math-compose-expr (nth 2 a) 0)))
624 ((and (eq (car a) 'calcFunc-cflat)
625 (not (eq calc-language 'unform))
626 (memq (length a) '(2 3))
627 (or (null (nth 2 a))
628 (integerp (nth 2 a))))
629 (let ((calc-language (if (memq calc-language '(nil big))
630 'flat calc-language)))
631 (math-compose-expr (nth 1 a) (or (nth 2 a) 0))))
632 ((and (eq (car a) 'calcFunc-cspace)
633 (memq (length a) '(2 3))
634 (natnump (nth 1 a)))
635 (if (nth 2 a)
636 (cons 'horiz (make-list (nth 1 a)
637 (if (and (math-vectorp (nth 2 a))
638 (math-vector-is-string (nth 2 a)))
639 (math-vector-to-string (nth 2 a) nil)
640 (math-compose-expr (nth 2 a) 0))))
641 (make-string (nth 1 a) ?\ )))
642 ((and (memq (car a) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace))
643 (memq (length a) '(2 3))
644 (natnump (nth 1 a)))
645 (if (= (nth 1 a) 0)
646 ""
647 (let* ((c (if (nth 2 a)
648 (if (and (math-vectorp (nth 2 a))
649 (math-vector-is-string (nth 2 a)))
650 (math-vector-to-string (nth 2 a) nil)
651 (math-compose-expr (nth 2 a) 0))
652 " "))
653 (ca (math-comp-ascent c))
654 (cd (math-comp-descent c)))
655 (cons 'vleft
656 (cons (if (eq (car a) 'calcFunc-ctspace)
657 (1- ca)
658 (if (eq (car a) 'calcFunc-cbspace)
659 (+ (* (1- (nth 1 a)) (+ ca cd)) (1- ca))
660 (/ (1- (* (nth 1 a) (+ ca cd))) 2)))
661 (make-list (nth 1 a) c))))))
662 ((and (eq (car a) 'calcFunc-evalto)
663 (setq calc-any-evaltos t)
664 (memq calc-language '(tex latex eqn))
665 (= math-compose-level (if math-comp-tagged 2 1))
666 (= (length a) 3))
667 (list 'horiz
668 (if (memq calc-language '(tex latex)) "\\evalto " "evalto ")
669 (math-compose-expr (nth 1 a) 0)
670 (if (memq calc-language '(tex latex)) " \\to " " -> ")
671 (math-compose-expr (nth 2 a) 0)))
672 (t
673 (let ((op (and (not (eq calc-language 'unform))
674 (if (and (eq (car a) 'calcFunc-if) (= (length a) 4))
675 (assoc "?" math-expr-opers)
676 (math-assq2 (car a) math-expr-opers)))))
677 (cond ((and op
678 (or (= (length a) 3) (eq (car a) 'calcFunc-if))
679 (/= (nth 3 op) -1))
680 (cond
681 ((> prec (or (nth 4 op) (min (nth 2 op) (nth 3 op))))
682 (if (and (memq calc-language '(tex latex))
683 (not (math-tex-expr-is-flat a)))
684 (if (eq (car-safe a) '/)
685 (list 'horiz "{" (math-compose-expr a -1) "}")
686 (list 'horiz "\\left( "
687 (math-compose-expr a -1)
688 " \\right)"))
689 (if (eq calc-language 'eqn)
690 (if (or (eq (car-safe a) '/)
691 (= (/ prec 100) 9))
692 (list 'horiz "{" (math-compose-expr a -1) "}")
693 (if (math-tex-expr-is-flat a)
694 (list 'horiz "( " (math-compose-expr a -1) " )")
695 (list 'horiz "{left ( "
696 (math-compose-expr a -1)
697 " right )}")))
698 (list 'horiz "(" (math-compose-expr a 0) ")"))))
699 ((and (memq calc-language '(tex latex))
700 (memq (car a) '(/ calcFunc-choose calcFunc-evalto))
701 (>= prec 0))
702 (list 'horiz "{" (math-compose-expr a -1) "}"))
703 ((eq (car a) 'calcFunc-if)
704 (list 'horiz
705 (math-compose-expr (nth 1 a) (nth 2 op))
706 " ? "
707 (math-compose-expr (nth 2 a) 0)
708 " : "
709 (math-compose-expr (nth 3 a) (nth 3 op))))
710 (t
711 (let* ((math-comp-tagged (and math-comp-tagged
712 (not (math-primp a))
713 math-comp-tagged))
714 (setlev (if (= prec (min (nth 2 op) (nth 3 op)))
715 (progn
716 (setq math-compose-level
717 (1- math-compose-level))
718 nil)
719 math-compose-level))
720 (lhs (math-compose-expr (nth 1 a) (nth 2 op)))
721 (rhs (math-compose-expr (nth 2 a) (nth 3 op))))
722 (and (equal (car op) "^")
723 (eq (math-comp-first-char lhs) ?-)
724 (setq lhs (list 'horiz "(" lhs ")")))
725 (and (memq calc-language '(tex latex))
726 (or (equal (car op) "^") (equal (car op) "_"))
727 (not (and (stringp rhs) (= (length rhs) 1)))
728 (setq rhs (list 'horiz "{" rhs "}")))
729 (or (and (eq (car a) '*)
730 (or (null calc-language)
731 (assoc "2x" math-expr-opers))
732 (let* ((prevt (math-prod-last-term (nth 1 a)))
733 (nextt (math-prod-first-term (nth 2 a)))
734 (prevc (or (math-comp-last-char lhs)
735 (and (memq (car-safe prevt)
736 '(^ calcFunc-subscr
737 calcFunc-sqrt
738 frac))
739 (eq calc-language 'big)
740 ?0)))
741 (nextc (or (math-comp-first-char rhs)
742 (and (memq (car-safe nextt)
743 '(calcFunc-sqrt
744 calcFunc-sum
745 calcFunc-prod
746 calcFunc-integ))
747 (eq calc-language 'big)
748 ?0))))
749 (and prevc nextc
750 (or (and (>= nextc ?a) (<= nextc ?z))
751 (and (>= nextc ?A) (<= nextc ?Z))
752 (and (>= nextc ?0) (<= nextc ?9))
753 (memq nextc '(?. ?_ ?#
754 ?\( ?\[ ?\{))
755 (and (eq nextc ?\\)
756 (not (string-match
757 "\\`\\\\left("
758 (math-comp-first-string
759 rhs)))))
760 (not (and (eq (car-safe prevt) 'var)
761 (eq nextc ?\()))
762 (list 'horiz
763 (list 'set setlev 1)
764 lhs
765 (list 'break math-compose-level)
766 " "
767 rhs))))
768 (list 'horiz
769 (list 'set setlev 1)
770 lhs
771 (list 'break math-compose-level)
772 (if (or (equal (car op) "^")
773 (equal (car op) "_")
774 (equal (car op) "**")
775 (and (equal (car op) "*")
776 (math-comp-last-char lhs)
777 (math-comp-first-char rhs))
778 (and (equal (car op) "/")
779 (math-num-integerp (nth 1 a))
780 (math-integerp (nth 2 a))))
781 (car op)
782 (if (and (eq calc-language 'big)
783 (equal (car op) "=>"))
784 " => "
785 (concat " " (car op) " ")))
786 rhs))))))
787 ((and op (= (length a) 2) (= (nth 3 op) -1))
788 (cond
789 ((or (> prec (or (nth 4 op) (nth 2 op)))
790 (and (not (eq (assoc (car op) math-expr-opers) op))
791 (> prec 0))) ; don't write x% + y
792 (if (and (memq calc-language '(tex latex))
793 (not (math-tex-expr-is-flat a)))
794 (list 'horiz "\\left( "
795 (math-compose-expr a -1)
796 " \\right)")
797 (if (eq calc-language 'eqn)
798 (if (= (/ prec 100) 9)
799 (list 'horiz "{" (math-compose-expr a -1) "}")
800 (if (math-tex-expr-is-flat a)
801 (list 'horiz "{( " (math-compose-expr a -1) " )}")
802 (list 'horiz "{left ( "
803 (math-compose-expr a -1)
804 " right )}")))
805 (list 'horiz "(" (math-compose-expr a 0) ")"))))
806 (t
807 (let ((lhs (math-compose-expr (nth 1 a) (nth 2 op))))
808 (list 'horiz
809 lhs
810 (if (or (> (length (car op)) 1)
811 (not (math-comp-is-flat lhs)))
812 (concat " " (car op))
813 (car op)))))))
814 ((and op (= (length a) 2) (= (nth 2 op) -1))
815 (cond
816 ((eq (nth 3 op) 0)
817 (let ((lr (and (memq calc-language '(tex latex))
818 (not (math-tex-expr-is-flat (nth 1 a))))))
819 (list 'horiz
820 (if lr "\\left" "")
821 (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op))
822 (substring (car op) 1)
823 (car op))
824 (if (or lr (> (length (car op)) 2)) " " "")
825 (math-compose-expr (nth 1 a) -1)
826 (if (or lr (> (length (car op)) 2)) " " "")
827 (if lr "\\right" "")
828 (car (nth 1 (memq op math-expr-opers))))))
829 ((> prec (or (nth 4 op) (nth 3 op)))
830 (if (and (memq calc-language '(tex latex))
831 (not (math-tex-expr-is-flat a)))
832 (list 'horiz "\\left( "
833 (math-compose-expr a -1)
834 " \\right)")
835 (if (eq calc-language 'eqn)
836 (if (= (/ prec 100) 9)
837 (list 'horiz "{" (math-compose-expr a -1) "}")
838 (if (math-tex-expr-is-flat a)
839 (list 'horiz "{( " (math-compose-expr a -1) " )}")
840 (list 'horiz "{left ( "
841 (math-compose-expr a -1)
842 " right )}")))
843 (list 'horiz "(" (math-compose-expr a 0) ")"))))
844 (t
845 (let ((rhs (math-compose-expr (nth 1 a) (nth 3 op))))
846 (list 'horiz
847 (let ((ops (if (string-match "\\`u\\([^a-zA-Z]\\)\\'"
848 (car op))
849 (substring (car op) 1)
850 (car op))))
851 (if (or (> (length ops) 1)
852 (not (math-comp-is-flat rhs)))
853 (concat ops " ")
854 ops))
855 rhs)))))
856 ((and (eq calc-language 'big)
857 (setq op (get (car a) 'math-compose-big))
858 (funcall op a prec)))
859 ((and (setq op (assq calc-language
860 '( ( nil . math-compose-normal )
861 ( flat . math-compose-normal )
862 ( big . math-compose-normal )
863 ( c . math-compose-c )
864 ( pascal . math-compose-pascal )
865 ( fortran . math-compose-fortran )
866 ( tex . math-compose-tex )
867 ( latex . math-compose-latex )
868 ( eqn . math-compose-eqn )
869 ( math . math-compose-math )
870 ( maple . math-compose-maple ))))
871 (setq op (get (car a) (cdr op)))
872 (funcall op a prec)))
873 (t
874 (let* ((func (car a))
875 (func2 (assq func '(( mod . calcFunc-makemod )
876 ( sdev . calcFunc-sdev )
877 ( + . calcFunc-add )
878 ( - . calcFunc-sub )
879 ( * . calcFunc-mul )
880 ( / . calcFunc-div )
881 ( % . calcFunc-mod )
882 ( ^ . calcFunc-pow )
883 ( neg . calcFunc-neg )
884 ( | . calcFunc-vconcat ))))
885 left right args)
886 (if func2
887 (setq func (cdr func2)))
888 (if (setq func2 (rassq func math-expr-function-mapping))
889 (setq func (car func2)))
890 (setq func (math-remove-dashes
891 (if (string-match
892 "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
893 (symbol-name func))
894 (math-match-substring (symbol-name func) 1)
895 (symbol-name func))))
896 (if (memq calc-language '(c fortran pascal maple))
897 (setq func (math-to-underscores func)))
898 (if (and (memq calc-language '(tex latex))
899 calc-language-option
900 (not (= calc-language-option 0))
901 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
902 (if (< (prefix-numeric-value calc-language-option) 0)
903 (setq func (format "\\%s" func))
904 (setq func (if (eq calc-language 'latex)
905 (format "\\text{%s}" func)
906 (format "\\hbox{%s}" func)))))
907 (if (and (eq calc-language 'eqn)
908 (string-match "[^']'+\\'" func))
909 (let ((n (- (length func) (match-beginning 0) 1)))
910 (setq func (substring func 0 (- n)))
911 (while (>= (setq n (1- n)) 0)
912 (setq func (concat func " prime")))))
913 (cond ((and (memq calc-language '(tex latex))
914 (or (> (length a) 2)
915 (not (math-tex-expr-is-flat (nth 1 a)))))
916 (setq left "\\left( "
917 right " \\right)"))
918 ((and (eq calc-language 'eqn)
919 (or (> (length a) 2)
920 (not (math-tex-expr-is-flat (nth 1 a)))))
921 (setq left "{left ( "
922 right " right )}"))
923 ((and (or (and (memq calc-language '(tex latex))
924 (eq (aref func 0) ?\\))
925 (and (eq calc-language 'eqn)
926 (memq (car a) math-eqn-special-funcs)))
927 (not (or
928 (string-match "\\hbox{" func)
929 (string-match "\\text{" func)))
930 (= (length a) 2)
931 (or (Math-realp (nth 1 a))
932 (memq (car (nth 1 a)) '(var *))))
933 (setq left (if (eq calc-language 'eqn) "~{" "{")
934 right "}"))
935 ((eq calc-language 'eqn)
936 (setq left " ( "
937 right " )"))
938 (t (setq left calc-function-open
939 right calc-function-close)))
940 (list 'horiz func left
941 (math-compose-vector (cdr a)
942 (if (eq calc-language 'eqn)
943 " , " ", ")
944 0)
945 right)))))))))
946
947
948 (defun math-prod-first-term (x)
949 (while (eq (car-safe x) '*)
950 (setq x (nth 1 x)))
951 x)
952
953 (defun math-prod-last-term (x)
954 (while (eq (car-safe x) '*)
955 (setq x (nth 2 x)))
956 x)
957
958 (defun math-compose-vector (a sep prec)
959 (if a
960 (cons 'horiz
961 (cons (list 'set math-compose-level)
962 (let ((c (list (math-compose-expr (car a) prec))))
963 (while (setq a (cdr a))
964 (setq c (cons (if (eq (car-safe (car a))
965 'calcFunc-bstring)
966 (let ((math-compose-level
967 (1- math-compose-level)))
968 (math-compose-expr (car a) -123))
969 (math-compose-expr (car a) prec))
970 (cons (list 'break math-compose-level)
971 (cons sep c)))))
972 (nreverse c))))
973 ""))
974
975 (defun math-vector-no-parens (a)
976 (or (cdr (cdr a))
977 (not (eq (car-safe (nth 1 a)) '*))))
978
979 (defun math-compose-matrix (a col cols base)
980 (let ((col 0)
981 (res nil))
982 (while (<= (setq col (1+ col)) cols)
983 (setq res (cons (cons math-comp-just
984 (cons base
985 (mapcar (function
986 (lambda (r)
987 (list 'horiz
988 (math-compose-expr
989 (nth col r)
990 math-comp-vector-prec)
991 (if (= col cols)
992 ""
993 (concat
994 math-comp-comma-spc " ")))))
995 a)))
996 res)))
997 (nreverse res)))
998
999 (defun math-compose-rows (a count first)
1000 (if (cdr a)
1001 (if (<= count 0)
1002 (if (< count 0)
1003 (math-compose-rows (cdr a) -1 nil)
1004 (cons (concat (if (memq calc-language '(tex latex)) " \\ldots" " ...")
1005 math-comp-comma)
1006 (math-compose-rows (cdr a) -1 nil)))
1007 (cons (list 'horiz
1008 (if first (concat math-comp-left-bracket " ") " ")
1009 (math-compose-expr (car a) math-comp-vector-prec)
1010 math-comp-comma)
1011 (math-compose-rows (cdr a) (1- count) nil)))
1012 (list (list 'horiz
1013 (if first (concat math-comp-left-bracket " ") " ")
1014 (math-compose-expr (car a) math-comp-vector-prec)
1015 (concat " " math-comp-right-bracket)))))
1016
1017 (defun math-compose-tex-matrix (a)
1018 (if (cdr a)
1019 (cons (append (math-compose-vector (cdr (car a)) " & " 0) '(" \\\\ "))
1020 (math-compose-tex-matrix (cdr a)))
1021 (list (math-compose-vector (cdr (car a)) " & " 0))))
1022
1023 (defun math-compose-eqn-matrix (a)
1024 (if a
1025 (cons
1026 (cond ((eq calc-matrix-just 'right) "rcol ")
1027 ((eq calc-matrix-just 'center) "ccol ")
1028 (t "lcol "))
1029 (cons
1030 (list 'break math-compose-level)
1031 (cons
1032 "{ "
1033 (cons
1034 (let ((math-compose-level (1+ math-compose-level)))
1035 (math-compose-vector (cdr (car a)) " above " 1000))
1036 (cons
1037 " } "
1038 (math-compose-eqn-matrix (cdr a)))))))
1039 nil))
1040
1041 (defun math-vector-is-string (a)
1042 (while (and (setq a (cdr a))
1043 (or (and (natnump (car a))
1044 (<= (car a) 255))
1045 (and (eq (car-safe (car a)) 'cplx)
1046 (natnump (nth 1 (car a)))
1047 (eq (nth 2 (car a)) 0)
1048 (<= (nth 1 (car a)) 255)))))
1049 (null a))
1050
1051 (defconst math-vector-to-string-chars '( ( ?\" . "\\\"" )
1052 ( ?\\ . "\\\\" )
1053 ( ?\a . "\\a" )
1054 ( ?\b . "\\b" )
1055 ( ?\e . "\\e" )
1056 ( ?\f . "\\f" )
1057 ( ?\n . "\\n" )
1058 ( ?\r . "\\r" )
1059 ( ?\t . "\\t" )
1060 ( ?\^? . "\\^?" )))
1061
1062 (defun math-vector-to-string (a &optional quoted)
1063 (setq a (concat (mapcar (function (lambda (x) (if (consp x) (nth 1 x) x)))
1064 (cdr a))))
1065 (if (string-match "[\000-\037\177\\\"]" a)
1066 (let ((p 0)
1067 (pat (if quoted "[\000-\037\177\\\"]" "[\000-\037\177]"))
1068 (codes (if quoted math-vector-to-string-chars '((?\^? . "^?"))))
1069 (fmt (if quoted "\\^%c" "^%c"))
1070 new)
1071 (while (setq p (string-match pat a p))
1072 (if (setq new (assq (aref a p) codes))
1073 (setq a (concat (substring a 0 p)
1074 (cdr new)
1075 (substring a (1+ p)))
1076 p (+ p (length (cdr new))))
1077 (setq a (concat (substring a 0 p)
1078 (format fmt (+ (aref a p) 64))
1079 (substring a (1+ p)))
1080 p (+ p 2))))))
1081 (if quoted
1082 (concat "\"" a "\"")
1083 a))
1084
1085
1086 (defun math-to-underscores (x)
1087 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x)
1088 (math-to-underscores
1089 (concat (math-match-substring x 1) "_" (math-match-substring x 2)))
1090 x))
1091
1092 (defun math-tex-expr-is-flat (a)
1093 (or (Math-integerp a)
1094 (memq (car a) '(float var))
1095 (and (memq (car a) '(+ - * neg))
1096 (progn
1097 (while (and (setq a (cdr a))
1098 (math-tex-expr-is-flat (car a))))
1099 (null a)))
1100 (and (memq (car a) '(^ calcFunc-subscr))
1101 (math-tex-expr-is-flat (nth 1 a)))))
1102
1103 (put 'calcFunc-log 'math-compose-big 'math-compose-log)
1104 (defun math-compose-log (a prec)
1105 (and (= (length a) 3)
1106 (list 'horiz
1107 (list 'subscr "log"
1108 (let ((calc-language 'flat))
1109 (math-compose-expr (nth 2 a) 1000)))
1110 "("
1111 (math-compose-expr (nth 1 a) 1000)
1112 ")")))
1113
1114 (put 'calcFunc-log10 'math-compose-big 'math-compose-log10)
1115 (defun math-compose-log10 (a prec)
1116 (and (= (length a) 2)
1117 (list 'horiz
1118 (list 'subscr "log" "10")
1119 "("
1120 (math-compose-expr (nth 1 a) 1000)
1121 ")")))
1122
1123 (put 'calcFunc-deriv 'math-compose-big 'math-compose-deriv)
1124 (put 'calcFunc-tderiv 'math-compose-big 'math-compose-deriv)
1125 (defun math-compose-deriv (a prec)
1126 (when (= (length a) 3)
1127 (math-compose-expr (list '/
1128 (list 'calcFunc-choriz
1129 (list 'vec
1130 '(calcFunc-string (vec ?d))
1131 (nth 1 a)))
1132 (list 'calcFunc-choriz
1133 (list 'vec
1134 '(calcFunc-string (vec ?d))
1135 (nth 2 a))))
1136 prec)))
1137
1138 (put 'calcFunc-sqrt 'math-compose-big 'math-compose-sqrt)
1139 (defun math-compose-sqrt (a prec)
1140 (when (= (length a) 2)
1141 (let* ((c (math-compose-expr (nth 1 a) 0))
1142 (a (math-comp-ascent c))
1143 (d (math-comp-descent c))
1144 (h (+ a d))
1145 (w (math-comp-width c)))
1146 (list 'vleft
1147 a
1148 (concat (if (= h 1) " " " ")
1149 (make-string (+ w 2) ?\_))
1150 (list 'horiz
1151 (if (= h 1)
1152 "V"
1153 (append (list 'vleft (1- a))
1154 (make-list (1- h) " |")
1155 '("\\|")))
1156 " "
1157 c)))))
1158
1159 (put 'calcFunc-choose 'math-compose-big 'math-compose-choose)
1160 (defun math-compose-choose (a prec)
1161 (let ((a1 (math-compose-expr (nth 1 a) 0))
1162 (a2 (math-compose-expr (nth 2 a) 0)))
1163 (list 'horiz
1164 "("
1165 (list 'vcent
1166 (math-comp-height a1)
1167 a1 " " a2)
1168 ")")))
1169
1170 (put 'calcFunc-integ 'math-compose-big 'math-compose-integ)
1171 (defun math-compose-integ (a prec)
1172 (and (memq (length a) '(3 5))
1173 (eq (car-safe (nth 2 a)) 'var)
1174 (let* ((parens (and (>= prec 196) (/= prec 1000)))
1175 (var (math-compose-expr (nth 2 a) 0))
1176 (over (and (eq (car-safe (nth 2 a)) 'var)
1177 (or (and (eq (car-safe (nth 1 a)) '/)
1178 (math-numberp (nth 1 (nth 1 a))))
1179 (and (eq (car-safe (nth 1 a)) '^)
1180 (math-looks-negp (nth 2 (nth 1 a)))))))
1181 (expr (math-compose-expr (if over
1182 (math-mul (nth 1 a)
1183 (math-build-var-name
1184 (format
1185 "d%s"
1186 (nth 1 (nth 2 a)))))
1187 (nth 1 a)) 185))
1188 (calc-language 'flat)
1189 (low (and (nth 3 a) (math-compose-expr (nth 3 a) 0)))
1190 (high (and (nth 4 a) (math-compose-expr (nth 4 a) 0))))
1191 (list 'horiz
1192 (if parens "(" "")
1193 (append (list 'vcent (if high 3 2))
1194 (and high (list (list 'horiz " " high)))
1195 '(" /"
1196 " | "
1197 " | "
1198 " | "
1199 "/ ")
1200 (and low (list (list 'horiz low " "))))
1201 expr
1202 (if over
1203 ""
1204 (list 'horiz " d" var))
1205 (if parens ")" "")))))
1206
1207 (put 'calcFunc-sum 'math-compose-big 'math-compose-sum)
1208 (defun math-compose-sum (a prec)
1209 (and (memq (length a) '(3 5 6))
1210 (let* ((expr (math-compose-expr (nth 1 a) 185))
1211 (calc-language 'flat)
1212 (var (math-compose-expr (nth 2 a) 0))
1213 (low (and (nth 3 a) (math-compose-expr (nth 3 a) 0)))
1214 (high (and (nth 4 a) (math-compose-vector (nthcdr 4 a) ", " 0))))
1215 (list 'horiz
1216 (if (memq prec '(180 201)) "(" "")
1217 (append (list 'vcent (if high 3 2))
1218 (and high (list high))
1219 '("---- "
1220 "\\ "
1221 " > "
1222 "/ "
1223 "---- ")
1224 (if low
1225 (list (list 'horiz var " = " low))
1226 (list var)))
1227 (if (memq (car-safe (nth 1 a)) '(calcFunc-sum calcFunc-prod))
1228 " " "")
1229 expr
1230 (if (memq prec '(180 201)) ")" "")))))
1231
1232 (put 'calcFunc-prod 'math-compose-big 'math-compose-prod)
1233 (defun math-compose-prod (a prec)
1234 (and (memq (length a) '(3 5 6))
1235 (let* ((expr (math-compose-expr (nth 1 a) 198))
1236 (calc-language 'flat)
1237 (var (math-compose-expr (nth 2 a) 0))
1238 (low (and (nth 3 a) (math-compose-expr (nth 3 a) 0)))
1239 (high (and (nth 4 a) (math-compose-vector (nthcdr 4 a) ", " 0))))
1240 (list 'horiz
1241 (if (memq prec '(196 201)) "(" "")
1242 (append (list 'vcent (if high 3 2))
1243 (and high (list high))
1244 '("----- "
1245 " | | "
1246 " | | "
1247 " | | ")
1248 (if low
1249 (list (list 'horiz var " = " low))
1250 (list var)))
1251 (if (memq (car-safe (nth 1 a)) '(calcFunc-sum calcFunc-prod))
1252 " " "")
1253 expr
1254 (if (memq prec '(196 201)) ")" "")))))
1255
1256 ;; The variables math-svo-c, math-svo-wid and math-svo-off are local
1257 ;; to math-stack-value-offset in calc.el, but are used by
1258 ;; math-stack-value-offset-fancy, which is called by math-stack-value-offset..
1259 (defvar math-svo-c)
1260 (defvar math-svo-wid)
1261 (defvar math-svo-off)
1262
1263 (defun math-stack-value-offset-fancy ()
1264 (let ((cwid (+ (math-comp-width math-svo-c))))
1265 (cond ((eq calc-display-just 'right)
1266 (if calc-display-origin
1267 (setq math-svo-wid (max calc-display-origin 5))
1268 (if (integerp calc-line-breaking)
1269 (setq math-svo-wid calc-line-breaking)))
1270 (setq math-svo-off (- math-svo-wid cwid
1271 (max (- (length calc-right-label)
1272 (if (and (integerp calc-line-breaking)
1273 calc-display-origin)
1274 (max (- calc-line-breaking
1275 calc-display-origin)
1276 0)
1277 0))
1278 0))))
1279 (t
1280 (if calc-display-origin
1281 (progn
1282 (setq math-svo-off (- calc-display-origin (/ cwid 2)))
1283 (if (integerp calc-line-breaking)
1284 (setq math-svo-off (min math-svo-off (- calc-line-breaking cwid
1285 (length calc-right-label)))))
1286 (if (>= math-svo-off 0)
1287 (setq math-svo-wid (max math-svo-wid (+ math-svo-off cwid)))))
1288 (if (integerp calc-line-breaking)
1289 (setq math-svo-wid calc-line-breaking))
1290 (setq math-svo-off (/ (- math-svo-wid cwid) 2)))))
1291 (and (integerp calc-line-breaking)
1292 (or (< math-svo-off 0)
1293 (and calc-display-origin
1294 (> calc-line-breaking calc-display-origin)))
1295 (setq math-svo-wid calc-line-breaking))))
1296
1297
1298 ;;; Convert a composition to string form, with embedded \n's if necessary.
1299
1300 (defun math-composition-to-string (c &optional width)
1301 (or width (setq width (calc-window-width)))
1302 (if calc-display-raw
1303 (math-comp-to-string-raw c 0)
1304 (if (math-comp-is-flat c)
1305 (math-comp-to-string-flat c width)
1306 (math-vert-comp-to-string
1307 (math-comp-simplify c width)))))
1308
1309 (defvar math-comp-buf-string (make-vector 10 ""))
1310 (defvar math-comp-buf-margin (make-vector 10 0))
1311 (defvar math-comp-buf-level (make-vector 10 0))
1312 (defun math-comp-is-flat (c) ; check if c's height is 1.
1313 (cond ((not (consp c)) t)
1314 ((memq (car c) '(set break)) t)
1315 ((eq (car c) 'horiz)
1316 (while (and (setq c (cdr c))
1317 (math-comp-is-flat (car c))))
1318 (null c))
1319 ((memq (car c) '(vleft vcent vright))
1320 (and (= (length c) 3)
1321 (= (nth 1 c) 0)
1322 (math-comp-is-flat (nth 2 c))))
1323 ((eq (car c) 'tag)
1324 (math-comp-is-flat (nth 2 c)))
1325 (t nil)))
1326
1327
1328 ;;; Convert a one-line composition to a string. Break into multiple
1329 ;;; lines if necessary, choosing break points according to the structure
1330 ;;; of the formula.
1331
1332 ;; The variables math-comp-full-width, math-comp-highlight, math-comp-word,
1333 ;; math-comp-level, math-comp-margin and math-comp-buf are local to
1334 ;; math-comp-to-string-flat, but are used by math-comp-to-string-flat-term,
1335 ;; which is called by math-comp-to-string-flat.
1336 ;; math-comp-highlight and math-comp-buf are also local to
1337 ;; math-comp-simplify-term and math-comp-simplify respectively, but are used
1338 ;; by math-comp-add-string.
1339 (defvar math-comp-full-width)
1340 (defvar math-comp-highlight)
1341 (defvar math-comp-word)
1342 (defvar math-comp-level)
1343 (defvar math-comp-margin)
1344 (defvar math-comp-buf)
1345 ;; The variable math-comp-pos is local to math-comp-to-string-flat, but
1346 ;; is used by math-comp-to-string-flat-term and math-comp-sel-first-term,
1347 ;; which are called by math-comp-to-string-flat.
1348 (defvar math-comp-pos)
1349
1350 (defun math-comp-to-string-flat (c math-comp-full-width)
1351 (if math-comp-sel-hpos
1352 (let ((math-comp-pos 0))
1353 (math-comp-sel-flat-term c))
1354 (let ((math-comp-buf "")
1355 (math-comp-word "")
1356 (math-comp-pos 0)
1357 (math-comp-margin 0)
1358 (math-comp-highlight (and math-comp-selected calc-show-selections))
1359 (math-comp-level -1))
1360 (math-comp-to-string-flat-term '(set -1 0))
1361 (math-comp-to-string-flat-term c)
1362 (math-comp-to-string-flat-term '(break -1))
1363 (let ((str (aref math-comp-buf-string 0))
1364 (prefix ""))
1365 (and (> (length str) 0) (= (aref str 0) ? )
1366 (> (length math-comp-buf) 0)
1367 (let ((k (length math-comp-buf)))
1368 (while (not (= (aref math-comp-buf (setq k (1- k))) ?\n)))
1369 (aset math-comp-buf k ? )
1370 (if (and (< (1+ k) (length math-comp-buf))
1371 (= (aref math-comp-buf (1+ k)) ? ))
1372 (progn
1373 (aset math-comp-buf (1+ k) ?\n)
1374 (setq prefix " "))
1375 (setq prefix "\n"))))
1376 (concat math-comp-buf prefix str)))))
1377
1378 (defun math-comp-to-string-flat-term (c)
1379 (cond ((not (consp c))
1380 (if math-comp-highlight
1381 (setq c (math-comp-highlight-string c)))
1382 (setq math-comp-word (if (= (length math-comp-word) 0) c
1383 (concat math-comp-word c))
1384 math-comp-pos (+ math-comp-pos (length c))))
1385
1386 ((eq (car c) 'horiz)
1387 (while (setq c (cdr c))
1388 (math-comp-to-string-flat-term (car c))))
1389
1390 ((eq (car c) 'set)
1391 (if (nth 1 c)
1392 (progn
1393 (setq math-comp-level (1+ math-comp-level))
1394 (if (>= math-comp-level (length math-comp-buf-string))
1395 (setq math-comp-buf-string (vconcat math-comp-buf-string
1396 math-comp-buf-string)
1397 math-comp-buf-margin (vconcat math-comp-buf-margin
1398 math-comp-buf-margin)
1399 math-comp-buf-level (vconcat math-comp-buf-level
1400 math-comp-buf-level)))
1401 (aset math-comp-buf-string math-comp-level "")
1402 (aset math-comp-buf-margin math-comp-level (+ math-comp-pos
1403 (or (nth 2 c) 0)))
1404 (aset math-comp-buf-level math-comp-level (nth 1 c)))))
1405
1406 ((eq (car c) 'break)
1407 (if (not calc-line-breaking)
1408 (setq math-comp-buf (concat math-comp-buf math-comp-word)
1409 math-comp-word "")
1410 (let ((i 0) str)
1411 (if (and (> math-comp-pos math-comp-full-width)
1412 (progn
1413 (while (progn
1414 (setq str (aref math-comp-buf-string i))
1415 (and (= (length str) 0) (< i math-comp-level)))
1416 (setq i (1+ i)))
1417 (or (> (length str) 0) (> (length math-comp-buf) 0))))
1418 (let ((prefix "") mrg wid)
1419 (setq mrg (aref math-comp-buf-margin i))
1420 (if (> mrg 12) ; indenting too far, go back to far left
1421 (let ((j i) (new (if calc-line-numbering 5 1)))
1422 '(while (<= j math-comp-level)
1423 (aset math-comp-buf-margin j
1424 (+ (aref math-comp-buf-margin j) (- new mrg)))
1425 (setq j (1+ j)))
1426 (setq mrg new)))
1427 (setq wid (+ (length str) math-comp-margin))
1428 (and (> (length str) 0) (= (aref str 0) ? )
1429 (> (length math-comp-buf) 0)
1430 (let ((k (length math-comp-buf)))
1431 (while (not (= (aref math-comp-buf (setq k (1- k))) ?\n)))
1432 (aset math-comp-buf k ? )
1433 (if (and (< (1+ k) (length math-comp-buf))
1434 (= (aref math-comp-buf (1+ k)) ? ))
1435 (progn
1436 (aset math-comp-buf (1+ k) ?\n)
1437 (setq prefix " "))
1438 (setq prefix "\n"))))
1439 (setq math-comp-buf (concat math-comp-buf prefix str "\n"
1440 (make-string mrg ? ))
1441 math-comp-pos (+ math-comp-pos (- mrg wid))
1442 math-comp-margin mrg)
1443 (aset math-comp-buf-string i "")
1444 (while (<= (setq i (1+ i)) math-comp-level)
1445 (if (> (aref math-comp-buf-margin i) wid)
1446 (aset math-comp-buf-margin i
1447 (+ (aref math-comp-buf-margin i)
1448 (- mrg wid))))))))
1449 (if (and (= (nth 1 c) (aref math-comp-buf-level math-comp-level))
1450 (< math-comp-pos (+ (aref math-comp-buf-margin math-comp-level) 2)))
1451 () ; avoid stupid breaks, e.g., "1 +\n really_long_expr"
1452 (let ((str (aref math-comp-buf-string math-comp-level)))
1453 (setq str (if (= (length str) 0)
1454 math-comp-word
1455 (concat str math-comp-word))
1456 math-comp-word "")
1457 (while (< (nth 1 c) (aref math-comp-buf-level math-comp-level))
1458 (setq math-comp-level (1- math-comp-level))
1459 (or (= (length (aref math-comp-buf-string math-comp-level)) 0)
1460 (setq str (concat (aref math-comp-buf-string math-comp-level)
1461 str))))
1462 (aset math-comp-buf-string math-comp-level str)))))
1463
1464 ((eq (car c) 'tag)
1465 (cond ((eq (nth 1 c) math-comp-selected)
1466 (let ((math-comp-highlight (not calc-show-selections)))
1467 (math-comp-to-string-flat-term (nth 2 c))))
1468 ((eq (nth 1 c) t)
1469 (let ((math-comp-highlight nil))
1470 (math-comp-to-string-flat-term (nth 2 c))))
1471 (t (math-comp-to-string-flat-term (nth 2 c)))))
1472
1473 (t (math-comp-to-string-flat-term (nth 2 c)))))
1474
1475 (defun math-comp-highlight-string (s)
1476 (setq s (copy-sequence s))
1477 (let ((i (length s)))
1478 (while (>= (setq i (1- i)) 0)
1479 (or (memq (aref s i) '(32 ?\n))
1480 (aset s i (if calc-show-selections ?\. ?\#)))))
1481 s)
1482
1483
1484 ;; The variable math-comp-sel-tag is local to calc-find-selected-part
1485 ;; in calc-sel.el, but is used by math-comp-sel-flat-term and
1486 ;; math-comp-add-string-sel, which are called (indirectly) by
1487 ;; calc-find-selected-part.
1488 (defvar math-comp-sel-tag)
1489
1490 (defun math-comp-sel-flat-term (c)
1491 (cond ((not (consp c))
1492 (setq math-comp-pos (+ math-comp-pos (length c))))
1493 ((memq (car c) '(set break)))
1494 ((eq (car c) 'horiz)
1495 (while (and (setq c (cdr c)) (< math-comp-sel-cpos 1000000))
1496 (math-comp-sel-flat-term (car c))))
1497 ((eq (car c) 'tag)
1498 (if (<= math-comp-pos math-comp-sel-cpos)
1499 (progn
1500 (math-comp-sel-flat-term (nth 2 c))
1501 (if (> math-comp-pos math-comp-sel-cpos)
1502 (setq math-comp-sel-tag c
1503 math-comp-sel-cpos 1000000)))
1504 (math-comp-sel-flat-term (nth 2 c))))
1505 (t (math-comp-sel-flat-term (nth 2 c)))))
1506
1507
1508 ;;; Simplify a composition to a canonical form consisting of
1509 ;;; (vleft n "string" "string" "string" ...)
1510 ;;; where 0 <= n < number-of-strings.
1511
1512 ;; The variables math-comp-base, math-comp-hgt, math-comp-tag,
1513 ;; math-comp-hpos and math-comp-vpos are local to math-comp-simplify,
1514 ;; but are used by math-comp-add-string (math-comp-base, math-comp-hgt),
1515 ;; math-comp-add-string-sel (math-comp-tag) and math-comp-simplify-term
1516 ;; (math-comp-tag, math-comp-vpos, math-comp-hpos), which are called by
1517 ;; math-comp-simplify.
1518 (defvar math-comp-base)
1519 (defvar math-comp-hgt)
1520 (defvar math-comp-tag)
1521 (defvar math-comp-hpos)
1522 (defvar math-comp-vpos)
1523
1524 (defun math-comp-simplify (c full-width)
1525 (let ((math-comp-buf (list ""))
1526 (math-comp-base 0)
1527 (math-comp-hgt 1)
1528 (math-comp-hpos 0)
1529 (math-comp-vpos 0)
1530 (math-comp-highlight (and math-comp-selected calc-show-selections))
1531 (math-comp-tag nil))
1532 (math-comp-simplify-term c)
1533 (cons 'vleft (cons math-comp-base math-comp-buf))))
1534
1535 (defun math-comp-add-string (s h v)
1536 (and (> (length s) 0)
1537 (let ((vv (+ v math-comp-base)))
1538 (if math-comp-sel-hpos
1539 (math-comp-add-string-sel h vv (length s) 1)
1540 (if (< vv 0)
1541 (setq math-comp-buf (nconc (make-list (- vv) "") math-comp-buf)
1542 math-comp-base (- v)
1543 math-comp-hgt (- math-comp-hgt vv)
1544 vv 0)
1545 (if (>= vv math-comp-hgt)
1546 (setq math-comp-buf (nconc math-comp-buf
1547 (make-list (1+ (- vv math-comp-hgt)) ""))
1548 math-comp-hgt (1+ vv))))
1549 (let ((str (nthcdr vv math-comp-buf)))
1550 (setcar str (concat (car str)
1551 (make-string (- h (length (car str))) 32)
1552 (if math-comp-highlight
1553 (math-comp-highlight-string s)
1554 s))))))))
1555
1556 (defun math-comp-add-string-sel (x y w h)
1557 (if (and (<= y math-comp-sel-vpos)
1558 (> (+ y h) math-comp-sel-vpos)
1559 (<= x math-comp-sel-hpos)
1560 (> (+ x w) math-comp-sel-hpos))
1561 (setq math-comp-sel-tag math-comp-tag
1562 math-comp-sel-vpos 10000)))
1563
1564 (defun math-comp-simplify-term (c)
1565 (cond ((stringp c)
1566 (math-comp-add-string c math-comp-hpos math-comp-vpos)
1567 (setq math-comp-hpos (+ math-comp-hpos (length c))))
1568 ((memq (car c) '(set break))
1569 nil)
1570 ((eq (car c) 'horiz)
1571 (while (setq c (cdr c))
1572 (math-comp-simplify-term (car c))))
1573 ((memq (car c) '(vleft vcent vright))
1574 (let* ((math-comp-vpos (+ (- math-comp-vpos (nth 1 c))
1575 (1- (math-comp-ascent (nth 2 c)))))
1576 (widths (mapcar 'math-comp-width (cdr (cdr c))))
1577 (maxwid (apply 'max widths))
1578 (bias (cond ((eq (car c) 'vleft) 0)
1579 ((eq (car c) 'vcent) 1)
1580 (t 2))))
1581 (setq c (cdr c))
1582 (while (setq c (cdr c))
1583 (if (eq (car-safe (car c)) 'rule)
1584 (math-comp-add-string (make-string maxwid (nth 1 (car c)))
1585 math-comp-hpos math-comp-vpos)
1586 (let ((math-comp-hpos (+ math-comp-hpos (/ (* bias (- maxwid
1587 (car widths)))
1588 2))))
1589 (math-comp-simplify-term (car c))))
1590 (and (cdr c)
1591 (setq math-comp-vpos (+ math-comp-vpos
1592 (+ (math-comp-descent (car c))
1593 (math-comp-ascent (nth 1 c))))
1594 widths (cdr widths))))
1595 (setq math-comp-hpos (+ math-comp-hpos maxwid))))
1596 ((eq (car c) 'supscr)
1597 (let* ((asc (or 1 (math-comp-ascent (nth 1 c))))
1598 (desc (math-comp-descent (nth 2 c)))
1599 (oldh (prog1
1600 math-comp-hpos
1601 (math-comp-simplify-term (nth 1 c))))
1602 (math-comp-vpos (- math-comp-vpos (+ asc desc))))
1603 (math-comp-simplify-term (nth 2 c))
1604 (if math-comp-sel-hpos
1605 (math-comp-add-string-sel oldh
1606 (- math-comp-vpos
1607 -1
1608 (math-comp-ascent (nth 2 c)))
1609 (- math-comp-hpos oldh)
1610 (math-comp-height c)))))
1611 ((eq (car c) 'subscr)
1612 (let* ((asc (math-comp-ascent (nth 2 c)))
1613 (desc (math-comp-descent (nth 1 c)))
1614 (oldv math-comp-vpos)
1615 (oldh (prog1
1616 math-comp-hpos
1617 (math-comp-simplify-term (nth 1 c))))
1618 (math-comp-vpos (+ math-comp-vpos (+ asc desc))))
1619 (math-comp-simplify-term (nth 2 c))
1620 (if math-comp-sel-hpos
1621 (math-comp-add-string-sel oldh oldv
1622 (- math-comp-hpos oldh)
1623 (math-comp-height c)))))
1624 ((eq (car c) 'tag)
1625 (cond ((eq (nth 1 c) math-comp-selected)
1626 (let ((math-comp-highlight (not calc-show-selections)))
1627 (math-comp-simplify-term (nth 2 c))))
1628 ((eq (nth 1 c) t)
1629 (let ((math-comp-highlight nil))
1630 (math-comp-simplify-term (nth 2 c))))
1631 (t (let ((math-comp-tag c))
1632 (math-comp-simplify-term (nth 2 c))))))))
1633
1634
1635 ;;; Measuring a composition.
1636
1637 (defun math-comp-first-char (c)
1638 (cond ((stringp c)
1639 (and (> (length c) 0)
1640 (elt c 0)))
1641 ((memq (car c) '(horiz subscr supscr))
1642 (while (and (setq c (cdr c))
1643 (math-comp-is-null (car c))))
1644 (and c (math-comp-first-char (car c))))
1645 ((eq (car c) 'tag)
1646 (math-comp-first-char (nth 2 c)))))
1647
1648 (defun math-comp-first-string (c)
1649 (cond ((stringp c)
1650 (and (> (length c) 0)
1651 c))
1652 ((eq (car c) 'horiz)
1653 (while (and (setq c (cdr c))
1654 (math-comp-is-null (car c))))
1655 (and c (math-comp-first-string (car c))))
1656 ((eq (car c) 'tag)
1657 (math-comp-first-string (nth 2 c)))))
1658
1659 (defun math-comp-last-char (c)
1660 (cond ((stringp c)
1661 (and (> (length c) 0)
1662 (elt c (1- (length c)))))
1663 ((eq (car c) 'horiz)
1664 (let ((c (reverse (cdr c))))
1665 (while (and c (math-comp-is-null (car c)))
1666 (setq c (cdr c)))
1667 (and c (math-comp-last-char (car c)))))
1668 ((eq (car c) 'tag)
1669 (math-comp-last-char (nth 2 c)))))
1670
1671 (defun math-comp-is-null (c)
1672 (cond ((stringp c) (= (length c) 0))
1673 ((memq (car c) '(horiz subscr supscr))
1674 (while (and (setq c (cdr c))
1675 (math-comp-is-null (car c))))
1676 (null c))
1677 ((eq (car c) 'tag)
1678 (math-comp-is-null (nth 2 c)))
1679 ((memq (car c) '(set break)) t)))
1680
1681 (defun math-comp-width (c)
1682 (cond ((not (consp c)) (length c))
1683 ((memq (car c) '(horiz subscr supscr))
1684 (let ((accum 0))
1685 (while (setq c (cdr c))
1686 (setq accum (+ accum (math-comp-width (car c)))))
1687 accum))
1688 ((memq (car c) '(vcent vleft vright))
1689 (setq c (cdr c))
1690 (let ((accum 0))
1691 (while (setq c (cdr c))
1692 (setq accum (max accum (math-comp-width (car c)))))
1693 accum))
1694 ((eq (car c) 'tag)
1695 (math-comp-width (nth 2 c)))
1696 (t 0)))
1697
1698 (defun math-comp-height (c)
1699 (if (stringp c)
1700 1
1701 (+ (math-comp-ascent c) (math-comp-descent c))))
1702
1703 (defun math-comp-ascent (c)
1704 (cond ((not (consp c)) 1)
1705 ((eq (car c) 'horiz)
1706 (let ((accum 0))
1707 (while (setq c (cdr c))
1708 (setq accum (max accum (math-comp-ascent (car c)))))
1709 accum))
1710 ((memq (car c) '(vcent vleft vright))
1711 (if (> (nth 1 c) 0) (1+ (nth 1 c)) 1))
1712 ((eq (car c) 'supscr)
1713 (max (math-comp-ascent (nth 1 c)) (1+ (math-comp-height (nth 2 c)))))
1714 ((eq (car c) 'subscr)
1715 (math-comp-ascent (nth 1 c)))
1716 ((eq (car c) 'tag)
1717 (math-comp-ascent (nth 2 c)))
1718 (t 1)))
1719
1720 (defun math-comp-descent (c)
1721 (cond ((not (consp c)) 0)
1722 ((eq (car c) 'horiz)
1723 (let ((accum 0))
1724 (while (setq c (cdr c))
1725 (setq accum (max accum (math-comp-descent (car c)))))
1726 accum))
1727 ((memq (car c) '(vcent vleft vright))
1728 (let ((accum (- (nth 1 c))))
1729 (setq c (cdr c))
1730 (while (setq c (cdr c))
1731 (setq accum (+ accum (math-comp-height (car c)))))
1732 (max (1- accum) 0)))
1733 ((eq (car c) 'supscr)
1734 (math-comp-descent (nth 1 c)))
1735 ((eq (car c) 'subscr)
1736 (+ (math-comp-descent (nth 1 c)) (math-comp-height (nth 2 c))))
1737 ((eq (car c) 'tag)
1738 (math-comp-descent (nth 2 c)))
1739 (t 0)))
1740
1741 (defun calcFunc-cwidth (a &optional prec)
1742 (if (and prec (not (integerp prec))) (math-reject-arg prec 'fixnump))
1743 (math-comp-width (math-compose-expr a (or prec 0))))
1744
1745 (defun calcFunc-cheight (a &optional prec)
1746 (if (and prec (not (integerp prec))) (math-reject-arg prec 'fixnump))
1747 (if (and (memq (car a) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace))
1748 (memq (length a) '(2 3))
1749 (eq (nth 1 a) 0))
1750 0
1751 (math-comp-height (math-compose-expr a (or prec 0)))))
1752
1753 (defun calcFunc-cascent (a &optional prec)
1754 (if (and prec (not (integerp prec))) (math-reject-arg prec 'fixnump))
1755 (if (and (memq (car a) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace))
1756 (memq (length a) '(2 3))
1757 (eq (nth 1 a) 0))
1758 0
1759 (math-comp-ascent (math-compose-expr a (or prec 0)))))
1760
1761 (defun calcFunc-cdescent (a &optional prec)
1762 (if (and prec (not (integerp prec))) (math-reject-arg prec 'fixnump))
1763 (math-comp-descent (math-compose-expr a (or prec 0))))
1764
1765
1766 ;;; Convert a simplified composition into string form.
1767
1768 (defun math-vert-comp-to-string (c)
1769 (if (stringp c)
1770 c
1771 (math-vert-comp-to-string-step (cdr (cdr c)))))
1772
1773 (defun math-vert-comp-to-string-step (c)
1774 (if (cdr c)
1775 (concat (car c) "\n" (math-vert-comp-to-string-step (cdr c)))
1776 (car c)))
1777
1778
1779 ;;; Convert a composition to a string in "raw" form (for debugging).
1780
1781 (defun math-comp-to-string-raw (c indent)
1782 (cond ((or (not (consp c)) (eq (car c) 'set))
1783 (prin1-to-string c))
1784 ((null (cdr c))
1785 (concat "(" (symbol-name (car c)) ")"))
1786 (t
1787 (let ((next-indent (+ indent 2 (length (symbol-name (car c))))))
1788 (concat "("
1789 (symbol-name (car c))
1790 " "
1791 (math-comp-to-string-raw (nth 1 c) next-indent)
1792 (math-comp-to-string-raw-step (cdr (cdr c))
1793 next-indent)
1794 ")")))))
1795
1796 (defun math-comp-to-string-raw-step (cl indent)
1797 (if cl
1798 (concat "\n"
1799 (make-string indent 32)
1800 (math-comp-to-string-raw (car cl) indent)
1801 (math-comp-to-string-raw-step (cdr cl) indent))
1802 ""))
1803
1804 (provide 'calccomp)
1805
1806 ;;; arch-tag: 7c45d10a-a286-4dab-af49-7ae8989fbf78
1807 ;;; calccomp.el ends here