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