1 ;; This file is part of GNU Emacs.
2 ;; Copyright (C) 1998 William F. Schelter
4 ;; GNU Emacs is distributed in the hope that it will be useful, but
5 ;; WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
6 ;; to anyone for the consequences of using it or for whether it serves
7 ;; any particular purpose or works at all, unless he says so in writing.
8 ;; Refer to the GNU Emacs General Public License for full details.
10 ;; Everyone is granted permission to copy, modify and redistribute GNU
11 ;; Emacs, but only under the conditions described in the GNU Emacs
12 ;; General Public License. A copy of this license is supposed to have
13 ;; been given to you along with GNU Emacs so you can know your rights and
14 ;; responsibilities. It should be in a file named COPYING. Among other
15 ;; things, the copyright notice and this notice must be preserved on all
19 ;; Maxima mode by W. Schelter Code for running maxima under something
20 ;; like shell mode (inferior-maxima-mode). A mode for editing a buffer
21 ;; of maxima code is called maxima-mode.
24 (provide 'maxima-mode
)
25 (defvar maxima-mode-syntax-table nil
"")
26 (defvar maxima-mode-abbrev-table nil
"")
27 (if (not maxima-mode-syntax-table
)
29 (setq maxima-mode-syntax-table
(make-syntax-table))
31 (modify-syntax-entry i
"_ " maxima-mode-syntax-table
)
35 (modify-syntax-entry i
"_ " maxima-mode-syntax-table
)
39 (modify-syntax-entry i
"_ " maxima-mode-syntax-table
)
43 (modify-syntax-entry i
"_ " maxima-mode-syntax-table
)
45 (modify-syntax-entry ?
" " maxima-mode-syntax-table
)
46 (modify-syntax-entry ?
\t " " maxima-mode-syntax-table
)
47 (modify-syntax-entry ?
` "' " maxima-mode-syntax-table
)
48 (modify-syntax-entry ?
' "' " maxima-mode-syntax-table
)
49 (modify-syntax-entry ?
, "' " maxima-mode-syntax-table
)
50 (modify-syntax-entry ?.
"' " maxima-mode-syntax-table
)
51 (modify-syntax-entry ?
# "' " maxima-mode-syntax-table
)
52 (modify-syntax-entry ?
\\ "\\" maxima-mode-syntax-table
)
53 (modify-syntax-entry ?
/ ". 14" maxima-mode-syntax-table
)
54 (modify-syntax-entry ?
* ". 23" maxima-mode-syntax-table
)
55 (modify-syntax-entry ?
+ "." maxima-mode-syntax-table
)
56 (modify-syntax-entry ?-
"." maxima-mode-syntax-table
)
57 (modify-syntax-entry ?
= "." maxima-mode-syntax-table
)
58 (modify-syntax-entry ?%
"." maxima-mode-syntax-table
)
59 (modify-syntax-entry ?
< "." maxima-mode-syntax-table
)
60 (modify-syntax-entry ?
> "." maxima-mode-syntax-table
)
61 (modify-syntax-entry ?
& "." maxima-mode-syntax-table
)
62 (modify-syntax-entry ?|
"." maxima-mode-syntax-table
)
63 (modify-syntax-entry ?
\" "\" " maxima-mode-syntax-table
)
64 (modify-syntax-entry ?
\\ "\\ " maxima-mode-syntax-table
)
65 (modify-syntax-entry ?\
( "() " maxima-mode-syntax-table
)
66 (modify-syntax-entry ?\
) ")( " maxima-mode-syntax-table
)
67 (modify-syntax-entry ?\
[ "(] " maxima-mode-syntax-table
)
68 (modify-syntax-entry ?\
] ")[ " maxima-mode-syntax-table
)
71 (defun maxima-mode-variables ()
72 (set-syntax-table maxima-mode-syntax-table
)
73 (setq local-abbrev-table maxima-mode-abbrev-table
)
74 (make-local-variable 'paragraph-start
)
75 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
76 (make-local-variable 'paragraph-separate
)
77 (setq paragraph-separate paragraph-start
)
78 (make-local-variable 'indent-line-function
)
79 (setq indent-line-function
'maxima-indent-line
)
80 (make-local-variable 'comment-start
)
81 (setq comment-start
"/* ")
82 (make-local-variable 'comment-start-skip
)
83 (setq comment-start-skip
"/\*+ *")
84 (make-local-variable 'comment-column
)
85 (setq comment-column
40)
86 (make-local-variable 'maxima-process
))
89 (defun maxima-mode-commands (map)
90 (define-key map
"\e\C-q" 'indent-maxima-sexp
)
91 (define-key map
"\177" 'backward-delete-char-untabify
)
92 (define-key map
"\t" 'maxima-indent-line
)
93 (define-key map
"\C-j" 'maxima-newline-and-indent
)
94 (define-key map
"\C-x " 'maxima-add-breakpoint
)
95 (define-key map
"\e\C-h" 'mark-maxima-form
)
99 (defun maxima-add-breakpoint ()
102 (let ((buf (current-buffer)) name
(at (point)) found
(looking t
))
104 (beginning-of-maxima-form)
106 (and (re-search-forward "\\b\\([a-z_A-Z0-9]+\\)[ \t\n]*(" nil t
)
108 (not (or (is-sharp-comment-line) (in-maxima-comment-p))))
112 (setq name
(buffer-substring (match-beginning 1) (match-end 1)))
113 (setq line
(count-lines (match-beginning 1) at
))
114 (send-string (get-maxima-process)
115 (concat ":br " name
" " line
"\n"))))
119 (defun get-maxima-process ()
121 (cond ((and (boundp 'maxima-process
) maxima-process
))
122 ((setq proc
(get-buffer-process (current-buffer)))
123 (setq maxima-process proc
))
126 (cond ((setq proc
(get-buffer-process (current-buffer)))
127 (setq maxima-process proc
))
128 (t (error "cant find the maxima process"))))))))
132 (defun in-maxima-comment-p ()
136 (cond ((search-backward "/" (- (point) 2000) t
)
137 (cond ((looking-at "/\\*")
138 (setq result t ok nil
))
139 ((bobp) (setq ok nil
))
140 ((progn (forward-char -
1)
146 (defvar maxima-mode-map
())
150 (setq maxima-mode-map
(make-sparse-keymap))
151 (define-key maxima-mode-map
"\e\C-x" 'maxima-send-maxima-form-compile
)
152 (maxima-mode-commands maxima-mode-map
))
154 (defun maxima-mode ()
155 "Major mode for editing Maxima code.
157 Tab indents the current line relative to previous lines.
158 Linefeed (c-j) enters a new line and the indents appropriately.
159 Blank lines separate paragraphs. Comments are delimited by /* and */
161 Entry to this mode calls the value of maxima-mode-hook
162 if that value is non-nil."
164 (kill-all-local-variables)
165 (use-local-map maxima-mode-map
)
166 (setq major-mode
'maxima-mode
)
167 (setq mode-name
"Maxima")
168 (maxima-mode-variables)
169 (run-hooks 'maxima-mode-hook
))
171 ;; This will do unless sshell.el is loaded.
175 (defun maxima-indent-line (&optional whole-exp
)
176 "Indent current line as Maxima code.
177 With argument, indent any additional lines of the same expression
178 rigidly along with this one."
180 (let ((indent (calculate-maxima-indent)) shift-amt beg end
181 (pos (- (point-max) (point))))
185 (skip-chars-forward " \t")
186 (if (looking-at "[ \t]*/\\*")
187 ;; Don't alter indentation of a ;;; comment line.
189 (if (listp indent
) (setq indent
(car indent
)))
190 (setq shift-amt
(- indent
(current-column)))
191 (if (zerop shift-amt
)
193 (delete-region beg
(point))
195 ;; If initial point was within line's indentation,
196 ;; position after the indentation. Else stay at same point in text.
197 (if (> (- (point-max) pos
) (point))
198 (goto-char (- (point-max) pos
)))
199 ;; If desired, shift remaining lines of expression the same amount.
200 (and whole-exp
(not (zerop shift-amt
))
209 (indent-code-rigidly beg end shift-amt
)))))
211 (defun forward-over-comment-whitespace ()
215 (skip-chars-forward " \t\f")
216 (cond ((looking-at "/\\*")
217 (search-forward "*/" nil t
)
219 ((looking-at "\n[\t ]*#")
224 (t (setq ok nil
))))))
228 (defun back-over-comment-whitespace ()
231 (skip-chars-backward " \t\n\f")
234 (cond ((looking-at "\\*/")
235 (search-backward "/*" )
237 (t (forward-char 2) (setq ok nil
))))))
239 (defun calculate-maxima-indent (&optional parse-start
)
240 "Return appropriate indentation for current line as Maxima code.
241 In usual case returns an integer: the column to indent to.
245 (let ((indent-point (point))
247 ;; setting this to a number inhibits calling hook
250 indent-col begin-char
251 last-sexp containing-sexp
)
254 (cond ((looking-at "[a-zA-Z]")
256 ((progn (skip-chars-forward " \t")
257 (setq begin-char
(char-after (point)))
260 (re-search-backward "then")
261 (setq indent-col
(current-column)))
263 (back-over-comment-whitespace)
266 ((looking-at "[a-zA-Z]")
269 (skip-chars-forward " \t")
270 (let ((col (current-column))col1
273 (back-over-comment-whitespace)
275 (cond ((looking-at "[,]")
276 (let ((pt (begin-paren)))
279 (setq indent-col
(+ (current-column) 1))
281 (t (setq indent-col
2)))
283 (cond ((looking-at "BLOCK(")
285 (+ (current-column) 2))))))
286 ((or (looking-at "[*---+]")
287 (memq begin-char
'(?\
* ?\
+ ?\-
)))
288 (cond ((looking-at "[])]")
290 (setq indent-col
(begin-paren-column)))
292 (setq indent-col
(current-column)))
295 (setq indent-col
(begin-paren-column)))
296 (t (beginning-of-line)
297 (skip-chars-forward " \t")
298 (setq indent-col
(+ (current-column) 2))
300 ; (message "col %d" indent-col )
303 (defun begin-paren-column ()
305 (let ((pt (begin-paren)))
306 (if pt
(progn (goto-char pt
) (current-column))
309 (defun maxima-newline-and-indent ()
313 (maxima-indent-line))
315 (defvar maxima-paren-nest
(make-vector 20 nil
))
317 ;(defun fo ()(interactive) (save-excursion
318 ; (goto-char(begin-paren)) (sit-for 1)))
321 (defun begin-paren ()
323 (let ((ok t
)(level 0)(here (point)) last
(max 0))
325 (cond ((re-search-backward "[;$]" nil t
)
326 (skip-chars-forward ";$ \t\n"))
327 (t (goto-char (point-min))
328 (forward-over-comment-whitespace)))
331 (cond ((re-search-forward "[][()]" here t
)
332 (setq max
(max level max
))
333 (let ((ch (char-after (- (point) 1))))
334 (cond ((memq ch
'( ?
( ?
[)) ;)
335 (setq level
(+ level
1))
336 (aset maxima-paren-nest level
(1- (point))))
338 (setq level
(- level
1))))))
340 (aref maxima-paren-nest level
)
343 ;(setq stack-trace-on-error t)
348 ;; (put 'progn 'maxima-indent-hook 0), say, causes progn to be indented
349 ;; like defun if the first form is placed on the next line, otherwise
350 ;; it is indented like any other form (i.e. forms line up under first).
353 ;; Of course this should be done in one pass but..
354 (defun indent-maxima-sexp ()
357 (let ((beg (progn (beginning-of-maxima-form)
359 (end (progn (end-of-maxima-form) (point))))
361 (while (< (point) end
)
368 (defun is-sharp-comment-line ()
371 (looking-at "[ \t]*#")))
375 (defun beginning-of-maxima-form ()
376 (let ((tem (re-search-backward "[;$]" (point-min) t
)))
378 (cond ((in-maxima-comment-p)
379 (search-backward "/\\*" nil t
)
380 (beginning-of-maxima-form))
381 ((is-sharp-comment-line)
383 (or (equal (point) (point-min)) (forward-char -
1))
384 (beginning-of-maxima-form))
385 (t (forward-char 1))))
386 (t (goto-char (point-min))))
387 (forward-over-comment-whitespace)))
389 (defun end-of-maxima-form ()
390 (let ((tem (re-search-forward "[;$]" (point-max) t
)))
392 (cond ((in-maxima-comment-p)
393 (search-forward "*/" nil t
)
394 (end-of-maxima-form))
395 ((is-sharp-comment-line)
396 (end-of-line) (forward-char 1)
400 (t (error "No ; or $ at end ")))))
404 (make-sshell "maxima" "maxima")
405 (switch-to-buffer "*maxima*")
406 (inferior-maxima-mode))
408 (defun mark-maxima-form ()
410 (beginning-of-maxima-form)
416 (defun maxima-send-maxima-form-compile (arg)
417 "Send the current maxima-form to the maxima-process and compile it if there
418 is a numeric arg and (compile 'foo) is understood by maxima-process The
419 value of maxima-process will be the process of the other exposed window
420 if there is one or else the global value of maxima-process. If the
421 ...received message is not received, probably either the reading of
422 the form c form caused an error, or time-to-throw-away needs increasing."
425 (let* ((proc (get-maxima-process))
427 (this-maxima-process proc
)
428 (maxima-buffer (process-buffer this-maxima-process
))
433 (let ((end (dot)) (buffer (current-buffer))
434 (proc (get-process this-maxima-process
)))
435 (setq maxima-process proc
)
437 (beginning-of-maxima-form)
438 (cond ((equal (char-after (1- end
)) ?
\n)
439 (setq end
(1- end
)) ))
440 (setq bill
(buffer-substring (dot) end
))
441 (send-region this-maxima-process
(dot) end
)))
442 ; (send-string this-maxima-process
443 ; (concat ";;end of form" "\n" telnet-new-line))
444 (cond ((string-match "maxima" (buffer-name (process-buffer proc
)))
445 (send-string proc
"\n")
448 (cond ((eq (window-buffer) maxima-buffer
)
449 (forward-char (- (dot-max) (dot)))))
452 ; (send-string this-maxima-process telnet-new-line)
453 (send-string this-maxima-process
"\n")
454 (and (boundp 'time-to-throw-away
) time-to-throw-away
(dump-output proc time-to-throw-away
))
455 (set-buffer maxima-buffer
)
456 (set-window-dot (get-buffer-window maxima-buffer
) (dot-max))))))
459 (defun maxima-send-maxima-form-compile (arg)
460 "Send the current maxima-form to the maxima-process and compile it if there
461 is a numeric arg and (compile 'foo) is understood by maxima-process The
462 value of maxima-process will be the process of the other exposed window
463 if there is one or else the global value of maxima-process. If the
464 ...received message is not received, probably either the reading of
465 the form c form caused an error, or time-to-throw-away needs increasing."
469 (let* ((proc (or (get-buffer-process (current-buffer)) maxima-process
))
472 (this-maxima-process proc
)
473 (maxima-buffer (process-buffer this-maxima-process
))
475 (setq maxima-process proc
)
479 (let ((end (point)) (buffer (current-buffer))
480 (proc (get-process this-maxima-process
)))
481 (setq maxima-process proc
)
483 (beginning-of-maxima-form)
484 (setq filename
(buffer-file-name buffer
))
485 (setq line-number
(count-lines (point-min) (point)))
486 (setq bill
(buffer-substring (point) end
))
487 (setq com
(concat ":l (setq *mread-prompt* \"\")(princ #\\newline) (values) \n"
488 " \n# " line-number
" \"" filename
"\"\n"
491 (send-string this-maxima-process com
)
492 ;(set-buffer maxima-buffer)
495 ;(message (concat "at " (point-max)))
496 ;(goto-char (point-max))
503 (defvar inferior-maxima-mode-map nil
)
505 (if inferior-maxima-mode-map
507 (setq inferior-maxima-mode-map
(copy-alist sshell-mode-map
))
508 ;(maxima-mode-commands inferior-maxima-mode-map)
509 (define-key inferior-maxima-mode-map
"\e\C-x" 'maxima-send-defun
))
511 (defvar inferior-maxima-prompt
512 "\\(^([C][0-9]*) \\)\\|\\(^[^#%)>]*[#%)>]+ *\\)"
513 "*Regexp to recognize prompts from the inferior Maxima or lisp
516 (defun inferior-maxima-mode ()
517 "Major mode for interacting with an inferior Maxima process.
519 The following commands are available:
520 \\{inferior-maxima-mode-map}
522 Entry to this mode calls the value of maxima-mode-hook with no arguments,
523 if that value is non-nil. Likewise with the value of sshell-mode-hook.
524 maxima-mode-hook is called after sshell-mode-hook.
526 You can send text to the inferior Maxima from other buffers
527 using the commands process-send-region, process-send-string
528 and \\[maxima-send-defun].
531 Delete converts tabs to spaces as it moves back.
532 Tab indents for Maxima; with argument, shifts rest
533 of expression rigidly with the current line.
534 Meta-Control-Q does Tab on each line starting within following expression.
535 Paragraphs are separated only by blank lines.
537 Return at end of buffer sends line as input.
538 Return not at end copies rest of line to end and sends it.
539 C-d at end of buffer sends end-of-file as input.
540 C-d not at end or with arg deletes or kills characters.
541 C-u and C-w are kill commands, imitating normal Unix input editing.
542 C-c interrupts the sshell or its current subjob if any.
543 C-z stops, likewise. C-\\ sends quit signal, likewise.
545 C-x C-k deletes last batch of output from sshell.
546 C-x C-v puts top of last batch of output at top of window."
548 (kill-all-local-variables)
549 (setq major-mode
'inferior-maxima-mode
)
550 (setq mode-name
"Inferior Maxima")
551 (setq mode-line-process
'(": %s"))
552 (maxima-mode-variables)
553 (use-local-map inferior-maxima-mode-map
)
554 (make-local-variable 'last-input-start
)
555 (setq last-input-start
(make-marker))
556 (make-local-variable 'last-input-end
)
557 (setq last-input-end
(make-marker))
558 (make-local-variable 'sshell-prompt-pattern
)
559 (setq sshell-prompt-pattern inferior-maxima-prompt
)
560 (run-hooks 'sshell-mode-hook
'maxima-mode-hook
))