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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 ;; You will need both maxima.el and maxima-font-lock.el
29 ;; This is a branch of William Schelter's maxima-mode.el
33 ;; To install, put this file (as well as maxima-font-lock.el)
34 ;; somewhere in your emacs load path.
35 ;; To make sure that `maxima.el' is loaded when necessary, whether to
36 ;; edit a file in maxima mode or interact with Maxima in an Emacs buffer,
38 ;; (autoload 'maxima-mode "maxima" "Maxima mode" t)
39 ;; (autoload 'maxima "maxima" "Maxima interaction" t)
40 ;; in your `.emacs' file. If you want any file ending in `.mac' to begin
41 ;; in `maxima-mode', for example, put the line
42 ;; (setq auto-mode-alist (cons '("\\.mac" . maxima-mode) auto-mode-alist))
43 ;; to your `.emacs' file.
45 ;; In any of the Maxima modes, to get help on a prompted for Maxima topic,
50 ;; To get help with the symbol under point, use ("d" for describe):
54 ;; For apropos help, use:
58 ;; To get apropos with the symbol under point, use:
62 ;; To read the Maxima info manual, use:
68 ;; (For Maxima minor mode, replace C-cC-d by C-c=d.)
71 ;; To put the current buffer into maxima-mode, type M-x maxima-mode
73 ;; Maxima mode provides the following motion commands:
74 ;; M-C-a: Move to the beginning of the form.
75 ;; M-C-e: Move to the end of the form.
76 ;; M-C-b: Move to the beginning of the list.
77 ;; M-C-f: Move to the end of the list.
79 ;; and the following miscellaneous commands.
80 ;; M-h: Mark the current form
81 ;; C-c): Check the current region for balanced parentheses.
82 ;; C-cC-): Check the current form for balanced parentheses.
84 ;; Maxima mode has the following completions command:
85 ;; M-TAB: Complete the Maxima symbol as much as possible, providing
86 ;; a completion buffer if there is more than one possible completion.
88 ;; Portions of the buffer can be sent to a Maxima process. (If a process is
89 ;; not running, one will be started.)
90 ;; C-cC-r: Send the region to Maxima.
91 ;; C-cC-b: Send the buffer to Maxima.
92 ;; C-cC-c: Send the line to Maxima.
93 ;; C-cC-e: Send the form to Maxima.
94 ;; C-RET: Send the smallest set of lines which contains
95 ;; the cursor and contains no incomplete forms, and go to the next form.
96 ;; M-RET: As above, but with the region instead of the current line.
97 ;; C-cC-l: Prompt for a file name to load into Maxima.
99 ;; When something is sent to Maxima, a buffer running an inferior Maxima
100 ;; process will appear. It can also be made to appear by using the command
102 ;; When a command is given to send information to Maxima, the region
103 ;; (buffer, line, form) is first checked to make sure the parentheses
104 ;; are balanced. With an argument, they will not be checked first.
105 ;; The Maxima process can be killed, after asking for confirmation
106 ;; with C-cC-k. To kill without confirmation, give C-cC-k
109 ;; By default, indentation will be to the same level as the
110 ;; previous line, with an additional space added for open parentheses.
111 ;; The behaviour of indent can be changed by the command
112 ;; M-x maxima-change-indent-style. The possibilities are:
113 ;; Standard: Simply indent
114 ;; Perhaps smart: Tries to guess an appropriate indentation, based on
115 ;; open parentheses, "do" loops, etc.
116 ;; The default can be set by setting the value of the variable
117 ;; "maxima-indent-style" to either 'standard or 'perhaps-smart.
118 ;; In both cases, M-x maxima-untab will remove a level of indentation.
120 ;; ** Maxima noweb mode **
121 ;; maxima-noweb-mode is a modification of maxima-mode that will work
122 ;; nicely with noweb-mode; namely, it will limit any relevant searches
123 ;; to the current chunk and treat <<...>> as word parts.
125 ;; ** Running Maxima interactively **
126 ;; To run Maxima interactively in a buffer, type M-x maxima
127 ;; In the Maxima process buffer,
128 ;; return will check the line for balanced parentheses, and send line as input.
129 ;; Control return will send the line as input without checking for balanced
132 ;; <M-tab> will complete the Maxima symbol as much as possible, providing
133 ;; a completion buffer if there is more than one possible completion.
135 ;; <C-M-tab> will complete the input line, based on previous input lines.
136 ;; C-c C-d will get help on a Maxima topic.
137 ;; C-c C-m will bring up the Maxima info manual.
138 ;; C-cC-k will kill the process and the buffer, after asking for
139 ;; confirmation. To kill without confirmation, give C-M-k an
142 ;; To scroll through previous commands,
143 ;; M-p will bring the previous input to the current prompt,
144 ;; M-n will bring the next input to the prompt.
145 ;; M-r will bring the previous input matching
146 ;; a regular expression to the prompt,
147 ;; M-s will bring the next input matching
148 ;; a regular expression to the prompt.
150 ;; ** Running Maxima from the minibuffer **
151 ;; The command M-x maxima-minibuffer
152 ;; will allow you to interact with Maxima from the minibuffer.
153 ;; The arrows will allow you to scroll through previous inputs.
155 ;; (autoload 'maxima-minibuffer "maxima" "Maxima in a minibuffer" t)
156 ;; in your .emacs will make sure the function is available.
157 ;; If the variable maxima-minibuffer-2d is non-nil, then the output
158 ;; will be in Maxima's 2d output form, otherwise it will be in
159 ;; Maxima's 1d output form. (For XEmacs, only the 1d form is available,
160 ;; since the minibuffer isn't resizable.)
161 ;; The command maxima-insert-last-output will insert
162 ;; the last maxima output into the current buffer; if the output is in 2d,
163 ;; this will look unpleasant. The command maxima-insert-last-output-tex
164 ;; will insert the TeX form of the output.
166 ;; ** Reading Maxima results in the minibuffer **
167 ;; The command `maxima-minibuffer-on-determined-region'
168 ;; will send the part of the current buffer containing the point and between
169 ;; the regexps `maxima-minor-prefix' and `maxima-minor-postfix' (currently
170 ;; both blank lines) to the Maxima process and insert the result in the
171 ;; minibuffer. With an argument, `maxima-minibuffer-in-determined-region'
172 ;; will also insert the output into the current buffer, after " ==> "
173 ;; and before "//". (The symbol ` ==> ' is the value of the customizable
174 ;; variable `maxima-minor-output' and "//" is the value of
175 ;; `maxima-minor-output-end'. The new output is inserted, these strings
176 ;; will be used to delete the old output.
177 ;; Outside of comments in maxima-mode, the opening and closing indicators
178 ;; are the values of `maxima-mode-minor-output' and
179 ;; `maxima-mode-minor-output-end', which by default are " /*==>" and
180 ;; " <==*/", respectively.
181 ;; The commands `maxima-minibuffer-on-region', `maxima-minibuffer-on-line'
182 ;; and `maxima-minibuffer-on-form' work similarly to
183 ;; `maxima-minibuffer-on-determined-region', but send the current region
184 ;; (respectively, the current line, current form) to Maxima and display
185 ;; the result in the minibuffer.
186 ;; (The form is the region between the preceding ; or $ and the subsequent
188 ;; Care must be taken when inserting the output into the current buffer
189 ;; with `maxima-minibuffer-on-region' and `maxima-minibuffer-on-form'.
190 ;; With `maxima-minibuffer-on-region', as with
191 ;; `maxima-minibuffer-on-determined-region' above, everything after any
192 ;; "==>" in the region will be ignored.
193 ;; What will typically happen with `maxima-minibuffer-on-region' and
194 ;; `maxima-minibuffer-on-form', however, is that new outputs will
195 ;; be inserted without old output being deleted.
197 ;; The commands for the Maxima-minibuffer interaction can be made
198 ;; available by putting
199 ;; (autoload 'maxima-minibuffer "maxima" "Interact with Maxima from the minibuffer" t)
200 ;; (autoload 'maxima-minibuffer-on-determined-region "maxima"
201 ;; "Send a information to Maxima, display the results in the minibuffer" t)
202 ;; (autoload 'maxima-minibuffer-on-region "maxima"
203 ;; "Send a information to Maxima, display the results in the minibuffer" t)
204 ;; (autoload 'maxima-minibuffer-on-line "maxima"
205 ;; "Send a information to Maxima, display the results in the minibuffer" t)
206 ;; (autoload 'maxima-minibuffer-on-form "maxima"
207 ;; "Send a information to Maxima, display the results in the minibuffer" t)
210 ;; ** Maxima minor mode **
211 ;; maxima-minor-mode provides convenient keybindings for the various
212 ;; interactions between Maxima and the minibuffer.
213 ;; It can be made easily available by placing
214 ;; (autoload 'maxima-minor-mode "maxima" "Maxima minor mode" t)
215 ;; in your .emacs, then M-x maxima-minor-mode will start the minor mode.
216 ;; (The autoloads for the individual function will not then be necessary.)
218 ;; `maxima-minibuffer-on-determined-region'
220 ;; `maxima-minibuffer-on-line'
222 ;; `maxima-minibuffer-on-region'
224 ;; `maxima-minibuffer-on-form'
226 ;; `maxima-minibuffer'
228 ;; `maxima-insert-last-output'
230 ;; `maxima-insert-last-output-tex'
235 (defvar maxima-running-xemacs
242 (require 'maxima-font-lock
)
244 ;;;; The variables that the user may wish to change
251 (defcustom maxima-inchar
"\\(C\\|%i\\)"
252 "*The character used for an input prompt."
256 (defcustom maxima-outchar
"\\(D\\|%o\\)"
257 "*The character used for an output prompt."
261 (defcustom maxima-linechar
"\\(E\\|%t\\)"
262 "*The character used for an intermediate output prompt."
266 (defcustom maxima-indent-amount
2
267 "*The amount of each indentation level in `maxima-mode'.
268 This is used after `then', etc."
272 (defcustom maxima-paren-indent-amount
1
273 "*The amount of indentation after a parenthesis."
277 (defcustom maxima-function-indent-amount
2
278 "*The amount of extra indentation to give within functions."
282 (defvar maxima-blockparen-indent-amount nil
)
283 (if maxima-blockparen-indent-amount
284 (setq maxima-function-indent-amount
285 maxima-blockparen-indent-amount
))
287 (defcustom maxima-continuation-indent-amount
2
288 "*The amount of extra indentation given when a line is continued."
292 (defcustom maxima-multiline-comment-indent-amount
2
293 "*The amount of extra indentation inside a multiline comment."
297 (defcustom maxima-dont-reindent-some-comments t
298 "*If non-nil, TAB will not change indentation of some comments,
299 namely those with nothing on the starting line past the `/*'."
303 (defcustom maxima-if-extra-indent-amount
0
304 "*The amount of extra indentation to give a \"then\" following an \"if\"."
308 ;(defcustom maxima-use-dynamic-complete nil
309 ; "*If non-nil, then M-TAB will complete words dynamically."
313 (defcustom maxima-indent-style
'standard
314 "*Determines how `maxima-mode' will handle tabs.
315 Choices are 'standard, 'perhaps-smart"
317 :type
'(choice :menu-tag
"Indent style"
320 (const perhaps-smart
)))
322 (defcustom maxima-return-style
'newline-and-indent
323 "*Determines how `maxima-mode' will handle RET.
324 Choices are 'newline, 'newline-and-indent, and 'reindent-then-newline-and-indent"
326 :type
'(choice :menu-tag
"Return style"
329 (const newline-and-indent
)
330 (const reindent-then-newline-and-indent
)))
332 (defvar maxima-newline-style nil
333 "For compatibility.")
335 (defcustom maxima-command
"maxima"
336 "*The command used to start Maxima. See also `maxima-args'."
340 (defcustom maxima-args nil
341 "*A list of extra arguments to pass to the `maxima-command'. Each
342 element in the list should be a distinct command-line option."
346 (defcustom maxima-use-tabs nil
347 "*If non-nil, indentation will use tabs."
351 (defcustom maxima-minibuffer-2d nil
352 "*If non-nil, use 2D output for maxima-minibuffer."
356 (defcustom maxima-use-full-color-in-process-buffer nil
357 "*If non-nil, font-lock the maxima process buffer."
361 (defcustom maxima-fix-double-prompt maxima-running-xemacs
362 "*If non-nil, fix the double prompt that sometimes appears in XEmacs."
366 (defcustom maxima-save-input-history nil
367 "*If non-nil, save the input history in a file."
371 (defcustom maxima-input-history-file
"~/.maxima_history"
372 "*A file to save the input history in."
376 (defcustom maxima-input-history-length
50
377 "*How many lines of history to save."
381 (defcustom maxima-minor-prefix
"^[ \t]*$"
382 "*A regexp to indicate the beginning of a region to send to Maxima
383 in maxima minor mode."
387 (defcustom maxima-minor-postfix
"^[ \t]*$"
388 "*A regexp to indicate the end of a region to send to Maxima
389 in maxima minor mode."
393 (defcustom maxima-minor-output
"==>"
394 "*A string to insert in the buffer right before the output."
398 (defcustom maxima-minor-output-end
" //"
399 "*A string to insert in the buffer right after the output."
403 (defcustom maxima-mode-minor-output
"/*==>"
404 "*A string to insert in the buffer right before the output."
408 (defcustom maxima-mode-minor-output-end
" <==*/"
409 "*A string to insert in the buffer right after the output."
413 (defcustom maxima-minor-mode-check-input t
414 "*Non-nil means check the input in Maxima minor mode before sending it."
418 (defun maxima-minor-output-mark ()
420 (eq major-mode
'maxima-mode
)
421 (not (maxima-in-comment-p)))
422 maxima-mode-minor-output
423 maxima-minor-output
))
425 (defun maxima-minor-output-mark-end ()
427 (eq major-mode
'maxima-mode
)
428 (not (maxima-in-comment-p)))
429 maxima-mode-minor-output-end
430 maxima-minor-output-end
))
432 ;;;; The other variables
434 ;; This variable seems to be necessary ...
435 (defvar inferior-maxima-after-output-wait
100)
437 (defvar maxima-temp-suffix
0
438 "Temporary filename suffix. Incremented by 1 for each filename.")
440 (defvar maxima-special-symbol-letters
"!:='")
442 (defvar maxima-minibuffer-history nil
)
444 (defvar maxima-block
"")
446 (defvar maxima-block-wait
"")
448 (defvar inferior-maxima-process nil
449 "The Maxima process.")
451 (defvar inferior-maxima-input-end
0
452 "The end of the latest input that was sent to Maxima.")
454 (defvar inferior-maxima-output-end
0)
456 (defvar inferior-maxima-waiting-for-output nil
)
458 (defvar inferior-maxima-exit-hook nil
)
460 (defvar inferior-maxima-prompt
461 (concat "\\(^(" maxima-inchar
462 "[0-9]*) \\)\\|\\(^MAXIMA> \\)\\|\\(^(dbm:[0-9]*) \\)")
463 ; \\(^[^#%)>]*[#%)>]+ *\\)"
464 "*Regexp to recognize prompts from the inferior Maxima") ; or lisp")
467 (defvar maxima-mode-highlight nil
)
469 (defvar maxima-mode-region-begin nil
)
471 (defvar maxima-mode-region-end nil
)
473 (defvar maxima-minor-mode-region-begin nil
)
475 (defvar maxima-minor-mode-region-end nil
)
477 (defvar maxima-minor-mode-highlight nil
)
479 (defvar maxima-minor-mode-bad-delimiter-regexp
"\\([ \t\n]+\\|[0-9]+\\)")
481 ;;;; Utility functions
483 (defun maxima-string-equal (str1 str2
)
484 (string= (downcase str1
) (downcase str2
)))
486 ;; This was taken from `replace-regexp-in-string' from subr.el in GNU emacs.
487 (defun maxima-replace-in-string (regexp rep string
)
488 "Replace all matches for REGEXP with REP in STRING."
489 (let ((l (length string
))
493 (while (and (< start l
) (string-match regexp string start
))
494 (setq mb
(match-beginning 0)
496 (when (= me mb
) (setq me
(min l
(1+ mb
))))
497 (string-match regexp
(setq str
(substring string mb me
)))
499 (cons (replace-match (if (stringp rep
)
501 (funcall rep
(match-string 0 str
)))
503 (cons (substring string start mb
) ; unmatched prefix
506 ;; Reconstruct a string from the pieces.
507 (setq matches
(cons (substring string start l
) matches
)) ; leftover
508 (apply #'concat
(nreverse matches
)))))
510 (defun maxima-remove-kill-buffer-hooks ()
511 "Remove the kill-buffer-hooks locally"
512 (if (or maxima-running-xemacs
(< emacs-major-version
21))
514 (make-local-hook 'kill-buffer-hook
)
515 (setq kill-buffer-hook nil
))
516 (let ((hooks kill-buffer-hook
))
518 (remove-hook 'kill-buffer-hook
(car hooks
) t
)
519 (setq hooks
(cdr hooks
))))))
521 (defun maxima-make-temp-name ()
522 "Return a unique filename."
523 (setq maxima-temp-suffix
(+ maxima-temp-suffix
1))
524 (concat (concat (make-temp-name "#mz") "-")
525 (int-to-string maxima-temp-suffix
)
528 (defun maxima-strip-string-beginning (string)
529 "Return STRING with whitespace and comments removed from the beginning."
530 (let* ((tmpfile (maxima-make-temp-name))
531 (tmpbuf (get-buffer-create tmpfile
))
535 (maxima-remove-kill-buffer-hooks)
536 (modify-syntax-entry ?
/ ". 14")
537 (modify-syntax-entry ?
* ". 23")
539 (goto-char (point-min))
540 (maxima-forward-over-comment-whitespace)
541 (setq out
(buffer-substring-no-properties (point) (point-max))))
545 (defun maxima-strip-string-end (string)
546 "Return STRING with whitespace and comments removed from the end."
547 (let* ((tmpfile (maxima-make-temp-name))
548 (tmpbuf (get-buffer-create tmpfile
))
552 (maxima-remove-kill-buffer-hooks)
553 (modify-syntax-entry ?
/ ". 14")
554 (modify-syntax-entry ?
* ". 23")
556 (goto-char (point-max))
557 (maxima-back-over-comment-whitespace)
558 (setq out
(buffer-substring-no-properties (point-min) (point))))
562 (defun maxima-strip-string (string)
563 "Return STRING with whitespace and comments removed from the ends."
564 (maxima-strip-string-beginning (maxima-strip-string-end string
)))
566 (defun maxima-strip-string-add-semicolon (string)
567 "Return STRING with whitespace and comments removed from the ends."
569 (maxima-strip-string-beginning (maxima-strip-string-end string
)))
572 (and (>= (length string
) 5)
573 (string= (substring string
0 5) ":lisp"))
574 (string= (substring string -
1) ";")
575 (string= (substring string -
1) "$"))
576 (setq string
(concat string
";")))
579 (defun maxima-remove-whitespace-from-ends (string)
580 "Return STRING with whitespace from the ends."
581 (let* ((tmpfile (maxima-make-temp-name))
582 (tmpbuf (get-buffer-create tmpfile
))
589 (maxima-remove-kill-buffer-hooks)
591 (goto-char (point-min))
592 (skip-chars-forward " \t\n")
594 (goto-char (point-max))
595 (skip-chars-backward " \t\n")
597 (setq out
(buffer-substring-no-properties beg end
)))
601 (defun maxima-remove-whitespace-from-beg (string)
602 "Return STRING with whitespace removed from the beginning."
603 (let* ((tmpfile (maxima-make-temp-name))
604 (tmpbuf (get-buffer-create tmpfile
))
611 (maxima-remove-kill-buffer-hooks)
613 (goto-char (point-min))
614 (skip-chars-forward " \t\n")
616 (setq out
(buffer-substring-no-properties beg
(point-max))))
620 (defun maxima-remove-whitespace-from-end (string)
621 "Return STRING with whitespace removed from the end."
622 (let* ((tmpfile (maxima-make-temp-name))
623 (tmpbuf (get-buffer-create tmpfile
))
630 (maxima-remove-kill-buffer-hooks)
632 (goto-char (point-max))
633 (skip-chars-backward " \t\n")
635 (setq out
(buffer-substring-no-properties (point-min) end
)))
639 ;;;; Functions that query position
640 (defun maxima-in-comment-p ()
641 "Non-nil means that the point is in a comment."
645 (search-backward "/*" nil t
)
646 (not (search-forward "*/" pt t
))))))
648 (defun maxima-in-output-p ()
649 "Non-nil means that the point is in minibuffer output."
653 (search-backward (maxima-minor-output-mark) nil t
)
654 (not (search-forward (maxima-minor-output-mark-end) pt t
))))))
656 ;;; Functions that search
658 ;; Some additions to help with different types of searches
659 (defvar maxima-mode-type
'maxima-mode
)
660 (make-variable-buffer-local 'maxima-mode-type
)
662 (defvar maxima-noweb-ignore-bounds
'("<<" ">>"))
664 (defun maxima-noweb-in-ignore-bounds-p ()
665 (if (not maxima-noweb-ignore-bounds
)
669 (if (not (re-search-backward (car maxima-noweb-ignore-bounds
) nil t
))
671 (not (re-search-forward (cadr maxima-noweb-ignore-bounds
) pt t
)))))))
673 (defun maxima-noweb-forward-out-of-ignore-bounds (&optional pmax
)
674 (re-search-forward (cadr maxima-noweb-ignore-bounds
) pmax
1))
676 (defun maxima-noweb-backward-out-of-ignore-bounds (&optional pmin
)
677 (re-search-backward (car maxima-noweb-ignore-bounds
) pmin
1))
680 (defun maxima-standard-re-search-forward (regexp &optional pmax
)
681 "Search forward for REGEXP, bounded by PMAX.
682 Ignore matches found in comments and strings."
683 (let ((keep-looking t
)
690 (while (and keep-looking
692 (re-search-forward regexp pmax t
))
693 (setq match
(match-string 0))
694 (setq ppe
(parse-partial-sexp pt
(point)))
696 ((nth 3 ppe
) ;; In a string
697 (if (maxima-goto-end-of-string)
699 (setq didnt-find t
)))
700 ((nth 4 ppe
) ;; In a comment
701 (if (maxima-goto-end-of-comment)
703 (setq didnt-find t
)))
704 (t ;; not in a comment or string
705 (setq keep-looking nil
))))
706 (if (or didnt-find keep-looking
)
713 (defun maxima-noweb-re-search-forward (regexp &optional pmax
)
715 (maxima-standard-re-search-forward regexp pmax
)))
716 (while (maxima-noweb-in-ignore-bounds-p)
717 (maxima-noweb-forward-out-of-ignore-bounds pmax
)
719 (maxima-standard-re-search-forward regexp pmax
)))
722 (defun maxima-re-search-forward (regexp &optional pmax
)
724 ((eq maxima-mode-type
'maxima-noweb-mode
)
725 (maxima-noweb-re-search-forward regexp pmax
))
727 (maxima-standard-re-search-forward regexp pmax
))))
729 (defun maxima-re-search-forward-skip-blocks (regexp &optional pmax
)
730 "Search forward for REGEXP, bounded by PMAX.
731 Ignore matches found in comments and strings, and skip over
732 parenthesized or bracketed blocks."
733 (let ((keep-looking t
)
737 (while (and keep-looking
738 (setq match
(maxima-re-search-forward
739 (concat regexp
"\\|[[(]") pmax
)))
745 (maxima-goto-end-of-list)
747 (setq keep-looking nil
)))
749 (setq keep-looking nil
))))
750 (if (or keep-looking didnt-find
)
756 (defun maxima-standard-re-search-backward (regexp &optional pmin
)
757 "Search backward for REGEXP, bounded by PMIN.
758 Ignore matches found in comments and strings."
759 (let ((keep-looking t
)
764 (setq pmin
(point-min)))
765 (while (and keep-looking
766 (re-search-backward regexp pmin t
))
767 (setq match
(match-string 0))
768 (setq ppe
(parse-partial-sexp pmin
(point)))
770 ((nth 8 ppe
) ;; In a string or comment
771 (goto-char (nth 8 ppe
)))
772 (t ;; not in a comment or string
773 (setq keep-looking nil
))))
780 (defun maxima-noweb-re-search-backward (regexp &optional pmin
)
782 (maxima-standard-re-search-backward regexp pmin
)))
783 (while (maxima-noweb-in-ignore-bounds-p)
784 (maxima-noweb-backward-out-of-ignore-bounds pmin
)
786 (maxima-standard-re-search-backward regexp pmin
)))
789 (defun maxima-re-search-backward (regexp &optional pmin
)
791 ((eq maxima-mode-type
'maxima-noweb-mode
)
792 (maxima-noweb-re-search-backward regexp pmin
))
794 (maxima-standard-re-search-backward regexp pmin
))))
796 (defun maxima-re-search-backward-skip-blocks (regexp &optional pmin
)
797 "Search forward for REGEXP, bounded by PMIN.
798 Ignore matches found in comments and strings, and skip over
799 parenthesized and bracketed blocks."
800 (let ((keep-looking t
)
804 (while (and keep-looking
805 (setq match
(maxima-re-search-backward
806 (concat regexp
"\\|[])]") pmin
)))
812 (maxima-goto-beginning-of-list)
814 (setq keep-looking nil
)))
816 (setq keep-looking nil
))))
817 (if (or keep-looking didnt-find
)
823 (defun maxima-escaped-char-p ()
824 "Returns non-nil if the character after point is escaped"
825 (let ((pm (point-min))
832 (setq esc-chars
(1+ esc-chars
))
835 (if (= (% esc-chars
2) 0)
839 (defun maxima-goto-end-of-string ()
840 "Go to the end of the string that the point is in.
841 Assumes that point is in a string."
843 (let ((keep-looking t
))
844 (while (and keep-looking
(search-forward "\"" nil t
))
846 (unless (maxima-escaped-char-p)
847 (setq keep-looking nil
))
853 (defun maxima-goto-beginning-of-string ()
854 "Go to the beginning of the string that the point is in.
855 Assumes that point is in a string."
857 (let ((keep-looking t
))
858 (while (and keep-looking
(search-backward "\"" nil t
))
860 (unless (maxima-escaped-char-p)
861 (setq keep-looking nil
))
864 (defun maxima-goto-end-of-comment ()
865 "Go to the end of the comment that the point is in.
866 Assumes that point is in a comment."
868 (search-forward "*/" nil t
))
870 (defun maxima-goto-beginning-of-comment ()
871 "Go to the beginning of the comment that the point is in.
872 Assumes that point is in a comment."
874 (search-backward "/*"))
876 (defun maxima-find-next-nonnested-close-char ()
877 "Search forward for next , ; $ or closing ), skipping over balanced parens.
878 If character is in a string or a list, ignore it."
880 (maxima-re-search-forward-skip-blocks "[,;$)]"))
883 ;;; Functions for dealing with words
885 ;; (defun maxima-number-of-preceding-backslashes ()
886 ;; "The number of backslashes (including the one being looked at)."
887 ;; (let ((pt (point)))
888 ;; (if (not (looking-at "\\\\"))
891 ;; (skip-chars-backward "\\\\")
892 ;; (1+ (- pt (point)))))))
894 (defun maxima-standard-next-char-word-part-p ()
895 "Non-nil if next char is a a word part."
901 (looking-at "\\\\"))))
903 (defun maxima-noweb-next-char-word-part-p ()
904 "Non-nil if next char is a a word part."
915 (looking-at "\\\\"))))
917 (defun maxima-next-char-word-part-p ()
919 ((eq maxima-mode-type
'maxima-noweb-mode
)
920 (maxima-noweb-next-char-word-part-p))
922 (maxima-standard-next-char-word-part-p))))
924 (defun maxima-previous-char-word-part-p ()
925 "Non-nil if previous character is a word part."
928 (maxima-next-char-word-part-p)))
930 (defun maxima-standard-forward-word ()
931 "Go to the end of the current word."
932 (let ((keep-going t
))
940 (setq keep-going nil
))))))
942 (defun maxima-noweb-forward-word ()
943 "Go to the end of the current word."
944 (if (maxima-noweb-in-ignore-bounds-p)
945 (maxima-noweb-forward-out-of-ignore-bounds))
946 (let ((keep-going t
))
955 (maxima-noweb-forward-out-of-ignore-bounds))
957 (setq keep-going nil
))))))
959 (defun maxima-forward-word ()
961 ((eq maxima-mode-type
'maxima-noweb-mode
)
962 (maxima-noweb-forward-word))
964 (maxima-standard-forward-word))))
966 (defun maxima-standard-backward-word ()
967 "Go to the beginning of the current word."
968 (let ((keep-going t
))
972 (> (point) (point-min))
978 (> (point) (1+ (point-min)))
981 (looking-at "\\\\")))
984 (setq keep-going nil
))))))
986 (defun maxima-noweb-backward-word ()
987 "Go to the beginning of the current word."
988 (if (maxima-noweb-in-ignore-bounds-p)
989 (maxima-noweb-backward-out-of-ignore-bounds))
990 (let ((keep-going t
))
994 (> (point) (point-min))
1000 (> (point) (1+ (point-min)))
1003 (looking-at "\\\\")))
1006 (> (point) (1+ (point-min)))
1011 (maxima-noweb-backward-out-of-ignore-bounds))
1013 (setq keep-going nil
))))))
1015 (defun maxima-backward-word ()
1017 ((eq maxima-mode-type
'maxima-noweb-mode
)
1018 (maxima-noweb-backward-word))
1020 (maxima-standard-backward-word))))
1022 ;;;; Functions that return special positions
1024 (defun maxima-line-beginning-position ()
1025 (if (not (fboundp 'line-beginning-position
))
1029 (line-beginning-position)))
1031 (defun maxima-line-end-position ()
1032 (if (not (fboundp 'line-end-position
))
1036 (line-end-position)))
1038 (defun maxima-name-beginning ()
1040 (maxima-backward-word)
1043 (defun maxima-special-symbol-beginning ()
1045 (skip-chars-backward maxima-special-symbol-letters
)
1048 (defun maxima-special-symbol-end ()
1050 (skip-chars-forward maxima-special-symbol-letters
)
1053 (defun maxima-form-beginning-position ()
1055 (maxima-goto-beginning-of-form)
1058 (defun maxima-form-end-position ()
1060 (if (maxima-goto-end-of-form)
1064 (defun maxima-form-end-position-or-point-max ()
1065 (let ((mfep (maxima-form-end-position)))
1070 (defun maxima-expression-end-position ()
1071 "Return the point where the current expression ends,
1074 (if (maxima-goto-end-of-expression)
1078 (defun maxima-begin-if-position (pmin)
1079 "Find the point of the opening \"if\" for the current point."
1085 (while (and keep-looking
1087 (maxima-re-search-backward-skip-blocks
1088 "\\<if\\>\\|\\<then\\>" pmin
)))
1089 (setq match
(downcase match
))
1090 (cond ((maxima-string-equal match
"if") (setq nest
(1+ nest
)))
1091 (t (setq nest
(1- nest
))))
1094 (setq keep-looking nil
))))
1099 (defun maxima-begin-then-position (pmin)
1100 "Find the point of the opening \"then\" for the current \"else\"."
1101 (let ((keep-going t
)
1105 (while (and keep-going
1106 (maxima-re-search-backward-skip-blocks "\\<then\\>" pmin
))
1107 ;; A potential "then". Let's see if it is.
1108 (let ((meep (maxima-expression-end-position)))
1109 (when (or (not meep
) (<= pt meep
))
1110 ;; This "then" is looking pretty good.
1111 ;; Now we need to make sure that there aren't any "else"s
1116 (while (setq match
(maxima-re-search-forward-skip-blocks
1117 "\\<then\\>\\|\\<else\\>" pt
))
1118 (cond ((maxima-string-equal match
"then")
1119 (setq level
(1+ level
)))
1120 ((maxima-string-equal match
"else")
1121 (setq level
(1- level
))))))
1123 (setq begin-then
(point))
1124 (setq keep-going nil
))))))
1127 ;;;; Functions that move the position
1128 (defun maxima-forward-over-comment-whitespace ()
1129 "Move forward over comments and whitespace."
1130 (forward-comment (buffer-size))
1131 (let ((mmo (maxima-remove-whitespace-from-beg (maxima-minor-output-mark))))
1132 (when (and (> (- (point-max) (point)) (length mmo
))
1134 (buffer-substring-no-properties
1135 (point) (+ (point) (length mmo
)))
1137 (search-forward (maxima-minor-output-mark-end))
1138 (forward-comment (buffer-size)))))
1140 (defun maxima-back-over-comment-whitespace ()
1141 "Move backward over comments and whitespace."
1142 (forward-comment (- (buffer-size)))
1143 (let ((mme (maxima-remove-whitespace-from-end (maxima-minor-output-mark-end))))
1144 (when (and (> (- (point) (point-min)) (length mme
))
1146 (buffer-substring-no-properties
1147 (- (point) (length mme
)) (point))
1149 (search-backward (maxima-minor-output-mark))
1150 (forward-comment (- (buffer-size))))))
1152 (defun maxima-standard-goto-beginning-of-form ()
1153 "Move to the beginning of the form."
1156 (while (and keep-looking
1157 (maxima-re-search-backward "[;$]" nil
))
1159 (unless (looking-at "\\\\\\$")
1161 (setq keep-looking nil
)))
1163 (goto-char (point-min)))
1164 (maxima-forward-over-comment-whitespace)
1169 (defun maxima-noweb-goto-beginning-of-form ()
1170 "Move to the beginning of the form."
1171 (if (re-search-backward "^<<.*?>>= *$" (point-min) 1)
1173 (maxima-forward-over-comment-whitespace))
1175 (defun maxima-goto-beginning-of-form ()
1177 ((eq maxima-mode-type
'maxima-noweb-mode
)
1178 (maxima-noweb-goto-beginning-of-form))
1180 (maxima-standard-goto-beginning-of-form))))
1182 (defun maxima-goto-beginning-of-form-interactive ()
1183 "Move to the beginning of the form."
1185 (maxima-goto-beginning-of-form))
1187 (defun maxima-standard-goto-end-of-form ()
1188 "Move to the end of the form."
1189 (let ((keep-looking t
)
1191 (while (and keep-looking
1192 (maxima-re-search-forward "[;$]" nil
))
1194 (unless (looking-at "\\\\\\$")
1195 (setq keep-looking nil
))
1197 (if (not keep-looking
)
1202 (defun maxima-noweb-goto-end-of-form ()
1203 "Move to the end of the form."
1204 (when (re-search-forward "\\(^@\\( \\|$\\)\\|^<<.*>>= *$\\)" nil
1)
1207 (maxima-back-over-comment-whitespace)))
1209 (defun maxima-goto-end-of-form ()
1211 ((eq maxima-mode-type
'maxima-noweb-mode
)
1212 (maxima-noweb-goto-end-of-form))
1214 (maxima-standard-goto-end-of-form))))
1216 (defun maxima-goto-end-of-form-interactive ()
1217 "Move to the end of the form."
1219 (unless (maxima-goto-end-of-form)
1220 (message "No end of form")))
1222 (defun maxima-goto-end-of-expression ()
1223 "Find the point that ends the expression that begins at point.
1224 The expression is assumed to begin with \"if\", \"then\", \"do\"
1225 \"else\" or \"(\". Return nil if the end is not found."
1227 ;; To find the end of the expression:
1228 ;; if looking at (, look for )
1229 ;; otherwise look for a , ; or $ at the same nesting level of
1230 ;; parentheses or a closing ).
1231 (cond ((or (looking-at "(")
1233 (maxima-forward-list))
1235 (maxima-find-next-nonnested-close-char))))
1237 (defun maxima-goto-beginning-of-construct (pmin)
1238 "Go to the point the begins the current construct."
1239 (let ((keep-looking t
)
1241 (while (and keep-looking
1242 (maxima-re-search-backward-skip-blocks
1243 "\\<if\\>\\|\\<then\\>\\|\\<do\\>\\|\\<else\\>\\|(\\|\\[" pmin
))
1245 (when (or (not (maxima-goto-end-of-expression)) (<= pt
(point)))
1246 (setq keep-looking nil
))))
1251 (defun maxima-goto-end-of-list ()
1253 Return t if possible, nil otherwise."
1262 (defun maxima-goto-end-of-list-interactive ()
1265 (if (maxima-goto-end-of-list)
1267 (error "No list to end.")))
1269 (defun maxima-goto-beginning-of-list ()
1270 "Go up a list backwards.
1271 Return t if possible, nil otherwise."
1279 (defun maxima-goto-beginning-of-list-interactive ()
1282 (if (maxima-goto-beginning-of-list)
1284 (error "No list to begin.")))
1286 (defun maxima-forward-list ()
1288 Return t if possible, nil otherwise."
1296 (defun maxima-backward-list ()
1297 "Go backward a list.
1298 Return t if possible, nil otherwise."
1306 ;;; Newlines and indents
1307 (defun maxima-indent-form ()
1308 "Indent the entire form."
1311 (maxima-form-beginning-position)
1312 (maxima-form-end-position-or-point-max)
1316 (defun maxima-standard-indent ()
1317 "Indent the line based on the previous line.
1318 If the previous line opened a parenthesis, `maxima-indent-amount' is
1319 added to the indentation, if the previous line closed a parenthesis,
1320 `maxima-indent-amount' is subtracted, otherwise the indentation
1321 is the same as the previous line."
1327 (when (= (forward-line -
1) 0)
1329 (setq indent
(current-indentation))
1330 (setq pt
(maxima-line-end-position))
1331 (while (setq match
(maxima-re-search-forward "[()]" pt
))
1332 (cond ((string= match
")")
1333 (setq indent
(- indent maxima-indent-amount
)))
1334 ((string= match
"(")
1335 (setq indent
(+ indent maxima-indent-amount
))))))))
1338 (delete-horizontal-space)
1339 (indent-line-to (max indent
0)))
1340 (skip-chars-forward " \t")))
1342 (defun maxima-untab ()
1343 "Delete a level of indentation"
1345 ;; Check to see if the previous maxima-indent-amount spaces are
1346 ;; on the same line and blank
1347 (let ((i maxima-indent-amount
)
1351 (while (and (> i
0) ok
)
1354 (if (not (looking-at " "))
1356 (if (looking-at "\n")
1359 (delete-region pt
(- pt maxima-indent-amount
)))))
1362 ;;; Find the beginning of a function
1364 (defun maxima-open-paren-is-function ()
1365 "Check to see if the point is before an opening paren,
1366 and if the previous character is a close paren or word part."
1368 (looking-at "[ \t]*(")
1370 (maxima-back-over-comment-whitespace)
1372 (looking-at ")\\|\\w"))))
1374 (defun maxima-close-paren-before-open-paren ()
1375 "Return point of close paren before the current open paren,
1376 or nil if there is none."
1378 (maxima-back-over-comment-whitespace)
1380 (if (looking-at ")")
1384 (defun maxima-back-over-paren-groups ()
1385 "Go over any paren groups.
1386 Assume point is at \"(\", as long as preceding character is
1387 \")\", go to open parentheses."
1388 (let ((cpbop (maxima-close-paren-before-open-paren)))
1391 (maxima-backward-list)
1393 (maxima-close-paren-before-open-paren)))))
1395 (defun maxima-word-on-previous-line ()
1396 "Go to the previous word part, return nil if "
1399 (skip-chars-backward " \t\n")
1402 (maxima-previous-char-word-part-p)
1404 (buffer-substring-no-properties (point) pt
))))))
1406 (defun maxima-string-on-previous-line ()
1407 "Go to the previous word part, return nil if "
1410 (skip-chars-backward " \t\n")
1416 (buffer-substring-no-properties (point) pt
))))))
1418 (defun maxima-back-over-function-name ()
1419 "Go back over the `foo' in 'foo(x,y)', return the length of `foo'.
1420 Return nil if open paren is not part of function.
1421 Assumes the point is right before the open parenthesis."
1424 (maxima-back-over-paren-groups)
1425 (setq endpt
(point))
1426 ;; Let's see what's before this
1428 ;; There is a word right before this
1429 ((maxima-previous-char-word-part-p)
1430 (maxima-backward-word)
1432 ;; There is a string before this
1436 (maxima-goto-beginning-of-string)
1438 ;; There is a word before this on the previous line
1439 ((maxima-word-on-previous-line)
1440 (skip-chars-backward " \t\n")
1441 (setq endpt
(point))
1442 (maxima-backward-word)
1444 ;; There is a string before this on the previous line
1445 ((maxima-string-on-previous-line)
1446 (skip-chars-backward " \t\n")
1447 (setq endpt
(point))
1448 (maxima-goto-beginning-of-string)
1450 ;; This point is the original point
1453 ;; Finally, the last parenthesized expression is the function
1456 (maxima-forward-list)
1457 (setq endpt
(point)))
1458 (- endpt
(point))))))
1462 (defun maxima-after-lisp-expression-p ()
1463 "Return non-nil if the point is right after a lisp expression."
1464 (let ((pm (point-min))
1467 (maxima-back-over-comment-whitespace)
1472 (when (< (point) pt
)
1473 (maxima-back-over-comment-whitespace)
1474 (if (< (point) (+ 5 pm
))
1477 (if (looking-at ":lisp")
1481 (defun maxima-standard-perhaps-smart-calculate-indent ()
1482 "Return appropriate indentation for current line as Maxima code.
1483 Returns an integer: the column to indent to."
1495 (setq pmin
(maxima-form-beginning-position))
1496 (setq pps
(parse-partial-sexp pmin
(point)))
1497 (setq le
(maxima-after-lisp-expression-p))
1499 (setq pmin
(nth 1 pps
))
1500 (unless (looking-at "^[ \t]*,")
1502 (when (maxima-re-search-backward-skip-blocks "," pmin
)
1504 (let ((lep (maxima-line-end-position)))
1506 (maxima-forward-over-comment-whitespace)
1507 (unless (>= (point) lep
)
1508 (setq pmin
(point)))))))))
1510 ;; First, take care of the cases where the indentation is clear
1511 ;; No indentation at the beginning of the buffer
1514 ;; Don't change the indentation if in a string
1517 ;; If the line begins a comment and comments aren't reindented,
1518 ;; don't reindent it
1520 (looking-at "[ \t]*/\\*[ \t]*$")
1521 maxima-dont-reindent-some-comments
)
1523 ;; Deal with comments separately
1524 ((maxima-perhaps-smart-in-comment-p (nth 4 pps
) pmin pt
)
1525 (setq indent
(maxima-perhaps-smart-comment-calculate-indent)))
1526 ;; If the current point is the beginning of the form, the level is 0
1529 ;; If the current point is in maxima minor output, don't reindent it
1530 ((maxima-in-output-p)
1532 ;; A line beginning "then" is indented like the opening "if"
1534 (looking-at "[ \t]*\\<then\\>")
1535 (setq tmpchar
(maxima-begin-if-position pmin
)))
1537 (setq indent
(+ maxima-if-extra-indent-amount
(current-column))))
1538 ;; A line beginning "else" is indented like the corresponding "then"
1540 (looking-at "[ \t]*\\<else\\>")
1541 (setq tmpchar
(maxima-begin-then-position pmin
)))
1543 (setq indent
(current-column)))
1544 ;; A line beginning with an open paren that is the
1545 ;; beginning of a function argument is indented according to
1548 (looking-at "[ \t]*(")
1549 (setq len
(maxima-back-over-function-name)))
1550 (setq indent
(+ (current-column)
1551 (min len maxima-function-indent-amount
))))
1552 ;; A line beginning with a closing paren is indented like the open paren
1553 ((looking-at "[ \t]*)")
1554 ;; Here, pmin should be the opening paren position
1556 (if (looking-at "( *$")
1558 (setq len
(maxima-back-over-function-name))
1561 (min len maxima-function-indent-amount
)
1564 (setq indent
(current-column))))
1565 ;; A line beginning with a closing bracket is indented like the open bracket
1566 ((looking-at "[ \t]*\\]")
1567 ;; Here, pmin should be the opening bracket position
1569 (setq indent
(current-column)))
1570 ; ;; A line beginning with a comma is indented like the opening paren
1571 ; ((looking-at "[ \t]*,")
1573 ; (setq indent (current-column)))
1574 ;; The point is at the end of a lisp expression
1577 ;; Otherwise, the correct indentation needs to be computed.
1579 ;; Find the indentation of beginning of the current construct
1580 ;; If begin-construct is nil, indent according to the opening paren
1581 (setq comma-line
(looking-at "[ \t]*,"))
1583 (maxima-goto-beginning-of-construct pmin
)
1585 ;; The construct begins with a bracket
1588 ; (setq indent (current-column))
1589 (setq indent
(+ maxima-paren-indent-amount
(current-column)))
1591 (skip-chars-forward " \t")
1592 (unless (looking-at "\n")
1593 (setq indent
(current-column))));)
1594 ;; The construct begins with a paren
1598 ; (setq indent (current-column)))
1600 (let ((lep (maxima-line-end-position)))
1602 (maxima-forward-over-comment-whitespace)
1604 ;(looking-at "( *$")
1605 ;; Check to see if there is anything before the (
1607 (re-search-backward "\\(?:^[ \t]*\\)\\=" nil t
))
1608 (setq tmpchar maxima-paren-indent-amount
)
1610 ;; If there is nothing after the (, there are two
1612 ;; First, there is a function before it
1613 (if (setq len
(maxima-back-over-function-name))
1614 (setq indent
(+ (min len maxima-function-indent-amount
)
1618 (setq indent
(+ maxima-paren-indent-amount
(current-column)))))
1619 ;; If there is something after the (, indent according to that
1622 (skip-chars-forward " \t")
1623 (setq indent
(current-column)))))
1624 ;; The construct does not begin with a paren
1626 (setq indent
(current-column)))))
1627 ;; Now, we need to possibly do some adjustments.
1628 ;; If the previous column doesn't end with close-char or a
1629 ;; parenthesis, assume that the current line in a continuation
1630 ;; of that line, and add to the indentation, unless the
1631 ;; previous line was the beginning of the construct containing
1632 ;; the point or only an open parenthesis.
1634 (let ((bol-pt (point))
1637 (skip-chars-forward " \t")
1638 (setq comma-pt
(point))
1640 (skip-chars-forward " \t")
1641 (setq diff
(- (point) comma-pt
))
1646 (1+ (count-lines (point-min) (point))))))
1648 (format "Leading comma prevents proper indentation on line %d"
1651 (setq indent
(- indent diff
))))
1652 (maxima-back-over-comment-whitespace)
1653 (when (not (looking-at "^"))
1655 (if (not (or le
(looking-at "[,;$]")))
1656 (setq indent
(+ maxima-continuation-indent-amount indent
))))))))
1659 (defun maxima-noweb-perhaps-smart-calculate-indent ()
1665 ((looking-at "^<<.*?>>=[ \t]*$")
1667 ((looking-at "^@[ \n]")
1671 (if (looking-at "^<<.*?>>=[ \t]*$")
1672 (setq indent -
1)))))
1675 (maxima-standard-perhaps-smart-calculate-indent))))
1677 (defun maxima-perhaps-smart-calculate-indent ()
1679 ((eq maxima-mode-type
'maxima-mode
)
1680 (maxima-standard-perhaps-smart-calculate-indent))
1681 ((eq maxima-mode-type
'maxima-noweb-mode
)
1682 (maxima-noweb-perhaps-smart-calculate-indent))))
1685 (defun maxima-perhaps-smart-in-comment-p (incomment pmin pt
)
1686 "Determine if the point is in a comment or not."
1688 ;; If we're told it's in a comment, then it is.
1691 ;; Otherwise, if pmin is less than point, we're not in a comment
1694 ;; Otherwise, we have to check it out
1696 (maxima-in-comment-p))))
1698 (defun maxima-perhaps-smart-comment-calculate-indent ()
1699 "Calculate the indentation of the current line,
1700 which is in a comment which begins on a previous line."
1705 ((looking-at "^[ \t]*$")
1707 ((looking-at "^[ \t]*\\*/")
1708 (setq endcomment t
)))
1709 ;; First of all, if the comment begins with `/*' and nothing
1710 ;; else on the line, don't indent it.
1712 (maxima-goto-beginning-of-comment)
1714 ;; Take care of the case when comments won't be indented
1716 (looking-at "/\\*[ \t]*$")
1717 maxima-dont-reindent-some-comments
)
1719 (setq indent
(+ (current-column)
1720 maxima-multiline-comment-indent-amount
))
1722 ;; Now, the other cases
1723 ;; If the current line ends a column, indent it like the opening line
1725 (setq indent
(current-column)))
1726 ;; If the opening line has a blank after the `/*'
1727 ((looking-at "/\\*[ \t\n]")
1729 (skip-chars-forward " \t")
1730 (if (not (looking-at "\n"))
1731 (setq indent
(current-column))
1732 (search-backward "/*")
1733 (setq indent
(+ (current-column)
1734 maxima-multiline-comment-indent-amount
))))
1736 (setq indent
(+ (current-column)
1737 maxima-multiline-comment-indent-amount
)))))
1740 (defun maxima-perhaps-smart-indent-line ()
1741 "Reindent the current line."
1743 (let ((indent (maxima-perhaps-smart-calculate-indent)))
1744 (unless (= indent -
1)
1747 (delete-horizontal-space)
1748 (indent-to indent
)))
1749 (skip-chars-forward " \t")))
1751 ;;;; Indentation according to style
1753 (defun maxima-indent-line ()
1756 ((eq maxima-newline-style
'basic
)
1757 (maxima-standard-indent))
1758 ((eq maxima-indent-style
'standard
)
1759 (maxima-standard-indent))
1760 ((eq maxima-indent-style
'perhaps-smart
)
1761 (maxima-perhaps-smart-indent-line))))
1763 (defun maxima-change-indent-style (new-style)
1764 "Change the newline style."
1765 (interactive "sNewline style (insert \"b\" for basic, \"s\" for standard, or \"p\" for perhaps-smart): ")
1767 ((string= new-style
"b")
1768 (setq maxima-indent-style
'basic
))
1769 ((string= new-style
"s")
1770 (setq maxima-indent-style
'standard
))
1771 ((string= new-style
"p")
1772 (setq maxima-indent-style
'perhaps-smart
))))
1774 (defun maxima-return ()
1777 ((eq maxima-return-style
'newline
)
1779 ((eq maxima-return-style
'newline-and-indent
)
1780 (newline-and-indent))
1781 ((eq maxima-return-style
'reindent-then-newline-and-indent
)
1782 (reindent-then-newline-and-indent))))
1786 (defun maxima-insert-short-comment ()
1787 "Prompt for a comment."
1789 (let ((comment (read-string "Comment: ")))
1790 (insert "/* " comment
" */")
1791 (newline-and-indent)))
1793 (defun maxima-insert-long-comment ()
1794 "Insert a comment environment"
1796 (indent-for-tab-command)
1801 (indent-for-tab-command)
1803 (indent-for-tab-command))
1805 (defun maxima-uncomment-region (beg end
)
1806 "`uncomment-region' to use with the menu."
1808 (uncomment-region beg end
(universal-argument)))
1812 (defun maxima-goto-info-node (node)
1813 (if maxima-running-xemacs
1815 (info-other-window (concat "(Maxima)" node
))))
1817 (defun maxima-get-info-on-subject (subject &optional same-window
)
1819 (Info-menu "Function and Variable Index")
1820 (cond (maxima-running-xemacs
1821 (or (and (search-forward subject nil t
)
1822 (Info-follow-nearest-node (point)))
1823 (message (concat "Unable to locate " subject
))))
1825 (Info-menu subject
))))
1827 (defun maxima-get-help ()
1828 "Get help on a given subject"
1833 (skip-chars-forward "* ")
1835 (search-forward ":")
1836 (skip-chars-backward ": ")
1837 (setq name
(buffer-substring-no-properties pt
(point))))
1838 (maxima-get-info-on-subject name t
)))
1839 ; (if (not maxima-running-xemacs)
1840 ; (info-other-window (concat "(Maxima)" place))
1842 ; (search-forward place)
1843 ; (Info-follow-nearest-node))
1844 ; (re-search-forward (concat "-.*: *" name "\\>"))
1845 ; (beginning-of-line)))
1847 (defun maxima-help (&optional arg
)
1849 (let* ((cw (current-word))
1852 (completing-read (concat "Maxima help (" cw
"): ")
1853 maxima-symbols nil nil nil nil cw
))))
1855 (maxima-get-info-on-subject subj
)
1858 (defun maxima-help-at-point ()
1862 (defun maxima-apropos (&optional arg
)
1863 "Get help on a certain subject"
1865 (let* ((cw (current-word))
1868 (read-string (concat "Maxima help (" cw
"): ")
1871 (get-buffer-create (concat "*Maxima Help*")))
1875 (set-buffer maxima-help-buffer
)
1876 (maxima-remove-kill-buffer-hooks)
1877 (setq buffer-read-only nil
)
1879 (insert "Maxima help for " expr
"\n\n")
1880 (insert "[RET] will get help on the subject on the given line\n")
1881 (insert "q in the *info* buffer will return you here.\n")
1882 (insert "q in this buffer will exit Maxima help\n\n")
1886 (Info-goto-node "(Maxima)Function and Variable Index")
1887 (goto-char (point-min))
1888 (search-forward "Menu:")
1891 (while (re-search-forward (concat "\\*.*" expr
".*:") nil t
)
1893 (setq expr-line
(buffer-substring-no-properties
1894 (maxima-line-beginning-position)
1895 (maxima-line-end-position)))
1897 (set-buffer maxima-help-buffer
)
1898 (insert expr-line
"\n"))))
1901 (set-buffer maxima-help-buffer
)
1902 (defun maxima-help-subject ()
1905 (defun maxima-kill-help ()
1907 (let ((buf (current-buffer)))
1909 (maxima-remove-kill-buffer-hooks)
1911 (defun maxima-next-subject ()
1914 (if (re-search-forward "^\\*" nil t
)
1916 (goto-char (point-min))
1917 (re-search-forward "^\\*" nil t
))
1919 (use-local-map (make-sparse-keymap))
1920 (define-key (current-local-map) "\C-m" 'maxima-help-subject
)
1921 (define-key (current-local-map) "q" 'maxima-kill-help
)
1922 (define-key (current-local-map) "\t" 'maxima-next-subject
)
1923 (goto-char (point-min))
1924 (re-search-forward "^\\*")
1926 (pop-to-buffer maxima-help-buffer
)
1927 (setq buffer-read-only t
))
1928 (kill-buffer maxima-help-buffer
)
1929 (message (concat "No help for \"" expr
"\"")))))
1931 (defun maxima-apropos-at-point ()
1935 (defun maxima-apropos-help ()
1937 (maxima-help-dispatcher nil nil
))
1939 (defun maxima-completion-help ()
1941 (maxima-help-dispatcher nil t
))
1943 (defun maxima-help-dispatcher (&optional arg1 arg2
)
1946 ((or (looking-at "[a-zA-Z_]")
1947 (looking-at "?[a-zA-Z]")
1948 (looking-at "%[a-zA-Z]"))
1950 (maxima-context-help)
1951 (maxima-help (current-word))))
1953 (maxima-get-info-on-subject "\"\\?\""))
1955 (maxima-get-info-on-subject "\"#\""))
1957 (maxima-get-info-on-subject "\"\\.\""))
1958 ((looking-at "[:=!%']")
1959 (let ((expr (buffer-substring-no-properties
1960 (maxima-special-symbol-beginning) (maxima-special-symbol-end))))
1962 ((or (string= expr
"%") (string= expr
"%%"))
1963 (maxima-get-info-on-subject expr
)) ; % and %% are without double quotes
1964 ((string= expr
"''")
1965 (maxima-get-info-on-subject "\"")) ; "''" is called """ in the manual
1966 ((or (string= expr
":") (string= expr
"::")
1967 (string= expr
":=") (string= expr
"::=")
1968 (string= expr
"=") (string= expr
"!") (string= expr
"!!"))
1969 (maxima-get-info-on-subject (concat "\"" expr
"\"")))
1970 (t (error "no help for %s" expr
)))))
1972 (error "No help for %s" (char-to-string (char-after (point)))))
1973 (t ; point is behind a name?
1977 (maxima-help-dispatcher t arg2
))))))
1979 (defun maxima-context-help ()
1981 (let* ((stub (current-word))
1982 (completions (all-completions (downcase stub
) maxima-symbols
)))
1985 (function upcase
) completions
))
1986 (if (member (upcase stub
) completions
)
1987 (setq completions
(list (upcase stub
))))
1988 (cond ((null completions
)
1989 (message "No help for %s" stub
))
1990 ((= 1 (length completions
))
1991 (maxima-get-info-on-subject (car completions
)))
1992 (t ; There's no unique completion.
1993 (maxima-help-variation completions
)))))
1995 (defun maxima-help-variation (completions)
1996 "Get help on certain subjects."
1997 (let* ((maxima-help-buffer
1998 (get-buffer-create (concat "*Maxima Help*")))
2001 (set-buffer maxima-help-buffer
)
2003 (insert "Maxima help\n")
2004 (insert "[RET] will get help on the subject on the given line\n")
2005 (insert "q in the *info* buffer will return you here.\n")
2006 (insert "q in this buffer will exit Maxima help\n\n")
2007 (defun maxima-help-insert-line (expr)
2008 (re-search-forward (concat "\\* " expr
":") nil t
)
2009 (setq expr-line
(buffer-substring-no-properties
2010 (maxima-line-beginning-position)
2011 (maxima-line-end-position)))
2013 (set-buffer maxima-help-buffer
)
2014 (insert expr-line
"\n")))
2016 (require 'info nil t
)
2018 (Info-goto-node "(Maxima)Function and Variable Index")
2019 (goto-char (point-min))
2020 (search-forward "Menu:")
2023 (mapcar (function maxima-help-insert-line
) completions
))
2024 (goto-char (point-min))
2025 (defun maxima-help-subject ()
2028 (defun maxima-kill-help ()
2030 (let ((buf (current-buffer)))
2032 (maxima-remove-kill-buffer-hooks)
2034 (use-local-map (append (make-sparse-keymap) (current-local-map)))
2035 (define-key (current-local-map) "\C-m" 'maxima-help-subject
)
2036 (define-key (current-local-map) "q" 'maxima-kill-help
)
2038 (pop-to-buffer maxima-help-buffer
)))
2040 (defun maxima-info ()
2041 "Read the info file for Maxima."
2043 (if maxima-running-xemacs
2045 (info-other-window "Maxima")))
2049 (defvar maxima-help-map nil
)
2052 (let ((map (make-sparse-keymap)))
2053 (define-key map
"h" 'maxima-help
)
2054 (define-key map
"d" 'maxima-completion-help
)
2055 (define-key map
"\C-d" 'maxima-completion-help
)
2056 (define-key map
"i" 'maxima-info
)
2057 (define-key map
"\C-i" 'maxima-info
)
2058 (define-key map
"m" 'maxima-info
)
2059 (define-key map
"\C-m" 'maxima-info
)
2060 (define-key map
"a" 'maxima-apropos
)
2061 (define-key map
"\C-a" 'maxima-apropos
)
2062 (define-key map
"p" 'maxima-apropos-help
)
2063 (define-key map
"\C-p" 'maxima-apropos-help
)
2064 (setq maxima-help-map map
)))
2068 ;;; This next functions are from hippie-expand.el
2069 (defun maxima-he-capitalize-first (str)
2071 (if (string-match "\\Sw*\\(\\sw\\).*" str
)
2072 (let ((res (downcase str
))
2073 (no (match-beginning 1)))
2074 (aset res no
(upcase (aref str no
)))
2078 (defun maxima-he-ordinary-case-p (str)
2079 (or (string= str
(downcase str
))
2080 (string= str
(upcase str
))
2081 (string= str
(capitalize str
))
2082 (string= str
(maxima-he-capitalize-first str
))))
2085 (defun maxima-he-transfer-case (from-str to-str
)
2086 (cond ((string= from-str
(substring to-str
0 (min (length from-str
)
2089 ((not (maxima-he-ordinary-case-p to-str
))
2091 ((string= from-str
(downcase from-str
))
2093 ((string= from-str
(upcase from-str
))
2095 ((string= from-str
(maxima-he-capitalize-first from-str
))
2096 (maxima-he-capitalize-first to-str
))
2097 ((string= from-str
(capitalize from-str
))
2098 (capitalize to-str
))
2102 ;;; The next functions are from comint.el in cvs emacs
2104 (not maxima-running-xemacs
)
2105 (<= emacs-major-version
21)
2107 (< emacs-major-version
21)
2108 (< emacs-minor-version
3)))
2109 (defun comint-dynamic-list-completions (completions)
2110 "List in help buffer sorted COMPLETIONS.
2111 Typing SPC flushes the help buffer."
2112 (let ((window (get-buffer-window "*Completions*")))
2113 (setq completions
(sort completions
'string-lessp
))
2114 (if (and (eq last-command this-command
)
2115 window
(window-live-p window
) (window-buffer window
)
2116 (buffer-name (window-buffer window
))
2117 ;; The above tests are not sufficient to detect the case where we
2118 ;; should scroll, because the top-level interactive command may
2119 ;; not have displayed a completions window the last time it was
2120 ;; invoked, and there may be such a window left over from a
2121 ;; previous completion command with a different set of
2122 ;; completions. To detect that case, we also test that the set
2123 ;; of displayed completions is in fact the same as the previously
2126 (buffer-local-value 'comint-displayed-dynamic-completions
2127 (window-buffer window
))))
2128 ;; If this command was repeated, and
2129 ;; there's a fresh completion window with a live buffer,
2130 ;; and this command is repeated, scroll that window.
2131 (with-current-buffer (window-buffer window
)
2132 (if (pos-visible-in-window-p (point-max) window
)
2133 (set-window-start window
(point-min))
2134 (save-selected-window
2135 (select-window window
)
2137 ;; Display a completion list for the first time.
2138 (setq comint-dynamic-list-completions-config
2139 (current-window-configuration))
2140 (with-output-to-temp-buffer "*Completions*"
2141 (display-completion-list completions
))
2142 (message "Type space to flush; repeat completion command to scroll"))
2144 ;; Read the next key, to process SPC.
2146 (if (with-current-buffer (get-buffer "*Completions*")
2147 (set (make-local-variable 'comint-displayed-dynamic-completions
)
2149 (setq key
(read-key-sequence nil
)
2151 (and (consp first
) (consp (event-start first
))
2152 (eq (window-buffer (posn-window (event-start first
)))
2153 (get-buffer "*Completions*"))
2154 (eq (key-binding key
) 'mouse-choose-completion
)))
2155 ;; If the user does mouse-choose-completion with the mouse,
2156 ;; execute the command, then delete the completion window.
2158 (mouse-choose-completion first
)
2159 (set-window-configuration comint-dynamic-list-completions-config
))
2160 (unless (eq first ?\
)
2161 (setq unread-command-events
(listify-key-sequence key
)))
2162 (unless (eq first ?
\t)
2163 (set-window-configuration comint-dynamic-list-completions-config
)))))))
2166 (defun maxima-complete-symbol ()
2167 "Complete word from list of candidates.
2168 A completions listing will be shown in a help buffer
2169 if completion is ambiguous."
2170 (let* ((comint-completion-addsuffix nil
)
2171 (stub (buffer-substring-no-properties
2172 (maxima-name-beginning) (point)))
2173 (completions (all-completions (downcase stub
) maxima-symbols
)))
2174 (comint-dynamic-simple-complete stub completions
)))
2176 (defun maxima-complete-filename ()
2177 "Complete the filename."
2178 (comint-dynamic-complete-filename))
2180 (defun maxima-complete ()
2181 "Complete the current object, depending on context."
2183 (let* ((pmin (maxima-form-beginning-position))
2184 (pps (parse-partial-sexp pmin
(point))))
2186 ;; complete filename if the point is in a string
2188 (maxima-complete-filename))
2189 ;; Otherwise, complete the symbol
2191 (maxima-complete-symbol)))))
2193 ;; ;;; Use hippie-expand to help with completions
2194 ;; (require 'hippie-exp)
2196 ;; (defun maxima-he-try (old)
2199 ;; ;;; let beg be the beginning of the word
2201 ;; (he-init-string (maxima-name-beginning) (point))
2202 ;; (setq he-expand-list
2203 ;; (all-completions (downcase he-search-string) maxima-symbols))
2204 ;; (setq he-expand-list
2205 ;; (mapcar (function
2206 ;; (lambda (x) (he-transfer-case he-search-string x)))
2208 ;; (if he-expand-list
2209 ;; (he-substitute-string (car he-expand-list))
2211 ;; (setq he-expand-list (cdr he-expand-list))
2212 ;; (if he-expand-list
2213 ;; (he-substitute-string (car he-expand-list))
2214 ;; (he-reset-string)
2217 ;; (fset 'maxima-dynamic-complete
2218 ;; (make-hippie-expand-function '(maxima-he-try)))
2222 (defun maxima-mark-form ()
2223 "Make the current form as the region."
2225 (maxima-goto-beginning-of-form)
2226 (set-mark (maxima-form-end-position-or-point-max)))
2228 (defun maxima-check-commas (beg end
)
2229 "Check to see if there is a stray comma at the beginning or end."
2230 (let ((commapt nil
))
2233 (maxima-forward-over-comment-whitespace)
2234 (if (looking-at ",")
2235 (setq commapt
(point))
2237 (maxima-back-over-comment-whitespace)
2238 (when (save-excursion
2240 (looking-at "[;$]"))
2242 (maxima-back-over-comment-whitespace))
2244 (if (looking-at ",")
2245 (setq commapt
(point)))))
2248 (message "Misplaced comma")
2253 (defun maxima-check-parens (beg end
)
2254 "Check to make sure that the parentheses are balanced in the region."
2256 (let* ((tmpfile (maxima-make-temp-name))
2257 (tmpbuf (get-buffer-create tmpfile
))
2258 (string (buffer-substring-no-properties beg end
))
2267 (maxima-remove-kill-buffer-hooks)
2268 (modify-syntax-entry ?
/ ". 14")
2269 (modify-syntax-entry ?
* ". 23")
2271 (goto-char (point-min))
2272 (while (and (not errmessage
)
2273 (setq match
(maxima-re-search-forward "[][()]" end
)))
2274 (unless (save-excursion
2276 (maxima-escaped-char-p))
2278 ((string= match
"(")
2279 (setq parenstack
(cons (cons 1 (1- (point))) parenstack
)))
2280 ((string= match
"[")
2281 (setq parenstack
(cons (cons 2 (1- (point))) parenstack
)))
2282 ((string= match
")")
2285 (setq errmessage
"Unmatched close parenthesis")
2286 (setq pt
(1- (point))))
2287 ((= (caar parenstack
) 1)
2288 (setq parenstack
(cdr parenstack
)))
2289 ((= (caar parenstack
) 2)
2290 (setq errmessage
"Open bracket closed by parenthesis")
2291 (setq pt
(1- (point))))))
2292 ((string= match
"]")
2295 (setq errmessage
"Unmatched close bracket")
2296 (setq pt
(1- (point))))
2297 ((= (caar parenstack
) 2)
2298 (setq parenstack
(cdr parenstack
)))
2299 ((= (caar parenstack
) 1)
2300 (setq errmessage
"Open parenthesis closed by bracket")
2301 (setq pt
(1- (point))))))))))
2302 (kill-buffer tmpbuf
)
2304 ((not (or parenstack errmessage
))
2305 ; (message "Parenthesis and brackets match")
2308 (message errmessage
)
2309 (goto-char (1- (+ beg pt
)))
2313 ((= (caar parenstack
) 1)
2314 (message "Unmatched open parenthesis")
2315 (goto-char (1- (+ beg
(cdar parenstack
))))
2318 (message "Unmatched open bracket")
2319 (goto-char (+ beg
(cdar parenstack
)))
2323 (defun maxima-check-form-parens ()
2324 "Check to see if the parentheses in the current form are balanced."
2326 (maxima-check-parens (maxima-form-beginning-position)
2327 (maxima-form-end-position-or-point-max)))
2329 (defun maxima-load-file (file)
2330 "Prompt for a Maxima file to load."
2331 (interactive "fMaxima file: ")
2332 (maxima-string (concat "load(\"" (expand-file-name file
) "\");")))
2334 (defun maxima-load-current-file ()
2335 "Load the current file into Maxima."
2337 (maxima-string (concat "load(\"" buffer-file-name
"\");")))
2339 ;;; For highlighting the region being sent
2341 (defun maxima-mode-add-highlight ()
2342 (maxima-mode-remove-highlight)
2343 (if (and maxima-mode-region-begin maxima-mode-region-end
)
2344 (if maxima-running-xemacs
2346 (setq maxima-mode-highlight
2348 maxima-mode-region-begin
2349 maxima-mode-region-end
))
2350 (set-extent-property maxima-mode-highlight
'face
'highlight
))
2351 (setq maxima-mode-highlight
2353 maxima-mode-region-begin
2354 maxima-mode-region-end
))
2355 (overlay-put maxima-mode-highlight
'face
'highlight
)))
2356 (setq maxima-mode-region-begin nil
)
2357 (setq maxima-mode-region-end nil
))
2359 (defun maxima-mode-remove-highlight ()
2360 (when maxima-mode-highlight
2361 (if maxima-running-xemacs
2362 (delete-extent maxima-mode-highlight
)
2363 (delete-overlay maxima-mode-highlight
))
2364 (setq maxima-mode-highlight nil
)))
2366 (defun maxima-mode-add-remove-highlight ()
2368 (eq this-command
'maxima-send-region
)
2369 (eq this-command
'maxima-send-buffer
)
2370 (eq this-command
'maxima-send-line
)
2371 (eq this-command
'maxima-send-form
)
2372 (eq this-command
'maxima-send-previous-form
)
2373 (eq this-command
'maxima-send-previous-form-and-goto-end-of-form
)
2374 (eq this-command
'maxima-send-full-line
)
2375 (eq this-command
'maxima-send-full-line-and-goto-next-form
)
2376 (eq this-command
'maxima-send-completed-region
)
2377 (eq this-command
'maxima-send-completed-region-and-goto-next-form
)
2378 (eq this-command
'maxima-minibuffer-on-region
)
2379 (eq this-command
'maxima-minibuffer-on-form
)
2380 (eq this-command
'maxima-minibuffer-on-line
))
2381 (maxima-mode-add-highlight)
2382 (maxima-mode-remove-highlight)))
2386 (defvar maxima-mode-syntax-table nil
"")
2388 (if (not maxima-mode-syntax-table
)
2390 (setq maxima-mode-syntax-table
(make-syntax-table))
2391 (modify-syntax-entry ?_
"w" maxima-mode-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)
2396 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2400 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2404 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2408 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2410 (modify-syntax-entry ?
" " maxima-mode-syntax-table
)
2411 (modify-syntax-entry ?
\t " " maxima-mode-syntax-table
)
2412 (modify-syntax-entry ?
` "' " maxima-mode-syntax-table
)
2413 (modify-syntax-entry ?
' "' " maxima-mode-syntax-table
)
2414 (modify-syntax-entry ?
, "' " maxima-mode-syntax-table
)
2415 (modify-syntax-entry ?.
"w" maxima-mode-syntax-table
)
2416 (modify-syntax-entry ?
# "' " maxima-mode-syntax-table
)
2417 (modify-syntax-entry ?
\\ "\\" maxima-mode-syntax-table
)
2418 (modify-syntax-entry ?
/ ". 14" maxima-mode-syntax-table
)
2419 (modify-syntax-entry ?
* ". 23" maxima-mode-syntax-table
)
2420 (modify-syntax-entry ?
+ "." 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
)))
2437 (defvar maxima-mode-map nil
2438 "The keymap for maxima-mode")
2442 (let ((map (make-sparse-keymap)))
2444 (define-key map
"\M-\C-a" 'maxima-goto-beginning-of-form-interactive
)
2445 (define-key map
"\M-\C-e" 'maxima-goto-end-of-form-interactive
)
2446 (define-key map
"\M-\C-b" 'maxima-goto-beginning-of-list-interactive
)
2447 (define-key map
"\M-\C-f" 'maxima-goto-end-of-list-interactive
)
2449 (define-key map
"\C-c\C-p" 'maxima-display-buffer
)
2450 (define-key map
"\C-c\C-r" 'maxima-send-region
)
2451 (define-key map
"\C-c\C-b" 'maxima-send-buffer
)
2452 (define-key map
"\C-c\C-c" 'maxima-send-line
)
2453 (define-key map
"\C-c\C-e" 'maxima-send-previous-form
)
2454 (define-key map
"\C-c\C-s" 'maxima-send-previous-form-and-goto-end-of-form
)
2455 (define-key map
[(control return
)]
2456 'maxima-send-full-line-and-goto-next-form
)
2457 (define-key map
[(meta return
)]
2458 'maxima-send-completed-region-and-goto-next-form
)
2459 (define-key map
[(control meta return
)] 'maxima-send-buffer
)
2460 (define-key map
"\C-c\C-k" 'maxima-stop
)
2461 (define-key map
"\C-c\C-q" 'maxima-clear-queue
)
2462 (define-key map
"\C-c\C-l" 'maxima-load-file
)
2463 (define-key map
"\C-c\C-f" 'maxima-load-current-file
)
2465 ;(if maxima-use-dynamic-complete
2466 ; (define-key map (kbd "M-TAB") 'maxima-dynamic-complete)
2467 (define-key map
(kbd "M-TAB") 'maxima-complete
)
2469 (define-key map
"\C-c;" 'comment-region
)
2470 (define-key map
"\C-c:" 'maxima-uncomment-region
)
2471 (define-key map
"\M-;" 'maxima-insert-short-comment
)
2472 (define-key map
"\C-c*" 'maxima-insert-long-comment
)
2474 ; (define-key map "\t" 'maxima-reindent-line)
2475 (define-key map
"\C-m" 'maxima-return
)
2476 (define-key map
"\M-\C-q" 'maxima-indent-form
)
2477 ; (define-key map [(control tab)] 'maxima-untab)
2479 (define-key map
"\C-c\C-d" maxima-help-map
)
2480 (define-key map
[(f12)] 'maxima-help
)
2481 (define-key map
[(meta f12
)] 'maxima-apropos
)
2483 (define-key map
"\C-c\C-nr" 'maxima-minibuffer-on-region
)
2484 (define-key map
"\C-c\C-nl" 'maxima-minibuffer-on-line
)
2485 (define-key map
"\C-c\C-nf" 'maxima-minibuffer-on-form
)
2487 (define-key map
"\M-h" 'maxima-mark-form
)
2488 (define-key map
"\C-c\)" 'maxima-check-parens
)
2489 (define-key map
[(control c
) (control \
))] 'maxima-check-form-parens
)
2490 ; (define-key map "\C-cC-\)" 'maxima-check-form-parens)
2491 (define-key map
"\177" 'backward-delete-char-untabify
)
2492 (setq maxima-mode-map map
)))
2496 (easy-menu-define maxima-mode-menu maxima-mode-map
"Maxima mode menu"
2499 ["Beginning of form" maxima-goto-beginning-of-form-interactive t
]
2500 ["End of form" maxima-goto-end-of-form-interactive t
]
2501 ["Beginning of sexp" maxima-goto-beginning-of-list-interactive t
]
2502 ["End of sexp" maxima-goto-end-of-list-interactive t
])
2504 ["Start process" maxima-start t
]
2505 ["Send region" maxima-send-region t
]
2506 ["Send buffer" maxima-send-buffer t
]
2507 ["Send line" maxima-send-line t
]
2508 ["Send form" maxima-send-form t
]
2509 ["Load file" maxima-load-file t
]
2511 ["Display buffer" maxima-display-buffer t
]
2513 ["Kill process" maxima-stop t
])
2515 ; ["Change to basic" (maxima-change-indent-style "b")
2516 ; (not (eq maxima-newline-style 'basic))]
2517 ["Change to standard" (maxima-change-indent-style "s")
2518 (not (eq maxima-indent-style
'standard
))]
2519 ["Change to smart" (maxima-change-indent-style "p")
2520 (not (eq maxima-indent-style
'perhaps-smart
))])
2522 ["Mark form" maxima-mark-form t
]
2523 ["Check parens in region" maxima-check-parens t
]
2524 ["Check parens in form" maxima-check-form-parens t
]
2525 ["Comment region" comment-region t
]
2526 ["Uncomment region" maxima-uncomment-region t
])
2528 ["Maxima info" maxima-info t
]
2529 ["Help" maxima-help t
])))
2533 ;;;; (These are used in both maxima-mode and inferior-maxima-mode).
2535 (defvar maxima-mode-abbrev-table nil
"")
2537 (defun maxima-mode-variables ()
2538 "Sets all the necessary variables for maxima-mode"
2539 (set-syntax-table maxima-mode-syntax-table
)
2540 (setq local-abbrev-table maxima-mode-abbrev-table
)
2541 (make-local-variable 'paragraph-start
)
2542 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
2543 (make-local-variable 'paragraph-separate
)
2544 (setq paragraph-separate paragraph-start
)
2545 (make-local-variable 'indent-line-function
)
2546 (setq indent-line-function
'maxima-indent-line
)
2547 (make-local-variable 'indent-tabs-mode
)
2548 (unless maxima-use-tabs
2549 (setq indent-tabs-mode nil
))
2550 (make-local-variable 'case-fold-search
)
2551 (setq case-fold-search t
)
2552 (make-local-variable 'comment-start
)
2553 (setq comment-start
"/*")
2554 (make-local-variable 'comment-end
)
2555 (setq comment-end
"*/")
2556 (make-local-variable 'comment-start-skip
)
2557 (setq comment-start-skip
"/\\*+ *")
2558 (make-local-variable 'comment-column
)
2559 (setq comment-column
40)
2560 (make-local-variable 'comment-indent-function
)
2561 (setq comment-indent-function
'comment-indent-default
)
2562 (setq imenu-generic-expression
2563 (list '(nil "^ *\\([a-zA-Z0-9_]*\\) *(.*) *:=" 1))))
2568 (defun maxima-mode ()
2569 "Major mode for editing Maxima code.
2571 Maxima mode provides the following motion commands:
2572 \\[maxima-goto-beginning-of-form-interactive]: Move to the beginning of the form.
2573 \\[maxima-goto-end-of-form-interactive]: Move to the end of the form.
2574 \\[maxima-goto-beginning-of-list-interactive]: Move to the beginning of the sexp.
2575 \\[maxima-goto-end-of-list-interactive]: Move to the end of the sexp.
2577 and the following miscellaneous commands.
2578 \\[maxima-mark-form]: Mark the current form
2579 \\[maxima-check-parens]: Check the current region for balanced parentheses.
2580 \\[maxima-check-form-parens]: Check the current form for balanced parentheses.
2582 Maxima mode has the following completions commands:
2583 M-TAB: Complete the Maxima symbol as much as possible, providing
2584 a completion buffer if there is more than one possible completion.
2586 Portions of the buffer can be sent to a Maxima process. (If a process is
2587 not running, one will be started.)
2588 \\[maxima-send-region]: Send the region to Maxima.
2589 \\[maxima-send-buffer]: Send the buffer to Maxima.
2590 \\[maxima-send-line]: Send the line to Maxima.
2591 \\[maxima-send-form]: Send the form to Maxima.
2592 \\[maxima-send-full-line-and-goto-next-form]: Send the smallest set of lines which contains
2593 the cursor and contains no incomplete forms, and go to the next form.
2594 \\[maxima-send-completed-region-and-goto-next-form]: As above, but with
2595 the region instead of the current line.
2596 \\[maxima-load-file] will prompt for a filename and load it into Maxima
2597 When something is sent to Maxima, a buffer running an inferior Maxima
2598 process will appear. It can also be made to appear by using the command
2599 \\[maxima-display-buffer].
2600 If an argument is given to a command to send information to Maxima,
2601 the region (buffer, line, form) will first be checked to make sure
2602 the parentheses are balanced.
2603 The Maxima process can be killed, after asking for confirmation
2604 with \\[maxima-stop]. To kill without confirmation, give \\[maxima-stop]
2607 By default, indentation will be to the same level as the
2608 previous line, with an additional space added for open parentheses.
2609 The behaviour of indent can be changed by the command
2610 \\[maxima-change-indent-style]. The possibilities are:
2611 Standard: Standard indentation.
2612 Perhaps smart: Tries to guess an appropriate indentation, based on
2613 open parentheses, \"do\" loops, etc.
2614 The default can be set by setting the value of the variable
2615 \"maxima-indent-style\" to either 'standard or 'perhaps-smart.
2616 In both cases, \\[maxima-untab] will remove a level of indentation.
2618 To get help on a Maxima topic, use:
2620 To read the Maxima info manual, use:
2622 To get help with the symbol under point, use:
2623 \\[maxima-completion-help].
2624 To get apropos with the symbol under point, use:
2625 \\[maxima-apropos-help].
2630 (kill-all-local-variables)
2631 (setq major-mode
'maxima-mode
)
2632 (setq mode-name
"Maxima")
2633 (use-local-map maxima-mode-map
)
2634 (maxima-mode-variables)
2636 ((eq maxima-newline-style
'basic
)
2637 (setq maxima-indent-style
'standard
))
2638 ((eq maxima-newline-style
'standard
)
2639 (setq maxima-indent-style
'standard
))
2640 ((eq maxima-newline-style
'perhaps-smart
)
2641 (setq maxima-indent-style
'perhaps-smart
)))
2642 (easy-menu-add maxima-mode-menu maxima-mode-map
)
2643 (if maxima-running-xemacs
2644 (add-local-hook 'post-command-hook
2645 'maxima-mode-add-remove-highlight
)
2646 (add-hook 'post-command-hook
2647 'maxima-mode-add-remove-highlight nil t
))
2648 (run-hooks 'maxima-mode-hook
))
2650 (define-derived-mode maxima-noweb-mode maxima-mode
2652 (setq maxima-mode-type
'maxima-noweb-mode
))
2654 ;;;; Interacting with the Maxima process
2656 ;;; Checking on the process
2657 (defun inferior-maxima-running ()
2658 (and (processp inferior-maxima-process
)
2659 (eq (process-status inferior-maxima-process
) 'run
)))
2661 ;;; Sending the information
2662 (defun inferior-maxima-get-old-input ()
2665 (if (re-search-forward
2666 (concat "\\(^(\\(" maxima-outchar
"\\|" maxima-linechar
"\\)[0-9]*) \\)")
2668 (goto-char (match-beginning 0)))
2669 (skip-chars-backward " \t\n")
2672 (re-search-backward inferior-maxima-prompt
)
2673 (setq pt1
(match-end 0)))
2674 (buffer-substring-no-properties pt1 pt
)))
2676 (defun inferior-maxima-comint-send-input (&optional query
)
2677 "Take note of position, then send the input"
2679 (setq inferior-maxima-input-end
(point)))
2680 (setq inferior-maxima-waiting-for-output t
)
2681 (comint-send-input))
2683 ;;; This next function is a modified version of comint-strip-ctrl-m
2684 (defun inferior-maxima-remove-double-prompt (&optional string
)
2685 "Fix the double prompt that occasionally appears in XEmacs."
2686 (let ((pmark (process-mark inferior-maxima-process
))
2688 (set-buffer (process-buffer inferior-maxima-process
))
2689 (setq pos comint-last-output-start
)
2690 (if (marker-position pos
)
2694 (while (re-search-forward
2695 (concat "(" maxima-inchar
"[0-9]+).*\r") pmark t
)
2696 (replace-match "" t t
))))))
2698 (defun inferior-maxima-remove-double-input-prompt (&optional string
)
2699 "Fix the double prompt that occasionally appears in Emacs."
2700 (let ((pmark (process-mark inferior-maxima-process
))
2703 (set-buffer (process-buffer inferior-maxima-process
))
2704 (goto-char inferior-maxima-input-end
)
2706 (if (looking-at (concat "(" maxima-inchar
"[0-9]+)"))
2708 (if (looking-at "
")
2711 ;;; This next function will replace tabs in the output by spaces
2712 ;; untabify isn't defined in xemacs
2713 (unless (fboundp 'untabify
)
2714 (defun untabify (start end
)
2715 "Convert all tabs in region to multiple spaces, preserving columns.
2716 Called non-interactively, the region is specified by arguments
2717 START and END, rather than by the position of point and mark.
2718 The variable `tab-width' controls the spacing of tab stops."
2722 (narrow-to-region (point-min) end
)
2724 (while (search-forward "\t" nil t
) ; faster than re-search
2726 (let ((tab-beg (point))
2727 (indent-tabs-mode nil
)
2729 (skip-chars-forward "\t")
2730 (setq column
(current-column))
2731 (delete-region tab-beg
(point))
2732 (indent-to column
)))))))
2734 (defun inferior-maxima-replace-tabs-by-spaces (&optional string
)
2735 "Replace tabs in the Maxima output by spaces."
2737 (set-buffer (process-buffer inferior-maxima-process
))
2738 (if (marker-position comint-last-output-start
)
2739 (setq beg comint-last-output-start
)
2740 (setq beg
(point-min)))
2742 (process-mark inferior-maxima-process
))))
2744 (defun inferior-maxima-wait-for-output ()
2745 "Wait for output from the Maxima process."
2747 inferior-maxima-waiting-for-output
2748 (inferior-maxima-running))
2749 (accept-process-output inferior-maxima-process
))
2750 (if maxima-running-xemacs
2752 (sit-for 0 inferior-maxima-after-output-wait
)))
2754 (defun inferior-maxima-output-filter (str)
2755 "Look for a new input prompt"
2757 (string-match "? *$" str
)
2758 (not (string-match (concat "(" maxima-outchar
"[0-9]+)") str
)))
2759 (maxima-ask-question str
))
2760 ((string-match inferior-maxima-prompt str
)
2761 (if (and inferior-maxima-process
(not (string= maxima-block
"")))
2762 (maxima-single-string (maxima-get-command))
2763 (if (not inferior-maxima-process
)
2764 (maxima-clear-queue))
2765 (setq inferior-maxima-waiting-for-output nil
)))))
2767 (defun inferior-maxima-sentinel (proc state
)
2768 "Write the input history when the process ends"
2769 (unless (string-match "^run" state
)
2770 (comint-write-input-ring)))
2772 (defun maxima-start ()
2773 "Start the Maxima process."
2775 (if (processp inferior-maxima-process
)
2776 (unless (eq (process-status inferior-maxima-process
) 'run
)
2777 (delete-process inferior-maxima-process
)
2778 (if (get-buffer "*maxima*")
2780 (set-buffer "*maxima*")
2782 (setq inferior-maxima-process nil
)))
2783 (unless (processp inferior-maxima-process
)
2784 (setq inferior-maxima-input-end
0)
2785 (setq inferior-maxima-waiting-for-output t
)
2788 (setq mbuf
(apply #'make-comint
"maxima" maxima-command nil maxima-args
))
2791 (setq inferior-maxima-process
(get-buffer-process mbuf
))
2792 (add-hook 'comint-output-filter-functions
2793 'inferior-maxima-output-filter nil t
)
2794 (add-hook 'comint-output-filter-functions
2795 'inferior-maxima-replace-tabs-by-spaces nil t
)
2796 ; (add-hook 'comint-output-filter-functions
2797 ; 'inferior-maxima-remove-double-input-prompt nil t)
2798 (if maxima-fix-double-prompt
2799 (add-hook 'comint-output-filter-functions
2800 'inferior-maxima-remove-double-prompt nil t
))
2801 (inferior-maxima-wait-for-output)
2802 (inferior-maxima-mode)))))
2804 (defun maxima-stop (&optional arg
)
2805 "Kill the currently running Maxima process."
2807 (if (processp inferior-maxima-process
)
2810 (delete-process inferior-maxima-process
)
2811 (kill-buffer "*maxima*")
2812 (setq inferior-maxima-process nil
))
2813 (if (y-or-n-p "Really quit Maxima? ")
2815 (delete-process inferior-maxima-process
)
2816 (kill-buffer "*maxima*")
2817 (setq inferior-maxima-process nil
))))))
2819 ;;; Sending information to the process
2821 (defun maxima-single-string (string)
2822 "Send a string to the Maxima process."
2823 (setq string
(maxima-strip-string-add-semicolon string
))
2825 ; (inferior-maxima-wait-for-output)
2826 (save-current-buffer
2827 (set-buffer (process-buffer inferior-maxima-process
))
2828 (goto-char (point-max))
2829 (let ((start (point)))
2831 (untabify start
(point)))
2832 (goto-char (point-max))
2833 (inferior-maxima-comint-send-input)
2834 (goto-char (point-max))))
2836 (defun maxima-ask-question (string)
2837 "Ask the question maxima wants answered."
2838 (let ((ans (read-string
2839 (concat (maxima-strip-string string
) " " ))))
2840 (unless (string-match "[;$]" ans
)
2841 (setq ans
(concat ans
";")))
2842 (setq ans
(maxima-strip-string ans
))
2843 (save-current-buffer
2844 (set-buffer (process-buffer inferior-maxima-process
))
2845 (goto-char (point-max))
2847 (inferior-maxima-comint-send-input t
)
2848 (goto-char (point-max)))))
2850 (defun maxima-get-command (&optional arg
)
2851 "Return the maxima command that's at the front of maxima-block.
2852 Remove it from the front of maxima-block.
2853 With an argument, use maxima-block-wait instead of maxima-block."
2854 (let* ((tmpfile (maxima-make-temp-name))
2855 (tmpbuf (get-buffer-create tmpfile
))
2860 (maxima-remove-kill-buffer-hooks)
2862 (insert maxima-block-wait
)
2863 (insert maxima-block
))
2864 (goto-char (point-min))
2865 (maxima-forward-over-comment-whitespace)
2867 (if (string-match "[$;]\\|:lisp"
2868 (buffer-substring-no-properties (point) (point-max)))
2870 (if (looking-at ":lisp")
2872 (search-forward ":lisp")
2874 (setq command
(buffer-substring-no-properties pt
(point))))
2875 (maxima-goto-end-of-form)
2876 (setq command
(buffer-substring-no-properties pt
(point))))
2877 (maxima-forward-over-comment-whitespace)
2879 (setq maxima-block-wait
2880 (maxima-strip-string-add-semicolon
2881 (buffer-substring-no-properties (point) (point-max))))
2883 (maxima-strip-string-add-semicolon
2884 (buffer-substring-no-properties (point) (point-max)))))
2885 (setq command
(buffer-substring-no-properties pt
(point))))
2887 (setq maxima-block-wait
"")
2888 (setq maxima-block
"")))
2890 (if (string= maxima-block-wait
";") (setq maxima-block-wait
""))
2891 (if (string= maxima-block
";") (setq maxima-block
"")))
2892 (kill-buffer tmpbuf
))
2895 (defun maxima-send-block (stuff)
2896 "Send a block of code to Maxima."
2898 (setq stuff
(maxima-strip-string-add-semicolon stuff
))
2899 ; (unless (string-match (substring stuff -1) ";$")
2900 ; (setq stuff (concat stuff ";")))
2901 (if (string= maxima-block
"")
2903 (setq maxima-block stuff
)
2904 (maxima-single-string (maxima-get-command)))
2905 (setq maxima-block
(concat maxima-block stuff
))))
2907 (defun maxima-send-block-wait (stuff)
2908 "Send a block of code to Maxima; wait for it to finish.
2909 Return the last string sent."
2911 (if (not (string= maxima-block
""))
2912 (message "Maxima process currently busy.")
2913 (setq maxima-block-wait
(maxima-strip-string-add-semicolon stuff
))
2914 (while (not (string= maxima-block-wait
""))
2915 (maxima-single-string-wait (maxima-get-command t
)))))
2917 (defun maxima-clear-queue ()
2918 "Clear out the queue of commands to send to the maxima process."
2920 (setq maxima-block
"")
2921 (setq maxima-block-wait
""))
2923 ;;; Getting information back from Maxima.
2925 (defun maxima-last-output ()
2926 "Get the most recent output from Maxima."
2928 (inferior-maxima-wait-for-output)
2930 (set-buffer (process-buffer inferior-maxima-process
))
2932 (pmark (progn (goto-char (process-mark inferior-maxima-process
))
2936 (goto-char inferior-maxima-input-end
)
2939 (output (buffer-substring-no-properties beg pmark
)))
2943 (defun maxima-last-output-noprompt ()
2944 "Return the last Maxima output, without the prompts"
2946 (if (not (inferior-maxima-running))
2947 (maxima-last-output)
2948 (let* ((output (maxima-last-output))
2954 ;; Replace the output prompt with spaces
2955 (setq beg
(string-match
2956 (concat "\\(^(" maxima-outchar
"[0-9]*) \\)") output
))
2959 (setq end
(1+ (string-match ")" output beg
)))
2960 (setq newstring
(substring output
0 beg
))
2961 (setq k
(- end beg
))
2963 (setq newstring
(concat newstring
" "))
2969 (defun maxima-last-output-tex-noprompt ()
2970 "Return the last Maxima output, between the dollar signs."
2972 (let* ((output (maxima-last-output))
2973 (begtex (string-match "\\$\\$" output
))
2974 (endtex (string-match "\\$\\$" output
(1+ begtex
))))
2976 (substring output begtex
(+ endtex
2))
2980 ;;; Sending information to the process should be done through these
2981 ;; next five commands
2983 (defun maxima-single-string-wait (string)
2984 "Send a single string to the maxima process,
2985 waiting for output after."
2986 (inferior-maxima-wait-for-output)
2987 (maxima-single-string string
)
2988 (inferior-maxima-wait-for-output))
2990 (defun maxima-string (string)
2991 "Send a string to the Maxima process."
2992 (maxima-send-block string
))
2994 (defun maxima-region (beg end
)
2995 "Send the region to the Maxima process."
2996 (setq maxima-mode-region-begin beg
)
2997 (setq maxima-mode-region-end end
)
2999 (buffer-substring-no-properties beg end
)))
3001 ;;; Some functions to send commands to the process.
3003 (defun maxima-send-region (beg end
&optional arg
)
3004 "Send the current region to the Maxima process.
3005 With an argument, don't check the parentheses first."
3006 (interactive "r\nP")
3008 (maxima-region beg end
)
3009 (if (maxima-check-parens beg end
)
3010 (maxima-region beg end
)))
3011 (maxima-display-buffer))
3013 (defun maxima-send-buffer (&optional arg
)
3014 "Send the buffer to the Maxima process, after checking the parentheses.
3015 With an argument, don't check the parentheses."
3017 (maxima-send-region (point-min) (point-max) arg
))
3019 (defun maxima-send-line (&optional arg
)
3020 "Send the current line to the Maxima process, after checking parentheses.
3021 With an argument, don't check parentheses."
3023 (let ((b (maxima-line-beginning-position))
3024 (e (maxima-line-end-position)))
3025 (maxima-send-region b e arg
)))
3027 (defun maxima-send-form (&optional arg
)
3028 "Send the current form to the Maxima process, checking parentheses.
3029 With an argument, don't check parentheses."
3031 (maxima-send-region (maxima-form-beginning-position)
3032 (maxima-form-end-position-or-point-max) arg
))
3034 (defun maxima-send-previous-form (&optional arg
)
3035 "Send the previous form to the Maxima process, checking parentheses.
3036 With an argument, don't check parentheses."
3039 (if (maxima-re-search-backward "[;$]")
3040 (maxima-send-region (maxima-form-beginning-position)
3041 (maxima-form-end-position-or-point-max) arg
)
3042 (message "No previous form."))))
3044 (defun maxima-send-previous-form-and-goto-end-of-form (&optional arg
)
3045 "Send the previous form to the Maxima process and go to the end of form."
3047 (maxima-send-previous-form arg
)
3048 (maxima-goto-end-of-form-interactive))
3050 (defun maxima-send-full-line ()
3051 "Send the minimum number of lines such that the current is one of them,
3052 and such that no line contains an incomplete form."
3054 (let ((beg (point)) (end (point)))
3059 (maxima-goto-beginning-of-form)
3060 (while (< (point) beg
)
3064 (maxima-goto-beginning-of-form)))
3068 (while (and (< (maxima-form-beginning-position) end
) (< end
(point-max)))
3072 (setq end
(point))))
3073 (skip-chars-backward " \t;$")
3074 (if (re-search-forward "[;$]" end t
)
3075 (maxima-send-region beg
(point))
3076 (error "No ; or $ at end"))
3079 (defun maxima-send-full-line-and-goto-next-form ()
3080 "Do a maxima-send-full-line and go to the beginning of the next form."
3082 (goto-char (maxima-send-full-line))
3083 (maxima-goto-beginning-of-form))
3085 (defun maxima-send-completed-region (beg end
)
3086 "Send the marked region, but complete possibly non-complete forms at the boundaries."
3087 (interactive "r\nP")
3092 (setq beg1
(maxima-form-beginning-position))
3094 (setq end1
(maxima-form-end-position-or-point-max))
3095 (maxima-send-region beg1 end1
)
3098 (defun maxima-send-completed-region-and-goto-next-form (beg end
)
3099 "Do a maxima-send-completed-region and go to the beginning of the next form."
3100 (interactive "r\nP")
3101 (goto-char (maxima-send-completed-region beg end
))
3102 (maxima-goto-beginning-of-form))
3104 (defun maxima-display-buffer ()
3105 "Display the inferior-maxima-process buffer so the recent output is visible."
3107 (let ((origbuffer (current-buffer)))
3108 (if (not (processp inferior-maxima-process
))
3110 (pop-to-buffer (process-buffer inferior-maxima-process
))
3111 (goto-char (point-max))
3112 ; (recenter (universal-argument))
3113 (pop-to-buffer origbuffer
)))
3116 ;;;; The inferior Maxima process
3118 ;;; Completions from previous input
3120 ;; First, a function to take the comint-input-ring and return a
3121 ;; list of previous inputs
3123 (defun inferior-maxima-previous-inputs ()
3124 "Return a list of previous inputs."
3127 (comint-inputs (cddr comint-input-ring
))
3129 (while (and (< i comint-input-ring-size
)
3130 (not (null (aref comint-inputs i
))))
3131 (unless (member (aref comint-inputs i
) inputs
)
3132 (setq inputs
(cons (aref comint-inputs i
) inputs
)))
3136 (defun inferior-maxima-input-complete ()
3137 "Complete line from list of previous input."
3139 (let* ((stub (buffer-substring-no-properties
3140 (inferior-maxima-bol-position) (point)))
3141 (completions (all-completions (downcase stub
)
3142 (inferior-maxima-previous-inputs))))
3145 (function (lambda (x) (maxima-he-transfer-case stub x
))) completions
))
3146 (cond ((null completions
)
3147 (message "No completions of %s" stub
))
3148 ((= 1 (length completions
)) ; Gotcha!
3149 (let ((completion (car completions
)))
3150 (if (string-equal completion stub
)
3151 (message "Sole completion")
3152 (insert (substring completion
(length stub
)))
3153 (message "Completed"))))
3154 (t ; There's no unique completion.
3155 (comint-dynamic-list-completions completions
)))))
3157 (defun inferior-maxima-complete ()
3158 "Complete the current object, depending on context."
3160 (let* ((pmin (save-excursion
3161 (re-search-backward inferior-maxima-prompt
)
3163 (pps (parse-partial-sexp pmin
(point))))
3165 ;; complete filename if the point is in a string
3167 (maxima-complete-filename))
3168 ;; Otherwise, complete the symbol
3170 (maxima-complete-symbol)))))
3172 ;; (defun maxima-input-he-try (old)
3175 ;; ;;; let beg be the beginning of the word
3177 ;; (he-init-string (inferior-maxima-bol-position) (point))
3178 ;; (setq he-expand-list
3179 ;; (all-completions (downcase he-search-string) (maxima-previous-inputs)))
3180 ;; (setq he-expand-list
3181 ;; (mapcar (function
3182 ;; (lambda (x) (he-transfer-case he-search-string x)))
3184 ;; (if he-expand-list
3185 ;; (he-substitute-string (car he-expand-list))
3187 ;; (setq he-expand-list (cdr he-expand-list))
3188 ;; (if he-expand-list
3189 ;; (he-substitute-string (car he-expand-list))
3190 ;; (he-reset-string)
3193 ;; (fset 'maxima-dynamic-input-complete
3194 ;; (make-hippie-expand-function '(maxima-input-he-try)))
3196 ;;; Sending a line to the process while in the process buffer
3198 (defun inferior-maxima-check-and-send-line ()
3199 "Check the lines for mis-matched parentheses, then send the line."
3206 (skip-chars-backward " \t")
3208 (when (looking-at "[$;]")
3214 (re-search-backward inferior-maxima-prompt
)
3215 (setq pt1
(match-end 0)))
3216 (if (maxima-check-parens pt1 pt
)
3217 (inferior-maxima-comint-send-input)))
3218 (inferior-maxima-comint-send-input))))
3220 (defun inferior-maxima-send-line ()
3221 "Send the line to the Maxima process."
3223 (inferior-maxima-comint-send-input))
3225 (defun inferior-maxima-bol ()
3226 "Go to the beginning of the line, but past the prompt."
3228 (let ((eol (save-excursion (end-of-line) (point))))
3230 (if (and (looking-at inferior-maxima-prompt
)
3231 (<= (match-end 0) eol
))
3232 (goto-char (match-end 0)))))
3234 (defun inferior-maxima-bol-position ()
3236 (inferior-maxima-bol)
3240 ;;;; Inferior Maxima mode
3242 (define-derived-mode inferior-maxima-mode
3245 "Major mode for interacting with an inferior Maxima process.
3247 Return will check the line for balanced parentheses, and send line as input.
3248 Control return will send the line as input without checking for balanced
3251 M-TAB will complete the Maxima symbol as much as possible, providing
3252 a completion buffer if there is more than one possible completion.
3254 \\[maxima-smart-complete] will complete the input line, based on previous input lines.
3255 \\[maxima-help] will get help on a Maxima topic.
3256 \\[maxima-info] will bring up the Maxima info manual.
3257 \\[maxima-stop] will kill the process and the buffer, after asking for
3258 confirmation. To kill without confirmation, give \\[maxima-stop] an
3261 To scroll through previous commands,
3262 \\[comint-previous-input] will bring the previous input to the current prompt,
3263 \\[comint-next-input] will bring the next input to the prompt.
3264 \\[comint-previous-matching-input] will bring the previous input matching
3265 a regular expression to the prompt,
3266 \\[comint-next-matching-input] will bring the next input matching
3267 a regular expression to the prompt.
3269 ; (if maxima-use-full-color-in-process-buffer
3270 ; (inferior-maxima-font-setup))
3271 (setq comint-prompt-regexp inferior-maxima-prompt
)
3272 (setq comint-get-old-input
(function inferior-maxima-get-old-input
))
3273 (setq mode-line-process
'(": %s"))
3274 (maxima-mode-variables)
3276 (if (and (not maxima-running-xemacs
) (< emacs-major-version
21))
3277 (make-local-hook 'kill-buffer-hook
))
3278 (if maxima-running-xemacs
3279 (add-local-hook 'kill-buffer-hook
3282 (maxima-clear-queue)
3283 (if (processp inferior-maxima-process
)
3284 (delete-process inferior-maxima-process
))
3285 (setq inferior-maxima-process nil
)
3286 (run-hooks 'inferior-maxima-exit-hook
))))
3287 (add-hook 'kill-buffer-hook
3290 (maxima-clear-queue)
3291 (if (processp inferior-maxima-process
)
3292 (delete-process inferior-maxima-process
))
3293 (setq inferior-maxima-process nil
)
3294 (run-hooks 'inferior-maxima-exit-hook
))) t t
))
3295 (setq comint-input-ring-size maxima-input-history-length
)
3296 (if maxima-save-input-history
3298 (setq comint-input-ring-file-name maxima-input-history-file
)
3299 (comint-read-input-ring t
)
3300 (set-process-sentinel inferior-maxima-process
3301 'inferior-maxima-sentinel
)))
3302 (set (make-local-variable 'comint-prompt-read-only
) t
)
3303 (run-hooks 'inferior-maxima-mode-hook
))
3307 (define-key inferior-maxima-mode-map
"\C-a"
3308 'inferior-maxima-bol
)
3309 (define-key inferior-maxima-mode-map
"\C-m"
3310 'inferior-maxima-check-and-send-line
)
3311 (define-key inferior-maxima-mode-map
[(control return
)]
3312 'inferior-maxima-send-line
)
3313 (define-key inferior-maxima-mode-map
[(meta control tab
)]
3314 'inferior-maxima-input-complete
)
3315 (define-key inferior-maxima-mode-map
"\e\t" 'inferior-maxima-complete
)
3316 (define-key inferior-maxima-mode-map
"\177" 'backward-delete-char-untabify
)
3317 (define-key inferior-maxima-mode-map
"\C-c\C-k" 'maxima-stop
)
3318 (define-key inferior-maxima-mode-map
"\C-c\C-d" maxima-help-map
)
3322 (easy-menu-define inferior-maxima-mode-menu inferior-maxima-mode-map
3326 ["Maxima info" maxima-info t
]
3327 ["Help" maxima-help t
])
3329 ["Kill process" maxima-stop t
])))
3334 "Run Maxima interactively inside a buffer."
3337 (switch-to-buffer (process-buffer inferior-maxima-process
)))
3339 ;;; Interacting with Maxima outside of a maxima buffer
3341 (defun maxima-minibuffer ()
3342 "Communicate with Maxima through the minibuffer"
3345 (let ((input (read-string "Maxima: " nil maxima-minibuffer-history
))
3347 (twod (and maxima-minibuffer-2d
(not maxima-running-xemacs
))))
3348 (setq input
(maxima-strip-string-add-semicolon input
))
3350 (maxima-single-string-wait
3351 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3352 (maxima-single-string-wait
3353 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3354 (maxima-single-string-wait input
)
3355 (setq output
(maxima-last-output-noprompt))
3356 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3358 (setq output
(maxima-remove-whitespace-from-ends output
))
3359 ;; Strip the beginning and trailing newline
3360 (while (string-match "\\` *\n" output
)
3361 (setq output
(substring output
(match-end 0))))
3362 (while (string-match "\n *\\'" output
)
3363 (setq output
(substring output
0 (match-beginning 0)))))
3364 (setq output
(maxima-replace-in-string "%" "%%" output
))
3367 (defun maxima-minibuffer-delete-output (beg end
)
3368 (let ((mmom (maxima-minor-output-mark))
3369 (mmoe (maxima-minor-output-mark-end)))
3371 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmom
)
3372 (string= (match-string 0 mmom
) mmom
))
3373 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmoe
)
3374 (string= (match-string 0 mmoe
) mmoe
)))
3375 (message "Old output not deleted (improper delimiter).")
3379 (if (search-forward mmom end t
)
3381 (setq pt
(match-beginning 0))
3382 (search-forward mmoe
)
3383 (kill-region pt
(point)))
3385 (if (looking-at (concat "[ \n]*" (regexp-quote mmom
)))
3387 (search-forward mmoe
)
3388 (kill-region end
(point)))))
3391 (defun maxima-minibuffer-on-region (beg end
&optional arg
)
3392 "Send the current region to Maxima; display last output in minibuffer.
3393 With an argument, insert \" ==> \" into the current buffer,
3394 followed by the output, followed by \"\\\". In this case, any previous output
3396 (interactive "r\nP")
3406 (twod (and maxima-minibuffer-2d
(not maxima-running-xemacs
))))
3409 (maxima-forward-over-comment-whitespace)
3410 (setq realbeg
(point))
3411 (if (re-search-forward (maxima-minor-output-mark) end t
)
3413 (if (eq major-mode
'maxima-mode
)
3414 (- (point) (length maxima-mode-minor-output
))
3415 (- (point) (length maxima-minor-output
))))
3417 (maxima-back-over-comment-whitespace)
3418 (setq realend
(point))))
3419 (setq input
(maxima-strip-string-add-semicolon
3420 (buffer-substring-no-properties realbeg realend
)))
3422 (maxima-minibuffer-delete-output beg end
))
3423 (setq maxima-minor-mode-region-begin realbeg
)
3424 (setq maxima-minor-mode-region-end realend
)
3425 (when (or (not maxima-minor-mode-check-input
)
3427 (maxima-check-parens realbeg realend
)
3428 (maxima-check-commas realbeg realend
)))
3431 (maxima-single-string-wait
3432 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3433 (maxima-single-string-wait
3434 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3435 (maxima-send-block-wait input
)
3436 (setq output
(maxima-last-output-noprompt))
3437 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3439 (setq output
(maxima-remove-whitespace-from-ends output
))
3440 ;; Strip the beginning and trailing newline
3441 (while (string-match "\\` *\n" output
)
3442 (setq output
(substring output
(match-end 0))))
3443 (while (string-match "\n *\\'" output
)
3444 (setq output
(substring output
0 (match-beginning 0)))))
3446 (setq minibufferoutput
(maxima-replace-in-string "%" "%%" output
))
3447 (message minibufferoutput
))
3452 (if (looking-at "^")
3453 (setq realend
(1- realend
)))
3454 ;(delete-region realend end)
3456 (skip-chars-backward " \t\n")
3457 (when (not (= (point) realend
))
3458 (setq delreg
(buffer-substring-no-properties (point) realend
))
3459 (kill-region (point) realend
)
3461 ((< (length delreg
) 15)
3462 (setq delreg
(maxima-replace-in-string "\n" " " delreg
))
3463 (message (concat "\"" delreg
"\" killed")))
3466 (maxima-replace-in-string "\n" " "(substring delreg
0 5)))
3468 (maxima-replace-in-string "\n" " "(substring delreg -
5)))
3469 (message (concat "\"" delregbeg
" ... " delregend
"\" killed")))))
3470 (let ((ind (save-excursion
3474 (there (make-marker)))
3476 (string-match "\n" output
)
3477 (> (+ (current-column) (length output
)) fill-column
))
3480 (setq here
(point)))
3482 (insert (maxima-minor-output-mark) " " output
3483 (maxima-minor-output-mark-end))
3484 (set-marker there
(point))
3486 (goto-char (line-end-position))
3487 ; (fill-region (line-beginning-position) (point))
3490 (buffer-substring-no-properties here
(point)))
3493 (indent-region (point) there ind
)))
3495 (let ((ind (save-excursion
3501 (insert (maxima-minor-output-mark) "\n")
3503 (insert output
(maxima-minor-output-mark-end))
3504 (indent-region here
(point) ind
))))))))
3506 (defun maxima-minibuffer-on-line (&optional arg
)
3507 "Send the current line to Maxima; display last output in minibuffer.
3508 With an argument, insert \" ==> \" into the current buffer,
3509 followed by the output. In this case, anything in the line
3510 after any occurrence of \" ==> \" will be deleted."
3512 (maxima-minibuffer-on-region
3513 (maxima-line-beginning-position)
3514 (maxima-line-end-position)
3517 (defun maxima-minibuffer-on-form (&optional arg
)
3518 "Send the current form to Maxima; display last output in minibuffer.
3519 With an argument, insert \" ==> \" into the current buffer,
3520 followed by the output."
3522 (let ((beg (maxima-form-beginning-position))
3523 (end (maxima-form-end-position)))
3525 (when (re-search-backward "^[ \t]*$" beg t
)
3526 (maxima-forward-over-comment-whitespace)
3527 (setq beg
(point))))
3528 (maxima-minibuffer-on-region beg end arg
)))
3530 (defun maxima-minibuffer-on-determined-region (&optional arg
)
3531 "Send a determined region to Maxima; display the output in the minibuffer.
3532 The region is the region between `maxima-minor-prefix' and `maxima-minor-postfix'
3533 With an argument, insert \" ==> \" into the current buffer,
3534 followed by the output. In this case, anything in the determined region
3535 after any occurrence of \" ==> \" will be deleted."
3541 (if (re-search-backward maxima-minor-prefix nil t
)
3542 (setq beg
(match-end 0))
3543 (error "No beginning to determined region"))
3545 (if (re-search-forward maxima-minor-prefix nil t
)
3546 (setq end
(match-beginning 0))))
3547 (maxima-minibuffer-on-region beg end arg
)))
3549 (defun maxima-insert-last-output ()
3551 (maxima-single-string-wait
3552 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);")
3553 (let ((output (maxima-last-output-noprompt)))
3554 (maxima-single-string "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3555 (insert (maxima-remove-whitespace-from-ends output
))))
3557 (defun maxima-insert-last-output-tex ()
3559 (maxima-single-string-wait "tex(%);")
3560 (let ((output (substring (maxima-last-output-tex-noprompt) 2 -
3)))
3561 (maxima-single-string "block(linenum:linenum-2,%th(2));")
3564 ;;; The Maxima minor mode
3566 (defvar maxima-minor-mode-map nil
3567 "The keymap for maxima-minor-mode.")
3569 (if maxima-minor-mode-map
3571 (let ((map (make-sparse-keymap)))
3572 (define-key map
"\C-c=m" 'maxima-minibuffer
)
3573 (define-key map
"\C-c=e" 'maxima-minibuffer-on-determined-region
)
3574 (define-key map
"\C-c=l" 'maxima-minibuffer-on-line
)
3575 (define-key map
"\C-c=r" 'maxima-minibuffer-on-region
)
3576 (define-key map
"\C-c=f" 'maxima-minibuffer-on-form
)
3577 (define-key map
"\C-c=o" 'maxima-insert-last-output
)
3578 (define-key map
"\C-c=t" 'maxima-insert-last-output-tex
)
3579 (define-key map
"\C-c=d" maxima-help-map
)
3580 (setq maxima-minor-mode-map map
)))
3582 (unless (fboundp 'define-minor-mode
)
3583 (defalias 'define-minor-mode
'easy-mmode-define-minor-mode
))
3585 (define-minor-mode maxima-minor-mode
3586 "Toggle Maxima minor mode.
3587 With no argument, this command toggles the mode.
3588 Non-null prefix argument turns on the mode.
3589 Null prefix argument turns off the mode.
3591 When Maxima minor mode is enabled, the following keystrokes
3593 \\[maxima-minibuffer-on-determined-region]
3594 Send the region between the customizable regexps
3595 `maxima-minor-prefix' and `maxima-minor-postfix' to Maxima
3596 and display the result in the minibuffer.
3597 \\[maxima-minibuffer-on-line]
3598 Send the current line to Maxima and display the result in the minibuffer.
3599 \\[maxima-minibuffer-on-region]
3600 Send the current region to Maxima and display the result in the minibuffer.
3601 \\[maxima-minibuffer-on-form]
3602 Send the current form to Maxima and display the result in the minibuffer.
3603 (The form is the region between the preceding ; or $ and the subsequent
3605 With a prefix, the above commands will insert the output in the current
3606 buffer, preceded by \" ==> \" (customizable with `maxima-minor-output').
3607 \\[maxima-minibuffer]
3608 Prompt for an expression in the minibuffer, return result in minibuffer.
3609 \\[maxima-insert-last-output]
3610 Insert the last Maxima result into the current buffer.
3611 \\[maxima-insert-last-output-tex]
3612 Insert the last Maxima result in TeX form into the current buffer."
3613 ;; The initial value.; :initial-value
3615 ;; The indicator for the mode line.; :lighter
3619 ;;; For highlighting the region being sent
3621 (defun maxima-minor-mode-add-highlight ()
3622 (maxima-minor-mode-remove-highlight)
3623 (when (and maxima-minor-mode-region-begin
3624 maxima-minor-mode-region-end
)
3625 (if maxima-running-xemacs
3627 (setq maxima-minor-mode-highlight
3629 maxima-minor-mode-region-begin
3630 maxima-minor-mode-region-end
))
3631 (set-extent-property maxima-minor-mode-highlight
'face
'highlight
))
3632 (setq maxima-minor-mode-highlight
3634 maxima-minor-mode-region-begin
3635 maxima-minor-mode-region-end
))
3636 (overlay-put maxima-minor-mode-highlight
'face
'highlight
))
3637 (setq maxima-minor-mode-region-begin nil
)
3638 (setq maxima-minor-mode-region-end nil
)))
3640 (defun maxima-minor-mode-remove-highlight ()
3641 (when maxima-minor-mode-highlight
3642 (if maxima-running-xemacs
3643 (delete-extent maxima-minor-mode-highlight
)
3644 (delete-overlay maxima-minor-mode-highlight
))
3645 (setq maxima-minor-mode-highlight nil
)))
3647 (defun maxima-minor-mode-add-remove-highlight ()
3649 (eq this-command
'maxima-minibuffer-on-region
)
3650 (eq this-command
'maxima-minibuffer-on-determined-region
)
3651 (eq this-command
'maxima-minibuffer-on-form
)
3652 (eq this-command
'maxima-minibuffer-on-line
))
3653 (maxima-minor-mode-add-highlight)
3654 (maxima-minor-mode-remove-highlight)))
3656 (defun maxima-minor-mode-highlighting ()
3657 (if maxima-running-xemacs
3658 (add-local-hook 'post-command-hook
3659 'maxima-minor-mode-add-remove-highlight
)
3660 (add-hook 'post-command-hook
3661 'maxima-minor-mode-add-remove-highlight nil t
)))
3663 (add-hook 'maxima-minor-mode-hook
3664 'maxima-minor-mode-highlighting
)
3667 ;;; maxima.el ends here