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