1 ;;; calccomp.el --- composition functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
33 ;;; A "composition" has one of the following forms:
35 ;;; "string" A literal string
37 ;;; (horiz C1 C2 ...) Horizontally abutted sub-compositions
39 ;;; (set LEVEL OFF) Set left margin + offset for line-break level
40 ;;; (break LEVEL) A potential line-break point
42 ;;; (vleft N C1 C2 ...) Vertically stacked, left-justified sub-comps
43 ;;; (vcent N C1 C2 ...) Vertically stacked, centered sub-comps
44 ;;; (vright N C1 C2 ...) Vertically stacked, right-justified sub-comps
45 ;;; N specifies baseline of the stack, 0=top line.
47 ;;; (supscr C1 C2) Composition C1 with superscript C2
48 ;;; (subscr C1 C2) Composition C1 with subscript C2
49 ;;; (rule X) Horizontal line of X, full width of enclosing comp
51 ;;; (tag X C) Composition C corresponds to sub-expression X
53 ;; math-comp-just and math-comp-comma-spc are local to
54 ;; math-compose-expr, but are used by math-compose-matrix, which is
55 ;; called by math-compose-expr
56 (defvar math-comp-just
)
57 (defvar math-comp-comma-spc
)
59 ;; math-comp-vector-prec is local to math-compose-expr, but is used by
60 ;; math-compose-matrix and math-compose-rows, which are called by
62 (defvar math-comp-vector-prec
)
64 ;; math-comp-left-bracket, math-comp-right-bracket and math-comp-comma are
65 ;; local to math-compose-expr, but are used by math-compose-rows, which is
66 ;; called by math-compose-expr.
67 (defvar math-comp-left-bracket
)
68 (defvar math-comp-right-bracket
)
69 (defvar math-comp-comma
)
71 (defun math-compose-var (a)
73 (if (and math-compose-hash-args
74 (let ((p calc-arg-values
))
76 (while (and p
(not (equal (car p
) a
)))
77 (setq p
(and (eq math-compose-hash-args t
) (cdr p
))
80 (if (eq math-compose-hash-args
1)
83 (setq sn
(symbol-name (nth 1 a
)))
84 (if (memq calc-language calc-lang-allow-percentsigns
)
85 (setq sn
(math-to-percentsigns sn
)))
86 (if (memq calc-language calc-lang-allow-underscores
)
87 (setq sn
(math-to-underscores sn
)))
90 (defun math-compose-expr (a prec
)
91 (let ((math-compose-level (1+ math-compose-level
))
92 (math-expr-opers (math-expr-ops))
95 ((or (and (eq a math-comp-selected
) a
)
97 (not (eq math-comp-tagged a
))))
98 (let ((math-comp-selected nil
))
99 (and math-comp-tagged
(setq math-comp-tagged a
))
100 (list 'tag a
(math-compose-expr a prec
))))
101 ((and (not (consp a
)) (not (integerp a
)))
102 (concat "'" (prin1-to-string a
)))
103 ((setq spfn
(assq (car-safe a
)
104 (get calc-language
'math-special-function-table
)))
105 (setq spfn
(cdr spfn
))
107 (funcall (car spfn
) a spfn
)
110 (if (or (eq (car-safe a
) 'frac
)
111 (and (nth 1 calc-frac-format
) (Math-integerp a
)))
114 (not (memq calc-language
115 '(flat big unform
))))
116 (let ((aa (math-adjust-fraction a
))
117 (calc-frac-format nil
))
118 (math-compose-expr (list '/
119 (if (memq calc-language
120 calc-lang-slash-idiv
)
121 (math-float (nth 1 aa
))
124 (if (and (eq calc-language
'big
)
125 (= (length (car calc-frac-format
)) 1))
126 (let* ((aa (math-adjust-fraction a
))
127 (calc-frac-format nil
)
128 (math-radix-explicit-format nil
)
130 (if (math-negp (nth 1 aa
))
134 (math-abs (nth 1 aa
)))
136 (math-format-number (nth 2 aa
))))))
137 (if (= calc-number-radix
10)
141 (int-to-string calc-number-radix
)))))
142 (math-format-number a
)))
143 (if (not (eq calc-language
'big
))
144 (math-format-number a prec
)
145 (if (memq (car-safe a
) '(cplx polar
))
146 (if (math-zerop (nth 2 a
))
147 (math-compose-expr (nth 1 a
) prec
)
149 (math-compose-expr (nth 1 a
) 0)
150 (if (eq (car a
) 'cplx
) ", " "; ")
151 (math-compose-expr (nth 2 a
) 0) ")"))
152 (if (or (= calc-number-radix
10)
154 (and calc-group-digits
155 (not (assoc calc-group-char
'((",") (" "))))))
156 (math-format-number a prec
)
157 (let ((s (math-format-number a prec
))
159 (while (string-match (if (> calc-number-radix
14)
160 "\\([0-9]+\\)#\\([0-9a-zA-Z., ]+\\)"
161 "\\([0-9]+\\)#\\([0-9a-dA-D., ]+\\)")
163 (setq c
(nconc c
(list (substring s
0 (match-beginning 0))
165 (math-match-substring s
2)
166 (math-match-substring s
1))))
167 s
(substring s
(match-end 0))))
169 "\\*\\([0-9.]+\\)\\^\\(-?[0-9]+\\)\\()?\\)\\'" s
)
171 (substring s
0 (match-beginning 0)) " "
173 (math-match-substring s
1)
174 (math-match-substring s
2))
175 (math-match-substring s
3))))
176 (if c
(cons 'horiz
(nconc c
(list s
))) s
)))))))
177 ((and (get (car a
) 'math-compose-forms
)
178 (not (eq calc-language
'unform
))
179 (let ((comps (get (car a
) 'math-compose-forms
))
181 (or (and (setq temp
(assq calc-language comps
))
182 (or (and (setq temp2
(assq (1- (length a
)) (cdr temp
)))
183 (setq temp
(apply (cdr temp2
) (cdr a
)))
184 (math-compose-expr temp prec
))
185 (and (setq temp2
(assq nil
(cdr temp
)))
186 (funcall (cdr temp2
) a
))))
187 (and (setq temp
(assq nil comps
))
188 (or (and (setq temp2
(assq (1- (length a
)) (cdr temp
)))
189 (setq temp
(apply (cdr temp2
) (cdr a
)))
190 (math-compose-expr temp prec
))
191 (and (setq temp2
(assq nil
(cdr temp
)))
192 (funcall (cdr temp2
) a
))))))))
194 (let* ((math-comp-left-bracket (if calc-vector-brackets
195 (substring calc-vector-brackets
0 1) ""))
196 (math-comp-right-bracket (if calc-vector-brackets
197 (substring calc-vector-brackets
1 2) ""))
198 (inner-brackets (memq 'R calc-matrix-brackets
))
199 (outer-brackets (memq 'O calc-matrix-brackets
))
200 (row-commas (memq 'C calc-matrix-brackets
))
201 (math-comp-comma-spc (or calc-vector-commas
" "))
202 (math-comp-comma (or calc-vector-commas
""))
203 (math-comp-vector-prec (if (or (and calc-vector-commas
204 (math-vector-no-parens a
))
205 (memq 'P calc-matrix-brackets
)) 0 1000))
206 (math-comp-just (cond ((eq calc-matrix-just
'right
) 'vright
)
207 ((eq calc-matrix-just
'center
) 'vcent
)
209 (break calc-break-vectors
))
210 (if (and (memq calc-language
'(nil big
))
211 (not calc-break-vectors
)
212 (math-matrixp a
) (not (math-matrixp (nth 1 a
)))
213 (or calc-full-vectors
214 (and (< (length a
) 7) (< (length (nth 1 a
)) 7))
215 (progn (setq break t
) nil
)))
217 (setq math-comp-vector-prec
(if (or (and calc-vector-commas
218 (math-vector-no-parens
220 (memq 'P calc-matrix-brackets
))
224 (concat math-comp-left-bracket math-comp-left-bracket
" ")
225 (math-compose-vector (cdr (nth 1 a
)) (concat math-comp-comma
" ")
226 math-comp-vector-prec
)
227 (concat " " math-comp-right-bracket math-comp-right-bracket
))
228 (let* ((rows (1- (length a
)))
229 (cols (1- (length (nth 1 a
))))
230 (base (/ (1- rows
) 2))
231 (calc-language 'flat
))
233 (list (append '(vleft)
235 (list (concat (and outer-brackets
236 (concat math-comp-left-bracket
239 (concat math-comp-left-bracket
242 (concat (and outer-brackets
246 math-comp-left-bracket
248 (math-compose-matrix (cdr a
) 1 cols base
)
249 (list (append '(vleft)
254 math-comp-right-bracket
257 (if (and outer-brackets
263 math-comp-right-bracket
))
267 math-comp-right-bracket
)))))))))
268 (if (and calc-display-strings
270 (math-vector-is-string a
))
271 (math-vector-to-string a t
)
272 (if (and break
(cdr a
)
273 (not (eq calc-language
'flat
)))
274 (let* ((full (or calc-full-vectors
(< (length a
) 7)))
275 (rows (if full
(1- (length a
)) 5))
276 (base (/ (1- rows
) 2))
277 (calc-break-vectors nil
))
279 (cons 'vleft
(cons base
282 (if full rows
3) t
)))))
283 (if (or calc-full-vectors
(< (length a
) 7))
285 (setq spfn
(get calc-language
'math-matrix-formatter
))
289 math-comp-left-bracket
290 (math-compose-vector (cdr a
)
291 (concat math-comp-comma
" ")
292 math-comp-vector-prec
)
293 math-comp-right-bracket
))
295 math-comp-left-bracket
296 (math-compose-vector (list (nth 1 a
) (nth 2 a
) (nth 3 a
))
297 (concat math-comp-comma
" ")
298 math-comp-vector-prec
)
300 (if (setq spfn
(get calc-language
'math-dots
))
304 (list 'break math-compose-level
)
305 (math-compose-expr (nth (1- (length a
)) a
)
306 (if (equal math-comp-comma
"") 1000 0))
307 math-comp-right-bracket
)))))))
308 ((eq (car a
) 'incomplete
)
310 (cond ((eq (nth 1 a
) 'vec
)
312 (math-compose-vector (cdr (cdr a
)) ", " 0)
314 ((eq (nth 1 a
) 'cplx
)
316 (math-compose-vector (cdr (cdr a
)) ", " 0)
318 ((eq (nth 1 a
) 'polar
)
320 (math-compose-vector (cdr (cdr a
)) "; " 0)
322 ((eq (nth 1 a
) 'intv
)
324 (if (memq (nth 2 a
) '(0 1)) "(" "[")
325 (math-compose-vector (cdr (cdr (cdr a
))) " .. " 0)
328 (cond ((eq (nth 1 a
) 'vec
) "[ ...")
329 ((eq (nth 1 a
) 'intv
)
330 (if (memq (nth 2 a
) '(0 1)) "( ..." "[ ..."))
333 (let ((v (rassq (nth 2 a
) math-expr-variable-mapping
)))
335 (symbol-name (car v
))
336 (if (setq spfn
(get calc-language
'math-var-formatter
))
337 (funcall spfn a prec
)
338 (math-compose-var a
)))))
341 (if (memq (nth 1 a
) '(0 1)) "(" "[")
342 (math-compose-expr (nth 2 a
) 0)
344 (math-compose-expr (nth 3 a
) 0)
345 (if (memq (nth 1 a
) '(0 2)) ")" "]")))
347 (if (eq (car calc-date-format
) 'X
)
349 (concat "<" (math-format-date a
) ">")))
350 ((and (eq (car a
) 'calcFunc-subscr
)
351 (setq spfn
(get calc-language
'math-compose-subscr
)))
353 ((and (eq (car a
) 'calcFunc-subscr
) (= (length a
) 3)
354 (eq calc-language
'big
))
355 (let* ((a1 (math-compose-expr (nth 1 a
) 1000))
356 (calc-language 'flat
)
357 (a2 (math-compose-expr (nth 2 a
) 0)))
358 (if (or (eq (car-safe a1
) 'subscr
)
359 (and (eq (car-safe a1
) 'tag
)
360 (eq (car-safe (nth 2 a1
)) 'subscr
)
361 (setq a1
(nth 2 a1
))))
368 (list 'subscr a1 a2
))))
369 ((and (eq (car a
) '^
)
370 (eq calc-language
'big
))
372 (if (or (math-looks-negp (nth 1 a
))
373 (memq (car-safe (nth 1 a
)) '(^
/ frac calcFunc-sqrt
))
374 (and (eq (car-safe (nth 1 a
)) 'cplx
)
375 (math-negp (nth 1 (nth 1 a
)))
376 (eq (nth 2 (nth 1 a
)) 0)))
377 (list 'horiz
"(" (math-compose-expr (nth 1 a
) 0) ")")
378 (math-compose-expr (nth 1 a
) 201))
379 (let ((calc-language 'flat
)
380 (calc-number-radix 10)
381 (calc-twos-complement-mode nil
))
382 (math-compose-expr (nth 2 a
) 0))))
383 ((and (eq (car a
) '/)
384 (eq calc-language
'big
))
385 (let ((a1 (let ((calc-language (if (memq (car-safe (nth 1 a
)) '(/ frac
))
387 (math-compose-expr (nth 1 a
) 0)))
388 (a2 (let ((calc-language (if (memq (car-safe (nth 2 a
)) '(/ frac
))
390 (math-compose-expr (nth 2 a
) 0))))
392 (math-comp-height a1
)
394 ((and (eq (car a
) 'calcFunc-lambda
)
396 (memq calc-language
'(nil flat big
)))
399 (math-compose-hash-args (if (= (length a
) 3) 1 t
)))
400 (while (and (cdr p
) (equal (car p
) (car ap
)))
401 (setq p
(cdr p
) ap
(cdr ap
)))
404 (list (math-compose-vector
405 (nreverse (cdr (reverse (cdr a
)))) ", " 0)
408 (list (math-compose-expr (nth (1- (length a
)) a
) 0)
410 ((and (eq (car a
) 'calcFunc-string
)
412 (math-vectorp (nth 1 a
))
413 (math-vector-is-string (nth 1 a
)))
414 (if (eq calc-language
'unform
)
415 (concat "string(" (math-vector-to-string (nth 1 a
) t
) ")")
416 (math-vector-to-string (nth 1 a
) nil
)))
417 ((and (eq (car a
) 'calcFunc-bstring
)
419 (math-vectorp (nth 1 a
))
420 (math-vector-is-string (nth 1 a
)))
421 (if (eq calc-language
'unform
)
422 (concat "bstring(" (math-vector-to-string (nth 1 a
) t
) ")")
424 (s (math-vector-to-string (nth 1 a
) nil
))
426 (while (string-match "[^ ] +[^ ]" s
)
427 (setq p
(1- (match-end 0))
428 c
(cons (list 'break math-compose-level
)
429 (cons (substring s
0 p
)
432 (setq c
(nreverse (cons s c
)))
434 (setq c
(cons (list 'set math-compose-level
2) c
)))
436 ((and (eq (car a
) 'calcFunc-cprec
)
437 (not (eq calc-language
'unform
))
439 (integerp (nth 2 a
)))
440 (let ((c (math-compose-expr (nth 1 a
) -
1)))
441 (if (> prec
(nth 2 a
))
442 (if (setq spfn
(get calc-language
'math-big-parens
))
443 (list 'horiz
(car spfn
) c
(cdr spfn
))
444 (list 'horiz
"(" c
")"))
446 ((and (eq (car a
) 'calcFunc-choriz
)
447 (not (eq calc-language
'unform
))
448 (memq (length a
) '(2 3 4))
449 (math-vectorp (nth 1 a
))
450 (if (integerp (nth 2 a
))
452 (and (math-vectorp (nth 3 a
))
453 (math-vector-is-string (nth 3 a
))))
455 (and (math-vectorp (nth 2 a
))
456 (math-vector-is-string (nth 2 a
))))))
457 (let* ((cprec (and (integerp (nth 2 a
)) (nth 2 a
)))
458 (sep (nth (if cprec
3 2) a
))
461 (math-compose-vector (cdr (nth 1 a
))
462 (math-vector-to-string sep nil
)
464 (cons 'horiz
(mapcar (function
466 (if (eq (car-safe x
) 'calcFunc-bstring
)
469 x
(or bprec cprec prec
))
471 (math-compose-expr x
(or cprec prec
)))))
473 ((and (memq (car a
) '(calcFunc-cvert calcFunc-clvert calcFunc-crvert
))
474 (not (eq calc-language
'unform
))
475 (memq (length a
) '(2 3))
476 (math-vectorp (nth 1 a
))
478 (integerp (nth 2 a
))))
481 (prec (or (nth 2 a
) prec
))
484 (let ((b nil
) (cc nil
) a d
)
485 (if (and (memq (car-safe x
) '(calcFunc-cbase
488 (memq (length x
) '(1 2)))
491 (if (and (eq (car-safe x
) 'calcFunc-crule
)
492 (memq (length x
) '(1 2))
494 (and (math-vectorp (nth 1 x
))
495 (= (length (nth 1 x
)) 2)
496 (math-vector-is-string
498 (and (natnump (nth 1 x
))
499 (<= (nth 1 x
) 255))))
502 (if (math-vectorp (nth 1 x
))
503 (aref (math-vector-to-string
506 (or (and (memq (car-safe x
) '(calcFunc-cvspace
509 (memq (length x
) '(2 3))
512 (setq cc
(math-compose-expr x prec
))))
513 (setq a
(if cc
(math-comp-ascent cc
) 0)
514 d
(if cc
(math-comp-descent cc
) 0))
515 (if (eq b
'calcFunc-cbase
)
516 (setq base
(+ v a -
1))
517 (if (eq b
'calcFunc-ctbase
)
519 (if (eq b
'calcFunc-cbbase
)
520 (setq base
(+ v a d -
1)))))
524 (setq c
(delq nil c
))
526 (cons (if (eq (car a
) 'calcFunc-cvert
) 'vcent
527 (if (eq (car a
) 'calcFunc-clvert
) 'vleft
'vright
))
530 ((and (memq (car a
) '(calcFunc-csup calcFunc-csub
))
531 (not (eq calc-language
'unform
))
532 (memq (length a
) '(3 4))
534 (integerp (nth 3 a
))))
535 (list (if (eq (car a
) 'calcFunc-csup
) 'supscr
'subscr
)
536 (math-compose-expr (nth 1 a
) (or (nth 3 a
) 0))
537 (math-compose-expr (nth 2 a
) 0)))
538 ((and (eq (car a
) 'calcFunc-cflat
)
539 (not (eq calc-language
'unform
))
540 (memq (length a
) '(2 3))
542 (integerp (nth 2 a
))))
543 (let ((calc-language (if (memq calc-language
'(nil big
))
544 'flat calc-language
)))
545 (math-compose-expr (nth 1 a
) (or (nth 2 a
) 0))))
546 ((and (eq (car a
) 'calcFunc-cspace
)
547 (memq (length a
) '(2 3))
550 (cons 'horiz
(make-list (nth 1 a
)
551 (if (and (math-vectorp (nth 2 a
))
552 (math-vector-is-string (nth 2 a
)))
553 (math-vector-to-string (nth 2 a
) nil
)
554 (math-compose-expr (nth 2 a
) 0))))
555 (make-string (nth 1 a
) ?\
)))
556 ((and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
557 (memq (length a
) '(2 3))
561 (let* ((c (if (nth 2 a
)
562 (if (and (math-vectorp (nth 2 a
))
563 (math-vector-is-string (nth 2 a
)))
564 (math-vector-to-string (nth 2 a
) nil
)
565 (math-compose-expr (nth 2 a
) 0))
567 (ca (math-comp-ascent c
))
568 (cd (math-comp-descent c
)))
570 (cons (if (eq (car a
) 'calcFunc-ctspace
)
572 (if (eq (car a
) 'calcFunc-cbspace
)
573 (+ (* (1- (nth 1 a
)) (+ ca cd
)) (1- ca
))
574 (/ (1- (* (nth 1 a
) (+ ca cd
))) 2)))
575 (make-list (nth 1 a
) c
))))))
576 ((and (eq (car a
) 'calcFunc-evalto
)
577 (setq calc-any-evaltos t
)
578 (setq spfn
(get calc-language
'math-evalto
))
579 (= math-compose-level
(if math-comp-tagged
2 1))
583 (math-compose-expr (nth 1 a
) 0)
585 (math-compose-expr (nth 2 a
) 0)))
587 (let ((op (and (not (eq calc-language
'unform
))
588 (if (and (eq (car a
) 'calcFunc-if
) (= (length a
) 4))
589 (assoc "?" math-expr-opers
)
590 (math-assq2 (car a
) math-expr-opers
)))))
592 (or (= (length a
) 3) (eq (car a
) 'calcFunc-if
))
595 ((> prec
(or (nth 4 op
) (min (nth 2 op
) (nth 3 op
))))
596 (if (and (memq calc-language
'(tex latex
))
597 (not (math-tex-expr-is-flat a
)))
598 (if (eq (car-safe a
) '/)
599 (list 'horiz
"{" (math-compose-expr a -
1) "}")
600 (list 'horiz
"\\left( "
601 (math-compose-expr a -
1)
603 (if (eq calc-language
'eqn
)
604 (if (or (eq (car-safe a
) '/)
606 (list 'horiz
"{" (math-compose-expr a -
1) "}")
607 (if (math-tex-expr-is-flat a
)
608 (list 'horiz
"( " (math-compose-expr a -
1) " )")
609 (list 'horiz
"{left ( "
610 (math-compose-expr a -
1)
612 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
613 ((and (memq calc-language
'(tex latex
))
614 (memq (car a
) '(/ calcFunc-choose calcFunc-evalto
))
616 (list 'horiz
"{" (math-compose-expr a -
1) "}"))
617 ((eq (car a
) 'calcFunc-if
)
619 (math-compose-expr (nth 1 a
) (nth 2 op
))
621 (math-compose-expr (nth 2 a
) 0)
623 (math-compose-expr (nth 3 a
) (nth 3 op
))))
625 (let* ((math-comp-tagged (and math-comp-tagged
628 (setlev (if (= prec
(min (nth 2 op
) (nth 3 op
)))
630 (setq math-compose-level
631 (1- math-compose-level
))
634 (lhs (math-compose-expr (nth 1 a
) (nth 2 op
)))
635 (rhs (math-compose-expr (nth 2 a
) (nth 3 op
))))
636 (and (equal (car op
) "^")
637 (eq (math-comp-first-char lhs
) ?-
)
638 (setq lhs
(list 'horiz
"(" lhs
")")))
639 (and (memq calc-language
'(tex latex
))
640 (or (equal (car op
) "^") (equal (car op
) "_"))
641 (not (and (stringp rhs
) (= (length rhs
) 1)))
642 (setq rhs
(list 'horiz
"{" rhs
"}")))
643 (or (and (eq (car a
) '*)
644 (or (null calc-language
)
645 (assoc "2x" math-expr-opers
))
646 (let* ((prevt (math-prod-last-term (nth 1 a
)))
647 (nextt (math-prod-first-term (nth 2 a
)))
648 (prevc (or (math-comp-last-char lhs
)
649 (and (memq (car-safe prevt
)
653 (eq calc-language
'big
)
655 (nextc (or (math-comp-first-char rhs
)
656 (and (memq (car-safe nextt
)
661 (eq calc-language
'big
)
664 (or (and (>= nextc ?a
) (<= nextc ?z
))
665 (and (>= nextc ?A
) (<= nextc ?Z
))
666 (and (>= nextc ?
0) (<= nextc ?
9))
667 (memq nextc
'(?. ?_ ?
#
672 (math-comp-first-string
674 (not (and (eq (car-safe prevt
) 'var
)
679 (list 'break math-compose-level
)
685 (list 'break math-compose-level
)
686 (if (or (equal (car op
) "^")
688 (equal (car op
) "**")
689 (and (equal (car op
) "*")
690 (math-comp-last-char lhs
)
691 (math-comp-first-char rhs
))
692 (and (equal (car op
) "/")
693 (math-num-integerp (nth 1 a
))
694 (math-integerp (nth 2 a
))))
696 (if (and (eq calc-language
'big
)
697 (equal (car op
) "=>"))
699 (concat " " (car op
) " ")))
701 ((and op
(= (length a
) 2) (= (nth 3 op
) -
1))
703 ((or (> prec
(or (nth 4 op
) (nth 2 op
)))
704 (and (not (eq (assoc (car op
) math-expr-opers
) op
))
705 (> prec
0))) ; don't write x% + y
706 (if (and (memq calc-language
'(tex latex
))
707 (not (math-tex-expr-is-flat a
)))
708 (list 'horiz
"\\left( "
709 (math-compose-expr a -
1)
711 (if (eq calc-language
'eqn
)
712 (if (= (/ prec
100) 9)
713 (list 'horiz
"{" (math-compose-expr a -
1) "}")
714 (if (math-tex-expr-is-flat a
)
715 (list 'horiz
"{( " (math-compose-expr a -
1) " )}")
716 (list 'horiz
"{left ( "
717 (math-compose-expr a -
1)
719 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
721 (let ((lhs (math-compose-expr (nth 1 a
) (nth 2 op
))))
724 (if (or (> (length (car op
)) 1)
725 (not (math-comp-is-flat lhs
)))
726 (concat " " (car op
))
728 ((and op
(= (length a
) 2) (= (nth 2 op
) -
1))
731 (let ((lr (and (memq calc-language
'(tex latex
))
732 (not (math-tex-expr-is-flat (nth 1 a
))))))
735 (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op
))
736 (substring (car op
) 1)
738 (if (or lr
(> (length (car op
)) 2)) " " "")
739 (math-compose-expr (nth 1 a
) -
1)
740 (if (or lr
(> (length (car op
)) 2)) " " "")
742 (car (nth 1 (memq op math-expr-opers
))))))
743 ((> prec
(or (nth 4 op
) (nth 3 op
)))
744 (if (and (memq calc-language
'(tex latex
))
745 (not (math-tex-expr-is-flat a
)))
746 (list 'horiz
"\\left( "
747 (math-compose-expr a -
1)
749 (if (eq calc-language
'eqn
)
750 (if (= (/ prec
100) 9)
751 (list 'horiz
"{" (math-compose-expr a -
1) "}")
752 (if (math-tex-expr-is-flat a
)
753 (list 'horiz
"{( " (math-compose-expr a -
1) " )}")
754 (list 'horiz
"{left ( "
755 (math-compose-expr a -
1)
757 (list 'horiz
"(" (math-compose-expr a
0) ")"))))
759 (let ((rhs (math-compose-expr (nth 1 a
) (nth 3 op
))))
761 (let ((ops (if (string-match "\\`u\\([^a-zA-Z]\\)\\'"
763 (substring (car op
) 1)
765 (if (or (> (length ops
) 1)
766 (not (math-comp-is-flat rhs
)))
770 ((and (eq calc-language
'big
)
771 (setq op
(get (car a
) 'math-compose-big
))
772 (funcall op a prec
)))
773 ((and (setq op
(assq calc-language
774 '( ( nil . math-compose-normal
)
775 ( flat . math-compose-normal
)
776 ( big . math-compose-normal
)
777 ( c . math-compose-c
)
778 ( pascal . math-compose-pascal
)
779 ( fortran . math-compose-fortran
)
780 ( tex . math-compose-tex
)
781 ( latex . math-compose-latex
)
782 ( eqn . math-compose-eqn
)
783 ( yacas . math-compose-yacas
)
784 ( maxima . math-compose-maxima
)
785 ( giac . math-compose-giac
)
786 ( math . math-compose-math
)
787 ( maple . math-compose-maple
))))
788 (setq op
(get (car a
) (cdr op
)))
789 (funcall op a prec
)))
791 (let* ((func (car a
))
792 (func2 (assq func
'(( mod . calcFunc-makemod
)
793 ( sdev . calcFunc-sdev
)
800 ( neg . calcFunc-neg
)
801 ( | . calcFunc-vconcat
))))
804 (setq func
(cdr func2
)))
805 (if (setq func2
(rassq func math-expr-function-mapping
))
806 (setq func
(car func2
)))
807 (setq func
(math-remove-dashes
809 "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
811 (math-match-substring (symbol-name func
) 1)
812 (symbol-name func
))))
813 (if (memq calc-language calc-lang-allow-percentsigns
)
814 (setq func
(math-to-percentsigns func
)))
815 (if (memq calc-language calc-lang-allow-underscores
)
816 (setq func
(math-to-underscores func
)))
817 (if (setq spfn
(get calc-language
'math-func-formatter
))
818 (funcall spfn func a
)
820 (list 'horiz func calc-function-open
821 (math-compose-vector (cdr a
) ", " 0)
822 calc-function-close
))))))))))
825 (defun math-prod-first-term (x)
826 (while (eq (car-safe x
) '*)
830 (defun math-prod-last-term (x)
831 (while (eq (car-safe x
) '*)
835 (defun math-compose-vector (a sep prec
)
838 (cons (list 'set math-compose-level
)
839 (let ((c (list (math-compose-expr (car a
) prec
))))
840 (while (setq a
(cdr a
))
841 (setq c
(cons (if (eq (car-safe (car a
))
843 (let ((math-compose-level
844 (1- math-compose-level
)))
845 (math-compose-expr (car a
) -
123))
846 (math-compose-expr (car a
) prec
))
847 (cons (list 'break math-compose-level
)
852 (defun math-vector-no-parens (a)
854 (not (eq (car-safe (nth 1 a
)) '*))))
856 (defun math-compose-matrix (a col cols base
)
859 (while (<= (setq col
(1+ col
)) cols
)
860 (setq res
(cons (cons math-comp-just
867 math-comp-vector-prec
)
871 math-comp-comma-spc
" ")))))
876 (defun math-compose-rows (a count first
)
880 (math-compose-rows (cdr a
) -
1 nil
)
882 (let ((mdots (get calc-language
'math-dots
)))
887 (math-compose-rows (cdr a
) -
1 nil
)))
889 (if first
(concat math-comp-left-bracket
" ") " ")
890 (math-compose-expr (car a
) math-comp-vector-prec
)
892 (math-compose-rows (cdr a
) (1- count
) nil
)))
894 (if first
(concat math-comp-left-bracket
" ") " ")
895 (math-compose-expr (car a
) math-comp-vector-prec
)
896 (concat " " math-comp-right-bracket
)))))
898 (defun math-vector-is-string (a)
899 (while (and (setq a
(cdr a
))
900 (or (and (natnump (car a
))
902 (and (eq (car-safe (car a
)) 'cplx
)
903 (natnump (nth 1 (car a
)))
904 (eq (nth 2 (car a
)) 0)
905 (<= (nth 1 (car a
)) 255)))))
908 (defconst math-vector-to-string-chars
'( ( ?
\" .
"\\\"" )
919 (defun math-vector-to-string (a &optional quoted
)
920 (setq a
(concat (mapcar (function (lambda (x) (if (consp x
) (nth 1 x
) x
)))
922 (if (string-match "[\000-\037\177\\\"]" a
)
924 (pat (if quoted
"[\000-\037\177\\\"]" "[\000-\037\177]"))
925 (codes (if quoted math-vector-to-string-chars
'((?\^? .
"^?"))))
926 (fmt (if quoted
"\\^%c" "^%c"))
928 (while (setq p
(string-match pat a p
))
929 (if (setq new
(assq (aref a p
) codes
))
930 (setq a
(concat (substring a
0 p
)
932 (substring a
(1+ p
)))
933 p
(+ p
(length (cdr new
))))
934 (setq a
(concat (substring a
0 p
)
935 (format fmt
(+ (aref a p
) 64))
936 (substring a
(1+ p
)))
943 (defun math-to-underscores (x)
944 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
946 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
949 (defun math-to-percentsigns (x)
950 (if (string-match "\\`\\(.*\\)o'o\\(.*\\)\\'" x
)
952 (concat (math-match-substring x
1) "%" (math-match-substring x
2)))
955 (defun math-tex-expr-is-flat (a)
956 (or (Math-integerp a
)
957 (memq (car a
) '(float var
))
958 (and (memq (car a
) '(+ -
* neg
))
960 (while (and (setq a
(cdr a
))
961 (math-tex-expr-is-flat (car a
))))
963 (and (memq (car a
) '(^ calcFunc-subscr
))
964 (math-tex-expr-is-flat (nth 1 a
)))))
966 (put 'calcFunc-log
'math-compose-big
'math-compose-log
)
967 (defun math-compose-log (a prec
)
968 (and (= (length a
) 3)
971 (let ((calc-language 'flat
))
972 (math-compose-expr (nth 2 a
) 1000)))
974 (math-compose-expr (nth 1 a
) 1000)
977 (put 'calcFunc-log10
'math-compose-big
'math-compose-log10
)
978 (defun math-compose-log10 (a prec
)
979 (and (= (length a
) 2)
981 (list 'subscr
"log" "10")
983 (math-compose-expr (nth 1 a
) 1000)
986 (put 'calcFunc-deriv
'math-compose-big
'math-compose-deriv
)
987 (put 'calcFunc-tderiv
'math-compose-big
'math-compose-deriv
)
988 (defun math-compose-deriv (a prec
)
989 (when (= (length a
) 3)
990 (math-compose-expr (list '/
991 (list 'calcFunc-choriz
993 '(calcFunc-string (vec ?d
))
995 (list 'calcFunc-choriz
997 '(calcFunc-string (vec ?d
))
1001 (put 'calcFunc-sqrt
'math-compose-big
'math-compose-sqrt
)
1002 (defun math-compose-sqrt (a prec
)
1003 (when (= (length a
) 2)
1004 (let* ((c (math-compose-expr (nth 1 a
) 0))
1005 (a (math-comp-ascent c
))
1006 (d (math-comp-descent c
))
1008 (w (math-comp-width c
)))
1011 (concat (if (= h
1) " " " ")
1012 (make-string (+ w
2) ?\_
))
1016 (append (list 'vleft
(1- a
))
1017 (make-list (1- h
) " |")
1022 (put 'calcFunc-choose
'math-compose-big
'math-compose-choose
)
1023 (defun math-compose-choose (a prec
)
1024 (let ((a1 (math-compose-expr (nth 1 a
) 0))
1025 (a2 (math-compose-expr (nth 2 a
) 0)))
1029 (math-comp-height a1
)
1033 (put 'calcFunc-integ
'math-compose-big
'math-compose-integ
)
1034 (defun math-compose-integ (a prec
)
1035 (and (memq (length a
) '(3 5))
1036 (eq (car-safe (nth 2 a
)) 'var
)
1037 (let* ((parens (and (>= prec
196) (/= prec
1000)))
1038 (var (math-compose-expr (nth 2 a
) 0))
1039 (over (and (eq (car-safe (nth 2 a
)) 'var
)
1040 (or (and (eq (car-safe (nth 1 a
)) '/)
1041 (math-numberp (nth 1 (nth 1 a
))))
1042 (and (eq (car-safe (nth 1 a
)) '^
)
1043 (math-looks-negp (nth 2 (nth 1 a
)))))))
1044 (expr (math-compose-expr (if over
1046 (math-build-var-name
1049 (nth 1 (nth 2 a
)))))
1051 (calc-language 'flat
)
1052 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1053 (high (and (nth 4 a
) (math-compose-expr (nth 4 a
) 0))))
1056 (append (list 'vcent
(if high
3 2))
1057 (and high
(list (list 'horiz
" " high
)))
1063 (and low
(list (list 'horiz low
" "))))
1067 (list 'horiz
" d" var
))
1068 (if parens
")" "")))))
1070 (put 'calcFunc-sum
'math-compose-big
'math-compose-sum
)
1071 (defun math-compose-sum (a prec
)
1072 (and (memq (length a
) '(3 5 6))
1073 (let* ((expr (math-compose-expr (nth 1 a
) 185))
1074 (calc-language 'flat
)
1075 (var (math-compose-expr (nth 2 a
) 0))
1076 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1077 (high (and (nth 4 a
) (math-compose-vector (nthcdr 4 a
) ", " 0))))
1079 (if (memq prec
'(180 201)) "(" "")
1080 (append (list 'vcent
(if high
3 2))
1081 (and high
(list high
))
1088 (list (list 'horiz var
" = " low
))
1090 (if (memq (car-safe (nth 1 a
)) '(calcFunc-sum calcFunc-prod
))
1093 (if (memq prec
'(180 201)) ")" "")))))
1095 (put 'calcFunc-prod
'math-compose-big
'math-compose-prod
)
1096 (defun math-compose-prod (a prec
)
1097 (and (memq (length a
) '(3 5 6))
1098 (let* ((expr (math-compose-expr (nth 1 a
) 198))
1099 (calc-language 'flat
)
1100 (var (math-compose-expr (nth 2 a
) 0))
1101 (low (and (nth 3 a
) (math-compose-expr (nth 3 a
) 0)))
1102 (high (and (nth 4 a
) (math-compose-vector (nthcdr 4 a
) ", " 0))))
1104 (if (memq prec
'(196 201)) "(" "")
1105 (append (list 'vcent
(if high
3 2))
1106 (and high
(list high
))
1112 (list (list 'horiz var
" = " low
))
1114 (if (memq (car-safe (nth 1 a
)) '(calcFunc-sum calcFunc-prod
))
1117 (if (memq prec
'(196 201)) ")" "")))))
1119 ;; The variables math-svo-c, math-svo-wid and math-svo-off are local
1120 ;; to math-stack-value-offset in calc.el, but are used by
1121 ;; math-stack-value-offset-fancy, which is called by math-stack-value-offset..
1123 (defvar math-svo-wid
)
1124 (defvar math-svo-off
)
1126 (defun math-stack-value-offset-fancy ()
1127 (let ((cwid (+ (math-comp-width math-svo-c
))))
1128 (cond ((eq calc-display-just
'right
)
1129 (if calc-display-origin
1130 (setq math-svo-wid
(max calc-display-origin
5))
1131 (if (integerp calc-line-breaking
)
1132 (setq math-svo-wid calc-line-breaking
)))
1133 (setq math-svo-off
(- math-svo-wid cwid
1134 (max (- (length calc-right-label
)
1135 (if (and (integerp calc-line-breaking
)
1136 calc-display-origin
)
1137 (max (- calc-line-breaking
1138 calc-display-origin
)
1143 (if calc-display-origin
1145 (setq math-svo-off
(- calc-display-origin
(/ cwid
2)))
1146 (if (integerp calc-line-breaking
)
1147 (setq math-svo-off
(min math-svo-off
(- calc-line-breaking cwid
1148 (length calc-right-label
)))))
1149 (if (>= math-svo-off
0)
1150 (setq math-svo-wid
(max math-svo-wid
(+ math-svo-off cwid
)))))
1151 (if (integerp calc-line-breaking
)
1152 (setq math-svo-wid calc-line-breaking
))
1153 (setq math-svo-off
(/ (- math-svo-wid cwid
) 2)))))
1154 (and (integerp calc-line-breaking
)
1155 (or (< math-svo-off
0)
1156 (and calc-display-origin
1157 (> calc-line-breaking calc-display-origin
)))
1158 (setq math-svo-wid calc-line-breaking
))))
1161 ;;; Convert a composition to string form, with embedded \n's if necessary.
1163 (defun math-composition-to-string (c &optional width
)
1164 (or width
(setq width
(calc-window-width)))
1165 (if calc-display-raw
1166 (math-comp-to-string-raw c
0)
1167 (if (math-comp-is-flat c
)
1168 (math-comp-to-string-flat c width
)
1169 (math-vert-comp-to-string
1170 (math-comp-simplify c width
)))))
1172 (defvar math-comp-buf-string
(make-vector 10 ""))
1173 (defvar math-comp-buf-margin
(make-vector 10 0))
1174 (defvar math-comp-buf-level
(make-vector 10 0))
1175 (defun math-comp-is-flat (c) ; check if c's height is 1.
1176 (cond ((not (consp c
)) t
)
1177 ((memq (car c
) '(set break
)) t
)
1178 ((eq (car c
) 'horiz
)
1179 (while (and (setq c
(cdr c
))
1180 (math-comp-is-flat (car c
))))
1182 ((memq (car c
) '(vleft vcent vright
))
1183 (and (= (length c
) 3)
1185 (math-comp-is-flat (nth 2 c
))))
1187 (math-comp-is-flat (nth 2 c
)))
1191 ;;; Convert a one-line composition to a string. Break into multiple
1192 ;;; lines if necessary, choosing break points according to the structure
1195 ;; The variables math-comp-full-width, math-comp-highlight, math-comp-word,
1196 ;; math-comp-level, math-comp-margin and math-comp-buf are local to
1197 ;; math-comp-to-string-flat, but are used by math-comp-to-string-flat-term,
1198 ;; which is called by math-comp-to-string-flat.
1199 ;; math-comp-highlight and math-comp-buf are also local to
1200 ;; math-comp-simplify-term and math-comp-simplify respectively, but are used
1201 ;; by math-comp-add-string.
1202 (defvar math-comp-full-width
)
1203 (defvar math-comp-highlight
)
1204 (defvar math-comp-word
)
1205 (defvar math-comp-level
)
1206 (defvar math-comp-margin
)
1207 (defvar math-comp-buf
)
1208 ;; The variable math-comp-pos is local to math-comp-to-string-flat, but
1209 ;; is used by math-comp-to-string-flat-term and math-comp-sel-first-term,
1210 ;; which are called by math-comp-to-string-flat.
1211 (defvar math-comp-pos
)
1213 (defun math-comp-to-string-flat (c math-comp-full-width
)
1214 (if math-comp-sel-hpos
1215 (let ((math-comp-pos 0))
1216 (math-comp-sel-flat-term c
))
1217 (let ((math-comp-buf "")
1220 (math-comp-margin 0)
1221 (math-comp-highlight (and math-comp-selected calc-show-selections
))
1222 (math-comp-level -
1))
1223 (math-comp-to-string-flat-term '(set -
1 0))
1224 (math-comp-to-string-flat-term c
)
1225 (math-comp-to-string-flat-term '(break -
1))
1226 (let ((str (aref math-comp-buf-string
0))
1228 (and (> (length str
) 0) (= (aref str
0) ?
)
1229 (> (length math-comp-buf
) 0)
1230 (let ((k (length math-comp-buf
)))
1231 (while (not (= (aref math-comp-buf
(setq k
(1- k
))) ?
\n)))
1232 (aset math-comp-buf k ?
)
1233 (if (and (< (1+ k
) (length math-comp-buf
))
1234 (= (aref math-comp-buf
(1+ k
)) ?
))
1236 (aset math-comp-buf
(1+ k
) ?
\n)
1238 (setq prefix
"\n"))))
1239 (concat math-comp-buf prefix str
)))))
1241 (defun math-comp-to-string-flat-term (c)
1242 (cond ((not (consp c
))
1243 (if math-comp-highlight
1244 (setq c
(math-comp-highlight-string c
)))
1245 (setq math-comp-word
(if (= (length math-comp-word
) 0) c
1246 (concat math-comp-word c
))
1247 math-comp-pos
(+ math-comp-pos
(length c
))))
1249 ((eq (car c
) 'horiz
)
1250 (while (setq c
(cdr c
))
1251 (math-comp-to-string-flat-term (car c
))))
1256 (setq math-comp-level
(1+ math-comp-level
))
1257 (if (>= math-comp-level
(length math-comp-buf-string
))
1258 (setq math-comp-buf-string
(vconcat math-comp-buf-string
1259 math-comp-buf-string
)
1260 math-comp-buf-margin
(vconcat math-comp-buf-margin
1261 math-comp-buf-margin
)
1262 math-comp-buf-level
(vconcat math-comp-buf-level
1263 math-comp-buf-level
)))
1264 (aset math-comp-buf-string math-comp-level
"")
1265 (aset math-comp-buf-margin math-comp-level
(+ math-comp-pos
1267 (aset math-comp-buf-level math-comp-level
(nth 1 c
)))))
1269 ((eq (car c
) 'break
)
1270 (if (not calc-line-breaking
)
1271 (setq math-comp-buf
(concat math-comp-buf math-comp-word
)
1274 (if (and (> math-comp-pos math-comp-full-width
)
1277 (setq str
(aref math-comp-buf-string i
))
1278 (and (= (length str
) 0) (< i math-comp-level
)))
1280 (or (> (length str
) 0) (> (length math-comp-buf
) 0))))
1281 (let ((prefix "") mrg wid
)
1282 (setq mrg
(aref math-comp-buf-margin i
))
1283 (if (> mrg
12) ; indenting too far, go back to far left
1284 (let ((j i
) (new (if calc-line-numbering
5 1)))
1285 '(while (<= j math-comp-level
)
1286 (aset math-comp-buf-margin j
1287 (+ (aref math-comp-buf-margin j
) (- new mrg
)))
1290 (setq wid
(+ (length str
) math-comp-margin
))
1291 (and (> (length str
) 0) (= (aref str
0) ?
)
1292 (> (length math-comp-buf
) 0)
1293 (let ((k (length math-comp-buf
)))
1294 (while (not (= (aref math-comp-buf
(setq k
(1- k
))) ?
\n)))
1295 (aset math-comp-buf k ?
)
1296 (if (and (< (1+ k
) (length math-comp-buf
))
1297 (= (aref math-comp-buf
(1+ k
)) ?
))
1299 (aset math-comp-buf
(1+ k
) ?
\n)
1301 (setq prefix
"\n"))))
1302 (setq math-comp-buf
(concat math-comp-buf prefix str
"\n"
1303 (make-string mrg ?
))
1304 math-comp-pos
(+ math-comp-pos
(- mrg wid
))
1305 math-comp-margin mrg
)
1306 (aset math-comp-buf-string i
"")
1307 (while (<= (setq i
(1+ i
)) math-comp-level
)
1308 (if (> (aref math-comp-buf-margin i
) wid
)
1309 (aset math-comp-buf-margin i
1310 (+ (aref math-comp-buf-margin i
)
1312 (if (and (= (nth 1 c
) (aref math-comp-buf-level math-comp-level
))
1313 (< math-comp-pos
(+ (aref math-comp-buf-margin math-comp-level
) 2)))
1314 () ; avoid stupid breaks, e.g., "1 +\n really_long_expr"
1315 (let ((str (aref math-comp-buf-string math-comp-level
)))
1316 (setq str
(if (= (length str
) 0)
1318 (concat str math-comp-word
))
1320 (while (< (nth 1 c
) (aref math-comp-buf-level math-comp-level
))
1321 (setq math-comp-level
(1- math-comp-level
))
1322 (or (= (length (aref math-comp-buf-string math-comp-level
)) 0)
1323 (setq str
(concat (aref math-comp-buf-string math-comp-level
)
1325 (aset math-comp-buf-string math-comp-level str
)))))
1328 (cond ((eq (nth 1 c
) math-comp-selected
)
1329 (let ((math-comp-highlight (not calc-show-selections
)))
1330 (math-comp-to-string-flat-term (nth 2 c
))))
1332 (let ((math-comp-highlight nil
))
1333 (math-comp-to-string-flat-term (nth 2 c
))))
1334 (t (math-comp-to-string-flat-term (nth 2 c
)))))
1336 (t (math-comp-to-string-flat-term (nth 2 c
)))))
1338 (defun math-comp-highlight-string (s)
1339 (setq s
(copy-sequence s
))
1340 (let ((i (length s
)))
1341 (while (>= (setq i
(1- i
)) 0)
1342 (or (memq (aref s i
) '(32 ?
\n))
1343 (aset s i
(if calc-show-selections ?\. ?\
#)))))
1347 ;; The variable math-comp-sel-tag is local to calc-find-selected-part
1348 ;; in calc-sel.el, but is used by math-comp-sel-flat-term and
1349 ;; math-comp-add-string-sel, which are called (indirectly) by
1350 ;; calc-find-selected-part.
1351 (defvar math-comp-sel-tag
)
1353 (defun math-comp-sel-flat-term (c)
1354 (cond ((not (consp c
))
1355 (setq math-comp-pos
(+ math-comp-pos
(length c
))))
1356 ((memq (car c
) '(set break
)))
1357 ((eq (car c
) 'horiz
)
1358 (while (and (setq c
(cdr c
)) (< math-comp-sel-cpos
1000000))
1359 (math-comp-sel-flat-term (car c
))))
1361 (if (<= math-comp-pos math-comp-sel-cpos
)
1363 (math-comp-sel-flat-term (nth 2 c
))
1364 (if (> math-comp-pos math-comp-sel-cpos
)
1365 (setq math-comp-sel-tag c
1366 math-comp-sel-cpos
1000000)))
1367 (math-comp-sel-flat-term (nth 2 c
))))
1368 (t (math-comp-sel-flat-term (nth 2 c
)))))
1371 ;;; Simplify a composition to a canonical form consisting of
1372 ;;; (vleft n "string" "string" "string" ...)
1373 ;;; where 0 <= n < number-of-strings.
1375 ;; The variables math-comp-base, math-comp-hgt, math-comp-tag,
1376 ;; math-comp-hpos and math-comp-vpos are local to math-comp-simplify,
1377 ;; but are used by math-comp-add-string (math-comp-base, math-comp-hgt),
1378 ;; math-comp-add-string-sel (math-comp-tag) and math-comp-simplify-term
1379 ;; (math-comp-tag, math-comp-vpos, math-comp-hpos), which are called by
1380 ;; math-comp-simplify.
1381 (defvar math-comp-base
)
1382 (defvar math-comp-hgt
)
1383 (defvar math-comp-tag
)
1384 (defvar math-comp-hpos
)
1385 (defvar math-comp-vpos
)
1387 (defun math-comp-simplify (c full-width
)
1388 (let ((math-comp-buf (list ""))
1393 (math-comp-highlight (and math-comp-selected calc-show-selections
))
1394 (math-comp-tag nil
))
1395 (math-comp-simplify-term c
)
1396 (cons 'vleft
(cons math-comp-base math-comp-buf
))))
1398 (defun math-comp-add-string (s h v
)
1399 (and (> (length s
) 0)
1400 (let ((vv (+ v math-comp-base
)))
1401 (if math-comp-sel-hpos
1402 (math-comp-add-string-sel h vv
(length s
) 1)
1404 (setq math-comp-buf
(nconc (make-list (- vv
) "") math-comp-buf
)
1405 math-comp-base
(- v
)
1406 math-comp-hgt
(- math-comp-hgt vv
)
1408 (if (>= vv math-comp-hgt
)
1409 (setq math-comp-buf
(nconc math-comp-buf
1410 (make-list (1+ (- vv math-comp-hgt
)) ""))
1411 math-comp-hgt
(1+ vv
))))
1412 (let ((str (nthcdr vv math-comp-buf
)))
1413 (setcar str
(concat (car str
)
1414 (make-string (- h
(length (car str
))) 32)
1415 (if math-comp-highlight
1416 (math-comp-highlight-string s
)
1419 (defun math-comp-add-string-sel (x y w h
)
1420 (if (and (<= y math-comp-sel-vpos
)
1421 (> (+ y h
) math-comp-sel-vpos
)
1422 (<= x math-comp-sel-hpos
)
1423 (> (+ x w
) math-comp-sel-hpos
))
1424 (setq math-comp-sel-tag math-comp-tag
1425 math-comp-sel-vpos
10000)))
1427 (defun math-comp-simplify-term (c)
1429 (math-comp-add-string c math-comp-hpos math-comp-vpos
)
1430 (setq math-comp-hpos
(+ math-comp-hpos
(length c
))))
1431 ((memq (car c
) '(set break
))
1433 ((eq (car c
) 'horiz
)
1434 (while (setq c
(cdr c
))
1435 (math-comp-simplify-term (car c
))))
1436 ((memq (car c
) '(vleft vcent vright
))
1437 (let* ((math-comp-vpos (+ (- math-comp-vpos
(nth 1 c
))
1438 (1- (math-comp-ascent (nth 2 c
)))))
1439 (widths (mapcar 'math-comp-width
(cdr (cdr c
))))
1440 (maxwid (apply 'max widths
))
1441 (bias (cond ((eq (car c
) 'vleft
) 0)
1442 ((eq (car c
) 'vcent
) 1)
1445 (while (setq c
(cdr c
))
1446 (if (eq (car-safe (car c
)) 'rule
)
1447 (math-comp-add-string (make-string maxwid
(nth 1 (car c
)))
1448 math-comp-hpos math-comp-vpos
)
1449 (let ((math-comp-hpos (+ math-comp-hpos
(/ (* bias
(- maxwid
1452 (math-comp-simplify-term (car c
))))
1454 (setq math-comp-vpos
(+ math-comp-vpos
1455 (+ (math-comp-descent (car c
))
1456 (math-comp-ascent (nth 1 c
))))
1457 widths
(cdr widths
))))
1458 (setq math-comp-hpos
(+ math-comp-hpos maxwid
))))
1459 ((eq (car c
) 'supscr
)
1460 (let* ((asc (or 1 (math-comp-ascent (nth 1 c
))))
1461 (desc (math-comp-descent (nth 2 c
)))
1464 (math-comp-simplify-term (nth 1 c
))))
1465 (math-comp-vpos (- math-comp-vpos
(+ asc desc
))))
1466 (math-comp-simplify-term (nth 2 c
))
1467 (if math-comp-sel-hpos
1468 (math-comp-add-string-sel oldh
1471 (math-comp-ascent (nth 2 c
)))
1472 (- math-comp-hpos oldh
)
1473 (math-comp-height c
)))))
1474 ((eq (car c
) 'subscr
)
1475 (let* ((asc (math-comp-ascent (nth 2 c
)))
1476 (desc (math-comp-descent (nth 1 c
)))
1477 (oldv math-comp-vpos
)
1480 (math-comp-simplify-term (nth 1 c
))))
1481 (math-comp-vpos (+ math-comp-vpos
(+ asc desc
))))
1482 (math-comp-simplify-term (nth 2 c
))
1483 (if math-comp-sel-hpos
1484 (math-comp-add-string-sel oldh oldv
1485 (- math-comp-hpos oldh
)
1486 (math-comp-height c
)))))
1488 (cond ((eq (nth 1 c
) math-comp-selected
)
1489 (let ((math-comp-highlight (not calc-show-selections
)))
1490 (math-comp-simplify-term (nth 2 c
))))
1492 (let ((math-comp-highlight nil
))
1493 (math-comp-simplify-term (nth 2 c
))))
1494 (t (let ((math-comp-tag c
))
1495 (math-comp-simplify-term (nth 2 c
))))))))
1498 ;;; Measuring a composition.
1500 (defun math-comp-first-char (c)
1502 (and (> (length c
) 0)
1504 ((memq (car c
) '(horiz subscr supscr
))
1505 (while (and (setq c
(cdr c
))
1506 (math-comp-is-null (car c
))))
1507 (and c
(math-comp-first-char (car c
))))
1509 (math-comp-first-char (nth 2 c
)))))
1511 (defun math-comp-first-string (c)
1513 (and (> (length c
) 0)
1515 ((eq (car c
) 'horiz
)
1516 (while (and (setq c
(cdr c
))
1517 (math-comp-is-null (car c
))))
1518 (and c
(math-comp-first-string (car c
))))
1520 (math-comp-first-string (nth 2 c
)))))
1522 (defun math-comp-last-char (c)
1524 (and (> (length c
) 0)
1525 (elt c
(1- (length c
)))))
1526 ((eq (car c
) 'horiz
)
1527 (let ((c (reverse (cdr c
))))
1528 (while (and c
(math-comp-is-null (car c
)))
1530 (and c
(math-comp-last-char (car c
)))))
1532 (math-comp-last-char (nth 2 c
)))))
1534 (defun math-comp-is-null (c)
1535 (cond ((stringp c
) (= (length c
) 0))
1536 ((memq (car c
) '(horiz subscr supscr
))
1537 (while (and (setq c
(cdr c
))
1538 (math-comp-is-null (car c
))))
1541 (math-comp-is-null (nth 2 c
)))
1542 ((memq (car c
) '(set break
)) t
)))
1544 (defun math-comp-width (c)
1545 (cond ((not (consp c
)) (length c
))
1546 ((memq (car c
) '(horiz subscr supscr
))
1548 (while (setq c
(cdr c
))
1549 (setq accum
(+ accum
(math-comp-width (car c
)))))
1551 ((memq (car c
) '(vcent vleft vright
))
1554 (while (setq c
(cdr c
))
1555 (setq accum
(max accum
(math-comp-width (car c
)))))
1558 (math-comp-width (nth 2 c
)))
1561 (defun math-comp-height (c)
1564 (+ (math-comp-ascent c
) (math-comp-descent c
))))
1566 (defun math-comp-ascent (c)
1567 (cond ((not (consp c
)) 1)
1568 ((eq (car c
) 'horiz
)
1570 (while (setq c
(cdr c
))
1571 (setq accum
(max accum
(math-comp-ascent (car c
)))))
1573 ((memq (car c
) '(vcent vleft vright
))
1574 (if (> (nth 1 c
) 0) (1+ (nth 1 c
)) 1))
1575 ((eq (car c
) 'supscr
)
1576 (max (math-comp-ascent (nth 1 c
)) (1+ (math-comp-height (nth 2 c
)))))
1577 ((eq (car c
) 'subscr
)
1578 (math-comp-ascent (nth 1 c
)))
1580 (math-comp-ascent (nth 2 c
)))
1583 (defun math-comp-descent (c)
1584 (cond ((not (consp c
)) 0)
1585 ((eq (car c
) 'horiz
)
1587 (while (setq c
(cdr c
))
1588 (setq accum
(max accum
(math-comp-descent (car c
)))))
1590 ((memq (car c
) '(vcent vleft vright
))
1591 (let ((accum (- (nth 1 c
))))
1593 (while (setq c
(cdr c
))
1594 (setq accum
(+ accum
(math-comp-height (car c
)))))
1595 (max (1- accum
) 0)))
1596 ((eq (car c
) 'supscr
)
1597 (math-comp-descent (nth 1 c
)))
1598 ((eq (car c
) 'subscr
)
1599 (+ (math-comp-descent (nth 1 c
)) (math-comp-height (nth 2 c
))))
1601 (math-comp-descent (nth 2 c
)))
1604 (defun calcFunc-cwidth (a &optional prec
)
1605 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1606 (math-comp-width (math-compose-expr a
(or prec
0))))
1608 (defun calcFunc-cheight (a &optional prec
)
1609 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1610 (if (and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
1611 (memq (length a
) '(2 3))
1614 (math-comp-height (math-compose-expr a
(or prec
0)))))
1616 (defun calcFunc-cascent (a &optional prec
)
1617 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1618 (if (and (memq (car a
) '(calcFunc-cvspace calcFunc-ctspace calcFunc-cbspace
))
1619 (memq (length a
) '(2 3))
1622 (math-comp-ascent (math-compose-expr a
(or prec
0)))))
1624 (defun calcFunc-cdescent (a &optional prec
)
1625 (if (and prec
(not (integerp prec
))) (math-reject-arg prec
'fixnump
))
1626 (math-comp-descent (math-compose-expr a
(or prec
0))))
1629 ;;; Convert a simplified composition into string form.
1631 (defun math-vert-comp-to-string (c)
1634 (math-vert-comp-to-string-step (cdr (cdr c
)))))
1636 (defun math-vert-comp-to-string-step (c)
1638 (concat (car c
) "\n" (math-vert-comp-to-string-step (cdr c
)))
1642 ;;; Convert a composition to a string in "raw" form (for debugging).
1644 (defun math-comp-to-string-raw (c indent
)
1645 (cond ((or (not (consp c
)) (eq (car c
) 'set
))
1646 (prin1-to-string c
))
1648 (concat "(" (symbol-name (car c
)) ")"))
1650 (let ((next-indent (+ indent
2 (length (symbol-name (car c
))))))
1652 (symbol-name (car c
))
1654 (math-comp-to-string-raw (nth 1 c
) next-indent
)
1655 (math-comp-to-string-raw-step (cdr (cdr c
))
1659 (defun math-comp-to-string-raw-step (cl indent
)
1662 (make-string indent
32)
1663 (math-comp-to-string-raw (car cl
) indent
)
1664 (math-comp-to-string-raw-step (cdr cl
) indent
))
1669 ;; arch-tag: 7c45d10a-a286-4dab-af49-7ae8989fbf78
1670 ;;; calccomp.el ends here