1 ;; Calculator for GNU Emacs, part II [calc-comp.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-comp () nil
)
32 ;;; A "composition" has one of the following forms:
34 ;;; "string" A literal string
36 ;;; (horiz C1 C2 ...) Horizontally abutted sub-compositions
38 ;;; (set LEVEL OFF) Set left margin + offset for line-break level
39 ;;; (break LEVEL) A potential line-break point
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.
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
50 ;;; (tag X C) Composition C corresponds to sub-expression X
52 (defun math-compose-expr (a prec
)
53 (let ((math-compose-level (1+ math-compose-level
)))
55 ((or (and (eq a math-comp-selected
) a
)
57 (not (eq math-comp-tagged a
))))
58 (let ((math-comp-selected nil
))
59 (and math-comp-tagged
(setq math-comp-tagged a
))
60 (list 'tag a
(math-compose-expr a prec
))))
61 ((and (not (consp a
)) (not (integerp a
)))
62 (concat "'" (prin1-to-string a
)))
64 (if (or (eq (car-safe a
) 'frac
)
65 (and (nth 1 calc-frac-format
) (Math-integerp a
)))
66 (if (memq calc-language
'(tex eqn math maple c fortran pascal
))
67 (let ((aa (math-adjust-fraction a
))
68 (calc-frac-format nil
))
69 (math-compose-expr (list '/
70 (if (memq calc-language
'(c fortran
))
71 (math-float (nth 1 aa
))
74 (if (and (eq calc-language
'big
)
75 (= (length (car calc-frac-format
)) 1))
76 (let* ((aa (math-adjust-fraction a
))
77 (calc-frac-format nil
)
78 (math-radix-explicit-format nil
)
80 (if (math-negp (nth 1 aa
))
84 (math-abs (nth 1 aa
)))
86 (math-format-number (nth 2 aa
))))))
87 (if (= calc-number-radix
10)
91 (int-to-string calc-number-radix
)))))
92 (math-format-number a
)))
93 (if (not (eq calc-language
'big
))
94 (math-format-number a prec
)
95 (if (memq (car-safe a
) '(cplx polar
))
96 (if (math-zerop (nth 2 a
))
97 (math-compose-expr (nth 1 a
) prec
)
99 (math-compose-expr (nth 1 a
) 0)
100 (if (eq (car a
) 'cplx
) ", " "; ")
101 (math-compose-expr (nth 2 a
) 0) ")"))
102 (if (or (= calc-number-radix
10)
104 (and calc-group-digits
105 (not (assoc calc-group-char
'((",") (" "))))))
106 (math-format-number a prec
)
107 (let ((s (math-format-number a prec
))
109 (while (string-match (if (> calc-number-radix
14)
110 "\\([0-9]+\\)#\\([0-9a-zA-Z., ]+\\)"
111 "\\([0-9]+\\)#\\([0-9a-dA-D., ]+\\)")
113 (setq c
(nconc c
(list (substring s
0 (match-beginning 0))
115 (math-match-substring s
2)
116 (math-match-substring s
1))))
117 s
(substring s
(match-end 0))))
119 "\\*\\([0-9.]+\\)\\^\\(-?[0-9]+\\)\\()?\\)\\'" s
)
121 (substring s
0 (match-beginning 0)) " "
123 (math-match-substring s
1)
124 (math-match-substring s
2))
125 (math-match-substring s
3))))
126 (if c
(cons 'horiz
(nconc c
(list s
))) s
)))))))
127 ((and (get (car a
) 'math-compose-forms
)
128 (not (eq calc-language
'unform
))
129 (let ((comps (get (car a
) 'math-compose-forms
))
131 (or (and (setq temp
(assq calc-language comps
))
132 (or (and (setq temp2
(assq (1- (length a
)) (cdr temp
)))
133 (setq temp
(apply (cdr temp2
) (cdr a
)))
134 (math-compose-expr temp prec
))
135 (and (setq temp2
(assq nil
(cdr temp
)))
136 (funcall (cdr temp2
) a
))))
137 (and (setq temp
(assq nil comps
))
138 (or (and (setq temp2
(assq (1- (length a
)) (cdr temp
)))
139 (setq temp
(apply (cdr temp2
) (cdr a
)))
140 (math-compose-expr temp prec
))
141 (and (setq temp2
(assq nil
(cdr temp
)))
142 (funcall (cdr temp2
) a
))))))))
144 (let* ((left-bracket (if calc-vector-brackets
145 (substring calc-vector-brackets
0 1) ""))
146 (right-bracket (if calc-vector-brackets
147 (substring calc-vector-brackets
1 2) ""))
148 (inner-brackets (memq 'R calc-matrix-brackets
))
149 (outer-brackets (memq 'O calc-matrix-brackets
))
150 (row-commas (memq 'C calc-matrix-brackets
))
151 (comma-spc (or calc-vector-commas
" "))
152 (comma (or calc-vector-commas
""))
153 (vector-prec (if (or (and calc-vector-commas
154 (math-vector-no-parens a
))
155 (memq 'P calc-matrix-brackets
)) 0 1000))
156 (just (cond ((eq calc-matrix-just
'right
) 'vright
)
157 ((eq calc-matrix-just
'center
) 'vcent
)
159 (break calc-break-vectors
))
160 (if (and (memq calc-language
'(nil big
))
161 (not calc-break-vectors
)
162 (math-matrixp a
) (not (math-matrixp (nth 1 a
)))
163 (or calc-full-vectors
164 (and (< (length a
) 7) (< (length (nth 1 a
)) 7))
165 (progn (setq break t
) nil
)))
167 (setq vector-prec
(if (or (and calc-vector-commas
168 (math-vector-no-parens
170 (memq 'P calc-matrix-brackets
))
174 (concat left-bracket left-bracket
" ")
175 (math-compose-vector (cdr (nth 1 a
)) (concat comma
" ")
177 (concat " " right-bracket right-bracket
))
178 (let* ((rows (1- (length a
)))
179 (cols (1- (length (nth 1 a
))))
180 (base (/ (1- rows
) 2))
181 (calc-language 'flat
))
183 (list (append '(vleft)
185 (list (concat (and outer-brackets
192 (concat (and outer-brackets
198 (math-compose-matrix (cdr a
) 1 cols base
)
199 (list (append '(vleft)
207 (if (and outer-brackets
217 right-bracket
)))))))))
218 (if (and calc-display-strings
220 (math-vector-is-string a
))
221 (math-vector-to-string a t
)
222 (if (and break
(cdr a
)
223 (not (eq calc-language
'flat
)))
224 (let* ((full (or calc-full-vectors
(< (length a
) 7)))
225 (rows (if full
(1- (length a
)) 5))
226 (base (/ (1- rows
) 2))
228 (calc-break-vectors nil
))
230 (cons 'vleft
(cons base
233 (if full rows
3) t
)))))
234 (if (or calc-full-vectors
(< (length a
) 7))
235 (if (and (eq calc-language
'tex
)
237 (append '(horiz "\\matrix{ ")
238 (math-compose-tex-matrix (cdr a
))
240 (if (and (eq calc-language
'eqn
)
242 (append '(horiz "matrix { ")
243 (math-compose-eqn-matrix
244 (cdr (math-transpose a
)))
246 (if (and (eq calc-language
'maple
)
251 (math-compose-vector (cdr a
) (concat comma
" ")
257 (math-compose-vector (cdr a
) (concat comma
" ")
262 (math-compose-vector (list (nth 1 a
) (nth 2 a
) (nth 3 a
))
263 (concat comma
" ") vector-prec
)
264 comma
(if (eq calc-language
'tex
) " \\ldots" " ...")
266 (list 'break math-compose-level
)
267 (math-compose-expr (nth (1- (length a
)) a
)
268 (if (equal comma
"") 1000 0))
270 ((eq (car a
) 'incomplete
)
272 (cond ((eq (nth 1 a
) 'vec
)
274 (math-compose-vector (cdr (cdr a
)) ", " 0)
276 ((eq (nth 1 a
) 'cplx
)
278 (math-compose-vector (cdr (cdr a
)) ", " 0)
280 ((eq (nth 1 a
) 'polar
)
282 (math-compose-vector (cdr (cdr a
)) "; " 0)
284 ((eq (nth 1 a
) 'intv
)
286 (if (memq (nth 2 a
) '(0 1)) "(" "[")
287 (math-compose-vector (cdr (cdr (cdr a
))) " .. " 0)
290 (cond ((eq (nth 1 a
) 'vec
) "[ ...")
291 ((eq (nth 1 a
) 'intv
)
292 (if (memq (nth 2 a
) '(0 1)) "( ..." "[ ..."))
295 (let ((v (rassq (nth 2 a
) math-expr-variable-mapping
)))
297 (symbol-name (car v
))
298 (if (and (eq calc-language
'tex
)
300 (not (= calc-language-option
0))
301 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
302 (symbol-name (nth 1 a
))))
303 (format "\\hbox{%s}" (symbol-name (nth 1 a
)))
304 (if (and math-compose-hash-args
305 (let ((p calc-arg-values
))
307 (while (and p
(not (equal (car p
) a
)))
308 (setq p
(and (eq math-compose-hash-args t
) (cdr p
))
311 (if (eq math-compose-hash-args
1)
314 (if (memq calc-language
'(c fortran pascal maple
))
315 (math-to-underscores (symbol-name (nth 1 a
)))
316 (if (and (eq calc-language
'eqn
)
317 (string-match ".'\\'" (symbol-name (nth 2 a
))))
319 (list 'calcFunc-Prime
322 (intern (substring (symbol-name (nth 1 a
)) 0 -
1))
323 (intern (substring (symbol-name (nth 2 a
)) 0 -
1))))
325 (symbol-name (nth 1 a
)))))))))
328 (if (eq calc-language
'maple
) ""
329 (if (memq (nth 1 a
) '(0 1)) "(" "["))
330 (math-compose-expr (nth 2 a
) 0)
331 (if (eq calc-language
'tex
) " \\ldots "
332 (if (eq calc-language
'eqn
) " ... " " .. "))
333 (math-compose-expr (nth 3 a
) 0)
334 (if (eq calc-language
'maple
) ""
335 (if (memq (nth 1 a
) '(0 2)) ")" "]"))))
337 (if (eq (car calc-date-format
) 'X
)
339 (concat "<" (math-format-date a
) ">")))
340 ((and (eq (car a
) 'calcFunc-subscr
) (cdr (cdr a
))
341 (memq calc-language
'(c pascal fortran maple
)))
342 (let ((args (cdr (cdr a
))))
343 (while (and (memq calc-language
'(pascal fortran
))
344 (eq (car-safe (nth 1 a
)) 'calcFunc-subscr
))
345 (setq args
(append (cdr (cdr (nth 1 a
))) args
)
348 (math-compose-expr (nth 1 a
) 1000)
349 (if (eq calc-language
'fortran
) "(" "[")
350 (math-compose-vector args
", " 0)
351 (if (eq calc-language
'fortran
) ")" "]"))))
352 ((and (eq (car a
) 'calcFunc-subscr
) (= (length a
) 3)
353 (eq calc-language
'big
))
354 (let* ((a1 (math-compose-expr (nth 1 a
) 1000))
355 (calc-language 'flat
)
356 (a2 (math-compose-expr (nth 2 a
) 0)))
357 (if (or (eq (car-safe a1
) 'subscr
)
358 (and (eq (car-safe a1
) 'tag
)
359 (eq (car-safe (nth 2 a1
)) 'subscr
)
360 (setq a1
(nth 2 a1
))))
367 (list 'subscr a1 a2
))))
368 ((and (eq (car a
) 'calcFunc-subscr
) (= (length a
) 3)
369 (eq calc-language
'math
))
371 (math-compose-expr (nth 1 a
) 1000)
373 (math-compose-expr (nth 2 a
) 0)
375 ((and (eq (car a
) 'calcFunc-sqrt
)
376 (eq calc-language
'tex
))
379 (math-compose-expr (nth 1 a
) 0)
381 ((and nil
(eq (car a
) 'calcFunc-sqrt
)
382 (eq calc-language
'eqn
))
385 (math-compose-expr (nth 1 a
) -
1)
387 ((and (eq (car a
) '^
)
388 (eq calc-language
'big
))
390 (if (or (math-looks-negp (nth 1 a
))
391 (memq (car-safe (nth 1 a
)) '(^
/ frac calcFunc-sqrt
))
392 (and (eq (car-safe (nth 1 a
)) 'cplx
)
393 (math-negp (nth 1 (nth 1 a
)))
394 (eq (nth 2 (nth 1 a
)) 0)))
395 (list 'horiz
"(" (math-compose-expr (nth 1 a
) 0) ")")
396 (math-compose-expr (nth 1 a
) 201))
397 (let ((calc-language 'flat
)
398 (calc-number-radix 10))
399 (math-compose-expr (nth 2 a
) 0))))
400 ((and (eq (car a
) '/)
401 (eq calc-language
'big
))
402 (let ((a1 (let ((calc-language (if (memq (car-safe (nth 1 a
)) '(/ frac
))
404 (math-compose-expr (nth 1 a
) 0)))
405 (a2 (let ((calc-language (if (memq (car-safe (nth 2 a
)) '(/ frac
))
407 (math-compose-expr (nth 2 a
) 0))))
409 (math-comp-height a1
)
411 ((and (memq (car a
) '(calcFunc-sum calcFunc-prod
))
412 (eq calc-language
'tex
)
414 (list 'horiz
(if (eq (car a
) 'calcFunc-sum
) "\\sum" "\\prod")
415 "_{" (math-compose-expr (nth 2 a
) 0)
416 "=" (math-compose-expr (nth 3 a
) 0)
417 "}^{" (math-compose-expr (nth 4 a
) 0)
418 "}{" (math-compose-expr (nth 1 a
) 0) "}"))
419 ((and (eq (car a
) 'calcFunc-lambda
)
421 (memq calc-language
'(nil flat big
)))
424 (math-compose-hash-args (if (= (length a
) 3) 1 t
)))
425 (while (and (cdr p
) (equal (car p
) (car ap
)))
426 (setq p
(cdr p
) ap
(cdr ap
)))
429 (list (math-compose-vector
430 (nreverse (cdr (reverse (cdr a
)))) ", " 0)
433 (list (math-compose-expr (nth (1- (length a
)) a
) 0)
435 ((and (eq (car a
) 'calcFunc-string
)
437 (math-vectorp (nth 1 a
))
438 (math-vector-is-string (nth 1 a
)))
439 (if (eq calc-language
'unform
)
440 (concat "string(" (math-vector-to-string (nth 1 a
) t
) ")")
441 (math-vector-to-string (nth 1 a
) nil
)))
442 ((and (eq (car a
) 'calcFunc-bstring
)
444 (math-vectorp (nth 1 a
))
445 (math-vector-is-string (nth 1 a
)))
446 (if (eq calc-language
'unform
)
447 (concat "bstring(" (math-vector-to-string (nth 1 a
) t
) ")")
449 (s (math-vector-to-string (nth 1 a
) nil
))
451 (while (string-match "[^ ] +[^ ]" s
)
452 (setq p
(1- (match-end 0))
453 c
(cons (list 'break math-compose-level
)
454 (cons (substring s
0 p
)
457 (setq c
(nreverse (cons s c
)))
459 (setq c
(cons (list 'set math-compose-level
2) c
)))
461 ((and (eq (car a
) 'calcFunc-cprec
)
462 (not (eq calc-language
'unform
))
464 (integerp (nth 2 a
)))
465 (let ((c (math-compose-expr (nth 1 a
) -
1)))
466 (if (> prec
(nth 2 a
))
467 (if (eq calc-language
'tex
)
468 (list 'horiz
"\\left( " c
" \\right)")
469 (if (eq calc-language
'eqn
)
470 (list 'horiz
"{left ( " c
" right )}")
471 (list 'horiz
"(" c
")")))
473 ((and (eq (car a
) 'calcFunc-choriz
)
474 (not (eq calc-language
'unform
))
475 (memq (length a
) '(2 3 4))
476 (math-vectorp (nth 1 a
))
477 (if (integerp (nth 2 a
))
479 (and (math-vectorp (nth 3 a
))
480 (math-vector-is-string (nth 3 a
))))
482 (and (math-vectorp (nth 2 a
))
483 (math-vector-is-string (nth 2 a
))))))
484 (let* ((cprec (and (integerp (nth 2 a
)) (nth 2 a
)))
485 (sep (nth (if cprec
3 2) a
))
488 (math-compose-vector (cdr (nth 1 a
))
489 (math-vector-to-string sep nil
)
491 (cons 'horiz
(mapcar (function
493 (if (eq (car-safe x
) 'calcFunc-bstring
)
496 x
(or bprec cprec prec
))
498 (math-compose-expr x
(or cprec prec
)))))
500 ((and (memq (car a
) '(calcFunc-cvert calcFunc-clvert calcFunc-crvert
))
501 (not (eq calc-language
'unform
))
502 (memq (length a
) '(2 3))
503 (math-vectorp (nth 1 a
))
505 (integerp (nth 2 a
))))
508 (prec (or (nth 2 a
) prec
))
511 (let ((b nil
) (cc nil
) a d
)
512 (if (and (memq (car-safe x
) '(calcFunc-cbase
515 (memq (length x
) '(1 2)))
518 (if (and (eq (car-safe x
) 'calcFunc-crule
)
519 (memq (length x
) '(1 2))
521 (and (math-vectorp (nth 1 x
))
522 (= (length (nth 1 x
)) 2)
523 (math-vector-is-string
525 (and (natnump (nth 1 x
))
526 (<= (nth 1 x
) 255))))
529 (if (math-vectorp (nth 1 x
))
530 (aref (math-vector-to-string
533 (or (and (memq (car-safe x
) '(calcFunc-cvspace
536 (memq (length x
) '(2 3))
539 (setq cc
(math-compose-expr x prec
))))
540 (setq a
(if cc
(math-comp-ascent cc
) 0)
541 d
(if cc
(math-comp-descent cc
) 0))
542 (if (eq b
'calcFunc-cbase
)
543 (setq base
(+ v a -
1))
544 (if (eq b
'calcFunc-ctbase
)
546 (if (eq b
'calcFunc-cbbase
)
547 (setq base
(+ v a d -
1)))))
551 (setq c
(delq nil c
))
553 (cons (if (eq (car a
) 'calcFunc-cvert
) 'vcent
554 (if (eq (car a
) 'calcFunc-clvert
) 'vleft
'vright
))
557 ((and (memq (car a
) '(calcFunc-csup calcFunc-csub
))
558 (not (eq calc-language
'unform
))
559 (memq (length a
) '(3 4))
561 (integerp (nth 3 a
))))
562 (list (if (eq (car a
) 'calcFunc-csup
) 'supscr
'subscr
)
563 (math-compose-expr (nth 1 a
) (or (nth 3 a
) 0))
564 (math-compose-expr (nth 2 a
) 0)))
565 ((and (eq (car a
) 'calcFunc-cflat
)
566 (not (eq calc-language
'unform
))
567 (memq (length a
) '(2 3))
569 (integerp (nth 2 a
))))
570 (let ((calc-language (if (memq calc-language
'(nil big
))
571 'flat calc-language
)))
572 (math-compose-expr (nth 1 a
) (or (nth 2 a
) 0))))
573 ((and (eq (car a
) 'calcFunc-cspace
)
574 (memq (length a
) '(2 3))
577 (cons 'horiz
(make-list (nth 1 a
)
578 (if (and (math-vectorp (nth 2 a
))
579 (math-vector-is-string (nth 2 a
)))
580 (math-vector-to-string (nth 2 a
) nil
)
581 (math-compose-expr (nth 2 a
) 0))))
582 (make-string (nth 1 a
) ?\
)))
583 ((and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
584 (memq (length a
) '(2 3))
588 (let* ((c (if (nth 2 a
)
589 (if (and (math-vectorp (nth 2 a
))
590 (math-vector-is-string (nth 2 a
)))
591 (math-vector-to-string (nth 2 a
) nil
)
592 (math-compose-expr (nth 2 a
) 0))
594 (ca (math-comp-ascent c
))
595 (cd (math-comp-descent c
)))
597 (cons (if (eq (car a
) 'calcFunc-ctspace
)
599 (if (eq (car a
) 'calcFunc-cbspace
)
600 (+ (* (1- (nth 1 a
)) (+ ca cd
)) (1- ca
))
601 (/ (1- (* (nth 1 a
) (+ ca cd
))) 2)))
602 (make-list (nth 1 a
) c
))))))
603 ((and (eq (car a
) 'calcFunc-evalto
)
604 (setq calc-any-evaltos t
)
605 (memq calc-language
'(tex eqn
))
606 (= math-compose-level
(if math-comp-tagged
2 1))
609 (if (eq calc-language
'tex
) "\\evalto " "evalto ")
610 (math-compose-expr (nth 1 a
) 0)
611 (if (eq calc-language
'tex
) " \\to " " -> ")
612 (math-compose-expr (nth 2 a
) 0)))
614 (let ((op (and (not (eq calc-language
'unform
))
615 (if (and (eq (car a
) 'calcFunc-if
) (= (length a
) 4))
616 (assoc "?" math-expr-opers
)
617 (math-assq2 (car a
) math-expr-opers
)))))
619 (or (= (length a
) 3) (eq (car a
) 'calcFunc-if
))
622 ((> prec
(or (nth 4 op
) (min (nth 2 op
) (nth 3 op
))))
623 (if (and (eq calc-language
'tex
)
624 (not (math-tex-expr-is-flat a
)))
625 (if (eq (car-safe a
) '/)
626 (list 'horiz
"{" (math-compose-expr a -
1) "}")
627 (list 'horiz
"\\left( "
628 (math-compose-expr a -
1)
630 (if (eq calc-language
'eqn
)
631 (if (or (eq (car-safe a
) '/)
633 (list 'horiz
"{" (math-compose-expr a -
1) "}")
634 (if (math-tex-expr-is-flat a
)
635 (list 'horiz
"( " (math-compose-expr a -
1) " )")
636 (list 'horiz
"{left ( "
637 (math-compose-expr a -
1)
639 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
640 ((and (eq calc-language
'tex
)
641 (memq (car a
) '(/ calcFunc-choose calcFunc-evalto
))
643 (list 'horiz
"{" (math-compose-expr a -
1) "}"))
644 ((eq (car a
) 'calcFunc-if
)
646 (math-compose-expr (nth 1 a
) (nth 2 op
))
648 (math-compose-expr (nth 2 a
) 0)
650 (math-compose-expr (nth 3 a
) (nth 3 op
))))
652 (let* ((math-comp-tagged (and math-comp-tagged
655 (setlev (if (= prec
(min (nth 2 op
) (nth 3 op
)))
657 (setq math-compose-level
658 (1- math-compose-level
))
661 (lhs (math-compose-expr (nth 1 a
) (nth 2 op
)))
662 (rhs (math-compose-expr (nth 2 a
) (nth 3 op
))))
663 (and (equal (car op
) "^")
664 (eq (math-comp-first-char lhs
) ?-
)
665 (setq lhs
(list 'horiz
"(" lhs
")")))
666 (and (eq calc-language
'tex
)
667 (or (equal (car op
) "^") (equal (car op
) "_"))
668 (not (and (stringp rhs
) (= (length rhs
) 1)))
669 (setq rhs
(list 'horiz
"{" rhs
"}")))
670 (or (and (eq (car a
) '*)
671 (or (null calc-language
)
672 (assoc "2x" math-expr-opers
))
673 (let* ((prevt (math-prod-last-term (nth 1 a
)))
674 (nextt (math-prod-first-term (nth 2 a
)))
675 (prevc (or (math-comp-last-char lhs
)
676 (and (memq (car-safe prevt
)
680 (eq calc-language
'big
)
682 (nextc (or (math-comp-first-char rhs
)
683 (and (memq (car-safe nextt
)
688 (eq calc-language
'big
)
691 (or (and (>= nextc ?a
) (<= nextc ?z
))
692 (and (>= nextc ?A
) (<= nextc ?Z
))
693 (and (>= nextc ?
0) (<= nextc ?
9))
694 (memq nextc
'(?. ?_ ?
#
699 (math-comp-first-string
701 (not (and (eq (car-safe prevt
) 'var
)
706 (list 'break math-compose-level
)
712 (list 'break math-compose-level
)
713 (if (or (equal (car op
) "^")
715 (equal (car op
) "**")
716 (and (equal (car op
) "*")
717 (math-comp-last-char lhs
)
718 (math-comp-first-char rhs
))
719 (and (equal (car op
) "/")
720 (math-num-integerp (nth 1 a
))
721 (math-integerp (nth 2 a
))))
723 (if (and (eq calc-language
'big
)
724 (equal (car op
) "=>"))
726 (concat " " (car op
) " ")))
728 ((and op
(= (length a
) 2) (= (nth 3 op
) -
1))
730 ((or (> prec
(or (nth 4 op
) (nth 2 op
)))
731 (and (not (eq (assoc (car op
) math-expr-opers
) op
))
732 (> prec
0))) ; don't write x% + y
733 (if (and (eq calc-language
'tex
)
734 (not (math-tex-expr-is-flat a
)))
735 (list 'horiz
"\\left( "
736 (math-compose-expr a -
1)
738 (if (eq calc-language
'eqn
)
739 (if (= (/ prec
100) 9)
740 (list 'horiz
"{" (math-compose-expr a -
1) "}")
741 (if (math-tex-expr-is-flat a
)
742 (list 'horiz
"{( " (math-compose-expr a -
1) " )}")
743 (list 'horiz
"{left ( "
744 (math-compose-expr a -
1)
746 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
748 (let ((lhs (math-compose-expr (nth 1 a
) (nth 2 op
))))
751 (if (or (> (length (car op
)) 1)
752 (not (math-comp-is-flat lhs
)))
753 (concat " " (car op
))
755 ((and op
(= (length a
) 2) (= (nth 2 op
) -
1))
758 (let ((lr (and (eq calc-language
'tex
)
759 (not (math-tex-expr-is-flat (nth 1 a
))))))
762 (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op
))
763 (substring (car op
) 1)
765 (if (or lr
(> (length (car op
)) 2)) " " "")
766 (math-compose-expr (nth 1 a
) -
1)
767 (if (or lr
(> (length (car op
)) 2)) " " "")
769 (car (nth 1 (memq op math-expr-opers
))))))
770 ((> prec
(or (nth 4 op
) (nth 3 op
)))
771 (if (and (eq calc-language
'tex
)
772 (not (math-tex-expr-is-flat a
)))
773 (list 'horiz
"\\left( "
774 (math-compose-expr a -
1)
776 (if (eq calc-language
'eqn
)
777 (if (= (/ prec
100) 9)
778 (list 'horiz
"{" (math-compose-expr a -
1) "}")
779 (if (math-tex-expr-is-flat a
)
780 (list 'horiz
"{( " (math-compose-expr a -
1) " )}")
781 (list 'horiz
"{left ( "
782 (math-compose-expr a -
1)
784 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
786 (let ((rhs (math-compose-expr (nth 1 a
) (nth 3 op
))))
788 (let ((ops (if (string-match "\\`u\\([^a-zA-Z]\\)\\'"
790 (substring (car op
) 1)
792 (if (or (> (length ops
) 1)
793 (not (math-comp-is-flat rhs
)))
797 ((and (eq calc-language
'big
)
798 (setq op
(get (car a
) 'math-compose-big
))
799 (funcall op a prec
)))
800 ((and (setq op
(assq calc-language
801 '( ( nil . math-compose-normal
)
802 ( flat . math-compose-normal
)
803 ( big . math-compose-normal
)
804 ( c . math-compose-c
)
805 ( pascal . math-compose-pascal
)
806 ( fortran . math-compose-fortran
)
807 ( tex . math-compose-tex
)
808 ( eqn . math-compose-eqn
)
809 ( math . math-compose-math
)
810 ( maple . math-compose-maple
))))
811 (setq op
(get (car a
) (cdr op
)))
812 (funcall op a prec
)))
814 (let* ((func (car a
))
815 (func2 (assq func
'(( mod . calcFunc-makemod
)
816 ( sdev . calcFunc-sdev
)
823 ( neg . calcFunc-neg
)
824 ( | . calcFunc-vconcat
))))
827 (setq func
(cdr func2
)))
828 (if (setq func2
(rassq func math-expr-function-mapping
))
829 (setq func
(car func2
)))
830 (setq func
(math-remove-dashes
832 "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
834 (math-match-substring (symbol-name func
) 1)
835 (symbol-name func
))))
836 (if (memq calc-language
'(c fortran pascal maple
))
837 (setq func
(math-to-underscores func
)))
838 (if (and (eq calc-language
'tex
)
840 (not (= calc-language-option
0))
841 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func
))
842 (if (< (prefix-numeric-value calc-language-option
) 0)
843 (setq func
(format "\\%s" func
))
844 (setq func
(format "\\hbox{%s}" func
))))
845 (if (and (eq calc-language
'eqn
)
846 (string-match "[^']'+\\'" func
))
847 (let ((n (- (length func
) (match-beginning 0) 1)))
848 (setq func
(substring func
0 (- n
)))
849 (while (>= (setq n
(1- n
)) 0)
850 (setq func
(concat func
" prime")))))
851 (cond ((and (eq calc-language
'tex
)
853 (not (math-tex-expr-is-flat (nth 1 a
)))))
854 (setq left
"\\left( "
856 ((and (eq calc-language
'eqn
)
858 (not (math-tex-expr-is-flat (nth 1 a
)))))
859 (setq left
"{left ( "
861 ((and (or (and (eq calc-language
'tex
)
862 (eq (aref func
0) ?
\\))
863 (and (eq calc-language
'eqn
)
864 (memq (car a
) math-eqn-special-funcs
)))
865 (not (string-match "\\hbox{" func
))
867 (or (Math-realp (nth 1 a
))
868 (memq (car (nth 1 a
)) '(var *))))
869 (setq left
(if (eq calc-language
'eqn
) "~{" "{")
871 ((eq calc-language
'eqn
)
874 (t (setq left calc-function-open
875 right calc-function-close
)))
876 (list 'horiz func left
877 (math-compose-vector (cdr a
)
878 (if (eq calc-language
'eqn
)
883 (defconst math-eqn-special-funcs
885 calcFunc-ln calcFunc-exp
886 calcFunc-sin calcFunc-cos calcFunc-tan
887 calcFunc-sinh calcFunc-cosh calcFunc-tanh
888 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
889 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh
893 (defun math-prod-first-term (x)
894 (while (eq (car-safe x
) '*)
898 (defun math-prod-last-term (x)
899 (while (eq (car-safe x
) '*)
903 (defun math-compose-vector (a sep prec
)
906 (cons (list 'set math-compose-level
)
907 (let ((c (list (math-compose-expr (car a
) prec
))))
908 (while (setq a
(cdr a
))
909 (setq c
(cons (if (eq (car-safe (car a
))
911 (let ((math-compose-level
912 (1- math-compose-level
)))
913 (math-compose-expr (car a
) -
123))
914 (math-compose-expr (car a
) prec
))
915 (cons (list 'break math-compose-level
)
920 (defun math-vector-no-parens (a)
922 (not (eq (car-safe (nth 1 a
)) '*))))
924 (defun math-compose-matrix (a col cols base
)
927 (while (<= (setq col
(1+ col
)) cols
)
928 (setq res
(cons (cons just
938 (concat comma-spc
" ")))))
943 (defun math-compose-rows (a count first
)
947 (math-compose-rows (cdr a
) -
1 nil
)
948 (cons (concat (if (eq calc-language
'tex
) " \\ldots" " ...")
950 (math-compose-rows (cdr a
) -
1 nil
)))
952 (if first
(concat left-bracket
" ") " ")
953 (math-compose-expr (car a
) vector-prec
)
955 (math-compose-rows (cdr a
) (1- count
) nil
)))
957 (if first
(concat left-bracket
" ") " ")
958 (math-compose-expr (car a
) vector-prec
)
959 (concat " " right-bracket
)))))
961 (defun math-compose-tex-matrix (a)
963 (cons (math-compose-vector (cdr (car a
)) " & " 0)
965 (math-compose-tex-matrix (cdr a
))))
966 (list (math-compose-vector (cdr (car a
)) " & " 0))))
968 (defun math-compose-eqn-matrix (a)
971 (cond ((eq calc-matrix-just
'right
) "rcol ")
972 ((eq calc-matrix-just
'center
) "ccol ")
975 (list 'break math-compose-level
)
979 (let ((math-compose-level (1+ math-compose-level
)))
980 (math-compose-vector (cdr (car a
)) " above " 1000))
983 (math-compose-eqn-matrix (cdr a
)))))))
986 (defun math-vector-is-string (a)
987 (while (and (setq a
(cdr a
))
988 (or (and (natnump (car a
))
990 (and (eq (car-safe (car a
)) 'cplx
)
991 (natnump (nth 1 (car a
)))
992 (eq (nth 2 (car a
)) 0)
993 (<= (nth 1 (car a
)) 255)))))
996 (defun math-vector-to-string (a &optional quoted
)
997 (setq a
(concat (mapcar (function (lambda (x) (if (consp x
) (nth 1 x
) x
)))
999 (if (string-match "[\000-\037\177\\\"]" a
)
1001 (pat (if quoted
"[\000-\037\177\\\"]" "[\000-\037\177]"))
1002 (codes (if quoted math-vector-to-string-chars
'((?\^? .
"^?"))))
1003 (fmt (if quoted
"\\^%c" "^%c"))
1005 (while (setq p
(string-match pat a p
))
1006 (if (setq new
(assq (aref a p
) codes
))
1007 (setq a
(concat (substring a
0 p
)
1009 (substring a
(1+ p
)))
1010 p
(+ p
(length (cdr new
))))
1011 (setq a
(concat (substring a
0 p
)
1012 (format fmt
(+ (aref a p
) 64))
1013 (substring a
(1+ p
)))
1016 (concat "\"" a
"\"")
1018 (defconst math-vector-to-string-chars
'( ( ?
\" .
"\\\"" )
1030 (defun math-to-underscores (x)
1031 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
1032 (math-to-underscores
1033 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
1036 (defun math-tex-expr-is-flat (a)
1037 (or (Math-integerp a
)
1038 (memq (car a
) '(float var
))
1039 (and (memq (car a
) '(+ -
* neg
))
1041 (while (and (setq a
(cdr a
))
1042 (math-tex-expr-is-flat (car a
))))
1044 (and (memq (car a
) '(^ calcFunc-subscr
))
1045 (math-tex-expr-is-flat (nth 1 a
)))))
1047 (put 'calcFunc-log
'math-compose-big
'math-compose-log
)
1048 (defun math-compose-log (a prec
)
1049 (and (= (length a
) 3)
1052 (let ((calc-language 'flat
))
1053 (math-compose-expr (nth 2 a
) 1000)))
1055 (math-compose-expr (nth 1 a
) 1000)
1058 (put 'calcFunc-log10
'math-compose-big
'math-compose-log10
)
1059 (defun math-compose-log10 (a prec
)
1060 (and (= (length a
) 2)
1062 (list 'subscr
"log" "10")
1064 (math-compose-expr (nth 1 a
) 1000)
1067 (put 'calcFunc-deriv
'math-compose-big
'math-compose-deriv
)
1068 (put 'calcFunc-tderiv
'math-compose-big
'math-compose-deriv
)
1069 (defun math-compose-deriv (a prec
)
1070 (and (= (length a
) 3)
1071 (math-compose-expr (list '/
1072 (list 'calcFunc-choriz
1074 '(calcFunc-string (vec ?d
))
1076 (list 'calcFunc-choriz
1078 '(calcFunc-string (vec ?d
))
1082 (put 'calcFunc-sqrt
'math-compose-big
'math-compose-sqrt
)
1083 (defun math-compose-sqrt (a prec
)
1084 (and (= (length a
) 2)
1085 (let* ((c (math-compose-expr (nth 1 a
) 0))
1086 (a (math-comp-ascent c
))
1087 (d (math-comp-descent c
))
1089 (w (math-comp-width c
)))
1092 (concat (if (= h
1) " " " ")
1093 (make-string (+ w
2) ?\_
))
1097 (append (list 'vleft
(1- a
))
1098 (make-list (1- h
) " |")
1103 (put 'calcFunc-choose
'math-compose-big
'math-compose-choose
)
1104 (defun math-compose-choose (a prec
)
1105 (let ((a1 (math-compose-expr (nth 1 a
) 0))
1106 (a2 (math-compose-expr (nth 2 a
) 0)))
1110 (math-comp-height a1
)
1114 (put 'calcFunc-integ
'math-compose-big
'math-compose-integ
)
1115 (defun math-compose-integ (a prec
)
1116 (and (memq (length a
) '(3 5))
1117 (eq (car-safe (nth 2 a
)) 'var
)
1118 (let* ((parens (and (>= prec
196) (/= prec
1000)))
1119 (var (math-compose-expr (nth 2 a
) 0))
1120 (over (and (eq (car-safe (nth 2 a
)) 'var
)
1121 (or (and (eq (car-safe (nth 1 a
)) '/)
1122 (math-numberp (nth 1 (nth 1 a
))))
1123 (and (eq (car-safe (nth 1 a
)) '^
)
1124 (math-looks-negp (nth 2 (nth 1 a
)))))))
1125 (expr (math-compose-expr (if over
1127 (math-build-var-name
1130 (nth 1 (nth 2 a
)))))
1132 (calc-language 'flat
)
1133 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1134 (high (and (nth 4 a
) (math-compose-expr (nth 4 a
) 0))))
1137 (append (list 'vcent
(if high
3 2))
1138 (and high
(list (list 'horiz
" " high
)))
1144 (and low
(list (list 'horiz low
" "))))
1148 (list 'horiz
" d" var
))
1149 (if parens
")" "")))))
1151 (put 'calcFunc-sum
'math-compose-big
'math-compose-sum
)
1152 (defun math-compose-sum (a prec
)
1153 (and (memq (length a
) '(3 5 6))
1154 (let* ((expr (math-compose-expr (nth 1 a
) 185))
1155 (calc-language 'flat
)
1156 (var (math-compose-expr (nth 2 a
) 0))
1157 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1158 (high (and (nth 4 a
) (math-compose-vector (nthcdr 4 a
) ", " 0))))
1160 (if (memq prec
'(180 201)) "(" "")
1161 (append (list 'vcent
(if high
3 2))
1162 (and high
(list high
))
1169 (list (list 'horiz var
" = " low
))
1171 (if (memq (car-safe (nth 1 a
)) '(calcFunc-sum calcFunc-prod
))
1174 (if (memq prec
'(180 201)) ")" "")))))
1176 (put 'calcFunc-prod
'math-compose-big
'math-compose-prod
)
1177 (defun math-compose-prod (a prec
)
1178 (and (memq (length a
) '(3 5 6))
1179 (let* ((expr (math-compose-expr (nth 1 a
) 198))
1180 (calc-language 'flat
)
1181 (var (math-compose-expr (nth 2 a
) 0))
1182 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1183 (high (and (nth 4 a
) (math-compose-vector (nthcdr 4 a
) ", " 0))))
1185 (if (memq prec
'(196 201)) "(" "")
1186 (append (list 'vcent
(if high
3 2))
1187 (and high
(list high
))
1193 (list (list 'horiz var
" = " low
))
1195 (if (memq (car-safe (nth 1 a
)) '(calcFunc-sum calcFunc-prod
))
1198 (if (memq prec
'(196 201)) ")" "")))))
1201 (defun math-stack-value-offset-fancy ()
1202 (let ((cwid (+ (math-comp-width c
))))
1203 (cond ((eq calc-display-just
'right
)
1204 (if calc-display-origin
1205 (setq wid
(max calc-display-origin
5))
1206 (if (integerp calc-line-breaking
)
1207 (setq wid calc-line-breaking
)))
1208 (setq off
(- wid cwid
1209 (max (- (length calc-right-label
)
1210 (if (and (integerp calc-line-breaking
)
1211 calc-display-origin
)
1212 (max (- calc-line-breaking
1213 calc-display-origin
)
1218 (if calc-display-origin
1220 (setq off
(- calc-display-origin
(/ cwid
2)))
1221 (if (integerp calc-line-breaking
)
1222 (setq off
(min off
(- calc-line-breaking cwid
1223 (length calc-right-label
)))))
1225 (setq wid
(max wid
(+ off cwid
)))))
1226 (if (integerp calc-line-breaking
)
1227 (setq wid calc-line-breaking
))
1228 (setq off
(/ (- wid cwid
) 2)))))
1229 (and (integerp calc-line-breaking
)
1231 (and calc-display-origin
1232 (> calc-line-breaking calc-display-origin
)))
1233 (setq wid calc-line-breaking
))))
1237 ;;; Convert a composition to string form, with embedded \n's if necessary.
1239 (defun math-composition-to-string (c &optional width
)
1240 (or width
(setq width
(calc-window-width)))
1241 (if calc-display-raw
1242 (math-comp-to-string-raw c
0)
1243 (if (math-comp-is-flat c
)
1244 (math-comp-to-string-flat c width
)
1245 (math-vert-comp-to-string
1246 (math-comp-simplify c width
)))))
1248 (defun math-comp-is-flat (c) ; check if c's height is 1.
1249 (cond ((not (consp c
)) t
)
1250 ((memq (car c
) '(set break
)) t
)
1251 ((eq (car c
) 'horiz
)
1252 (while (and (setq c
(cdr c
))
1253 (math-comp-is-flat (car c
))))
1255 ((memq (car c
) '(vleft vcent vright
))
1256 (and (= (length c
) 3)
1258 (math-comp-is-flat (nth 2 c
))))
1260 (math-comp-is-flat (nth 2 c
)))
1264 ;;; Convert a one-line composition to a string. Break into multiple
1265 ;;; lines if necessary, choosing break points according to the structure
1268 (defun math-comp-to-string-flat (c full-width
)
1269 (if math-comp-sel-hpos
1271 (math-comp-sel-flat-term c
))
1276 (comp-highlight (and math-comp-selected calc-show-selections
))
1278 (math-comp-to-string-flat-term '(set -
1 0))
1279 (math-comp-to-string-flat-term c
)
1280 (math-comp-to-string-flat-term '(break -
1))
1281 (let ((str (aref math-comp-buf-string
0))
1283 (and (> (length str
) 0) (= (aref str
0) ?
)
1284 (> (length comp-buf
) 0)
1285 (let ((k (length comp-buf
)))
1286 (while (not (= (aref comp-buf
(setq k
(1- k
))) ?
\n)))
1287 (aset comp-buf k ?
)
1288 (if (and (< (1+ k
) (length comp-buf
))
1289 (= (aref comp-buf
(1+ k
)) ?
))
1291 (aset comp-buf
(1+ k
) ?
\n)
1293 (setq prefix
"\n"))))
1294 (concat comp-buf prefix str
)))))
1295 (setq math-comp-buf-string
(make-vector 10 ""))
1296 (setq math-comp-buf-margin
(make-vector 10 0))
1297 (setq math-comp-buf-level
(make-vector 10 0))
1299 (defun math-comp-to-string-flat-term (c)
1300 (cond ((not (consp c
))
1302 (setq c
(math-comp-highlight-string c
)))
1303 (setq comp-word
(if (= (length comp-word
) 0) c
(concat comp-word c
))
1304 comp-pos
(+ comp-pos
(length c
))))
1306 ((eq (car c
) 'horiz
)
1307 (while (setq c
(cdr c
))
1308 (math-comp-to-string-flat-term (car c
))))
1313 (setq comp-level
(1+ comp-level
))
1314 (if (>= comp-level
(length math-comp-buf-string
))
1315 (setq math-comp-buf-string
(vconcat math-comp-buf-string
1316 math-comp-buf-string
)
1317 math-comp-buf-margin
(vconcat math-comp-buf-margin
1318 math-comp-buf-margin
)
1319 math-comp-buf-level
(vconcat math-comp-buf-level
1320 math-comp-buf-level
)))
1321 (aset math-comp-buf-string comp-level
"")
1322 (aset math-comp-buf-margin comp-level
(+ comp-pos
1324 (aset math-comp-buf-level comp-level
(nth 1 c
)))))
1326 ((eq (car c
) 'break
)
1327 (if (not calc-line-breaking
)
1328 (setq comp-buf
(concat comp-buf comp-word
)
1331 (if (and (> comp-pos full-width
)
1334 (setq str
(aref math-comp-buf-string i
))
1335 (and (= (length str
) 0) (< i comp-level
)))
1337 (or (> (length str
) 0) (> (length comp-buf
) 0))))
1338 (let ((prefix "") mrg wid
)
1339 (setq mrg
(aref math-comp-buf-margin i
))
1340 (if (> mrg
12) ; indenting too far, go back to far left
1341 (let ((j i
) (new (if calc-line-numbering
5 1)))
1342 '(while (<= j comp-level
)
1343 (aset math-comp-buf-margin j
1344 (+ (aref math-comp-buf-margin j
) (- new mrg
)))
1347 (setq wid
(+ (length str
) comp-margin
))
1348 (and (> (length str
) 0) (= (aref str
0) ?
)
1349 (> (length comp-buf
) 0)
1350 (let ((k (length comp-buf
)))
1351 (while (not (= (aref comp-buf
(setq k
(1- k
))) ?
\n)))
1352 (aset comp-buf k ?
)
1353 (if (and (< (1+ k
) (length comp-buf
))
1354 (= (aref comp-buf
(1+ k
)) ?
))
1356 (aset comp-buf
(1+ k
) ?
\n)
1358 (setq prefix
"\n"))))
1359 (setq comp-buf
(concat comp-buf prefix str
"\n"
1360 (make-string mrg ?
))
1361 comp-pos
(+ comp-pos
(- mrg wid
))
1363 (aset math-comp-buf-string i
"")
1364 (while (<= (setq i
(1+ i
)) comp-level
)
1365 (if (> (aref math-comp-buf-margin i
) wid
)
1366 (aset math-comp-buf-margin i
1367 (+ (aref math-comp-buf-margin i
)
1369 (if (and (= (nth 1 c
) (aref math-comp-buf-level comp-level
))
1370 (< comp-pos
(+ (aref math-comp-buf-margin comp-level
) 2)))
1371 () ; avoid stupid breaks, e.g., "1 +\n really_long_expr"
1372 (let ((str (aref math-comp-buf-string comp-level
)))
1373 (setq str
(if (= (length str
) 0)
1375 (concat str comp-word
))
1377 (while (< (nth 1 c
) (aref math-comp-buf-level comp-level
))
1378 (setq comp-level
(1- comp-level
))
1379 (or (= (length (aref math-comp-buf-string comp-level
)) 0)
1380 (setq str
(concat (aref math-comp-buf-string comp-level
)
1382 (aset math-comp-buf-string comp-level str
)))))
1385 (cond ((eq (nth 1 c
) math-comp-selected
)
1386 (let ((comp-highlight (not calc-show-selections
)))
1387 (math-comp-to-string-flat-term (nth 2 c
))))
1389 (let ((comp-highlight nil
))
1390 (math-comp-to-string-flat-term (nth 2 c
))))
1391 (t (math-comp-to-string-flat-term (nth 2 c
)))))
1393 (t (math-comp-to-string-flat-term (nth 2 c
)))))
1395 (defun math-comp-highlight-string (s)
1396 (setq s
(copy-sequence s
))
1397 (let ((i (length s
)))
1398 (while (>= (setq i
(1- i
)) 0)
1399 (or (memq (aref s i
) '(32 ?
\n))
1400 (aset s i
(if calc-show-selections ?\. ?\
#)))))
1403 (defun math-comp-sel-flat-term (c)
1404 (cond ((not (consp c
))
1405 (setq comp-pos
(+ comp-pos
(length c
))))
1406 ((memq (car c
) '(set break
)))
1407 ((eq (car c
) 'horiz
)
1408 (while (and (setq c
(cdr c
)) (< math-comp-sel-cpos
1000000))
1409 (math-comp-sel-flat-term (car c
))))
1411 (if (<= comp-pos math-comp-sel-cpos
)
1413 (math-comp-sel-flat-term (nth 2 c
))
1414 (if (> comp-pos math-comp-sel-cpos
)
1415 (setq math-comp-sel-tag c
1416 math-comp-sel-cpos
1000000)))
1417 (math-comp-sel-flat-term (nth 2 c
))))
1418 (t (math-comp-sel-flat-term (nth 2 c
)))))
1421 ;;; Simplify a composition to a canonical form consisting of
1422 ;;; (vleft n "string" "string" "string" ...)
1423 ;;; where 0 <= n < number-of-strings.
1425 (defun math-comp-simplify (c full-width
)
1426 (let ((comp-buf (list ""))
1431 (comp-highlight (and math-comp-selected calc-show-selections
))
1433 (math-comp-simplify-term c
)
1434 (cons 'vleft
(cons comp-base comp-buf
))))
1436 (defun math-comp-add-string (s h v
)
1437 (and (> (length s
) 0)
1438 (let ((vv (+ v comp-base
)))
1439 (if math-comp-sel-hpos
1440 (math-comp-add-string-sel h vv
(length s
) 1)
1442 (setq comp-buf
(nconc (make-list (- vv
) "") comp-buf
)
1444 comp-height
(- comp-height vv
)
1446 (if (>= vv comp-height
)
1447 (setq comp-buf
(nconc comp-buf
1448 (make-list (1+ (- vv comp-height
)) ""))
1449 comp-height
(1+ vv
))))
1450 (let ((str (nthcdr vv comp-buf
)))
1451 (setcar str
(concat (car str
)
1452 (make-string (- h
(length (car str
))) 32)
1454 (math-comp-highlight-string s
)
1457 (defun math-comp-add-string-sel (x y w h
)
1458 (if (and (<= y math-comp-sel-vpos
)
1459 (> (+ y h
) math-comp-sel-vpos
)
1460 (<= x math-comp-sel-hpos
)
1461 (> (+ x w
) math-comp-sel-hpos
))
1462 (setq math-comp-sel-tag comp-tag
1463 math-comp-sel-vpos
10000)))
1465 (defun math-comp-simplify-term (c)
1467 (math-comp-add-string c comp-hpos comp-vpos
)
1468 (setq comp-hpos
(+ comp-hpos
(length c
))))
1469 ((memq (car c
) '(set break
))
1471 ((eq (car c
) 'horiz
)
1472 (while (setq c
(cdr c
))
1473 (math-comp-simplify-term (car c
))))
1474 ((memq (car c
) '(vleft vcent vright
))
1475 (let* ((comp-vpos (+ (- comp-vpos
(nth 1 c
))
1476 (1- (math-comp-ascent (nth 2 c
)))))
1477 (widths (mapcar 'math-comp-width
(cdr (cdr c
))))
1478 (maxwid (apply 'max widths
))
1479 (bias (cond ((eq (car c
) 'vleft
) 0)
1480 ((eq (car c
) 'vcent
) 1)
1483 (while (setq c
(cdr c
))
1484 (if (eq (car-safe (car c
)) 'rule
)
1485 (math-comp-add-string (make-string maxwid
(nth 1 (car c
)))
1486 comp-hpos comp-vpos
)
1487 (let ((comp-hpos (+ comp-hpos
(/ (* bias
(- maxwid
1490 (math-comp-simplify-term (car c
))))
1492 (setq comp-vpos
(+ comp-vpos
1493 (+ (math-comp-descent (car c
))
1494 (math-comp-ascent (nth 1 c
))))
1495 widths
(cdr widths
))))
1496 (setq comp-hpos
(+ comp-hpos maxwid
))))
1497 ((eq (car c
) 'supscr
)
1498 (let* ((asc (or 1 (math-comp-ascent (nth 1 c
))))
1499 (desc (math-comp-descent (nth 2 c
)))
1502 (math-comp-simplify-term (nth 1 c
))))
1503 (comp-vpos (- comp-vpos
(+ asc desc
))))
1504 (math-comp-simplify-term (nth 2 c
))
1505 (if math-comp-sel-hpos
1506 (math-comp-add-string-sel oldh
1509 (math-comp-ascent (nth 2 c
)))
1511 (math-comp-height c
)))))
1512 ((eq (car c
) 'subscr
)
1513 (let* ((asc (math-comp-ascent (nth 2 c
)))
1514 (desc (math-comp-descent (nth 1 c
)))
1518 (math-comp-simplify-term (nth 1 c
))))
1519 (comp-vpos (+ comp-vpos
(+ asc desc
))))
1520 (math-comp-simplify-term (nth 2 c
))
1521 (if math-comp-sel-hpos
1522 (math-comp-add-string-sel oldh oldv
1524 (math-comp-height c
)))))
1526 (cond ((eq (nth 1 c
) math-comp-selected
)
1527 (let ((comp-highlight (not calc-show-selections
)))
1528 (math-comp-simplify-term (nth 2 c
))))
1530 (let ((comp-highlight nil
))
1531 (math-comp-simplify-term (nth 2 c
))))
1532 (t (let ((comp-tag c
))
1533 (math-comp-simplify-term (nth 2 c
))))))))
1536 ;;; Measuring a composition.
1538 (defun math-comp-first-char (c)
1540 (and (> (length c
) 0)
1542 ((memq (car c
) '(horiz subscr supscr
))
1543 (while (and (setq c
(cdr c
))
1544 (math-comp-is-null (car c
))))
1545 (and c
(math-comp-first-char (car c
))))
1547 (math-comp-first-char (nth 2 c
)))))
1549 (defun math-comp-first-string (c)
1551 (and (> (length c
) 0)
1553 ((eq (car c
) 'horiz
)
1554 (while (and (setq c
(cdr c
))
1555 (math-comp-is-null (car c
))))
1556 (and c
(math-comp-first-string (car c
))))
1558 (math-comp-first-string (nth 2 c
)))))
1560 (defun math-comp-last-char (c)
1562 (and (> (length c
) 0)
1563 (elt c
(1- (length c
)))))
1564 ((eq (car c
) 'horiz
)
1565 (let ((c (reverse (cdr c
))))
1566 (while (and c
(math-comp-is-null (car c
)))
1568 (and c
(math-comp-last-char (car c
)))))
1570 (math-comp-last-char (nth 2 c
)))))
1572 (defun math-comp-is-null (c)
1573 (cond ((stringp c
) (= (length c
) 0))
1574 ((memq (car c
) '(horiz subscr supscr
))
1575 (while (and (setq c
(cdr c
))
1576 (math-comp-is-null (car c
))))
1579 (math-comp-is-null (nth 2 c
)))
1580 ((memq (car c
) '(set break
)) t
)))
1582 (defun math-comp-width (c)
1583 (cond ((not (consp c
)) (length c
))
1584 ((memq (car c
) '(horiz subscr supscr
))
1586 (while (setq c
(cdr c
))
1587 (setq accum
(+ accum
(math-comp-width (car c
)))))
1589 ((memq (car c
) '(vcent vleft vright
))
1592 (while (setq c
(cdr c
))
1593 (setq accum
(max accum
(math-comp-width (car c
)))))
1596 (math-comp-width (nth 2 c
)))
1599 (defun math-comp-height (c)
1602 (+ (math-comp-ascent c
) (math-comp-descent c
))))
1604 (defun math-comp-ascent (c)
1605 (cond ((not (consp c
)) 1)
1606 ((eq (car c
) 'horiz
)
1608 (while (setq c
(cdr c
))
1609 (setq accum
(max accum
(math-comp-ascent (car c
)))))
1611 ((memq (car c
) '(vcent vleft vright
))
1612 (if (> (nth 1 c
) 0) (1+ (nth 1 c
)) 1))
1613 ((eq (car c
) 'supscr
)
1614 (max (math-comp-ascent (nth 1 c
)) (1+ (math-comp-height (nth 2 c
)))))
1615 ((eq (car c
) 'subscr
)
1616 (math-comp-ascent (nth 1 c
)))
1618 (math-comp-ascent (nth 2 c
)))
1621 (defun math-comp-descent (c)
1622 (cond ((not (consp c
)) 0)
1623 ((eq (car c
) 'horiz
)
1625 (while (setq c
(cdr c
))
1626 (setq accum
(max accum
(math-comp-descent (car c
)))))
1628 ((memq (car c
) '(vcent vleft vright
))
1629 (let ((accum (- (nth 1 c
))))
1631 (while (setq c
(cdr c
))
1632 (setq accum
(+ accum
(math-comp-height (car c
)))))
1633 (max (1- accum
) 0)))
1634 ((eq (car c
) 'supscr
)
1635 (math-comp-descent (nth 1 c
)))
1636 ((eq (car c
) 'subscr
)
1637 (+ (math-comp-descent (nth 1 c
)) (math-comp-height (nth 2 c
))))
1639 (math-comp-descent (nth 2 c
)))
1642 (defun calcFunc-cwidth (a &optional prec
)
1643 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1644 (math-comp-width (math-compose-expr a
(or prec
0))))
1646 (defun calcFunc-cheight (a &optional prec
)
1647 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1648 (if (and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
1649 (memq (length a
) '(2 3))
1652 (math-comp-height (math-compose-expr a
(or prec
0)))))
1654 (defun calcFunc-cascent (a &optional prec
)
1655 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1656 (if (and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
1657 (memq (length a
) '(2 3))
1660 (math-comp-ascent (math-compose-expr a
(or prec
0)))))
1662 (defun calcFunc-cdescent (a &optional prec
)
1663 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1664 (math-comp-descent (math-compose-expr a
(or prec
0))))
1667 ;;; Convert a simplified composition into string form.
1669 (defun math-vert-comp-to-string (c)
1672 (math-vert-comp-to-string-step (cdr (cdr c
)))))
1674 (defun math-vert-comp-to-string-step (c)
1676 (concat (car c
) "\n" (math-vert-comp-to-string-step (cdr c
)))
1680 ;;; Convert a composition to a string in "raw" form (for debugging).
1682 (defun math-comp-to-string-raw (c indent
)
1683 (cond ((or (not (consp c
)) (eq (car c
) 'set
))
1684 (prin1-to-string c
))
1686 (concat "(" (symbol-name (car c
)) ")"))
1688 (let ((next-indent (+ indent
2 (length (symbol-name (car c
))))))
1690 (symbol-name (car c
))
1692 (math-comp-to-string-raw (nth 1 c
) next-indent
)
1693 (math-comp-to-string-raw-step (cdr (cdr c
))
1697 (defun math-comp-to-string-raw-step (cl indent
)
1700 (make-string indent
32)
1701 (math-comp-to-string-raw (car cl
) indent
)
1702 (math-comp-to-string-raw-step (cdr cl
) indent
))
1705 ;;; calccomp.el ends here