Updating configure.ac with new version number
[maxima/cygwin.git] / interfaces / emacs / emaxima / maxima.el
blob29eb8148e83329ec98af50bf35be6d8651c44af9
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
7 ;; Jay Belanger
8 ;;
9 ;; Keywords: maxima
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.
15 ;;
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.
20 ;;
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
27 ;;; Commentary:
29 ;; This is a branch of William Schelter's maxima-mode.el
31 ;; Quick intro
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,
37 ;; put the lines
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,
46 ;; use
47 ;; C-c C-d h
48 ;; or
49 ;; f12.
50 ;; To get help with the symbol under point, use ("d" for describe):
51 ;; C-c C-d d
52 ;; or
53 ;; C-c C-d C-d
54 ;; For apropos help, use:
55 ;; C-c C-d a
56 ;; or
57 ;; C-c C-d C-a
58 ;; To get apropos with the symbol under point, use:
59 ;; C-c C-d p
60 ;; C-c C-d C-p
61 ;; or M-f12.
62 ;; To read the Maxima info manual, use:
63 ;; C-c C-d m
64 ;; C-c C-d C-m
65 ;; C-c C-d i
66 ;; or
67 ;; C-c C-d C-i
68 ;; (For Maxima minor mode, replace C-cC-d by C-c=d.)
70 ;; ** Maxima mode **
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
101 ;; C-c C-p.
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
107 ;; an argument.
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
130 ;; parentheses.
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
140 ;; argument.
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.
154 ;; The line
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
187 ;; ; or $)
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)
208 ;; in your .emacs
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.)
217 ;; C-c=e
218 ;; `maxima-minibuffer-on-determined-region'
219 ;; C-c=l
220 ;; `maxima-minibuffer-on-line'
221 ;; C-c=r
222 ;; `maxima-minibuffer-on-region'
223 ;; C-c=f
224 ;; `maxima-minibuffer-on-form'
225 ;; C-c=m
226 ;; `maxima-minibuffer'
227 ;; C-c=o
228 ;; `maxima-insert-last-output'
229 ;; C-c=t
230 ;; `maxima-insert-last-output-tex'
232 ;;; Code:
234 ;;;; First
235 (defvar maxima-running-xemacs
236 (featurep 'xemacs))
238 ;;;; The requires
240 (require 'comint)
241 (require 'easymenu)
242 (require 'maxima-font-lock)
244 ;;;; The variables that the user may wish to change
246 (defgroup maxima nil
247 "Maxima mode"
248 :prefix "maxima-"
249 :tag "Maxima")
251 (defcustom maxima-inchar "\\(C\\|%i\\)"
252 "*The character used for an input prompt."
253 :group 'maxima
254 :type 'string)
256 (defcustom maxima-outchar "\\(D\\|%o\\)"
257 "*The character used for an output prompt."
258 :group 'maxima
259 :type 'string)
261 (defcustom maxima-linechar "\\(E\\|%t\\)"
262 "*The character used for an intermediate output prompt."
263 :group 'maxima
264 :type 'string)
266 (defcustom maxima-indent-amount 2
267 "*The amount of each indentation level in `maxima-mode'.
268 This is used after `then', etc."
269 :group 'maxima
270 :type '(integer))
272 (defcustom maxima-paren-indent-amount 1
273 "*The amount of indentation after a parenthesis."
274 :group 'maxima
275 :type '(integer))
277 (defcustom maxima-function-indent-amount 2
278 "*The amount of extra indentation to give within functions."
279 :group 'maxima
280 :type 'integer)
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."
289 :group 'maxima
290 :type '(integer))
292 (defcustom maxima-multiline-comment-indent-amount 2
293 "*The amount of extra indentation inside a multiline comment."
294 :group 'maxima
295 :type '(integer))
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 `/*'."
300 :group 'maxima
301 :type 'boolean)
303 (defcustom maxima-if-extra-indent-amount 0
304 "*The amount of extra indentation to give a \"then\" following an \"if\"."
305 :group 'maxima
306 :type 'integer)
308 ;(defcustom maxima-use-dynamic-complete nil
309 ; "*If non-nil, then M-TAB will complete words dynamically."
310 ; :group 'maxima
311 ; :type 'boolean)
313 (defcustom maxima-indent-style 'standard
314 "*Determines how `maxima-mode' will handle tabs.
315 Choices are 'standard, 'perhaps-smart"
316 :group 'maxima
317 :type '(choice :menu-tag "Indent style"
318 :tag "Indent style"
319 (const standard)
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"
325 :group 'maxima
326 :type '(choice :menu-tag "Return style"
327 :tag "Return style"
328 (const newline)
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'."
337 :group 'maxima
338 :type 'string)
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."
343 :group 'maxima
344 :type 'list)
346 (defcustom maxima-use-tabs nil
347 "*If non-nil, indentation will use tabs."
348 :group 'maxima
349 :type 'boolean)
351 (defcustom maxima-minibuffer-2d nil
352 "*If non-nil, use 2D output for maxima-minibuffer."
353 :group 'maxima
354 :type 'boolean)
356 (defcustom maxima-use-full-color-in-process-buffer nil
357 "*If non-nil, font-lock the maxima process buffer."
358 :group 'maxima
359 :type 'boolean)
361 (defcustom maxima-fix-double-prompt maxima-running-xemacs
362 "*If non-nil, fix the double prompt that sometimes appears in XEmacs."
363 :group 'maxima
364 :type 'boolean)
366 (defcustom maxima-save-input-history nil
367 "*If non-nil, save the input history in a file."
368 :group 'maxima
369 :type 'boolean)
371 (defcustom maxima-input-history-file "~/.maxima_history"
372 "*A file to save the input history in."
373 :group 'maxima
374 :type 'file)
376 (defcustom maxima-input-history-length 50
377 "*How many lines of history to save."
378 :group 'maxima
379 :type 'integer)
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."
384 :group 'maxima
385 :type 'string)
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."
390 :group 'maxima
391 :type 'string)
393 (defcustom maxima-minor-output "==>"
394 "*A string to insert in the buffer right before the output."
395 :group 'maxima
396 :type 'string)
398 (defcustom maxima-minor-output-end " //"
399 "*A string to insert in the buffer right after the output."
400 :group 'maxima
401 :type 'string)
403 (defcustom maxima-mode-minor-output "/*==>"
404 "*A string to insert in the buffer right before the output."
405 :group 'maxima
406 :type 'string)
408 (defcustom maxima-mode-minor-output-end " <==*/"
409 "*A string to insert in the buffer right after the output."
410 :group 'maxima
411 :type 'string)
413 (defcustom maxima-minor-mode-check-input t
414 "*Non-nil means check the input in Maxima minor mode before sending it."
415 :group 'maxima
416 :type 'boolean)
418 (defun maxima-minor-output-mark ()
419 (if (and
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 ()
426 (if (and
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))
490 (start 0)
491 matches str mb me)
492 (save-match-data
493 (while (and (< start l) (string-match regexp string start))
494 (setq mb (match-beginning 0)
495 me (match-end 0))
496 (when (= me mb) (setq me (min l (1+ mb))))
497 (string-match regexp (setq str (substring string mb me)))
498 (setq matches
499 (cons (replace-match (if (stringp rep)
501 (funcall rep (match-string 0 str)))
502 nil nil str)
503 (cons (substring string start mb) ; unmatched prefix
504 matches)))
505 (setq start me))
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))
513 (progn
514 (make-local-hook 'kill-buffer-hook)
515 (setq kill-buffer-hook nil))
516 (let ((hooks kill-buffer-hook))
517 (while hooks
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)
526 ".mac"))
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))
532 (out))
533 (save-excursion
534 (set-buffer tmpbuf)
535 (maxima-remove-kill-buffer-hooks)
536 (modify-syntax-entry ?/ ". 14")
537 (modify-syntax-entry ?* ". 23")
538 (insert string)
539 (goto-char (point-min))
540 (maxima-forward-over-comment-whitespace)
541 (setq out (buffer-substring-no-properties (point) (point-max))))
542 (kill-buffer tmpbuf)
543 out))
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))
549 (out))
550 (save-excursion
551 (set-buffer tmpbuf)
552 (maxima-remove-kill-buffer-hooks)
553 (modify-syntax-entry ?/ ". 14")
554 (modify-syntax-entry ?* ". 23")
555 (insert string)
556 (goto-char (point-max))
557 (maxima-back-over-comment-whitespace)
558 (setq out (buffer-substring-no-properties (point-min) (point))))
559 (kill-buffer tmpbuf)
560 out))
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."
568 (setq string
569 (maxima-strip-string-beginning (maxima-strip-string-end string)))
570 (unless (or
571 (string= 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 ";")))
577 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))
583 (str string)
584 (out)
585 (beg)
586 (end))
587 (save-excursion
588 (set-buffer tmpbuf)
589 (maxima-remove-kill-buffer-hooks)
590 (insert str)
591 (goto-char (point-min))
592 (skip-chars-forward " \t\n")
593 (setq beg (point))
594 (goto-char (point-max))
595 (skip-chars-backward " \t\n")
596 (setq end (point))
597 (setq out (buffer-substring-no-properties beg end)))
598 (kill-buffer tmpbuf)
599 out))
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))
605 (out)
606 (str string)
607 (beg)
608 (end))
609 (save-excursion
610 (set-buffer tmpbuf)
611 (maxima-remove-kill-buffer-hooks)
612 (insert str)
613 (goto-char (point-min))
614 (skip-chars-forward " \t\n")
615 (setq beg (point))
616 (setq out (buffer-substring-no-properties beg (point-max))))
617 (kill-buffer tmpbuf)
618 out))
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))
624 (out)
625 (str string)
626 (beg)
627 (end))
628 (save-excursion
629 (set-buffer tmpbuf)
630 (maxima-remove-kill-buffer-hooks)
631 (insert str)
632 (goto-char (point-max))
633 (skip-chars-backward " \t\n")
634 (setq end (point))
635 (setq out (buffer-substring-no-properties (point-min) end)))
636 (kill-buffer tmpbuf)
637 out))
639 ;;;; Functions that query position
640 (defun maxima-in-comment-p ()
641 "Non-nil means that the point is in a comment."
642 (let ((pt (point)))
643 (save-excursion
644 (and
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."
650 (let ((pt (point)))
651 (save-excursion
652 (and
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)
667 (let ((pt (point)))
668 (save-excursion
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)
684 (didnt-find nil)
685 (match nil)
686 (ppe)
687 (pt)
688 (origpt (point)))
689 (setq pt origpt)
690 (while (and keep-looking
691 (not didnt-find)
692 (re-search-forward regexp pmax t))
693 (setq match (match-string 0))
694 (setq ppe (parse-partial-sexp pt (point)))
695 (cond
696 ((nth 3 ppe) ;; In a string
697 (if (maxima-goto-end-of-string)
698 (setq pt (point))
699 (setq didnt-find t)))
700 ((nth 4 ppe) ;; In a comment
701 (if (maxima-goto-end-of-comment)
702 (setq pt (point))
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)
707 (progn
708 (goto-char origpt)
709 nil)
710 match)))
713 (defun maxima-noweb-re-search-forward (regexp &optional pmax)
714 (let ((match
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)
718 (setq match
719 (maxima-standard-re-search-forward regexp pmax)))
720 match))
722 (defun maxima-re-search-forward (regexp &optional pmax)
723 (cond
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)
734 (didnt-find nil)
735 (match nil)
736 (pt (point)))
737 (while (and keep-looking
738 (setq match (maxima-re-search-forward
739 (concat regexp "\\|[[(]") pmax)))
740 (cond
741 ((or
742 (string= match "[")
743 (string= match "("))
744 (unless
745 (maxima-goto-end-of-list)
746 (setq didnt-find t)
747 (setq keep-looking nil)))
749 (setq keep-looking nil))))
750 (if (or keep-looking didnt-find)
751 (progn
752 (goto-char pt)
753 nil)
754 match)))
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)
760 (match nil)
761 (ppe)
762 (origpt (point)))
763 (unless pmin
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)))
769 (cond
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))))
774 (if keep-looking
775 (progn
776 (goto-char origpt)
777 nil)
778 match)))
780 (defun maxima-noweb-re-search-backward (regexp &optional pmin)
781 (let ((match
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)
785 (setq match
786 (maxima-standard-re-search-backward regexp pmin)))
787 match))
789 (defun maxima-re-search-backward (regexp &optional pmin)
790 (cond
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)
801 (didnt-find nil)
802 (match nil)
803 (pt (point)))
804 (while (and keep-looking
805 (setq match (maxima-re-search-backward
806 (concat regexp "\\|[])]") pmin)))
807 (cond
808 ((or
809 (string= match ")")
810 (string= match "]"))
811 (unless
812 (maxima-goto-beginning-of-list)
813 (setq didnt-find t)
814 (setq keep-looking nil)))
816 (setq keep-looking nil))))
817 (if (or keep-looking didnt-find)
818 (progn
819 (goto-char pt)
820 nil)
821 match)))
823 (defun maxima-escaped-char-p ()
824 "Returns non-nil if the character after point is escaped"
825 (let ((pm (point-min))
826 (esc-chars 0))
827 (when (> (point) pm)
828 (save-excursion
829 (forward-char -1)
830 (while (and
831 (looking-at "\\\\")
832 (setq esc-chars (1+ esc-chars))
833 (> (point) pm))
834 (forward-char -1))))
835 (if (= (% esc-chars 2) 0)
837 t)))
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."
842 (interactive)
843 (let ((keep-looking t))
844 (while (and keep-looking (search-forward "\"" nil t))
845 (forward-char -2)
846 (unless (maxima-escaped-char-p)
847 (setq keep-looking nil))
848 (forward-char 2))
849 (if keep-looking
851 t)))
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."
856 (interactive)
857 (let ((keep-looking t))
858 (while (and keep-looking (search-backward "\"" nil t))
859 (forward-char -1)
860 (unless (maxima-escaped-char-p)
861 (setq keep-looking nil))
862 (forward-char 1))))
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."
867 (interactive)
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."
873 (interactive)
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."
879 (interactive)
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 "\\\\"))
889 ;; 0
890 ;; (save-excursion
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."
897 (looking-at "\\w")
898 (looking-at "\\\\")
899 (save-excursion
900 (forward-char -1)
901 (looking-at "\\\\"))))
903 (defun maxima-noweb-next-char-word-part-p ()
904 "Non-nil if next char is a a word part."
906 (looking-at "\\w")
907 (looking-at "\\\\")
908 (and
909 (looking-at ">")
910 (save-excursion
911 (forward-char -1)
912 (looking-at ">")))
913 (save-excursion
914 (forward-char -1)
915 (looking-at "\\\\"))))
917 (defun maxima-next-char-word-part-p ()
918 (cond
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."
926 (save-excursion
927 (forward-char -1)
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))
933 (while keep-going
934 (cond
935 ((looking-at "\\w")
936 (forward-word 1))
937 ((looking-at "\\\\")
938 (forward-char 2))
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))
947 (while keep-going
948 (cond
949 ((looking-at "\\w")
950 (forward-word 1))
951 ((looking-at "\\\\")
952 (forward-char 2))
953 ((looking-at "<<")
954 (forward-char 2)
955 (maxima-noweb-forward-out-of-ignore-bounds))
957 (setq keep-going nil))))))
959 (defun maxima-forward-word ()
960 (cond
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))
969 (while keep-going
970 (cond
971 ((and
972 (> (point) (point-min))
973 (save-excursion
974 (forward-char -1)
975 (looking-at "\\w")))
976 (backward-word 1))
977 ((and
978 (> (point) (1+ (point-min)))
979 (save-excursion
980 (forward-char -2)
981 (looking-at "\\\\")))
982 (forward-char -2))
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))
991 (while keep-going
992 (cond
993 ((and
994 (> (point) (point-min))
995 (save-excursion
996 (forward-char -1)
997 (looking-at "\\w")))
998 (backward-word 1))
999 ((and
1000 (> (point) (1+ (point-min)))
1001 (save-excursion
1002 (forward-char -2)
1003 (looking-at "\\\\")))
1004 (forward-char -2))
1005 ((and
1006 (> (point) (1+ (point-min)))
1007 (save-excursion
1008 (forward-char -2)
1009 (looking-at ">>")))
1010 (forward-char -2)
1011 (maxima-noweb-backward-out-of-ignore-bounds))
1013 (setq keep-going nil))))))
1015 (defun maxima-backward-word ()
1016 (cond
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))
1026 (save-excursion
1027 (beginning-of-line)
1028 (point))
1029 (line-beginning-position)))
1031 (defun maxima-line-end-position ()
1032 (if (not (fboundp 'line-end-position))
1033 (save-excursion
1034 (end-of-line)
1035 (point))
1036 (line-end-position)))
1038 (defun maxima-name-beginning ()
1039 (save-excursion
1040 (maxima-backward-word)
1041 (point)))
1043 (defun maxima-special-symbol-beginning ()
1044 (save-excursion
1045 (skip-chars-backward maxima-special-symbol-letters)
1046 (point)))
1048 (defun maxima-special-symbol-end ()
1049 (save-excursion
1050 (skip-chars-forward maxima-special-symbol-letters)
1051 (point)))
1053 (defun maxima-form-beginning-position ()
1054 (save-excursion
1055 (maxima-goto-beginning-of-form)
1056 (point)))
1058 (defun maxima-form-end-position ()
1059 (save-excursion
1060 (if (maxima-goto-end-of-form)
1061 (point)
1062 nil)))
1064 (defun maxima-form-end-position-or-point-max ()
1065 (let ((mfep (maxima-form-end-position)))
1066 (if mfep
1067 mfep
1068 (point-max))))
1070 (defun maxima-expression-end-position ()
1071 "Return the point where the current expression ends,
1072 or nil."
1073 (save-excursion
1074 (if (maxima-goto-end-of-expression)
1075 (point)
1076 nil)))
1078 (defun maxima-begin-if-position (pmin)
1079 "Find the point of the opening \"if\" for the current point."
1080 (let ((nest 0)
1081 (match)
1082 (keep-looking t)
1083 (pt (point)))
1084 (save-excursion
1085 (while (and keep-looking
1086 (setq match
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))))
1092 (when (= nest 1)
1093 (setq pt (point))
1094 (setq keep-looking nil))))
1095 (if keep-looking
1097 pt)))
1099 (defun maxima-begin-then-position (pmin)
1100 "Find the point of the opening \"then\" for the current \"else\"."
1101 (let ((keep-going t)
1102 (pt (point))
1103 (begin-then nil))
1104 (save-excursion
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
1112 ;; in the way.
1113 (let ((level 0)
1114 (match))
1115 (save-excursion
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))))))
1122 (when (= level 1)
1123 (setq begin-then (point))
1124 (setq keep-going nil))))))
1125 begin-then)))
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))
1133 (string=
1134 (buffer-substring-no-properties
1135 (point) (+ (point) (length mmo)))
1136 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))
1145 (string=
1146 (buffer-substring-no-properties
1147 (- (point) (length mme)) (point))
1148 mme))
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."
1154 (let ((pt (point))
1155 (keep-looking t))
1156 (while (and keep-looking
1157 (maxima-re-search-backward "[;$]" nil))
1158 (forward-char -1)
1159 (unless (looking-at "\\\\\\$")
1160 (forward-char 2)
1161 (setq keep-looking nil)))
1162 (if keep-looking
1163 (goto-char (point-min)))
1164 (maxima-forward-over-comment-whitespace)
1165 (if (< pt (point))
1166 (goto-char pt))
1167 (point)))
1169 (defun maxima-noweb-goto-beginning-of-form ()
1170 "Move to the beginning of the form."
1171 (if (re-search-backward "^<<.*?>>= *$" (point-min) 1)
1172 (forward-line 1))
1173 (maxima-forward-over-comment-whitespace))
1175 (defun maxima-goto-beginning-of-form ()
1176 (cond
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."
1184 (interactive)
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)
1190 (pt (point)))
1191 (while (and keep-looking
1192 (maxima-re-search-forward "[;$]" nil))
1193 (forward-char -2)
1194 (unless (looking-at "\\\\\\$")
1195 (setq keep-looking nil))
1196 (forward-char 2))
1197 (if (not keep-looking)
1198 (point)
1199 (goto-char pt)
1200 nil)))
1202 (defun maxima-noweb-goto-end-of-form ()
1203 "Move to the end of the form."
1204 (when (re-search-forward "\\(^@\\( \\|$\\)\\|^<<.*>>= *$\\)" nil 1)
1205 (forward-line -1)
1206 (end-of-line)
1207 (maxima-back-over-comment-whitespace)))
1209 (defun maxima-goto-end-of-form ()
1210 (cond
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."
1218 (interactive)
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."
1226 ; (interactive)
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 "(")
1232 (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)
1240 (pt (point)))
1241 (while (and keep-looking
1242 (maxima-re-search-backward-skip-blocks
1243 "\\<if\\>\\|\\<then\\>\\|\\<do\\>\\|\\<else\\>\\|(\\|\\[" pmin))
1244 (save-excursion
1245 (when (or (not (maxima-goto-end-of-expression)) (<= pt (point)))
1246 (setq keep-looking nil))))
1247 (if keep-looking
1248 (goto-char pmin))
1249 (point)))
1251 (defun maxima-goto-end-of-list ()
1252 "Go up a list.
1253 Return t if possible, nil otherwise."
1254 (interactive)
1256 (condition-case nil
1257 (up-list 1)
1258 (error t))
1262 (defun maxima-goto-end-of-list-interactive ()
1263 "Go up a list."
1264 (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."
1273 (condition-case nil
1274 (up-list -1)
1275 (error t))
1279 (defun maxima-goto-beginning-of-list-interactive ()
1280 "Go up a list."
1281 (interactive)
1282 (if (maxima-goto-beginning-of-list)
1284 (error "No list to begin.")))
1286 (defun maxima-forward-list ()
1287 "Go forward a list.
1288 Return t if possible, nil otherwise."
1290 (condition-case nil
1291 (forward-list 1)
1292 (error nil))
1294 nil))
1296 (defun maxima-backward-list ()
1297 "Go backward a list.
1298 Return t if possible, nil otherwise."
1300 (condition-case nil
1301 (backward-list 1)
1302 (error nil))
1304 nil))
1306 ;;; Newlines and indents
1307 (defun maxima-indent-form ()
1308 "Indent the entire form."
1309 (interactive)
1310 (indent-region
1311 (maxima-form-beginning-position)
1312 (maxima-form-end-position-or-point-max)
1313 nil))
1315 ;;; 'standard
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."
1322 (interactive)
1323 (let ((indent 0)
1324 (match)
1325 (pt))
1326 (save-excursion
1327 (when (= (forward-line -1) 0)
1328 (progn
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))))))))
1336 (save-excursion
1337 (beginning-of-line)
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"
1344 (interactive)
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)
1348 (ok t)
1349 (pt (point)))
1350 (save-excursion
1351 (while (and (> i 0) ok)
1352 (setq i (- i 1))
1353 (forward-char -1)
1354 (if (not (looking-at " "))
1355 (setq ok nil))
1356 (if (looking-at "\n")
1357 (setq ok nil))))
1358 (if ok
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."
1367 (and
1368 (looking-at "[ \t]*(")
1369 (save-excursion
1370 (maxima-back-over-comment-whitespace)
1371 (forward-char -1)
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."
1377 (save-excursion
1378 (maxima-back-over-comment-whitespace)
1379 (forward-char -1)
1380 (if (looking-at ")")
1381 (1+ (point))
1382 nil)))
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)))
1389 (while cpbop
1390 (goto-char cpbop)
1391 (maxima-backward-list)
1392 (setq cpbop
1393 (maxima-close-paren-before-open-paren)))))
1395 (defun maxima-word-on-previous-line ()
1396 "Go to the previous word part, return nil if "
1397 (let ((pt (point)))
1398 (save-excursion
1399 (skip-chars-backward " \t\n")
1400 ;; Same line?
1401 (and
1402 (maxima-previous-char-word-part-p)
1403 (string-match "\n"
1404 (buffer-substring-no-properties (point) pt))))))
1406 (defun maxima-string-on-previous-line ()
1407 "Go to the previous word part, return nil if "
1408 (let ((pt (point)))
1409 (save-excursion
1410 (skip-chars-backward " \t\n")
1411 ;; Same line?
1412 (forward-char -1)
1413 (and
1414 (looking-at "\"")
1415 (string-match "\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."
1422 (let ((pt (point))
1423 (endpt))
1424 (maxima-back-over-paren-groups)
1425 (setq endpt (point))
1426 ;; Let's see what's before this
1427 (cond
1428 ;; There is a word right before this
1429 ((maxima-previous-char-word-part-p)
1430 (maxima-backward-word)
1431 (- endpt (point)))
1432 ;; There is a string before this
1433 ((save-excursion
1434 (forward-char -1)
1435 (looking-at "\""))
1436 (maxima-goto-beginning-of-string)
1437 (- endpt (point)))
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)
1443 (- endpt (point)))
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)
1449 (- endpt (point)))
1450 ;; This point is the original point
1451 ((= endpt pt)
1452 nil)
1453 ;; Finally, the last parenthesized expression is the function
1455 (save-excursion
1456 (maxima-forward-list)
1457 (setq endpt (point)))
1458 (- endpt (point))))))
1460 ;;; 'perhaps-smart
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))
1465 (pt))
1466 (save-excursion
1467 (maxima-back-over-comment-whitespace)
1468 (setq pt (point))
1469 (condition-case nil
1470 (backward-sexp)
1471 (error t))
1472 (when (< (point) pt)
1473 (maxima-back-over-comment-whitespace)
1474 (if (< (point) (+ 5 pm))
1476 (forward-char -5)
1477 (if (looking-at ":lisp")
1478 (current-column)
1479 nil))))))
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."
1484 (let ((indent 0)
1485 (pmin)
1486 (tmpchar)
1487 (pt)
1488 (le)
1489 (comma-line)
1490 (len)
1491 (pps))
1492 (save-excursion
1493 (beginning-of-line)
1494 (setq pt (point))
1495 (setq pmin (maxima-form-beginning-position))
1496 (setq pps (parse-partial-sexp pmin (point)))
1497 (setq le (maxima-after-lisp-expression-p))
1498 (when (nth 1 pps)
1499 (setq pmin (nth 1 pps))
1500 (unless (looking-at "^[ \t]*,")
1501 (save-excursion
1502 (when (maxima-re-search-backward-skip-blocks "," pmin)
1503 (save-excursion
1504 (let ((lep (maxima-line-end-position)))
1505 (forward-char 1)
1506 (maxima-forward-over-comment-whitespace)
1507 (unless (>= (point) lep)
1508 (setq pmin (point)))))))))
1509 (cond
1510 ;; First, take care of the cases where the indentation is clear
1511 ;; No indentation at the beginning of the buffer
1512 ((= pt (point-min))
1513 (setq indent 0))
1514 ;; Don't change the indentation if in a string
1515 ((nth 3 pps)
1516 (setq indent -1))
1517 ;; If the line begins a comment and comments aren't reindented,
1518 ;; don't reindent it
1519 ((and
1520 (looking-at "[ \t]*/\\*[ \t]*$")
1521 maxima-dont-reindent-some-comments)
1522 (setq indent -1))
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
1527 ((= (point) pmin)
1528 (setq indent 0))
1529 ;; If the current point is in maxima minor output, don't reindent it
1530 ((maxima-in-output-p)
1531 (setq indent -1))
1532 ;; A line beginning "then" is indented like the opening "if"
1533 ((and
1534 (looking-at "[ \t]*\\<then\\>")
1535 (setq tmpchar (maxima-begin-if-position pmin)))
1536 (goto-char tmpchar)
1537 (setq indent (+ maxima-if-extra-indent-amount (current-column))))
1538 ;; A line beginning "else" is indented like the corresponding "then"
1539 ((and
1540 (looking-at "[ \t]*\\<else\\>")
1541 (setq tmpchar (maxima-begin-then-position pmin)))
1542 (goto-char tmpchar)
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
1546 ;; the function
1547 ((and
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
1555 (goto-char pmin)
1556 (if (looking-at "( *$")
1557 (progn
1558 (setq len (maxima-back-over-function-name))
1559 (setq indent (+
1560 (if len
1561 (min len maxima-function-indent-amount)
1563 (current-column))))
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
1568 (goto-char pmin)
1569 (setq indent (current-column)))
1570 ; ;; A line beginning with a comma is indented like the opening paren
1571 ; ((looking-at "[ \t]*,")
1572 ; (goto-char pmin)
1573 ; (setq indent (current-column)))
1574 ;; The point is at the end of a lisp expression
1576 (setq indent le))
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]*,"))
1582 (save-excursion
1583 (maxima-goto-beginning-of-construct pmin)
1584 (cond
1585 ;; The construct begins with a bracket
1586 ((looking-at "\\[")
1587 ; (if comma-line
1588 ; (setq indent (current-column))
1589 (setq indent (+ maxima-paren-indent-amount (current-column)))
1590 (forward-char 1)
1591 (skip-chars-forward " \t")
1592 (unless (looking-at "\n")
1593 (setq indent (current-column))));)
1594 ;; The construct begins with a paren
1595 ((looking-at "(")
1596 (cond
1597 ; (comma-line
1598 ; (setq indent (current-column)))
1599 ((save-excursion
1600 (let ((lep (maxima-line-end-position)))
1601 (forward-char 1)
1602 (maxima-forward-over-comment-whitespace)
1603 (>= (point) lep)))
1604 ;(looking-at "( *$")
1605 ;; Check to see if there is anything before the (
1606 (if (save-excursion
1607 (re-search-backward "\\(?:^[ \t]*\\)\\=" nil t))
1608 (setq tmpchar maxima-paren-indent-amount)
1609 (setq tmpchar 0))
1610 ;; If there is nothing after the (, there are two
1611 ;; cases
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)
1615 tmpchar
1616 (current-column)))
1617 ;; Otherwise,
1618 (setq indent (+ maxima-paren-indent-amount (current-column)))))
1619 ;; If there is something after the (, indent according to that
1621 (forward-char 1)
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.
1633 (if comma-line
1634 (let ((bol-pt (point))
1635 comma-pt
1636 diff)
1637 (skip-chars-forward " \t")
1638 (setq comma-pt (point))
1639 (forward-char 1)
1640 (skip-chars-forward " \t")
1641 (setq diff (- (point) comma-pt))
1642 (if (> diff indent)
1643 (let ((lineno
1644 (save-excursion
1645 (forward-line 0)
1646 (1+ (count-lines (point-min) (point))))))
1647 (message
1648 (format "Leading comma prevents proper indentation on line %d"
1649 lineno))
1650 (setq indent 0))
1651 (setq indent (- indent diff))))
1652 (maxima-back-over-comment-whitespace)
1653 (when (not (looking-at "^"))
1654 (forward-char -1)
1655 (if (not (or le (looking-at "[,;$]")))
1656 (setq indent (+ maxima-continuation-indent-amount indent))))))))
1657 indent))
1659 (defun maxima-noweb-perhaps-smart-calculate-indent ()
1660 (let ((indent nil)
1661 (pt))
1662 (save-excursion
1663 (beginning-of-line)
1664 (cond
1665 ((looking-at "^<<.*?>>=[ \t]*$")
1666 (setq indent -1))
1667 ((looking-at "^@[ \n]")
1668 (setq indent -1))
1670 (forward-line -1)
1671 (if (looking-at "^<<.*?>>=[ \t]*$")
1672 (setq indent -1)))))
1673 (if indent
1674 indent
1675 (maxima-standard-perhaps-smart-calculate-indent))))
1677 (defun maxima-perhaps-smart-calculate-indent ()
1678 (cond
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."
1687 (cond
1688 ;; If we're told it's in a comment, then it is.
1689 (incomment
1691 ;; Otherwise, if pmin is less than point, we're not in a comment
1692 ((> pt pmin)
1693 nil)
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."
1701 (let ((indent 0)
1702 (blankline nil)
1703 (endcomment nil))
1704 (cond
1705 ((looking-at "^[ \t]*$")
1706 (setq blankline 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.
1711 (save-excursion
1712 (maxima-goto-beginning-of-comment)
1713 (cond
1714 ;; Take care of the case when comments won't be indented
1715 ((and
1716 (looking-at "/\\*[ \t]*$")
1717 maxima-dont-reindent-some-comments)
1718 (if blankline
1719 (setq indent (+ (current-column)
1720 maxima-multiline-comment-indent-amount))
1721 (setq indent -1)))
1722 ;; Now, the other cases
1723 ;; If the current line ends a column, indent it like the opening line
1724 (endcomment
1725 (setq indent (current-column)))
1726 ;; If the opening line has a blank after the `/*'
1727 ((looking-at "/\\*[ \t\n]")
1728 (forward-char 2)
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)))))
1738 indent))
1740 (defun maxima-perhaps-smart-indent-line ()
1741 "Reindent the current line."
1742 (interactive)
1743 (let ((indent (maxima-perhaps-smart-calculate-indent)))
1744 (unless (= indent -1)
1745 (save-excursion
1746 (beginning-of-line)
1747 (delete-horizontal-space)
1748 (indent-to indent)))
1749 (skip-chars-forward " \t")))
1751 ;;;; Indentation according to style
1753 (defun maxima-indent-line ()
1754 (interactive)
1755 (cond
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): ")
1766 (cond
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 ()
1775 (interactive)
1776 (cond
1777 ((eq maxima-return-style 'newline)
1778 (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))))
1784 ;;;; Commenting
1786 (defun maxima-insert-short-comment ()
1787 "Prompt for a comment."
1788 (interactive)
1789 (let ((comment (read-string "Comment: ")))
1790 (insert "/* " comment " */")
1791 (newline-and-indent)))
1793 (defun maxima-insert-long-comment ()
1794 "Insert a comment environment"
1795 (interactive)
1796 (indent-for-tab-command)
1797 (insert "/*")
1798 (newline)
1799 (newline)
1800 (insert "*/")
1801 (indent-for-tab-command)
1802 (forward-line -1)
1803 (indent-for-tab-command))
1805 (defun maxima-uncomment-region (beg end)
1806 "`uncomment-region' to use with the menu."
1807 (interactive "r")
1808 (uncomment-region beg end (universal-argument)))
1810 ;;;; Help functions
1812 (defun maxima-goto-info-node (node)
1813 (if maxima-running-xemacs
1814 (info "Maxima")
1815 (info-other-window (concat "(Maxima)" node))))
1817 (defun maxima-get-info-on-subject (subject &optional same-window)
1818 (info "Maxima")
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"
1829 (let ((pt)
1830 (place))
1831 (save-excursion
1832 (beginning-of-line)
1833 (skip-chars-forward "* ")
1834 (setq pt (point))
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))
1841 ; (info "Maxima")
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)
1848 (interactive "P")
1849 (let* ((cw (current-word))
1850 (subj (if arg
1852 (completing-read (concat "Maxima help (" cw "): ")
1853 maxima-symbols nil nil nil nil cw))))
1854 (condition-case nil
1855 (maxima-get-info-on-subject subj)
1856 (error nil))))
1858 (defun maxima-help-at-point ()
1859 (interactive)
1860 (maxima-help t))
1862 (defun maxima-apropos (&optional arg)
1863 "Get help on a certain subject"
1864 (interactive "P")
1865 (let* ((cw (current-word))
1866 (expr (if arg
1868 (read-string (concat "Maxima help (" cw "): ")
1869 nil nil cw)))
1870 (maxima-help-buffer
1871 (get-buffer-create (concat "*Maxima Help*")))
1872 (have-info nil)
1873 expr-line
1874 (lmark))
1875 (set-buffer maxima-help-buffer)
1876 (maxima-remove-kill-buffer-hooks)
1877 (setq buffer-read-only nil)
1878 (erase-buffer)
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")
1883 (with-temp-buffer
1884 (require 'info)
1885 (Info-mode)
1886 (Info-goto-node "(Maxima)Function and Variable Index")
1887 (goto-char (point-min))
1888 (search-forward "Menu:")
1889 (forward-line 1)
1890 (beginning-of-line)
1891 (while (re-search-forward (concat "\\*.*" expr ".*:") nil t)
1892 (setq have-info t)
1893 (setq expr-line (buffer-substring-no-properties
1894 (maxima-line-beginning-position)
1895 (maxima-line-end-position)))
1896 (save-excursion
1897 (set-buffer maxima-help-buffer)
1898 (insert expr-line "\n"))))
1899 (if have-info
1900 (progn
1901 (set-buffer maxima-help-buffer)
1902 (defun maxima-help-subject ()
1903 (interactive)
1904 (maxima-get-help))
1905 (defun maxima-kill-help ()
1906 (interactive)
1907 (let ((buf (current-buffer)))
1908 (delete-window)
1909 (maxima-remove-kill-buffer-hooks)
1910 (kill-buffer buf)))
1911 (defun maxima-next-subject ()
1912 (interactive)
1913 (forward-char 1)
1914 (if (re-search-forward "^\\*" nil t)
1916 (goto-char (point-min))
1917 (re-search-forward "^\\*" nil t))
1918 (forward-char -1))
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 "^\\*")
1925 (forward-char -1)
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 ()
1932 (interactive)
1933 (maxima-apropos t))
1935 (defun maxima-apropos-help ()
1936 (interactive)
1937 (maxima-help-dispatcher nil nil))
1939 (defun maxima-completion-help ()
1940 (interactive)
1941 (maxima-help-dispatcher nil t))
1943 (defun maxima-help-dispatcher (&optional arg1 arg2)
1944 (interactive)
1945 (cond
1946 ((or (looking-at "[a-zA-Z_]")
1947 (looking-at "?[a-zA-Z]")
1948 (looking-at "%[a-zA-Z]"))
1949 (if arg2
1950 (maxima-context-help)
1951 (maxima-help (current-word))))
1952 ((looking-at "?")
1953 (maxima-get-info-on-subject "\"\\?\""))
1954 ((looking-at "#")
1955 (maxima-get-info-on-subject "\"#\""))
1956 ((looking-at "\\.")
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))))
1961 (cond
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)))))
1971 (arg1
1972 (error "No help for %s" (char-to-string (char-after (point)))))
1973 (t ; point is behind a name?
1974 (save-excursion
1975 (progn
1976 (backward-char 1)
1977 (maxima-help-dispatcher t arg2))))))
1979 (defun maxima-context-help ()
1980 (interactive)
1981 (let* ((stub (current-word))
1982 (completions (all-completions (downcase stub) maxima-symbols)))
1983 (setq completions
1984 (mapcar
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*")))
1999 expr-line
2000 (lmark))
2001 (set-buffer maxima-help-buffer)
2002 (erase-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)))
2012 (save-excursion
2013 (set-buffer maxima-help-buffer)
2014 (insert expr-line "\n")))
2015 (with-temp-buffer
2016 (require 'info nil t)
2017 (Info-mode)
2018 (Info-goto-node "(Maxima)Function and Variable Index")
2019 (goto-char (point-min))
2020 (search-forward "Menu:")
2021 (forward-line 1)
2022 (beginning-of-line)
2023 (mapcar (function maxima-help-insert-line) completions))
2024 (goto-char (point-min))
2025 (defun maxima-help-subject ()
2026 (interactive)
2027 (maxima-get-help))
2028 (defun maxima-kill-help ()
2029 (interactive)
2030 (let ((buf (current-buffer)))
2031 (delete-window)
2032 (maxima-remove-kill-buffer-hooks)
2033 (kill-buffer buf)))
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)
2037 (goto-char 1)
2038 (pop-to-buffer maxima-help-buffer)))
2040 (defun maxima-info ()
2041 "Read the info file for Maxima."
2042 (interactive)
2043 (if maxima-running-xemacs
2044 (info "Maxima")
2045 (info-other-window "Maxima")))
2047 ;;; The help map
2049 (defvar maxima-help-map nil)
2050 (if maxima-help-map
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)))
2066 ;;;; Completion
2068 ;;; This next functions are from hippie-expand.el
2069 (defun maxima-he-capitalize-first (str)
2070 (save-match-data
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)))
2075 res)
2076 str)))
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)
2087 (length to-str))))
2088 to-str)
2089 ((not (maxima-he-ordinary-case-p to-str))
2090 to-str)
2091 ((string= from-str (downcase from-str))
2092 (downcase to-str))
2093 ((string= from-str (upcase from-str))
2094 (upcase to-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))
2100 to-str)))
2102 ;;; The next functions are from comint.el in cvs emacs
2103 (if (and
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
2124 ;; displayed set.
2125 (equal completions
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)
2136 (scroll-up))))
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.
2145 (let (key first)
2146 (if (with-current-buffer (get-buffer "*Completions*")
2147 (set (make-local-variable 'comint-displayed-dynamic-completions)
2148 completions)
2149 (setq key (read-key-sequence nil)
2150 first (aref key 0))
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.
2157 (progn
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."
2182 (interactive)
2183 (let* ((pmin (maxima-form-beginning-position))
2184 (pps (parse-partial-sexp pmin (point))))
2185 (cond
2186 ;; complete filename if the point is in a string
2187 ((nth 3 pps)
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)
2197 ;; (interactive)
2198 ;; (if (not old)
2199 ;; ;;; let beg be the beginning of the word
2200 ;; (progn
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)))
2207 ;; he-expand-list))
2208 ;; (if he-expand-list
2209 ;; (he-substitute-string (car he-expand-list))
2210 ;; nil))
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)
2215 ;; nil)))
2217 ;; (fset 'maxima-dynamic-complete
2218 ;; (make-hippie-expand-function '(maxima-he-try)))
2220 ;;;; Miscellaneous
2222 (defun maxima-mark-form ()
2223 "Make the current form as the region."
2224 (interactive)
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))
2231 (save-excursion
2232 (goto-char beg)
2233 (maxima-forward-over-comment-whitespace)
2234 (if (looking-at ",")
2235 (setq commapt (point))
2236 (goto-char end)
2237 (maxima-back-over-comment-whitespace)
2238 (when (save-excursion
2239 (forward-char -1)
2240 (looking-at "[;$]"))
2241 (forward-char -1)
2242 (maxima-back-over-comment-whitespace))
2243 (forward-char -1)
2244 (if (looking-at ",")
2245 (setq commapt (point)))))
2246 (if commapt
2247 (progn
2248 (message "Misplaced comma")
2249 (goto-char commapt)
2250 nil)
2251 t)))
2253 (defun maxima-check-parens (beg end)
2254 "Check to make sure that the parentheses are balanced in the region."
2255 (interactive "r")
2256 (let* ((tmpfile (maxima-make-temp-name))
2257 (tmpbuf (get-buffer-create tmpfile))
2258 (string (buffer-substring-no-properties beg end))
2259 (keep-going t)
2260 (match)
2261 (pt)
2262 (errmessage nil)
2263 (parenstack nil))
2264 (save-excursion
2265 (set-buffer tmpbuf)
2266 (maxima-mode)
2267 (maxima-remove-kill-buffer-hooks)
2268 (modify-syntax-entry ?/ ". 14")
2269 (modify-syntax-entry ?* ". 23")
2270 (insert string)
2271 (goto-char (point-min))
2272 (while (and (not errmessage)
2273 (setq match (maxima-re-search-forward "[][()]" end)))
2274 (unless (save-excursion
2275 (forward-char -1)
2276 (maxima-escaped-char-p))
2277 (cond
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 ")")
2283 (cond
2284 ((not parenstack)
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 "]")
2293 (cond
2294 ((not parenstack)
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)
2303 (cond
2304 ((not (or parenstack errmessage))
2305 ; (message "Parenthesis and brackets match")
2307 (errmessage
2308 (message errmessage)
2309 (goto-char (1- (+ beg pt)))
2310 nil)
2312 (cond
2313 ((= (caar parenstack) 1)
2314 (message "Unmatched open parenthesis")
2315 (goto-char (1- (+ beg (cdar parenstack))))
2316 nil)
2318 (message "Unmatched open bracket")
2319 (goto-char (+ beg (cdar parenstack)))
2320 nil))))))
2323 (defun maxima-check-form-parens ()
2324 "Check to see if the parentheses in the current form are balanced."
2325 (interactive)
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."
2336 (interactive)
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
2345 (progn
2346 (setq maxima-mode-highlight
2347 (make-extent
2348 maxima-mode-region-begin
2349 maxima-mode-region-end))
2350 (set-extent-property maxima-mode-highlight 'face 'highlight))
2351 (setq maxima-mode-highlight
2352 (make-overlay
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 ()
2367 (if (or
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)))
2384 ;;;; Syntax table
2386 (defvar maxima-mode-syntax-table nil "")
2388 (if (not maxima-mode-syntax-table)
2389 (let ((i 0))
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)
2395 ;; (while (< i ?0)
2396 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2397 ;; (setq i (1+ i)))
2398 ;; (setq i (1+ ?9))
2399 ;; (while (< i ?A)
2400 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2401 ;; (setq i (1+ i)))
2402 ;; (setq i (1+ ?Z))
2403 ;; (while (< i ?a)
2404 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2405 ;; (setq i (1+ i)))
2406 ;; (setq i (1+ ?z))
2407 ;; (while (< i 128)
2408 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2409 ;; (setq i (1+ i)))
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)))
2435 ;;;; Keymap
2437 (defvar maxima-mode-map nil
2438 "The keymap for maxima-mode")
2440 (if maxima-mode-map
2442 (let ((map (make-sparse-keymap)))
2443 ;; Motion
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)
2448 ;; Process
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)
2464 ;; Completion
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)
2468 ;; Commenting
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)
2473 ;; Indentation
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)
2478 ;; Help
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)
2482 ;; Minibuffer
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)
2486 ;; Misc
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)))
2494 ;;;; Menu
2496 (easy-menu-define maxima-mode-menu maxima-mode-map "Maxima mode menu"
2497 '("Maxima"
2498 ("Motion"
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])
2503 ("Process"
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]
2510 "----"
2511 ["Display buffer" maxima-display-buffer t]
2512 "----"
2513 ["Kill process" maxima-stop t])
2514 ("Indentation"
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))])
2521 ("Misc"
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])
2527 ("Help"
2528 ["Maxima info" maxima-info t]
2529 ["Help" maxima-help t])))
2532 ;;;; Variable setup
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))))
2566 ;;;; Maxima mode
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]
2605 an argument.
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:
2619 \\[maxima-help].
2620 To read the Maxima info manual, use:
2621 \\[maxima-info].
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].
2627 \\{maxima-mode-map}
2629 (interactive)
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)
2635 (cond
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
2651 "Maxima Noweb 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 ()
2663 (let (pt pt1)
2664 (save-excursion
2665 (if (re-search-forward
2666 (concat "\\(^(\\(" maxima-outchar "\\|" maxima-linechar "\\)[0-9]*) \\)")
2667 nil 1)
2668 (goto-char (match-beginning 0)))
2669 (skip-chars-backward " \t\n")
2670 (setq pt (point)))
2671 (save-excursion
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"
2678 (unless query
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))
2687 (pos))
2688 (set-buffer (process-buffer inferior-maxima-process))
2689 (setq pos comint-last-output-start)
2690 (if (marker-position pos)
2691 (save-excursion
2692 (goto-char pos)
2693 (beginning-of-line)
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))
2701 (pos))
2702 (save-excursion
2703 (set-buffer (process-buffer inferior-maxima-process))
2704 (goto-char inferior-maxima-input-end)
2705 (forward-line 1)
2706 (if (looking-at (concat "(" maxima-inchar "[0-9]+)"))
2707 (kill-line 1))
2708 (if (looking-at " ")
2709 (delete-char 1)))))
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."
2719 (interactive "r")
2720 (save-excursion
2721 (save-restriction
2722 (narrow-to-region (point-min) end)
2723 (goto-char start)
2724 (while (search-forward "\t" nil t) ; faster than re-search
2725 (forward-char -1)
2726 (let ((tab-beg (point))
2727 (indent-tabs-mode nil)
2728 column)
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."
2736 (let ((beg))
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)))
2741 (untabify beg
2742 (process-mark inferior-maxima-process))))
2744 (defun inferior-maxima-wait-for-output ()
2745 "Wait for output from the Maxima process."
2746 (when (and
2747 inferior-maxima-waiting-for-output
2748 (inferior-maxima-running))
2749 (accept-process-output inferior-maxima-process))
2750 (if maxima-running-xemacs
2751 (sleep-for 0.1)
2752 (sit-for 0 inferior-maxima-after-output-wait)))
2754 (defun inferior-maxima-output-filter (str)
2755 "Look for a new input prompt"
2756 (cond ((and
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."
2774 (interactive)
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*")
2779 (save-excursion
2780 (set-buffer "*maxima*")
2781 (erase-buffer)))
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)
2786 (let ((mbuf)
2787 (cmd))
2788 (setq mbuf (apply #'make-comint "maxima" maxima-command nil maxima-args))
2789 (save-excursion
2790 (set-buffer mbuf)
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."
2806 (interactive "P")
2807 (if (processp inferior-maxima-process)
2808 (if arg
2809 (progn
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? ")
2814 (progn
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))
2824 (maxima-start)
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)))
2830 (insert string)
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))
2846 (insert ans)
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))
2856 (pt)
2857 (command))
2858 (save-excursion
2859 (set-buffer tmpbuf)
2860 (maxima-remove-kill-buffer-hooks)
2861 (if arg
2862 (insert maxima-block-wait)
2863 (insert maxima-block))
2864 (goto-char (point-min))
2865 (maxima-forward-over-comment-whitespace)
2866 (setq pt (point))
2867 (if (string-match "[$;]\\|:lisp"
2868 (buffer-substring-no-properties (point) (point-max)))
2869 (progn
2870 (if (looking-at ":lisp")
2871 (progn
2872 (search-forward ":lisp")
2873 (forward-sexp)
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)
2878 (if arg
2879 (setq maxima-block-wait
2880 (maxima-strip-string-add-semicolon
2881 (buffer-substring-no-properties (point) (point-max))))
2882 (setq maxima-block
2883 (maxima-strip-string-add-semicolon
2884 (buffer-substring-no-properties (point) (point-max)))))
2885 (setq command (buffer-substring-no-properties pt (point))))
2886 (if arg
2887 (setq maxima-block-wait "")
2888 (setq maxima-block "")))
2889 (if arg
2890 (if (string= maxima-block-wait ";") (setq maxima-block-wait ""))
2891 (if (string= maxima-block ";") (setq maxima-block "")))
2892 (kill-buffer tmpbuf))
2893 command))
2895 (defun maxima-send-block (stuff)
2896 "Send a block of code to Maxima."
2897 (maxima-start)
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 "")
2902 (progn
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."
2910 (maxima-start)
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."
2919 (interactive)
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."
2927 (interactive)
2928 (inferior-maxima-wait-for-output)
2929 (save-excursion
2930 (set-buffer (process-buffer inferior-maxima-process))
2931 (let* ((pt (point))
2932 (pmark (progn (goto-char (process-mark inferior-maxima-process))
2933 (forward-line 0)
2934 (point-marker)))
2935 (beg (progn
2936 (goto-char inferior-maxima-input-end)
2937 (forward-line 1)
2938 (point)))
2939 (output (buffer-substring-no-properties beg pmark)))
2940 (goto-char pt)
2941 output)))
2943 (defun maxima-last-output-noprompt ()
2944 "Return the last Maxima output, without the prompts"
2945 (interactive)
2946 (if (not (inferior-maxima-running))
2947 (maxima-last-output)
2948 (let* ((output (maxima-last-output))
2949 (newstring)
2950 (i 0)
2951 (beg)
2952 (end)
2953 (k))
2954 ;; Replace the output prompt with spaces
2955 (setq beg (string-match
2956 (concat "\\(^(" maxima-outchar "[0-9]*) \\)") output))
2957 (if (not beg)
2958 output
2959 (setq end (1+ (string-match ")" output beg)))
2960 (setq newstring (substring output 0 beg))
2961 (setq k (- end beg))
2962 (while (< i k)
2963 (setq newstring (concat newstring " "))
2964 (setq i (1+ i)))
2965 (concat newstring
2966 (substring output
2967 end))))))
2969 (defun maxima-last-output-tex-noprompt ()
2970 "Return the last Maxima output, between the dollar signs."
2971 (interactive)
2972 (let* ((output (maxima-last-output))
2973 (begtex (string-match "\\$\\$" output))
2974 (endtex (string-match "\\$\\$" output (1+ begtex))))
2975 (concat
2976 (substring output begtex (+ endtex 2))
2977 "\n")))
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)
2998 (maxima-string
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")
3007 (if arg
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."
3016 (interactive "P")
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."
3022 (interactive "P")
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."
3030 (interactive "P")
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."
3037 (interactive "P")
3038 (save-excursion
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."
3046 (interactive "P")
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."
3053 (interactive)
3054 (let ((beg (point)) (end (point)))
3055 (save-excursion
3056 (goto-char beg)
3057 (beginning-of-line)
3058 (setq beg (point))
3059 (maxima-goto-beginning-of-form)
3060 (while (< (point) beg)
3061 (progn
3062 (beginning-of-line)
3063 (setq beg (point))
3064 (maxima-goto-beginning-of-form)))
3065 (goto-char end)
3066 (end-of-line)
3067 (setq end (point))
3068 (while (and (< (maxima-form-beginning-position) end) (< end (point-max)))
3069 (progn
3070 (forward-line 1)
3071 (end-of-line)
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"))
3077 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."
3081 (interactive)
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")
3088 (let ((beg1)
3089 (end1))
3090 (save-excursion
3091 (goto-char beg)
3092 (setq beg1 (maxima-form-beginning-position))
3093 (goto-char end)
3094 (setq end1 (maxima-form-end-position-or-point-max))
3095 (maxima-send-region beg1 end1)
3096 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."
3106 (interactive)
3107 (let ((origbuffer (current-buffer)))
3108 (if (not (processp inferior-maxima-process))
3109 (maxima-start))
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."
3125 (interactive)
3126 (let* ((inputs nil)
3127 (comint-inputs (cddr comint-input-ring))
3128 (i 0))
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)))
3133 (setq i (1+ i)))
3134 (reverse inputs)))
3136 (defun inferior-maxima-input-complete ()
3137 "Complete line from list of previous input."
3138 (interactive)
3139 (let* ((stub (buffer-substring-no-properties
3140 (inferior-maxima-bol-position) (point)))
3141 (completions (all-completions (downcase stub)
3142 (inferior-maxima-previous-inputs))))
3143 (setq completions
3144 (mapcar
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."
3159 (interactive)
3160 (let* ((pmin (save-excursion
3161 (re-search-backward inferior-maxima-prompt)
3162 (point)))
3163 (pps (parse-partial-sexp pmin (point))))
3164 (cond
3165 ;; complete filename if the point is in a string
3166 ((nth 3 pps)
3167 (maxima-complete-filename))
3168 ;; Otherwise, complete the symbol
3170 (maxima-complete-symbol)))))
3172 ;; (defun maxima-input-he-try (old)
3173 ;; (interactive)
3174 ;; (if (not old)
3175 ;; ;;; let beg be the beginning of the word
3176 ;; (progn
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)))
3183 ;; he-expand-list))
3184 ;; (if he-expand-list
3185 ;; (he-substitute-string (car he-expand-list))
3186 ;; nil))
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)
3191 ;; nil)))
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."
3200 (interactive)
3201 (let ((ok nil)
3202 (pt (point))
3203 pt1)
3204 (save-excursion
3205 (end-of-line)
3206 (skip-chars-backward " \t")
3207 (forward-char -1)
3208 (when (looking-at "[$;]")
3209 (setq pt (point))
3210 (setq ok t)))
3211 (if ok
3212 (progn
3213 (save-excursion
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."
3222 (interactive)
3223 (inferior-maxima-comint-send-input))
3225 (defun inferior-maxima-bol ()
3226 "Go to the beginning of the line, but past the prompt."
3227 (interactive)
3228 (let ((eol (save-excursion (end-of-line) (point))))
3229 (forward-line 0)
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 ()
3235 (save-excursion
3236 (inferior-maxima-bol)
3237 (point)))
3240 ;;;; Inferior Maxima mode
3242 (define-derived-mode inferior-maxima-mode
3243 comint-mode
3244 "Inferior Maxima"
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
3249 parentheses.
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
3259 argument.
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)
3275 (setq tab-width 8)
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
3280 (function
3281 (lambda ()
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
3288 (function
3289 (lambda ()
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
3297 (progn
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))
3305 ;;;; Keymap
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)
3320 ;;;; Menu
3322 (easy-menu-define inferior-maxima-mode-menu inferior-maxima-mode-map
3323 "Maxima mode menu"
3324 '("Maxima"
3325 ("Help"
3326 ["Maxima info" maxima-info t]
3327 ["Help" maxima-help t])
3328 ("Quit"
3329 ["Kill process" maxima-stop t])))
3331 ;;;; Running Maxima
3333 (defun maxima ()
3334 "Run Maxima interactively inside a buffer."
3335 (interactive)
3336 (maxima-start)
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"
3343 (interactive)
3344 (maxima-start)
3345 (let ((input (read-string "Maxima: " nil maxima-minibuffer-history))
3346 (output nil)
3347 (twod (and maxima-minibuffer-2d (not maxima-running-xemacs))))
3348 (setq input (maxima-strip-string-add-semicolon input))
3349 (if twod
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,%);")
3357 (if (not twod)
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))
3365 (message output)))
3367 (defun maxima-minibuffer-delete-output (beg end)
3368 (let ((mmom (maxima-minor-output-mark))
3369 (mmoe (maxima-minor-output-mark-end)))
3370 (if (or
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).")
3376 (let (pt)
3377 (save-excursion
3378 (goto-char beg)
3379 (if (search-forward mmom end t)
3380 (progn
3381 (setq pt (match-beginning 0))
3382 (search-forward mmoe)
3383 (kill-region pt (point)))
3384 (goto-char end)
3385 (if (looking-at (concat "[ \n]*" (regexp-quote mmom)))
3386 (progn
3387 (search-forward mmoe)
3388 (kill-region end (point)))))
3389 (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
3395 will be deleted."
3396 (interactive "r\nP")
3397 (let ((output nil)
3398 (minibufferoutput)
3399 (input)
3400 (realend nil)
3401 (realbeg)
3402 (outputbeg)
3403 (delreg)
3404 (delregbeg)
3405 (delregend)
3406 (twod (and maxima-minibuffer-2d (not maxima-running-xemacs))))
3407 (save-excursion
3408 (goto-char beg)
3409 (maxima-forward-over-comment-whitespace)
3410 (setq realbeg (point))
3411 (if (re-search-forward (maxima-minor-output-mark) end t)
3412 (setq realend
3413 (if (eq major-mode 'maxima-mode)
3414 (- (point) (length maxima-mode-minor-output))
3415 (- (point) (length maxima-minor-output))))
3416 (goto-char end)
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)))
3421 (if arg
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)
3426 (and
3427 (maxima-check-parens realbeg realend)
3428 (maxima-check-commas realbeg realend)))
3429 (maxima-start)
3430 (if twod
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,%);")
3438 (if (not twod)
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)))))
3445 (unless arg
3446 (setq minibufferoutput (maxima-replace-in-string "%" "%%" output))
3447 (message minibufferoutput))
3448 (if (and arg
3449 (not twod))
3450 (save-excursion
3451 (goto-char realend)
3452 (if (looking-at "^")
3453 (setq realend (1- realend)))
3454 ;(delete-region realend end)
3455 (goto-char realend)
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)
3460 (cond
3461 ((< (length delreg) 15)
3462 (setq delreg (maxima-replace-in-string "\n" " " delreg))
3463 (message (concat "\"" delreg "\" killed")))
3465 (setq delregbeg
3466 (maxima-replace-in-string "\n" " "(substring delreg 0 5)))
3467 (setq delregend
3468 (maxima-replace-in-string "\n" " "(substring delreg -5)))
3469 (message (concat "\"" delregbeg " ... " delregend "\" killed")))))
3470 (let ((ind (save-excursion
3471 (goto-char realbeg)
3472 (current-column)))
3473 (here (point))
3474 (there (make-marker)))
3475 (if (or
3476 (string-match "\n" output)
3477 (> (+ (current-column) (length output)) fill-column))
3478 (progn
3479 (insert "\n")
3480 (setq here (point)))
3481 (insert " "))
3482 (insert (maxima-minor-output-mark) " " output
3483 (maxima-minor-output-mark-end))
3484 (set-marker there (point))
3485 (goto-char here)
3486 (goto-char (line-end-position))
3487 ; (fill-region (line-beginning-position) (point))
3488 (if (string-match
3489 "\n"
3490 (buffer-substring-no-properties here (point)))
3491 (forward-line -1)
3492 (forward-line 1))
3493 (indent-region (point) there ind)))
3494 (if (and arg twod)
3495 (let ((ind (save-excursion
3496 (goto-char realbeg)
3497 (current-column)))
3498 (here))
3499 (save-excursion
3500 (goto-char realend)
3501 (insert (maxima-minor-output-mark) "\n")
3502 (setq here (point))
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."
3511 (interactive "P")
3512 (maxima-minibuffer-on-region
3513 (maxima-line-beginning-position)
3514 (maxima-line-end-position)
3515 arg))
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."
3521 (interactive "P")
3522 (let ((beg (maxima-form-beginning-position))
3523 (end (maxima-form-end-position)))
3524 (save-excursion
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."
3536 (interactive "P")
3537 (let ((beg)
3538 (end)
3539 (pt (point)))
3540 (save-excursion
3541 (if (re-search-backward maxima-minor-prefix nil t)
3542 (setq beg (match-end 0))
3543 (error "No beginning to determined region"))
3544 (goto-char pt)
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 ()
3550 (interactive)
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 ()
3558 (interactive)
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));")
3562 (insert output)))
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
3592 are in effect:
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
3604 ; or $)
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
3616 " maxima"
3617 nil)
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
3626 (progn
3627 (setq maxima-minor-mode-highlight
3628 (make-extent
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
3633 (make-overlay
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 ()
3648 (if (or
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)
3666 (provide 'maxima)
3667 ;;; maxima.el ends here