1 ;;; maxima.el --- Major modes for writing Maxima code
3 ;; Copyright (C) 1998,1999 William F. Schelter
4 ;; Copyright (C) 2001-2007 Jay Belanger
6 ;; Author: William F. Schelter
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2 of
14 ;; the License, or (at your option) any later version.
16 ;; This program is distributed in the hope that it will be
17 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
18 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 ;; PURPOSE. See the GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public
22 ;; License along with this program; if not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 ;; You will need both maxima.el and maxima-font-lock.el
30 ;; This is a branch of William Schelter's maxima-mode.el
34 ;; To install, put this file (as well as maxima-font-lock.el)
35 ;; somewhere in your emacs load path.
36 ;; To make sure that `maxima.el' is loaded when necessary, whether to
37 ;; edit a file in maxima mode or interact with Maxima in an Emacs buffer,
39 ;; (autoload 'maxima-mode "maxima" "Maxima mode" t)
40 ;; (autoload 'maxima "maxima" "Maxima interaction" t)
41 ;; in your `.emacs' file. If you want any file ending in `.mac' to begin
42 ;; in `maxima-mode', for example, put the line
43 ;; (setq auto-mode-alist (cons '("\\.mac" . maxima-mode) auto-mode-alist))
44 ;; to your `.emacs' file.
46 ;; In any of the Maxima modes, to get help on a prompted for Maxima topic,
51 ;; To get help with the symbol under point, use ("d" for describe):
55 ;; For apropos help, use:
59 ;; To get apropos with the symbol under point, use:
63 ;; To read the Maxima info manual, use:
69 ;; (For Maxima minor mode, replace C-cC-d by C-c=d.)
72 ;; To put the current buffer into maxima-mode, type M-x maxima-mode
74 ;; Maxima mode provides the following motion commands:
75 ;; M-C-a: Move to the beginning of the form.
76 ;; M-C-e: Move to the end of the form.
77 ;; M-C-b: Move to the beginning of the list.
78 ;; M-C-f: Move to the end of the list.
80 ;; and the following miscellaneous commands.
81 ;; M-h: Mark the current form
82 ;; C-c): Check the current region for balanced parentheses.
83 ;; C-cC-): Check the current form for balanced parentheses.
85 ;; Maxima mode has the following completions command:
86 ;; M-TAB: Complete the Maxima symbol as much as possible, providing
87 ;; a completion buffer if there is more than one possible completion.
89 ;; Portions of the buffer can be sent to a Maxima process. (If a process is
90 ;; not running, one will be started.)
91 ;; C-cC-r: Send the region to Maxima.
92 ;; C-cC-b: Send the buffer to Maxima.
93 ;; C-cC-c: Send the line to Maxima.
94 ;; C-cC-e: Send the form to Maxima.
95 ;; C-RET: Send the smallest set of lines which contains
96 ;; the cursor and contains no incomplete forms, and go to the next form.
97 ;; M-RET: As above, but with the region instead of the current line.
98 ;; C-cC-l: Prompt for a file name to load into Maxima.
100 ;; When something is sent to Maxima, a buffer running an inferior Maxima
101 ;; process will appear. It can also be made to appear by using the command
103 ;; When a command is given to send information to Maxima, the region
104 ;; (buffer, line, form) is first checked to make sure the parentheses
105 ;; are balanced. With an argument, they will not be checked first.
106 ;; The Maxima process can be killed, after asking for confirmation
107 ;; with C-cC-k. To kill without confirmation, give C-cC-k
110 ;; By default, indentation will be to the same level as the
111 ;; previous line, with an additional space added for open parentheses.
112 ;; The behaviour of indent can be changed by the command
113 ;; M-x maxima-change-indent-style. The possibilities are:
114 ;; Standard: Simply indent
115 ;; Perhaps smart: Tries to guess an appropriate indentation, based on
116 ;; open parentheses, "do" loops, etc.
117 ;; The default can be set by setting the value of the variable
118 ;; "maxima-indent-style" to either 'standard or 'perhaps-smart.
119 ;; In both cases, M-x maxima-untab will remove a level of indentation.
121 ;; ** Maxima noweb mode **
122 ;; maxima-noweb-mode is a modification of maxima-mode that will work
123 ;; nicely with noweb-mode; namely, it will limit any relevant searches
124 ;; to the current chunk and treat <<...>> as word parts.
126 ;; ** Running Maxima interactively **
127 ;; To run Maxima interactively in a buffer, type M-x maxima
128 ;; In the Maxima process buffer,
129 ;; return will check the line for balanced parentheses, and send line as input.
130 ;; Control return will send the line as input without checking for balanced
133 ;; <M-tab> will complete the Maxima symbol as much as possible, providing
134 ;; a completion buffer if there is more than one possible completion.
136 ;; <C-M-tab> will complete the input line, based on previous input lines.
137 ;; C-c C-d will get help on a Maxima topic.
138 ;; C-c C-m will bring up the Maxima info manual.
139 ;; C-cC-k will kill the process and the buffer, after asking for
140 ;; confirmation. To kill without confirmation, give C-M-k an
143 ;; To scroll through previous commands,
144 ;; M-p will bring the previous input to the current prompt,
145 ;; M-n will bring the next input to the prompt.
146 ;; M-r will bring the previous input matching
147 ;; a regular expression to the prompt,
148 ;; M-s will bring the next input matching
149 ;; a regular expression to the prompt.
151 ;; ** Running Maxima from the minibuffer **
152 ;; The command M-x maxima-minibuffer
153 ;; will allow you to interact with Maxima from the minibuffer.
154 ;; The arrows will allow you to scroll through previous inputs.
156 ;; (autoload 'maxima-minibuffer "maxima" "Maxima in a minibuffer" t)
157 ;; in your .emacs will make sure the function is available.
158 ;; If the variable maxima-minibuffer-2d is non-nil, then the output
159 ;; will be in Maxima's 2d output form, otherwise it will be in
160 ;; Maxima's 1d output form. (For XEmacs, only the 1d form is available,
161 ;; since the minibuffer isn't resizable.)
162 ;; The command maxima-insert-last-output will insert
163 ;; the last maxima output into the current buffer; if the output is in 2d,
164 ;; this will look unpleasant. The command maxima-insert-last-output-tex
165 ;; will insert the TeX form of the output.
167 ;; ** Reading Maxima results in the minibuffer **
168 ;; The command `maxima-minibuffer-on-determined-region'
169 ;; will send the part of the current buffer containing the point and between
170 ;; the regexps `maxima-minor-prefix' and `maxima-minor-postfix' (currently
171 ;; both blank lines) to the Maxima process and insert the result in the
172 ;; minibuffer. With an argument, `maxima-minibuffer-in-determined-region'
173 ;; will also insert the output into the current buffer, after " ==> "
174 ;; and before "//". (The symbol ` ==> ' is the value of the customizable
175 ;; variable `maxima-minor-output' and "//" is the value of
176 ;; `maxima-minor-output-end'. The new output is inserted, these strings
177 ;; will be used to delete the old output.
178 ;; Outside of comments in maxima-mode, the opening and closing indicators
179 ;; are the values of `maxima-mode-minor-output' and
180 ;; `maxima-mode-minor-output-end', which by default are " /*==>" and
181 ;; " <==*/", respectively.
182 ;; The commands `maxima-minibuffer-on-region', `maxima-minibuffer-on-line'
183 ;; and `maxima-minibuffer-on-form' work similarly to
184 ;; `maxima-minibuffer-on-determined-region', but send the current region
185 ;; (respectively, the current line, current form) to Maxima and display
186 ;; the result in the minibuffer.
187 ;; (The form is the region between the preceding ; or $ and the subsequent
189 ;; Care must be taken when inserting the output into the current buffer
190 ;; with `maxima-minibuffer-on-region' and `maxima-minibuffer-on-form'.
191 ;; With `maxima-minibuffer-on-region', as with
192 ;; `maxima-minibuffer-on-determined-region' above, everything after any
193 ;; "==>" in the region will be ignored.
194 ;; What will typically happen with `maxima-minibuffer-on-region' and
195 ;; `maxima-minibuffer-on-form', however, is that new outputs will
196 ;; be inserted without old output being deleted.
198 ;; The commands for the Maxima-minibuffer interaction can be made
199 ;; available by putting
200 ;; (autoload 'maxima-minibuffer "maxima" "Interact with Maxima from the minibuffer" t)
201 ;; (autoload 'maxima-minibuffer-on-determined-region "maxima"
202 ;; "Send a information to Maxima, display the results in the minibuffer" t)
203 ;; (autoload 'maxima-minibuffer-on-region "maxima"
204 ;; "Send a information to Maxima, display the results in the minibuffer" t)
205 ;; (autoload 'maxima-minibuffer-on-line "maxima"
206 ;; "Send a information to Maxima, display the results in the minibuffer" t)
207 ;; (autoload 'maxima-minibuffer-on-form "maxima"
208 ;; "Send a information to Maxima, display the results in the minibuffer" t)
211 ;; ** Maxima minor mode **
212 ;; maxima-minor-mode provides convenient keybindings for the various
213 ;; interactions between Maxima and the minibuffer.
214 ;; It can be made easily available by placing
215 ;; (autoload 'maxima-minor-mode "maxima" "Maxima minor mode" t)
216 ;; in your .emacs, then M-x maxima-minor-mode will start the minor mode.
217 ;; (The autoloads for the individual function will not then be necessary.)
219 ;; `maxima-minibuffer-on-determined-region'
221 ;; `maxima-minibuffer-on-line'
223 ;; `maxima-minibuffer-on-region'
225 ;; `maxima-minibuffer-on-form'
227 ;; `maxima-minibuffer'
229 ;; `maxima-insert-last-output'
231 ;; `maxima-insert-last-output-tex'
236 (defvar maxima-running-xemacs
243 (require 'maxima-font-lock
)
245 ;;;; The variables that the user may wish to change
252 (defcustom maxima-inchar
"\\(C\\|%i\\)"
253 "*The character used for an input prompt."
257 (defcustom maxima-outchar
"\\(D\\|%o\\)"
258 "*The character used for an output prompt."
262 (defcustom maxima-linechar
"\\(E\\|%t\\)"
263 "*The character used for an intermediate output prompt."
267 (defcustom maxima-indent-amount
2
268 "*The amount of each indentation level in `maxima-mode'.
269 This is used after `then', etc."
273 (defcustom maxima-paren-indent-amount
1
274 "*The amount of indentation after a parenthesis."
278 (defcustom maxima-function-indent-amount
2
279 "*The amount of extra indentation to give within functions."
283 (defvar maxima-blockparen-indent-amount nil
)
284 (if maxima-blockparen-indent-amount
285 (setq maxima-function-indent-amount
286 maxima-blockparen-indent-amount
))
288 (defcustom maxima-continuation-indent-amount
2
289 "*The amount of extra indentation given when a line is continued."
293 (defcustom maxima-multiline-comment-indent-amount
2
294 "*The amount of extra indentation inside a multiline comment."
298 (defcustom maxima-dont-reindent-some-comments t
299 "*If non-nil, TAB will not change indentation of some comments,
300 namely those with nothing on the starting line past the `/*'."
304 (defcustom maxima-if-extra-indent-amount
0
305 "*The amount of extra indentation to give a \"then\" following an \"if\"."
309 ;(defcustom maxima-use-dynamic-complete nil
310 ; "*If non-nil, then M-TAB will complete words dynamically."
314 (defcustom maxima-indent-style
'standard
315 "*Determines how `maxima-mode' will handle tabs.
316 Choices are 'standard, 'perhaps-smart"
318 :type
'(choice :menu-tag
"Indent style"
321 (const perhaps-smart
)))
323 (defcustom maxima-return-style
'newline-and-indent
324 "*Determines how `maxima-mode' will handle RET.
325 Choices are 'newline, 'newline-and-indent, and 'reindent-then-newline-and-indent"
327 :type
'(choice :menu-tag
"Return style"
330 (const newline-and-indent
)
331 (const reindent-then-newline-and-indent
)))
333 (defvar maxima-newline-style nil
334 "For compatibility.")
336 (defcustom maxima-command
"maxima"
337 "*The command used to start Maxima. See also `maxima-args'."
341 (defcustom maxima-args nil
342 "*A list of extra arguments to pass to the `maxima-command'. Each
343 element in the list should be a distinct command-line option."
347 (defcustom maxima-use-tabs nil
348 "*If non-nil, indentation will use tabs."
352 (defcustom maxima-minibuffer-2d nil
353 "*If non-nil, use 2D output for maxima-minibuffer."
357 (defcustom maxima-use-full-color-in-process-buffer nil
358 "*If non-nil, font-lock the maxima process buffer."
362 (defcustom maxima-fix-double-prompt maxima-running-xemacs
363 "*If non-nil, fix the double prompt that sometimes appears in XEmacs."
367 (defcustom maxima-save-input-history nil
368 "*If non-nil, save the input history in a file."
372 (defcustom maxima-input-history-file
"~/.maxima_history"
373 "*A file to save the input history in."
377 (defcustom maxima-input-history-length
50
378 "*How many lines of history to save."
382 (defcustom maxima-minor-prefix
"^[ \t]*$"
383 "*A regexp to indicate the beginning of a region to send to Maxima
384 in maxima minor mode."
388 (defcustom maxima-minor-postfix
"^[ \t]*$"
389 "*A regexp to indicate the end of a region to send to Maxima
390 in maxima minor mode."
394 (defcustom maxima-minor-output
"==>"
395 "*A string to insert in the buffer right before the output."
399 (defcustom maxima-minor-output-end
" //"
400 "*A string to insert in the buffer right after the output."
404 (defcustom maxima-mode-minor-output
"/*==>"
405 "*A string to insert in the buffer right before the output."
409 (defcustom maxima-mode-minor-output-end
" <==*/"
410 "*A string to insert in the buffer right after the output."
414 (defcustom maxima-minor-mode-check-input t
415 "*Non-nil means check the input in Maxima minor mode before sending it."
419 (defun maxima-minor-output-mark ()
421 (eq major-mode
'maxima-mode
)
422 (not (maxima-in-comment-p)))
423 maxima-mode-minor-output
424 maxima-minor-output
))
426 (defun maxima-minor-output-mark-end ()
428 (eq major-mode
'maxima-mode
)
429 (not (maxima-in-comment-p)))
430 maxima-mode-minor-output-end
431 maxima-minor-output-end
))
433 ;;;; The other variables
435 ;; This variable seems to be necessary ...
436 (defvar inferior-maxima-after-output-wait
100)
438 (defvar maxima-temp-suffix
0
439 "Temporary filename suffix. Incremented by 1 for each filename.")
441 (defvar maxima-special-symbol-letters
"!:='")
443 (defvar maxima-minibuffer-history nil
)
445 (defvar maxima-block
"")
447 (defvar maxima-block-wait
"")
449 (defvar inferior-maxima-process nil
450 "The Maxima process.")
452 (defvar inferior-maxima-input-end
0
453 "The end of the latest input that was sent to Maxima.")
455 (defvar inferior-maxima-output-end
0)
457 (defvar inferior-maxima-waiting-for-output nil
)
459 (defvar inferior-maxima-exit-hook nil
)
461 (defvar inferior-maxima-prompt
462 (concat "\\(^(" maxima-inchar
463 "[0-9]*) \\)\\|\\(^MAXIMA> \\)\\|\\(^(dbm:[0-9]*) \\)")
464 ; \\(^[^#%)>]*[#%)>]+ *\\)"
465 "*Regexp to recognize prompts from the inferior Maxima") ; or lisp")
468 (defvar maxima-mode-highlight nil
)
470 (defvar maxima-mode-region-begin nil
)
472 (defvar maxima-mode-region-end nil
)
474 (defvar maxima-minor-mode-region-begin nil
)
476 (defvar maxima-minor-mode-region-end nil
)
478 (defvar maxima-minor-mode-highlight nil
)
480 (defvar maxima-minor-mode-bad-delimiter-regexp
"\\([ \t\n]+\\|[0-9]+\\)")
482 ;;;; Utility functions
484 (defun maxima-string-equal (str1 str2
)
485 (string= (downcase str1
) (downcase str2
)))
487 ;; This was taken from `replace-regexp-in-string' from subr.el in GNU emacs.
488 (defun maxima-replace-in-string (regexp rep string
)
489 "Replace all matches for REGEXP with REP in STRING."
490 (let ((l (length string
))
494 (while (and (< start l
) (string-match regexp string start
))
495 (setq mb
(match-beginning 0)
497 (when (= me mb
) (setq me
(min l
(1+ mb
))))
498 (string-match regexp
(setq str
(substring string mb me
)))
500 (cons (replace-match (if (stringp rep
)
502 (funcall rep
(match-string 0 str
)))
504 (cons (substring string start mb
) ; unmatched prefix
507 ;; Reconstruct a string from the pieces.
508 (setq matches
(cons (substring string start l
) matches
)) ; leftover
509 (apply #'concat
(nreverse matches
)))))
511 (defun maxima-remove-kill-buffer-hooks ()
512 "Remove the kill-buffer-hooks locally"
513 (if (or maxima-running-xemacs
(< emacs-major-version
21))
515 (make-local-hook 'kill-buffer-hook
)
516 (setq kill-buffer-hook nil
))
517 (let ((hooks kill-buffer-hook
))
519 (remove-hook 'kill-buffer-hook
(car hooks
) t
)
520 (setq hooks
(cdr hooks
))))))
522 (defun maxima-make-temp-name ()
523 "Return a unique filename."
524 (setq maxima-temp-suffix
(+ maxima-temp-suffix
1))
525 (concat (concat (make-temp-name "#mz") "-")
526 (int-to-string maxima-temp-suffix
)
529 (defun maxima-strip-string-beginning (string)
530 "Return STRING with whitespace and comments removed from the beginning."
531 (let* ((tmpfile (maxima-make-temp-name))
532 (tmpbuf (get-buffer-create tmpfile
))
536 (maxima-remove-kill-buffer-hooks)
537 (modify-syntax-entry ?
/ ". 14")
538 (modify-syntax-entry ?
* ". 23")
540 (goto-char (point-min))
541 (maxima-forward-over-comment-whitespace)
542 (setq out
(buffer-substring-no-properties (point) (point-max))))
546 (defun maxima-strip-string-end (string)
547 "Return STRING with whitespace and comments removed from the end."
548 (let* ((tmpfile (maxima-make-temp-name))
549 (tmpbuf (get-buffer-create tmpfile
))
553 (maxima-remove-kill-buffer-hooks)
554 (modify-syntax-entry ?
/ ". 14")
555 (modify-syntax-entry ?
* ". 23")
557 (goto-char (point-max))
558 (maxima-back-over-comment-whitespace)
559 (setq out
(buffer-substring-no-properties (point-min) (point))))
563 (defun maxima-strip-string (string)
564 "Return STRING with whitespace and comments removed from the ends."
565 (maxima-strip-string-beginning (maxima-strip-string-end string
)))
567 (defun maxima-strip-string-add-semicolon (string)
568 "Return STRING with whitespace and comments removed from the ends."
570 (maxima-strip-string-beginning (maxima-strip-string-end string
)))
573 (and (>= (length string
) 5)
574 (string= (substring string
0 5) ":lisp"))
575 (string= (substring string -
1) ";")
576 (string= (substring string -
1) "$"))
577 (setq string
(concat string
";")))
580 (defun maxima-remove-whitespace-from-ends (string)
581 "Return STRING with whitespace from the ends."
582 (let* ((tmpfile (maxima-make-temp-name))
583 (tmpbuf (get-buffer-create tmpfile
))
590 (maxima-remove-kill-buffer-hooks)
592 (goto-char (point-min))
593 (skip-chars-forward " \t\n")
595 (goto-char (point-max))
596 (skip-chars-backward " \t\n")
598 (setq out
(buffer-substring-no-properties beg end
)))
602 (defun maxima-remove-whitespace-from-beg (string)
603 "Return STRING with whitespace removed from the beginning."
604 (let* ((tmpfile (maxima-make-temp-name))
605 (tmpbuf (get-buffer-create tmpfile
))
612 (maxima-remove-kill-buffer-hooks)
614 (goto-char (point-min))
615 (skip-chars-forward " \t\n")
617 (setq out
(buffer-substring-no-properties beg
(point-max))))
621 (defun maxima-remove-whitespace-from-end (string)
622 "Return STRING with whitespace removed from the end."
623 (let* ((tmpfile (maxima-make-temp-name))
624 (tmpbuf (get-buffer-create tmpfile
))
631 (maxima-remove-kill-buffer-hooks)
633 (goto-char (point-max))
634 (skip-chars-backward " \t\n")
636 (setq out
(buffer-substring-no-properties (point-min) end
)))
640 ;;;; Functions that query position
641 (defun maxima-in-comment-p ()
642 "Non-nil means that the point is in a comment."
646 (search-backward "/*" nil t
)
647 (not (search-forward "*/" pt t
))))))
649 (defun maxima-in-output-p ()
650 "Non-nil means that the point is in minibuffer output."
654 (search-backward (maxima-minor-output-mark) nil t
)
655 (not (search-forward (maxima-minor-output-mark-end) pt t
))))))
657 ;;; Functions that search
659 ;; Some additions to help with different types of searches
660 (defvar maxima-mode-type
'maxima-mode
)
661 (make-variable-buffer-local 'maxima-mode-type
)
663 (defvar maxima-noweb-ignore-bounds
'("<<" ">>"))
665 (defun maxima-noweb-in-ignore-bounds-p ()
666 (if (not maxima-noweb-ignore-bounds
)
670 (if (not (re-search-backward (car maxima-noweb-ignore-bounds
) nil t
))
672 (not (re-search-forward (cadr maxima-noweb-ignore-bounds
) pt t
)))))))
674 (defun maxima-noweb-forward-out-of-ignore-bounds (&optional pmax
)
675 (re-search-forward (cadr maxima-noweb-ignore-bounds
) pmax
1))
677 (defun maxima-noweb-backward-out-of-ignore-bounds (&optional pmin
)
678 (re-search-backward (car maxima-noweb-ignore-bounds
) pmin
1))
681 (defun maxima-standard-re-search-forward (regexp &optional pmax
)
682 "Search forward for REGEXP, bounded by PMAX.
683 Ignore matches found in comments and strings."
684 (let ((keep-looking t
)
691 (while (and keep-looking
693 (re-search-forward regexp pmax t
))
694 (setq match
(match-string 0))
695 (setq ppe
(parse-partial-sexp pt
(point)))
697 ((nth 3 ppe
) ;; In a string
698 (if (maxima-goto-end-of-string)
700 (setq didnt-find t
)))
701 ((nth 4 ppe
) ;; In a comment
702 (if (maxima-goto-end-of-comment)
704 (setq didnt-find t
)))
705 (t ;; not in a comment or string
706 (setq keep-looking nil
))))
707 (if (or didnt-find keep-looking
)
714 (defun maxima-noweb-re-search-forward (regexp &optional pmax
)
716 (maxima-standard-re-search-forward regexp pmax
)))
717 (while (maxima-noweb-in-ignore-bounds-p)
718 (maxima-noweb-forward-out-of-ignore-bounds pmax
)
720 (maxima-standard-re-search-forward regexp pmax
)))
723 (defun maxima-re-search-forward (regexp &optional pmax
)
725 ((eq maxima-mode-type
'maxima-noweb-mode
)
726 (maxima-noweb-re-search-forward regexp pmax
))
728 (maxima-standard-re-search-forward regexp pmax
))))
730 (defun maxima-re-search-forward-skip-blocks (regexp &optional pmax
)
731 "Search forward for REGEXP, bounded by PMAX.
732 Ignore matches found in comments and strings, and skip over
733 parenthesized or bracketed blocks."
734 (let ((keep-looking t
)
738 (while (and keep-looking
739 (setq match
(maxima-re-search-forward
740 (concat regexp
"\\|[[(]") pmax
)))
746 (maxima-goto-end-of-list)
748 (setq keep-looking nil
)))
750 (setq keep-looking nil
))))
751 (if (or keep-looking didnt-find
)
757 (defun maxima-standard-re-search-backward (regexp &optional pmin
)
758 "Search backward for REGEXP, bounded by PMIN.
759 Ignore matches found in comments and strings."
760 (let ((keep-looking t
)
765 (setq pmin
(point-min)))
766 (while (and keep-looking
767 (re-search-backward regexp pmin t
))
768 (setq match
(match-string 0))
769 (setq ppe
(parse-partial-sexp pmin
(point)))
771 ((nth 8 ppe
) ;; In a string or comment
772 (goto-char (nth 8 ppe
)))
773 (t ;; not in a comment or string
774 (setq keep-looking nil
))))
781 (defun maxima-noweb-re-search-backward (regexp &optional pmin
)
783 (maxima-standard-re-search-backward regexp pmin
)))
784 (while (maxima-noweb-in-ignore-bounds-p)
785 (maxima-noweb-backward-out-of-ignore-bounds pmin
)
787 (maxima-standard-re-search-backward regexp pmin
)))
790 (defun maxima-re-search-backward (regexp &optional pmin
)
792 ((eq maxima-mode-type
'maxima-noweb-mode
)
793 (maxima-noweb-re-search-backward regexp pmin
))
795 (maxima-standard-re-search-backward regexp pmin
))))
797 (defun maxima-re-search-backward-skip-blocks (regexp &optional pmin
)
798 "Search forward for REGEXP, bounded by PMIN.
799 Ignore matches found in comments and strings, and skip over
800 parenthesized and bracketed blocks."
801 (let ((keep-looking t
)
805 (while (and keep-looking
806 (setq match
(maxima-re-search-backward
807 (concat regexp
"\\|[])]") pmin
)))
813 (maxima-goto-beginning-of-list)
815 (setq keep-looking nil
)))
817 (setq keep-looking nil
))))
818 (if (or keep-looking didnt-find
)
824 (defun maxima-escaped-char-p ()
825 "Returns non-nil if the character after point is escaped"
826 (let ((pm (point-min))
833 (setq esc-chars
(1+ esc-chars
))
836 (if (= (% esc-chars
2) 0)
840 (defun maxima-goto-end-of-string ()
841 "Go to the end of the string that the point is in.
842 Assumes that point is in a string."
844 (let ((keep-looking t
))
845 (while (and keep-looking
(search-forward "\"" nil t
))
847 (unless (maxima-escaped-char-p)
848 (setq keep-looking nil
))
854 (defun maxima-goto-beginning-of-string ()
855 "Go to the beginning of the string that the point is in.
856 Assumes that point is in a string."
858 (let ((keep-looking t
))
859 (while (and keep-looking
(search-backward "\"" nil t
))
861 (unless (maxima-escaped-char-p)
862 (setq keep-looking nil
))
865 (defun maxima-goto-end-of-comment ()
866 "Go to the end of the comment that the point is in.
867 Assumes that point is in a comment."
869 (search-forward "*/" nil t
))
871 (defun maxima-goto-beginning-of-comment ()
872 "Go to the beginning of the comment that the point is in.
873 Assumes that point is in a comment."
875 (search-backward "/*"))
877 (defun maxima-find-next-nonnested-close-char ()
878 "Search forward for next , ; $ or closing ), skipping over balanced parens.
879 If character is in a string or a list, ignore it."
881 (maxima-re-search-forward-skip-blocks "[,;$)]"))
884 ;;; Functions for dealing with words
886 ;; (defun maxima-number-of-preceding-backslashes ()
887 ;; "The number of backslashes (including the one being looked at)."
888 ;; (let ((pt (point)))
889 ;; (if (not (looking-at "\\\\"))
892 ;; (skip-chars-backward "\\\\")
893 ;; (1+ (- pt (point)))))))
895 (defun maxima-standard-next-char-word-part-p ()
896 "Non-nil if next char is a a word part."
902 (looking-at "\\\\"))))
904 (defun maxima-noweb-next-char-word-part-p ()
905 "Non-nil if next char is a a word part."
916 (looking-at "\\\\"))))
918 (defun maxima-next-char-word-part-p ()
920 ((eq maxima-mode-type
'maxima-noweb-mode
)
921 (maxima-noweb-next-char-word-part-p))
923 (maxima-standard-next-char-word-part-p))))
925 (defun maxima-previous-char-word-part-p ()
926 "Non-nil if previous character is a word part."
929 (maxima-next-char-word-part-p)))
931 (defun maxima-standard-forward-word ()
932 "Go to the end of the current word."
933 (let ((keep-going t
))
941 (setq keep-going nil
))))))
943 (defun maxima-noweb-forward-word ()
944 "Go to the end of the current word."
945 (if (maxima-noweb-in-ignore-bounds-p)
946 (maxima-noweb-forward-out-of-ignore-bounds))
947 (let ((keep-going t
))
956 (maxima-noweb-forward-out-of-ignore-bounds))
958 (setq keep-going nil
))))))
960 (defun maxima-forward-word ()
962 ((eq maxima-mode-type
'maxima-noweb-mode
)
963 (maxima-noweb-forward-word))
965 (maxima-standard-forward-word))))
967 (defun maxima-standard-backward-word ()
968 "Go to the beginning of the current word."
969 (let ((keep-going t
))
973 (> (point) (point-min))
979 (> (point) (1+ (point-min)))
982 (looking-at "\\\\")))
985 (setq keep-going nil
))))))
987 (defun maxima-noweb-backward-word ()
988 "Go to the beginning of the current word."
989 (if (maxima-noweb-in-ignore-bounds-p)
990 (maxima-noweb-backward-out-of-ignore-bounds))
991 (let ((keep-going t
))
995 (> (point) (point-min))
1001 (> (point) (1+ (point-min)))
1004 (looking-at "\\\\")))
1007 (> (point) (1+ (point-min)))
1012 (maxima-noweb-backward-out-of-ignore-bounds))
1014 (setq keep-going nil
))))))
1016 (defun maxima-backward-word ()
1018 ((eq maxima-mode-type
'maxima-noweb-mode
)
1019 (maxima-noweb-backward-word))
1021 (maxima-standard-backward-word))))
1023 ;;;; Functions that return special positions
1025 (defun maxima-line-beginning-position ()
1026 (if (not (fboundp 'line-beginning-position
))
1030 (line-beginning-position)))
1032 (defun maxima-line-end-position ()
1033 (if (not (fboundp 'line-end-position
))
1037 (line-end-position)))
1039 (defun maxima-name-beginning ()
1041 (maxima-backward-word)
1044 (defun maxima-special-symbol-beginning ()
1046 (skip-chars-backward maxima-special-symbol-letters
)
1049 (defun maxima-special-symbol-end ()
1051 (skip-chars-forward maxima-special-symbol-letters
)
1054 (defun maxima-form-beginning-position ()
1056 (maxima-goto-beginning-of-form)
1059 (defun maxima-form-end-position ()
1061 (if (maxima-goto-end-of-form)
1065 (defun maxima-form-end-position-or-point-max ()
1066 (let ((mfep (maxima-form-end-position)))
1071 (defun maxima-expression-end-position ()
1072 "Return the point where the current expression ends,
1075 (if (maxima-goto-end-of-expression)
1079 (defun maxima-begin-if-position (pmin)
1080 "Find the point of the opening \"if\" for the current point."
1086 (while (and keep-looking
1088 (maxima-re-search-backward-skip-blocks
1089 "\\<if\\>\\|\\<then\\>" pmin
)))
1090 (setq match
(downcase match
))
1091 (cond ((maxima-string-equal match
"if") (setq nest
(1+ nest
)))
1092 (t (setq nest
(1- nest
))))
1095 (setq keep-looking nil
))))
1100 (defun maxima-begin-then-position (pmin)
1101 "Find the point of the opening \"then\" for the current \"else\"."
1102 (let ((keep-going t
)
1106 (while (and keep-going
1107 (maxima-re-search-backward-skip-blocks "\\<then\\>" pmin
))
1108 ;; A potential "then". Let's see if it is.
1109 (let ((meep (maxima-expression-end-position)))
1110 (when (or (not meep
) (<= pt meep
))
1111 ;; This "then" is looking pretty good.
1112 ;; Now we need to make sure that there aren't any "else"s
1117 (while (setq match
(maxima-re-search-forward-skip-blocks
1118 "\\<then\\>\\|\\<else\\>" pt
))
1119 (cond ((maxima-string-equal match
"then")
1120 (setq level
(1+ level
)))
1121 ((maxima-string-equal match
"else")
1122 (setq level
(1- level
))))))
1124 (setq begin-then
(point))
1125 (setq keep-going nil
))))))
1128 ;;;; Functions that move the position
1129 (defun maxima-forward-over-comment-whitespace ()
1130 "Move forward over comments and whitespace."
1131 (forward-comment (buffer-size))
1132 (let ((mmo (maxima-remove-whitespace-from-beg (maxima-minor-output-mark))))
1133 (when (and (> (- (point-max) (point)) (length mmo
))
1135 (buffer-substring-no-properties
1136 (point) (+ (point) (length mmo
)))
1138 (search-forward (maxima-minor-output-mark-end))
1139 (forward-comment (buffer-size)))))
1141 (defun maxima-back-over-comment-whitespace ()
1142 "Move backward over comments and whitespace."
1143 (forward-comment (- (buffer-size)))
1144 (let ((mme (maxima-remove-whitespace-from-end (maxima-minor-output-mark-end))))
1145 (when (and (> (- (point) (point-min)) (length mme
))
1147 (buffer-substring-no-properties
1148 (- (point) (length mme
)) (point))
1150 (search-backward (maxima-minor-output-mark))
1151 (forward-comment (- (buffer-size))))))
1153 (defun maxima-standard-goto-beginning-of-form ()
1154 "Move to the beginning of the form."
1157 (while (and keep-looking
1158 (maxima-re-search-backward "[;$]" nil
))
1160 (unless (looking-at "\\\\\\$")
1162 (setq keep-looking nil
)))
1164 (goto-char (point-min)))
1165 (maxima-forward-over-comment-whitespace)
1170 (defun maxima-noweb-goto-beginning-of-form ()
1171 "Move to the beginning of the form."
1172 (if (re-search-backward "^<<.*?>>= *$" (point-min) 1)
1174 (maxima-forward-over-comment-whitespace))
1176 (defun maxima-goto-beginning-of-form ()
1178 ((eq maxima-mode-type
'maxima-noweb-mode
)
1179 (maxima-noweb-goto-beginning-of-form))
1181 (maxima-standard-goto-beginning-of-form))))
1183 (defun maxima-goto-beginning-of-form-interactive ()
1184 "Move to the beginning of the form."
1186 (maxima-goto-beginning-of-form))
1188 (defun maxima-standard-goto-end-of-form ()
1189 "Move to the end of the form."
1190 (let ((keep-looking t
)
1192 (while (and keep-looking
1193 (maxima-re-search-forward "[;$]" nil
))
1195 (unless (looking-at "\\\\\\$")
1196 (setq keep-looking nil
))
1198 (if (not keep-looking
)
1203 (defun maxima-noweb-goto-end-of-form ()
1204 "Move to the end of the form."
1205 (when (re-search-forward "\\(^@\\( \\|$\\)\\|^<<.*>>= *$\\)" nil
1)
1208 (maxima-back-over-comment-whitespace)))
1210 (defun maxima-goto-end-of-form ()
1212 ((eq maxima-mode-type
'maxima-noweb-mode
)
1213 (maxima-noweb-goto-end-of-form))
1215 (maxima-standard-goto-end-of-form))))
1217 (defun maxima-goto-end-of-form-interactive ()
1218 "Move to the end of the form."
1220 (unless (maxima-goto-end-of-form)
1221 (message "No end of form")))
1223 (defun maxima-goto-end-of-expression ()
1224 "Find the point that ends the expression that begins at point.
1225 The expression is assumed to begin with \"if\", \"then\", \"do\"
1226 \"else\" or \"(\". Return nil if the end is not found."
1228 ;; To find the end of the expression:
1229 ;; if looking at (, look for )
1230 ;; otherwise look for a , ; or $ at the same nesting level of
1231 ;; parentheses or a closing ).
1232 (cond ((or (looking-at "(")
1234 (maxima-forward-list))
1236 (maxima-find-next-nonnested-close-char))))
1238 (defun maxima-goto-beginning-of-construct (pmin)
1239 "Go to the point the begins the current construct."
1240 (let ((keep-looking t
)
1242 (while (and keep-looking
1243 (maxima-re-search-backward-skip-blocks
1244 "\\<if\\>\\|\\<then\\>\\|\\<do\\>\\|\\<else\\>\\|(\\|\\[" pmin
))
1246 (when (or (not (maxima-goto-end-of-expression)) (<= pt
(point)))
1247 (setq keep-looking nil
))))
1252 (defun maxima-goto-end-of-list ()
1254 Return t if possible, nil otherwise."
1263 (defun maxima-goto-end-of-list-interactive ()
1266 (if (maxima-goto-end-of-list)
1268 (error "No list to end.")))
1270 (defun maxima-goto-beginning-of-list ()
1271 "Go up a list backwards.
1272 Return t if possible, nil otherwise."
1280 (defun maxima-goto-beginning-of-list-interactive ()
1283 (if (maxima-goto-beginning-of-list)
1285 (error "No list to begin.")))
1287 (defun maxima-forward-list ()
1289 Return t if possible, nil otherwise."
1297 (defun maxima-backward-list ()
1298 "Go backward a list.
1299 Return t if possible, nil otherwise."
1307 ;;; Newlines and indents
1308 (defun maxima-indent-form ()
1309 "Indent the entire form."
1312 (maxima-form-beginning-position)
1313 (maxima-form-end-position-or-point-max)
1317 (defun maxima-standard-indent ()
1318 "Indent the line based on the previous line.
1319 If the previous line opened a parenthesis, `maxima-indent-amount' is
1320 added to the indentation, if the previous line closed a parenthesis,
1321 `maxima-indent-amount' is subtracted, otherwise the indentation
1322 is the same as the previous line."
1328 (when (= (forward-line -
1) 0)
1330 (setq indent
(current-indentation))
1331 (setq pt
(maxima-line-end-position))
1332 (while (setq match
(maxima-re-search-forward "[()]" pt
))
1333 (cond ((string= match
")")
1334 (setq indent
(- indent maxima-indent-amount
)))
1335 ((string= match
"(")
1336 (setq indent
(+ indent maxima-indent-amount
))))))))
1339 (delete-horizontal-space)
1340 (indent-line-to (max indent
0)))
1341 (skip-chars-forward " \t")))
1343 (defun maxima-untab ()
1344 "Delete a level of indentation"
1346 ;; Check to see if the previous maxima-indent-amount spaces are
1347 ;; on the same line and blank
1348 (let ((i maxima-indent-amount
)
1352 (while (and (> i
0) ok
)
1355 (if (not (looking-at " "))
1357 (if (looking-at "\n")
1360 (delete-region pt
(- pt maxima-indent-amount
)))))
1363 ;;; Find the beginning of a function
1365 (defun maxima-open-paren-is-function ()
1366 "Check to see if the point is before an opening paren,
1367 and if the previous character is a close paren or word part."
1369 (looking-at "[ \t]*(")
1371 (maxima-back-over-comment-whitespace)
1373 (looking-at ")\\|\\w"))))
1375 (defun maxima-close-paren-before-open-paren ()
1376 "Return point of close paren before the current open paren,
1377 or nil if there is none."
1379 (maxima-back-over-comment-whitespace)
1381 (if (looking-at ")")
1385 (defun maxima-back-over-paren-groups ()
1386 "Go over any paren groups.
1387 Assume point is at \"(\", as long as preceding character is
1388 \")\", go to open parentheses."
1389 (let ((cpbop (maxima-close-paren-before-open-paren)))
1392 (maxima-backward-list)
1394 (maxima-close-paren-before-open-paren)))))
1396 (defun maxima-word-on-previous-line ()
1397 "Go to the previous word part, return nil if "
1400 (skip-chars-backward " \t\n")
1403 (maxima-previous-char-word-part-p)
1405 (buffer-substring-no-properties (point) pt
))))))
1407 (defun maxima-string-on-previous-line ()
1408 "Go to the previous word part, return nil if "
1411 (skip-chars-backward " \t\n")
1417 (buffer-substring-no-properties (point) pt
))))))
1419 (defun maxima-back-over-function-name ()
1420 "Go back over the `foo' in 'foo(x,y)', return the length of `foo'.
1421 Return nil if open paren is not part of function.
1422 Assumes the point is right before the open parenthesis."
1425 (maxima-back-over-paren-groups)
1426 (setq endpt
(point))
1427 ;; Let's see what's before this
1429 ;; There is a word right before this
1430 ((maxima-previous-char-word-part-p)
1431 (maxima-backward-word)
1433 ;; There is a string before this
1437 (maxima-goto-beginning-of-string)
1439 ;; There is a word before this on the previous line
1440 ((maxima-word-on-previous-line)
1441 (skip-chars-backward " \t\n")
1442 (setq endpt
(point))
1443 (maxima-backward-word)
1445 ;; There is a string before this on the previous line
1446 ((maxima-string-on-previous-line)
1447 (skip-chars-backward " \t\n")
1448 (setq endpt
(point))
1449 (maxima-goto-beginning-of-string)
1451 ;; This point is the original point
1454 ;; Finally, the last parenthesized expression is the function
1457 (maxima-forward-list)
1458 (setq endpt
(point)))
1459 (- endpt
(point))))))
1463 (defun maxima-after-lisp-expression-p ()
1464 "Return non-nil if the point is right after a lisp expression."
1465 (let ((pm (point-min))
1468 (maxima-back-over-comment-whitespace)
1473 (when (< (point) pt
)
1474 (maxima-back-over-comment-whitespace)
1475 (if (< (point) (+ 5 pm
))
1478 (if (looking-at ":lisp")
1482 (defun maxima-standard-perhaps-smart-calculate-indent ()
1483 "Return appropriate indentation for current line as Maxima code.
1484 Returns an integer: the column to indent to."
1496 (setq pmin
(maxima-form-beginning-position))
1497 (setq pps
(parse-partial-sexp pmin
(point)))
1498 (setq le
(maxima-after-lisp-expression-p))
1500 (setq pmin
(nth 1 pps
))
1501 (unless (looking-at "^[ \t]*,")
1503 (when (maxima-re-search-backward-skip-blocks "," pmin
)
1505 (let ((lep (maxima-line-end-position)))
1507 (maxima-forward-over-comment-whitespace)
1508 (unless (>= (point) lep
)
1509 (setq pmin
(point)))))))))
1511 ;; First, take care of the cases where the indentation is clear
1512 ;; No indentation at the beginning of the buffer
1515 ;; Don't change the indentation if in a string
1518 ;; If the line begins a comment and comments aren't reindented,
1519 ;; don't reindent it
1521 (looking-at "[ \t]*/\\*[ \t]*$")
1522 maxima-dont-reindent-some-comments
)
1524 ;; Deal with comments separately
1525 ((maxima-perhaps-smart-in-comment-p (nth 4 pps
) pmin pt
)
1526 (setq indent
(maxima-perhaps-smart-comment-calculate-indent)))
1527 ;; If the current point is the beginning of the form, the level is 0
1530 ;; If the current point is in maxima minor output, don't reindent it
1531 ((maxima-in-output-p)
1533 ;; A line beginning "then" is indented like the opening "if"
1535 (looking-at "[ \t]*\\<then\\>")
1536 (setq tmpchar
(maxima-begin-if-position pmin
)))
1538 (setq indent
(+ maxima-if-extra-indent-amount
(current-column))))
1539 ;; A line beginning "else" is indented like the corresponding "then"
1541 (looking-at "[ \t]*\\<else\\>")
1542 (setq tmpchar
(maxima-begin-then-position pmin
)))
1544 (setq indent
(current-column)))
1545 ;; A line beginning with an open paren that is the
1546 ;; beginning of a function argument is indented according to
1549 (looking-at "[ \t]*(")
1550 (setq len
(maxima-back-over-function-name)))
1551 (setq indent
(+ (current-column)
1552 (min len maxima-function-indent-amount
))))
1553 ;; A line beginning with a closing paren is indented like the open paren
1554 ((looking-at "[ \t]*)")
1555 ;; Here, pmin should be the opening paren position
1557 (if (looking-at "( *$")
1559 (setq len
(maxima-back-over-function-name))
1562 (min len maxima-function-indent-amount
)
1565 (setq indent
(current-column))))
1566 ;; A line beginning with a closing bracket is indented like the open bracket
1567 ((looking-at "[ \t]*\\]")
1568 ;; Here, pmin should be the opening bracket position
1570 (setq indent
(current-column)))
1571 ; ;; A line beginning with a comma is indented like the opening paren
1572 ; ((looking-at "[ \t]*,")
1574 ; (setq indent (current-column)))
1575 ;; The point is at the end of a lisp expression
1578 ;; Otherwise, the correct indentation needs to be computed.
1580 ;; Find the indentation of beginning of the current construct
1581 ;; If begin-construct is nil, indent according to the opening paren
1582 (setq comma-line
(looking-at "[ \t]*,"))
1584 (maxima-goto-beginning-of-construct pmin
)
1586 ;; The construct begins with a bracket
1589 ; (setq indent (current-column))
1590 (setq indent
(+ maxima-paren-indent-amount
(current-column)))
1592 (skip-chars-forward " \t")
1593 (unless (looking-at "\n")
1594 (setq indent
(current-column))));)
1595 ;; The construct begins with a paren
1599 ; (setq indent (current-column)))
1601 (let ((lep (maxima-line-end-position)))
1603 (maxima-forward-over-comment-whitespace)
1605 ;(looking-at "( *$")
1606 ;; Check to see if there is anything before the (
1608 (re-search-backward "\\(?:^[ \t]*\\)\\=" nil t
))
1609 (setq tmpchar maxima-paren-indent-amount
)
1611 ;; If there is nothing after the (, there are two
1613 ;; First, there is a function before it
1614 (if (setq len
(maxima-back-over-function-name))
1615 (setq indent
(+ (min len maxima-function-indent-amount
)
1619 (setq indent
(+ maxima-paren-indent-amount
(current-column)))))
1620 ;; If there is something after the (, indent according to that
1623 (skip-chars-forward " \t")
1624 (setq indent
(current-column)))))
1625 ;; The construct does not begin with a paren
1627 (setq indent
(current-column)))))
1628 ;; Now, we need to possibly do some adjustments.
1629 ;; If the previous column doesn't end with close-char or a
1630 ;; parenthesis, assume that the current line in a continuation
1631 ;; of that line, and add to the indentation, unless the
1632 ;; previous line was the beginning of the construct containing
1633 ;; the point or only an open parenthesis.
1635 (let ((bol-pt (point))
1638 (skip-chars-forward " \t")
1639 (setq comma-pt
(point))
1641 (skip-chars-forward " \t")
1642 (setq diff
(- (point) comma-pt
))
1647 (1+ (count-lines (point-min) (point))))))
1649 (format "Leading comma prevents proper indentation on line %d"
1652 (setq indent
(- indent diff
))))
1653 (maxima-back-over-comment-whitespace)
1654 (when (not (looking-at "^"))
1656 (if (not (or le
(looking-at "[,;$]")))
1657 (setq indent
(+ maxima-continuation-indent-amount indent
))))))))
1660 (defun maxima-noweb-perhaps-smart-calculate-indent ()
1666 ((looking-at "^<<.*?>>=[ \t]*$")
1668 ((looking-at "^@[ \n]")
1672 (if (looking-at "^<<.*?>>=[ \t]*$")
1673 (setq indent -
1)))))
1676 (maxima-standard-perhaps-smart-calculate-indent))))
1678 (defun maxima-perhaps-smart-calculate-indent ()
1680 ((eq maxima-mode-type
'maxima-mode
)
1681 (maxima-standard-perhaps-smart-calculate-indent))
1682 ((eq maxima-mode-type
'maxima-noweb-mode
)
1683 (maxima-noweb-perhaps-smart-calculate-indent))))
1686 (defun maxima-perhaps-smart-in-comment-p (incomment pmin pt
)
1687 "Determine if the point is in a comment or not."
1689 ;; If we're told it's in a comment, then it is.
1692 ;; Otherwise, if pmin is less than point, we're not in a comment
1695 ;; Otherwise, we have to check it out
1697 (maxima-in-comment-p))))
1699 (defun maxima-perhaps-smart-comment-calculate-indent ()
1700 "Calculate the indentation of the current line,
1701 which is in a comment which begins on a previous line."
1706 ((looking-at "^[ \t]*$")
1708 ((looking-at "^[ \t]*\\*/")
1709 (setq endcomment t
)))
1710 ;; First of all, if the comment begins with `/*' and nothing
1711 ;; else on the line, don't indent it.
1713 (maxima-goto-beginning-of-comment)
1715 ;; Take care of the case when comments won't be indented
1717 (looking-at "/\\*[ \t]*$")
1718 maxima-dont-reindent-some-comments
)
1720 (setq indent
(+ (current-column)
1721 maxima-multiline-comment-indent-amount
))
1723 ;; Now, the other cases
1724 ;; If the current line ends a column, indent it like the opening line
1726 (setq indent
(current-column)))
1727 ;; If the opening line has a blank after the `/*'
1728 ((looking-at "/\\*[ \t\n]")
1730 (skip-chars-forward " \t")
1731 (if (not (looking-at "\n"))
1732 (setq indent
(current-column))
1733 (search-backward "/*")
1734 (setq indent
(+ (current-column)
1735 maxima-multiline-comment-indent-amount
))))
1737 (setq indent
(+ (current-column)
1738 maxima-multiline-comment-indent-amount
)))))
1741 (defun maxima-perhaps-smart-indent-line ()
1742 "Reindent the current line."
1744 (let ((indent (maxima-perhaps-smart-calculate-indent)))
1745 (unless (= indent -
1)
1748 (delete-horizontal-space)
1749 (indent-to indent
)))
1750 (skip-chars-forward " \t")))
1752 ;;;; Indentation according to style
1754 (defun maxima-indent-line ()
1757 ((eq maxima-newline-style
'basic
)
1758 (maxima-standard-indent))
1759 ((eq maxima-indent-style
'standard
)
1760 (maxima-standard-indent))
1761 ((eq maxima-indent-style
'perhaps-smart
)
1762 (maxima-perhaps-smart-indent-line))))
1764 (defun maxima-change-indent-style (new-style)
1765 "Change the newline style."
1766 (interactive "sNewline style (insert \"b\" for basic, \"s\" for standard, or \"p\" for perhaps-smart): ")
1768 ((string= new-style
"b")
1769 (setq maxima-indent-style
'basic
))
1770 ((string= new-style
"s")
1771 (setq maxima-indent-style
'standard
))
1772 ((string= new-style
"p")
1773 (setq maxima-indent-style
'perhaps-smart
))))
1775 (defun maxima-return ()
1778 ((eq maxima-return-style
'newline
)
1780 ((eq maxima-return-style
'newline-and-indent
)
1781 (newline-and-indent))
1782 ((eq maxima-return-style
'reindent-then-newline-and-indent
)
1783 (reindent-then-newline-and-indent))))
1787 (defun maxima-insert-short-comment ()
1788 "Prompt for a comment."
1790 (let ((comment (read-string "Comment: ")))
1791 (insert "/* " comment
" */")
1792 (newline-and-indent)))
1794 (defun maxima-insert-long-comment ()
1795 "Insert a comment environment"
1797 (indent-for-tab-command)
1802 (indent-for-tab-command)
1804 (indent-for-tab-command))
1806 (defun maxima-uncomment-region (beg end
)
1807 "`uncomment-region' to use with the menu."
1809 (uncomment-region beg end
(universal-argument)))
1813 (defun maxima-goto-info-node (node)
1814 (if maxima-running-xemacs
1816 (info-other-window (concat "(Maxima)" node
))))
1818 (defun maxima-get-info-on-subject (subject &optional same-window
)
1820 (Info-menu "Function and Variable Index")
1821 (cond (maxima-running-xemacs
1822 (or (and (search-forward subject nil t
)
1823 (Info-follow-nearest-node (point)))
1824 (message (concat "Unable to locate " subject
))))
1826 (Info-menu subject
))))
1828 (defun maxima-get-help ()
1829 "Get help on a given subject"
1834 (skip-chars-forward "* ")
1836 (search-forward ":")
1837 (skip-chars-backward ": ")
1838 (setq name
(buffer-substring-no-properties pt
(point))))
1839 (maxima-get-info-on-subject name t
)))
1840 ; (if (not maxima-running-xemacs)
1841 ; (info-other-window (concat "(Maxima)" place))
1843 ; (search-forward place)
1844 ; (Info-follow-nearest-node))
1845 ; (re-search-forward (concat "-.*: *" name "\\>"))
1846 ; (beginning-of-line)))
1848 (defun maxima-help (&optional arg
)
1850 (let* ((cw (current-word))
1853 (completing-read (concat "Maxima help (" cw
"): ")
1854 maxima-symbols nil nil nil nil cw
))))
1856 (maxima-get-info-on-subject subj
)
1859 (defun maxima-help-at-point ()
1863 (defun maxima-apropos (&optional arg
)
1864 "Get help on a certain subject"
1866 (let* ((cw (current-word))
1869 (read-string (concat "Maxima help (" cw
"): ")
1872 (get-buffer-create (concat "*Maxima Help*")))
1876 (set-buffer maxima-help-buffer
)
1877 (maxima-remove-kill-buffer-hooks)
1878 (setq buffer-read-only nil
)
1880 (insert "Maxima help for " expr
"\n\n")
1881 (insert "[RET] will get help on the subject on the given line\n")
1882 (insert "q in the *info* buffer will return you here.\n")
1883 (insert "q in this buffer will exit Maxima help\n\n")
1887 (Info-goto-node "(Maxima)Function and Variable Index")
1888 (goto-char (point-min))
1889 (search-forward "Menu:")
1892 (while (re-search-forward (concat "\\*.*" expr
".*:") nil t
)
1894 (setq expr-line
(buffer-substring-no-properties
1895 (maxima-line-beginning-position)
1896 (maxima-line-end-position)))
1898 (set-buffer maxima-help-buffer
)
1899 (insert expr-line
"\n"))))
1902 (set-buffer maxima-help-buffer
)
1903 (defun maxima-help-subject ()
1906 (defun maxima-kill-help ()
1908 (let ((buf (current-buffer)))
1910 (maxima-remove-kill-buffer-hooks)
1912 (defun maxima-next-subject ()
1915 (if (re-search-forward "^\\*" nil t
)
1917 (goto-char (point-min))
1918 (re-search-forward "^\\*" nil t
))
1920 (use-local-map (make-sparse-keymap))
1921 (define-key (current-local-map) "\C-m" 'maxima-help-subject
)
1922 (define-key (current-local-map) "q" 'maxima-kill-help
)
1923 (define-key (current-local-map) "\t" 'maxima-next-subject
)
1924 (goto-char (point-min))
1925 (re-search-forward "^\\*")
1927 (pop-to-buffer maxima-help-buffer
)
1928 (setq buffer-read-only t
))
1929 (kill-buffer maxima-help-buffer
)
1930 (message (concat "No help for \"" expr
"\"")))))
1932 (defun maxima-apropos-at-point ()
1936 (defun maxima-apropos-help ()
1938 (maxima-help-dispatcher nil nil
))
1940 (defun maxima-completion-help ()
1942 (maxima-help-dispatcher nil t
))
1944 (defun maxima-help-dispatcher (&optional arg1 arg2
)
1947 ((or (looking-at "[a-zA-Z_]")
1948 (looking-at "?[a-zA-Z]")
1949 (looking-at "%[a-zA-Z]"))
1951 (maxima-context-help)
1952 (maxima-help (current-word))))
1954 (maxima-get-info-on-subject "\"\\?\""))
1956 (maxima-get-info-on-subject "\"#\""))
1958 (maxima-get-info-on-subject "\"\\.\""))
1959 ((looking-at "[:=!%']")
1960 (let ((expr (buffer-substring-no-properties
1961 (maxima-special-symbol-beginning) (maxima-special-symbol-end))))
1963 ((or (string= expr
"%") (string= expr
"%%"))
1964 (maxima-get-info-on-subject expr
)) ; % and %% are without double quotes
1965 ((string= expr
"''")
1966 (maxima-get-info-on-subject "\"")) ; "''" is called """ in the manual
1967 ((or (string= expr
":") (string= expr
"::")
1968 (string= expr
":=") (string= expr
"::=")
1969 (string= expr
"=") (string= expr
"!") (string= expr
"!!"))
1970 (maxima-get-info-on-subject (concat "\"" expr
"\"")))
1971 (t (error "no help for %s" expr
)))))
1973 (error "No help for %s" (char-to-string (char-after (point)))))
1974 (t ; point is behind a name?
1978 (maxima-help-dispatcher t arg2
))))))
1980 (defun maxima-context-help ()
1982 (let* ((stub (current-word))
1983 (completions (all-completions (downcase stub
) maxima-symbols
)))
1986 (function upcase
) completions
))
1987 (if (member (upcase stub
) completions
)
1988 (setq completions
(list (upcase stub
))))
1989 (cond ((null completions
)
1990 (message "No help for %s" stub
))
1991 ((= 1 (length completions
))
1992 (maxima-get-info-on-subject (car completions
)))
1993 (t ; There's no unique completion.
1994 (maxima-help-variation completions
)))))
1996 (defun maxima-help-variation (completions)
1997 "Get help on certain subjects."
1998 (let* ((maxima-help-buffer
1999 (get-buffer-create (concat "*Maxima Help*")))
2002 (set-buffer maxima-help-buffer
)
2004 (insert "Maxima help\n")
2005 (insert "[RET] will get help on the subject on the given line\n")
2006 (insert "q in the *info* buffer will return you here.\n")
2007 (insert "q in this buffer will exit Maxima help\n\n")
2008 (defun maxima-help-insert-line (expr)
2009 (re-search-forward (concat "\\* " expr
":") nil t
)
2010 (setq expr-line
(buffer-substring-no-properties
2011 (maxima-line-beginning-position)
2012 (maxima-line-end-position)))
2014 (set-buffer maxima-help-buffer
)
2015 (insert expr-line
"\n")))
2017 (require 'info nil t
)
2019 (Info-goto-node "(Maxima)Function and Variable Index")
2020 (goto-char (point-min))
2021 (search-forward "Menu:")
2024 (mapcar (function maxima-help-insert-line
) completions
))
2025 (goto-char (point-min))
2026 (defun maxima-help-subject ()
2029 (defun maxima-kill-help ()
2031 (let ((buf (current-buffer)))
2033 (maxima-remove-kill-buffer-hooks)
2035 (use-local-map (append (make-sparse-keymap) (current-local-map)))
2036 (define-key (current-local-map) "\C-m" 'maxima-help-subject
)
2037 (define-key (current-local-map) "q" 'maxima-kill-help
)
2039 (pop-to-buffer maxima-help-buffer
)))
2041 (defun maxima-info ()
2042 "Read the info file for Maxima."
2044 (if maxima-running-xemacs
2046 (info-other-window "Maxima")))
2050 (defvar maxima-help-map nil
)
2053 (let ((map (make-sparse-keymap)))
2054 (define-key map
"h" 'maxima-help
)
2055 (define-key map
"d" 'maxima-completion-help
)
2056 (define-key map
"\C-d" 'maxima-completion-help
)
2057 (define-key map
"i" 'maxima-info
)
2058 (define-key map
"\C-i" 'maxima-info
)
2059 (define-key map
"m" 'maxima-info
)
2060 (define-key map
"\C-m" 'maxima-info
)
2061 (define-key map
"a" 'maxima-apropos
)
2062 (define-key map
"\C-a" 'maxima-apropos
)
2063 (define-key map
"p" 'maxima-apropos-help
)
2064 (define-key map
"\C-p" 'maxima-apropos-help
)
2065 (setq maxima-help-map map
)))
2069 ;;; This next functions are from hippie-expand.el
2070 (defun maxima-he-capitalize-first (str)
2072 (if (string-match "\\Sw*\\(\\sw\\).*" str
)
2073 (let ((res (downcase str
))
2074 (no (match-beginning 1)))
2075 (aset res no
(upcase (aref str no
)))
2079 (defun maxima-he-ordinary-case-p (str)
2080 (or (string= str
(downcase str
))
2081 (string= str
(upcase str
))
2082 (string= str
(capitalize str
))
2083 (string= str
(maxima-he-capitalize-first str
))))
2086 (defun maxima-he-transfer-case (from-str to-str
)
2087 (cond ((string= from-str
(substring to-str
0 (min (length from-str
)
2090 ((not (maxima-he-ordinary-case-p to-str
))
2092 ((string= from-str
(downcase from-str
))
2094 ((string= from-str
(upcase from-str
))
2096 ((string= from-str
(maxima-he-capitalize-first from-str
))
2097 (maxima-he-capitalize-first to-str
))
2098 ((string= from-str
(capitalize from-str
))
2099 (capitalize to-str
))
2103 ;;; The next functions are from comint.el in cvs emacs
2105 (not maxima-running-xemacs
)
2106 (<= emacs-major-version
21)
2108 (< emacs-major-version
21)
2109 (< emacs-minor-version
3)))
2110 (defun comint-dynamic-list-completions (completions)
2111 "List in help buffer sorted COMPLETIONS.
2112 Typing SPC flushes the help buffer."
2113 (let ((window (get-buffer-window "*Completions*")))
2114 (setq completions
(sort completions
'string-lessp
))
2115 (if (and (eq last-command this-command
)
2116 window
(window-live-p window
) (window-buffer window
)
2117 (buffer-name (window-buffer window
))
2118 ;; The above tests are not sufficient to detect the case where we
2119 ;; should scroll, because the top-level interactive command may
2120 ;; not have displayed a completions window the last time it was
2121 ;; invoked, and there may be such a window left over from a
2122 ;; previous completion command with a different set of
2123 ;; completions. To detect that case, we also test that the set
2124 ;; of displayed completions is in fact the same as the previously
2127 (buffer-local-value 'comint-displayed-dynamic-completions
2128 (window-buffer window
))))
2129 ;; If this command was repeated, and
2130 ;; there's a fresh completion window with a live buffer,
2131 ;; and this command is repeated, scroll that window.
2132 (with-current-buffer (window-buffer window
)
2133 (if (pos-visible-in-window-p (point-max) window
)
2134 (set-window-start window
(point-min))
2135 (save-selected-window
2136 (select-window window
)
2138 ;; Display a completion list for the first time.
2139 (setq comint-dynamic-list-completions-config
2140 (current-window-configuration))
2141 (with-output-to-temp-buffer "*Completions*"
2142 (display-completion-list completions
))
2143 (message "Type space to flush; repeat completion command to scroll"))
2145 ;; Read the next key, to process SPC.
2147 (if (with-current-buffer (get-buffer "*Completions*")
2148 (set (make-local-variable 'comint-displayed-dynamic-completions
)
2150 (setq key
(read-key-sequence nil
)
2152 (and (consp first
) (consp (event-start first
))
2153 (eq (window-buffer (posn-window (event-start first
)))
2154 (get-buffer "*Completions*"))
2155 (eq (key-binding key
) 'mouse-choose-completion
)))
2156 ;; If the user does mouse-choose-completion with the mouse,
2157 ;; execute the command, then delete the completion window.
2159 (mouse-choose-completion first
)
2160 (set-window-configuration comint-dynamic-list-completions-config
))
2161 (unless (eq first ?\
)
2162 (setq unread-command-events
(listify-key-sequence key
)))
2163 (unless (eq first ?
\t)
2164 (set-window-configuration comint-dynamic-list-completions-config
)))))))
2167 (defun maxima-complete-symbol ()
2168 "Complete word from list of candidates.
2169 A completions listing will be shown in a help buffer
2170 if completion is ambiguous."
2171 (let* ((comint-completion-addsuffix nil
)
2172 (stub (buffer-substring-no-properties
2173 (maxima-name-beginning) (point)))
2174 (completions (all-completions (downcase stub
) maxima-symbols
)))
2175 (comint-dynamic-simple-complete stub completions
)))
2177 (defun maxima-complete-filename ()
2178 "Complete the filename."
2179 (comint-dynamic-complete-filename))
2181 (defun maxima-complete ()
2182 "Complete the current object, depending on context."
2184 (let* ((pmin (maxima-form-beginning-position))
2185 (pps (parse-partial-sexp pmin
(point))))
2187 ;; complete filename if the point is in a string
2189 (maxima-complete-filename))
2190 ;; Otherwise, complete the symbol
2192 (maxima-complete-symbol)))))
2194 ;; ;;; Use hippie-expand to help with completions
2195 ;; (require 'hippie-exp)
2197 ;; (defun maxima-he-try (old)
2200 ;; ;;; let beg be the beginning of the word
2202 ;; (he-init-string (maxima-name-beginning) (point))
2203 ;; (setq he-expand-list
2204 ;; (all-completions (downcase he-search-string) maxima-symbols))
2205 ;; (setq he-expand-list
2206 ;; (mapcar (function
2207 ;; (lambda (x) (he-transfer-case he-search-string x)))
2209 ;; (if he-expand-list
2210 ;; (he-substitute-string (car he-expand-list))
2212 ;; (setq he-expand-list (cdr he-expand-list))
2213 ;; (if he-expand-list
2214 ;; (he-substitute-string (car he-expand-list))
2215 ;; (he-reset-string)
2218 ;; (fset 'maxima-dynamic-complete
2219 ;; (make-hippie-expand-function '(maxima-he-try)))
2223 (defun maxima-mark-form ()
2224 "Make the current form as the region."
2226 (maxima-goto-beginning-of-form)
2227 (set-mark (maxima-form-end-position-or-point-max)))
2229 (defun maxima-check-commas (beg end
)
2230 "Check to see if there is a stray comma at the beginning or end."
2231 (let ((commapt nil
))
2234 (maxima-forward-over-comment-whitespace)
2235 (if (looking-at ",")
2236 (setq commapt
(point))
2238 (maxima-back-over-comment-whitespace)
2239 (when (save-excursion
2241 (looking-at "[;$]"))
2243 (maxima-back-over-comment-whitespace))
2245 (if (looking-at ",")
2246 (setq commapt
(point)))))
2249 (message "Misplaced comma")
2254 (defun maxima-check-parens (beg end
)
2255 "Check to make sure that the parentheses are balanced in the region."
2257 (let* ((tmpfile (maxima-make-temp-name))
2258 (tmpbuf (get-buffer-create tmpfile
))
2259 (string (buffer-substring-no-properties beg end
))
2268 (maxima-remove-kill-buffer-hooks)
2269 (modify-syntax-entry ?
/ ". 14")
2270 (modify-syntax-entry ?
* ". 23")
2272 (goto-char (point-min))
2273 (while (and (not errmessage
)
2274 (setq match
(maxima-re-search-forward "[][()]" end
)))
2275 (unless (save-excursion
2277 (maxima-escaped-char-p))
2279 ((string= match
"(")
2280 (setq parenstack
(cons (cons 1 (1- (point))) parenstack
)))
2281 ((string= match
"[")
2282 (setq parenstack
(cons (cons 2 (1- (point))) parenstack
)))
2283 ((string= match
")")
2286 (setq errmessage
"Unmatched close parenthesis")
2287 (setq pt
(1- (point))))
2288 ((= (caar parenstack
) 1)
2289 (setq parenstack
(cdr parenstack
)))
2290 ((= (caar parenstack
) 2)
2291 (setq errmessage
"Open bracket closed by parenthesis")
2292 (setq pt
(1- (point))))))
2293 ((string= match
"]")
2296 (setq errmessage
"Unmatched close bracket")
2297 (setq pt
(1- (point))))
2298 ((= (caar parenstack
) 2)
2299 (setq parenstack
(cdr parenstack
)))
2300 ((= (caar parenstack
) 1)
2301 (setq errmessage
"Open parenthesis closed by bracket")
2302 (setq pt
(1- (point))))))))))
2303 (kill-buffer tmpbuf
)
2305 ((not (or parenstack errmessage
))
2306 ; (message "Parenthesis and brackets match")
2309 (message errmessage
)
2310 (goto-char (1- (+ beg pt
)))
2314 ((= (caar parenstack
) 1)
2315 (message "Unmatched open parenthesis")
2316 (goto-char (1- (+ beg
(cdar parenstack
))))
2319 (message "Unmatched open bracket")
2320 (goto-char (+ beg
(cdar parenstack
)))
2324 (defun maxima-check-form-parens ()
2325 "Check to see if the parentheses in the current form are balanced."
2327 (maxima-check-parens (maxima-form-beginning-position)
2328 (maxima-form-end-position-or-point-max)))
2330 (defun maxima-load-file (file)
2331 "Prompt for a Maxima file to load."
2332 (interactive "fMaxima file: ")
2333 (maxima-string (concat "load(\"" (expand-file-name file
) "\");")))
2335 (defun maxima-load-current-file ()
2336 "Load the current file into Maxima."
2338 (maxima-string (concat "load(\"" buffer-file-name
"\");")))
2340 ;;; For highlighting the region being sent
2342 (defun maxima-mode-add-highlight ()
2343 (maxima-mode-remove-highlight)
2344 (if (and maxima-mode-region-begin maxima-mode-region-end
)
2345 (if maxima-running-xemacs
2347 (setq maxima-mode-highlight
2349 maxima-mode-region-begin
2350 maxima-mode-region-end
))
2351 (set-extent-property maxima-mode-highlight
'face
'highlight
))
2352 (setq maxima-mode-highlight
2354 maxima-mode-region-begin
2355 maxima-mode-region-end
))
2356 (overlay-put maxima-mode-highlight
'face
'highlight
)))
2357 (setq maxima-mode-region-begin nil
)
2358 (setq maxima-mode-region-end nil
))
2360 (defun maxima-mode-remove-highlight ()
2361 (when maxima-mode-highlight
2362 (if maxima-running-xemacs
2363 (delete-extent maxima-mode-highlight
)
2364 (delete-overlay maxima-mode-highlight
))
2365 (setq maxima-mode-highlight nil
)))
2367 (defun maxima-mode-add-remove-highlight ()
2369 (eq this-command
'maxima-send-region
)
2370 (eq this-command
'maxima-send-buffer
)
2371 (eq this-command
'maxima-send-line
)
2372 (eq this-command
'maxima-send-form
)
2373 (eq this-command
'maxima-send-previous-form
)
2374 (eq this-command
'maxima-send-previous-form-and-goto-end-of-form
)
2375 (eq this-command
'maxima-send-full-line
)
2376 (eq this-command
'maxima-send-full-line-and-goto-next-form
)
2377 (eq this-command
'maxima-send-completed-region
)
2378 (eq this-command
'maxima-send-completed-region-and-goto-next-form
)
2379 (eq this-command
'maxima-minibuffer-on-region
)
2380 (eq this-command
'maxima-minibuffer-on-form
)
2381 (eq this-command
'maxima-minibuffer-on-line
))
2382 (maxima-mode-add-highlight)
2383 (maxima-mode-remove-highlight)))
2387 (defvar maxima-mode-syntax-table nil
"")
2389 (if (not maxima-mode-syntax-table
)
2391 (setq maxima-mode-syntax-table
(make-syntax-table))
2392 (modify-syntax-entry ?_
"w" maxima-mode-syntax-table
)
2393 (modify-syntax-entry ?%
"w" maxima-mode-syntax-table
)
2394 (modify-syntax-entry ??
"w" maxima-mode-syntax-table
)
2395 ; (modify-syntax-entry ?\_ "w" maxima-mode-syntax-table)
2397 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2401 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2405 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2409 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2411 (modify-syntax-entry ?
" " maxima-mode-syntax-table
)
2412 (modify-syntax-entry ?
\t " " maxima-mode-syntax-table
)
2413 (modify-syntax-entry ?
` "' " maxima-mode-syntax-table
)
2414 (modify-syntax-entry ?
' "' " maxima-mode-syntax-table
)
2415 (modify-syntax-entry ?
, "' " maxima-mode-syntax-table
)
2416 (modify-syntax-entry ?.
"w" maxima-mode-syntax-table
)
2417 (modify-syntax-entry ?
# "' " maxima-mode-syntax-table
)
2418 (modify-syntax-entry ?
\\ "\\" maxima-mode-syntax-table
)
2419 (modify-syntax-entry ?
/ ". 14" maxima-mode-syntax-table
)
2420 (modify-syntax-entry ?
* ". 23" maxima-mode-syntax-table
)
2421 (modify-syntax-entry ?
+ "." maxima-mode-syntax-table
)
2422 (modify-syntax-entry ?-
"." maxima-mode-syntax-table
)
2423 (modify-syntax-entry ?
= "." maxima-mode-syntax-table
)
2424 (modify-syntax-entry ?
< "." maxima-mode-syntax-table
)
2425 (modify-syntax-entry ?
> "." maxima-mode-syntax-table
)
2426 (modify-syntax-entry ?
& "." maxima-mode-syntax-table
)
2427 (modify-syntax-entry ?|
"." maxima-mode-syntax-table
)
2428 (modify-syntax-entry ?
\" "\" " maxima-mode-syntax-table
)
2429 (modify-syntax-entry ?
\\ "\\ " maxima-mode-syntax-table
)
2430 (modify-syntax-entry ?\
( "() " maxima-mode-syntax-table
)
2431 (modify-syntax-entry ?\
) ")( " maxima-mode-syntax-table
)
2432 (modify-syntax-entry ?\
[ "(] " maxima-mode-syntax-table
)
2433 (modify-syntax-entry ?\
] ")[ " maxima-mode-syntax-table
)))
2438 (defvar maxima-mode-map nil
2439 "The keymap for maxima-mode")
2443 (let ((map (make-sparse-keymap)))
2445 (define-key map
"\M-\C-a" 'maxima-goto-beginning-of-form-interactive
)
2446 (define-key map
"\M-\C-e" 'maxima-goto-end-of-form-interactive
)
2447 (define-key map
"\M-\C-b" 'maxima-goto-beginning-of-list-interactive
)
2448 (define-key map
"\M-\C-f" 'maxima-goto-end-of-list-interactive
)
2450 (define-key map
"\C-c\C-p" 'maxima-display-buffer
)
2451 (define-key map
"\C-c\C-r" 'maxima-send-region
)
2452 (define-key map
"\C-c\C-b" 'maxima-send-buffer
)
2453 (define-key map
"\C-c\C-c" 'maxima-send-line
)
2454 (define-key map
"\C-c\C-e" 'maxima-send-previous-form
)
2455 (define-key map
"\C-c\C-s" 'maxima-send-previous-form-and-goto-end-of-form
)
2456 (define-key map
[(control return
)]
2457 'maxima-send-full-line-and-goto-next-form
)
2458 (define-key map
[(meta return
)]
2459 'maxima-send-completed-region-and-goto-next-form
)
2460 (define-key map
[(control meta return
)] 'maxima-send-buffer
)
2461 (define-key map
"\C-c\C-k" 'maxima-stop
)
2462 (define-key map
"\C-c\C-q" 'maxima-clear-queue
)
2463 (define-key map
"\C-c\C-l" 'maxima-load-file
)
2464 (define-key map
"\C-c\C-f" 'maxima-load-current-file
)
2466 ;(if maxima-use-dynamic-complete
2467 ; (define-key map (kbd "M-TAB") 'maxima-dynamic-complete)
2468 (define-key map
(kbd "M-TAB") 'maxima-complete
)
2470 (define-key map
"\C-c;" 'comment-region
)
2471 (define-key map
"\C-c:" 'maxima-uncomment-region
)
2472 (define-key map
"\M-;" 'maxima-insert-short-comment
)
2473 (define-key map
"\C-c*" 'maxima-insert-long-comment
)
2475 ; (define-key map "\t" 'maxima-reindent-line)
2476 (define-key map
"\C-m" 'maxima-return
)
2477 (define-key map
"\M-\C-q" 'maxima-indent-form
)
2478 ; (define-key map [(control tab)] 'maxima-untab)
2480 (define-key map
"\C-c\C-d" maxima-help-map
)
2481 (define-key map
[(f12)] 'maxima-help
)
2482 (define-key map
[(meta f12
)] 'maxima-apropos
)
2484 (define-key map
"\C-c\C-nr" 'maxima-minibuffer-on-region
)
2485 (define-key map
"\C-c\C-nl" 'maxima-minibuffer-on-line
)
2486 (define-key map
"\C-c\C-nf" 'maxima-minibuffer-on-form
)
2488 (define-key map
"\M-h" 'maxima-mark-form
)
2489 (define-key map
"\C-c\)" 'maxima-check-parens
)
2490 (define-key map
[(control c
) (control \
))] 'maxima-check-form-parens
)
2491 ; (define-key map "\C-cC-\)" 'maxima-check-form-parens)
2492 (define-key map
"\177" 'backward-delete-char-untabify
)
2493 (setq maxima-mode-map map
)))
2497 (easy-menu-define maxima-mode-menu maxima-mode-map
"Maxima mode menu"
2500 ["Beginning of form" maxima-goto-beginning-of-form-interactive t
]
2501 ["End of form" maxima-goto-end-of-form-interactive t
]
2502 ["Beginning of sexp" maxima-goto-beginning-of-list-interactive t
]
2503 ["End of sexp" maxima-goto-end-of-list-interactive t
])
2505 ["Start process" maxima-start t
]
2506 ["Send region" maxima-send-region t
]
2507 ["Send buffer" maxima-send-buffer t
]
2508 ["Send line" maxima-send-line t
]
2509 ["Send form" maxima-send-form t
]
2510 ["Load file" maxima-load-file t
]
2512 ["Display buffer" maxima-display-buffer t
]
2514 ["Kill process" maxima-stop t
])
2516 ; ["Change to basic" (maxima-change-indent-style "b")
2517 ; (not (eq maxima-newline-style 'basic))]
2518 ["Change to standard" (maxima-change-indent-style "s")
2519 (not (eq maxima-indent-style
'standard
))]
2520 ["Change to smart" (maxima-change-indent-style "p")
2521 (not (eq maxima-indent-style
'perhaps-smart
))])
2523 ["Mark form" maxima-mark-form t
]
2524 ["Check parens in region" maxima-check-parens t
]
2525 ["Check parens in form" maxima-check-form-parens t
]
2526 ["Comment region" comment-region t
]
2527 ["Uncomment region" maxima-uncomment-region t
])
2529 ["Maxima info" maxima-info t
]
2530 ["Help" maxima-help t
])))
2534 ;;;; (These are used in both maxima-mode and inferior-maxima-mode).
2536 (defvar maxima-mode-abbrev-table nil
"")
2538 (defun maxima-mode-variables ()
2539 "Sets all the necessary variables for maxima-mode"
2540 (set-syntax-table maxima-mode-syntax-table
)
2541 (setq local-abbrev-table maxima-mode-abbrev-table
)
2542 (make-local-variable 'paragraph-start
)
2543 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
2544 (make-local-variable 'paragraph-separate
)
2545 (setq paragraph-separate paragraph-start
)
2546 (make-local-variable 'indent-line-function
)
2547 (setq indent-line-function
'maxima-indent-line
)
2548 (make-local-variable 'indent-tabs-mode
)
2549 (unless maxima-use-tabs
2550 (setq indent-tabs-mode nil
))
2551 (make-local-variable 'case-fold-search
)
2552 (setq case-fold-search t
)
2553 (make-local-variable 'comment-start
)
2554 (setq comment-start
"/*")
2555 (make-local-variable 'comment-end
)
2556 (setq comment-end
"*/")
2557 (make-local-variable 'comment-start-skip
)
2558 (setq comment-start-skip
"/\\*+ *")
2559 (make-local-variable 'comment-column
)
2560 (setq comment-column
40)
2561 (make-local-variable 'comment-indent-function
)
2562 (setq comment-indent-function
'comment-indent-default
)
2563 (setq imenu-generic-expression
2564 (list '(nil "^ *\\([a-zA-Z0-9_]*\\) *(.*) *:=" 1))))
2569 (defun maxima-mode ()
2570 "Major mode for editing Maxima code.
2572 Maxima mode provides the following motion commands:
2573 \\[maxima-goto-beginning-of-form-interactive]: Move to the beginning of the form.
2574 \\[maxima-goto-end-of-form-interactive]: Move to the end of the form.
2575 \\[maxima-goto-beginning-of-list-interactive]: Move to the beginning of the sexp.
2576 \\[maxima-goto-end-of-list-interactive]: Move to the end of the sexp.
2578 and the following miscellaneous commands.
2579 \\[maxima-mark-form]: Mark the current form
2580 \\[maxima-check-parens]: Check the current region for balanced parentheses.
2581 \\[maxima-check-form-parens]: Check the current form for balanced parentheses.
2583 Maxima mode has the following completions commands:
2584 M-TAB: Complete the Maxima symbol as much as possible, providing
2585 a completion buffer if there is more than one possible completion.
2587 Portions of the buffer can be sent to a Maxima process. (If a process is
2588 not running, one will be started.)
2589 \\[maxima-send-region]: Send the region to Maxima.
2590 \\[maxima-send-buffer]: Send the buffer to Maxima.
2591 \\[maxima-send-line]: Send the line to Maxima.
2592 \\[maxima-send-form]: Send the form to Maxima.
2593 \\[maxima-send-full-line-and-goto-next-form]: Send the smallest set of lines which contains
2594 the cursor and contains no incomplete forms, and go to the next form.
2595 \\[maxima-send-completed-region-and-goto-next-form]: As above, but with
2596 the region instead of the current line.
2597 \\[maxima-load-file] will prompt for a filename and load it into Maxima
2598 When something is sent to Maxima, a buffer running an inferior Maxima
2599 process will appear. It can also be made to appear by using the command
2600 \\[maxima-display-buffer].
2601 If an argument is given to a command to send information to Maxima,
2602 the region (buffer, line, form) will first be checked to make sure
2603 the parentheses are balanced.
2604 The Maxima process can be killed, after asking for confirmation
2605 with \\[maxima-stop]. To kill without confirmation, give \\[maxima-stop]
2608 By default, indentation will be to the same level as the
2609 previous line, with an additional space added for open parentheses.
2610 The behaviour of indent can be changed by the command
2611 \\[maxima-change-indent-style]. The possibilities are:
2612 Standard: Standard indentation.
2613 Perhaps smart: Tries to guess an appropriate indentation, based on
2614 open parentheses, \"do\" loops, etc.
2615 The default can be set by setting the value of the variable
2616 \"maxima-indent-style\" to either 'standard or 'perhaps-smart.
2617 In both cases, \\[maxima-untab] will remove a level of indentation.
2619 To get help on a Maxima topic, use:
2621 To read the Maxima info manual, use:
2623 To get help with the symbol under point, use:
2624 \\[maxima-completion-help].
2625 To get apropos with the symbol under point, use:
2626 \\[maxima-apropos-help].
2631 (kill-all-local-variables)
2632 (setq major-mode
'maxima-mode
)
2633 (setq mode-name
"Maxima")
2634 (use-local-map maxima-mode-map
)
2635 (maxima-mode-variables)
2637 ((eq maxima-newline-style
'basic
)
2638 (setq maxima-indent-style
'standard
))
2639 ((eq maxima-newline-style
'standard
)
2640 (setq maxima-indent-style
'standard
))
2641 ((eq maxima-newline-style
'perhaps-smart
)
2642 (setq maxima-indent-style
'perhaps-smart
)))
2643 (easy-menu-add maxima-mode-menu maxima-mode-map
)
2644 (if maxima-running-xemacs
2645 (add-local-hook 'post-command-hook
2646 'maxima-mode-add-remove-highlight
)
2647 (add-hook 'post-command-hook
2648 'maxima-mode-add-remove-highlight nil t
))
2649 (run-hooks 'maxima-mode-hook
))
2651 (define-derived-mode maxima-noweb-mode maxima-mode
2653 (setq maxima-mode-type
'maxima-noweb-mode
))
2655 ;;;; Interacting with the Maxima process
2657 ;;; Checking on the process
2658 (defun inferior-maxima-running ()
2659 (and (processp inferior-maxima-process
)
2660 (eq (process-status inferior-maxima-process
) 'run
)))
2662 ;;; Sending the information
2663 (defun inferior-maxima-get-old-input ()
2666 (if (re-search-forward
2667 (concat "\\(^(\\(" maxima-outchar
"\\|" maxima-linechar
"\\)[0-9]*) \\)")
2669 (goto-char (match-beginning 0)))
2670 (skip-chars-backward " \t\n")
2673 (re-search-backward inferior-maxima-prompt
)
2674 (setq pt1
(match-end 0)))
2675 (buffer-substring-no-properties pt1 pt
)))
2677 (defun inferior-maxima-comint-send-input (&optional query
)
2678 "Take note of position, then send the input"
2680 (setq inferior-maxima-input-end
(point)))
2681 (setq inferior-maxima-waiting-for-output t
)
2682 (comint-send-input))
2684 ;;; This next function is a modified version of comint-strip-ctrl-m
2685 (defun inferior-maxima-remove-double-prompt (&optional string
)
2686 "Fix the double prompt that occasionally appears in XEmacs."
2687 (let ((pmark (process-mark inferior-maxima-process
))
2689 (set-buffer (process-buffer inferior-maxima-process
))
2690 (setq pos comint-last-output-start
)
2691 (if (marker-position pos
)
2695 (while (re-search-forward
2696 (concat "(" maxima-inchar
"[0-9]+).*\r") pmark t
)
2697 (replace-match "" t t
))))))
2699 (defun inferior-maxima-remove-double-input-prompt (&optional string
)
2700 "Fix the double prompt that occasionally appears in Emacs."
2701 (let ((pmark (process-mark inferior-maxima-process
))
2704 (set-buffer (process-buffer inferior-maxima-process
))
2705 (goto-char inferior-maxima-input-end
)
2707 (if (looking-at (concat "(" maxima-inchar
"[0-9]+)"))
2709 (if (looking-at "
")
2712 ;;; This next function will replace tabs in the output by spaces
2713 ;; untabify isn't defined in xemacs
2714 (unless (fboundp 'untabify
)
2715 (defun untabify (start end
)
2716 "Convert all tabs in region to multiple spaces, preserving columns.
2717 Called non-interactively, the region is specified by arguments
2718 START and END, rather than by the position of point and mark.
2719 The variable `tab-width' controls the spacing of tab stops."
2723 (narrow-to-region (point-min) end
)
2725 (while (search-forward "\t" nil t
) ; faster than re-search
2727 (let ((tab-beg (point))
2728 (indent-tabs-mode nil
)
2730 (skip-chars-forward "\t")
2731 (setq column
(current-column))
2732 (delete-region tab-beg
(point))
2733 (indent-to column
)))))))
2735 (defun inferior-maxima-replace-tabs-by-spaces (&optional string
)
2736 "Replace tabs in the Maxima output by spaces."
2738 (set-buffer (process-buffer inferior-maxima-process
))
2739 (if (marker-position comint-last-output-start
)
2740 (setq beg comint-last-output-start
)
2741 (setq beg
(point-min)))
2743 (process-mark inferior-maxima-process
))))
2745 (defun inferior-maxima-wait-for-output ()
2746 "Wait for output from the Maxima process."
2748 inferior-maxima-waiting-for-output
2749 (inferior-maxima-running))
2750 (accept-process-output inferior-maxima-process
))
2751 (if maxima-running-xemacs
2753 (sit-for 0 inferior-maxima-after-output-wait
)))
2755 (defun inferior-maxima-output-filter (str)
2756 "Look for a new input prompt"
2758 (string-match "? *$" str
)
2759 (not (string-match (concat "(" maxima-outchar
"[0-9]+)") str
)))
2760 (maxima-ask-question str
))
2761 ((string-match inferior-maxima-prompt str
)
2762 (if (and inferior-maxima-process
(not (string= maxima-block
"")))
2763 (maxima-single-string (maxima-get-command))
2764 (if (not inferior-maxima-process
)
2765 (maxima-clear-queue))
2766 (setq inferior-maxima-waiting-for-output nil
)))))
2768 (defun inferior-maxima-sentinel (proc state
)
2769 "Write the input history when the process ends"
2770 (unless (string-match "^run" state
)
2771 (comint-write-input-ring)))
2773 (defun maxima-start ()
2774 "Start the Maxima process."
2776 (if (processp inferior-maxima-process
)
2777 (unless (eq (process-status inferior-maxima-process
) 'run
)
2778 (delete-process inferior-maxima-process
)
2779 (if (get-buffer "*maxima*")
2781 (set-buffer "*maxima*")
2783 (setq inferior-maxima-process nil
)))
2784 (unless (processp inferior-maxima-process
)
2785 (setq inferior-maxima-input-end
0)
2786 (setq inferior-maxima-waiting-for-output t
)
2789 (setq mbuf
(apply #'make-comint
"maxima" maxima-command nil maxima-args
))
2792 (setq inferior-maxima-process
(get-buffer-process mbuf
))
2793 (add-hook 'comint-output-filter-functions
2794 'inferior-maxima-output-filter nil t
)
2795 (add-hook 'comint-output-filter-functions
2796 'inferior-maxima-replace-tabs-by-spaces nil t
)
2797 ; (add-hook 'comint-output-filter-functions
2798 ; 'inferior-maxima-remove-double-input-prompt nil t)
2799 (if maxima-fix-double-prompt
2800 (add-hook 'comint-output-filter-functions
2801 'inferior-maxima-remove-double-prompt nil t
))
2802 (inferior-maxima-wait-for-output)
2803 (inferior-maxima-mode)))))
2805 (defun maxima-stop (&optional arg
)
2806 "Kill the currently running Maxima process."
2808 (if (processp inferior-maxima-process
)
2811 (delete-process inferior-maxima-process
)
2812 (kill-buffer "*maxima*")
2813 (setq inferior-maxima-process nil
))
2814 (if (y-or-n-p "Really quit Maxima? ")
2816 (delete-process inferior-maxima-process
)
2817 (kill-buffer "*maxima*")
2818 (setq inferior-maxima-process nil
))))))
2820 ;;; Sending information to the process
2822 (defun maxima-single-string (string)
2823 "Send a string to the Maxima process."
2824 (setq string
(maxima-strip-string-add-semicolon string
))
2826 ; (inferior-maxima-wait-for-output)
2827 (save-current-buffer
2828 (set-buffer (process-buffer inferior-maxima-process
))
2829 (goto-char (point-max))
2830 (let ((start (point)))
2832 (untabify start
(point)))
2833 (goto-char (point-max))
2834 (inferior-maxima-comint-send-input)
2835 (goto-char (point-max))))
2837 (defun maxima-ask-question (string)
2838 "Ask the question maxima wants answered."
2839 (let ((ans (read-string
2840 (concat (maxima-strip-string string
) " " ))))
2841 (unless (string-match "[;$]" ans
)
2842 (setq ans
(concat ans
";")))
2843 (setq ans
(maxima-strip-string ans
))
2844 (save-current-buffer
2845 (set-buffer (process-buffer inferior-maxima-process
))
2846 (goto-char (point-max))
2848 (inferior-maxima-comint-send-input t
)
2849 (goto-char (point-max)))))
2851 (defun maxima-get-command (&optional arg
)
2852 "Return the maxima command that's at the front of maxima-block.
2853 Remove it from the front of maxima-block.
2854 With an argument, use maxima-block-wait instead of maxima-block."
2855 (let* ((tmpfile (maxima-make-temp-name))
2856 (tmpbuf (get-buffer-create tmpfile
))
2861 (maxima-remove-kill-buffer-hooks)
2863 (insert maxima-block-wait
)
2864 (insert maxima-block
))
2865 (goto-char (point-min))
2866 (maxima-forward-over-comment-whitespace)
2868 (if (string-match "[$;]\\|:lisp"
2869 (buffer-substring-no-properties (point) (point-max)))
2871 (if (looking-at ":lisp")
2873 (search-forward ":lisp")
2875 (setq command
(buffer-substring-no-properties pt
(point))))
2876 (maxima-goto-end-of-form)
2877 (setq command
(buffer-substring-no-properties pt
(point))))
2878 (maxima-forward-over-comment-whitespace)
2880 (setq maxima-block-wait
2881 (maxima-strip-string-add-semicolon
2882 (buffer-substring-no-properties (point) (point-max))))
2884 (maxima-strip-string-add-semicolon
2885 (buffer-substring-no-properties (point) (point-max)))))
2886 (setq command
(buffer-substring-no-properties pt
(point))))
2888 (setq maxima-block-wait
"")
2889 (setq maxima-block
"")))
2891 (if (string= maxima-block-wait
";") (setq maxima-block-wait
""))
2892 (if (string= maxima-block
";") (setq maxima-block
"")))
2893 (kill-buffer tmpbuf
))
2896 (defun maxima-send-block (stuff)
2897 "Send a block of code to Maxima."
2899 (setq stuff
(maxima-strip-string-add-semicolon stuff
))
2900 ; (unless (string-match (substring stuff -1) ";$")
2901 ; (setq stuff (concat stuff ";")))
2902 (if (string= maxima-block
"")
2904 (setq maxima-block stuff
)
2905 (maxima-single-string (maxima-get-command)))
2906 (setq maxima-block
(concat maxima-block stuff
))))
2908 (defun maxima-send-block-wait (stuff)
2909 "Send a block of code to Maxima; wait for it to finish.
2910 Return the last string sent."
2912 (if (not (string= maxima-block
""))
2913 (message "Maxima process currently busy.")
2914 (setq maxima-block-wait
(maxima-strip-string-add-semicolon stuff
))
2915 (while (not (string= maxima-block-wait
""))
2916 (maxima-single-string-wait (maxima-get-command t
)))))
2918 (defun maxima-clear-queue ()
2919 "Clear out the queue of commands to send to the maxima process."
2921 (setq maxima-block
"")
2922 (setq maxima-block-wait
""))
2924 ;;; Getting information back from Maxima.
2926 (defun maxima-last-output ()
2927 "Get the most recent output from Maxima."
2929 (inferior-maxima-wait-for-output)
2931 (set-buffer (process-buffer inferior-maxima-process
))
2933 (pmark (progn (goto-char (process-mark inferior-maxima-process
))
2937 (goto-char inferior-maxima-input-end
)
2940 (output (buffer-substring-no-properties beg pmark
)))
2944 (defun maxima-last-output-noprompt ()
2945 "Return the last Maxima output, without the prompts"
2947 (if (not (inferior-maxima-running))
2948 (maxima-last-output)
2949 (let* ((output (maxima-last-output))
2955 ;; Replace the output prompt with spaces
2956 (setq beg
(string-match
2957 (concat "\\(^(" maxima-outchar
"[0-9]*) \\)") output
))
2960 (setq end
(1+ (string-match ")" output beg
)))
2961 (setq newstring
(substring output
0 beg
))
2962 (setq k
(- end beg
))
2964 (setq newstring
(concat newstring
" "))
2970 (defun maxima-last-output-tex-noprompt ()
2971 "Return the last Maxima output, between the dollar signs."
2973 (let* ((output (maxima-last-output))
2974 (begtex (string-match "\\$\\$" output
))
2975 (endtex (string-match "\\$\\$" output
(1+ begtex
))))
2977 (substring output begtex
(+ endtex
2))
2981 ;;; Sending information to the process should be done through these
2982 ;; next five commands
2984 (defun maxima-single-string-wait (string)
2985 "Send a single string to the maxima process,
2986 waiting for output after."
2987 (inferior-maxima-wait-for-output)
2988 (maxima-single-string string
)
2989 (inferior-maxima-wait-for-output))
2991 (defun maxima-string (string)
2992 "Send a string to the Maxima process."
2993 (maxima-send-block string
))
2995 (defun maxima-region (beg end
)
2996 "Send the region to the Maxima process."
2997 (setq maxima-mode-region-begin beg
)
2998 (setq maxima-mode-region-end end
)
3000 (buffer-substring-no-properties beg end
)))
3002 ;;; Some functions to send commands to the process.
3004 (defun maxima-send-region (beg end
&optional arg
)
3005 "Send the current region to the Maxima process.
3006 With an argument, don't check the parentheses first."
3007 (interactive "r\nP")
3009 (maxima-region beg end
)
3010 (if (maxima-check-parens beg end
)
3011 (maxima-region beg end
)))
3012 (maxima-display-buffer))
3014 (defun maxima-send-buffer (&optional arg
)
3015 "Send the buffer to the Maxima process, after checking the parentheses.
3016 With an argument, don't check the parentheses."
3018 (maxima-send-region (point-min) (point-max) arg
))
3020 (defun maxima-send-line (&optional arg
)
3021 "Send the current line to the Maxima process, after checking parentheses.
3022 With an argument, don't check parentheses."
3024 (let ((b (maxima-line-beginning-position))
3025 (e (maxima-line-end-position)))
3026 (maxima-send-region b e arg
)))
3028 (defun maxima-send-form (&optional arg
)
3029 "Send the current form to the Maxima process, checking parentheses.
3030 With an argument, don't check parentheses."
3032 (maxima-send-region (maxima-form-beginning-position)
3033 (maxima-form-end-position-or-point-max) arg
))
3035 (defun maxima-send-previous-form (&optional arg
)
3036 "Send the previous form to the Maxima process, checking parentheses.
3037 With an argument, don't check parentheses."
3040 (if (maxima-re-search-backward "[;$]")
3041 (maxima-send-region (maxima-form-beginning-position)
3042 (maxima-form-end-position-or-point-max) arg
)
3043 (message "No previous form."))))
3045 (defun maxima-send-previous-form-and-goto-end-of-form (&optional arg
)
3046 "Send the previous form to the Maxima process and go to the end of form."
3048 (maxima-send-previous-form arg
)
3049 (maxima-goto-end-of-form-interactive))
3051 (defun maxima-send-full-line ()
3052 "Send the minimum number of lines such that the current is one of them,
3053 and such that no line contains an incomplete form."
3055 (let ((beg (point)) (end (point)))
3060 (maxima-goto-beginning-of-form)
3061 (while (< (point) beg
)
3065 (maxima-goto-beginning-of-form)))
3069 (while (and (< (maxima-form-beginning-position) end
) (< end
(point-max)))
3073 (setq end
(point))))
3074 (skip-chars-backward " \t;$")
3075 (if (re-search-forward "[;$]" end t
)
3076 (maxima-send-region beg
(point))
3077 (error "No ; or $ at end"))
3080 (defun maxima-send-full-line-and-goto-next-form ()
3081 "Do a maxima-send-full-line and go to the beginning of the next form."
3083 (goto-char (maxima-send-full-line))
3084 (maxima-goto-beginning-of-form))
3086 (defun maxima-send-completed-region (beg end
)
3087 "Send the marked region, but complete possibly non-complete forms at the bounderies."
3088 (interactive "r\nP")
3093 (setq beg1
(maxima-form-beginning-position))
3095 (setq end1
(maxima-form-end-position-or-point-max))
3096 (maxima-send-region beg1 end1
)
3099 (defun maxima-send-completed-region-and-goto-next-form (beg end
)
3100 "Do a maxima-send-completed-region and go to the beginning of the next form."
3101 (interactive "r\nP")
3102 (goto-char (maxima-send-completed-region beg end
))
3103 (maxima-goto-beginning-of-form))
3105 (defun maxima-display-buffer ()
3106 "Display the inferior-maxima-process buffer so the recent output is visible."
3108 (let ((origbuffer (current-buffer)))
3109 (if (not (processp inferior-maxima-process
))
3111 (pop-to-buffer (process-buffer inferior-maxima-process
))
3112 (goto-char (point-max))
3113 ; (recenter (universal-argument))
3114 (pop-to-buffer origbuffer
)))
3117 ;;;; The inferior Maxima process
3119 ;;; Completions from previous input
3121 ;; First, a function to take the comint-input-ring and return a
3122 ;; list of previous inputs
3124 (defun inferior-maxima-previous-inputs ()
3125 "Return a list of previous inputs."
3128 (comint-inputs (cddr comint-input-ring
))
3130 (while (and (< i comint-input-ring-size
)
3131 (not (null (aref comint-inputs i
))))
3132 (unless (member (aref comint-inputs i
) inputs
)
3133 (setq inputs
(cons (aref comint-inputs i
) inputs
)))
3137 (defun inferior-maxima-input-complete ()
3138 "Complete line from list of previous input."
3140 (let* ((stub (buffer-substring-no-properties
3141 (inferior-maxima-bol-position) (point)))
3142 (completions (all-completions (downcase stub
)
3143 (inferior-maxima-previous-inputs))))
3146 (function (lambda (x) (maxima-he-transfer-case stub x
))) completions
))
3147 (cond ((null completions
)
3148 (message "No completions of %s" stub
))
3149 ((= 1 (length completions
)) ; Gotcha!
3150 (let ((completion (car completions
)))
3151 (if (string-equal completion stub
)
3152 (message "Sole completion")
3153 (insert (substring completion
(length stub
)))
3154 (message "Completed"))))
3155 (t ; There's no unique completion.
3156 (comint-dynamic-list-completions completions
)))))
3158 (defun inferior-maxima-complete ()
3159 "Complete the current object, depending on context."
3161 (let* ((pmin (save-excursion
3162 (re-search-backward inferior-maxima-prompt
)
3164 (pps (parse-partial-sexp pmin
(point))))
3166 ;; complete filename if the point is in a string
3168 (maxima-complete-filename))
3169 ;; Otherwise, complete the symbol
3171 (maxima-complete-symbol)))))
3173 ;; (defun maxima-input-he-try (old)
3176 ;; ;;; let beg be the beginning of the word
3178 ;; (he-init-string (inferior-maxima-bol-position) (point))
3179 ;; (setq he-expand-list
3180 ;; (all-completions (downcase he-search-string) (maxima-previous-inputs)))
3181 ;; (setq he-expand-list
3182 ;; (mapcar (function
3183 ;; (lambda (x) (he-transfer-case he-search-string x)))
3185 ;; (if he-expand-list
3186 ;; (he-substitute-string (car he-expand-list))
3188 ;; (setq he-expand-list (cdr he-expand-list))
3189 ;; (if he-expand-list
3190 ;; (he-substitute-string (car he-expand-list))
3191 ;; (he-reset-string)
3194 ;; (fset 'maxima-dynamic-input-complete
3195 ;; (make-hippie-expand-function '(maxima-input-he-try)))
3197 ;;; Sending a line to the process while in the process buffer
3199 (defun inferior-maxima-check-and-send-line ()
3200 "Check the lines for mis-matched parentheses, then send the line."
3207 (skip-chars-backward " \t")
3209 (when (looking-at "[$;]")
3215 (re-search-backward inferior-maxima-prompt
)
3216 (setq pt1
(match-end 0)))
3217 (if (maxima-check-parens pt1 pt
)
3218 (inferior-maxima-comint-send-input)))
3219 (inferior-maxima-comint-send-input))))
3221 (defun inferior-maxima-send-line ()
3222 "Send the line to the Maxima process."
3224 (inferior-maxima-comint-send-input))
3226 (defun inferior-maxima-bol ()
3227 "Go to the beginning of the line, but past the prompt."
3229 (let ((eol (save-excursion (end-of-line) (point))))
3231 (if (and (looking-at inferior-maxima-prompt
)
3232 (<= (match-end 0) eol
))
3233 (goto-char (match-end 0)))))
3235 (defun inferior-maxima-bol-position ()
3237 (inferior-maxima-bol)
3241 ;;;; Inferior Maxima mode
3243 (define-derived-mode inferior-maxima-mode
3246 "Major mode for interacting with an inferior Maxima process.
3248 Return will check the line for balanced parentheses, and send line as input.
3249 Control return will send the line as input without checking for balanced
3252 M-TAB will complete the Maxima symbol as much as possible, providing
3253 a completion buffer if there is more than one possible completion.
3255 \\[maxima-smart-complete] will complete the input line, based on previous input lines.
3256 \\[maxima-help] will get help on a Maxima topic.
3257 \\[maxima-info] will bring up the Maxima info manual.
3258 \\[maxima-stop] will kill the process and the buffer, after asking for
3259 confirmation. To kill without confirmation, give \\[maxima-stop] an
3262 To scroll through previous commands,
3263 \\[comint-previous-input] will bring the previous input to the current prompt,
3264 \\[comint-next-input] will bring the next input to the prompt.
3265 \\[comint-previous-matching-input] will bring the previous input matching
3266 a regular expression to the prompt,
3267 \\[comint-next-matching-input] will bring the next input matching
3268 a regular expression to the prompt.
3270 ; (if maxima-use-full-color-in-process-buffer
3271 ; (inferior-maxima-font-setup))
3272 (setq comint-prompt-regexp inferior-maxima-prompt
)
3273 (setq comint-get-old-input
(function inferior-maxima-get-old-input
))
3274 (setq mode-line-process
'(": %s"))
3275 (maxima-mode-variables)
3277 (if (and (not maxima-running-xemacs
) (< emacs-major-version
21))
3278 (make-local-hook 'kill-buffer-hook
))
3279 (if maxima-running-xemacs
3280 (add-local-hook 'kill-buffer-hook
3283 (maxima-clear-queue)
3284 (if (processp inferior-maxima-process
)
3285 (delete-process inferior-maxima-process
))
3286 (setq inferior-maxima-process nil
)
3287 (run-hooks 'inferior-maxima-exit-hook
))))
3288 (add-hook 'kill-buffer-hook
3291 (maxima-clear-queue)
3292 (if (processp inferior-maxima-process
)
3293 (delete-process inferior-maxima-process
))
3294 (setq inferior-maxima-process nil
)
3295 (run-hooks 'inferior-maxima-exit-hook
))) t t
))
3296 (setq comint-input-ring-size maxima-input-history-length
)
3297 (if maxima-save-input-history
3299 (setq comint-input-ring-file-name maxima-input-history-file
)
3300 (comint-read-input-ring t
)
3301 (set-process-sentinel inferior-maxima-process
3302 'inferior-maxima-sentinel
)))
3303 (set (make-local-variable 'comint-prompt-read-only
) t
)
3304 (run-hooks 'inferior-maxima-mode-hook
))
3308 (define-key inferior-maxima-mode-map
"\C-a"
3309 'inferior-maxima-bol
)
3310 (define-key inferior-maxima-mode-map
"\C-m"
3311 'inferior-maxima-check-and-send-line
)
3312 (define-key inferior-maxima-mode-map
[(control return
)]
3313 'inferior-maxima-send-line
)
3314 (define-key inferior-maxima-mode-map
[(meta control tab
)]
3315 'inferior-maxima-input-complete
)
3316 (define-key inferior-maxima-mode-map
"\e\t" 'inferior-maxima-complete
)
3317 (define-key inferior-maxima-mode-map
"\177" 'backward-delete-char-untabify
)
3318 (define-key inferior-maxima-mode-map
"\C-c\C-k" 'maxima-stop
)
3319 (define-key inferior-maxima-mode-map
"\C-c\C-d" maxima-help-map
)
3323 (easy-menu-define inferior-maxima-mode-menu inferior-maxima-mode-map
3327 ["Maxima info" maxima-info t
]
3328 ["Help" maxima-help t
])
3330 ["Kill process" maxima-stop t
])))
3335 "Run Maxima interactively inside a buffer."
3338 (switch-to-buffer (process-buffer inferior-maxima-process
)))
3340 ;;; Interacting with Maxima outside of a maxima buffer
3342 (defun maxima-minibuffer ()
3343 "Communicate with Maxima through the minibuffer"
3346 (let ((input (read-string "Maxima: " nil maxima-minibuffer-history
))
3348 (twod (and maxima-minibuffer-2d
(not maxima-running-xemacs
))))
3349 (setq input
(maxima-strip-string-add-semicolon input
))
3351 (maxima-single-string-wait
3352 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3353 (maxima-single-string-wait
3354 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3355 (maxima-single-string-wait input
)
3356 (setq output
(maxima-last-output-noprompt))
3357 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3359 (setq output
(maxima-remove-whitespace-from-ends output
))
3360 ;; Strip the beginning and trailing newline
3361 (while (string-match "\\` *\n" output
)
3362 (setq output
(substring output
(match-end 0))))
3363 (while (string-match "\n *\\'" output
)
3364 (setq output
(substring output
0 (match-beginning 0)))))
3365 (setq output
(maxima-replace-in-string "%" "%%" output
))
3368 (defun maxima-minibuffer-delete-output (beg end
)
3369 (let ((mmom (maxima-minor-output-mark))
3370 (mmoe (maxima-minor-output-mark-end)))
3372 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmom
)
3373 (string= (match-string 0 mmom
) mmom
))
3374 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmoe
)
3375 (string= (match-string 0 mmoe
) mmoe
)))
3376 (message "Old output not deleted (improper delimiter).")
3380 (if (search-forward mmom end t
)
3382 (setq pt
(match-beginning 0))
3383 (search-forward mmoe
)
3384 (kill-region pt
(point)))
3386 (if (looking-at (concat "[ \n]*" (regexp-quote mmom
)))
3388 (search-forward mmoe
)
3389 (kill-region end
(point)))))
3392 (defun maxima-minibuffer-on-region (beg end
&optional arg
)
3393 "Send the current region to Maxima; display last output in minibuffer.
3394 With an argument, insert \" ==> \" into the current buffer,
3395 followed by the output, followed by \"\\\". In this case, any previous output
3397 (interactive "r\nP")
3407 (twod (and maxima-minibuffer-2d
(not maxima-running-xemacs
))))
3410 (maxima-forward-over-comment-whitespace)
3411 (setq realbeg
(point))
3412 (if (re-search-forward (maxima-minor-output-mark) end t
)
3414 (if (eq major-mode
'maxima-mode
)
3415 (- (point) (length maxima-mode-minor-output
))
3416 (- (point) (length maxima-minor-output
))))
3418 (maxima-back-over-comment-whitespace)
3419 (setq realend
(point))))
3420 (setq input
(maxima-strip-string-add-semicolon
3421 (buffer-substring-no-properties realbeg realend
)))
3423 (maxima-minibuffer-delete-output beg end
))
3424 (setq maxima-minor-mode-region-begin realbeg
)
3425 (setq maxima-minor-mode-region-end realend
)
3426 (when (or (not maxima-minor-mode-check-input
)
3428 (maxima-check-parens realbeg realend
)
3429 (maxima-check-commas realbeg realend
)))
3432 (maxima-single-string-wait
3433 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3434 (maxima-single-string-wait
3435 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3436 (maxima-send-block-wait input
)
3437 (setq output
(maxima-last-output-noprompt))
3438 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3440 (setq output
(maxima-remove-whitespace-from-ends output
))
3441 ;; Strip the beginning and trailing newline
3442 (while (string-match "\\` *\n" output
)
3443 (setq output
(substring output
(match-end 0))))
3444 (while (string-match "\n *\\'" output
)
3445 (setq output
(substring output
0 (match-beginning 0)))))
3447 (setq minibufferoutput
(maxima-replace-in-string "%" "%%" output
))
3448 (message minibufferoutput
))
3453 (if (looking-at "^")
3454 (setq realend
(1- realend
)))
3455 ;(delete-region realend end)
3457 (skip-chars-backward " \t\n")
3458 (when (not (= (point) realend
))
3459 (setq delreg
(buffer-substring-no-properties (point) realend
))
3460 (kill-region (point) realend
)
3462 ((< (length delreg
) 15)
3463 (setq delreg
(maxima-replace-in-string "\n" " " delreg
))
3464 (message (concat "\"" delreg
"\" killed")))
3467 (maxima-replace-in-string "\n" " "(substring delreg
0 5)))
3469 (maxima-replace-in-string "\n" " "(substring delreg -
5)))
3470 (message (concat "\"" delregbeg
" ... " delregend
"\" killed")))))
3471 (let ((ind (save-excursion
3475 (there (make-marker)))
3477 (string-match "\n" output
)
3478 (> (+ (current-column) (length output
)) fill-column
))
3481 (setq here
(point)))
3483 (insert (maxima-minor-output-mark) " " output
3484 (maxima-minor-output-mark-end))
3485 (set-marker there
(point))
3487 (goto-char (line-end-position))
3488 ; (fill-region (line-beginning-position) (point))
3491 (buffer-substring-no-properties here
(point)))
3494 (indent-region (point) there ind
)))
3496 (let ((ind (save-excursion
3502 (insert (maxima-minor-output-mark) "\n")
3504 (insert output
(maxima-minor-output-mark-end))
3505 (indent-region here
(point) ind
))))))))
3507 (defun maxima-minibuffer-on-line (&optional arg
)
3508 "Send the current line to Maxima; display last output in minibuffer.
3509 With an argument, insert \" ==> \" into the current buffer,
3510 followed by the output. In this case, anything in the line
3511 after any occurrence of \" ==> \" will be deleted."
3513 (maxima-minibuffer-on-region
3514 (maxima-line-beginning-position)
3515 (maxima-line-end-position)
3518 (defun maxima-minibuffer-on-form (&optional arg
)
3519 "Send the current form to Maxima; display last output in minibuffer.
3520 With an argument, insert \" ==> \" into the current buffer,
3521 followed by the output."
3523 (let ((beg (maxima-form-beginning-position))
3524 (end (maxima-form-end-position)))
3526 (when (re-search-backward "^[ \t]*$" beg t
)
3527 (maxima-forward-over-comment-whitespace)
3528 (setq beg
(point))))
3529 (maxima-minibuffer-on-region beg end arg
)))
3531 (defun maxima-minibuffer-on-determined-region (&optional arg
)
3532 "Send a determined region to Maxima; display the output in the minibuffer.
3533 The region is the region between `maxima-minor-prefix' and `maxima-minor-postfix'
3534 With an argument, insert \" ==> \" into the current buffer,
3535 followed by the output. In this case, anything in the determined region
3536 after any occurrence of \" ==> \" will be deleted."
3542 (if (re-search-backward maxima-minor-prefix nil t
)
3543 (setq beg
(match-end 0))
3544 (error "No beginning to determined region"))
3546 (if (re-search-forward maxima-minor-prefix nil t
)
3547 (setq end
(match-beginning 0))))
3548 (maxima-minibuffer-on-region beg end arg
)))
3550 (defun maxima-insert-last-output ()
3552 (maxima-single-string-wait
3553 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);")
3554 (let ((output (maxima-last-output-noprompt)))
3555 (maxima-single-string "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3556 (insert (maxima-remove-whitespace-from-ends output
))))
3558 (defun maxima-insert-last-output-tex ()
3560 (maxima-single-string-wait "tex(%);")
3561 (let ((output (substring (maxima-last-output-tex-noprompt) 2 -
3)))
3562 (maxima-single-string "block(linenum:linenum-2,%th(2));")
3565 ;;; The Maxima minor mode
3567 (defvar maxima-minor-mode-map nil
3568 "The keymap for maxima-minor-mode.")
3570 (if maxima-minor-mode-map
3572 (let ((map (make-sparse-keymap)))
3573 (define-key map
"\C-c=m" 'maxima-minibuffer
)
3574 (define-key map
"\C-c=e" 'maxima-minibuffer-on-determined-region
)
3575 (define-key map
"\C-c=l" 'maxima-minibuffer-on-line
)
3576 (define-key map
"\C-c=r" 'maxima-minibuffer-on-region
)
3577 (define-key map
"\C-c=f" 'maxima-minibuffer-on-form
)
3578 (define-key map
"\C-c=o" 'maxima-insert-last-output
)
3579 (define-key map
"\C-c=t" 'maxima-insert-last-output-tex
)
3580 (define-key map
"\C-c=d" maxima-help-map
)
3581 (setq maxima-minor-mode-map map
)))
3583 (unless (fboundp 'define-minor-mode
)
3584 (defalias 'define-minor-mode
'easy-mmode-define-minor-mode
))
3586 (define-minor-mode maxima-minor-mode
3587 "Toggle Maxima minor mode.
3588 With no argument, this command toggles the mode.
3589 Non-null prefix argument turns on the mode.
3590 Null prefix argument turns off the mode.
3592 When Maxima minor mode is enabled, the following keystrokes
3594 \\[maxima-minibuffer-on-determined-region]
3595 Send the region between the customizable regexps
3596 `maxima-minor-prefix' and `maxima-minor-postfix' to Maxima
3597 and display the result in the minibuffer.
3598 \\[maxima-minibuffer-on-line]
3599 Send the current line to Maxima and display the result in the minibuffer.
3600 \\[maxima-minibuffer-on-region]
3601 Send the current region to Maxima and display the result in the minibuffer.
3602 \\[maxima-minibuffer-on-form]
3603 Send the current form to Maxima and display the result in the minibuffer.
3604 (The form is the region between the preceding ; or $ and the subsequent
3606 With a prefix, the above commands will insert the output in the current
3607 buffer, preceded by \" ==> \" (customizable with `maxima-minor-output').
3608 \\[maxima-minibuffer]
3609 Prompt for an expression in the minibuffer, return result in minibuffer.
3610 \\[maxima-insert-last-output]
3611 Insert the last Maxima result into the current buffer.
3612 \\[maxima-insert-last-output-tex]
3613 Insert the last Maxima result in TeX form into the current buffer."
3614 ;; The initial value.; :initial-value
3616 ;; The indicator for the mode line.; :lighter
3620 ;;; For highlighting the region being sent
3622 (defun maxima-minor-mode-add-highlight ()
3623 (maxima-minor-mode-remove-highlight)
3624 (when (and maxima-minor-mode-region-begin
3625 maxima-minor-mode-region-end
)
3626 (if maxima-running-xemacs
3628 (setq maxima-minor-mode-highlight
3630 maxima-minor-mode-region-begin
3631 maxima-minor-mode-region-end
))
3632 (set-extent-property maxima-minor-mode-highlight
'face
'highlight
))
3633 (setq maxima-minor-mode-highlight
3635 maxima-minor-mode-region-begin
3636 maxima-minor-mode-region-end
))
3637 (overlay-put maxima-minor-mode-highlight
'face
'highlight
))
3638 (setq maxima-minor-mode-region-begin nil
)
3639 (setq maxima-minor-mode-region-end nil
)))
3641 (defun maxima-minor-mode-remove-highlight ()
3642 (when maxima-minor-mode-highlight
3643 (if maxima-running-xemacs
3644 (delete-extent maxima-minor-mode-highlight
)
3645 (delete-overlay maxima-minor-mode-highlight
))
3646 (setq maxima-minor-mode-highlight nil
)))
3648 (defun maxima-minor-mode-add-remove-highlight ()
3650 (eq this-command
'maxima-minibuffer-on-region
)
3651 (eq this-command
'maxima-minibuffer-on-determined-region
)
3652 (eq this-command
'maxima-minibuffer-on-form
)
3653 (eq this-command
'maxima-minibuffer-on-line
))
3654 (maxima-minor-mode-add-highlight)
3655 (maxima-minor-mode-remove-highlight)))
3657 (defun maxima-minor-mode-highlighting ()
3658 (if maxima-running-xemacs
3659 (add-local-hook 'post-command-hook
3660 'maxima-minor-mode-add-remove-highlight
)
3661 (add-hook 'post-command-hook
3662 'maxima-minor-mode-add-remove-highlight nil t
)))
3664 (add-hook 'maxima-minor-mode-hook
3665 'maxima-minor-mode-highlighting
)
3668 ;;; maxima.el ends here