comparison lisp/calc/calccomp.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 0d8b17d428b5
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; calccomp.el --- composition functions for Calc 1 ;;; calccomp.el --- composition functions for Calc
2 2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
4 5
5 ;; Author: David Gillespie <daveg@synaptics.com> 6 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org> 7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
7 ;; Colin Walters <walters@debian.org>
8 8
9 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
10 10
11 ;; GNU Emacs is distributed in the hope that it will be useful, 11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor 12 ;; but WITHOUT ANY WARRANTY. No author or distributor
26 ;;; Commentary: 26 ;;; Commentary:
27 27
28 ;;; Code: 28 ;;; Code:
29 29
30 ;; This file is autoloaded from calc-ext.el. 30 ;; This file is autoloaded from calc-ext.el.
31
31 (require 'calc-ext) 32 (require 'calc-ext)
32
33 (require 'calc-macs) 33 (require 'calc-macs)
34
35 (defun calc-Need-calc-comp () nil)
36 34
37 (defconst math-eqn-special-funcs 35 (defconst math-eqn-special-funcs
38 '( calcFunc-log 36 '( calcFunc-log
39 calcFunc-ln calcFunc-exp 37 calcFunc-ln calcFunc-exp
40 calcFunc-sin calcFunc-cos calcFunc-tan 38 calcFunc-sin calcFunc-cos calcFunc-tan
39 calcFunc-sec calcFunc-csc calcFunc-cot
41 calcFunc-sinh calcFunc-cosh calcFunc-tanh 40 calcFunc-sinh calcFunc-cosh calcFunc-tanh
41 calcFunc-sech calcFunc-csch calcFunc-coth
42 calcFunc-arcsin calcFunc-arccos calcFunc-arctan 42 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
43 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh)) 43 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh))
44 44
45 ;;; A "composition" has one of the following forms: 45 ;;; A "composition" has one of the following forms:
46 ;;; 46 ;;;
60 ;;; (subscr C1 C2) Composition C1 with subscript C2 60 ;;; (subscr C1 C2) Composition C1 with subscript C2
61 ;;; (rule X) Horizontal line of X, full width of enclosing comp 61 ;;; (rule X) Horizontal line of X, full width of enclosing comp
62 ;;; 62 ;;;
63 ;;; (tag X C) Composition C corresponds to sub-expression X 63 ;;; (tag X C) Composition C corresponds to sub-expression X
64 64
65 ;; math-comp-just and math-comp-comma-spc are local to
66 ;; math-compose-expr, but are used by math-compose-matrix, which is
67 ;; called by math-compose-expr
68 (defvar math-comp-just)
69 (defvar math-comp-comma-spc)
70
71 ;; math-comp-vector-prec is local to math-compose-expr, but is used by
72 ;; math-compose-matrix and math-compose-rows, which are called by
73 ;; math-compose-expr.
74 (defvar math-comp-vector-prec)
75
76 ;; math-comp-left-bracket, math-comp-right-bracket and math-comp-comma are
77 ;; local to math-compose-expr, but are used by math-compose-rows, which is
78 ;; called by math-compose-expr.
79 (defvar math-comp-left-bracket)
80 (defvar math-comp-right-bracket)
81 (defvar math-comp-comma)
82
83
65 (defun math-compose-expr (a prec) 84 (defun math-compose-expr (a prec)
66 (let ((math-compose-level (1+ math-compose-level))) 85 (let ((math-compose-level (1+ math-compose-level))
86 spfn)
67 (cond 87 (cond
68 ((or (and (eq a math-comp-selected) a) 88 ((or (and (eq a math-comp-selected) a)
69 (and math-comp-tagged 89 (and math-comp-tagged
70 (not (eq math-comp-tagged a)))) 90 (not (eq math-comp-tagged a))))
71 (let ((math-comp-selected nil)) 91 (let ((math-comp-selected nil))
72 (and math-comp-tagged (setq math-comp-tagged a)) 92 (and math-comp-tagged (setq math-comp-tagged a))
73 (list 'tag a (math-compose-expr a prec)))) 93 (list 'tag a (math-compose-expr a prec))))
74 ((and (not (consp a)) (not (integerp a))) 94 ((and (not (consp a)) (not (integerp a)))
75 (concat "'" (prin1-to-string a))) 95 (concat "'" (prin1-to-string a)))
96 ((setq spfn (assq (car-safe a) math-expr-special-function-mapping))
97 (setq spfn (cdr spfn))
98 (funcall (car spfn) a spfn))
76 ((math-scalarp a) 99 ((math-scalarp a)
77 (if (or (eq (car-safe a) 'frac) 100 (if (or (eq (car-safe a) 'frac)
78 (and (nth 1 calc-frac-format) (Math-integerp a))) 101 (and (nth 1 calc-frac-format) (Math-integerp a)))
79 (if (memq calc-language '(tex eqn math maple c fortran pascal)) 102 (if (memq calc-language '(tex latex eqn math maple c fortran pascal))
80 (let ((aa (math-adjust-fraction a)) 103 (let ((aa (math-adjust-fraction a))
81 (calc-frac-format nil)) 104 (calc-frac-format nil))
82 (math-compose-expr (list '/ 105 (math-compose-expr (list '/
83 (if (memq calc-language '(c fortran)) 106 (if (memq calc-language '(c fortran))
84 (math-float (nth 1 aa)) 107 (math-float (nth 1 aa))
152 (setq temp (apply (cdr temp2) (cdr a))) 175 (setq temp (apply (cdr temp2) (cdr a)))
153 (math-compose-expr temp prec)) 176 (math-compose-expr temp prec))
154 (and (setq temp2 (assq nil (cdr temp))) 177 (and (setq temp2 (assq nil (cdr temp)))
155 (funcall (cdr temp2) a)))))))) 178 (funcall (cdr temp2) a))))))))
156 ((eq (car a) 'vec) 179 ((eq (car a) 'vec)
157 (let* ((left-bracket (if calc-vector-brackets 180 (let* ((math-comp-left-bracket (if calc-vector-brackets
158 (substring calc-vector-brackets 0 1) "")) 181 (substring calc-vector-brackets 0 1) ""))
159 (right-bracket (if calc-vector-brackets 182 (math-comp-right-bracket (if calc-vector-brackets
160 (substring calc-vector-brackets 1 2) "")) 183 (substring calc-vector-brackets 1 2) ""))
161 (inner-brackets (memq 'R calc-matrix-brackets)) 184 (inner-brackets (memq 'R calc-matrix-brackets))
162 (outer-brackets (memq 'O calc-matrix-brackets)) 185 (outer-brackets (memq 'O calc-matrix-brackets))
163 (row-commas (memq 'C calc-matrix-brackets)) 186 (row-commas (memq 'C calc-matrix-brackets))
164 (comma-spc (or calc-vector-commas " ")) 187 (math-comp-comma-spc (or calc-vector-commas " "))
165 (comma (or calc-vector-commas "")) 188 (math-comp-comma (or calc-vector-commas ""))
166 (vector-prec (if (or (and calc-vector-commas 189 (math-comp-vector-prec (if (or (and calc-vector-commas
167 (math-vector-no-parens a)) 190 (math-vector-no-parens a))
168 (memq 'P calc-matrix-brackets)) 0 1000)) 191 (memq 'P calc-matrix-brackets)) 0 1000))
169 (just (cond ((eq calc-matrix-just 'right) 'vright) 192 (math-comp-just (cond ((eq calc-matrix-just 'right) 'vright)
170 ((eq calc-matrix-just 'center) 'vcent) 193 ((eq calc-matrix-just 'center) 'vcent)
171 (t 'vleft))) 194 (t 'vleft)))
172 (break calc-break-vectors)) 195 (break calc-break-vectors))
173 (if (and (memq calc-language '(nil big)) 196 (if (and (memq calc-language '(nil big))
174 (not calc-break-vectors) 197 (not calc-break-vectors)
175 (math-matrixp a) (not (math-matrixp (nth 1 a))) 198 (math-matrixp a) (not (math-matrixp (nth 1 a)))
176 (or calc-full-vectors 199 (or calc-full-vectors
177 (and (< (length a) 7) (< (length (nth 1 a)) 7)) 200 (and (< (length a) 7) (< (length (nth 1 a)) 7))
178 (progn (setq break t) nil))) 201 (progn (setq break t) nil)))
179 (if (progn 202 (if (progn
180 (setq vector-prec (if (or (and calc-vector-commas 203 (setq math-comp-vector-prec (if (or (and calc-vector-commas
181 (math-vector-no-parens 204 (math-vector-no-parens
182 (nth 1 a))) 205 (nth 1 a)))
183 (memq 'P calc-matrix-brackets)) 206 (memq 'P calc-matrix-brackets))
184 0 1000)) 207 0 1000))
185 (= (length a) 2)) 208 (= (length a) 2))
186 (list 'horiz 209 (list 'horiz
187 (concat left-bracket left-bracket " ") 210 (concat math-comp-left-bracket math-comp-left-bracket " ")
188 (math-compose-vector (cdr (nth 1 a)) (concat comma " ") 211 (math-compose-vector (cdr (nth 1 a)) (concat math-comp-comma " ")
189 vector-prec) 212 math-comp-vector-prec)
190 (concat " " right-bracket right-bracket)) 213 (concat " " math-comp-right-bracket math-comp-right-bracket))
191 (let* ((rows (1- (length a))) 214 (let* ((rows (1- (length a)))
192 (cols (1- (length (nth 1 a)))) 215 (cols (1- (length (nth 1 a))))
193 (base (/ (1- rows) 2)) 216 (base (/ (1- rows) 2))
194 (calc-language 'flat)) 217 (calc-language 'flat))
195 (append '(horiz) 218 (append '(horiz)
196 (list (append '(vleft) 219 (list (append '(vleft)
197 (list base) 220 (list base)
198 (list (concat (and outer-brackets 221 (list (concat (and outer-brackets
199 (concat left-bracket 222 (concat math-comp-left-bracket
200 " ")) 223 " "))
201 (and inner-brackets 224 (and inner-brackets
202 (concat left-bracket 225 (concat math-comp-left-bracket
203 " ")))) 226 " "))))
204 (make-list (1- rows) 227 (make-list (1- rows)
205 (concat (and outer-brackets 228 (concat (and outer-brackets
206 " ") 229 " ")
207 (and inner-brackets 230 (and inner-brackets
208 (concat 231 (concat
209 left-bracket 232 math-comp-left-bracket
210 " ")))))) 233 " "))))))
211 (math-compose-matrix (cdr a) 1 cols base) 234 (math-compose-matrix (cdr a) 1 cols base)
212 (list (append '(vleft) 235 (list (append '(vleft)
213 (list base) 236 (list base)
214 (make-list (1- rows) 237 (make-list (1- rows)
215 (if inner-brackets 238 (if inner-brackets
216 (concat " " 239 (concat " "
217 right-bracket 240 math-comp-right-bracket
218 (and row-commas 241 (and row-commas
219 comma)) 242 math-comp-comma))
220 (if (and outer-brackets 243 (if (and outer-brackets
221 row-commas) 244 row-commas)
222 ";" ""))) 245 ";" "")))
223 (list (concat 246 (list (concat
224 (and inner-brackets 247 (and inner-brackets
225 (concat " " 248 (concat " "
226 right-bracket)) 249 math-comp-right-bracket))
227 (and outer-brackets 250 (and outer-brackets
228 (concat 251 (concat
229 " " 252 " "
230 right-bracket))))))))) 253 math-comp-right-bracket)))))))))
231 (if (and calc-display-strings 254 (if (and calc-display-strings
232 (cdr a) 255 (cdr a)
233 (math-vector-is-string a)) 256 (math-vector-is-string a))
234 (math-vector-to-string a t) 257 (math-vector-to-string a t)
235 (if (and break (cdr a) 258 (if (and break (cdr a)
236 (not (eq calc-language 'flat))) 259 (not (eq calc-language 'flat)))
237 (let* ((full (or calc-full-vectors (< (length a) 7))) 260 (let* ((full (or calc-full-vectors (< (length a) 7)))
238 (rows (if full (1- (length a)) 5)) 261 (rows (if full (1- (length a)) 5))
239 (base (/ (1- rows) 2)) 262 (base (/ (1- rows) 2))
240 (just 'vleft)
241 (calc-break-vectors nil)) 263 (calc-break-vectors nil))
242 (list 'horiz 264 (list 'horiz
243 (cons 'vleft (cons base 265 (cons 'vleft (cons base
244 (math-compose-rows 266 (math-compose-rows
245 (cdr a) 267 (cdr a)
246 (if full rows 3) t))))) 268 (if full rows 3) t)))))
247 (if (or calc-full-vectors (< (length a) 7)) 269 (if (or calc-full-vectors (< (length a) 7))
248 (if (and (eq calc-language 'tex) 270 (if (and (eq calc-language 'tex)
249 (math-matrixp a)) 271 (math-matrixp a))
250 (append '(horiz "\\matrix{ ") 272 (if (and (integerp calc-language-option)
251 (math-compose-tex-matrix (cdr a)) 273 (or (= calc-language-option 0)
252 '(" }")) 274 (> calc-language-option 1)
253 (if (and (eq calc-language 'eqn) 275 (< calc-language-option -1)))
254 (math-matrixp a)) 276 (append '(vleft 0 "\\matrix{")
255 (append '(horiz "matrix { ") 277 (math-compose-tex-matrix (cdr a))
256 (math-compose-eqn-matrix 278 '("}"))
257 (cdr (math-transpose a))) 279 (append '(horiz "\\matrix{ ")
258 '("}")) 280 (math-compose-tex-matrix (cdr a))
259 (if (and (eq calc-language 'maple) 281 '(" }")))
260 (math-matrixp a)) 282 (if (and (eq calc-language 'latex)
261 (list 'horiz 283 (math-matrixp a))
262 "matrix(" 284 (if (and (integerp calc-language-option)
263 left-bracket 285 (or (= calc-language-option 0)
264 (math-compose-vector (cdr a) (concat comma " ") 286 (> calc-language-option 1)
265 vector-prec) 287 (< calc-language-option -1)))
266 right-bracket 288 (append '(vleft 0 "\\begin{pmatrix}")
267 ")") 289 (math-compose-tex-matrix (cdr a))
268 (list 'horiz 290 '("\\end{pmatrix}"))
269 left-bracket 291 (append '(horiz "\\begin{pmatrix} ")
270 (math-compose-vector (cdr a) (concat comma " ") 292 (math-compose-tex-matrix (cdr a))
271 vector-prec) 293 '(" \\end{pmatrix}")))
272 right-bracket)))) 294 (if (and (eq calc-language 'eqn)
295 (math-matrixp a))
296 (append '(horiz "matrix { ")
297 (math-compose-eqn-matrix
298 (cdr (math-transpose a)))
299 '("}"))
300 (if (and (eq calc-language 'maple)
301 (math-matrixp a))
302 (list 'horiz
303 "matrix("
304 math-comp-left-bracket
305 (math-compose-vector (cdr a)
306 (concat math-comp-comma " ")
307 math-comp-vector-prec)
308 math-comp-right-bracket
309 ")")
310 (list 'horiz
311 math-comp-left-bracket
312 (math-compose-vector (cdr a)
313 (concat math-comp-comma " ")
314 math-comp-vector-prec)
315 math-comp-right-bracket)))))
273 (list 'horiz 316 (list 'horiz
274 left-bracket 317 math-comp-left-bracket
275 (math-compose-vector (list (nth 1 a) (nth 2 a) (nth 3 a)) 318 (math-compose-vector (list (nth 1 a) (nth 2 a) (nth 3 a))
276 (concat comma " ") vector-prec) 319 (concat math-comp-comma " ")
277 comma (if (eq calc-language 'tex) " \\ldots" " ...") 320 math-comp-vector-prec)
278 comma " " 321 math-comp-comma (if (memq calc-language '(tex latex))
322 " \\ldots" " ...")
323 math-comp-comma " "
279 (list 'break math-compose-level) 324 (list 'break math-compose-level)
280 (math-compose-expr (nth (1- (length a)) a) 325 (math-compose-expr (nth (1- (length a)) a)
281 (if (equal comma "") 1000 0)) 326 (if (equal math-comp-comma "") 1000 0))
282 right-bracket))))))) 327 math-comp-right-bracket)))))))
283 ((eq (car a) 'incomplete) 328 ((eq (car a) 'incomplete)
284 (if (cdr (cdr a)) 329 (if (cdr (cdr a))
285 (cond ((eq (nth 1 a) 'vec) 330 (cond ((eq (nth 1 a) 'vec)
286 (list 'horiz "[" 331 (list 'horiz "["
287 (math-compose-vector (cdr (cdr a)) ", " 0) 332 (math-compose-vector (cdr (cdr a)) ", " 0)
306 (t "( ...")))) 351 (t "( ..."))))
307 ((eq (car a) 'var) 352 ((eq (car a) 'var)
308 (let ((v (rassq (nth 2 a) math-expr-variable-mapping))) 353 (let ((v (rassq (nth 2 a) math-expr-variable-mapping)))
309 (if v 354 (if v
310 (symbol-name (car v)) 355 (symbol-name (car v))
311 (if (and (eq calc-language 'tex) 356 (if (and (memq calc-language '(tex latex))
312 calc-language-option 357 calc-language-option
313 (not (= calc-language-option 0)) 358 (not (= calc-language-option 0))
314 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" 359 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
315 (symbol-name (nth 1 a)))) 360 (symbol-name (nth 1 a))))
316 (format "\\hbox{%s}" (symbol-name (nth 1 a))) 361 (if (eq calc-language 'latex)
362 (format "\\text{%s}" (symbol-name (nth 1 a)))
363 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
317 (if (and math-compose-hash-args 364 (if (and math-compose-hash-args
318 (let ((p calc-arg-values)) 365 (let ((p calc-arg-values))
319 (setq v 1) 366 (setq v 1)
320 (while (and p (not (equal (car p) a))) 367 (while (and p (not (equal (car p) a)))
321 (setq p (and (eq math-compose-hash-args t) (cdr p)) 368 (setq p (and (eq math-compose-hash-args t) (cdr p))
339 ((eq (car a) 'intv) 386 ((eq (car a) 'intv)
340 (list 'horiz 387 (list 'horiz
341 (if (eq calc-language 'maple) "" 388 (if (eq calc-language 'maple) ""
342 (if (memq (nth 1 a) '(0 1)) "(" "[")) 389 (if (memq (nth 1 a) '(0 1)) "(" "["))
343 (math-compose-expr (nth 2 a) 0) 390 (math-compose-expr (nth 2 a) 0)
344 (if (eq calc-language 'tex) " \\ldots " 391 (if (memq calc-language '(tex latex)) " \\ldots "
345 (if (eq calc-language 'eqn) " ... " " .. ")) 392 (if (eq calc-language 'eqn) " ... " " .. "))
346 (math-compose-expr (nth 3 a) 0) 393 (math-compose-expr (nth 3 a) 0)
347 (if (eq calc-language 'maple) "" 394 (if (eq calc-language 'maple) ""
348 (if (memq (nth 1 a) '(0 2)) ")" "]")))) 395 (if (memq (nth 1 a) '(0 2)) ")" "]"))))
349 ((eq (car a) 'date) 396 ((eq (car a) 'date)
384 (math-compose-expr (nth 1 a) 1000) 431 (math-compose-expr (nth 1 a) 1000)
385 "[[" 432 "[["
386 (math-compose-expr (nth 2 a) 0) 433 (math-compose-expr (nth 2 a) 0)
387 "]]")) 434 "]]"))
388 ((and (eq (car a) 'calcFunc-sqrt) 435 ((and (eq (car a) 'calcFunc-sqrt)
389 (eq calc-language 'tex)) 436 (memq calc-language '(tex latex)))
390 (list 'horiz 437 (list 'horiz
391 "\\sqrt{" 438 "\\sqrt{"
392 (math-compose-expr (nth 1 a) 0) 439 (math-compose-expr (nth 1 a) 0)
393 "}")) 440 "}"))
394 ((and nil (eq (car a) 'calcFunc-sqrt) 441 ((and nil (eq (car a) 'calcFunc-sqrt)
420 (math-compose-expr (nth 2 a) 0)))) 467 (math-compose-expr (nth 2 a) 0))))
421 (list 'vcent 468 (list 'vcent
422 (math-comp-height a1) 469 (math-comp-height a1)
423 a1 '(rule ?-) a2))) 470 a1 '(rule ?-) a2)))
424 ((and (memq (car a) '(calcFunc-sum calcFunc-prod)) 471 ((and (memq (car a) '(calcFunc-sum calcFunc-prod))
425 (eq calc-language 'tex) 472 (memq calc-language '(tex latex))
426 (= (length a) 5)) 473 (= (length a) 5))
427 (list 'horiz (if (eq (car a) 'calcFunc-sum) "\\sum" "\\prod") 474 (list 'horiz (if (eq (car a) 'calcFunc-sum) "\\sum" "\\prod")
428 "_{" (math-compose-expr (nth 2 a) 0) 475 "_{" (math-compose-expr (nth 2 a) 0)
429 "=" (math-compose-expr (nth 3 a) 0) 476 "=" (math-compose-expr (nth 3 a) 0)
430 "}^{" (math-compose-expr (nth 4 a) 0) 477 "}^{" (math-compose-expr (nth 4 a) 0)
475 (not (eq calc-language 'unform)) 522 (not (eq calc-language 'unform))
476 (= (length a) 3) 523 (= (length a) 3)
477 (integerp (nth 2 a))) 524 (integerp (nth 2 a)))
478 (let ((c (math-compose-expr (nth 1 a) -1))) 525 (let ((c (math-compose-expr (nth 1 a) -1)))
479 (if (> prec (nth 2 a)) 526 (if (> prec (nth 2 a))
480 (if (eq calc-language 'tex) 527 (if (memq calc-language '(tex latex))
481 (list 'horiz "\\left( " c " \\right)") 528 (list 'horiz "\\left( " c " \\right)")
482 (if (eq calc-language 'eqn) 529 (if (eq calc-language 'eqn)
483 (list 'horiz "{left ( " c " right )}") 530 (list 'horiz "{left ( " c " right )}")
484 (list 'horiz "(" c ")"))) 531 (list 'horiz "(" c ")")))
485 c))) 532 c)))
613 (+ (* (1- (nth 1 a)) (+ ca cd)) (1- ca)) 660 (+ (* (1- (nth 1 a)) (+ ca cd)) (1- ca))
614 (/ (1- (* (nth 1 a) (+ ca cd))) 2))) 661 (/ (1- (* (nth 1 a) (+ ca cd))) 2)))
615 (make-list (nth 1 a) c)))))) 662 (make-list (nth 1 a) c))))))
616 ((and (eq (car a) 'calcFunc-evalto) 663 ((and (eq (car a) 'calcFunc-evalto)
617 (setq calc-any-evaltos t) 664 (setq calc-any-evaltos t)
618 (memq calc-language '(tex eqn)) 665 (memq calc-language '(tex latex eqn))
619 (= math-compose-level (if math-comp-tagged 2 1)) 666 (= math-compose-level (if math-comp-tagged 2 1))
620 (= (length a) 3)) 667 (= (length a) 3))
621 (list 'horiz 668 (list 'horiz
622 (if (eq calc-language 'tex) "\\evalto " "evalto ") 669 (if (memq calc-language '(tex latex)) "\\evalto " "evalto ")
623 (math-compose-expr (nth 1 a) 0) 670 (math-compose-expr (nth 1 a) 0)
624 (if (eq calc-language 'tex) " \\to " " -> ") 671 (if (memq calc-language '(tex latex)) " \\to " " -> ")
625 (math-compose-expr (nth 2 a) 0))) 672 (math-compose-expr (nth 2 a) 0)))
626 (t 673 (t
627 (let ((op (and (not (eq calc-language 'unform)) 674 (let ((op (and (not (eq calc-language 'unform))
628 (if (and (eq (car a) 'calcFunc-if) (= (length a) 4)) 675 (if (and (eq (car a) 'calcFunc-if) (= (length a) 4))
629 (assoc "?" math-expr-opers) 676 (assoc "?" math-expr-opers)
631 (cond ((and op 678 (cond ((and op
632 (or (= (length a) 3) (eq (car a) 'calcFunc-if)) 679 (or (= (length a) 3) (eq (car a) 'calcFunc-if))
633 (/= (nth 3 op) -1)) 680 (/= (nth 3 op) -1))
634 (cond 681 (cond
635 ((> prec (or (nth 4 op) (min (nth 2 op) (nth 3 op)))) 682 ((> prec (or (nth 4 op) (min (nth 2 op) (nth 3 op))))
636 (if (and (eq calc-language 'tex) 683 (if (and (memq calc-language '(tex latex))
637 (not (math-tex-expr-is-flat a))) 684 (not (math-tex-expr-is-flat a)))
638 (if (eq (car-safe a) '/) 685 (if (eq (car-safe a) '/)
639 (list 'horiz "{" (math-compose-expr a -1) "}") 686 (list 'horiz "{" (math-compose-expr a -1) "}")
640 (list 'horiz "\\left( " 687 (list 'horiz "\\left( "
641 (math-compose-expr a -1) 688 (math-compose-expr a -1)
648 (list 'horiz "( " (math-compose-expr a -1) " )") 695 (list 'horiz "( " (math-compose-expr a -1) " )")
649 (list 'horiz "{left ( " 696 (list 'horiz "{left ( "
650 (math-compose-expr a -1) 697 (math-compose-expr a -1)
651 " right )}"))) 698 " right )}")))
652 (list 'horiz "(" (math-compose-expr a 0) ")")))) 699 (list 'horiz "(" (math-compose-expr a 0) ")"))))
653 ((and (eq calc-language 'tex) 700 ((and (memq calc-language '(tex latex))
654 (memq (car a) '(/ calcFunc-choose calcFunc-evalto)) 701 (memq (car a) '(/ calcFunc-choose calcFunc-evalto))
655 (>= prec 0)) 702 (>= prec 0))
656 (list 'horiz "{" (math-compose-expr a -1) "}")) 703 (list 'horiz "{" (math-compose-expr a -1) "}"))
657 ((eq (car a) 'calcFunc-if) 704 ((eq (car a) 'calcFunc-if)
658 (list 'horiz 705 (list 'horiz
674 (lhs (math-compose-expr (nth 1 a) (nth 2 op))) 721 (lhs (math-compose-expr (nth 1 a) (nth 2 op)))
675 (rhs (math-compose-expr (nth 2 a) (nth 3 op)))) 722 (rhs (math-compose-expr (nth 2 a) (nth 3 op))))
676 (and (equal (car op) "^") 723 (and (equal (car op) "^")
677 (eq (math-comp-first-char lhs) ?-) 724 (eq (math-comp-first-char lhs) ?-)
678 (setq lhs (list 'horiz "(" lhs ")"))) 725 (setq lhs (list 'horiz "(" lhs ")")))
679 (and (eq calc-language 'tex) 726 (and (memq calc-language '(tex latex))
680 (or (equal (car op) "^") (equal (car op) "_")) 727 (or (equal (car op) "^") (equal (car op) "_"))
681 (not (and (stringp rhs) (= (length rhs) 1))) 728 (not (and (stringp rhs) (= (length rhs) 1)))
682 (setq rhs (list 'horiz "{" rhs "}"))) 729 (setq rhs (list 'horiz "{" rhs "}")))
683 (or (and (eq (car a) '*) 730 (or (and (eq (car a) '*)
684 (or (null calc-language) 731 (or (null calc-language)
741 ((and op (= (length a) 2) (= (nth 3 op) -1)) 788 ((and op (= (length a) 2) (= (nth 3 op) -1))
742 (cond 789 (cond
743 ((or (> prec (or (nth 4 op) (nth 2 op))) 790 ((or (> prec (or (nth 4 op) (nth 2 op)))
744 (and (not (eq (assoc (car op) math-expr-opers) op)) 791 (and (not (eq (assoc (car op) math-expr-opers) op))
745 (> prec 0))) ; don't write x% + y 792 (> prec 0))) ; don't write x% + y
746 (if (and (eq calc-language 'tex) 793 (if (and (memq calc-language '(tex latex))
747 (not (math-tex-expr-is-flat a))) 794 (not (math-tex-expr-is-flat a)))
748 (list 'horiz "\\left( " 795 (list 'horiz "\\left( "
749 (math-compose-expr a -1) 796 (math-compose-expr a -1)
750 " \\right)") 797 " \\right)")
751 (if (eq calc-language 'eqn) 798 (if (eq calc-language 'eqn)
766 (concat " " (car op)) 813 (concat " " (car op))
767 (car op))))))) 814 (car op)))))))
768 ((and op (= (length a) 2) (= (nth 2 op) -1)) 815 ((and op (= (length a) 2) (= (nth 2 op) -1))
769 (cond 816 (cond
770 ((eq (nth 3 op) 0) 817 ((eq (nth 3 op) 0)
771 (let ((lr (and (eq calc-language 'tex) 818 (let ((lr (and (memq calc-language '(tex latex))
772 (not (math-tex-expr-is-flat (nth 1 a)))))) 819 (not (math-tex-expr-is-flat (nth 1 a))))))
773 (list 'horiz 820 (list 'horiz
774 (if lr "\\left" "") 821 (if lr "\\left" "")
775 (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op)) 822 (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op))
776 (substring (car op) 1) 823 (substring (car op) 1)
779 (math-compose-expr (nth 1 a) -1) 826 (math-compose-expr (nth 1 a) -1)
780 (if (or lr (> (length (car op)) 2)) " " "") 827 (if (or lr (> (length (car op)) 2)) " " "")
781 (if lr "\\right" "") 828 (if lr "\\right" "")
782 (car (nth 1 (memq op math-expr-opers)))))) 829 (car (nth 1 (memq op math-expr-opers))))))
783 ((> prec (or (nth 4 op) (nth 3 op))) 830 ((> prec (or (nth 4 op) (nth 3 op)))
784 (if (and (eq calc-language 'tex) 831 (if (and (memq calc-language '(tex latex))
785 (not (math-tex-expr-is-flat a))) 832 (not (math-tex-expr-is-flat a)))
786 (list 'horiz "\\left( " 833 (list 'horiz "\\left( "
787 (math-compose-expr a -1) 834 (math-compose-expr a -1)
788 " \\right)") 835 " \\right)")
789 (if (eq calc-language 'eqn) 836 (if (eq calc-language 'eqn)
816 ( big . math-compose-normal ) 863 ( big . math-compose-normal )
817 ( c . math-compose-c ) 864 ( c . math-compose-c )
818 ( pascal . math-compose-pascal ) 865 ( pascal . math-compose-pascal )
819 ( fortran . math-compose-fortran ) 866 ( fortran . math-compose-fortran )
820 ( tex . math-compose-tex ) 867 ( tex . math-compose-tex )
868 ( latex . math-compose-latex )
821 ( eqn . math-compose-eqn ) 869 ( eqn . math-compose-eqn )
822 ( math . math-compose-math ) 870 ( math . math-compose-math )
823 ( maple . math-compose-maple )))) 871 ( maple . math-compose-maple ))))
824 (setq op (get (car a) (cdr op))) 872 (setq op (get (car a) (cdr op)))
825 (funcall op a prec))) 873 (funcall op a prec)))
846 (symbol-name func)) 894 (symbol-name func))
847 (math-match-substring (symbol-name func) 1) 895 (math-match-substring (symbol-name func) 1)
848 (symbol-name func)))) 896 (symbol-name func))))
849 (if (memq calc-language '(c fortran pascal maple)) 897 (if (memq calc-language '(c fortran pascal maple))
850 (setq func (math-to-underscores func))) 898 (setq func (math-to-underscores func)))
851 (if (and (eq calc-language 'tex) 899 (if (and (memq calc-language '(tex latex))
852 calc-language-option 900 calc-language-option
853 (not (= calc-language-option 0)) 901 (not (= calc-language-option 0))
854 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func)) 902 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
855 (if (< (prefix-numeric-value calc-language-option) 0) 903 (if (< (prefix-numeric-value calc-language-option) 0)
856 (setq func (format "\\%s" func)) 904 (setq func (format "\\%s" func))
857 (setq func (format "\\hbox{%s}" func)))) 905 (setq func (if (eq calc-language 'latex)
906 (format "\\text{%s}" func)
907 (format "\\hbox{%s}" func)))))
858 (if (and (eq calc-language 'eqn) 908 (if (and (eq calc-language 'eqn)
859 (string-match "[^']'+\\'" func)) 909 (string-match "[^']'+\\'" func))
860 (let ((n (- (length func) (match-beginning 0) 1))) 910 (let ((n (- (length func) (match-beginning 0) 1)))
861 (setq func (substring func 0 (- n))) 911 (setq func (substring func 0 (- n)))
862 (while (>= (setq n (1- n)) 0) 912 (while (>= (setq n (1- n)) 0)
863 (setq func (concat func " prime"))))) 913 (setq func (concat func " prime")))))
864 (cond ((and (eq calc-language 'tex) 914 (cond ((and (memq calc-language '(tex latex))
865 (or (> (length a) 2) 915 (or (> (length a) 2)
866 (not (math-tex-expr-is-flat (nth 1 a))))) 916 (not (math-tex-expr-is-flat (nth 1 a)))))
867 (setq left "\\left( " 917 (setq left "\\left( "
868 right " \\right)")) 918 right " \\right)"))
869 ((and (eq calc-language 'eqn) 919 ((and (eq calc-language 'eqn)
870 (or (> (length a) 2) 920 (or (> (length a) 2)
871 (not (math-tex-expr-is-flat (nth 1 a))))) 921 (not (math-tex-expr-is-flat (nth 1 a)))))
872 (setq left "{left ( " 922 (setq left "{left ( "
873 right " right )}")) 923 right " right )}"))
874 ((and (or (and (eq calc-language 'tex) 924 ((and (or (and (memq calc-language '(tex latex))
875 (eq (aref func 0) ?\\)) 925 (eq (aref func 0) ?\\))
876 (and (eq calc-language 'eqn) 926 (and (eq calc-language 'eqn)
877 (memq (car a) math-eqn-special-funcs))) 927 (memq (car a) math-eqn-special-funcs)))
878 (not (string-match "\\hbox{" func)) 928 (not (or
929 (string-match "\\hbox{" func)
930 (string-match "\\text{" func)))
879 (= (length a) 2) 931 (= (length a) 2)
880 (or (Math-realp (nth 1 a)) 932 (or (Math-realp (nth 1 a))
881 (memq (car (nth 1 a)) '(var *)))) 933 (memq (car (nth 1 a)) '(var *))))
882 (setq left (if (eq calc-language 'eqn) "~{" "{") 934 (setq left (if (eq calc-language 'eqn) "~{" "{")
883 right "}")) 935 right "}"))
927 979
928 (defun math-compose-matrix (a col cols base) 980 (defun math-compose-matrix (a col cols base)
929 (let ((col 0) 981 (let ((col 0)
930 (res nil)) 982 (res nil))
931 (while (<= (setq col (1+ col)) cols) 983 (while (<= (setq col (1+ col)) cols)
932 (setq res (cons (cons just 984 (setq res (cons (cons math-comp-just
933 (cons base 985 (cons base
934 (mapcar (function 986 (mapcar (function
935 (lambda (r) 987 (lambda (r)
936 (list 'horiz 988 (list 'horiz
937 (math-compose-expr 989 (math-compose-expr
938 (nth col r) 990 (nth col r)
939 vector-prec) 991 math-comp-vector-prec)
940 (if (= col cols) 992 (if (= col cols)
941 "" 993 ""
942 (concat comma-spc " "))))) 994 (concat
995 math-comp-comma-spc " ")))))
943 a))) 996 a)))
944 res))) 997 res)))
945 (nreverse res))) 998 (nreverse res)))
946 999
947 (defun math-compose-rows (a count first) 1000 (defun math-compose-rows (a count first)
948 (if (cdr a) 1001 (if (cdr a)
949 (if (<= count 0) 1002 (if (<= count 0)
950 (if (< count 0) 1003 (if (< count 0)
951 (math-compose-rows (cdr a) -1 nil) 1004 (math-compose-rows (cdr a) -1 nil)
952 (cons (concat (if (eq calc-language 'tex) " \\ldots" " ...") 1005 (cons (concat (if (memq calc-language '(tex latex)) " \\ldots" " ...")
953 comma) 1006 math-comp-comma)
954 (math-compose-rows (cdr a) -1 nil))) 1007 (math-compose-rows (cdr a) -1 nil)))
955 (cons (list 'horiz 1008 (cons (list 'horiz
956 (if first (concat left-bracket " ") " ") 1009 (if first (concat math-comp-left-bracket " ") " ")
957 (math-compose-expr (car a) vector-prec) 1010 (math-compose-expr (car a) math-comp-vector-prec)
958 comma) 1011 math-comp-comma)
959 (math-compose-rows (cdr a) (1- count) nil))) 1012 (math-compose-rows (cdr a) (1- count) nil)))
960 (list (list 'horiz 1013 (list (list 'horiz
961 (if first (concat left-bracket " ") " ") 1014 (if first (concat math-comp-left-bracket " ") " ")
962 (math-compose-expr (car a) vector-prec) 1015 (math-compose-expr (car a) math-comp-vector-prec)
963 (concat " " right-bracket))))) 1016 (concat " " math-comp-right-bracket)))))
964 1017
965 (defun math-compose-tex-matrix (a) 1018 (defun math-compose-tex-matrix (a)
966 (if (cdr a) 1019 (if (cdr a)
967 (cons (math-compose-vector (cdr (car a)) " & " 0) 1020 (cons (append (math-compose-vector (cdr (car a)) " & " 0) '(" \\\\ "))
968 (cons " \\\\ " 1021 (math-compose-tex-matrix (cdr a)))
969 (math-compose-tex-matrix (cdr a))))
970 (list (math-compose-vector (cdr (car a)) " & " 0)))) 1022 (list (math-compose-vector (cdr (car a)) " & " 0))))
971 1023
972 (defun math-compose-eqn-matrix (a) 1024 (defun math-compose-eqn-matrix (a)
973 (if a 1025 (if a
974 (cons 1026 (cons
1200 (if (memq (car-safe (nth 1 a)) '(calcFunc-sum calcFunc-prod)) 1252 (if (memq (car-safe (nth 1 a)) '(calcFunc-sum calcFunc-prod))
1201 " " "") 1253 " " "")
1202 expr 1254 expr
1203 (if (memq prec '(196 201)) ")" ""))))) 1255 (if (memq prec '(196 201)) ")" "")))))
1204 1256
1257 ;; The variables math-svo-c, math-svo-wid and math-svo-off are local
1258 ;; to math-stack-value-offset in calc.el, but are used by
1259 ;; math-stack-value-offset-fancy, which is called by math-stack-value-offset..
1260 (defvar math-svo-c)
1261 (defvar math-svo-wid)
1262 (defvar math-svo-off)
1205 1263
1206 (defun math-stack-value-offset-fancy () 1264 (defun math-stack-value-offset-fancy ()
1207 (let ((cwid (+ (math-comp-width c)))) 1265 (let ((cwid (+ (math-comp-width math-svo-c))))
1208 (cond ((eq calc-display-just 'right) 1266 (cond ((eq calc-display-just 'right)
1209 (if calc-display-origin 1267 (if calc-display-origin
1210 (setq wid (max calc-display-origin 5)) 1268 (setq math-svo-wid (max calc-display-origin 5))
1211 (if (integerp calc-line-breaking) 1269 (if (integerp calc-line-breaking)
1212 (setq wid calc-line-breaking))) 1270 (setq math-svo-wid calc-line-breaking)))
1213 (setq off (- wid cwid 1271 (setq math-svo-off (- math-svo-wid cwid
1214 (max (- (length calc-right-label) 1272 (max (- (length calc-right-label)
1215 (if (and (integerp calc-line-breaking) 1273 (if (and (integerp calc-line-breaking)
1216 calc-display-origin) 1274 calc-display-origin)
1217 (max (- calc-line-breaking 1275 (max (- calc-line-breaking
1218 calc-display-origin) 1276 calc-display-origin)
1220 0)) 1278 0))
1221 0)))) 1279 0))))
1222 (t 1280 (t
1223 (if calc-display-origin 1281 (if calc-display-origin
1224 (progn 1282 (progn
1225 (setq off (- calc-display-origin (/ cwid 2))) 1283 (setq math-svo-off (- calc-display-origin (/ cwid 2)))
1226 (if (integerp calc-line-breaking) 1284 (if (integerp calc-line-breaking)
1227 (setq off (min off (- calc-line-breaking cwid 1285 (setq math-svo-off (min math-svo-off (- calc-line-breaking cwid
1228 (length calc-right-label))))) 1286 (length calc-right-label)))))
1229 (if (>= off 0) 1287 (if (>= math-svo-off 0)
1230 (setq wid (max wid (+ off cwid))))) 1288 (setq math-svo-wid (max math-svo-wid (+ math-svo-off cwid)))))
1231 (if (integerp calc-line-breaking) 1289 (if (integerp calc-line-breaking)
1232 (setq wid calc-line-breaking)) 1290 (setq math-svo-wid calc-line-breaking))
1233 (setq off (/ (- wid cwid) 2))))) 1291 (setq math-svo-off (/ (- math-svo-wid cwid) 2)))))
1234 (and (integerp calc-line-breaking) 1292 (and (integerp calc-line-breaking)
1235 (or (< off 0) 1293 (or (< math-svo-off 0)
1236 (and calc-display-origin 1294 (and calc-display-origin
1237 (> calc-line-breaking calc-display-origin))) 1295 (> calc-line-breaking calc-display-origin)))
1238 (setq wid calc-line-breaking)))) 1296 (setq math-svo-wid calc-line-breaking))))
1239
1240 1297
1241 1298
1242 ;;; Convert a composition to string form, with embedded \n's if necessary. 1299 ;;; Convert a composition to string form, with embedded \n's if necessary.
1243 1300
1244 (defun math-composition-to-string (c &optional width) 1301 (defun math-composition-to-string (c &optional width)
1271 1328
1272 ;;; Convert a one-line composition to a string. Break into multiple 1329 ;;; Convert a one-line composition to a string. Break into multiple
1273 ;;; lines if necessary, choosing break points according to the structure 1330 ;;; lines if necessary, choosing break points according to the structure
1274 ;;; of the formula. 1331 ;;; of the formula.
1275 1332
1276 (defun math-comp-to-string-flat (c full-width) 1333 ;; The variables math-comp-full-width, math-comp-highlight, math-comp-word,
1334 ;; math-comp-level, math-comp-margin and math-comp-buf are local to
1335 ;; math-comp-to-string-flat, but are used by math-comp-to-string-flat-term,
1336 ;; which is called by math-comp-to-string-flat.
1337 ;; math-comp-highlight and math-comp-buf are also local to
1338 ;; math-comp-simplify-term and math-comp-simplify respectively, but are used
1339 ;; by math-comp-add-string.
1340 (defvar math-comp-full-width)
1341 (defvar math-comp-highlight)
1342 (defvar math-comp-word)
1343 (defvar math-comp-level)
1344 (defvar math-comp-margin)
1345 (defvar math-comp-buf)
1346 ;; The variable math-comp-pos is local to math-comp-to-string-flat, but
1347 ;; is used by math-comp-to-string-flat-term and math-comp-sel-first-term,
1348 ;; which are called by math-comp-to-string-flat.
1349 (defvar math-comp-pos)
1350
1351 (defun math-comp-to-string-flat (c math-comp-full-width)
1277 (if math-comp-sel-hpos 1352 (if math-comp-sel-hpos
1278 (let ((comp-pos 0)) 1353 (let ((math-comp-pos 0))
1279 (math-comp-sel-flat-term c)) 1354 (math-comp-sel-flat-term c))
1280 (let ((comp-buf "") 1355 (let ((math-comp-buf "")
1281 (comp-word "") 1356 (math-comp-word "")
1282 (comp-pos 0) 1357 (math-comp-pos 0)
1283 (comp-margin 0) 1358 (math-comp-margin 0)
1284 (comp-highlight (and math-comp-selected calc-show-selections)) 1359 (math-comp-highlight (and math-comp-selected calc-show-selections))
1285 (comp-level -1)) 1360 (math-comp-level -1))
1286 (math-comp-to-string-flat-term '(set -1 0)) 1361 (math-comp-to-string-flat-term '(set -1 0))
1287 (math-comp-to-string-flat-term c) 1362 (math-comp-to-string-flat-term c)
1288 (math-comp-to-string-flat-term '(break -1)) 1363 (math-comp-to-string-flat-term '(break -1))
1289 (let ((str (aref math-comp-buf-string 0)) 1364 (let ((str (aref math-comp-buf-string 0))
1290 (prefix "")) 1365 (prefix ""))
1291 (and (> (length str) 0) (= (aref str 0) ? ) 1366 (and (> (length str) 0) (= (aref str 0) ? )
1292 (> (length comp-buf) 0) 1367 (> (length math-comp-buf) 0)
1293 (let ((k (length comp-buf))) 1368 (let ((k (length math-comp-buf)))
1294 (while (not (= (aref comp-buf (setq k (1- k))) ?\n))) 1369 (while (not (= (aref math-comp-buf (setq k (1- k))) ?\n)))
1295 (aset comp-buf k ? ) 1370 (aset math-comp-buf k ? )
1296 (if (and (< (1+ k) (length comp-buf)) 1371 (if (and (< (1+ k) (length math-comp-buf))
1297 (= (aref comp-buf (1+ k)) ? )) 1372 (= (aref math-comp-buf (1+ k)) ? ))
1298 (progn 1373 (progn
1299 (aset comp-buf (1+ k) ?\n) 1374 (aset math-comp-buf (1+ k) ?\n)
1300 (setq prefix " ")) 1375 (setq prefix " "))
1301 (setq prefix "\n")))) 1376 (setq prefix "\n"))))
1302 (concat comp-buf prefix str))))) 1377 (concat math-comp-buf prefix str)))))
1303 1378
1304 (defun math-comp-to-string-flat-term (c) 1379 (defun math-comp-to-string-flat-term (c)
1305 (cond ((not (consp c)) 1380 (cond ((not (consp c))
1306 (if comp-highlight 1381 (if math-comp-highlight
1307 (setq c (math-comp-highlight-string c))) 1382 (setq c (math-comp-highlight-string c)))
1308 (setq comp-word (if (= (length comp-word) 0) c (concat comp-word c)) 1383 (setq math-comp-word (if (= (length math-comp-word) 0) c
1309 comp-pos (+ comp-pos (length c)))) 1384 (concat math-comp-word c))
1385 math-comp-pos (+ math-comp-pos (length c))))
1310 1386
1311 ((eq (car c) 'horiz) 1387 ((eq (car c) 'horiz)
1312 (while (setq c (cdr c)) 1388 (while (setq c (cdr c))
1313 (math-comp-to-string-flat-term (car c)))) 1389 (math-comp-to-string-flat-term (car c))))
1314 1390
1315 ((eq (car c) 'set) 1391 ((eq (car c) 'set)
1316 (if (nth 1 c) 1392 (if (nth 1 c)
1317 (progn 1393 (progn
1318 (setq comp-level (1+ comp-level)) 1394 (setq math-comp-level (1+ math-comp-level))
1319 (if (>= comp-level (length math-comp-buf-string)) 1395 (if (>= math-comp-level (length math-comp-buf-string))
1320 (setq math-comp-buf-string (vconcat math-comp-buf-string 1396 (setq math-comp-buf-string (vconcat math-comp-buf-string
1321 math-comp-buf-string) 1397 math-comp-buf-string)
1322 math-comp-buf-margin (vconcat math-comp-buf-margin 1398 math-comp-buf-margin (vconcat math-comp-buf-margin
1323 math-comp-buf-margin) 1399 math-comp-buf-margin)
1324 math-comp-buf-level (vconcat math-comp-buf-level 1400 math-comp-buf-level (vconcat math-comp-buf-level
1325 math-comp-buf-level))) 1401 math-comp-buf-level)))
1326 (aset math-comp-buf-string comp-level "") 1402 (aset math-comp-buf-string math-comp-level "")
1327 (aset math-comp-buf-margin comp-level (+ comp-pos 1403 (aset math-comp-buf-margin math-comp-level (+ math-comp-pos
1328 (or (nth 2 c) 0))) 1404 (or (nth 2 c) 0)))
1329 (aset math-comp-buf-level comp-level (nth 1 c))))) 1405 (aset math-comp-buf-level math-comp-level (nth 1 c)))))
1330 1406
1331 ((eq (car c) 'break) 1407 ((eq (car c) 'break)
1332 (if (not calc-line-breaking) 1408 (if (not calc-line-breaking)
1333 (setq comp-buf (concat comp-buf comp-word) 1409 (setq math-comp-buf (concat math-comp-buf math-comp-word)
1334 comp-word "") 1410 math-comp-word "")
1335 (let ((i 0) str) 1411 (let ((i 0) str)
1336 (if (and (> comp-pos full-width) 1412 (if (and (> math-comp-pos math-comp-full-width)
1337 (progn 1413 (progn
1338 (while (progn 1414 (while (progn
1339 (setq str (aref math-comp-buf-string i)) 1415 (setq str (aref math-comp-buf-string i))
1340 (and (= (length str) 0) (< i comp-level))) 1416 (and (= (length str) 0) (< i math-comp-level)))
1341 (setq i (1+ i))) 1417 (setq i (1+ i)))
1342 (or (> (length str) 0) (> (length comp-buf) 0)))) 1418 (or (> (length str) 0) (> (length math-comp-buf) 0))))
1343 (let ((prefix "") mrg wid) 1419 (let ((prefix "") mrg wid)
1344 (setq mrg (aref math-comp-buf-margin i)) 1420 (setq mrg (aref math-comp-buf-margin i))
1345 (if (> mrg 12) ; indenting too far, go back to far left 1421 (if (> mrg 12) ; indenting too far, go back to far left
1346 (let ((j i) (new (if calc-line-numbering 5 1))) 1422 (let ((j i) (new (if calc-line-numbering 5 1)))
1347 '(while (<= j comp-level) 1423 '(while (<= j math-comp-level)
1348 (aset math-comp-buf-margin j 1424 (aset math-comp-buf-margin j
1349 (+ (aref math-comp-buf-margin j) (- new mrg))) 1425 (+ (aref math-comp-buf-margin j) (- new mrg)))
1350 (setq j (1+ j))) 1426 (setq j (1+ j)))
1351 (setq mrg new))) 1427 (setq mrg new)))
1352 (setq wid (+ (length str) comp-margin)) 1428 (setq wid (+ (length str) math-comp-margin))
1353 (and (> (length str) 0) (= (aref str 0) ? ) 1429 (and (> (length str) 0) (= (aref str 0) ? )
1354 (> (length comp-buf) 0) 1430 (> (length math-comp-buf) 0)
1355 (let ((k (length comp-buf))) 1431 (let ((k (length math-comp-buf)))
1356 (while (not (= (aref comp-buf (setq k (1- k))) ?\n))) 1432 (while (not (= (aref math-comp-buf (setq k (1- k))) ?\n)))
1357 (aset comp-buf k ? ) 1433 (aset math-comp-buf k ? )
1358 (if (and (< (1+ k) (length comp-buf)) 1434 (if (and (< (1+ k) (length math-comp-buf))
1359 (= (aref comp-buf (1+ k)) ? )) 1435 (= (aref math-comp-buf (1+ k)) ? ))
1360 (progn 1436 (progn
1361 (aset comp-buf (1+ k) ?\n) 1437 (aset math-comp-buf (1+ k) ?\n)
1362 (setq prefix " ")) 1438 (setq prefix " "))
1363 (setq prefix "\n")))) 1439 (setq prefix "\n"))))
1364 (setq comp-buf (concat comp-buf prefix str "\n" 1440 (setq math-comp-buf (concat math-comp-buf prefix str "\n"
1365 (make-string mrg ? )) 1441 (make-string mrg ? ))
1366 comp-pos (+ comp-pos (- mrg wid)) 1442 math-comp-pos (+ math-comp-pos (- mrg wid))
1367 comp-margin mrg) 1443 math-comp-margin mrg)
1368 (aset math-comp-buf-string i "") 1444 (aset math-comp-buf-string i "")
1369 (while (<= (setq i (1+ i)) comp-level) 1445 (while (<= (setq i (1+ i)) math-comp-level)
1370 (if (> (aref math-comp-buf-margin i) wid) 1446 (if (> (aref math-comp-buf-margin i) wid)
1371 (aset math-comp-buf-margin i 1447 (aset math-comp-buf-margin i
1372 (+ (aref math-comp-buf-margin i) 1448 (+ (aref math-comp-buf-margin i)
1373 (- mrg wid)))))))) 1449 (- mrg wid))))))))
1374 (if (and (= (nth 1 c) (aref math-comp-buf-level comp-level)) 1450 (if (and (= (nth 1 c) (aref math-comp-buf-level math-comp-level))
1375 (< comp-pos (+ (aref math-comp-buf-margin comp-level) 2))) 1451 (< math-comp-pos (+ (aref math-comp-buf-margin math-comp-level) 2)))
1376 () ; avoid stupid breaks, e.g., "1 +\n really_long_expr" 1452 () ; avoid stupid breaks, e.g., "1 +\n really_long_expr"
1377 (let ((str (aref math-comp-buf-string comp-level))) 1453 (let ((str (aref math-comp-buf-string math-comp-level)))
1378 (setq str (if (= (length str) 0) 1454 (setq str (if (= (length str) 0)
1379 comp-word 1455 math-comp-word
1380 (concat str comp-word)) 1456 (concat str math-comp-word))
1381 comp-word "") 1457 math-comp-word "")
1382 (while (< (nth 1 c) (aref math-comp-buf-level comp-level)) 1458 (while (< (nth 1 c) (aref math-comp-buf-level math-comp-level))
1383 (setq comp-level (1- comp-level)) 1459 (setq math-comp-level (1- math-comp-level))
1384 (or (= (length (aref math-comp-buf-string comp-level)) 0) 1460 (or (= (length (aref math-comp-buf-string math-comp-level)) 0)
1385 (setq str (concat (aref math-comp-buf-string comp-level) 1461 (setq str (concat (aref math-comp-buf-string math-comp-level)
1386 str)))) 1462 str))))
1387 (aset math-comp-buf-string comp-level str))))) 1463 (aset math-comp-buf-string math-comp-level str)))))
1388 1464
1389 ((eq (car c) 'tag) 1465 ((eq (car c) 'tag)
1390 (cond ((eq (nth 1 c) math-comp-selected) 1466 (cond ((eq (nth 1 c) math-comp-selected)
1391 (let ((comp-highlight (not calc-show-selections))) 1467 (let ((math-comp-highlight (not calc-show-selections)))
1392 (math-comp-to-string-flat-term (nth 2 c)))) 1468 (math-comp-to-string-flat-term (nth 2 c))))
1393 ((eq (nth 1 c) t) 1469 ((eq (nth 1 c) t)
1394 (let ((comp-highlight nil)) 1470 (let ((math-comp-highlight nil))
1395 (math-comp-to-string-flat-term (nth 2 c)))) 1471 (math-comp-to-string-flat-term (nth 2 c))))
1396 (t (math-comp-to-string-flat-term (nth 2 c))))) 1472 (t (math-comp-to-string-flat-term (nth 2 c)))))
1397 1473
1398 (t (math-comp-to-string-flat-term (nth 2 c))))) 1474 (t (math-comp-to-string-flat-term (nth 2 c)))))
1399 1475
1403 (while (>= (setq i (1- i)) 0) 1479 (while (>= (setq i (1- i)) 0)
1404 (or (memq (aref s i) '(32 ?\n)) 1480 (or (memq (aref s i) '(32 ?\n))
1405 (aset s i (if calc-show-selections ?\. ?\#))))) 1481 (aset s i (if calc-show-selections ?\. ?\#)))))
1406 s) 1482 s)
1407 1483
1484
1485 ;; The variable math-comp-sel-tag is local to calc-find-selected-part
1486 ;; in calc-sel.el, but is used by math-comp-sel-flat-term and
1487 ;; math-comp-add-string-sel, which are called (indirectly) by
1488 ;; calc-find-selected-part.
1489 (defvar math-comp-sel-tag)
1490
1408 (defun math-comp-sel-flat-term (c) 1491 (defun math-comp-sel-flat-term (c)
1409 (cond ((not (consp c)) 1492 (cond ((not (consp c))
1410 (setq comp-pos (+ comp-pos (length c)))) 1493 (setq math-comp-pos (+ math-comp-pos (length c))))
1411 ((memq (car c) '(set break))) 1494 ((memq (car c) '(set break)))
1412 ((eq (car c) 'horiz) 1495 ((eq (car c) 'horiz)
1413 (while (and (setq c (cdr c)) (< math-comp-sel-cpos 1000000)) 1496 (while (and (setq c (cdr c)) (< math-comp-sel-cpos 1000000))
1414 (math-comp-sel-flat-term (car c)))) 1497 (math-comp-sel-flat-term (car c))))
1415 ((eq (car c) 'tag) 1498 ((eq (car c) 'tag)
1416 (if (<= comp-pos math-comp-sel-cpos) 1499 (if (<= math-comp-pos math-comp-sel-cpos)
1417 (progn 1500 (progn
1418 (math-comp-sel-flat-term (nth 2 c)) 1501 (math-comp-sel-flat-term (nth 2 c))
1419 (if (> comp-pos math-comp-sel-cpos) 1502 (if (> math-comp-pos math-comp-sel-cpos)
1420 (setq math-comp-sel-tag c 1503 (setq math-comp-sel-tag c
1421 math-comp-sel-cpos 1000000))) 1504 math-comp-sel-cpos 1000000)))
1422 (math-comp-sel-flat-term (nth 2 c)))) 1505 (math-comp-sel-flat-term (nth 2 c))))
1423 (t (math-comp-sel-flat-term (nth 2 c))))) 1506 (t (math-comp-sel-flat-term (nth 2 c)))))
1424 1507
1425 1508
1426 ;;; Simplify a composition to a canonical form consisting of 1509 ;;; Simplify a composition to a canonical form consisting of
1427 ;;; (vleft n "string" "string" "string" ...) 1510 ;;; (vleft n "string" "string" "string" ...)
1428 ;;; where 0 <= n < number-of-strings. 1511 ;;; where 0 <= n < number-of-strings.
1429 1512
1513 ;; The variables math-comp-base, math-comp-hgt, math-comp-tag,
1514 ;; math-comp-hpos and math-comp-vpos are local to math-comp-simplify,
1515 ;; but are used by math-comp-add-string (math-comp-base, math-comp-hgt),
1516 ;; math-comp-add-string-sel (math-comp-tag) and math-comp-simplify-term
1517 ;; (math-comp-tag, math-comp-vpos, math-comp-hpos), which are called by
1518 ;; math-comp-simplify.
1519 (defvar math-comp-base)
1520 (defvar math-comp-hgt)
1521 (defvar math-comp-tag)
1522 (defvar math-comp-hpos)
1523 (defvar math-comp-vpos)
1524
1430 (defun math-comp-simplify (c full-width) 1525 (defun math-comp-simplify (c full-width)
1431 (let ((comp-buf (list "")) 1526 (let ((math-comp-buf (list ""))
1432 (comp-base 0) 1527 (math-comp-base 0)
1433 (comp-height 1) 1528 (math-comp-hgt 1)
1434 (comp-hpos 0) 1529 (math-comp-hpos 0)
1435 (comp-vpos 0) 1530 (math-comp-vpos 0)
1436 (comp-highlight (and math-comp-selected calc-show-selections)) 1531 (math-comp-highlight (and math-comp-selected calc-show-selections))
1437 (comp-tag nil)) 1532 (math-comp-tag nil))
1438 (math-comp-simplify-term c) 1533 (math-comp-simplify-term c)
1439 (cons 'vleft (cons comp-base comp-buf)))) 1534 (cons 'vleft (cons math-comp-base math-comp-buf))))
1440 1535
1441 (defun math-comp-add-string (s h v) 1536 (defun math-comp-add-string (s h v)
1442 (and (> (length s) 0) 1537 (and (> (length s) 0)
1443 (let ((vv (+ v comp-base))) 1538 (let ((vv (+ v math-comp-base)))
1444 (if math-comp-sel-hpos 1539 (if math-comp-sel-hpos
1445 (math-comp-add-string-sel h vv (length s) 1) 1540 (math-comp-add-string-sel h vv (length s) 1)
1446 (if (< vv 0) 1541 (if (< vv 0)
1447 (setq comp-buf (nconc (make-list (- vv) "") comp-buf) 1542 (setq math-comp-buf (nconc (make-list (- vv) "") math-comp-buf)
1448 comp-base (- v) 1543 math-comp-base (- v)
1449 comp-height (- comp-height vv) 1544 math-comp-hgt (- math-comp-hgt vv)
1450 vv 0) 1545 vv 0)
1451 (if (>= vv comp-height) 1546 (if (>= vv math-comp-hgt)
1452 (setq comp-buf (nconc comp-buf 1547 (setq math-comp-buf (nconc math-comp-buf
1453 (make-list (1+ (- vv comp-height)) "")) 1548 (make-list (1+ (- vv math-comp-hgt)) ""))
1454 comp-height (1+ vv)))) 1549 math-comp-hgt (1+ vv))))
1455 (let ((str (nthcdr vv comp-buf))) 1550 (let ((str (nthcdr vv math-comp-buf)))
1456 (setcar str (concat (car str) 1551 (setcar str (concat (car str)
1457 (make-string (- h (length (car str))) 32) 1552 (make-string (- h (length (car str))) 32)
1458 (if comp-highlight 1553 (if math-comp-highlight
1459 (math-comp-highlight-string s) 1554 (math-comp-highlight-string s)
1460 s)))))))) 1555 s))))))))
1461 1556
1462 (defun math-comp-add-string-sel (x y w h) 1557 (defun math-comp-add-string-sel (x y w h)
1463 (if (and (<= y math-comp-sel-vpos) 1558 (if (and (<= y math-comp-sel-vpos)
1464 (> (+ y h) math-comp-sel-vpos) 1559 (> (+ y h) math-comp-sel-vpos)
1465 (<= x math-comp-sel-hpos) 1560 (<= x math-comp-sel-hpos)
1466 (> (+ x w) math-comp-sel-hpos)) 1561 (> (+ x w) math-comp-sel-hpos))
1467 (setq math-comp-sel-tag comp-tag 1562 (setq math-comp-sel-tag math-comp-tag
1468 math-comp-sel-vpos 10000))) 1563 math-comp-sel-vpos 10000)))
1469 1564
1470 (defun math-comp-simplify-term (c) 1565 (defun math-comp-simplify-term (c)
1471 (cond ((stringp c) 1566 (cond ((stringp c)
1472 (math-comp-add-string c comp-hpos comp-vpos) 1567 (math-comp-add-string c math-comp-hpos math-comp-vpos)
1473 (setq comp-hpos (+ comp-hpos (length c)))) 1568 (setq math-comp-hpos (+ math-comp-hpos (length c))))
1474 ((memq (car c) '(set break)) 1569 ((memq (car c) '(set break))
1475 nil) 1570 nil)
1476 ((eq (car c) 'horiz) 1571 ((eq (car c) 'horiz)
1477 (while (setq c (cdr c)) 1572 (while (setq c (cdr c))
1478 (math-comp-simplify-term (car c)))) 1573 (math-comp-simplify-term (car c))))
1479 ((memq (car c) '(vleft vcent vright)) 1574 ((memq (car c) '(vleft vcent vright))
1480 (let* ((comp-vpos (+ (- comp-vpos (nth 1 c)) 1575 (let* ((math-comp-vpos (+ (- math-comp-vpos (nth 1 c))
1481 (1- (math-comp-ascent (nth 2 c))))) 1576 (1- (math-comp-ascent (nth 2 c)))))
1482 (widths (mapcar 'math-comp-width (cdr (cdr c)))) 1577 (widths (mapcar 'math-comp-width (cdr (cdr c))))
1483 (maxwid (apply 'max widths)) 1578 (maxwid (apply 'max widths))
1484 (bias (cond ((eq (car c) 'vleft) 0) 1579 (bias (cond ((eq (car c) 'vleft) 0)
1485 ((eq (car c) 'vcent) 1) 1580 ((eq (car c) 'vcent) 1)
1486 (t 2)))) 1581 (t 2))))
1487 (setq c (cdr c)) 1582 (setq c (cdr c))
1488 (while (setq c (cdr c)) 1583 (while (setq c (cdr c))
1489 (if (eq (car-safe (car c)) 'rule) 1584 (if (eq (car-safe (car c)) 'rule)
1490 (math-comp-add-string (make-string maxwid (nth 1 (car c))) 1585 (math-comp-add-string (make-string maxwid (nth 1 (car c)))
1491 comp-hpos comp-vpos) 1586 math-comp-hpos math-comp-vpos)
1492 (let ((comp-hpos (+ comp-hpos (/ (* bias (- maxwid 1587 (let ((math-comp-hpos (+ math-comp-hpos (/ (* bias (- maxwid
1493 (car widths))) 1588 (car widths)))
1494 2)))) 1589 2))))
1495 (math-comp-simplify-term (car c)))) 1590 (math-comp-simplify-term (car c))))
1496 (and (cdr c) 1591 (and (cdr c)
1497 (setq comp-vpos (+ comp-vpos 1592 (setq math-comp-vpos (+ math-comp-vpos
1498 (+ (math-comp-descent (car c)) 1593 (+ (math-comp-descent (car c))
1499 (math-comp-ascent (nth 1 c)))) 1594 (math-comp-ascent (nth 1 c))))
1500 widths (cdr widths)))) 1595 widths (cdr widths))))
1501 (setq comp-hpos (+ comp-hpos maxwid)))) 1596 (setq math-comp-hpos (+ math-comp-hpos maxwid))))
1502 ((eq (car c) 'supscr) 1597 ((eq (car c) 'supscr)
1503 (let* ((asc (or 1 (math-comp-ascent (nth 1 c)))) 1598 (let* ((asc (or 1 (math-comp-ascent (nth 1 c))))
1504 (desc (math-comp-descent (nth 2 c))) 1599 (desc (math-comp-descent (nth 2 c)))
1505 (oldh (prog1 1600 (oldh (prog1
1506 comp-hpos 1601 math-comp-hpos
1507 (math-comp-simplify-term (nth 1 c)))) 1602 (math-comp-simplify-term (nth 1 c))))
1508 (comp-vpos (- comp-vpos (+ asc desc)))) 1603 (math-comp-vpos (- math-comp-vpos (+ asc desc))))
1509 (math-comp-simplify-term (nth 2 c)) 1604 (math-comp-simplify-term (nth 2 c))
1510 (if math-comp-sel-hpos 1605 (if math-comp-sel-hpos
1511 (math-comp-add-string-sel oldh 1606 (math-comp-add-string-sel oldh
1512 (- comp-vpos 1607 (- math-comp-vpos
1513 -1 1608 -1
1514 (math-comp-ascent (nth 2 c))) 1609 (math-comp-ascent (nth 2 c)))
1515 (- comp-hpos oldh) 1610 (- math-comp-hpos oldh)
1516 (math-comp-height c))))) 1611 (math-comp-height c)))))
1517 ((eq (car c) 'subscr) 1612 ((eq (car c) 'subscr)
1518 (let* ((asc (math-comp-ascent (nth 2 c))) 1613 (let* ((asc (math-comp-ascent (nth 2 c)))
1519 (desc (math-comp-descent (nth 1 c))) 1614 (desc (math-comp-descent (nth 1 c)))
1520 (oldv comp-vpos) 1615 (oldv math-comp-vpos)
1521 (oldh (prog1 1616 (oldh (prog1
1522 comp-hpos 1617 math-comp-hpos
1523 (math-comp-simplify-term (nth 1 c)))) 1618 (math-comp-simplify-term (nth 1 c))))
1524 (comp-vpos (+ comp-vpos (+ asc desc)))) 1619 (math-comp-vpos (+ math-comp-vpos (+ asc desc))))
1525 (math-comp-simplify-term (nth 2 c)) 1620 (math-comp-simplify-term (nth 2 c))
1526 (if math-comp-sel-hpos 1621 (if math-comp-sel-hpos
1527 (math-comp-add-string-sel oldh oldv 1622 (math-comp-add-string-sel oldh oldv
1528 (- comp-hpos oldh) 1623 (- math-comp-hpos oldh)
1529 (math-comp-height c))))) 1624 (math-comp-height c)))))
1530 ((eq (car c) 'tag) 1625 ((eq (car c) 'tag)
1531 (cond ((eq (nth 1 c) math-comp-selected) 1626 (cond ((eq (nth 1 c) math-comp-selected)
1532 (let ((comp-highlight (not calc-show-selections))) 1627 (let ((math-comp-highlight (not calc-show-selections)))
1533 (math-comp-simplify-term (nth 2 c)))) 1628 (math-comp-simplify-term (nth 2 c))))
1534 ((eq (nth 1 c) t) 1629 ((eq (nth 1 c) t)
1535 (let ((comp-highlight nil)) 1630 (let ((math-comp-highlight nil))
1536 (math-comp-simplify-term (nth 2 c)))) 1631 (math-comp-simplify-term (nth 2 c))))
1537 (t (let ((comp-tag c)) 1632 (t (let ((math-comp-tag c))
1538 (math-comp-simplify-term (nth 2 c)))))))) 1633 (math-comp-simplify-term (nth 2 c))))))))
1539 1634
1540 1635
1541 ;;; Measuring a composition. 1636 ;;; Measuring a composition.
1542 1637
1705 (make-string indent 32) 1800 (make-string indent 32)
1706 (math-comp-to-string-raw (car cl) indent) 1801 (math-comp-to-string-raw (car cl) indent)
1707 (math-comp-to-string-raw-step (cdr cl) indent)) 1802 (math-comp-to-string-raw-step (cdr cl) indent))
1708 "")) 1803 ""))
1709 1804
1805 (provide 'calccomp)
1806
1807 ;;; arch-tag: 7c45d10a-a286-4dab-af49-7ae8989fbf78
1710 ;;; calccomp.el ends here 1808 ;;; calccomp.el ends here