~ubuntu-branches/ubuntu/karmic/maxima/karmic

« back to all changes in this revision

Viewing changes to interfaces/emacs/emaxima/emaxima.lisp

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-11-13 18:39:14 UTC
  • mto: (2.1.2 hoary) (3.2.1 sid) (1.1.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20041113183914-ttig0evwuatnqosl
Tags: upstream-5.9.1
ImportĀ upstreamĀ versionĀ 5.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
;;; This was stolen from imaxima, by Jesper Harder
2
 
;;; http://purl.org/harder/imaxima.tar.gz
3
 
 
4
1
(in-package "MAXIMA")
5
2
 
 
3
(declare-top
 
4
         (special lop rop $gcprint $inchar)
 
5
         (*expr tex-lbp tex-rbp))
 
6
 
6
7
(DEFUN MAIN-PROMPT ()
7
8
  (FORMAT () "(~A~D) "
8
9
    (STRIPDOLLAR $INCHAR) $LINENUM))
11
12
;  (declare (special $prompt))
12
13
;  (format nil "~A" (STRIPDOLLAR $PROMPT)))
13
14
 
14
 
(DEFMFUN DISPLA (FORM &aux #+kcl(form form))
15
 
  (IF (OR (NOT #.TTYOFF) #.WRITEFILEP)
16
 
      (cond #+Franz ($typeset (apply #'$photot (list form)))
17
 
            ((eq $display2d '$emaxima) (latex form))
18
 
            ($DISPLAY2D
19
 
             (LET ((DISPLAYP T)
20
 
                   (LINEARRAY (IF DISPLAYP (MAKE-array 80.) LINEARRAY))
21
 
                   (MRATP (CHECKRAT FORM))
22
 
                   (#.WRITEFILEP #.WRITEFILEP)
23
 
                   (MAXHT     1) (MAXDP   0) (WIDTH   0)
24
 
                   (HEIGHT    0) (DEPTH   0) (LEVEL   0) (SIZE   2)
25
 
                   (BREAK     0) (RIGHT   0) (LINES   1) BKPT
26
 
                   (BKPTWD    0) (BKPTHT  1) (BKPTDP  0) (BKPTOUT 0)
27
 
                   (BKPTLEVEL 0) IN-P
28
 
                   (MOREFLUSH D-MOREFLUSH)
29
 
                   MORE-^W
30
 
                   (MOREMSG D-MOREMSG))
31
 
               (UNWIND-PROTECT
32
 
                (PROGN (SETQ FORM (DIMENSION FORM
33
 
                                             NIL 'MPAREN 'MPAREN 0 0))
34
 
                       (CHECKBREAK FORM WIDTH)
35
 
                       (OUTPUT FORM (IF (AND (NOT $LEFTJUST) (= 2 LINES))
36
 
                                        (f- LINEL (f- WIDTH BKPTOUT))
37
 
                                        0))
38
 
                       (IF (AND SMART-TTY (NOT (AND SCROLLP (NOT $CURSORDISP)))
39
 
                                (> (CAR (CURSORPOS)) (f- TTYHEIGHT 3)))
40
 
                           (LET (#.writefilep) (MTERPRI))))
41
 
             ;; make sure the linearray gets cleared out.
42
 
             (CLEAR-LINEARRAY))))
43
 
            (T (LINEAR-DISPLA FORM)))))
44
 
 
45
15
(defun break-dbm-loop (at)
46
16
  (let* (
47
17
         (*quit-tags* (cons (cons *break-level* *quit-tag*) *quit-tags*))
87
57
         (restore-bindings)
88
58
        )))))
89
59
 
90
 
(setq $display2d 'true)
 
60
;(setq $display2d 'true)
91
61
 
92
62
;; TeX-printing
93
63
;; (c) copyright 1987, Richard J. Fateman
94
64
;; Small changes for interfacing with TeXmacs: Andrey Grozin, 2001
95
65
;; Yet more small changes for interfacing with imaxima: Jesper Harder 2001
96
 
 
97
 
(declare-top
98
 
         (special lop rop ccol $gcprint $inchar)
99
 
         (*expr tex-lbp tex-rbp))
100
 
(defconstant texport t)
101
 
 
102
 
;;; myprinc is an intelligent low level printing routine.  it keeps track of 
103
 
;;; the size of the output for purposes of allowing the TeX file to
104
 
;;; have a reasonable line-line. myprinc will break it at a space 
105
 
;;; once it crosses a threshold.
106
 
;;; this has nothign to do with breaking the resulting equations.
107
 
 
108
 
;-      arg:    chstr -  string or number to princ
109
 
;-      scheme: This function keeps track of the current location
110
 
;-              on the line of the cursor and makes sure
111
 
;-              that a value is all printed on one line (and not divided
112
 
;-              by the crazy top level os routines)
113
 
 
114
 
(defun myprinc (chstr)
115
 
       (prog (chlst) 
116
 
              (cond ((greaterp (plus (length (setq chlst (exploden chstr)))
117
 
                                 ccol)
118
 
                           70.)
119
 
                  (terpri texport)      ;would have exceeded the line length
120
 
                      (setq ccol 1.)
121
 
                      (myprinc " ")   ; lead off with a space for safety
122
 
                      )) ;so we split it up.
123
 
             (do ((ch chlst (cdr ch))
124
 
                  (colc ccol (add1 colc)))
125
 
                 ((null ch) (setq ccol colc))
126
 
                 (tyo (car ch) texport))))
127
 
 
128
 
(defun myterpri nil
129
 
  (cond (texport (terpri texport))
130
 
        (t (mterpri)))
131
 
        (setq ccol 1))
 
66
;; Even more small changes for emaxima: Jay Belanger 2003
 
67
 
132
68
 
133
69
(defun tex (x l r lop rop)
134
70
        ;; x is the expression of interest; l is the list of strings to its
190
126
     ((eql l 1) (myquote pname))
191
127
     (t (concatenate 'string "\\mathrm{" (myquote pname) "}")))))
192
128
 
193
 
(defun texnumformat(atom)  ;; 10/14/87 RJF  convert 1.2e20 to 1.2 \cdot 10^{20}
194
 
  (let(r firstpart exponent)
195
 
       (cond ((integerp atom)atom)
196
 
             (t (setq r (explode atom))
197
 
                (setq exponent (memq 'e r)) ;; is it ddd.ddde+EE
198
 
                (cond ((null exponent) atom); it is not. go with it as given
199
 
                      (t (setq firstpart (nreverse (cdr (memq 'e (reverse r)))))
200
 
                         (strcat (apply #'strcat firstpart )
201
 
                                         "\\cdot 10^{"
202
 
                                         (apply #'strcat (cdr exponent))
203
 
                                         "}")))))))
 
129
(defun strcat (&rest args)
 
130
  (apply #'concatenate 'string (mapcar #'string args)))
 
131
 
 
132
;; 10/14/87 RJF  convert 1.2e20 to 1.2 \cdot 10^{20}
 
133
;; 03/30/01 RLT  make that 1.2 \times 10^{20}
 
134
(defun texnumformat(atom)
 
135
  (let (r firstpart exponent)
 
136
    (cond ((integerp atom)
 
137
           atom)
 
138
          (t
 
139
           (setq r (explode atom))
 
140
           (setq exponent (member 'e r :test #'string-equal));; is it ddd.ddde+EE
 
141
           (cond ((null exponent)
 
142
                   ;; it is not. go with it as given
 
143
                  atom)
 
144
                 (t
 
145
                  (setq firstpart
 
146
                        (nreverse (cdr (member 'e (reverse r) :test #'string-equal))))
 
147
                  (strcat (apply #'strcat firstpart )
 
148
                          " \\times 10^{"
 
149
                          (apply #'strcat (cdr exponent))
 
150
                          "}")))))))
204
151
 
205
152
(defun tex-paren (x l r) 
206
153
  (tex x (append l '("\\left(")) (cons "\\right)" r) 'mparen 'mparen))
211
158
           (setq f (cadr x) 
212
159
                 x (cdr x))
213
160
           (setq f (caar x)))
214
 
       (setq l (tex (texword f) l nil lop 'mfunction)
215
 
             
216
 
             r (nconc (tex-list (cdr x) nil (list "}") ",") r)) 
217
 
       (nconc l (list "_{") r  )))
 
161
       (if (atom (cadr x))
 
162
           ;; subscript is an atom -- don't use \isubscript
 
163
           (progn
 
164
             (setq l (tex (texword f) l nil lop 'mfunction)
 
165
                   r (nconc (tex-list (cdr x) nil (list "}") ",") r))
 
166
             (nconc l (list "_{") r))
 
167
         (setq l (tex (texword f) (append l (list "\\isubscript{"))  nil lop 'mfunction)
 
168
               r (nconc (tex-list (cdr x) nil (list "}") ",") r))
 
169
         (nconc  l (list "}{") r ))))
218
170
 
219
171
;; we could patch this so sin x rather than sin(x), but instead we made sin a prefix
220
172
;; operator
233
185
          ((null (cdr x))
234
186
           (setq nl (nconc nl (tex (car x)  l r 'mparen 'mparen)))
235
187
           nl)
236
 
;         (setq nl (nconc nl (tex (car x)  l (list sym) 'mparen 'mparen))
237
 
          (setq nl (nconc nl (tex (car x)  l (list (concat sym "\\linebreak[0]")) 'mparen 'mparen))
238
 
                  x (cdr x) 
 
188
;;        (setq nl (nconc nl (tex (car x)  l (list sym) 'mparen 'mparen))
 
189
          (setq nl (nconc nl (tex (car x)  l (list (concatenate 'string sym "\\linebreak[0]")) 'mparen 'mparen))
 
190
                  x (cdr x)
239
191
                  l nil))))
240
192
 
241
193
(defun tex-prefix (x l r)
379
331
     ;; which should be sin^2 x rather than (sin x)^2 or (sin(x))^2. 
380
332
     ;; yet we must not display (a+b)^2 as +^2(a,b)...
381
333
     ;; or (sin(x))^(-1) as sin^(-1)x, which would be arcsine x
382
 
     (cond ;; this whole clause
383
 
           ;; should be deleted if this hack is unwanted and/or the
 
334
    (cond ;; this whole clause
 
335
     ;; should be deleted if this hack is unwanted and/or the
384
336
           ;; time it takes is of concern.
385
337
           ;; it shouldn't be too expensive.
386
338
           ((and (eq (caar x) 'mexpt) ; don't do this hack for mncexpt
400
352
                              ; inverse of f, if written f^-1 x
401
353
                              ; what else? f(x)^(1/2) is sqrt(f(x)), ??
402
354
                  (cond (doit
403
 
                        (setq l (tex `((mexpt) ,f ,expon) l nil 'mparen 'mparen))
404
 
                        (setq r (tex
405
 
                                 (if (and (null (cdr bascdr)) (eq (get f 'tex) 'tex-prefix))
406
 
                                     (car bascdr) (cons '(mprogn) bascdr))
407
 
                                 nil r f rop)))
408
 
                        (t nil))))) ; won't doit. fall through
409
 
      (t (setq l (tex (cadr x) l nil lop (caar x))
410
 
               r (if (mmminusp (setq x (nformat (caddr x))))
411
 
                    ;; the change in base-line makes parens unnecessary
412
 
                    (if nc
413
 
                        (tex (cadr x) '("^ {-\\langle ")(cons "\\rangle }" r) 'mparen 'mparen)
414
 
                        (tex (cadr x) '("^ {- ")(cons " }" r) 'mparen 'mparen))
415
 
                    (if nc
416
 
                        (tex x (list "^{\\langle ")(cons "\\rangle}" r) 'mparen 'mparen)
417
 
                        (tex x (list "^{")(cons "}" r) 'mparen 'mparen))))))
418
 
      (append l r)))
 
355
                         (setq l (append (tex f l nil lop 'mexpt)
 
356
                                        (tex expon (list "^{")
 
357
                                             (cons " }" nil) 'mparen 'mparen)))
 
358
                         (if (and (null (cdr bascdr))
 
359
                                  (eq (get f 'tex) 'tex-prefix))
 
360
                             (setq r (tex (car bascdr) nil r f 'mparen))
 
361
                           (setq r (tex (cons '(mprogn) bascdr) nil r 'mparen 'mparen)))
 
362
                         (append l r))
 
363
                        (t nil))))) ; won't doit. fall through
 
364
           (t
 
365
            (if (atom (caddr x))
 
366
                ;; Don't use \iexpt when exponent is an atom
 
367
                (progn
 
368
                  (setq l (tex (cadr x) l nil lop (caar x))
 
369
                        r (if (mmminusp (setq x (nformat (caddr x))))
 
370
                              ;; the change in base-line makes parens unnecessary
 
371
                              (if nc
 
372
                                  (tex (cadr x) '("^ {-\\langle ")(cons "\\rangle }" r) 'mparen 'mparen)
 
373
                                (tex (cadr x) '("^ {- ")(cons " }" r) 'mparen 'mparen))
 
374
                            (if nc
 
375
                                (tex x (list "^{\\langle ")(cons "\\rangle}" r) 'mparen 'mparen)
 
376
                              (tex x (list "^{")(cons "}" r) 'mparen 'mparen))))
 
377
                  (append l r))
 
378
              (setq l (tex (cadr x) (append l (list "\\iexpt{")) nil lop (caar x))
 
379
                  r (if (mmminusp (setq x (nformat (caddr x))))
 
380
                        ;; the change in base-line makes parens unnecessary
 
381
                        (if nc
 
382
                            (tex (cadr x) '("{-\\langle ")(cons "\\rangle }" r) 'mparen 'mparen)
 
383
                          (tex (cadr x) '("{- ")(cons " }" r) 'mparen 'mparen))
 
384
                      (if nc
 
385
                          (tex x (list "{\\langle ")(cons "\\rangle}" r) 'mparen 'mparen)
 
386
                        (tex x (list "{") (cons "}" r) 'mparen 'mparen))))
 
387
                 (append l (list "}") r))))))
419
388
 
420
389
(defprop mncexpt tex-mexpt tex)
421
390
 
452
421
 
453
422
(defun tex-mquotient (x l r)
454
423
  (if (or (null (cddr x)) (cdddr x)) (wna-err (caar x)))
455
 
  (setq l (tex (cadr x) (append l '("{{")) nil 'mparen 'mparen)
456
 
        ;the divide bar groups things
457
 
        r (tex (caddr x) (list "}\\over{") (append '("}}")r) 'mparen 'mparen))
 
424
  (cond ((and (atom (cadr x)) (atom (caddr x)))
 
425
         ;; both denom and numerator are atoms
 
426
         (setq l (tex (cadr x) (append l '("\\frac{")) nil nil nil) ;;fixme
 
427
               r (tex (caddr x) (list "}{") (append '("}")r) 'mparen 'mparen)))
 
428
        ((atom (cadr x))
 
429
         ;; numerator is an atom
 
430
         (setq l (tex (cadr x) (append l '("\\frac{")) nil 'mparen 'mparen)
 
431
               r (tex (caddr x) (list "}{") (append '("}")r) 'mparen 'mparen)))
 
432
        ((atom (caddr x))
 
433
         ;; denom is an atom
 
434
         (setq l (tex (cadr x) (append l '("\\frac{")) nil 'mparen 'mparen)
 
435
               r (tex (caddr x) (list "}{") (append '("}")r) 'mparen 'mparen)))
 
436
        (t
 
437
         ;; neither are atoms
 
438
         (setq l (tex (cadr x) (append l '("\\frac{")) nil 'mparen 'mparen)
 
439
               r (tex (caddr x) (list "}{") (append '("}")r) 'mparen 'mparen))))
458
440
  (append l r))
459
441
 
460
442
(defprop $matrix tex-matrix tex)
527
509
        (sub (tex (caddr x) nil nil 'mparen 'mparen)))
528
510
       (append l '("\\left.") s1  '("\\right|_{") sub '("}") r)))
529
511
 
530
 
;; (defprop mbox tex-mbox tex)
531
 
 
532
 
;; (defun tex-mbox (x l r)
533
 
;;   (append l '("\\fbox{") (tex (cadr x) nil nil 'mparen 'mparen) '("}"))) ; jh
 
512
(defprop mbox tex-mbox tex)
 
513
 
 
514
(defun tex-mbox (x l r)
 
515
  (append l '("\\boxed{") (tex (cadr x) nil nil 'mparen 'mparen) '("}") r))
 
516
 
 
517
(defprop mlabox tex-mlabox tex)
 
518
 
 
519
(defun tex-mlabox (x l r)
 
520
   (append l '("\\stackrel{") (tex (caddr x) nil nil 'mparen 'mparen)
 
521
           '("}{\\boxed{") (tex (cadr x) nil nil 'mparen 'mparen) '("}}") r)) ; jh
534
522
 
535
523
;;binomial coefficients
536
524
 
538
526
           
539
527
(defun tex-choose (x l r)
540
528
  `(,@l 
541
 
    "\\pmatrix{" 
 
529
    "{" 
542
530
    ,@(tex (cadr x) nil nil 'mparen 'mparen)
543
 
    "\\\\"
 
531
    "\\choose "
544
532
    ,@(tex (caddr x) nil nil 'mparen 'mparen)
545
533
    "}"
546
534
    ,@r))
647
635
     (%asin "\\arcsin ")
648
636
     (%acos "\\arccos ")
649
637
     (%atan "\\arctan ")
 
638
     (%acot "\\operatorname{arccot}")
 
639
     (%asec "\\operatorname{arcsec}")
 
640
     (%acsc "\\operatorname{arccsc}")
650
641
     (%sinh "\\sinh ")
651
642
     (%cosh "\\cosh ")
652
643
     (%tanh "\\tanh ")
653
644
     (%coth "\\coth ")
654
 
     (%sech "{\\rm sech}") ;; jah
 
645
     (%sech "\\operatorname{sech}")          
 
646
     (%csch "\\operatorname{csch}")
 
647
     (%asinh "\\operatorname{arcsinh}")
 
648
     (%acosh "\\operatorname{arccosh}")
 
649
     (%atanh "\\operatorname{arctanh}")
 
650
     (%acoth "\\operatorname{arccoth}")
 
651
     (%asech "\\operatorname{arcsech}")
 
652
     (%acsch "\\operatorname{arccsch}")
 
653
     (%determinant "\\det ")     
655
654
     (%ln "\\ln ")
656
655
     (%log "\\log ")
657
656
    ;; (%erf "{\\rm erf}") this would tend to set erf(x) as erf x. Unusual
677
676
    (difflist (cddr x)) ;; list of derivs e.g. (x 1 y 2)
678
677
    (ords (odds difflist 0)) ;; e.g. (1 2)
679
678
    (vars (odds difflist 1)) ;; e.g. (x y)
680
 
    (numer `((mexpt) $|d| ((mplus) ,@ords))) ; d^n numerator
 
679
    (numer `((mexpt) ,dsym ((mplus) ,@ords))) ; d^n numerator
681
680
    (denom (cons '(mtimes)
682
681
                 (mapcan #'(lambda(b e)
683
682
                                  `(,dsym ,(simplifya `((mexpt) ,b ,e) nil)))
733
732
         `("\\mathbf{do}" ,(eighth x))))
734
733
 
735
734
(defun texmdoin (x)
736
 
  (nconc `("\\mathbf{for}" ,(second x) $|in| ,(third x))
 
735
  (nconc `("\\mathbf{for}" ,(second x) "\\mathbf{in}" ,(third x))
737
736
         (cond ((sixth x) `("\\mathbf{thru}" ,(sixth x))))
738
737
         (cond ((null (seventh x)) nil)
739
738
               ((eq 'mnot (caar (seventh x)))
770
769
  (append l (list "\\verb|" (make-string (cadr x) :initial-element #\space) "|") r))
771
770
 
772
771
; jh: verb & mbox
 
772
(defun qndispla (form)
 
773
  (let (($display2d nil))
 
774
    (displa form)))
773
775
 
774
776
(defun latex (x)
775
 
  (let ((ccol 1))
776
 
    (mapc #'myprinc
777
 
          (if (and (listp x) (cdr x) (stringp (cadr x))
778
 
                   (equal (string-right-trim '(#\Space) (cadr x)) "Is"))
779
 
              (tex x '("") '("") 'mparen 'mparen)
780
 
            (tex x '("") '("
781
 
") 'mparen 'mparen)))
782
 
    ))
 
777
;;  (princ x)  ;; uncomment to debug.
 
778
  (mapc #'princ
 
779
        (if (and (listp x) (cdr x)
 
780
                 (equal (string-right-trim '(#\Space) (cadr x)) "Is"))
 
781
           (qndispla x)      ;(tex x '("") '("") 'mparen 'mparen)
 
782
          (tex x '("") '("
 
783
") 'mparen 'mparen))))
 
784
 
 
785
(let ((old-displa (symbol-function 'displa)))
 
786
  (defun displa (form)
 
787
    (if (eq $display2d '$emaxima)
 
788
        (latex form)
 
789
      (funcall old-displa form))))
 
790