Print a warning when translating subscripted functions
[maxima.git] / src / displa.lisp
blob6e6582ad94d90a203c4862c7387588a8a675d32c
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module displa)
15 ;; N.B. You must read the macro file before reading this file.
17 (load-macsyma-macros displm)
19 (declare-top (special $linel))
21 (defmvar $ttyoff nil)
23 (defmvar $stardisp nil
24 "Causes factors of products to be separated by * when displayed.")
26 (defmvar $leftjust nil
27 "Causes equations to be drawn left justified rather than centered.
28 For slow consoles.")
30 (defmvar $display2d t
31 "Causes equations to be drawn in two dimensions. Otherwise, drawn
32 linearly.")
34 (defmvar $lispdisp nil
35 "Causes symbols not having $ as the first character in their pnames
36 to be preceded with a ? when displayed.")
38 (defmvar $derivabbrev nil)
40 (defmvar $noundisp nil)
42 (defmvar $stringdisp nil
43 "Causes strings to be bracketed in double quotes when displayed.
44 Normally this is off, but is turned on when a procedure definition is
45 being displayed.")
47 (defmvar displayp nil "Is T when inside of `displa'")
49 ;; Parameters which control how boxes, absolute value signs,
50 ;; evaluation-at-a-point bars, and matrices are drawn.
52 (defmvar $boxchar "\"" "Character used for drawing boxes.")
53 (defmvar $absboxchar "!" "Character used for drawing absolute value signs and 'evaluation at' signs.")
54 (defmvar $lmxchar "[" "Character used for drawing the left edge of a matrix.")
55 (defmvar $rmxchar "]" "Character used for drawing the right edge of a matrix.")
57 ;; These variables are bound within Macsyma Listeners since they are different
58 ;; for each window. Set them here, anyway, so that RETRIEVE can be called from
59 ;; top level. The size of TOP-WINDOW is wired in here.
61 (defmvar $linel 79.)
62 (defvar linel 79.)
63 (defvar ttyheight 24.)
65 (defvar linearray (make-array 80. :initial-element nil))
67 (defun maxima-display (form &key (stream *standard-output*) )
68 (let ((*standard-output* stream))
69 (displa form)))
71 (defvar *alt-display2d* nil)
72 (defvar *alt-display1d* nil)
74 (defun displa (form &aux #+kcl(form form))
75 (if (not $ttyoff)
76 (cond ($display2d
77 (cond (*alt-display2d* (apply *alt-display2d* form ()))
79 (let ((displayp t)
80 (linearray (if displayp
81 (make-array 80. :initial-element nil)
82 linearray))
83 (mratp (checkrat form))
84 (maxht 1) (maxdp 0) (width 0)
85 (height 0) (depth 0) (level 0) (size 2)
86 (break 0) (right 0) (lines 1) bkpt
87 (bkptwd 0) (bkptht 1) (bkptdp 0) (bkptout 0)
88 (bkptlevel 0) in-p)
89 (unwind-protect
90 (progn
91 (setq form (dimension form nil 'mparen 'mparen 0 0))
92 (checkbreak form width)
93 (output form (if (and (not $leftjust) (= 2 lines))
94 (- linel (- width bkptout))
95 0)))
96 ;; make sure the linearray gets cleared out.
97 (fill linearray nil))))))
99 (if *alt-display1d*
100 (apply *alt-display1d* form ())
101 (linear-displa form))))))
103 (defmspec $with_default_2d_display (expr)
104 (let ((*alt-display2d* nil)
105 ($display2d t)
106 (mexpr (cons '(mprog) (cdr expr))))
107 (meval mexpr)))
109 ;;; (LINEAR-DISPLA <thing-to-display>)
111 ;;; Display text linearly. This function should be usable in any case
112 ;;; DISPLA is usable and will attempt to do something reasonable with
113 ;;; its input.
115 (defun linear-displa (form)
116 (fresh-line *standard-output*)
117 (mgrind form *standard-output*)
118 (mterpri))
120 (defmvar $display_format_internal nil
121 "Setting this TRUE can help give the user a greater understanding
122 of the behavior of maxima on certain of his problems,
123 especially those involving roots and quotients")
125 (defun nformat-check (form)
126 (if (and $display_format_internal
127 (not (or (atom form) (atom (car form)) (specrepp form))))
128 form
129 (nformat form)))
131 (defun dimension (form result lop rop w right)
132 (let ((level (1+ level))
133 (break (if (and w break) (+ w break))))
134 (setq form (nformat-check form))
135 (cond ((atom form)
136 (dimension-atom form result))
137 ((and (atom (car form)) (setq form (cons '(mprogn) form)) nil))
138 ((or (<= (lbp (caar form)) (rbp lop)) (>= (lbp rop) (rbp (caar form))))
139 (dimension-paren form result))
140 ((member 'array (car form) :test #'eq) (dimension-array form result))
141 ((safe-get (caar form) 'dimension)
142 (funcall (get (caar form) 'dimension) form result))
143 (t (dimension-function form result)))))
145 (defvar atom-context 'dimension-list)
146 ;; bound by DIMENSION-ARRAY and DIMENSION-FUNCTION.
147 ;; This ATOM-CONTEXT put in by GJC so that MCW could have a clean
148 ;; hook by which to write his extensions for vector-underbars.
150 ;; Referenced externally by RAT;FLOAT.
152 (defun dimension-atom (form result)
153 (cond ((and (symbolp form) (get form atom-context))
154 (funcall (get form atom-context) form result))
155 ((stringp form) (dimension-string (makestring form) result))
156 ((member (marray-type form) '(array hash-table $functional))
157 (dimension-array-object form result))
158 (t (dimension-string (makestring form) result))))
160 ;; Referenced externally by anyone who wants to display something as
161 ;; a funny looking atom, e.g. Trace, Mformat.
163 (defun dimension-string (dummy result &aux crp)
164 (setq width 0 height 1 depth 0)
165 (do ((l dummy (cdr l)))
166 ((null l))
167 (incf width)
168 (if (char= (car l) #\newline) (setq crp t)))
169 (cond ((or (and (checkfit width) (not crp)) (not break))
170 (nreconc dummy result))
171 (t (setq width 0)
172 (do ((l dummy) (w (- linel (- break bkptout))))
173 ((null l) (checkbreak result width) result)
174 (setq dummy l l (cdr l))
175 (cond ((char= (car dummy) #\newline)
176 (forcebreak result width)
177 (setq result nil w (+ linel width)))
178 (t (incf width)
179 (when (and (= w width) l)
180 (forcebreak (cons #\\ result) width)
181 (setq result nil w (+ linel width))
182 (incf width))
183 (setq result (rplacd dummy result))))))))
185 (defun makestring (atom)
186 (let (dummy)
187 (cond ((numberp atom) (exploden atom))
188 ((stringp atom)
189 (setq dummy (coerce atom 'list))
190 (if $stringdisp
191 (cons #\" (nconc dummy (list #\")))
192 dummy))
193 ((not (symbolp atom)) (exploden atom))
194 ((and (setq dummy (get atom 'reversealias))
195 (not (and (member atom $aliases :test #'eq) (get atom 'noun))))
196 (exploden (stripdollar dummy)))
197 ((not (eq (getop atom) atom))
198 (makestring (getop atom)))
199 (t (setq dummy (exploden atom))
200 (cond
201 ((null dummy) nil)
202 ((char= #\$ (car dummy)) (cdr dummy))
203 ((char= #\% (car dummy)) (cdr dummy))
204 ($lispdisp (cons #\? dummy))
205 (t dummy))))))
207 (defun dimension-paren (form result)
208 (setq result (cons #\) (dimension form (cons #\( result) 'mparen 'mparen 1 (1+ right))))
209 (incf width 2)
210 result)
212 (defun dimension-array (x result)
213 (prog (dummy bas (w-base 0) (h-base 0) (d-base 0))
214 (if (eq (caar x) 'mqapply)
215 (setq dummy (cadr x) x (cdr x))
216 (setq dummy (caar x)))
217 (cond ((or (not $noundisp) (not (symbolp (caar x)))))
218 ((and (get (caar x) 'verb) (get (caar x) 'alias))
219 (push-string "''" result) (setq w-base 2))
220 ((and (get (caar x) 'noun) (not (member (caar x) (cdr $aliases) :test #'eq))
221 (not (get (caar x) 'reversealias)))
222 (setq result (cons #\' result) w-base 1)))
223 (setq
224 bas (if (and (not (atom dummy)) (member 'array (car dummy) :test #'eq))
225 (let ((break nil) (right 0)) (dimension-paren dummy nil))
226 (let ((atom-context 'dimension-array))
227 (dimension dummy nil lop 'mfunction nil 0)))
228 w-base (+ w-base width)
229 h-base height
230 d-base depth)
231 (multiple-value-bind (w00 w01 w10 w11 h00 h01 h10 h11 d00 d01 d10 d11 pre-subscripts-output pre-superscripts-output post-subscripts-output post-superscripts-output)
232 (dimension-indices dummy (cdr x))
233 (declare (special pre-subscripts pre-superscripts post-subscripts post-superscripts))
234 (cond
235 ((not (checkfit (setq width (+ w-base (max w00 w01) (max w10 w11))))) ;; ?? !!
236 (return (dimension-function (cons '(subscript) (cons dummy (cdr x))) result)))
237 ((and (atom (car bas)) (char= #\ (car bas))) ; Adding the test (atom (car bas))
238 (setq result (cons (cons 0 (cons (- h-base) post-subscripts)) bas) depth (max (+ h-base d-base) depth))) ;; ?? !!
240 (setq result
241 (append
242 (if (and (> w11 0) (> w10 w11)) (list (list (- w10 w11) 0)))
243 (remove-if #'(lambda (l) (= (length l) 2))
244 (list
245 (append (list (- w10) (+ h-base d11)) post-superscripts-output)
246 (append (list 0 (- h10)) post-subscripts-output)
247 (append (list 0 0) bas)
248 (append (list (if (> w00 0) (- w01) 0) (+ h-base d01)) pre-superscripts-output)
249 (append (list (if (> w01 w00) (- w01 w00) 0) (- h00)) pre-subscripts-output)))
250 result)
251 height (+ h-base (max (+ d01 h01) (+ d11 h11)))
252 depth (+ d-base (max (+ d00 h00) (+ d10 h10))))))
253 (update-heights height depth)
254 (return result))))
256 (defmvar $known_index_properties '((mlist) $presubscript $presuperscript $postsubscript $postsuperscript))
258 (defun dimension-indices (base-symbol indices)
259 (let
260 ((display-indices (safe-mget base-symbol 'display-indices))
261 (separator (let ((x (safe-$get base-symbol '$display_index_separator)))
262 (if (or (null x) (stringp x)) x (coerce (mstring x) 'string)))))
263 (if (and (> (length display-indices) 0) (not (= (length display-indices) (length indices))))
264 ;; Ignore DISPLAY-INDICES if it's nonempty and not the same size as INDICES.
265 (setq display-indices nil))
266 (let (pre-subscripts pre-superscripts post-subscripts post-superscripts)
267 (declare (special pre-subscripts pre-superscripts post-subscripts post-superscripts))
268 (if display-indices
269 (setq pre-subscripts (extract-indices indices display-indices '$presubscript)
270 pre-superscripts (extract-indices indices display-indices '$presuperscript)
271 post-subscripts (extract-indices indices display-indices '$postsubscript)
272 post-superscripts (extract-indices indices display-indices '$postsuperscript))
273 (setq pre-subscripts nil
274 pre-superscripts nil
275 post-subscripts indices
276 post-superscripts nil))
277 (let (pre-subscripts-output pre-superscripts-output post-subscripts-output post-superscripts-output
278 (w00 0) (w01 0) (w10 0) (w11 0) (h00 0) (h01 0) (h10 0) (h11 0) (d00 0) (d01 0) (d10 0) (d11 0))
279 (if pre-subscripts
280 (setq pre-subscripts-output
281 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
282 (dimension-list (cons '(mlist) pre-subscripts) nil separator))
283 w00 width
284 h00 height
285 d00 depth))
286 (if pre-superscripts
287 (setq pre-superscripts-output
288 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
289 (dimension-list (cons '(mlist) pre-superscripts) nil separator))
290 w01 width
291 h01 height
292 d01 depth))
293 (if post-subscripts
294 (setq post-subscripts-output
295 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
296 (dimension-list (cons '(mlist) post-subscripts) nil separator))
297 w10 width
298 h10 height
299 d10 depth))
300 (if post-superscripts
301 (setq post-superscripts-output
302 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
303 (dimension-list (cons '(mlist) post-superscripts) nil separator))
304 w11 width
305 h11 height
306 d11 depth))
307 (values w00 w01 w10 w11 h00 h01 h10 h11 d00 d01 d10 d11 pre-subscripts-output pre-superscripts-output post-subscripts-output post-superscripts-output)))))
309 (defun extract-indices (indices display-indices index-flag)
310 (remove nil
311 (loop for i from 0 to (length display-indices)
312 collect (if (eq (nth i display-indices) index-flag) (nth i indices)))))
314 (defun safe-$get (x y)
315 (and (symbolp x) ($get x y)))
317 (defun dimension-function (x result)
318 (prog (fun (w 0) (h 0) (d 0))
319 (cond ((or (not $noundisp) (not (symbolp (caar x)))))
320 ((and (get (caar x) 'verb) (get (caar x) 'alias))
321 (push-string "''" result) (setq w 2))
322 ((and (get (caar x) 'noun) (not (member (caar x) (cdr $aliases) :test #'eq))
323 (not (get (caar x) 'reversealias)))
324 (setq result (cons #\' result) w 1)))
325 (if (eq (caar x) 'mqapply) (setq fun (cadr x) x (cdr x)) (setq fun (caar x)))
326 (setq result (let ((atom-context 'dimension-function))
327 (dimension fun result lop 'mparen 0 1))
328 w (+ w width) h height d depth)
329 (cond ((null (cdr x))
330 (setq result (list* #\) #\( result)
331 width (+ 2 w)))
332 (t (setq result (let ((lop 'mparen)
333 (rop 'mparen)
334 (break (if break (+ 1 w break))))
335 (cons #\) (dimension-list x (cons #\( result))))
336 width (+ 2 w width) height (max h height) depth (max d depth))))
337 (return result)))
339 (defun dimension-prefix (form result)
340 (prog (dissym (symlength 0))
341 (setq dissym (safe-get (caar form) 'dissym)
342 symlength (length dissym))
343 (setq result (dimension (cadr form) (revappend dissym result) (caar form) rop symlength right)
344 width (+ symlength width))
345 (return result)))
347 (defun dimension-list (form result &optional separator)
348 (prog ((w 0) (h 0) (d 0))
349 (setq result (dimension (cadr form) result lop 'mcomma 0 right)
350 w width h height d depth)
351 (do ((l (cddr form) (cdr l)))
352 ((null l))
353 (push-string (or separator ", ") result)
354 (incf w (if separator (length separator) 2))
355 (checkbreak result w)
356 (setq result (dimension (car l) result 'mcomma 'mcomma w right)
357 w (+ w width) h (max h height) d (max d depth)))
358 (setq width w height h depth d)
359 (return result)))
361 (defun dimension-infix (form result)
362 (unless (= (length (cdr form)) 2)
363 (return-from dimension-infix (dimension-function form result)))
364 (prog (dissym (symlength 0) (w 0) (h 0) (d 0))
365 (setq dissym (safe-get (caar form) 'dissym)
366 symlength (length dissym)
367 result (dimension (cadr form) result lop (caar form) 0 symlength)
368 w width
369 h height
370 d depth)
371 (setq result (revappend dissym result))
372 (checkbreak result (+ symlength w))
373 (setq result (dimension (caddr form) result (caar form) rop (+ symlength w) right)
374 width (+ w symlength width)
375 height (max h height)
376 depth (max d depth))
377 (return result)))
379 (defun dimension-nary (form result)
380 ;; If only 0 or 1 arguments, then print "*"() or "*"(A)
381 (cond ((null (cddr form))
382 (dimension-function form result))
384 (prog (dissym (symlength 0) (w 0) (h 0) (d 0) helper)
385 (setq dissym (safe-get (caar form) 'dissym)
386 symlength (length dissym)
388 ;; Look for a helper function. Fall back on default if none found.
389 helper (or (safe-get (caar form) 'dimension-nary-helper) 'dimnary)
391 result (funcall helper (cadr form) result lop (caar form) (caar form) 0)
392 w width
393 h height
394 d depth
396 (do ((l (cddr form) (cdr l)))
397 (nil)
398 (checkbreak result w)
399 (setq result (revappend dissym result) w (+ symlength w))
400 (cond ((null (cdr l))
401 (setq result (funcall helper (car l) result (caar form) (caar form) rop w)
402 width (+ w width)
403 height (max h height)
404 depth (max d depth))
405 (return t))
407 (setq result (funcall helper (car l) result (caar form) (caar form) (caar form) w)
408 w (+ w width)
409 h (max h height)
410 d (max d depth)))))
411 (return result)))))
413 ;; Output for MTEXT, generated for example by print(...).
414 ;; Suppress parentheses which would be generated by DIMNARY.
416 (defun dimnary-mtext (form result lop op rop w)
417 (declare (ignore op))
418 (dimension form result lop rop w right))
420 ;; Output for general n-ary operators.
421 ;; Heuristic: if some argument is displayed as an infix or n-ary operator,
422 ;; then put parentheses around that argument (even if parentheses are not
423 ;; necessary, according to operator precedence, to disambiguate the output).
425 ;; Examples:
427 ;; a * b * c; --> a b c
428 ;; a . b . c; --> a . b . c
429 ;; a * b . c; --> a (b . c)
430 ;; (a * b) . c; --> (a b) . c
431 ;; a . b * c; --> (a . b) c
432 ;; a . (b * c); --> a . (b c)
433 ;; a . b * c . d; --> (a . b) (c . d)
434 ;; a * b . c * d; --> a (b . c) d
435 ;; (a * b) . (c * d); --> (a b) . (c d)
436 ;; infix ("@@");
437 ;; a @@ b * c @@ d; --> (a @@ b) (c @@ d)
438 ;; a @@ b . c @@ d; --> (a @@ b) . (c @@ d)
439 ;; a * b @@ c . d; --> a ((b @@ c) . d)
440 ;; (a * b) @@ (c . d); --> (a b) @@ (c . d)
442 (defun dimnary (form result lop op rop w)
443 (declare (ignore op))
444 (if (and (consp form)
445 (member (safe-get (caar form) 'dimension)
446 '(dimension-infix dimension-nary)))
447 (progn
448 (setq result
449 (cons #\)
450 (dimension form
451 (cons #\( result)
452 'mparen 'mparen (if w (1+ w)) (1+ right))))
453 (incf width 2)
454 result)
455 (dimension form result lop rop w right)))
457 ;; Output for Boolean n-ary operators.
458 ;; Heuristic: if some argument is displayed as an infix or n-ary operator,
459 ;; or the operator of the argument is MNOT, then put parentheses around that argument
460 ;; (even if parentheses are not necessary, according to operator precedence,
461 ;; to disambiguate the output).
463 ;; Examples:
465 ;; a and b and c; --> a and b and c
466 ;; a or b or c; --> a or b or c
467 ;; a or b and c; --> a or (b and c)
468 ;; (a or b) and c; --> (a or b) and c
469 ;; a and b or c; --> (a and b) or c
470 ;; a and (b or c); --> a and (b or c)
471 ;; a and b or c and d; --> (a and b) or (c and d)
472 ;; a or b and c or d; --> a or (b and c) or d
473 ;; (a or b) and (c or d); --> (a or b) and (c or d)
474 ;; a < 0 and b < 0 or c < 0; --> ((a < 0) and (b < 0)) or (c < 0)
475 ;; a < 0 and not foo; --> (a < 0) and (not foo)
476 ;; a < 0 or not foo; --> (a < 0) or (not foo)
477 ;; infix ("=>");
478 ;; a => b and a => c; --> (a => b) and (a => c)
479 ;; a => b or not b => c; --> (a => b) or (not b => c)
480 ;; a and b => c and d; --> a and (b => c) and d
481 ;; a or b => c and d; --> a or ((b => c) and d)
482 ;; (a and b) => (c and d); --> (a and b) => (c and d)
483 ;; if a and b then 1 else 0; --> if a and b then 1 else 0
485 (defun dimnary-boolean (form result lop op rop w)
486 (declare (ignore op))
487 (if (and (consp form)
488 (or (member (safe-get (caar form) 'dimension) '(dimension-infix dimension-nary))
489 (eq (caar form) 'mnot)))
490 (dimension-paren form result)
491 (dimension form result lop rop w right)))
493 (defun dimension-postfix (form result)
494 (prog (dissym (symlength 0))
495 (setq dissym (safe-get (caar form) 'dissym)
496 symlength (length dissym))
497 (setq result (dimension (cadr form) result lop (caar form) 0 (+ symlength right))
498 width (+ symlength width))
499 (return (revappend dissym result))))
501 (defun dimension-nofix (form result)
502 (setq form (safe-get (caar form) 'dissym)
503 width (length form))
504 (revappend form result))
506 (defun dimension-match (form result)
507 (prog (dissym (symlength 0))
508 (setq dissym (safe-get (caar form) 'dissym)
509 symlength (length (car dissym)))
510 (cond ((null (cdr form))
511 (setq width (+ symlength (length (cdr dissym)))
512 height 1
513 depth 0)
514 (return (revappend (cdr dissym) (revappend (car dissym) result))))
515 (t (setq result (let ((lop 'mparen)
516 (rop 'mparen)
517 (break (if break (+ symlength break)))
518 (right (+ symlength right)))
519 (dimension-list form (revappend (car dissym) result))))
520 (setq width (+ (length (cdr dissym)) symlength width))
521 (return (revappend (cdr dissym) result))))))
523 (defun dimension-superscript (form result)
524 (prog (exp (w 0) (h 0) (d 0) bas)
525 (setq exp (let ((size 1))
526 (dimension (caddr form) nil 'mparen 'mparen nil 0))
527 w width
528 h height
529 d depth)
530 (cond ((and (not (atom (cadr form))) (member 'array (cdaadr form) :test #'eq))
531 (if (safe-mget (caaadr form) 'display-indices)
532 (return (dimension-superscript `(,(first form) ((mprogn) ,(second form)) ,(third form)) result))
533 (progn (prog (sub (w2 0) (h2 0) (d2 0))
534 (setq sub (if (eq 'mqapply (caaadr form))
535 (cdadr form) (cadr form)))
536 (setq sub (let ((lop 'mparen) (break nil) (size 1))
537 (dimension-list sub nil))
538 w2 width h2 height d2 depth)
539 (setq bas (dimension (mop (cadr form)) result lop 'mexpt nil 0))
540 (when (not (checkfit (+ width (max w w2))))
541 (setq result (dimension-function (cons '($expt) (cdr form)) result))
542 (return result))
543 (setq result (cons (cons 0 (cons (+ height d) exp)) bas))
544 (setq result (cons (cons (- w) (cons (- (+ depth h2)) sub)) result))
545 (setq result (cons (list (- (max w w2) w2) 0) result)
546 width (+ width (max w w2)) height (+ h d height) depth (+ d2 h2 depth)))
547 (update-heights height depth)
548 (return result))))
549 ((and (atom (caddr form))
550 (not (atom (cadr form)))
551 (not (safe-get (caaadr form) 'dimension))
552 (prog2 (setq bas (nformat-check (cadr form)))
553 (not (safe-get (caar bas) 'dimension))))
554 (return (dimension-function
555 (list* '(mqapply) (list '(mexpt) (mop bas) (caddr form)) (margs bas))
556 result)))
557 (t (setq bas (dimension (cadr form) result lop 'mexpt nil 0) width (+ w width))
558 (if (not (checkfit width))
559 (return (dimension-function (cons '($expt) (cdr form)) result)))
560 (if (eql #\) (car bas))
561 (setq result (cons (list* 0 (1+ d) exp) bas) height (max (+ 1 h d) height))
562 (setq result (cons (list* 0 (+ height d) exp) bas) height (+ h d height)))
563 (update-heights height depth)
564 (return result)))))
566 (defun dsumprod (form result d-form sw sh sd)
567 (prog (str to dummy (w 0) (h 0) (d 0) dummy2 (lsum (eq (caar form) '%lsum)))
568 (setq dummy2 (dimension (caddr form) nil 'mparen 'mequal nil 0)
569 w width
570 h height
571 d depth)
572 (if lsum
573 (setq str " in " to "")
574 (setq str " = " to (cadr (cdddr form))))
575 (push-string str dummy2)
576 (setq dummy2 (dimension (cadddr form) dummy2 'mequal 'mparen nil 0)
577 w (+ (length str) w width)
578 h (max h height)
579 d (max d depth))
580 (setq dummy (dimension to nil 'mparen 'mparen nil 0))
581 (unless (checkfit (max w width))
582 (return (dimension-function form result)))
583 (setq dummy2 (cons (cons (- sw) (cons (- (+ sd h)) dummy2)) (cons d-form result)))
584 (cond ((> width sw)
585 (setq sw 0))
587 (setq sw (truncate (- sw width) 2)
588 width (+ sw width))))
589 (setq dummy (cons (cons (- sw w) (cons (+ sh depth) dummy)) dummy2)
590 w (max w width)
591 d (+ sd h d)
592 h (+ sh height depth))
593 (update-heights h d)
594 (setq dummy (dimension (cadr form) (cons (list (1+ (- w width)) 0) dummy)
595 (caar form) rop w right)
596 width (+ 1 w width)
597 height (max h height)
598 depth (max d depth))
599 (return dummy)))
601 (displa-def bigfloat dim-bigfloat)
602 (displa-def %bigfloat dim-bigfloat)
603 (displa-def mquote dimension-prefix "'")
604 (displa-def %mquote dimension-prefix "'")
605 (displa-def msetq dimension-infix " : ")
606 (displa-def %msetq dimension-infix " : ")
607 (displa-def mset dimension-infix " :: ")
608 (displa-def %mset dimension-infix " :: ")
609 (displa-def mdefine dim-mdefine " := ")
610 (displa-def %mdefine dim-mdefine " := ")
611 (displa-def mdefmacro dim-mdefine " ::= ")
612 (displa-def %mdefmacro dim-mdefine " ::= ")
614 (defun dim-mdefine (form result)
615 (let (($noundisp t)
616 ($stringdisp t))
617 (dimension-infix (if (cdddr form)
618 (list (car form) (cadr form) (cons '(mprogn) (cddr form)))
619 form)
620 result)))
622 (displa-def mfactorial dimension-postfix "!")
623 (displa-def %mfactorial dimension-postfix "!")
624 (displa-def mexpt dimension-superscript)
625 (displa-def %mexpt dimension-superscript)
626 (displa-def mncexpt dim-mncexpt "^^")
627 (displa-def %mncexpt dim-mncexpt "^^")
629 (defun dim-mncexpt (form result)
630 (dimension-superscript (list '(mncexpt) (cadr form) (cons '(mangle) (cddr form)))
631 result))
633 (displa-def mnctimes dimension-nary " . ")
634 (displa-def %mnctimes dimension-nary " . ")
636 (displa-def %product dim-%product 115.)
638 (defun dim-%product (form result)
639 (dsumprod form result '(d-prodsign) 5 3 1))
641 (displa-def rat dim-rat "/")
642 (displa-def %rat dim-rat "/")
644 (defun dim-rat (form result)
645 (if $pfeformat
646 (dimension-nary form result)
647 (dim-mquotient form result)))
649 (displa-def mquotient dim-mquotient "/")
650 (displa-def %mquotient dim-mquotient "/")
652 (defun dim-mquotient (form result)
653 (unless (= (length (cdr form)) 2)
654 (return-from dim-mquotient (dimension-function form result)))
655 (prog (num (w 0) (h 0) (d 0) den)
656 (when (and (= 1 size) (atom (cadr form)) (atom (caddr form)))
657 (return (dimension-nary form result)))
658 (setq num (dimension (cadr form) nil 'mparen 'mparen nil right)
659 w width
660 h height
661 d depth)
662 (unless (checkfit w)
663 (return (dimension-nary form result)))
664 (setq den (dimension (caddr form) nil 'mparen 'mparen nil right))
665 (unless (checkfit width)
666 (return (dimension-nary form result)))
667 (return (dratio result num w h d den width height depth))))
669 ;; <-- W1 -->
670 ;; ------------------
671 ;; | ^ |
672 ;; <- X1 -> | | H1 |
673 ;; | | D1 |
674 ;; | v |
675 ;; ------------------
676 ;; ----------------------------------
677 ;; (Likewise for X2, H2, D2, W2 in the denominator)
679 (defun dratio (result num w1 h1 d1 den w2 h2 d2)
680 (setq width (max w1 w2)
681 height (+ 1 h1 d1)
682 depth (+ h2 d2))
683 (setq h1 (truncate (- width w1) 2)
684 d2 (truncate (- width w2) 2))
685 (update-heights height depth)
686 (push `(,h1 ,(1+ d1) . ,num) result)
687 (push `(,(- d2 (+ h1 w1)) ,(- h2) . ,den) result)
688 (push `(,(- 0 d2 w2) 0) result)
689 (push `(d-hbar ,width) result)
690 result)
692 (displa-def mtimes dimension-nary " ")
693 (displa-def %mtimes dimension-nary " ")
695 ;; This code gets run when STARDISP is assigned a value.
697 (defprop $stardisp stardisp assign)
699 (defun stardisp (symbol val)
700 (declare (ignore symbol))
701 (putprop 'mtimes (if val '(#\*) '(#\space)) 'dissym))
703 (displa-def %integrate dim-%integrate 115.)
705 (defun dim-%integrate (form result)
706 (prog (dummy (w 0)(h 0)(d 0) dummy2)
707 (cond ((not (or (= (length (cdr form)) 2) (= (length (cdr form)) 4)))
708 (return-from dim-%integrate (dimension-function form result)))
709 ((null (cdddr form))
710 (setq dummy `(#\space (d-integralsign) . ,result) w 2 h 3 d 2))
711 (t (setq dummy (dimension (cadr (cdddr form)) nil 'mparen 'mparen nil 0)
712 w width h height d depth)
713 (setq dummy2 (dimension (cadddr form) nil 'mparen 'mparen nil 0))
714 (if (not (checkfit (+ 2 (max w width))))
715 (return (dimension-function form result)))
716 (setq dummy `((0 ,(+ 3 d) . ,dummy) (d-integralsign) . ,result))
717 (setq dummy (cons (cons (- w) (cons (- (+ 2 height)) dummy2)) dummy)
718 w (+ 2 (max w width)) h (+ 3 h d) d (+ 2 height depth)
719 dummy (cons (list (- w 1 width) 0) dummy))))
720 (update-heights h d)
721 (setq dummy (dimension (cadr form) dummy '%integrate 'mparen w 2)
722 w (+ w width) h (max h height) d (max d depth))
723 (push-string " d" dummy)
724 (setq dummy (dimension (caddr form) dummy 'mparen rop (+ 2 w) right)
725 width (+ 2 w width) height (max h height) depth (max d depth))
726 (return dummy)))
728 (displa-def %derivative dim-%derivative 125.)
730 (defun dim-%derivative (form result)
731 (prog ()
732 (cond ((null (cddr form))
733 (return (dimension-function (cons '(%diff) (cdr form)) result))))
734 (cond ((null (cdddr form)) (setq form (append form '(1)))))
735 (cond ((and $derivabbrev
736 (do ((l (cddr form) (cddr l))) ((null l) t)
737 (cond ((and (atom (car l)) (integerp (cadr l)) (> (cadr l) 0)))
738 (t (return nil)))))
739 (return (dmderivabbrev form result)))
740 ((or (> (rbp lop) 130.) (> (lbp rop) 130.)
741 (and (not (atom (cadr form))) (or (> (rbp lop) 110.) (> (lbp rop) 110.))))
742 (return (dimension-paren form result)))
743 (t (return (dmderivlong form result))))))
745 (defun dmderivabbrev (form result)
746 (prog (dummy (w 0))
747 (do ((l (cddr form) (cddr l)) (var))
748 ((null l) (setq dummy (cdr dummy) w (1- w)))
749 (setq var (dimension (car l) nil 'mparen 'mparen nil 0))
750 (do ((i (cadr l) (1- i)))
751 ((= 1 i))
752 (setq dummy (cons #\space (append var dummy))))
753 (setq dummy (cons #\space (nconc var dummy)) w (+ w (cadr l) (* (cadr l) width))))
754 (setq result (dimension (cadr form) result lop '%deriv 0 right))
755 (setq result (cons (cons 0 (cons (- 0 depth 1) dummy)) result)
756 width (+ w width) depth (max 1 (1+ depth)))
757 (update-heights height depth)
758 (return result)))
760 (defun dmderivlong (form result)
761 (prog (num (w1 0) (h1 0) (d1 0) den (w2 0)( h2 0) (d2 0))
762 (setq num (list (cadddr form))
763 den (cond ((equal 1 (cadddr form))
764 (dimension (caddr form)
765 (list #\d) 'mparen 'mparen nil 0))
766 (t (dimension-superscript
767 (cons '(diff)(cddr form)) (list #\d))))
768 w2 (1+ width) h2 height d2 depth)
769 (do ((l (cddddr form) (cddr l))) ((null l))
770 (setq num (cons (cadr l) num)
771 den (cond ((equal 1 (cadr l))
772 (dimension (car l) (cons #\d (cons #\space den))
773 'mparen 'mparen nil 0))
774 (t (dimension-superscript
775 (cons '(diff) l) (cons #\d (cons #\space den)))))
776 w2 (+ 2 w2 width) h2 (max h2 height) d2 (+ d2 depth)))
777 (setq num (nformat-check (addn num t)))
778 (cond ((equal 1 num) (setq num (list #\d) w1 1 h1 1 d1 0))
779 (t (setq num (dimension-superscript (list '(diff) #\d num) nil)
780 w1 width h1 height d1 depth)))
781 (cond ((atom (setq form (nformat-check (cadr form))))
782 (setq num (dimension form num '%deriv 'mparen nil 0) w1 (+ w1 width))
783 (return (dratio result num w1 h1 d1 den w2 h2 d2)))
784 (t (setq result (dratio result num w1 h1 d1 den w2 h2 d2) w1 width h1 height d1 depth)
785 (setq result (dimension form (cons #\space result) '%deriv rop w1 right)
786 width (+ 1 w1 width) height (max h1 height) depth (max d1 depth))
787 (update-heights height depth)
788 (return result)))))
790 (displa-def %at dim-%at 105. 105.)
792 (defun dim-%at (form result)
793 (prog (exp eqs (w 0) (h 0) (d 0))
794 (unless (= (length (cdr form)) 2)
795 (return-from dim-%at (dimension-function form result)))
796 (setq exp (dimension (cadr form) result lop '%at nil 0)
797 w width
798 h height
799 d depth)
800 (setq eqs (dimension (cond ((not (eq 'mlist (caar (caddr form)))) (caddr form))
801 ((null (cddr (caddr form))) (cadr (caddr form)))
802 (t (cons '(mcomma) (cdaddr form))))
803 nil 'mparen 'mparen nil 0))
804 (unless (checkfit (+ 1 w width))
805 (return (dimension-function form result)))
806 (setq result (cons (cons 0 (cons (- 0 1 d) eqs))
807 (cons `(d-vbar ,(1+ h) ,(1+ d) ,(car (coerce $absboxchar 'list))) exp))
808 width (+ 1 w width)
809 height (1+ h)
810 depth (+ 1 d depth))
811 (update-heights height depth)
812 (return result)))
814 (displa-def mplus dim-mplus)
815 (displa-def %mplus dim-mplus)
816 (defprop munaryplus (#\+ #\space) dissym)
818 (defun dim-mplus (form result)
819 ;; If only 0 or 1 arguments, then print "+"() or +A
820 (cond ((and (null (cddr form))
821 (not (member (cadar form) '(trunc exact) :test #'eq)))
822 (if (null (cdr form))
823 (dimension-function form result)
824 (dimension-prefix (cons '(munaryplus) (cdr form)) result)))
825 (t (setq result (dimension (cadr form) result lop 'mplus 0 0))
826 (checkbreak result width)
827 (do ((l (cddr form) (cdr l))
828 (w width) (h height) (d depth)
829 (trunc (member 'trunc (cdar form) :test #'eq)) (dissym))
830 ((null l) (cond (trunc
831 (setq width (+ 8 w) height h depth d)
832 (push-string " + . . ." result)))
833 result)
834 (if (mmminusp (car l))
835 (setq dissym '(#\space #\- #\space) form (cadar l))
836 (setq dissym '(#\space #\+ #\space) form (car l)))
837 (cond ((and (not trunc) (null (cdr l)))
838 (setq result (dimension form (append dissym result)
839 'mplus rop (+ 3 w) right)
840 width (+ 3 w width)
841 height (max h height)
842 depth (max d depth))
843 (return result))
844 (t (setq result
845 (dimension form (append dissym result)
846 'mplus 'mplus (+ 3 w) 0)
847 w (+ 3 w width)
848 h (max h height)
849 d (max d depth))
850 (checkbreak result w)))))))
852 (displa-def mminus dim-mminus)
853 (displa-def %mminus dim-mminus)
854 (defprop munaryminus (#\- #\space) dissym)
855 (def-rbp munaryminus 134)
856 (def-rbp munaryminus 100)
858 (defun dim-mminus (form result)
859 (cond ((and (null (cddr form))
860 (not (member (cadar form) '(trunc exact) :test #'eq)))
861 (if (null (cdr form))
862 (dimension-function form result)
863 (dimension-prefix (cons '(munaryminus) (cdr form)) result)))
865 (setq result (dimension (cadr form) result lop 'mminus 0 0))
866 (checkbreak result width)
867 (do ((l (cddr form) (cdr l))
868 (w width)
869 (h height)
870 (d depth)
871 (trunc (member 'trunc (cdar form) :test #'eq))
872 (dissym))
873 ((null l)
874 (cond (trunc
875 (setq width (+ 8 w)
876 height h
877 depth d)
878 (push-string " + . . ." result)))
879 result)
880 (if (mmminusp (car l))
881 (setq dissym '(#\space #\+ #\space) form (cadar l))
882 (setq dissym '(#\space #\- #\space) form (car l)))
883 (cond ((and (not trunc) (null (cdr l)))
884 (setq result (dimension form (append dissym result)
885 'mminus rop (+ 3 w) right)
886 width (+ 3 w width)
887 height (max h height)
888 depth (max d depth))
889 (return result))
891 (setq result
892 (dimension form (append dissym result)
893 'mminus 'mminus (+ 3 w) 0)
894 w (+ 3 w width)
895 h (max h height)
896 d (max d depth))
897 (checkbreak result w)))))))
899 (displa-def %sum dim-%sum 110.)
900 (displa-def %limit dim-%limit 110. 110.)
901 (displa-def %lsum dim-%lsum 110.)
903 (defun dim-%lsum (form result)
904 (dsumprod form result '(d-sumsign) 4 3 2))
906 (defun dim-%sum (form result)
907 (dsumprod form result '(d-sumsign) 4 3 2))
909 (defun dim-%limit (form result)
910 (prog ((w 0) (h 0) (d 0) dummy)
911 (unless (or (= (length (cdr form)) 3) (= (length (cdr form)) 4))
912 (return-from dim-%limit (dimension-function form result)))
913 (setq dummy (dimension (third form) nil 'mparen 'mparen nil 0)
914 w width h height d depth)
915 (push-string " -> " dummy)
916 (setq dummy (dimension (fourth form) dummy 'mparen 'mparen nil 0)
917 w (+ 4 w width)
918 h (max h height)
919 d (max d depth))
920 (cond ((null (cddddr form)))
921 ((eq '$plus (fifth form))
922 (push #\+ dummy)
923 (incf w))
924 (t (push #\- dummy)
925 (incf w)))
926 (push-string "limit" result)
927 (setq dummy (cons (list* -5 (- h) dummy) result)
928 d (+ h d))
929 (update-heights 1 d)
930 (setq dummy (dimension (cadr form) (cons '(1 0) dummy) '%limit rop (1+ w) right))
931 (setq width (+ 1 w width)
932 depth (max d depth))
933 (return dummy)))
935 ;; Some scheme needs to be worked out to allow use of mathematical character
936 ;; sets on consoles which have them.
938 (displa-def marrow dimension-infix " -> " 80. 80.)
939 (displa-def %marrow dimension-infix " -> " 80. 80.)
940 (displa-def mgreaterp dimension-infix " > ")
941 (displa-def %mgreaterp dimension-infix " > ")
942 (displa-def mgeqp dimension-infix " >= ")
943 (displa-def %mgeqp dimension-infix " >= ")
944 (displa-def mequal dimension-infix " = ")
945 (displa-def %mequal dimension-infix " = ")
946 (displa-def mnotequal dimension-infix " # ")
947 (displa-def %mnotequal dimension-infix " # ")
948 (displa-def mleqp dimension-infix " <= ")
949 (displa-def %mleqp dimension-infix " <= ")
950 (displa-def mlessp dimension-infix " < ")
951 (displa-def %mlessp dimension-infix " < ")
952 (displa-def mnot dimension-prefix "not ")
953 (displa-def %mnot dimension-prefix "not ")
954 (displa-def mand dimension-nary " and ")
955 (displa-def %mand dimension-nary " and ")
956 (defprop mand dimnary-boolean dimension-nary-helper)
957 (displa-def mor dimension-nary " or ")
958 (displa-def %mor dimension-nary " or ")
959 (defprop mor dimnary-boolean dimension-nary-helper)
960 (displa-def mcond dim-mcond)
961 (displa-def %mcond dim-mcond)
963 ;; MCOND or %MCOND always has an even number of arguments.
964 ;; The first two arguments are the foo and bar in 'if foo then bar .
965 ;; Of the remaining pairs of arguments,
966 ;; the first is MAYBE-ELSEIF and the second is ELSE-OR-THEN in the code below.
967 ;; MAYBE-ELSEIF is T if the construct is 'if foo then bar else quux ,
968 ;; otherwise the construct is 'if foo then bar elseif baz then quux
969 ;; where baz is the value of MAYBE-ELSEIF.
970 ;; If ELSE-OR-THEN is NIL, just omit the final "else".
972 ;; The parser appends (T NIL) to any if-then which lacks an else.
973 ;; DIM-MCOND renders both '((%MCOND) $A $B) and '((%MCOND) $A $B T NIL) as "if a then b".
975 ;; Examples. The "<==>" here means that the stuff on the right parses as the stuff on the left,
976 ;; and the stuff on the left displays as the stuff on the right.
978 ;; ((%mcond) $a $b t nil) <==> 'if a then b
979 ;; ((%mcond) $a $b t $d) <==> 'if a then b else d
980 ;; ((%mcond) $a $b $c nil t nil) <==> 'if a then b elseif c then false
981 ;; ((%mcond) $a $b $c $d t nil) <==> 'if a then b elseif c then d
982 ;; ((%mcond) $a $b $c $d t $f) <==> 'if a then b elseif c then d else f
983 ;; ((%mcond) $a $b $c $d $e nil t nil) <==> 'if a then b elseif c then d elseif e then false
984 ;; ((%mcond) $a $b $c $d $e $f t nil) <==> 'if a then b elseif c then d elseif e then f
985 ;; ((%mcond) $a $b $c $d $e $f t $h) <==> 'if a then b elseif c then d elseif e then f else h
986 ;; ((%mcond) $a $b $c $d $e $f $g nil t nil) <==> 'if a then b elseif c then d elseif e then f elseif g then false
987 ;; ((%mcond) $a $b $c $d $e $f $g $h) <==> 'if a then b elseif c then d elseif e then f elseif g then h
989 (defun dim-mcond (form result)
990 (prog ((w 0) (h 0) (d 0))
991 (push-string "if " result)
992 (setq result (dimension (cadr form) result 'mcond 'mparen 3 0)
993 w (+ 3 width)
994 h height
995 d depth)
996 (checkbreak result w)
997 (push-string " then " result)
998 (setq result (dimension (caddr form) result 'mcond 'mparen (+ 6 w) 0)
999 w (+ 6 w width)
1000 h (max h height)
1001 d (max d depth))
1002 (let ((args (cdddr form)))
1003 (loop while (>= (length args) 2) do
1004 (let ((maybe-elseif (car args)) (else-or-then (cadr args)))
1005 (cond
1006 ((and (eq maybe-elseif t) (= (length args) 2))
1007 (unless (or (eq '$false else-or-then) (eq nil else-or-then))
1008 (checkbreak result w)
1009 (push-string " else " result)
1010 (setq result (dimension else-or-then result 'mcond rop (+ 6 w) right)
1011 w (+ 6 w width)
1012 h (max h height)
1013 d (max d depth))))
1015 (checkbreak result w)
1016 (push-string " elseif " result)
1017 (setq result (dimension maybe-elseif result 'mcond rop (+ 8 w) right)
1018 w (+ 8 w width)
1019 h (max h height)
1020 d (max d depth))
1021 (checkbreak result w)
1022 (push-string " then " result)
1023 (setq result (dimension else-or-then result 'mcond rop (+ 6 w) right)
1024 w (+ 6 w width)
1025 h (max h height)
1026 d (max d depth)))))
1027 (setq args (cddr args))))
1028 (setq width w height h depth d)
1029 (return result)))
1032 (displa-def mdo dim-mdo)
1033 (displa-def %mdo dim-mdo)
1035 (defun dim-mdo (form result)
1036 (prog ((w 0) (h 0) (d 0) brkflag)
1037 (cond ((not (null (cadr form)))
1038 (push-string "for " result)
1039 (setq result (cons #\space (dimension (cadr form) result 'mdo 'mparen 4 right))
1040 w (+ 4 width) h height d depth brkflag t)))
1041 (cond ((or (null (caddr form)) (equal 1 (caddr form))))
1042 (t (push-string "from " result)
1043 (setq result (cons #\space (dimension (caddr form) result 'mdo 'mparen (+ 6 w) 0))
1044 w (+ 6 w width) h (max h height) d (max d depth))))
1045 (setq form (cdddr form))
1046 (cond ((equal 1 (car form)))
1047 ((not (null (car form)))
1048 (push-string "step " result)
1049 (setq result (cons #\space (dimension (car form) result 'mdo 'mparen (+ 6 w) 0))
1050 w (+ 6 w width) h (max h height) d (max d depth)))
1051 ((not (null (cadr form)))
1052 (push-string "next " result)
1053 (setq result (cons #\space (dimension (cadr form) result 'mdo 'mparen (+ 6 w) 0))
1054 w (+ 6 w width) h (max h height) d (max d depth))))
1055 (cond ((not (null (caddr form)))
1056 (push-string "thru " result)
1057 (setq result (cons #\space (dimension (caddr form) result 'mdo 'mparen (+ 6 w) 0))
1058 w (+ 6 w width) h (max h height) d (max d depth) brkflag t)))
1059 (cond ((not (null (cadddr form)))
1060 (cond ((and (not (atom (cadddr form))) (eq (caar (cadddr form)) 'mnot))
1061 (push-string "while " result)
1062 (setq result (cons #\space (dimension (cadr (cadddr form)) result 'mdo 'mparen (+ 7 w) 0))
1063 w (+ 7 w width) h (max h height) d (max d depth)))
1064 (t (push-string "unless " result)
1065 (setq result
1066 (cons #\space (dimension (cadddr form) result 'mdo 'mparen (+ 8 w) 0))
1067 w (+ 8 w width) h (max h height) d (max d depth))))))
1068 (if brkflag (checkbreak result w))
1069 (push-string "do " result)
1070 (setq result (dimension (car (cddddr form)) result 'mdo rop (+ 4 w) right)
1071 width (+ 4 w width) height (max h height) depth (max d depth))
1072 (return result)))
1075 (displa-def mdoin dim-mdoin)
1076 (displa-def %mdoin dim-mdoin)
1078 (defun dim-mdoin (form result)
1079 (prog ((w 0) (h 0) ( d 0))
1080 (push-string "for " result)
1081 (setq result (dimension (cadr form) result 'mdo 'mparen 4 0)
1082 w (+ 4 width) h height d depth)
1083 (push-string " in " result)
1084 (setq result (dimension (caddr form) result 'mdo 'mparen (+ 4 w) 0)
1085 w (+ 4 w width) h (max h height) d (max d depth))
1086 (setq form (cdr (cddddr form)))
1087 (cond ((not (null (car form)))
1088 (push-string " thru " result)
1089 (setq result (dimension (car form) result 'mdo 'mparen (+ 6 w) 0)
1090 w (+ 6 w width) h (max h height) d (max d depth))))
1091 (cond ((not (null (cadr form)))
1092 (push-string " unless " result)
1093 (setq result (dimension (cadr form) result 'mdo 'mparen (+ 8 w) 0)
1094 w (+ 8 w width) h (max h height) d (max d depth))))
1095 (push-string " do " result)
1096 (setq result (dimension (caddr form) result 'mdo rop (+ 4 w) right)
1097 width (+ 4 w width) height (max h height) depth (max d depth))
1098 (return result)))
1100 (displa-def mprogn dimension-match "(" ")")
1101 (displa-def %mprogn dimension-match "(" ")")
1102 (displa-def mlist dimension-match "[" "]")
1103 (displa-def %mlist dimension-match "[" "]")
1104 (displa-def mangle dimension-match "<" ">")
1105 (displa-def %mangle dimension-match "<" ">")
1106 (displa-def mcomma dimension-nary ", " 10. 10.)
1107 (displa-def %mcomma dimension-nary ", " 10. 10.)
1108 (displa-def mabs dim-mabs)
1109 (displa-def %mabs dim-mabs)
1111 (defun dim-mabs (form result &aux arg bar)
1112 (setq arg (dimension (cadr form) nil 'mparen 'mparen nil 0))
1113 (cond ((or (> (+ 2 width) linel) (and (= 1 height) (= 0 depth)))
1114 (dimension-function form result))
1115 (t (setq width (+ 2 width))
1116 (update-heights height depth)
1117 (setq bar `(d-vbar ,height ,depth ,(car (coerce $absboxchar 'list))))
1118 (cons bar (nconc arg (cons bar result))))))
1120 (displa-def $matrix dim-$matrix)
1121 (displa-def %matrix dim-$matrix)
1123 (defun dim-$matrix (form result)
1124 (prog (dmstr rstr cstr consp cols)
1125 (setq cols (if ($listp (cadr form)) (length (cadr form)) 0))
1126 (if (or (null (cdr form))
1127 (memalike '((mlist simp)) (cdr form))
1128 ;; Check if the matrix has lists as rows with a equal number of
1129 ;; columns.
1130 (dolist (row (cdr form))
1131 (if (or (not ($listp row))
1132 (not (eql cols (length row))))
1133 (return t))))
1134 ;; The matrix is not well formed. Display the matrix in linear mode.
1135 (return (dimension-function form result)))
1136 (do ((l (cdadr form) (cdr l))) ((null l))
1137 (setq dmstr (cons nil dmstr) cstr (cons 0 cstr)))
1138 (do ((r (cdr form) (cdr r)) (h1 0) (d1 0))
1139 ((or consp (null r))
1140 (setq width 0)
1141 (do ((cs cstr (cdr cs))) ((null cs)) (setq width (+ 2 (car cs) width)))
1142 (setq h1 (1- (+ h1 d1)) depth (truncate h1 2) height (- h1 depth)))
1143 (do ((c (cdar r) (cdr c))
1144 (nc dmstr (cdr nc))
1145 (cs cstr (cdr cs)) (dummy) (h2 0) (d2 0))
1146 ((null c) (setq d1 (+ d1 h1 h2) h1 (1+ d2)))
1147 (setq dummy (dimension (car c) nil 'mparen 'mparen nil 0)
1148 h2 (max h2 height) d2 (max d2 depth))
1149 (cond ((not (checkfit (+ 14. width))) (setq consp t) (return nil))
1150 (t (rplaca nc (cons (list* width height depth dummy) (car nc)))
1151 (rplaca cs (max width (car cs))))))
1152 (setq rstr (cons d1 rstr)))
1153 (if (> (+ height depth) (length linearray))
1154 (setq consp t))
1155 (return
1156 (cond ((and (not consp) (checkfit (+ 2 width)))
1157 (matout dmstr cstr rstr result))
1158 ((and (not consp) (<= level 2)) (colout dmstr cstr result))
1159 (t (dimension-function form result))))))
1161 (defun matout (dmstr cstr rstr result)
1162 (push `(d-matrix left ,height ,depth) result)
1163 (push #\space result)
1164 (do ((d dmstr (cdr d)) (c cstr (cdr c)) (w 0 0))
1165 ((null d))
1166 (do ((d (car d) (cdr d)) (r rstr (cdr r))) ((null d))
1167 (rplaca (cddar d) (- height (car r)))
1168 (rplaca (cdar d) (- (truncate (- (car c) (caar d)) 2) w))
1169 (setq w (truncate (+ (car c) (caar d)) 2))
1170 (rplaca d (cdar d)))
1171 (setq result (cons (list (+ 2 (- (car c) w)) 0) (nreconc (car d) result))))
1172 (setq width (+ 2 width))
1173 (update-heights height depth)
1174 (rplaca (car result) (1- (caar result)))
1175 (push `(d-matrix right ,height ,depth) result)
1176 result)
1178 (defun colout (dmstr cstr result)
1179 (setq width 0 height 1 depth 0)
1180 (do ((r dmstr (cdr r)) (c cstr (cdr c)) (col 1 (1+ col)) (w 0 0) (h -1 -1) (d 0))
1181 ((null r))
1182 (push-string " Col " result)
1183 (setq result (nreconc (exploden col) result))
1184 (push-string " = " result)
1185 (setq width (+ 8 (flatc col) width))
1186 (do ((r (car r) (cdr r))) ((null r))
1187 (setq h (+ 1 h (cadar r) (caddar r)))
1188 (rplaca (cddar r) (- h (cadar r)))
1189 (rplaca (cdar r) (- (truncate (- (car c) (caar r)) 2) w))
1190 (setq w (truncate (+ (car c) (caar r)) 2))
1191 (rplaca r (cdar r)))
1192 (setq d (truncate h 2) h (- h d))
1193 (push `(d-matrix left ,h ,d) result)
1194 (push #\space result)
1195 (push `(0 ,(- d) . ,(nreverse (car r))) result)
1196 (push `(,(1+ (- (car c) w)) 0) result)
1197 (push `(d-matrix right ,h ,d) result)
1198 (setq width (+ 4 (car c) width) height (max h height) depth (max d depth))
1199 (update-heights h d)
1200 (checkbreak result width))
1201 result)
1203 (displa-def mbox dim-mbox)
1204 (displa-def %mbox dim-mbox)
1206 (defun dim-mbox (form result &aux dummy)
1207 (setq dummy (dimension (cadr form) nil 'mparen 'mparen nil 0))
1208 (cond ((not (checkfit (+ 2 width)))
1209 (dimension-function (cons '($box) (cdr form)) result))
1210 (t (push `(d-box ,height ,depth ,width ,(nreverse dummy)) result)
1211 (setq width (+ 2 width) height (1+ height) depth (1+ depth))
1212 (update-heights height depth)
1213 result)))
1215 (displa-def mlabox dim-mlabox)
1216 (displa-def %mlabox dim-mlabox)
1218 (defun dim-mlabox (form result)
1219 (prog (dummy ch)
1220 (setq dummy (dimension (cadr form) nil 'mparen 'mparen nil 0))
1221 (cond ((not (checkfit (+ 2 width)))
1222 (return (dimension-function (cons '($box) (cdr form)) result))))
1223 (setq width (+ 2 width) height (1+ height) depth (1+ depth))
1224 (setq ch (car (coerce $boxchar 'list)))
1225 (setq result
1226 (cons (do ((l (mapcar #'(lambda (l) (char (symbol-name l) 0))
1227 (makstring (caddr form))) (cdr l))
1228 (w 0) (nl))
1229 ((or (null l) (= width w))
1230 (cons 0 (cons (1- height)
1231 (cond ((< w width)
1232 (cons `(d-hbar ,(- width w) ,ch) nl))
1233 (t nl)))))
1234 (setq nl (cons (car l) nl) w (1+ w)))
1235 result))
1236 (setq result (nconc dummy (list* `(d-vbar ,(1- height) ,(1- depth) ,ch)
1237 (list (- width) 0) result)))
1238 (setq result (cons (list (- 1 width) (- depth) `(d-hbar ,width ,ch)) result))
1239 (setq result (list* `(d-vbar ,(1- height) ,(1- depth) ,ch) '(-1 0) result))
1240 (update-heights height depth)
1241 (return result)))
1243 (displa-def mtext dim-mtext 1 1)
1244 (displa-def %mtext dim-mtext 1 1)
1245 (defprop mtext dimnary-mtext dimension-nary-helper)
1247 (defun dim-mtext (form result)
1248 (if (null (cddr form)) (dimension (cadr form) result lop rop 0 0)
1249 (dimension-nary form result)))
1251 (displa-def mlabel dim-mlabel 0 0)
1252 (displa-def %mlabel dim-mlabel 0 0)
1253 (setf (get 'mlabel 'wxxml) 'wxxml-mlable) ;; backwards-compatibility for wxMaxima
1255 (defvar *display-labels-p* t)
1257 (defun dim-mlabel (form result)
1258 (prog (dummy (w 0) (h 0) (d 0))
1259 (cond ((eq nil (cadr form)) (setq w 0 h 0 d 0))
1260 (mratp (setq result (append mratp (if *display-labels-p*
1261 (dimension-paren (cadr form) result)))
1262 w (+ 4 width) h height d depth))
1263 (t (setq result (cons #\space (if *display-labels-p*
1264 (dimension-paren (cadr form) result)))
1265 w (1+ width) h height d depth)))
1266 (let ((level linel)) (checkbreak result w))
1267 (setq dummy (list 0 0))
1268 (setq result (dimension (caddr form) (cons dummy result) 'mlabel rop w right))
1269 (cond ((and (not $leftjust) (= 0 bkptout))
1270 (rplaca dummy (max 0 (- (truncate (- linel width) 2) w)))
1271 (setq width (+ (car dummy) width))))
1272 (setq width (+ w width) height (max h height) depth (max d depth))
1273 (return result)))
1275 (defprop mparen -1. lbp)
1276 (defprop mparen -1. rbp)
1278 (defun checkrat (form)
1279 (cond ((atom form) nil)
1280 ((and (not (atom (car form))) (eq (caar form) 'mrat))
1281 (if (member 'trunc (cdar form) :test #'eq)
1282 '(#\space #\/ #\T #\/)
1283 '(#\space #\/ #\R #\/)))
1284 ((and (not (atom (car form))) (eq (caar form) 'mpois))
1285 '(#\space #\/ #\P #\/))
1287 (do ((l (cdr form) (cdr l)))
1288 ((null l))
1289 (cond ((atom l)
1290 (merror (intl:gettext "display: not a well-formed Maxima expression: ~S") form))
1291 ((setq form (checkrat (car l)))
1292 (return form)))))))
1294 (defun checkfit (w)
1295 (or (not break) (<= (- (+ w break right 1) bkptwd) linel)))
1297 (defun checkbreak (result w)
1298 (cond ((not break))
1299 ((> (- (setq w (+ w break)) bkptout) linel)
1300 (if (or (null bkpt) (eq result bkpt))
1301 (merror (intl:gettext "display: failed to break up a long expression.~%display: change 'linel' slightly and try again.")))
1302 (do ((l result (cdr l)))
1303 ;; THE NEED FOR EQUAL HERE IS PROBABLY THE SYMPTOM OF A BUG IN ECL !!
1304 ;; PROBABLY RELATED TO SIDE-EFFECTS OF NRECONC, RPLACD, ETC !!
1305 ((#+ecl equal #-ecl eq bkpt (cdr l)) (rplacd l nil))
1306 (if (null l)
1307 ;; MEANING OF FOLLOWING MESSAGE IS OBSCURE.
1308 (merror (intl:gettext "display: 'checkbreak' not found."))))
1309 (output bkpt 0)
1310 (setq lines (1+ lines)
1311 bkpt result bkptout bkptwd bkptwd w
1312 bkptht maxht bkptdp maxdp bkptlevel level maxht 1 maxdp 0))
1313 ((or (null bkpt) (<= level bkptlevel) (> (truncate linel 2) (- bkptwd bkptout)))
1314 (setq bkpt result bkptwd w bkptlevel level
1315 bkptht (max maxht bkptht) bkptdp (max maxdp bkptdp) maxht 1 maxdp 0))))
1317 (defun forcebreak (result w)
1318 (output result 0)
1319 (setq lines (+ 2 lines)
1320 bkpt nil
1321 bkptout (+ w break)
1322 maxht 1 maxdp 0))
1324 (defun update-heights (ht* dp*)
1325 (if break
1326 (setq maxht (max maxht ht*)
1327 maxdp (max maxdp dp*))))
1329 ;;; BKPT dimension structure for last breakpoint saved
1330 ;;; BKPTWD width at last bkpt
1331 ;;; BKPTHT height of current line to last bkpt
1332 ;;; BKPTDP depth of current line to last bkpt
1333 ;;; BKPTOUT width of stuff already output
1335 ;;; MAXHT height from last bkpt saved to current point
1336 ;;; MAXDP depth from last bkpt saved to current point
1338 ;;; BREAK width up to last call to DIMENSION
1339 ;;; RESULT dimension structure to current point minus output
1340 ;;; W width from last call to DIMENSION to current point
1342 ;; Code above this point deals with dimensioning and constructing
1343 ;; up dimension strings. Code past this point deals with printing
1344 ;; them.
1346 ;; <dimension string> ::= () | (<string element> . <dimension string>)
1347 ;; <string element> ::= character |
1348 ;; (<column-relative> <row-relative> . <dimension string>) |
1349 ;; (<drawing function> . args)
1350 ;; <column-relative> ::= <fixnum>
1351 ;; <row-relative> ::= <fixnum>
1352 ;; <drawing function> ::= D-HBAR | D-VBAR | D-INTEGRALSIGN | ...
1354 ;; When a character appears in a dimension string, it is printed and
1355 ;; the cursor moves forward a single position. (The variable OLDCOL is
1356 ;; incremented) When a form with a fixnum car is encountered, the
1357 ;; first two elements of the form are taken to be relative displacements
1358 ;; for OLDCOL and OLDROW. *** NOTE *** After drawing the cddr of the form,
1359 ;; OLDROW is reset to its original value, but OLDCOL is left in the new
1360 ;; position. Why this is done is beyond me. It only appears to complicate
1361 ;; things.
1363 ;; When a <drawing function> is invoked, the first argument passed to it is a
1364 ;; flag which is T for linear output and NIL for 2D output. A
1365 ;; <drawing function> is also expected to return the new column position.
1367 (defun output (result w)
1368 ;; The following is a hack to attempt to determine if we're on an
1369 ;; interactive terminal, in which case the user's hitting of the ENTER
1370 ;; key will have caused a newline to be displayed already.
1371 #+(or gcl clisp) (cond ((not (equal *query-io* *standard-input*)) (fresh-line)))
1372 #-(or gcl clisp) (cond ((not (interactive-stream-p *standard-input*)) (fresh-line)))
1373 (cond
1374 ;; If console output is disabled, don't output anything.
1375 ($ttyoff)
1376 ((> (+ bkptht bkptdp) (length linearray))
1377 ;; I suppose we could reallocate LINEARRAY to some larger size and keep going here ...
1378 (merror (intl:gettext "display: expression is too tall to be displayed.")))
1380 (output-linear (nreverse result) w))))
1382 ;; Output function for terminals without cursor positioning capability.
1383 ;; Characters are drawn into LINEARRAY instead. Each element of LINEARRAY is a
1384 ;; list -- the car is how many spaces to indent; the cdr is a list of
1385 ;; characters to draw. After drawing into this array, lines are printed one at
1386 ;; a time. This is used for printing terminals and when writing to files.
1387 ;; Block mode i/o isn't needed since PRINC is used instead of WRITE-CHAR and
1388 ;; CURSORPOS.
1390 (defun output-linear (result w)
1391 (draw-linear result bkptdp w)
1392 (do ((i (1- (+ bkptht bkptdp)) (1- i)))
1393 ((< i 0))
1394 (cond ((null (aref linearray i)))
1395 (t (output-linear-one-line i)))))
1397 (defun output-linear-one-line (i)
1398 (prog (line (n 0))
1399 (setq line (aref linearray i)
1400 line (nreverse (cdr line))
1401 n (car line))
1402 (setf (aref linearray i) nil)
1403 (tyotbsp n)
1404 (loop for v in (cdr line) do (write-char v #+(or sbcl cmu) *standard-output*))
1405 (mterpri)))
1407 ;; Move the cursor over N spaces to the left by outputting spaces.
1408 ;; This function assumes that the cursor is in the left margin when
1409 ;; it is called. This is only called from OUTPUT-LINEAR, so it is
1410 ;; used only for printing terminals or for file output.
1412 (defun tyotbsp (n)
1413 (do ()
1414 ((< n 1))
1415 (write-char #\space #+(or sbcl cmu) *standard-output*)
1416 (decf n)))
1418 (defun draw-linear (dmstr oldrow oldcol)
1419 "This puts the LINE lists into LINEARRAY ready to be drawn.
1420 Each LINE consists of first an initial number of columns to space
1421 and then the characters to be printed.
1422 oldrow and oldcol are the starting points for the the (dx,dy) offsets
1423 given in the dimension string DMSTR. It does not check that oldrow
1424 is big enough for possible negative y offsets in DMSTR, but BKPTDP is the
1425 right global to use for oldrow."
1426 (do ((line))
1427 ((null dmstr))
1428 (cond ((atom (car dmstr))
1429 (setq line (aref linearray oldrow))
1430 (cond ((null line) (setq line (list oldcol)))
1431 (t (prog (n)
1432 (setq n (car line) line (cdr line))
1433 (do ()
1434 ((<= oldcol n))
1435 (push #\space line)
1436 (incf n)))))
1437 (do ()
1438 ((or (null dmstr) (not (atom (car dmstr))))
1439 (setf (aref linearray oldrow) (cons oldcol line)))
1440 (incf oldcol)
1441 (push (car dmstr) line)
1442 (pop dmstr)))
1443 ((integerp (caar dmstr))
1444 ;; Why update OLDCOL and not OLDROW? Should either update both
1445 ;; (requiring multiple value return) or neither (analogous to lambda
1446 ;; binding).
1447 (setq oldcol (draw-linear (reverse (cddar dmstr))
1448 (+ oldrow (cadar dmstr))
1449 (+ oldcol (caar dmstr))))
1450 (pop dmstr))
1451 (t (setq oldcol (apply (caar dmstr) t (cdar dmstr)))
1452 (pop dmstr))))
1453 ;; Be sure to return this.
1454 oldcol)
1456 ;; Special symbol drawing functions -- lines, boxes, summation signs, etc.
1457 ;; Every drawing function must take at least one argument. The first
1458 ;; argument is T if equations must be printed line-by-line. Otherwise,
1459 ;; draw them using cursor movement, character graphics, or line graphics
1460 ;; if possible.
1462 ;; Most of these functions just invoke DRAW-XXX on some constant
1463 ;; list structure, so be careful about NREVERSEing. In other cases,
1464 ;; stuff is consed only for the linear case, but direct calls are used
1465 ;; in the 2D case. This should work for both cases. (See end of
1466 ;; program.)
1468 (defun d-hbar (linear? w &optional (char #\-) &aux nl)
1469 (declare (ignore linear?))
1470 (dotimes (i w)
1471 (push char nl))
1472 (draw-linear nl oldrow oldcol))
1474 ;; Notice that in all of the height computations, an offset of 2 is added or
1475 ;; subtracted to the y-dimension. This is to get the lines to fit within the
1476 ;; character cell precisely and not get clipped when moving things around in
1477 ;; the equation editor.
1479 (defun d-vbar (linear? h d &optional (char #\|))
1480 (declare (ignore linear?))
1481 (setq d (- d))
1482 (do ((i (- h 2) (1- i))
1483 (nl `((0 ,(1- h) ,char))))
1484 ((< i d) (draw-linear (nreverse nl) oldrow oldcol))
1485 (push `(-1 ,i ,char) nl)))
1487 (defun d-integralsign (linear? &aux dmstr)
1488 (declare (ignore linear?))
1489 (setq dmstr `((0 2 #\/) (-1 1 #\[) (-1 0 #\I) (-1 -1 #\]) (-1 -2 #\/)))
1490 (draw-linear dmstr oldrow oldcol))
1492 (defun d-prodsign (linear? &aux dmstr)
1493 (declare (ignore linear?))
1494 (setq dmstr '((0 2 #\\ (d-hbar 3 #\=) #\/) (-4 0) (d-vbar 2 1 #\!) #\space (d-vbar 2 1 #\!) (1 0)))
1495 (draw-linear dmstr oldrow oldcol))
1497 (defun d-sumsign (linear? &aux dmstr)
1498 (declare (ignore linear?))
1499 (setq dmstr '((0 2 (d-hbar 4 #\=)) (-4 1 #\\) #\> (-2 -1 #\/) (-1 -2 (d-hbar 4 #\=))))
1500 (draw-linear dmstr oldrow oldcol))
1502 ;; Notice how this calls D-VBAR in the non-graphic case. The entire output
1503 ;; side should be structured this way, with no consing of intermediate
1504 ;; dimension strings.
1506 (defun d-matrix (linear? direction h d)
1507 (d-vbar linear? h d (car (coerce (if (eq direction 'right)
1508 $rmxchar
1509 $lmxchar) 'list))))
1511 ;; There is wired knowledge of character offsets here.
1513 (defun d-box (linear? h d w body &aux (char 0) dmstr) ;char a char?
1514 (declare (ignore linear?))
1515 (setq char (car (coerce $boxchar 'list)))
1516 (setq dmstr `((0 ,h (d-hbar ,(+ 2 w) ,char))
1517 (,(- (+ w 2)) 0)
1518 (d-vbar ,h ,d ,char)
1519 ,@body
1520 (,(- (1+ w)) ,(- (1+ d)) (d-hbar ,(+ w 2) ,char))
1521 (-1 0)
1522 (d-vbar ,h ,d ,char)))
1523 (draw-linear dmstr oldrow oldcol))