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