1 ;;; calccomp.el --- composition functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
34 (defconst math-eqn-special-funcs
36 calcFunc-ln calcFunc-exp
37 calcFunc-sin calcFunc-cos calcFunc-tan
38 calcFunc-sec calcFunc-csc calcFunc-cot
39 calcFunc-sinh calcFunc-cosh calcFunc-tanh
40 calcFunc-sech calcFunc-csch calcFunc-coth
41 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
42 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh
))
44 ;;; A "composition" has one of the following forms:
46 ;;; "string" A literal string
48 ;;; (horiz C1 C2 ...) Horizontally abutted sub-compositions
50 ;;; (set LEVEL OFF) Set left margin + offset for line-break level
51 ;;; (break LEVEL) A potential line-break point
53 ;;; (vleft N C1 C2 ...) Vertically stacked, left-justified sub-comps
54 ;;; (vcent N C1 C2 ...) Vertically stacked, centered sub-comps
55 ;;; (vright N C1 C2 ...) Vertically stacked, right-justified sub-comps
56 ;;; N specifies baseline of the stack, 0=top line.
58 ;;; (supscr C1 C2) Composition C1 with superscript C2
59 ;;; (subscr C1 C2) Composition C1 with subscript C2
60 ;;; (rule X) Horizontal line of X, full width of enclosing comp
62 ;;; (tag X C) Composition C corresponds to sub-expression X
64 ;; math-comp-just and math-comp-comma-spc are local to
65 ;; math-compose-expr, but are used by math-compose-matrix, which is
66 ;; called by math-compose-expr
67 (defvar math-comp-just
)
68 (defvar math-comp-comma-spc
)
70 ;; math-comp-vector-prec is local to math-compose-expr, but is used by
71 ;; math-compose-matrix and math-compose-rows, which are called by
73 (defvar math-comp-vector-prec
)
75 ;; math-comp-left-bracket, math-comp-right-bracket and math-comp-comma are
76 ;; local to math-compose-expr, but are used by math-compose-rows, which is
77 ;; called by math-compose-expr.
78 (defvar math-comp-left-bracket
)
79 (defvar math-comp-right-bracket
)
80 (defvar math-comp-comma
)
83 (defun math-compose-expr (a prec
)
84 (let ((math-compose-level (1+ math-compose-level
))
87 ((or (and (eq a math-comp-selected
) a
)
89 (not (eq math-comp-tagged a
))))
90 (let ((math-comp-selected nil
))
91 (and math-comp-tagged
(setq math-comp-tagged a
))
92 (list 'tag a
(math-compose-expr a prec
))))
93 ((and (not (consp a
)) (not (integerp a
)))
94 (concat "'" (prin1-to-string a
)))
95 ((setq spfn
(assq (car-safe a
) math-expr-special-function-mapping
))
96 (setq spfn
(cdr spfn
))
97 (funcall (car spfn
) a spfn
))
99 (if (or (eq (car-safe a
) 'frac
)
100 (and (nth 1 calc-frac-format
) (Math-integerp a
)))
101 (if (memq calc-language
'(tex latex eqn math maple c fortran pascal
))
102 (let ((aa (math-adjust-fraction a
))
103 (calc-frac-format nil
))
104 (math-compose-expr (list '/
105 (if (memq calc-language
'(c fortran
))
106 (math-float (nth 1 aa
))
109 (if (and (eq calc-language
'big
)
110 (= (length (car calc-frac-format
)) 1))
111 (let* ((aa (math-adjust-fraction a
))
112 (calc-frac-format nil
)
113 (math-radix-explicit-format nil
)
115 (if (math-negp (nth 1 aa
))
119 (math-abs (nth 1 aa
)))
121 (math-format-number (nth 2 aa
))))))
122 (if (= calc-number-radix
10)
126 (int-to-string calc-number-radix
)))))
127 (math-format-number a
)))
128 (if (not (eq calc-language
'big
))
129 (math-format-number a prec
)
130 (if (memq (car-safe a
) '(cplx polar
))
131 (if (math-zerop (nth 2 a
))
132 (math-compose-expr (nth 1 a
) prec
)
134 (math-compose-expr (nth 1 a
) 0)
135 (if (eq (car a
) 'cplx
) ", " "; ")
136 (math-compose-expr (nth 2 a
) 0) ")"))
137 (if (or (= calc-number-radix
10)
139 (and calc-group-digits
140 (not (assoc calc-group-char
'((",") (" "))))))
141 (math-format-number a prec
)
142 (let ((s (math-format-number a prec
))
144 (while (string-match (if (> calc-number-radix
14)
145 "\\([0-9]+\\)#\\([0-9a-zA-Z., ]+\\)"
146 "\\([0-9]+\\)#\\([0-9a-dA-D., ]+\\)")
148 (setq c
(nconc c
(list (substring s
0 (match-beginning 0))
150 (math-match-substring s
2)
151 (math-match-substring s
1))))
152 s
(substring s
(match-end 0))))
154 "\\*\\([0-9.]+\\)\\^\\(-?[0-9]+\\)\\()?\\)\\'" s
)
156 (substring s
0 (match-beginning 0)) " "
158 (math-match-substring s
1)
159 (math-match-substring s
2))
160 (math-match-substring s
3))))
161 (if c
(cons 'horiz
(nconc c
(list s
))) s
)))))))
162 ((and (get (car a
) 'math-compose-forms
)
163 (not (eq calc-language
'unform
))
164 (let ((comps (get (car a
) 'math-compose-forms
))
166 (or (and (setq temp
(assq calc-language comps
))
167 (or (and (setq temp2
(assq (1- (length a
)) (cdr temp
)))
168 (setq temp
(apply (cdr temp2
) (cdr a
)))
169 (math-compose-expr temp prec
))
170 (and (setq temp2
(assq nil
(cdr temp
)))
171 (funcall (cdr temp2
) a
))))
172 (and (setq temp
(assq nil comps
))
173 (or (and (setq temp2
(assq (1- (length a
)) (cdr temp
)))
174 (setq temp
(apply (cdr temp2
) (cdr a
)))
175 (math-compose-expr temp prec
))
176 (and (setq temp2
(assq nil
(cdr temp
)))
177 (funcall (cdr temp2
) a
))))))))
179 (let* ((math-comp-left-bracket (if calc-vector-brackets
180 (substring calc-vector-brackets
0 1) ""))
181 (math-comp-right-bracket (if calc-vector-brackets
182 (substring calc-vector-brackets
1 2) ""))
183 (inner-brackets (memq 'R calc-matrix-brackets
))
184 (outer-brackets (memq 'O calc-matrix-brackets
))
185 (row-commas (memq 'C calc-matrix-brackets
))
186 (math-comp-comma-spc (or calc-vector-commas
" "))
187 (math-comp-comma (or calc-vector-commas
""))
188 (math-comp-vector-prec (if (or (and calc-vector-commas
189 (math-vector-no-parens a
))
190 (memq 'P calc-matrix-brackets
)) 0 1000))
191 (math-comp-just (cond ((eq calc-matrix-just
'right
) 'vright
)
192 ((eq calc-matrix-just
'center
) 'vcent
)
194 (break calc-break-vectors
))
195 (if (and (memq calc-language
'(nil big
))
196 (not calc-break-vectors
)
197 (math-matrixp a
) (not (math-matrixp (nth 1 a
)))
198 (or calc-full-vectors
199 (and (< (length a
) 7) (< (length (nth 1 a
)) 7))
200 (progn (setq break t
) nil
)))
202 (setq math-comp-vector-prec
(if (or (and calc-vector-commas
203 (math-vector-no-parens
205 (memq 'P calc-matrix-brackets
))
209 (concat math-comp-left-bracket math-comp-left-bracket
" ")
210 (math-compose-vector (cdr (nth 1 a
)) (concat math-comp-comma
" ")
211 math-comp-vector-prec
)
212 (concat " " math-comp-right-bracket math-comp-right-bracket
))
213 (let* ((rows (1- (length a
)))
214 (cols (1- (length (nth 1 a
))))
215 (base (/ (1- rows
) 2))
216 (calc-language 'flat
))
218 (list (append '(vleft)
220 (list (concat (and outer-brackets
221 (concat math-comp-left-bracket
224 (concat math-comp-left-bracket
227 (concat (and outer-brackets
231 math-comp-left-bracket
233 (math-compose-matrix (cdr a
) 1 cols base
)
234 (list (append '(vleft)
239 math-comp-right-bracket
242 (if (and outer-brackets
248 math-comp-right-bracket
))
252 math-comp-right-bracket
)))))))))
253 (if (and calc-display-strings
255 (math-vector-is-string a
))
256 (math-vector-to-string a t
)
257 (if (and break
(cdr a
)
258 (not (eq calc-language
'flat
)))
259 (let* ((full (or calc-full-vectors
(< (length a
) 7)))
260 (rows (if full
(1- (length a
)) 5))
261 (base (/ (1- rows
) 2))
262 (calc-break-vectors nil
))
264 (cons 'vleft
(cons base
267 (if full rows
3) t
)))))
268 (if (or calc-full-vectors
(< (length a
) 7))
269 (if (and (eq calc-language
'tex
)
271 (if (and (integerp calc-language-option
)
272 (or (= calc-language-option
0)
273 (> calc-language-option
1)
274 (< calc-language-option -
1)))
275 (append '(vleft 0 "\\matrix{")
276 (math-compose-tex-matrix (cdr a
))
278 (append '(horiz "\\matrix{ ")
279 (math-compose-tex-matrix (cdr a
))
281 (if (and (eq calc-language
'latex
)
283 (if (and (integerp calc-language-option
)
284 (or (= calc-language-option
0)
285 (> calc-language-option
1)
286 (< calc-language-option -
1)))
287 (append '(vleft 0 "\\begin{pmatrix}")
288 (math-compose-tex-matrix (cdr a
))
290 (append '(horiz "\\begin{pmatrix} ")
291 (math-compose-tex-matrix (cdr a
))
292 '(" \\end{pmatrix}")))
293 (if (and (eq calc-language
'eqn
)
295 (append '(horiz "matrix { ")
296 (math-compose-eqn-matrix
297 (cdr (math-transpose a
)))
299 (if (and (eq calc-language
'maple
)
303 math-comp-left-bracket
304 (math-compose-vector (cdr a
)
305 (concat math-comp-comma
" ")
306 math-comp-vector-prec
)
307 math-comp-right-bracket
310 math-comp-left-bracket
311 (math-compose-vector (cdr a
)
312 (concat math-comp-comma
" ")
313 math-comp-vector-prec
)
314 math-comp-right-bracket
)))))
316 math-comp-left-bracket
317 (math-compose-vector (list (nth 1 a
) (nth 2 a
) (nth 3 a
))
318 (concat math-comp-comma
" ")
319 math-comp-vector-prec
)
320 math-comp-comma
(if (memq calc-language
'(tex latex
))
323 (list 'break math-compose-level
)
324 (math-compose-expr (nth (1- (length a
)) a
)
325 (if (equal math-comp-comma
"") 1000 0))
326 math-comp-right-bracket
)))))))
327 ((eq (car a
) 'incomplete
)
329 (cond ((eq (nth 1 a
) 'vec
)
331 (math-compose-vector (cdr (cdr a
)) ", " 0)
333 ((eq (nth 1 a
) 'cplx
)
335 (math-compose-vector (cdr (cdr a
)) ", " 0)
337 ((eq (nth 1 a
) 'polar
)
339 (math-compose-vector (cdr (cdr a
)) "; " 0)
341 ((eq (nth 1 a
) 'intv
)
343 (if (memq (nth 2 a
) '(0 1)) "(" "[")
344 (math-compose-vector (cdr (cdr (cdr a
))) " .. " 0)
347 (cond ((eq (nth 1 a
) 'vec
) "[ ...")
348 ((eq (nth 1 a
) 'intv
)
349 (if (memq (nth 2 a
) '(0 1)) "( ..." "[ ..."))
352 (let ((v (rassq (nth 2 a
) math-expr-variable-mapping
)))
354 (symbol-name (car v
))
355 (if (and (memq calc-language
'(tex latex
))
357 (not (= calc-language-option
0))
358 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
359 (symbol-name (nth 1 a
))))
360 (if (eq calc-language
'latex
)
361 (format "\\text{%s}" (symbol-name (nth 1 a
)))
362 (format "\\hbox{%s}" (symbol-name (nth 1 a
))))
363 (if (and math-compose-hash-args
364 (let ((p calc-arg-values
))
366 (while (and p
(not (equal (car p
) a
)))
367 (setq p
(and (eq math-compose-hash-args t
) (cdr p
))
370 (if (eq math-compose-hash-args
1)
373 (if (memq calc-language
'(c fortran pascal maple
))
374 (math-to-underscores (symbol-name (nth 1 a
)))
375 (if (and (eq calc-language
'eqn
)
376 (string-match ".'\\'" (symbol-name (nth 2 a
))))
378 (list 'calcFunc-Prime
381 (intern (substring (symbol-name (nth 1 a
)) 0 -
1))
382 (intern (substring (symbol-name (nth 2 a
)) 0 -
1))))
384 (symbol-name (nth 1 a
)))))))))
387 (if (eq calc-language
'maple
) ""
388 (if (memq (nth 1 a
) '(0 1)) "(" "["))
389 (math-compose-expr (nth 2 a
) 0)
390 (if (memq calc-language
'(tex latex
)) " \\ldots "
391 (if (eq calc-language
'eqn
) " ... " " .. "))
392 (math-compose-expr (nth 3 a
) 0)
393 (if (eq calc-language
'maple
) ""
394 (if (memq (nth 1 a
) '(0 2)) ")" "]"))))
396 (if (eq (car calc-date-format
) 'X
)
398 (concat "<" (math-format-date a
) ">")))
399 ((and (eq (car a
) 'calcFunc-subscr
) (cdr (cdr a
))
400 (memq calc-language
'(c pascal fortran maple
)))
401 (let ((args (cdr (cdr a
))))
402 (while (and (memq calc-language
'(pascal fortran
))
403 (eq (car-safe (nth 1 a
)) 'calcFunc-subscr
))
404 (setq args
(append (cdr (cdr (nth 1 a
))) args
)
407 (math-compose-expr (nth 1 a
) 1000)
408 (if (eq calc-language
'fortran
) "(" "[")
409 (math-compose-vector args
", " 0)
410 (if (eq calc-language
'fortran
) ")" "]"))))
411 ((and (eq (car a
) 'calcFunc-subscr
) (= (length a
) 3)
412 (eq calc-language
'big
))
413 (let* ((a1 (math-compose-expr (nth 1 a
) 1000))
414 (calc-language 'flat
)
415 (a2 (math-compose-expr (nth 2 a
) 0)))
416 (if (or (eq (car-safe a1
) 'subscr
)
417 (and (eq (car-safe a1
) 'tag
)
418 (eq (car-safe (nth 2 a1
)) 'subscr
)
419 (setq a1
(nth 2 a1
))))
426 (list 'subscr a1 a2
))))
427 ((and (eq (car a
) 'calcFunc-subscr
) (= (length a
) 3)
428 (eq calc-language
'math
))
430 (math-compose-expr (nth 1 a
) 1000)
432 (math-compose-expr (nth 2 a
) 0)
434 ((and (eq (car a
) 'calcFunc-sqrt
)
435 (memq calc-language
'(tex latex
)))
438 (math-compose-expr (nth 1 a
) 0)
440 ((and nil
(eq (car a
) 'calcFunc-sqrt
)
441 (eq calc-language
'eqn
))
444 (math-compose-expr (nth 1 a
) -
1)
446 ((and (eq (car a
) '^
)
447 (eq calc-language
'big
))
449 (if (or (math-looks-negp (nth 1 a
))
450 (memq (car-safe (nth 1 a
)) '(^
/ frac calcFunc-sqrt
))
451 (and (eq (car-safe (nth 1 a
)) 'cplx
)
452 (math-negp (nth 1 (nth 1 a
)))
453 (eq (nth 2 (nth 1 a
)) 0)))
454 (list 'horiz
"(" (math-compose-expr (nth 1 a
) 0) ")")
455 (math-compose-expr (nth 1 a
) 201))
456 (let ((calc-language 'flat
)
457 (calc-number-radix 10))
458 (math-compose-expr (nth 2 a
) 0))))
459 ((and (eq (car a
) '/)
460 (eq calc-language
'big
))
461 (let ((a1 (let ((calc-language (if (memq (car-safe (nth 1 a
)) '(/ frac
))
463 (math-compose-expr (nth 1 a
) 0)))
464 (a2 (let ((calc-language (if (memq (car-safe (nth 2 a
)) '(/ frac
))
466 (math-compose-expr (nth 2 a
) 0))))
468 (math-comp-height a1
)
470 ((and (memq (car a
) '(calcFunc-sum calcFunc-prod
))
471 (memq calc-language
'(tex latex
))
473 (list 'horiz
(if (eq (car a
) 'calcFunc-sum
) "\\sum" "\\prod")
474 "_{" (math-compose-expr (nth 2 a
) 0)
475 "=" (math-compose-expr (nth 3 a
) 0)
476 "}^{" (math-compose-expr (nth 4 a
) 0)
477 "}{" (math-compose-expr (nth 1 a
) 0) "}"))
478 ((and (eq (car a
) 'calcFunc-lambda
)
480 (memq calc-language
'(nil flat big
)))
483 (math-compose-hash-args (if (= (length a
) 3) 1 t
)))
484 (while (and (cdr p
) (equal (car p
) (car ap
)))
485 (setq p
(cdr p
) ap
(cdr ap
)))
488 (list (math-compose-vector
489 (nreverse (cdr (reverse (cdr a
)))) ", " 0)
492 (list (math-compose-expr (nth (1- (length a
)) a
) 0)
494 ((and (eq (car a
) 'calcFunc-string
)
496 (math-vectorp (nth 1 a
))
497 (math-vector-is-string (nth 1 a
)))
498 (if (eq calc-language
'unform
)
499 (concat "string(" (math-vector-to-string (nth 1 a
) t
) ")")
500 (math-vector-to-string (nth 1 a
) nil
)))
501 ((and (eq (car a
) 'calcFunc-bstring
)
503 (math-vectorp (nth 1 a
))
504 (math-vector-is-string (nth 1 a
)))
505 (if (eq calc-language
'unform
)
506 (concat "bstring(" (math-vector-to-string (nth 1 a
) t
) ")")
508 (s (math-vector-to-string (nth 1 a
) nil
))
510 (while (string-match "[^ ] +[^ ]" s
)
511 (setq p
(1- (match-end 0))
512 c
(cons (list 'break math-compose-level
)
513 (cons (substring s
0 p
)
516 (setq c
(nreverse (cons s c
)))
518 (setq c
(cons (list 'set math-compose-level
2) c
)))
520 ((and (eq (car a
) 'calcFunc-cprec
)
521 (not (eq calc-language
'unform
))
523 (integerp (nth 2 a
)))
524 (let ((c (math-compose-expr (nth 1 a
) -
1)))
525 (if (> prec
(nth 2 a
))
526 (if (memq calc-language
'(tex latex
))
527 (list 'horiz
"\\left( " c
" \\right)")
528 (if (eq calc-language
'eqn
)
529 (list 'horiz
"{left ( " c
" right )}")
530 (list 'horiz
"(" c
")")))
532 ((and (eq (car a
) 'calcFunc-choriz
)
533 (not (eq calc-language
'unform
))
534 (memq (length a
) '(2 3 4))
535 (math-vectorp (nth 1 a
))
536 (if (integerp (nth 2 a
))
538 (and (math-vectorp (nth 3 a
))
539 (math-vector-is-string (nth 3 a
))))
541 (and (math-vectorp (nth 2 a
))
542 (math-vector-is-string (nth 2 a
))))))
543 (let* ((cprec (and (integerp (nth 2 a
)) (nth 2 a
)))
544 (sep (nth (if cprec
3 2) a
))
547 (math-compose-vector (cdr (nth 1 a
))
548 (math-vector-to-string sep nil
)
550 (cons 'horiz
(mapcar (function
552 (if (eq (car-safe x
) 'calcFunc-bstring
)
555 x
(or bprec cprec prec
))
557 (math-compose-expr x
(or cprec prec
)))))
559 ((and (memq (car a
) '(calcFunc-cvert calcFunc-clvert calcFunc-crvert
))
560 (not (eq calc-language
'unform
))
561 (memq (length a
) '(2 3))
562 (math-vectorp (nth 1 a
))
564 (integerp (nth 2 a
))))
567 (prec (or (nth 2 a
) prec
))
570 (let ((b nil
) (cc nil
) a d
)
571 (if (and (memq (car-safe x
) '(calcFunc-cbase
574 (memq (length x
) '(1 2)))
577 (if (and (eq (car-safe x
) 'calcFunc-crule
)
578 (memq (length x
) '(1 2))
580 (and (math-vectorp (nth 1 x
))
581 (= (length (nth 1 x
)) 2)
582 (math-vector-is-string
584 (and (natnump (nth 1 x
))
585 (<= (nth 1 x
) 255))))
588 (if (math-vectorp (nth 1 x
))
589 (aref (math-vector-to-string
592 (or (and (memq (car-safe x
) '(calcFunc-cvspace
595 (memq (length x
) '(2 3))
598 (setq cc
(math-compose-expr x prec
))))
599 (setq a
(if cc
(math-comp-ascent cc
) 0)
600 d
(if cc
(math-comp-descent cc
) 0))
601 (if (eq b
'calcFunc-cbase
)
602 (setq base
(+ v a -
1))
603 (if (eq b
'calcFunc-ctbase
)
605 (if (eq b
'calcFunc-cbbase
)
606 (setq base
(+ v a d -
1)))))
610 (setq c
(delq nil c
))
612 (cons (if (eq (car a
) 'calcFunc-cvert
) 'vcent
613 (if (eq (car a
) 'calcFunc-clvert
) 'vleft
'vright
))
616 ((and (memq (car a
) '(calcFunc-csup calcFunc-csub
))
617 (not (eq calc-language
'unform
))
618 (memq (length a
) '(3 4))
620 (integerp (nth 3 a
))))
621 (list (if (eq (car a
) 'calcFunc-csup
) 'supscr
'subscr
)
622 (math-compose-expr (nth 1 a
) (or (nth 3 a
) 0))
623 (math-compose-expr (nth 2 a
) 0)))
624 ((and (eq (car a
) 'calcFunc-cflat
)
625 (not (eq calc-language
'unform
))
626 (memq (length a
) '(2 3))
628 (integerp (nth 2 a
))))
629 (let ((calc-language (if (memq calc-language
'(nil big
))
630 'flat calc-language
)))
631 (math-compose-expr (nth 1 a
) (or (nth 2 a
) 0))))
632 ((and (eq (car a
) 'calcFunc-cspace
)
633 (memq (length a
) '(2 3))
636 (cons 'horiz
(make-list (nth 1 a
)
637 (if (and (math-vectorp (nth 2 a
))
638 (math-vector-is-string (nth 2 a
)))
639 (math-vector-to-string (nth 2 a
) nil
)
640 (math-compose-expr (nth 2 a
) 0))))
641 (make-string (nth 1 a
) ?\
)))
642 ((and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
643 (memq (length a
) '(2 3))
647 (let* ((c (if (nth 2 a
)
648 (if (and (math-vectorp (nth 2 a
))
649 (math-vector-is-string (nth 2 a
)))
650 (math-vector-to-string (nth 2 a
) nil
)
651 (math-compose-expr (nth 2 a
) 0))
653 (ca (math-comp-ascent c
))
654 (cd (math-comp-descent c
)))
656 (cons (if (eq (car a
) 'calcFunc-ctspace
)
658 (if (eq (car a
) 'calcFunc-cbspace
)
659 (+ (* (1- (nth 1 a
)) (+ ca cd
)) (1- ca
))
660 (/ (1- (* (nth 1 a
) (+ ca cd
))) 2)))
661 (make-list (nth 1 a
) c
))))))
662 ((and (eq (car a
) 'calcFunc-evalto
)
663 (setq calc-any-evaltos t
)
664 (memq calc-language
'(tex latex eqn
))
665 (= math-compose-level
(if math-comp-tagged
2 1))
668 (if (memq calc-language
'(tex latex
)) "\\evalto " "evalto ")
669 (math-compose-expr (nth 1 a
) 0)
670 (if (memq calc-language
'(tex latex
)) " \\to " " -> ")
671 (math-compose-expr (nth 2 a
) 0)))
673 (let ((op (and (not (eq calc-language
'unform
))
674 (if (and (eq (car a
) 'calcFunc-if
) (= (length a
) 4))
675 (assoc "?" math-expr-opers
)
676 (math-assq2 (car a
) math-expr-opers
)))))
678 (or (= (length a
) 3) (eq (car a
) 'calcFunc-if
))
681 ((> prec
(or (nth 4 op
) (min (nth 2 op
) (nth 3 op
))))
682 (if (and (memq calc-language
'(tex latex
))
683 (not (math-tex-expr-is-flat a
)))
684 (if (eq (car-safe a
) '/)
685 (list 'horiz
"{" (math-compose-expr a -
1) "}")
686 (list 'horiz
"\\left( "
687 (math-compose-expr a -
1)
689 (if (eq calc-language
'eqn
)
690 (if (or (eq (car-safe a
) '/)
692 (list 'horiz
"{" (math-compose-expr a -
1) "}")
693 (if (math-tex-expr-is-flat a
)
694 (list 'horiz
"( " (math-compose-expr a -
1) " )")
695 (list 'horiz
"{left ( "
696 (math-compose-expr a -
1)
698 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
699 ((and (memq calc-language
'(tex latex
))
700 (memq (car a
) '(/ calcFunc-choose calcFunc-evalto
))
702 (list 'horiz
"{" (math-compose-expr a -
1) "}"))
703 ((eq (car a
) 'calcFunc-if
)
705 (math-compose-expr (nth 1 a
) (nth 2 op
))
707 (math-compose-expr (nth 2 a
) 0)
709 (math-compose-expr (nth 3 a
) (nth 3 op
))))
711 (let* ((math-comp-tagged (and math-comp-tagged
714 (setlev (if (= prec
(min (nth 2 op
) (nth 3 op
)))
716 (setq math-compose-level
717 (1- math-compose-level
))
720 (lhs (math-compose-expr (nth 1 a
) (nth 2 op
)))
721 (rhs (math-compose-expr (nth 2 a
) (nth 3 op
))))
722 (and (equal (car op
) "^")
723 (eq (math-comp-first-char lhs
) ?-
)
724 (setq lhs
(list 'horiz
"(" lhs
")")))
725 (and (memq calc-language
'(tex latex
))
726 (or (equal (car op
) "^") (equal (car op
) "_"))
727 (not (and (stringp rhs
) (= (length rhs
) 1)))
728 (setq rhs
(list 'horiz
"{" rhs
"}")))
729 (or (and (eq (car a
) '*)
730 (or (null calc-language
)
731 (assoc "2x" math-expr-opers
))
732 (let* ((prevt (math-prod-last-term (nth 1 a
)))
733 (nextt (math-prod-first-term (nth 2 a
)))
734 (prevc (or (math-comp-last-char lhs
)
735 (and (memq (car-safe prevt
)
739 (eq calc-language
'big
)
741 (nextc (or (math-comp-first-char rhs
)
742 (and (memq (car-safe nextt
)
747 (eq calc-language
'big
)
750 (or (and (>= nextc ?a
) (<= nextc ?z
))
751 (and (>= nextc ?A
) (<= nextc ?Z
))
752 (and (>= nextc ?
0) (<= nextc ?
9))
753 (memq nextc
'(?. ?_ ?
#
758 (math-comp-first-string
760 (not (and (eq (car-safe prevt
) 'var
)
765 (list 'break math-compose-level
)
771 (list 'break math-compose-level
)
772 (if (or (equal (car op
) "^")
774 (equal (car op
) "**")
775 (and (equal (car op
) "*")
776 (math-comp-last-char lhs
)
777 (math-comp-first-char rhs
))
778 (and (equal (car op
) "/")
779 (math-num-integerp (nth 1 a
))
780 (math-integerp (nth 2 a
))))
782 (if (and (eq calc-language
'big
)
783 (equal (car op
) "=>"))
785 (concat " " (car op
) " ")))
787 ((and op
(= (length a
) 2) (= (nth 3 op
) -
1))
789 ((or (> prec
(or (nth 4 op
) (nth 2 op
)))
790 (and (not (eq (assoc (car op
) math-expr-opers
) op
))
791 (> prec
0))) ; don't write x% + y
792 (if (and (memq calc-language
'(tex latex
))
793 (not (math-tex-expr-is-flat a
)))
794 (list 'horiz
"\\left( "
795 (math-compose-expr a -
1)
797 (if (eq calc-language
'eqn
)
798 (if (= (/ prec
100) 9)
799 (list 'horiz
"{" (math-compose-expr a -
1) "}")
800 (if (math-tex-expr-is-flat a
)
801 (list 'horiz
"{( " (math-compose-expr a -
1) " )}")
802 (list 'horiz
"{left ( "
803 (math-compose-expr a -
1)
805 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
807 (let ((lhs (math-compose-expr (nth 1 a
) (nth 2 op
))))
810 (if (or (> (length (car op
)) 1)
811 (not (math-comp-is-flat lhs
)))
812 (concat " " (car op
))
814 ((and op
(= (length a
) 2) (= (nth 2 op
) -
1))
817 (let ((lr (and (memq calc-language
'(tex latex
))
818 (not (math-tex-expr-is-flat (nth 1 a
))))))
821 (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op
))
822 (substring (car op
) 1)
824 (if (or lr
(> (length (car op
)) 2)) " " "")
825 (math-compose-expr (nth 1 a
) -
1)
826 (if (or lr
(> (length (car op
)) 2)) " " "")
828 (car (nth 1 (memq op math-expr-opers
))))))
829 ((> prec
(or (nth 4 op
) (nth 3 op
)))
830 (if (and (memq calc-language
'(tex latex
))
831 (not (math-tex-expr-is-flat a
)))
832 (list 'horiz
"\\left( "
833 (math-compose-expr a -
1)
835 (if (eq calc-language
'eqn
)
836 (if (= (/ prec
100) 9)
837 (list 'horiz
"{" (math-compose-expr a -
1) "}")
838 (if (math-tex-expr-is-flat a
)
839 (list 'horiz
"{( " (math-compose-expr a -
1) " )}")
840 (list 'horiz
"{left ( "
841 (math-compose-expr a -
1)
843 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
845 (let ((rhs (math-compose-expr (nth 1 a
) (nth 3 op
))))
847 (let ((ops (if (string-match "\\`u\\([^a-zA-Z]\\)\\'"
849 (substring (car op
) 1)
851 (if (or (> (length ops
) 1)
852 (not (math-comp-is-flat rhs
)))
856 ((and (eq calc-language
'big
)
857 (setq op
(get (car a
) 'math-compose-big
))
858 (funcall op a prec
)))
859 ((and (setq op
(assq calc-language
860 '( ( nil . math-compose-normal
)
861 ( flat . math-compose-normal
)
862 ( big . math-compose-normal
)
863 ( c . math-compose-c
)
864 ( pascal . math-compose-pascal
)
865 ( fortran . math-compose-fortran
)
866 ( tex . math-compose-tex
)
867 ( latex . math-compose-latex
)
868 ( eqn . math-compose-eqn
)
869 ( math . math-compose-math
)
870 ( maple . math-compose-maple
))))
871 (setq op
(get (car a
) (cdr op
)))
872 (funcall op a prec
)))
874 (let* ((func (car a
))
875 (func2 (assq func
'(( mod . calcFunc-makemod
)
876 ( sdev . calcFunc-sdev
)
883 ( neg . calcFunc-neg
)
884 ( | . calcFunc-vconcat
))))
887 (setq func
(cdr func2
)))
888 (if (setq func2
(rassq func math-expr-function-mapping
))
889 (setq func
(car func2
)))
890 (setq func
(math-remove-dashes
892 "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
894 (math-match-substring (symbol-name func
) 1)
895 (symbol-name func
))))
896 (if (memq calc-language
'(c fortran pascal maple
))
897 (setq func
(math-to-underscores func
)))
898 (if (and (memq calc-language
'(tex latex
))
900 (not (= calc-language-option
0))
901 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func
))
902 (if (< (prefix-numeric-value calc-language-option
) 0)
903 (setq func
(format "\\%s" func
))
904 (setq func
(if (eq calc-language
'latex
)
905 (format "\\text{%s}" func
)
906 (format "\\hbox{%s}" func
)))))
907 (if (and (eq calc-language
'eqn
)
908 (string-match "[^']'+\\'" func
))
909 (let ((n (- (length func
) (match-beginning 0) 1)))
910 (setq func
(substring func
0 (- n
)))
911 (while (>= (setq n
(1- n
)) 0)
912 (setq func
(concat func
" prime")))))
913 (cond ((and (memq calc-language
'(tex latex
))
915 (not (math-tex-expr-is-flat (nth 1 a
)))))
916 (setq left
"\\left( "
918 ((and (eq calc-language
'eqn
)
920 (not (math-tex-expr-is-flat (nth 1 a
)))))
921 (setq left
"{left ( "
923 ((and (or (and (memq calc-language
'(tex latex
))
924 (eq (aref func
0) ?
\\))
925 (and (eq calc-language
'eqn
)
926 (memq (car a
) math-eqn-special-funcs
)))
928 (string-match "\\hbox{" func
)
929 (string-match "\\text{" func
)))
931 (or (Math-realp (nth 1 a
))
932 (memq (car (nth 1 a
)) '(var *))))
933 (setq left
(if (eq calc-language
'eqn
) "~{" "{")
935 ((eq calc-language
'eqn
)
938 (t (setq left calc-function-open
939 right calc-function-close
)))
940 (list 'horiz func left
941 (math-compose-vector (cdr a
)
942 (if (eq calc-language
'eqn
)
948 (defun math-prod-first-term (x)
949 (while (eq (car-safe x
) '*)
953 (defun math-prod-last-term (x)
954 (while (eq (car-safe x
) '*)
958 (defun math-compose-vector (a sep prec
)
961 (cons (list 'set math-compose-level
)
962 (let ((c (list (math-compose-expr (car a
) prec
))))
963 (while (setq a
(cdr a
))
964 (setq c
(cons (if (eq (car-safe (car a
))
966 (let ((math-compose-level
967 (1- math-compose-level
)))
968 (math-compose-expr (car a
) -
123))
969 (math-compose-expr (car a
) prec
))
970 (cons (list 'break math-compose-level
)
975 (defun math-vector-no-parens (a)
977 (not (eq (car-safe (nth 1 a
)) '*))))
979 (defun math-compose-matrix (a col cols base
)
982 (while (<= (setq col
(1+ col
)) cols
)
983 (setq res
(cons (cons math-comp-just
990 math-comp-vector-prec
)
994 math-comp-comma-spc
" ")))))
999 (defun math-compose-rows (a count first
)
1003 (math-compose-rows (cdr a
) -
1 nil
)
1004 (cons (concat (if (memq calc-language
'(tex latex
)) " \\ldots" " ...")
1006 (math-compose-rows (cdr a
) -
1 nil
)))
1008 (if first
(concat math-comp-left-bracket
" ") " ")
1009 (math-compose-expr (car a
) math-comp-vector-prec
)
1011 (math-compose-rows (cdr a
) (1- count
) nil
)))
1013 (if first
(concat math-comp-left-bracket
" ") " ")
1014 (math-compose-expr (car a
) math-comp-vector-prec
)
1015 (concat " " math-comp-right-bracket
)))))
1017 (defun math-compose-tex-matrix (a)
1019 (cons (append (math-compose-vector (cdr (car a
)) " & " 0) '(" \\\\ "))
1020 (math-compose-tex-matrix (cdr a
)))
1021 (list (math-compose-vector (cdr (car a
)) " & " 0))))
1023 (defun math-compose-eqn-matrix (a)
1026 (cond ((eq calc-matrix-just
'right
) "rcol ")
1027 ((eq calc-matrix-just
'center
) "ccol ")
1030 (list 'break math-compose-level
)
1034 (let ((math-compose-level (1+ math-compose-level
)))
1035 (math-compose-vector (cdr (car a
)) " above " 1000))
1038 (math-compose-eqn-matrix (cdr a
)))))))
1041 (defun math-vector-is-string (a)
1042 (while (and (setq a
(cdr a
))
1043 (or (and (natnump (car a
))
1045 (and (eq (car-safe (car a
)) 'cplx
)
1046 (natnump (nth 1 (car a
)))
1047 (eq (nth 2 (car a
)) 0)
1048 (<= (nth 1 (car a
)) 255)))))
1051 (defconst math-vector-to-string-chars
'( ( ?
\" .
"\\\"" )
1062 (defun math-vector-to-string (a &optional quoted
)
1063 (setq a
(concat (mapcar (function (lambda (x) (if (consp x
) (nth 1 x
) x
)))
1065 (if (string-match "[\000-\037\177\\\"]" a
)
1067 (pat (if quoted
"[\000-\037\177\\\"]" "[\000-\037\177]"))
1068 (codes (if quoted math-vector-to-string-chars
'((?\^? .
"^?"))))
1069 (fmt (if quoted
"\\^%c" "^%c"))
1071 (while (setq p
(string-match pat a p
))
1072 (if (setq new
(assq (aref a p
) codes
))
1073 (setq a
(concat (substring a
0 p
)
1075 (substring a
(1+ p
)))
1076 p
(+ p
(length (cdr new
))))
1077 (setq a
(concat (substring a
0 p
)
1078 (format fmt
(+ (aref a p
) 64))
1079 (substring a
(1+ p
)))
1082 (concat "\"" a
"\"")
1086 (defun math-to-underscores (x)
1087 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
1088 (math-to-underscores
1089 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
1092 (defun math-tex-expr-is-flat (a)
1093 (or (Math-integerp a
)
1094 (memq (car a
) '(float var
))
1095 (and (memq (car a
) '(+ -
* neg
))
1097 (while (and (setq a
(cdr a
))
1098 (math-tex-expr-is-flat (car a
))))
1100 (and (memq (car a
) '(^ calcFunc-subscr
))
1101 (math-tex-expr-is-flat (nth 1 a
)))))
1103 (put 'calcFunc-log
'math-compose-big
'math-compose-log
)
1104 (defun math-compose-log (a prec
)
1105 (and (= (length a
) 3)
1108 (let ((calc-language 'flat
))
1109 (math-compose-expr (nth 2 a
) 1000)))
1111 (math-compose-expr (nth 1 a
) 1000)
1114 (put 'calcFunc-log10
'math-compose-big
'math-compose-log10
)
1115 (defun math-compose-log10 (a prec
)
1116 (and (= (length a
) 2)
1118 (list 'subscr
"log" "10")
1120 (math-compose-expr (nth 1 a
) 1000)
1123 (put 'calcFunc-deriv
'math-compose-big
'math-compose-deriv
)
1124 (put 'calcFunc-tderiv
'math-compose-big
'math-compose-deriv
)
1125 (defun math-compose-deriv (a prec
)
1126 (when (= (length a
) 3)
1127 (math-compose-expr (list '/
1128 (list 'calcFunc-choriz
1130 '(calcFunc-string (vec ?d
))
1132 (list 'calcFunc-choriz
1134 '(calcFunc-string (vec ?d
))
1138 (put 'calcFunc-sqrt
'math-compose-big
'math-compose-sqrt
)
1139 (defun math-compose-sqrt (a prec
)
1140 (when (= (length a
) 2)
1141 (let* ((c (math-compose-expr (nth 1 a
) 0))
1142 (a (math-comp-ascent c
))
1143 (d (math-comp-descent c
))
1145 (w (math-comp-width c
)))
1148 (concat (if (= h
1) " " " ")
1149 (make-string (+ w
2) ?\_
))
1153 (append (list 'vleft
(1- a
))
1154 (make-list (1- h
) " |")
1159 (put 'calcFunc-choose
'math-compose-big
'math-compose-choose
)
1160 (defun math-compose-choose (a prec
)
1161 (let ((a1 (math-compose-expr (nth 1 a
) 0))
1162 (a2 (math-compose-expr (nth 2 a
) 0)))
1166 (math-comp-height a1
)
1170 (put 'calcFunc-integ
'math-compose-big
'math-compose-integ
)
1171 (defun math-compose-integ (a prec
)
1172 (and (memq (length a
) '(3 5))
1173 (eq (car-safe (nth 2 a
)) 'var
)
1174 (let* ((parens (and (>= prec
196) (/= prec
1000)))
1175 (var (math-compose-expr (nth 2 a
) 0))
1176 (over (and (eq (car-safe (nth 2 a
)) 'var
)
1177 (or (and (eq (car-safe (nth 1 a
)) '/)
1178 (math-numberp (nth 1 (nth 1 a
))))
1179 (and (eq (car-safe (nth 1 a
)) '^
)
1180 (math-looks-negp (nth 2 (nth 1 a
)))))))
1181 (expr (math-compose-expr (if over
1183 (math-build-var-name
1186 (nth 1 (nth 2 a
)))))
1188 (calc-language 'flat
)
1189 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1190 (high (and (nth 4 a
) (math-compose-expr (nth 4 a
) 0))))
1193 (append (list 'vcent
(if high
3 2))
1194 (and high
(list (list 'horiz
" " high
)))
1200 (and low
(list (list 'horiz low
" "))))
1204 (list 'horiz
" d" var
))
1205 (if parens
")" "")))))
1207 (put 'calcFunc-sum
'math-compose-big
'math-compose-sum
)
1208 (defun math-compose-sum (a prec
)
1209 (and (memq (length a
) '(3 5 6))
1210 (let* ((expr (math-compose-expr (nth 1 a
) 185))
1211 (calc-language 'flat
)
1212 (var (math-compose-expr (nth 2 a
) 0))
1213 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1214 (high (and (nth 4 a
) (math-compose-vector (nthcdr 4 a
) ", " 0))))
1216 (if (memq prec
'(180 201)) "(" "")
1217 (append (list 'vcent
(if high
3 2))
1218 (and high
(list high
))
1225 (list (list 'horiz var
" = " low
))
1227 (if (memq (car-safe (nth 1 a
)) '(calcFunc-sum calcFunc-prod
))
1230 (if (memq prec
'(180 201)) ")" "")))))
1232 (put 'calcFunc-prod
'math-compose-big
'math-compose-prod
)
1233 (defun math-compose-prod (a prec
)
1234 (and (memq (length a
) '(3 5 6))
1235 (let* ((expr (math-compose-expr (nth 1 a
) 198))
1236 (calc-language 'flat
)
1237 (var (math-compose-expr (nth 2 a
) 0))
1238 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1239 (high (and (nth 4 a
) (math-compose-vector (nthcdr 4 a
) ", " 0))))
1241 (if (memq prec
'(196 201)) "(" "")
1242 (append (list 'vcent
(if high
3 2))
1243 (and high
(list high
))
1249 (list (list 'horiz var
" = " low
))
1251 (if (memq (car-safe (nth 1 a
)) '(calcFunc-sum calcFunc-prod
))
1254 (if (memq prec
'(196 201)) ")" "")))))
1256 ;; The variables math-svo-c, math-svo-wid and math-svo-off are local
1257 ;; to math-stack-value-offset in calc.el, but are used by
1258 ;; math-stack-value-offset-fancy, which is called by math-stack-value-offset..
1260 (defvar math-svo-wid
)
1261 (defvar math-svo-off
)
1263 (defun math-stack-value-offset-fancy ()
1264 (let ((cwid (+ (math-comp-width math-svo-c
))))
1265 (cond ((eq calc-display-just
'right
)
1266 (if calc-display-origin
1267 (setq math-svo-wid
(max calc-display-origin
5))
1268 (if (integerp calc-line-breaking
)
1269 (setq math-svo-wid calc-line-breaking
)))
1270 (setq math-svo-off
(- math-svo-wid cwid
1271 (max (- (length calc-right-label
)
1272 (if (and (integerp calc-line-breaking
)
1273 calc-display-origin
)
1274 (max (- calc-line-breaking
1275 calc-display-origin
)
1280 (if calc-display-origin
1282 (setq math-svo-off
(- calc-display-origin
(/ cwid
2)))
1283 (if (integerp calc-line-breaking
)
1284 (setq math-svo-off
(min math-svo-off
(- calc-line-breaking cwid
1285 (length calc-right-label
)))))
1286 (if (>= math-svo-off
0)
1287 (setq math-svo-wid
(max math-svo-wid
(+ math-svo-off cwid
)))))
1288 (if (integerp calc-line-breaking
)
1289 (setq math-svo-wid calc-line-breaking
))
1290 (setq math-svo-off
(/ (- math-svo-wid cwid
) 2)))))
1291 (and (integerp calc-line-breaking
)
1292 (or (< math-svo-off
0)
1293 (and calc-display-origin
1294 (> calc-line-breaking calc-display-origin
)))
1295 (setq math-svo-wid calc-line-breaking
))))
1298 ;;; Convert a composition to string form, with embedded \n's if necessary.
1300 (defun math-composition-to-string (c &optional width
)
1301 (or width
(setq width
(calc-window-width)))
1302 (if calc-display-raw
1303 (math-comp-to-string-raw c
0)
1304 (if (math-comp-is-flat c
)
1305 (math-comp-to-string-flat c width
)
1306 (math-vert-comp-to-string
1307 (math-comp-simplify c width
)))))
1309 (defvar math-comp-buf-string
(make-vector 10 ""))
1310 (defvar math-comp-buf-margin
(make-vector 10 0))
1311 (defvar math-comp-buf-level
(make-vector 10 0))
1312 (defun math-comp-is-flat (c) ; check if c's height is 1.
1313 (cond ((not (consp c
)) t
)
1314 ((memq (car c
) '(set break
)) t
)
1315 ((eq (car c
) 'horiz
)
1316 (while (and (setq c
(cdr c
))
1317 (math-comp-is-flat (car c
))))
1319 ((memq (car c
) '(vleft vcent vright
))
1320 (and (= (length c
) 3)
1322 (math-comp-is-flat (nth 2 c
))))
1324 (math-comp-is-flat (nth 2 c
)))
1328 ;;; Convert a one-line composition to a string. Break into multiple
1329 ;;; lines if necessary, choosing break points according to the structure
1332 ;; The variables math-comp-full-width, math-comp-highlight, math-comp-word,
1333 ;; math-comp-level, math-comp-margin and math-comp-buf are local to
1334 ;; math-comp-to-string-flat, but are used by math-comp-to-string-flat-term,
1335 ;; which is called by math-comp-to-string-flat.
1336 ;; math-comp-highlight and math-comp-buf are also local to
1337 ;; math-comp-simplify-term and math-comp-simplify respectively, but are used
1338 ;; by math-comp-add-string.
1339 (defvar math-comp-full-width
)
1340 (defvar math-comp-highlight
)
1341 (defvar math-comp-word
)
1342 (defvar math-comp-level
)
1343 (defvar math-comp-margin
)
1344 (defvar math-comp-buf
)
1345 ;; The variable math-comp-pos is local to math-comp-to-string-flat, but
1346 ;; is used by math-comp-to-string-flat-term and math-comp-sel-first-term,
1347 ;; which are called by math-comp-to-string-flat.
1348 (defvar math-comp-pos
)
1350 (defun math-comp-to-string-flat (c math-comp-full-width
)
1351 (if math-comp-sel-hpos
1352 (let ((math-comp-pos 0))
1353 (math-comp-sel-flat-term c
))
1354 (let ((math-comp-buf "")
1357 (math-comp-margin 0)
1358 (math-comp-highlight (and math-comp-selected calc-show-selections
))
1359 (math-comp-level -
1))
1360 (math-comp-to-string-flat-term '(set -
1 0))
1361 (math-comp-to-string-flat-term c
)
1362 (math-comp-to-string-flat-term '(break -
1))
1363 (let ((str (aref math-comp-buf-string
0))
1365 (and (> (length str
) 0) (= (aref str
0) ?
)
1366 (> (length math-comp-buf
) 0)
1367 (let ((k (length math-comp-buf
)))
1368 (while (not (= (aref math-comp-buf
(setq k
(1- k
))) ?
\n)))
1369 (aset math-comp-buf k ?
)
1370 (if (and (< (1+ k
) (length math-comp-buf
))
1371 (= (aref math-comp-buf
(1+ k
)) ?
))
1373 (aset math-comp-buf
(1+ k
) ?
\n)
1375 (setq prefix
"\n"))))
1376 (concat math-comp-buf prefix str
)))))
1378 (defun math-comp-to-string-flat-term (c)
1379 (cond ((not (consp c
))
1380 (if math-comp-highlight
1381 (setq c
(math-comp-highlight-string c
)))
1382 (setq math-comp-word
(if (= (length math-comp-word
) 0) c
1383 (concat math-comp-word c
))
1384 math-comp-pos
(+ math-comp-pos
(length c
))))
1386 ((eq (car c
) 'horiz
)
1387 (while (setq c
(cdr c
))
1388 (math-comp-to-string-flat-term (car c
))))
1393 (setq math-comp-level
(1+ math-comp-level
))
1394 (if (>= math-comp-level
(length math-comp-buf-string
))
1395 (setq math-comp-buf-string
(vconcat math-comp-buf-string
1396 math-comp-buf-string
)
1397 math-comp-buf-margin
(vconcat math-comp-buf-margin
1398 math-comp-buf-margin
)
1399 math-comp-buf-level
(vconcat math-comp-buf-level
1400 math-comp-buf-level
)))
1401 (aset math-comp-buf-string math-comp-level
"")
1402 (aset math-comp-buf-margin math-comp-level
(+ math-comp-pos
1404 (aset math-comp-buf-level math-comp-level
(nth 1 c
)))))
1406 ((eq (car c
) 'break
)
1407 (if (not calc-line-breaking
)
1408 (setq math-comp-buf
(concat math-comp-buf math-comp-word
)
1411 (if (and (> math-comp-pos math-comp-full-width
)
1414 (setq str
(aref math-comp-buf-string i
))
1415 (and (= (length str
) 0) (< i math-comp-level
)))
1417 (or (> (length str
) 0) (> (length math-comp-buf
) 0))))
1418 (let ((prefix "") mrg wid
)
1419 (setq mrg
(aref math-comp-buf-margin i
))
1420 (if (> mrg
12) ; indenting too far, go back to far left
1421 (let ((j i
) (new (if calc-line-numbering
5 1)))
1422 '(while (<= j math-comp-level
)
1423 (aset math-comp-buf-margin j
1424 (+ (aref math-comp-buf-margin j
) (- new mrg
)))
1427 (setq wid
(+ (length str
) math-comp-margin
))
1428 (and (> (length str
) 0) (= (aref str
0) ?
)
1429 (> (length math-comp-buf
) 0)
1430 (let ((k (length math-comp-buf
)))
1431 (while (not (= (aref math-comp-buf
(setq k
(1- k
))) ?
\n)))
1432 (aset math-comp-buf k ?
)
1433 (if (and (< (1+ k
) (length math-comp-buf
))
1434 (= (aref math-comp-buf
(1+ k
)) ?
))
1436 (aset math-comp-buf
(1+ k
) ?
\n)
1438 (setq prefix
"\n"))))
1439 (setq math-comp-buf
(concat math-comp-buf prefix str
"\n"
1440 (make-string mrg ?
))
1441 math-comp-pos
(+ math-comp-pos
(- mrg wid
))
1442 math-comp-margin mrg
)
1443 (aset math-comp-buf-string i
"")
1444 (while (<= (setq i
(1+ i
)) math-comp-level
)
1445 (if (> (aref math-comp-buf-margin i
) wid
)
1446 (aset math-comp-buf-margin i
1447 (+ (aref math-comp-buf-margin i
)
1449 (if (and (= (nth 1 c
) (aref math-comp-buf-level math-comp-level
))
1450 (< math-comp-pos
(+ (aref math-comp-buf-margin math-comp-level
) 2)))
1451 () ; avoid stupid breaks, e.g., "1 +\n really_long_expr"
1452 (let ((str (aref math-comp-buf-string math-comp-level
)))
1453 (setq str
(if (= (length str
) 0)
1455 (concat str math-comp-word
))
1457 (while (< (nth 1 c
) (aref math-comp-buf-level math-comp-level
))
1458 (setq math-comp-level
(1- math-comp-level
))
1459 (or (= (length (aref math-comp-buf-string math-comp-level
)) 0)
1460 (setq str
(concat (aref math-comp-buf-string math-comp-level
)
1462 (aset math-comp-buf-string math-comp-level str
)))))
1465 (cond ((eq (nth 1 c
) math-comp-selected
)
1466 (let ((math-comp-highlight (not calc-show-selections
)))
1467 (math-comp-to-string-flat-term (nth 2 c
))))
1469 (let ((math-comp-highlight nil
))
1470 (math-comp-to-string-flat-term (nth 2 c
))))
1471 (t (math-comp-to-string-flat-term (nth 2 c
)))))
1473 (t (math-comp-to-string-flat-term (nth 2 c
)))))
1475 (defun math-comp-highlight-string (s)
1476 (setq s
(copy-sequence s
))
1477 (let ((i (length s
)))
1478 (while (>= (setq i
(1- i
)) 0)
1479 (or (memq (aref s i
) '(32 ?
\n))
1480 (aset s i
(if calc-show-selections ?\. ?\
#)))))
1484 ;; The variable math-comp-sel-tag is local to calc-find-selected-part
1485 ;; in calc-sel.el, but is used by math-comp-sel-flat-term and
1486 ;; math-comp-add-string-sel, which are called (indirectly) by
1487 ;; calc-find-selected-part.
1488 (defvar math-comp-sel-tag
)
1490 (defun math-comp-sel-flat-term (c)
1491 (cond ((not (consp c
))
1492 (setq math-comp-pos
(+ math-comp-pos
(length c
))))
1493 ((memq (car c
) '(set break
)))
1494 ((eq (car c
) 'horiz
)
1495 (while (and (setq c
(cdr c
)) (< math-comp-sel-cpos
1000000))
1496 (math-comp-sel-flat-term (car c
))))
1498 (if (<= math-comp-pos math-comp-sel-cpos
)
1500 (math-comp-sel-flat-term (nth 2 c
))
1501 (if (> math-comp-pos math-comp-sel-cpos
)
1502 (setq math-comp-sel-tag c
1503 math-comp-sel-cpos
1000000)))
1504 (math-comp-sel-flat-term (nth 2 c
))))
1505 (t (math-comp-sel-flat-term (nth 2 c
)))))
1508 ;;; Simplify a composition to a canonical form consisting of
1509 ;;; (vleft n "string" "string" "string" ...)
1510 ;;; where 0 <= n < number-of-strings.
1512 ;; The variables math-comp-base, math-comp-hgt, math-comp-tag,
1513 ;; math-comp-hpos and math-comp-vpos are local to math-comp-simplify,
1514 ;; but are used by math-comp-add-string (math-comp-base, math-comp-hgt),
1515 ;; math-comp-add-string-sel (math-comp-tag) and math-comp-simplify-term
1516 ;; (math-comp-tag, math-comp-vpos, math-comp-hpos), which are called by
1517 ;; math-comp-simplify.
1518 (defvar math-comp-base
)
1519 (defvar math-comp-hgt
)
1520 (defvar math-comp-tag
)
1521 (defvar math-comp-hpos
)
1522 (defvar math-comp-vpos
)
1524 (defun math-comp-simplify (c full-width
)
1525 (let ((math-comp-buf (list ""))
1530 (math-comp-highlight (and math-comp-selected calc-show-selections
))
1531 (math-comp-tag nil
))
1532 (math-comp-simplify-term c
)
1533 (cons 'vleft
(cons math-comp-base math-comp-buf
))))
1535 (defun math-comp-add-string (s h v
)
1536 (and (> (length s
) 0)
1537 (let ((vv (+ v math-comp-base
)))
1538 (if math-comp-sel-hpos
1539 (math-comp-add-string-sel h vv
(length s
) 1)
1541 (setq math-comp-buf
(nconc (make-list (- vv
) "") math-comp-buf
)
1542 math-comp-base
(- v
)
1543 math-comp-hgt
(- math-comp-hgt vv
)
1545 (if (>= vv math-comp-hgt
)
1546 (setq math-comp-buf
(nconc math-comp-buf
1547 (make-list (1+ (- vv math-comp-hgt
)) ""))
1548 math-comp-hgt
(1+ vv
))))
1549 (let ((str (nthcdr vv math-comp-buf
)))
1550 (setcar str
(concat (car str
)
1551 (make-string (- h
(length (car str
))) 32)
1552 (if math-comp-highlight
1553 (math-comp-highlight-string s
)
1556 (defun math-comp-add-string-sel (x y w h
)
1557 (if (and (<= y math-comp-sel-vpos
)
1558 (> (+ y h
) math-comp-sel-vpos
)
1559 (<= x math-comp-sel-hpos
)
1560 (> (+ x w
) math-comp-sel-hpos
))
1561 (setq math-comp-sel-tag math-comp-tag
1562 math-comp-sel-vpos
10000)))
1564 (defun math-comp-simplify-term (c)
1566 (math-comp-add-string c math-comp-hpos math-comp-vpos
)
1567 (setq math-comp-hpos
(+ math-comp-hpos
(length c
))))
1568 ((memq (car c
) '(set break
))
1570 ((eq (car c
) 'horiz
)
1571 (while (setq c
(cdr c
))
1572 (math-comp-simplify-term (car c
))))
1573 ((memq (car c
) '(vleft vcent vright
))
1574 (let* ((math-comp-vpos (+ (- math-comp-vpos
(nth 1 c
))
1575 (1- (math-comp-ascent (nth 2 c
)))))
1576 (widths (mapcar 'math-comp-width
(cdr (cdr c
))))
1577 (maxwid (apply 'max widths
))
1578 (bias (cond ((eq (car c
) 'vleft
) 0)
1579 ((eq (car c
) 'vcent
) 1)
1582 (while (setq c
(cdr c
))
1583 (if (eq (car-safe (car c
)) 'rule
)
1584 (math-comp-add-string (make-string maxwid
(nth 1 (car c
)))
1585 math-comp-hpos math-comp-vpos
)
1586 (let ((math-comp-hpos (+ math-comp-hpos
(/ (* bias
(- maxwid
1589 (math-comp-simplify-term (car c
))))
1591 (setq math-comp-vpos
(+ math-comp-vpos
1592 (+ (math-comp-descent (car c
))
1593 (math-comp-ascent (nth 1 c
))))
1594 widths
(cdr widths
))))
1595 (setq math-comp-hpos
(+ math-comp-hpos maxwid
))))
1596 ((eq (car c
) 'supscr
)
1597 (let* ((asc (or 1 (math-comp-ascent (nth 1 c
))))
1598 (desc (math-comp-descent (nth 2 c
)))
1601 (math-comp-simplify-term (nth 1 c
))))
1602 (math-comp-vpos (- math-comp-vpos
(+ asc desc
))))
1603 (math-comp-simplify-term (nth 2 c
))
1604 (if math-comp-sel-hpos
1605 (math-comp-add-string-sel oldh
1608 (math-comp-ascent (nth 2 c
)))
1609 (- math-comp-hpos oldh
)
1610 (math-comp-height c
)))))
1611 ((eq (car c
) 'subscr
)
1612 (let* ((asc (math-comp-ascent (nth 2 c
)))
1613 (desc (math-comp-descent (nth 1 c
)))
1614 (oldv math-comp-vpos
)
1617 (math-comp-simplify-term (nth 1 c
))))
1618 (math-comp-vpos (+ math-comp-vpos
(+ asc desc
))))
1619 (math-comp-simplify-term (nth 2 c
))
1620 (if math-comp-sel-hpos
1621 (math-comp-add-string-sel oldh oldv
1622 (- math-comp-hpos oldh
)
1623 (math-comp-height c
)))))
1625 (cond ((eq (nth 1 c
) math-comp-selected
)
1626 (let ((math-comp-highlight (not calc-show-selections
)))
1627 (math-comp-simplify-term (nth 2 c
))))
1629 (let ((math-comp-highlight nil
))
1630 (math-comp-simplify-term (nth 2 c
))))
1631 (t (let ((math-comp-tag c
))
1632 (math-comp-simplify-term (nth 2 c
))))))))
1635 ;;; Measuring a composition.
1637 (defun math-comp-first-char (c)
1639 (and (> (length c
) 0)
1641 ((memq (car c
) '(horiz subscr supscr
))
1642 (while (and (setq c
(cdr c
))
1643 (math-comp-is-null (car c
))))
1644 (and c
(math-comp-first-char (car c
))))
1646 (math-comp-first-char (nth 2 c
)))))
1648 (defun math-comp-first-string (c)
1650 (and (> (length c
) 0)
1652 ((eq (car c
) 'horiz
)
1653 (while (and (setq c
(cdr c
))
1654 (math-comp-is-null (car c
))))
1655 (and c
(math-comp-first-string (car c
))))
1657 (math-comp-first-string (nth 2 c
)))))
1659 (defun math-comp-last-char (c)
1661 (and (> (length c
) 0)
1662 (elt c
(1- (length c
)))))
1663 ((eq (car c
) 'horiz
)
1664 (let ((c (reverse (cdr c
))))
1665 (while (and c
(math-comp-is-null (car c
)))
1667 (and c
(math-comp-last-char (car c
)))))
1669 (math-comp-last-char (nth 2 c
)))))
1671 (defun math-comp-is-null (c)
1672 (cond ((stringp c
) (= (length c
) 0))
1673 ((memq (car c
) '(horiz subscr supscr
))
1674 (while (and (setq c
(cdr c
))
1675 (math-comp-is-null (car c
))))
1678 (math-comp-is-null (nth 2 c
)))
1679 ((memq (car c
) '(set break
)) t
)))
1681 (defun math-comp-width (c)
1682 (cond ((not (consp c
)) (length c
))
1683 ((memq (car c
) '(horiz subscr supscr
))
1685 (while (setq c
(cdr c
))
1686 (setq accum
(+ accum
(math-comp-width (car c
)))))
1688 ((memq (car c
) '(vcent vleft vright
))
1691 (while (setq c
(cdr c
))
1692 (setq accum
(max accum
(math-comp-width (car c
)))))
1695 (math-comp-width (nth 2 c
)))
1698 (defun math-comp-height (c)
1701 (+ (math-comp-ascent c
) (math-comp-descent c
))))
1703 (defun math-comp-ascent (c)
1704 (cond ((not (consp c
)) 1)
1705 ((eq (car c
) 'horiz
)
1707 (while (setq c
(cdr c
))
1708 (setq accum
(max accum
(math-comp-ascent (car c
)))))
1710 ((memq (car c
) '(vcent vleft vright
))
1711 (if (> (nth 1 c
) 0) (1+ (nth 1 c
)) 1))
1712 ((eq (car c
) 'supscr
)
1713 (max (math-comp-ascent (nth 1 c
)) (1+ (math-comp-height (nth 2 c
)))))
1714 ((eq (car c
) 'subscr
)
1715 (math-comp-ascent (nth 1 c
)))
1717 (math-comp-ascent (nth 2 c
)))
1720 (defun math-comp-descent (c)
1721 (cond ((not (consp c
)) 0)
1722 ((eq (car c
) 'horiz
)
1724 (while (setq c
(cdr c
))
1725 (setq accum
(max accum
(math-comp-descent (car c
)))))
1727 ((memq (car c
) '(vcent vleft vright
))
1728 (let ((accum (- (nth 1 c
))))
1730 (while (setq c
(cdr c
))
1731 (setq accum
(+ accum
(math-comp-height (car c
)))))
1732 (max (1- accum
) 0)))
1733 ((eq (car c
) 'supscr
)
1734 (math-comp-descent (nth 1 c
)))
1735 ((eq (car c
) 'subscr
)
1736 (+ (math-comp-descent (nth 1 c
)) (math-comp-height (nth 2 c
))))
1738 (math-comp-descent (nth 2 c
)))
1741 (defun calcFunc-cwidth (a &optional prec
)
1742 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1743 (math-comp-width (math-compose-expr a
(or prec
0))))
1745 (defun calcFunc-cheight (a &optional prec
)
1746 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1747 (if (and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
1748 (memq (length a
) '(2 3))
1751 (math-comp-height (math-compose-expr a
(or prec
0)))))
1753 (defun calcFunc-cascent (a &optional prec
)
1754 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1755 (if (and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
1756 (memq (length a
) '(2 3))
1759 (math-comp-ascent (math-compose-expr a
(or prec
0)))))
1761 (defun calcFunc-cdescent (a &optional prec
)
1762 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1763 (math-comp-descent (math-compose-expr a
(or prec
0))))
1766 ;;; Convert a simplified composition into string form.
1768 (defun math-vert-comp-to-string (c)
1771 (math-vert-comp-to-string-step (cdr (cdr c
)))))
1773 (defun math-vert-comp-to-string-step (c)
1775 (concat (car c
) "\n" (math-vert-comp-to-string-step (cdr c
)))
1779 ;;; Convert a composition to a string in "raw" form (for debugging).
1781 (defun math-comp-to-string-raw (c indent
)
1782 (cond ((or (not (consp c
)) (eq (car c
) 'set
))
1783 (prin1-to-string c
))
1785 (concat "(" (symbol-name (car c
)) ")"))
1787 (let ((next-indent (+ indent
2 (length (symbol-name (car c
))))))
1789 (symbol-name (car c
))
1791 (math-comp-to-string-raw (nth 1 c
) next-indent
)
1792 (math-comp-to-string-raw-step (cdr (cdr c
))
1796 (defun math-comp-to-string-raw-step (cl indent
)
1799 (make-string indent
32)
1800 (math-comp-to-string-raw (car cl
) indent
)
1801 (math-comp-to-string-raw-step (cdr cl
) indent
))
1806 ;;; arch-tag: 7c45d10a-a286-4dab-af49-7ae8989fbf78
1807 ;;; calccomp.el ends here