Windows installer: update Gnuplot
[maxima.git] / interfaces / emacs / emaxima / maxima.el
blob42837ba757e13c2838a266e93d29321ffaa8e50d
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., 59 Temple Place, Suite 330, Boston,
24 ;; MA 02111-1307 USA
26 ;; You will need both maxima.el and maxima-font-lock.el
28 ;;; Commentary:
30 ;; This is a branch of William Schelter's maxima-mode.el
32 ;; Quick intro
34 ;; To install, put this file (as well as maxima-font-lock.el)
35 ;; somewhere in your emacs load path.
36 ;; To make sure that `maxima.el' is loaded when necessary, whether to
37 ;; edit a file in maxima mode or interact with Maxima in an Emacs buffer,
38 ;; put the lines
39 ;; (autoload 'maxima-mode "maxima" "Maxima mode" t)
40 ;; (autoload 'maxima "maxima" "Maxima interaction" t)
41 ;; in your `.emacs' file. If you want any file ending in `.mac' to begin
42 ;; in `maxima-mode', for example, put the line
43 ;; (setq auto-mode-alist (cons '("\\.mac" . maxima-mode) auto-mode-alist))
44 ;; to your `.emacs' file.
46 ;; In any of the Maxima modes, to get help on a prompted for Maxima topic,
47 ;; use
48 ;; C-c C-d h
49 ;; or
50 ;; f12.
51 ;; To get help with the symbol under point, use ("d" for describe):
52 ;; C-c C-d d
53 ;; or
54 ;; C-c C-d C-d
55 ;; For apropos help, use:
56 ;; C-c C-d a
57 ;; or
58 ;; C-c C-d C-a
59 ;; To get apropos with the symbol under point, use:
60 ;; C-c C-d p
61 ;; C-c C-d C-p
62 ;; or M-f12.
63 ;; To read the Maxima info manual, use:
64 ;; C-c C-d m
65 ;; C-c C-d C-m
66 ;; C-c C-d i
67 ;; or
68 ;; C-c C-d C-i
69 ;; (For Maxima minor mode, replace C-cC-d by C-c=d.)
71 ;; ** Maxima mode **
72 ;; To put the current buffer into maxima-mode, type M-x maxima-mode
74 ;; Maxima mode provides the following motion commands:
75 ;; M-C-a: Move to the beginning of the form.
76 ;; M-C-e: Move to the end of the form.
77 ;; M-C-b: Move to the beginning of the list.
78 ;; M-C-f: Move to the end of the list.
80 ;; and the following miscellaneous commands.
81 ;; M-h: Mark the current form
82 ;; C-c): Check the current region for balanced parentheses.
83 ;; C-cC-): Check the current form for balanced parentheses.
85 ;; Maxima mode has the following completions command:
86 ;; M-TAB: Complete the Maxima symbol as much as possible, providing
87 ;; a completion buffer if there is more than one possible completion.
89 ;; Portions of the buffer can be sent to a Maxima process. (If a process is
90 ;; not running, one will be started.)
91 ;; C-cC-r: Send the region to Maxima.
92 ;; C-cC-b: Send the buffer to Maxima.
93 ;; C-cC-c: Send the line to Maxima.
94 ;; C-cC-e: Send the form to Maxima.
95 ;; C-RET: Send the smallest set of lines which contains
96 ;; the cursor and contains no incomplete forms, and go to the next form.
97 ;; M-RET: As above, but with the region instead of the current line.
98 ;; C-cC-l: Prompt for a file name to load into Maxima.
100 ;; When something is sent to Maxima, a buffer running an inferior Maxima
101 ;; process will appear. It can also be made to appear by using the command
102 ;; C-c C-p.
103 ;; When a command is given to send information to Maxima, the region
104 ;; (buffer, line, form) is first checked to make sure the parentheses
105 ;; are balanced. With an argument, they will not be checked first.
106 ;; The Maxima process can be killed, after asking for confirmation
107 ;; with C-cC-k. To kill without confirmation, give C-cC-k
108 ;; an argument.
110 ;; By default, indentation will be to the same level as the
111 ;; previous line, with an additional space added for open parentheses.
112 ;; The behaviour of indent can be changed by the command
113 ;; M-x maxima-change-indent-style. The possibilities are:
114 ;; Standard: Simply indent
115 ;; Perhaps smart: Tries to guess an appropriate indentation, based on
116 ;; open parentheses, "do" loops, etc.
117 ;; The default can be set by setting the value of the variable
118 ;; "maxima-indent-style" to either 'standard or 'perhaps-smart.
119 ;; In both cases, M-x maxima-untab will remove a level of indentation.
121 ;; ** Maxima noweb mode **
122 ;; maxima-noweb-mode is a modification of maxima-mode that will work
123 ;; nicely with noweb-mode; namely, it will limit any relevant searches
124 ;; to the current chunk and treat <<...>> as word parts.
126 ;; ** Running Maxima interactively **
127 ;; To run Maxima interactively in a buffer, type M-x maxima
128 ;; In the Maxima process buffer,
129 ;; return will check the line for balanced parentheses, and send line as input.
130 ;; Control return will send the line as input without checking for balanced
131 ;; parentheses.
133 ;; <M-tab> will complete the Maxima symbol as much as possible, providing
134 ;; a completion buffer if there is more than one possible completion.
136 ;; <C-M-tab> will complete the input line, based on previous input lines.
137 ;; C-c C-d will get help on a Maxima topic.
138 ;; C-c C-m will bring up the Maxima info manual.
139 ;; C-cC-k will kill the process and the buffer, after asking for
140 ;; confirmation. To kill without confirmation, give C-M-k an
141 ;; argument.
143 ;; To scroll through previous commands,
144 ;; M-p will bring the previous input to the current prompt,
145 ;; M-n will bring the next input to the prompt.
146 ;; M-r will bring the previous input matching
147 ;; a regular expression to the prompt,
148 ;; M-s will bring the next input matching
149 ;; a regular expression to the prompt.
151 ;; ** Running Maxima from the minibuffer **
152 ;; The command M-x maxima-minibuffer
153 ;; will allow you to interact with Maxima from the minibuffer.
154 ;; The arrows will allow you to scroll through previous inputs.
155 ;; The line
156 ;; (autoload 'maxima-minibuffer "maxima" "Maxima in a minibuffer" t)
157 ;; in your .emacs will make sure the function is available.
158 ;; If the variable maxima-minibuffer-2d is non-nil, then the output
159 ;; will be in Maxima's 2d output form, otherwise it will be in
160 ;; Maxima's 1d output form. (For XEmacs, only the 1d form is available,
161 ;; since the minibuffer isn't resizable.)
162 ;; The command maxima-insert-last-output will insert
163 ;; the last maxima output into the current buffer; if the output is in 2d,
164 ;; this will look unpleasant. The command maxima-insert-last-output-tex
165 ;; will insert the TeX form of the output.
167 ;; ** Reading Maxima results in the minibuffer **
168 ;; The command `maxima-minibuffer-on-determined-region'
169 ;; will send the part of the current buffer containing the point and between
170 ;; the regexps `maxima-minor-prefix' and `maxima-minor-postfix' (currently
171 ;; both blank lines) to the Maxima process and insert the result in the
172 ;; minibuffer. With an argument, `maxima-minibuffer-in-determined-region'
173 ;; will also insert the output into the current buffer, after " ==> "
174 ;; and before "//". (The symbol ` ==> ' is the value of the customizable
175 ;; variable `maxima-minor-output' and "//" is the value of
176 ;; `maxima-minor-output-end'. The new output is inserted, these strings
177 ;; will be used to delete the old output.
178 ;; Outside of comments in maxima-mode, the opening and closing indicators
179 ;; are the values of `maxima-mode-minor-output' and
180 ;; `maxima-mode-minor-output-end', which by default are " /*==>" and
181 ;; " <==*/", respectively.
182 ;; The commands `maxima-minibuffer-on-region', `maxima-minibuffer-on-line'
183 ;; and `maxima-minibuffer-on-form' work similarly to
184 ;; `maxima-minibuffer-on-determined-region', but send the current region
185 ;; (respectively, the current line, current form) to Maxima and display
186 ;; the result in the minibuffer.
187 ;; (The form is the region between the preceding ; or $ and the subsequent
188 ;; ; or $)
189 ;; Care must be taken when inserting the output into the current buffer
190 ;; with `maxima-minibuffer-on-region' and `maxima-minibuffer-on-form'.
191 ;; With `maxima-minibuffer-on-region', as with
192 ;; `maxima-minibuffer-on-determined-region' above, everything after any
193 ;; "==>" in the region will be ignored.
194 ;; What will typically happen with `maxima-minibuffer-on-region' and
195 ;; `maxima-minibuffer-on-form', however, is that new outputs will
196 ;; be inserted without old output being deleted.
198 ;; The commands for the Maxima-minibuffer interaction can be made
199 ;; available by putting
200 ;; (autoload 'maxima-minibuffer "maxima" "Interact with Maxima from the minibuffer" t)
201 ;; (autoload 'maxima-minibuffer-on-determined-region "maxima"
202 ;; "Send a information to Maxima, display the results in the minibuffer" t)
203 ;; (autoload 'maxima-minibuffer-on-region "maxima"
204 ;; "Send a information to Maxima, display the results in the minibuffer" t)
205 ;; (autoload 'maxima-minibuffer-on-line "maxima"
206 ;; "Send a information to Maxima, display the results in the minibuffer" t)
207 ;; (autoload 'maxima-minibuffer-on-form "maxima"
208 ;; "Send a information to Maxima, display the results in the minibuffer" t)
209 ;; in your .emacs
211 ;; ** Maxima minor mode **
212 ;; maxima-minor-mode provides convenient keybindings for the various
213 ;; interactions between Maxima and the minibuffer.
214 ;; It can be made easily available by placing
215 ;; (autoload 'maxima-minor-mode "maxima" "Maxima minor mode" t)
216 ;; in your .emacs, then M-x maxima-minor-mode will start the minor mode.
217 ;; (The autoloads for the individual function will not then be necessary.)
218 ;; C-c=e
219 ;; `maxima-minibuffer-on-determined-region'
220 ;; C-c=l
221 ;; `maxima-minibuffer-on-line'
222 ;; C-c=r
223 ;; `maxima-minibuffer-on-region'
224 ;; C-c=f
225 ;; `maxima-minibuffer-on-form'
226 ;; C-c=m
227 ;; `maxima-minibuffer'
228 ;; C-c=o
229 ;; `maxima-insert-last-output'
230 ;; C-c=t
231 ;; `maxima-insert-last-output-tex'
233 ;;; Code:
235 ;;;; First
236 (defvar maxima-running-xemacs
237 (featurep 'xemacs))
239 ;;;; The requires
241 (require 'comint)
242 (require 'easymenu)
243 (require 'maxima-font-lock)
245 ;;;; The variables that the user may wish to change
247 (defgroup maxima nil
248 "Maxima mode"
249 :prefix "maxima-"
250 :tag "Maxima")
252 (defcustom maxima-inchar "\\(C\\|%i\\)"
253 "*The character used for an input prompt."
254 :group 'maxima
255 :type 'string)
257 (defcustom maxima-outchar "\\(D\\|%o\\)"
258 "*The character used for an output prompt."
259 :group 'maxima
260 :type 'string)
262 (defcustom maxima-linechar "\\(E\\|%t\\)"
263 "*The character used for an intermediate output prompt."
264 :group 'maxima
265 :type 'string)
267 (defcustom maxima-indent-amount 2
268 "*The amount of each indentation level in `maxima-mode'.
269 This is used after `then', etc."
270 :group 'maxima
271 :type '(integer))
273 (defcustom maxima-paren-indent-amount 1
274 "*The amount of indentation after a parenthesis."
275 :group 'maxima
276 :type '(integer))
278 (defcustom maxima-function-indent-amount 2
279 "*The amount of extra indentation to give within functions."
280 :group 'maxima
281 :type 'integer)
283 (defvar maxima-blockparen-indent-amount nil)
284 (if maxima-blockparen-indent-amount
285 (setq maxima-function-indent-amount
286 maxima-blockparen-indent-amount))
288 (defcustom maxima-continuation-indent-amount 2
289 "*The amount of extra indentation given when a line is continued."
290 :group 'maxima
291 :type '(integer))
293 (defcustom maxima-multiline-comment-indent-amount 2
294 "*The amount of extra indentation inside a multiline comment."
295 :group 'maxima
296 :type '(integer))
298 (defcustom maxima-dont-reindent-some-comments t
299 "*If non-nil, TAB will not change indentation of some comments,
300 namely those with nothing on the starting line past the `/*'."
301 :group 'maxima
302 :type 'boolean)
304 (defcustom maxima-if-extra-indent-amount 0
305 "*The amount of extra indentation to give a \"then\" following an \"if\"."
306 :group 'maxima
307 :type 'integer)
309 ;(defcustom maxima-use-dynamic-complete nil
310 ; "*If non-nil, then M-TAB will complete words dynamically."
311 ; :group 'maxima
312 ; :type 'boolean)
314 (defcustom maxima-indent-style 'standard
315 "*Determines how `maxima-mode' will handle tabs.
316 Choices are 'standard, 'perhaps-smart"
317 :group 'maxima
318 :type '(choice :menu-tag "Indent style"
319 :tag "Indent style"
320 (const standard)
321 (const perhaps-smart)))
323 (defcustom maxima-return-style 'newline-and-indent
324 "*Determines how `maxima-mode' will handle RET.
325 Choices are 'newline, 'newline-and-indent, and 'reindent-then-newline-and-indent"
326 :group 'maxima
327 :type '(choice :menu-tag "Return style"
328 :tag "Return style"
329 (const newline)
330 (const newline-and-indent)
331 (const reindent-then-newline-and-indent)))
333 (defvar maxima-newline-style nil
334 "For compatibility.")
336 (defcustom maxima-command "maxima"
337 "*The command used to start Maxima. See also `maxima-args'."
338 :group 'maxima
339 :type 'string)
341 (defcustom maxima-args nil
342 "*A list of extra arguments to pass to the `maxima-command'. Each
343 element in the list should be a distinct command-line option."
344 :group 'maxima
345 :type 'list)
347 (defcustom maxima-use-tabs nil
348 "*If non-nil, indentation will use tabs."
349 :group 'maxima
350 :type 'boolean)
352 (defcustom maxima-minibuffer-2d nil
353 "*If non-nil, use 2D output for maxima-minibuffer."
354 :group 'maxima
355 :type 'boolean)
357 (defcustom maxima-use-full-color-in-process-buffer nil
358 "*If non-nil, font-lock the maxima process buffer."
359 :group 'maxima
360 :type 'boolean)
362 (defcustom maxima-fix-double-prompt maxima-running-xemacs
363 "*If non-nil, fix the double prompt that sometimes appears in XEmacs."
364 :group 'maxima
365 :type 'boolean)
367 (defcustom maxima-save-input-history nil
368 "*If non-nil, save the input history in a file."
369 :group 'maxima
370 :type 'boolean)
372 (defcustom maxima-input-history-file "~/.maxima_history"
373 "*A file to save the input history in."
374 :group 'maxima
375 :type 'file)
377 (defcustom maxima-input-history-length 50
378 "*How many lines of history to save."
379 :group 'maxima
380 :type 'integer)
382 (defcustom maxima-minor-prefix "^[ \t]*$"
383 "*A regexp to indicate the beginning of a region to send to Maxima
384 in maxima minor mode."
385 :group 'maxima
386 :type 'string)
388 (defcustom maxima-minor-postfix "^[ \t]*$"
389 "*A regexp to indicate the end of a region to send to Maxima
390 in maxima minor mode."
391 :group 'maxima
392 :type 'string)
394 (defcustom maxima-minor-output "==>"
395 "*A string to insert in the buffer right before the output."
396 :group 'maxima
397 :type 'string)
399 (defcustom maxima-minor-output-end " //"
400 "*A string to insert in the buffer right after the output."
401 :group 'maxima
402 :type 'string)
404 (defcustom maxima-mode-minor-output "/*==>"
405 "*A string to insert in the buffer right before the output."
406 :group 'maxima
407 :type 'string)
409 (defcustom maxima-mode-minor-output-end " <==*/"
410 "*A string to insert in the buffer right after the output."
411 :group 'maxima
412 :type 'string)
414 (defcustom maxima-minor-mode-check-input t
415 "*Non-nil means check the input in Maxima minor mode before sending it."
416 :group 'maxima
417 :type 'boolean)
419 (defun maxima-minor-output-mark ()
420 (if (and
421 (eq major-mode 'maxima-mode)
422 (not (maxima-in-comment-p)))
423 maxima-mode-minor-output
424 maxima-minor-output))
426 (defun maxima-minor-output-mark-end ()
427 (if (and
428 (eq major-mode 'maxima-mode)
429 (not (maxima-in-comment-p)))
430 maxima-mode-minor-output-end
431 maxima-minor-output-end))
433 ;;;; The other variables
435 ;; This variable seems to be necessary ...
436 (defvar inferior-maxima-after-output-wait 100)
438 (defvar maxima-temp-suffix 0
439 "Temporary filename suffix. Incremented by 1 for each filename.")
441 (defvar maxima-special-symbol-letters "!:='")
443 (defvar maxima-minibuffer-history nil)
445 (defvar maxima-block "")
447 (defvar maxima-block-wait "")
449 (defvar inferior-maxima-process nil
450 "The Maxima process.")
452 (defvar inferior-maxima-input-end 0
453 "The end of the latest input that was sent to Maxima.")
455 (defvar inferior-maxima-output-end 0)
457 (defvar inferior-maxima-waiting-for-output nil)
459 (defvar inferior-maxima-exit-hook nil)
461 (defvar inferior-maxima-prompt
462 (concat "\\(^(" maxima-inchar
463 "[0-9]*) \\)\\|\\(^MAXIMA> \\)\\|\\(^(dbm:[0-9]*) \\)")
464 ; \\(^[^#%)>]*[#%)>]+ *\\)"
465 "*Regexp to recognize prompts from the inferior Maxima") ; or lisp")
468 (defvar maxima-mode-highlight nil)
470 (defvar maxima-mode-region-begin nil)
472 (defvar maxima-mode-region-end nil)
474 (defvar maxima-minor-mode-region-begin nil)
476 (defvar maxima-minor-mode-region-end nil)
478 (defvar maxima-minor-mode-highlight nil)
480 (defvar maxima-minor-mode-bad-delimiter-regexp "\\([ \t\n]+\\|[0-9]+\\)")
482 ;;;; Utility functions
484 (defun maxima-string-equal (str1 str2)
485 (string= (downcase str1) (downcase str2)))
487 ;; This was taken from `replace-regexp-in-string' from subr.el in GNU emacs.
488 (defun maxima-replace-in-string (regexp rep string)
489 "Replace all matches for REGEXP with REP in STRING."
490 (let ((l (length string))
491 (start 0)
492 matches str mb me)
493 (save-match-data
494 (while (and (< start l) (string-match regexp string start))
495 (setq mb (match-beginning 0)
496 me (match-end 0))
497 (when (= me mb) (setq me (min l (1+ mb))))
498 (string-match regexp (setq str (substring string mb me)))
499 (setq matches
500 (cons (replace-match (if (stringp rep)
502 (funcall rep (match-string 0 str)))
503 nil nil str)
504 (cons (substring string start mb) ; unmatched prefix
505 matches)))
506 (setq start me))
507 ;; Reconstruct a string from the pieces.
508 (setq matches (cons (substring string start l) matches)) ; leftover
509 (apply #'concat (nreverse matches)))))
511 (defun maxima-remove-kill-buffer-hooks ()
512 "Remove the kill-buffer-hooks locally"
513 (if (or maxima-running-xemacs (< emacs-major-version 21))
514 (progn
515 (make-local-hook 'kill-buffer-hook)
516 (setq kill-buffer-hook nil))
517 (let ((hooks kill-buffer-hook))
518 (while hooks
519 (remove-hook 'kill-buffer-hook (car hooks) t)
520 (setq hooks (cdr hooks))))))
522 (defun maxima-make-temp-name ()
523 "Return a unique filename."
524 (setq maxima-temp-suffix (+ maxima-temp-suffix 1))
525 (concat (concat (make-temp-name "#mz") "-")
526 (int-to-string maxima-temp-suffix)
527 ".mac"))
529 (defun maxima-strip-string-beginning (string)
530 "Return STRING with whitespace and comments removed from the beginning."
531 (let* ((tmpfile (maxima-make-temp-name))
532 (tmpbuf (get-buffer-create tmpfile))
533 (out))
534 (save-excursion
535 (set-buffer tmpbuf)
536 (maxima-remove-kill-buffer-hooks)
537 (modify-syntax-entry ?/ ". 14")
538 (modify-syntax-entry ?* ". 23")
539 (insert string)
540 (goto-char (point-min))
541 (maxima-forward-over-comment-whitespace)
542 (setq out (buffer-substring-no-properties (point) (point-max))))
543 (kill-buffer tmpbuf)
544 out))
546 (defun maxima-strip-string-end (string)
547 "Return STRING with whitespace and comments removed from the end."
548 (let* ((tmpfile (maxima-make-temp-name))
549 (tmpbuf (get-buffer-create tmpfile))
550 (out))
551 (save-excursion
552 (set-buffer tmpbuf)
553 (maxima-remove-kill-buffer-hooks)
554 (modify-syntax-entry ?/ ". 14")
555 (modify-syntax-entry ?* ". 23")
556 (insert string)
557 (goto-char (point-max))
558 (maxima-back-over-comment-whitespace)
559 (setq out (buffer-substring-no-properties (point-min) (point))))
560 (kill-buffer tmpbuf)
561 out))
563 (defun maxima-strip-string (string)
564 "Return STRING with whitespace and comments removed from the ends."
565 (maxima-strip-string-beginning (maxima-strip-string-end string)))
567 (defun maxima-strip-string-add-semicolon (string)
568 "Return STRING with whitespace and comments removed from the ends."
569 (setq string
570 (maxima-strip-string-beginning (maxima-strip-string-end string)))
571 (unless (or
572 (string= string "")
573 (and (>= (length string) 5)
574 (string= (substring string 0 5) ":lisp"))
575 (string= (substring string -1) ";")
576 (string= (substring string -1) "$"))
577 (setq string (concat string ";")))
578 string)
580 (defun maxima-remove-whitespace-from-ends (string)
581 "Return STRING with whitespace from the ends."
582 (let* ((tmpfile (maxima-make-temp-name))
583 (tmpbuf (get-buffer-create tmpfile))
584 (str string)
585 (out)
586 (beg)
587 (end))
588 (save-excursion
589 (set-buffer tmpbuf)
590 (maxima-remove-kill-buffer-hooks)
591 (insert str)
592 (goto-char (point-min))
593 (skip-chars-forward " \t\n")
594 (setq beg (point))
595 (goto-char (point-max))
596 (skip-chars-backward " \t\n")
597 (setq end (point))
598 (setq out (buffer-substring-no-properties beg end)))
599 (kill-buffer tmpbuf)
600 out))
602 (defun maxima-remove-whitespace-from-beg (string)
603 "Return STRING with whitespace removed from the beginning."
604 (let* ((tmpfile (maxima-make-temp-name))
605 (tmpbuf (get-buffer-create tmpfile))
606 (out)
607 (str string)
608 (beg)
609 (end))
610 (save-excursion
611 (set-buffer tmpbuf)
612 (maxima-remove-kill-buffer-hooks)
613 (insert str)
614 (goto-char (point-min))
615 (skip-chars-forward " \t\n")
616 (setq beg (point))
617 (setq out (buffer-substring-no-properties beg (point-max))))
618 (kill-buffer tmpbuf)
619 out))
621 (defun maxima-remove-whitespace-from-end (string)
622 "Return STRING with whitespace removed from the end."
623 (let* ((tmpfile (maxima-make-temp-name))
624 (tmpbuf (get-buffer-create tmpfile))
625 (out)
626 (str string)
627 (beg)
628 (end))
629 (save-excursion
630 (set-buffer tmpbuf)
631 (maxima-remove-kill-buffer-hooks)
632 (insert str)
633 (goto-char (point-max))
634 (skip-chars-backward " \t\n")
635 (setq end (point))
636 (setq out (buffer-substring-no-properties (point-min) end)))
637 (kill-buffer tmpbuf)
638 out))
640 ;;;; Functions that query position
641 (defun maxima-in-comment-p ()
642 "Non-nil means that the point is in a comment."
643 (let ((pt (point)))
644 (save-excursion
645 (and
646 (search-backward "/*" nil t)
647 (not (search-forward "*/" pt t))))))
649 (defun maxima-in-output-p ()
650 "Non-nil means that the point is in minibuffer output."
651 (let ((pt (point)))
652 (save-excursion
653 (and
654 (search-backward (maxima-minor-output-mark) nil t)
655 (not (search-forward (maxima-minor-output-mark-end) pt t))))))
657 ;;; Functions that search
659 ;; Some additions to help with different types of searches
660 (defvar maxima-mode-type 'maxima-mode)
661 (make-variable-buffer-local 'maxima-mode-type)
663 (defvar maxima-noweb-ignore-bounds '("<<" ">>"))
665 (defun maxima-noweb-in-ignore-bounds-p ()
666 (if (not maxima-noweb-ignore-bounds)
668 (let ((pt (point)))
669 (save-excursion
670 (if (not (re-search-backward (car maxima-noweb-ignore-bounds) nil t))
672 (not (re-search-forward (cadr maxima-noweb-ignore-bounds) pt t)))))))
674 (defun maxima-noweb-forward-out-of-ignore-bounds (&optional pmax)
675 (re-search-forward (cadr maxima-noweb-ignore-bounds) pmax 1))
677 (defun maxima-noweb-backward-out-of-ignore-bounds (&optional pmin)
678 (re-search-backward (car maxima-noweb-ignore-bounds) pmin 1))
681 (defun maxima-standard-re-search-forward (regexp &optional pmax)
682 "Search forward for REGEXP, bounded by PMAX.
683 Ignore matches found in comments and strings."
684 (let ((keep-looking t)
685 (didnt-find nil)
686 (match nil)
687 (ppe)
688 (pt)
689 (origpt (point)))
690 (setq pt origpt)
691 (while (and keep-looking
692 (not didnt-find)
693 (re-search-forward regexp pmax t))
694 (setq match (match-string 0))
695 (setq ppe (parse-partial-sexp pt (point)))
696 (cond
697 ((nth 3 ppe) ;; In a string
698 (if (maxima-goto-end-of-string)
699 (setq pt (point))
700 (setq didnt-find t)))
701 ((nth 4 ppe) ;; In a comment
702 (if (maxima-goto-end-of-comment)
703 (setq pt (point))
704 (setq didnt-find t)))
705 (t ;; not in a comment or string
706 (setq keep-looking nil))))
707 (if (or didnt-find keep-looking)
708 (progn
709 (goto-char origpt)
710 nil)
711 match)))
714 (defun maxima-noweb-re-search-forward (regexp &optional pmax)
715 (let ((match
716 (maxima-standard-re-search-forward regexp pmax)))
717 (while (maxima-noweb-in-ignore-bounds-p)
718 (maxima-noweb-forward-out-of-ignore-bounds pmax)
719 (setq match
720 (maxima-standard-re-search-forward regexp pmax)))
721 match))
723 (defun maxima-re-search-forward (regexp &optional pmax)
724 (cond
725 ((eq maxima-mode-type 'maxima-noweb-mode)
726 (maxima-noweb-re-search-forward regexp pmax))
728 (maxima-standard-re-search-forward regexp pmax))))
730 (defun maxima-re-search-forward-skip-blocks (regexp &optional pmax)
731 "Search forward for REGEXP, bounded by PMAX.
732 Ignore matches found in comments and strings, and skip over
733 parenthesized or bracketed blocks."
734 (let ((keep-looking t)
735 (didnt-find nil)
736 (match nil)
737 (pt (point)))
738 (while (and keep-looking
739 (setq match (maxima-re-search-forward
740 (concat regexp "\\|[[(]") pmax)))
741 (cond
742 ((or
743 (string= match "[")
744 (string= match "("))
745 (unless
746 (maxima-goto-end-of-list)
747 (setq didnt-find t)
748 (setq keep-looking nil)))
750 (setq keep-looking nil))))
751 (if (or keep-looking didnt-find)
752 (progn
753 (goto-char pt)
754 nil)
755 match)))
757 (defun maxima-standard-re-search-backward (regexp &optional pmin)
758 "Search backward for REGEXP, bounded by PMIN.
759 Ignore matches found in comments and strings."
760 (let ((keep-looking t)
761 (match nil)
762 (ppe)
763 (origpt (point)))
764 (unless pmin
765 (setq pmin (point-min)))
766 (while (and keep-looking
767 (re-search-backward regexp pmin t))
768 (setq match (match-string 0))
769 (setq ppe (parse-partial-sexp pmin (point)))
770 (cond
771 ((nth 8 ppe) ;; In a string or comment
772 (goto-char (nth 8 ppe)))
773 (t ;; not in a comment or string
774 (setq keep-looking nil))))
775 (if keep-looking
776 (progn
777 (goto-char origpt)
778 nil)
779 match)))
781 (defun maxima-noweb-re-search-backward (regexp &optional pmin)
782 (let ((match
783 (maxima-standard-re-search-backward regexp pmin)))
784 (while (maxima-noweb-in-ignore-bounds-p)
785 (maxima-noweb-backward-out-of-ignore-bounds pmin)
786 (setq match
787 (maxima-standard-re-search-backward regexp pmin)))
788 match))
790 (defun maxima-re-search-backward (regexp &optional pmin)
791 (cond
792 ((eq maxima-mode-type 'maxima-noweb-mode)
793 (maxima-noweb-re-search-backward regexp pmin))
795 (maxima-standard-re-search-backward regexp pmin))))
797 (defun maxima-re-search-backward-skip-blocks (regexp &optional pmin)
798 "Search forward for REGEXP, bounded by PMIN.
799 Ignore matches found in comments and strings, and skip over
800 parenthesized and bracketed blocks."
801 (let ((keep-looking t)
802 (didnt-find nil)
803 (match nil)
804 (pt (point)))
805 (while (and keep-looking
806 (setq match (maxima-re-search-backward
807 (concat regexp "\\|[])]") pmin)))
808 (cond
809 ((or
810 (string= match ")")
811 (string= match "]"))
812 (unless
813 (maxima-goto-beginning-of-list)
814 (setq didnt-find t)
815 (setq keep-looking nil)))
817 (setq keep-looking nil))))
818 (if (or keep-looking didnt-find)
819 (progn
820 (goto-char pt)
821 nil)
822 match)))
824 (defun maxima-escaped-char-p ()
825 "Returns non-nil if the character after point is escaped"
826 (let ((pm (point-min))
827 (esc-chars 0))
828 (when (> (point) pm)
829 (save-excursion
830 (forward-char -1)
831 (while (and
832 (looking-at "\\\\")
833 (setq esc-chars (1+ esc-chars))
834 (> (point) pm))
835 (forward-char -1))))
836 (if (= (% esc-chars 2) 0)
838 t)))
840 (defun maxima-goto-end-of-string ()
841 "Go to the end of the string that the point is in.
842 Assumes that point is in a string."
843 (interactive)
844 (let ((keep-looking t))
845 (while (and keep-looking (search-forward "\"" nil t))
846 (forward-char -2)
847 (unless (maxima-escaped-char-p)
848 (setq keep-looking nil))
849 (forward-char 2))
850 (if keep-looking
852 t)))
854 (defun maxima-goto-beginning-of-string ()
855 "Go to the beginning of the string that the point is in.
856 Assumes that point is in a string."
857 (interactive)
858 (let ((keep-looking t))
859 (while (and keep-looking (search-backward "\"" nil t))
860 (forward-char -1)
861 (unless (maxima-escaped-char-p)
862 (setq keep-looking nil))
863 (forward-char 1))))
865 (defun maxima-goto-end-of-comment ()
866 "Go to the end of the comment that the point is in.
867 Assumes that point is in a comment."
868 (interactive)
869 (search-forward "*/" nil t))
871 (defun maxima-goto-beginning-of-comment ()
872 "Go to the beginning of the comment that the point is in.
873 Assumes that point is in a comment."
874 (interactive)
875 (search-backward "/*"))
877 (defun maxima-find-next-nonnested-close-char ()
878 "Search forward for next , ; $ or closing ), skipping over balanced parens.
879 If character is in a string or a list, ignore it."
880 (interactive)
881 (maxima-re-search-forward-skip-blocks "[,;$)]"))
884 ;;; Functions for dealing with words
886 ;; (defun maxima-number-of-preceding-backslashes ()
887 ;; "The number of backslashes (including the one being looked at)."
888 ;; (let ((pt (point)))
889 ;; (if (not (looking-at "\\\\"))
890 ;; 0
891 ;; (save-excursion
892 ;; (skip-chars-backward "\\\\")
893 ;; (1+ (- pt (point)))))))
895 (defun maxima-standard-next-char-word-part-p ()
896 "Non-nil if next char is a a word part."
898 (looking-at "\\w")
899 (looking-at "\\\\")
900 (save-excursion
901 (forward-char -1)
902 (looking-at "\\\\"))))
904 (defun maxima-noweb-next-char-word-part-p ()
905 "Non-nil if next char is a a word part."
907 (looking-at "\\w")
908 (looking-at "\\\\")
909 (and
910 (looking-at ">")
911 (save-excursion
912 (forward-char -1)
913 (looking-at ">")))
914 (save-excursion
915 (forward-char -1)
916 (looking-at "\\\\"))))
918 (defun maxima-next-char-word-part-p ()
919 (cond
920 ((eq maxima-mode-type 'maxima-noweb-mode)
921 (maxima-noweb-next-char-word-part-p))
923 (maxima-standard-next-char-word-part-p))))
925 (defun maxima-previous-char-word-part-p ()
926 "Non-nil if previous character is a word part."
927 (save-excursion
928 (forward-char -1)
929 (maxima-next-char-word-part-p)))
931 (defun maxima-standard-forward-word ()
932 "Go to the end of the current word."
933 (let ((keep-going t))
934 (while keep-going
935 (cond
936 ((looking-at "\\w")
937 (forward-word 1))
938 ((looking-at "\\\\")
939 (forward-char 2))
941 (setq keep-going nil))))))
943 (defun maxima-noweb-forward-word ()
944 "Go to the end of the current word."
945 (if (maxima-noweb-in-ignore-bounds-p)
946 (maxima-noweb-forward-out-of-ignore-bounds))
947 (let ((keep-going t))
948 (while keep-going
949 (cond
950 ((looking-at "\\w")
951 (forward-word 1))
952 ((looking-at "\\\\")
953 (forward-char 2))
954 ((looking-at "<<")
955 (forward-char 2)
956 (maxima-noweb-forward-out-of-ignore-bounds))
958 (setq keep-going nil))))))
960 (defun maxima-forward-word ()
961 (cond
962 ((eq maxima-mode-type 'maxima-noweb-mode)
963 (maxima-noweb-forward-word))
965 (maxima-standard-forward-word))))
967 (defun maxima-standard-backward-word ()
968 "Go to the beginning of the current word."
969 (let ((keep-going t))
970 (while keep-going
971 (cond
972 ((and
973 (> (point) (point-min))
974 (save-excursion
975 (forward-char -1)
976 (looking-at "\\w")))
977 (backward-word 1))
978 ((and
979 (> (point) (1+ (point-min)))
980 (save-excursion
981 (forward-char -2)
982 (looking-at "\\\\")))
983 (forward-char -2))
985 (setq keep-going nil))))))
987 (defun maxima-noweb-backward-word ()
988 "Go to the beginning of the current word."
989 (if (maxima-noweb-in-ignore-bounds-p)
990 (maxima-noweb-backward-out-of-ignore-bounds))
991 (let ((keep-going t))
992 (while keep-going
993 (cond
994 ((and
995 (> (point) (point-min))
996 (save-excursion
997 (forward-char -1)
998 (looking-at "\\w")))
999 (backward-word 1))
1000 ((and
1001 (> (point) (1+ (point-min)))
1002 (save-excursion
1003 (forward-char -2)
1004 (looking-at "\\\\")))
1005 (forward-char -2))
1006 ((and
1007 (> (point) (1+ (point-min)))
1008 (save-excursion
1009 (forward-char -2)
1010 (looking-at ">>")))
1011 (forward-char -2)
1012 (maxima-noweb-backward-out-of-ignore-bounds))
1014 (setq keep-going nil))))))
1016 (defun maxima-backward-word ()
1017 (cond
1018 ((eq maxima-mode-type 'maxima-noweb-mode)
1019 (maxima-noweb-backward-word))
1021 (maxima-standard-backward-word))))
1023 ;;;; Functions that return special positions
1025 (defun maxima-line-beginning-position ()
1026 (if (not (fboundp 'line-beginning-position))
1027 (save-excursion
1028 (beginning-of-line)
1029 (point))
1030 (line-beginning-position)))
1032 (defun maxima-line-end-position ()
1033 (if (not (fboundp 'line-end-position))
1034 (save-excursion
1035 (end-of-line)
1036 (point))
1037 (line-end-position)))
1039 (defun maxima-name-beginning ()
1040 (save-excursion
1041 (maxima-backward-word)
1042 (point)))
1044 (defun maxima-special-symbol-beginning ()
1045 (save-excursion
1046 (skip-chars-backward maxima-special-symbol-letters)
1047 (point)))
1049 (defun maxima-special-symbol-end ()
1050 (save-excursion
1051 (skip-chars-forward maxima-special-symbol-letters)
1052 (point)))
1054 (defun maxima-form-beginning-position ()
1055 (save-excursion
1056 (maxima-goto-beginning-of-form)
1057 (point)))
1059 (defun maxima-form-end-position ()
1060 (save-excursion
1061 (if (maxima-goto-end-of-form)
1062 (point)
1063 nil)))
1065 (defun maxima-form-end-position-or-point-max ()
1066 (let ((mfep (maxima-form-end-position)))
1067 (if mfep
1068 mfep
1069 (point-max))))
1071 (defun maxima-expression-end-position ()
1072 "Return the point where the current expression ends,
1073 or nil."
1074 (save-excursion
1075 (if (maxima-goto-end-of-expression)
1076 (point)
1077 nil)))
1079 (defun maxima-begin-if-position (pmin)
1080 "Find the point of the opening \"if\" for the current point."
1081 (let ((nest 0)
1082 (match)
1083 (keep-looking t)
1084 (pt (point)))
1085 (save-excursion
1086 (while (and keep-looking
1087 (setq match
1088 (maxima-re-search-backward-skip-blocks
1089 "\\<if\\>\\|\\<then\\>" pmin)))
1090 (setq match (downcase match))
1091 (cond ((maxima-string-equal match "if") (setq nest (1+ nest)))
1092 (t (setq nest (1- nest))))
1093 (when (= nest 1)
1094 (setq pt (point))
1095 (setq keep-looking nil))))
1096 (if keep-looking
1098 pt)))
1100 (defun maxima-begin-then-position (pmin)
1101 "Find the point of the opening \"then\" for the current \"else\"."
1102 (let ((keep-going t)
1103 (pt (point))
1104 (begin-then nil))
1105 (save-excursion
1106 (while (and keep-going
1107 (maxima-re-search-backward-skip-blocks "\\<then\\>" pmin))
1108 ;; A potential "then". Let's see if it is.
1109 (let ((meep (maxima-expression-end-position)))
1110 (when (or (not meep) (<= pt meep))
1111 ;; This "then" is looking pretty good.
1112 ;; Now we need to make sure that there aren't any "else"s
1113 ;; in the way.
1114 (let ((level 0)
1115 (match))
1116 (save-excursion
1117 (while (setq match (maxima-re-search-forward-skip-blocks
1118 "\\<then\\>\\|\\<else\\>" pt))
1119 (cond ((maxima-string-equal match "then")
1120 (setq level (1+ level)))
1121 ((maxima-string-equal match "else")
1122 (setq level (1- level))))))
1123 (when (= level 1)
1124 (setq begin-then (point))
1125 (setq keep-going nil))))))
1126 begin-then)))
1128 ;;;; Functions that move the position
1129 (defun maxima-forward-over-comment-whitespace ()
1130 "Move forward over comments and whitespace."
1131 (forward-comment (buffer-size))
1132 (let ((mmo (maxima-remove-whitespace-from-beg (maxima-minor-output-mark))))
1133 (when (and (> (- (point-max) (point)) (length mmo))
1134 (string=
1135 (buffer-substring-no-properties
1136 (point) (+ (point) (length mmo)))
1137 mmo))
1138 (search-forward (maxima-minor-output-mark-end))
1139 (forward-comment (buffer-size)))))
1141 (defun maxima-back-over-comment-whitespace ()
1142 "Move backward over comments and whitespace."
1143 (forward-comment (- (buffer-size)))
1144 (let ((mme (maxima-remove-whitespace-from-end (maxima-minor-output-mark-end))))
1145 (when (and (> (- (point) (point-min)) (length mme))
1146 (string=
1147 (buffer-substring-no-properties
1148 (- (point) (length mme)) (point))
1149 mme))
1150 (search-backward (maxima-minor-output-mark))
1151 (forward-comment (- (buffer-size))))))
1153 (defun maxima-standard-goto-beginning-of-form ()
1154 "Move to the beginning of the form."
1155 (let ((pt (point))
1156 (keep-looking t))
1157 (while (and keep-looking
1158 (maxima-re-search-backward "[;$]" nil))
1159 (forward-char -1)
1160 (unless (looking-at "\\\\\\$")
1161 (forward-char 2)
1162 (setq keep-looking nil)))
1163 (if keep-looking
1164 (goto-char (point-min)))
1165 (maxima-forward-over-comment-whitespace)
1166 (if (< pt (point))
1167 (goto-char pt))
1168 (point)))
1170 (defun maxima-noweb-goto-beginning-of-form ()
1171 "Move to the beginning of the form."
1172 (if (re-search-backward "^<<.*?>>= *$" (point-min) 1)
1173 (forward-line 1))
1174 (maxima-forward-over-comment-whitespace))
1176 (defun maxima-goto-beginning-of-form ()
1177 (cond
1178 ((eq maxima-mode-type 'maxima-noweb-mode)
1179 (maxima-noweb-goto-beginning-of-form))
1181 (maxima-standard-goto-beginning-of-form))))
1183 (defun maxima-goto-beginning-of-form-interactive ()
1184 "Move to the beginning of the form."
1185 (interactive)
1186 (maxima-goto-beginning-of-form))
1188 (defun maxima-standard-goto-end-of-form ()
1189 "Move to the end of the form."
1190 (let ((keep-looking t)
1191 (pt (point)))
1192 (while (and keep-looking
1193 (maxima-re-search-forward "[;$]" nil))
1194 (forward-char -2)
1195 (unless (looking-at "\\\\\\$")
1196 (setq keep-looking nil))
1197 (forward-char 2))
1198 (if (not keep-looking)
1199 (point)
1200 (goto-char pt)
1201 nil)))
1203 (defun maxima-noweb-goto-end-of-form ()
1204 "Move to the end of the form."
1205 (when (re-search-forward "\\(^@\\( \\|$\\)\\|^<<.*>>= *$\\)" nil 1)
1206 (forward-line -1)
1207 (end-of-line)
1208 (maxima-back-over-comment-whitespace)))
1210 (defun maxima-goto-end-of-form ()
1211 (cond
1212 ((eq maxima-mode-type 'maxima-noweb-mode)
1213 (maxima-noweb-goto-end-of-form))
1215 (maxima-standard-goto-end-of-form))))
1217 (defun maxima-goto-end-of-form-interactive ()
1218 "Move to the end of the form."
1219 (interactive)
1220 (unless (maxima-goto-end-of-form)
1221 (message "No end of form")))
1223 (defun maxima-goto-end-of-expression ()
1224 "Find the point that ends the expression that begins at point.
1225 The expression is assumed to begin with \"if\", \"then\", \"do\"
1226 \"else\" or \"(\". Return nil if the end is not found."
1227 ; (interactive)
1228 ;; To find the end of the expression:
1229 ;; if looking at (, look for )
1230 ;; otherwise look for a , ; or $ at the same nesting level of
1231 ;; parentheses or a closing ).
1232 (cond ((or (looking-at "(")
1233 (looking-at "\\["))
1234 (maxima-forward-list))
1236 (maxima-find-next-nonnested-close-char))))
1238 (defun maxima-goto-beginning-of-construct (pmin)
1239 "Go to the point the begins the current construct."
1240 (let ((keep-looking t)
1241 (pt (point)))
1242 (while (and keep-looking
1243 (maxima-re-search-backward-skip-blocks
1244 "\\<if\\>\\|\\<then\\>\\|\\<do\\>\\|\\<else\\>\\|(\\|\\[" pmin))
1245 (save-excursion
1246 (when (or (not (maxima-goto-end-of-expression)) (<= pt (point)))
1247 (setq keep-looking nil))))
1248 (if keep-looking
1249 (goto-char pmin))
1250 (point)))
1252 (defun maxima-goto-end-of-list ()
1253 "Go up a list.
1254 Return t if possible, nil otherwise."
1255 (interactive)
1257 (condition-case nil
1258 (up-list 1)
1259 (error t))
1263 (defun maxima-goto-end-of-list-interactive ()
1264 "Go up a list."
1265 (interactive)
1266 (if (maxima-goto-end-of-list)
1268 (error "No list to end.")))
1270 (defun maxima-goto-beginning-of-list ()
1271 "Go up a list backwards.
1272 Return t if possible, nil otherwise."
1274 (condition-case nil
1275 (up-list -1)
1276 (error t))
1280 (defun maxima-goto-beginning-of-list-interactive ()
1281 "Go up a list."
1282 (interactive)
1283 (if (maxima-goto-beginning-of-list)
1285 (error "No list to begin.")))
1287 (defun maxima-forward-list ()
1288 "Go forward a list.
1289 Return t if possible, nil otherwise."
1291 (condition-case nil
1292 (forward-list 1)
1293 (error nil))
1295 nil))
1297 (defun maxima-backward-list ()
1298 "Go backward a list.
1299 Return t if possible, nil otherwise."
1301 (condition-case nil
1302 (backward-list 1)
1303 (error nil))
1305 nil))
1307 ;;; Newlines and indents
1308 (defun maxima-indent-form ()
1309 "Indent the entire form."
1310 (interactive)
1311 (indent-region
1312 (maxima-form-beginning-position)
1313 (maxima-form-end-position-or-point-max)
1314 nil))
1316 ;;; 'standard
1317 (defun maxima-standard-indent ()
1318 "Indent the line based on the previous line.
1319 If the previous line opened a parenthesis, `maxima-indent-amount' is
1320 added to the indentation, if the previous line closed a parenthesis,
1321 `maxima-indent-amount' is subtracted, otherwise the indentation
1322 is the same as the previous line."
1323 (interactive)
1324 (let ((indent 0)
1325 (match)
1326 (pt))
1327 (save-excursion
1328 (when (= (forward-line -1) 0)
1329 (progn
1330 (setq indent (current-indentation))
1331 (setq pt (maxima-line-end-position))
1332 (while (setq match (maxima-re-search-forward "[()]" pt))
1333 (cond ((string= match ")")
1334 (setq indent (- indent maxima-indent-amount)))
1335 ((string= match "(")
1336 (setq indent (+ indent maxima-indent-amount))))))))
1337 (save-excursion
1338 (beginning-of-line)
1339 (delete-horizontal-space)
1340 (indent-line-to (max indent 0)))
1341 (skip-chars-forward " \t")))
1343 (defun maxima-untab ()
1344 "Delete a level of indentation"
1345 (interactive)
1346 ;; Check to see if the previous maxima-indent-amount spaces are
1347 ;; on the same line and blank
1348 (let ((i maxima-indent-amount)
1349 (ok t)
1350 (pt (point)))
1351 (save-excursion
1352 (while (and (> i 0) ok)
1353 (setq i (- i 1))
1354 (forward-char -1)
1355 (if (not (looking-at " "))
1356 (setq ok nil))
1357 (if (looking-at "\n")
1358 (setq ok nil))))
1359 (if ok
1360 (delete-region pt (- pt maxima-indent-amount)))))
1363 ;;; Find the beginning of a function
1365 (defun maxima-open-paren-is-function ()
1366 "Check to see if the point is before an opening paren,
1367 and if the previous character is a close paren or word part."
1368 (and
1369 (looking-at "[ \t]*(")
1370 (save-excursion
1371 (maxima-back-over-comment-whitespace)
1372 (forward-char -1)
1373 (looking-at ")\\|\\w"))))
1375 (defun maxima-close-paren-before-open-paren ()
1376 "Return point of close paren before the current open paren,
1377 or nil if there is none."
1378 (save-excursion
1379 (maxima-back-over-comment-whitespace)
1380 (forward-char -1)
1381 (if (looking-at ")")
1382 (1+ (point))
1383 nil)))
1385 (defun maxima-back-over-paren-groups ()
1386 "Go over any paren groups.
1387 Assume point is at \"(\", as long as preceding character is
1388 \")\", go to open parentheses."
1389 (let ((cpbop (maxima-close-paren-before-open-paren)))
1390 (while cpbop
1391 (goto-char cpbop)
1392 (maxima-backward-list)
1393 (setq cpbop
1394 (maxima-close-paren-before-open-paren)))))
1396 (defun maxima-word-on-previous-line ()
1397 "Go to the previous word part, return nil if "
1398 (let ((pt (point)))
1399 (save-excursion
1400 (skip-chars-backward " \t\n")
1401 ;; Same line?
1402 (and
1403 (maxima-previous-char-word-part-p)
1404 (string-match "\n"
1405 (buffer-substring-no-properties (point) pt))))))
1407 (defun maxima-string-on-previous-line ()
1408 "Go to the previous word part, return nil if "
1409 (let ((pt (point)))
1410 (save-excursion
1411 (skip-chars-backward " \t\n")
1412 ;; Same line?
1413 (forward-char -1)
1414 (and
1415 (looking-at "\"")
1416 (string-match "\n"
1417 (buffer-substring-no-properties (point) pt))))))
1419 (defun maxima-back-over-function-name ()
1420 "Go back over the `foo' in 'foo(x,y)', return the length of `foo'.
1421 Return nil if open paren is not part of function.
1422 Assumes the point is right before the open parenthesis."
1423 (let ((pt (point))
1424 (endpt))
1425 (maxima-back-over-paren-groups)
1426 (setq endpt (point))
1427 ;; Let's see what's before this
1428 (cond
1429 ;; There is a word right before this
1430 ((maxima-previous-char-word-part-p)
1431 (maxima-backward-word)
1432 (- endpt (point)))
1433 ;; There is a string before this
1434 ((save-excursion
1435 (forward-char -1)
1436 (looking-at "\""))
1437 (maxima-goto-beginning-of-string)
1438 (- endpt (point)))
1439 ;; There is a word before this on the previous line
1440 ((maxima-word-on-previous-line)
1441 (skip-chars-backward " \t\n")
1442 (setq endpt (point))
1443 (maxima-backward-word)
1444 (- endpt (point)))
1445 ;; There is a string before this on the previous line
1446 ((maxima-string-on-previous-line)
1447 (skip-chars-backward " \t\n")
1448 (setq endpt (point))
1449 (maxima-goto-beginning-of-string)
1450 (- endpt (point)))
1451 ;; This point is the original point
1452 ((= endpt pt)
1453 nil)
1454 ;; Finally, the last parenthesized expression is the function
1456 (save-excursion
1457 (maxima-forward-list)
1458 (setq endpt (point)))
1459 (- endpt (point))))))
1461 ;;; 'perhaps-smart
1463 (defun maxima-after-lisp-expression-p ()
1464 "Return non-nil if the point is right after a lisp expression."
1465 (let ((pm (point-min))
1466 (pt))
1467 (save-excursion
1468 (maxima-back-over-comment-whitespace)
1469 (setq pt (point))
1470 (condition-case nil
1471 (backward-sexp)
1472 (error t))
1473 (when (< (point) pt)
1474 (maxima-back-over-comment-whitespace)
1475 (if (< (point) (+ 5 pm))
1477 (forward-char -5)
1478 (if (looking-at ":lisp")
1479 (current-column)
1480 nil))))))
1482 (defun maxima-standard-perhaps-smart-calculate-indent ()
1483 "Return appropriate indentation for current line as Maxima code.
1484 Returns an integer: the column to indent to."
1485 (let ((indent 0)
1486 (pmin)
1487 (tmpchar)
1488 (pt)
1489 (le)
1490 (comma-line)
1491 (len)
1492 (pps))
1493 (save-excursion
1494 (beginning-of-line)
1495 (setq pt (point))
1496 (setq pmin (maxima-form-beginning-position))
1497 (setq pps (parse-partial-sexp pmin (point)))
1498 (setq le (maxima-after-lisp-expression-p))
1499 (when (nth 1 pps)
1500 (setq pmin (nth 1 pps))
1501 (unless (looking-at "^[ \t]*,")
1502 (save-excursion
1503 (when (maxima-re-search-backward-skip-blocks "," pmin)
1504 (save-excursion
1505 (let ((lep (maxima-line-end-position)))
1506 (forward-char 1)
1507 (maxima-forward-over-comment-whitespace)
1508 (unless (>= (point) lep)
1509 (setq pmin (point)))))))))
1510 (cond
1511 ;; First, take care of the cases where the indentation is clear
1512 ;; No indentation at the beginning of the buffer
1513 ((= pt (point-min))
1514 (setq indent 0))
1515 ;; Don't change the indentation if in a string
1516 ((nth 3 pps)
1517 (setq indent -1))
1518 ;; If the line begins a comment and comments aren't reindented,
1519 ;; don't reindent it
1520 ((and
1521 (looking-at "[ \t]*/\\*[ \t]*$")
1522 maxima-dont-reindent-some-comments)
1523 (setq indent -1))
1524 ;; Deal with comments separately
1525 ((maxima-perhaps-smart-in-comment-p (nth 4 pps) pmin pt)
1526 (setq indent (maxima-perhaps-smart-comment-calculate-indent)))
1527 ;; If the current point is the beginning of the form, the level is 0
1528 ((= (point) pmin)
1529 (setq indent 0))
1530 ;; If the current point is in maxima minor output, don't reindent it
1531 ((maxima-in-output-p)
1532 (setq indent -1))
1533 ;; A line beginning "then" is indented like the opening "if"
1534 ((and
1535 (looking-at "[ \t]*\\<then\\>")
1536 (setq tmpchar (maxima-begin-if-position pmin)))
1537 (goto-char tmpchar)
1538 (setq indent (+ maxima-if-extra-indent-amount (current-column))))
1539 ;; A line beginning "else" is indented like the corresponding "then"
1540 ((and
1541 (looking-at "[ \t]*\\<else\\>")
1542 (setq tmpchar (maxima-begin-then-position pmin)))
1543 (goto-char tmpchar)
1544 (setq indent (current-column)))
1545 ;; A line beginning with an open paren that is the
1546 ;; beginning of a function argument is indented according to
1547 ;; the function
1548 ((and
1549 (looking-at "[ \t]*(")
1550 (setq len (maxima-back-over-function-name)))
1551 (setq indent (+ (current-column)
1552 (min len maxima-function-indent-amount))))
1553 ;; A line beginning with a closing paren is indented like the open paren
1554 ((looking-at "[ \t]*)")
1555 ;; Here, pmin should be the opening paren position
1556 (goto-char pmin)
1557 (if (looking-at "( *$")
1558 (progn
1559 (setq len (maxima-back-over-function-name))
1560 (setq indent (+
1561 (if len
1562 (min len maxima-function-indent-amount)
1564 (current-column))))
1565 (setq indent (current-column))))
1566 ;; A line beginning with a closing bracket is indented like the open bracket
1567 ((looking-at "[ \t]*\\]")
1568 ;; Here, pmin should be the opening bracket position
1569 (goto-char pmin)
1570 (setq indent (current-column)))
1571 ; ;; A line beginning with a comma is indented like the opening paren
1572 ; ((looking-at "[ \t]*,")
1573 ; (goto-char pmin)
1574 ; (setq indent (current-column)))
1575 ;; The point is at the end of a lisp expression
1577 (setq indent le))
1578 ;; Otherwise, the correct indentation needs to be computed.
1580 ;; Find the indentation of beginning of the current construct
1581 ;; If begin-construct is nil, indent according to the opening paren
1582 (setq comma-line (looking-at "[ \t]*,"))
1583 (save-excursion
1584 (maxima-goto-beginning-of-construct pmin)
1585 (cond
1586 ;; The construct begins with a bracket
1587 ((looking-at "\\[")
1588 ; (if comma-line
1589 ; (setq indent (current-column))
1590 (setq indent (+ maxima-paren-indent-amount (current-column)))
1591 (forward-char 1)
1592 (skip-chars-forward " \t")
1593 (unless (looking-at "\n")
1594 (setq indent (current-column))));)
1595 ;; The construct begins with a paren
1596 ((looking-at "(")
1597 (cond
1598 ; (comma-line
1599 ; (setq indent (current-column)))
1600 ((save-excursion
1601 (let ((lep (maxima-line-end-position)))
1602 (forward-char 1)
1603 (maxima-forward-over-comment-whitespace)
1604 (>= (point) lep)))
1605 ;(looking-at "( *$")
1606 ;; Check to see if there is anything before the (
1607 (if (save-excursion
1608 (re-search-backward "\\(?:^[ \t]*\\)\\=" nil t))
1609 (setq tmpchar maxima-paren-indent-amount)
1610 (setq tmpchar 0))
1611 ;; If there is nothing after the (, there are two
1612 ;; cases
1613 ;; First, there is a function before it
1614 (if (setq len (maxima-back-over-function-name))
1615 (setq indent (+ (min len maxima-function-indent-amount)
1616 tmpchar
1617 (current-column)))
1618 ;; Otherwise,
1619 (setq indent (+ maxima-paren-indent-amount (current-column)))))
1620 ;; If there is something after the (, indent according to that
1622 (forward-char 1)
1623 (skip-chars-forward " \t")
1624 (setq indent (current-column)))))
1625 ;; The construct does not begin with a paren
1627 (setq indent (current-column)))))
1628 ;; Now, we need to possibly do some adjustments.
1629 ;; If the previous column doesn't end with close-char or a
1630 ;; parenthesis, assume that the current line in a continuation
1631 ;; of that line, and add to the indentation, unless the
1632 ;; previous line was the beginning of the construct containing
1633 ;; the point or only an open parenthesis.
1634 (if comma-line
1635 (let ((bol-pt (point))
1636 comma-pt
1637 diff)
1638 (skip-chars-forward " \t")
1639 (setq comma-pt (point))
1640 (forward-char 1)
1641 (skip-chars-forward " \t")
1642 (setq diff (- (point) comma-pt))
1643 (if (> diff indent)
1644 (let ((lineno
1645 (save-excursion
1646 (forward-line 0)
1647 (1+ (count-lines (point-min) (point))))))
1648 (message
1649 (format "Leading comma prevents proper indentation on line %d"
1650 lineno))
1651 (setq indent 0))
1652 (setq indent (- indent diff))))
1653 (maxima-back-over-comment-whitespace)
1654 (when (not (looking-at "^"))
1655 (forward-char -1)
1656 (if (not (or le (looking-at "[,;$]")))
1657 (setq indent (+ maxima-continuation-indent-amount indent))))))))
1658 indent))
1660 (defun maxima-noweb-perhaps-smart-calculate-indent ()
1661 (let ((indent nil)
1662 (pt))
1663 (save-excursion
1664 (beginning-of-line)
1665 (cond
1666 ((looking-at "^<<.*?>>=[ \t]*$")
1667 (setq indent -1))
1668 ((looking-at "^@[ \n]")
1669 (setq indent -1))
1671 (forward-line -1)
1672 (if (looking-at "^<<.*?>>=[ \t]*$")
1673 (setq indent -1)))))
1674 (if indent
1675 indent
1676 (maxima-standard-perhaps-smart-calculate-indent))))
1678 (defun maxima-perhaps-smart-calculate-indent ()
1679 (cond
1680 ((eq maxima-mode-type 'maxima-mode)
1681 (maxima-standard-perhaps-smart-calculate-indent))
1682 ((eq maxima-mode-type 'maxima-noweb-mode)
1683 (maxima-noweb-perhaps-smart-calculate-indent))))
1686 (defun maxima-perhaps-smart-in-comment-p (incomment pmin pt)
1687 "Determine if the point is in a comment or not."
1688 (cond
1689 ;; If we're told it's in a comment, then it is.
1690 (incomment
1692 ;; Otherwise, if pmin is less than point, we're not in a comment
1693 ((> pt pmin)
1694 nil)
1695 ;; Otherwise, we have to check it out
1697 (maxima-in-comment-p))))
1699 (defun maxima-perhaps-smart-comment-calculate-indent ()
1700 "Calculate the indentation of the current line,
1701 which is in a comment which begins on a previous line."
1702 (let ((indent 0)
1703 (blankline nil)
1704 (endcomment nil))
1705 (cond
1706 ((looking-at "^[ \t]*$")
1707 (setq blankline t))
1708 ((looking-at "^[ \t]*\\*/")
1709 (setq endcomment t)))
1710 ;; First of all, if the comment begins with `/*' and nothing
1711 ;; else on the line, don't indent it.
1712 (save-excursion
1713 (maxima-goto-beginning-of-comment)
1714 (cond
1715 ;; Take care of the case when comments won't be indented
1716 ((and
1717 (looking-at "/\\*[ \t]*$")
1718 maxima-dont-reindent-some-comments)
1719 (if blankline
1720 (setq indent (+ (current-column)
1721 maxima-multiline-comment-indent-amount))
1722 (setq indent -1)))
1723 ;; Now, the other cases
1724 ;; If the current line ends a column, indent it like the opening line
1725 (endcomment
1726 (setq indent (current-column)))
1727 ;; If the opening line has a blank after the `/*'
1728 ((looking-at "/\\*[ \t\n]")
1729 (forward-char 2)
1730 (skip-chars-forward " \t")
1731 (if (not (looking-at "\n"))
1732 (setq indent (current-column))
1733 (search-backward "/*")
1734 (setq indent (+ (current-column)
1735 maxima-multiline-comment-indent-amount))))
1737 (setq indent (+ (current-column)
1738 maxima-multiline-comment-indent-amount)))))
1739 indent))
1741 (defun maxima-perhaps-smart-indent-line ()
1742 "Reindent the current line."
1743 (interactive)
1744 (let ((indent (maxima-perhaps-smart-calculate-indent)))
1745 (unless (= indent -1)
1746 (save-excursion
1747 (beginning-of-line)
1748 (delete-horizontal-space)
1749 (indent-to indent)))
1750 (skip-chars-forward " \t")))
1752 ;;;; Indentation according to style
1754 (defun maxima-indent-line ()
1755 (interactive)
1756 (cond
1757 ((eq maxima-newline-style 'basic)
1758 (maxima-standard-indent))
1759 ((eq maxima-indent-style 'standard)
1760 (maxima-standard-indent))
1761 ((eq maxima-indent-style 'perhaps-smart)
1762 (maxima-perhaps-smart-indent-line))))
1764 (defun maxima-change-indent-style (new-style)
1765 "Change the newline style."
1766 (interactive "sNewline style (insert \"b\" for basic, \"s\" for standard, or \"p\" for perhaps-smart): ")
1767 (cond
1768 ((string= new-style "b")
1769 (setq maxima-indent-style 'basic))
1770 ((string= new-style "s")
1771 (setq maxima-indent-style 'standard))
1772 ((string= new-style "p")
1773 (setq maxima-indent-style 'perhaps-smart))))
1775 (defun maxima-return ()
1776 (interactive)
1777 (cond
1778 ((eq maxima-return-style 'newline)
1779 (newline))
1780 ((eq maxima-return-style 'newline-and-indent)
1781 (newline-and-indent))
1782 ((eq maxima-return-style 'reindent-then-newline-and-indent)
1783 (reindent-then-newline-and-indent))))
1785 ;;;; Commenting
1787 (defun maxima-insert-short-comment ()
1788 "Prompt for a comment."
1789 (interactive)
1790 (let ((comment (read-string "Comment: ")))
1791 (insert "/* " comment " */")
1792 (newline-and-indent)))
1794 (defun maxima-insert-long-comment ()
1795 "Insert a comment environment"
1796 (interactive)
1797 (indent-for-tab-command)
1798 (insert "/*")
1799 (newline)
1800 (newline)
1801 (insert "*/")
1802 (indent-for-tab-command)
1803 (forward-line -1)
1804 (indent-for-tab-command))
1806 (defun maxima-uncomment-region (beg end)
1807 "`uncomment-region' to use with the menu."
1808 (interactive "r")
1809 (uncomment-region beg end (universal-argument)))
1811 ;;;; Help functions
1813 (defun maxima-goto-info-node (node)
1814 (if maxima-running-xemacs
1815 (info "Maxima")
1816 (info-other-window (concat "(Maxima)" node))))
1818 (defun maxima-get-info-on-subject (subject &optional same-window)
1819 (info "Maxima")
1820 (Info-menu "Function and Variable Index")
1821 (cond (maxima-running-xemacs
1822 (or (and (search-forward subject nil t)
1823 (Info-follow-nearest-node (point)))
1824 (message (concat "Unable to locate " subject))))
1826 (Info-menu subject))))
1828 (defun maxima-get-help ()
1829 "Get help on a given subject"
1830 (let ((pt)
1831 (place))
1832 (save-excursion
1833 (beginning-of-line)
1834 (skip-chars-forward "* ")
1835 (setq pt (point))
1836 (search-forward ":")
1837 (skip-chars-backward ": ")
1838 (setq name (buffer-substring-no-properties pt (point))))
1839 (maxima-get-info-on-subject name t)))
1840 ; (if (not maxima-running-xemacs)
1841 ; (info-other-window (concat "(Maxima)" place))
1842 ; (info "Maxima")
1843 ; (search-forward place)
1844 ; (Info-follow-nearest-node))
1845 ; (re-search-forward (concat "-.*: *" name "\\>"))
1846 ; (beginning-of-line)))
1848 (defun maxima-help (&optional arg)
1849 (interactive "P")
1850 (let* ((cw (current-word))
1851 (subj (if arg
1853 (completing-read (concat "Maxima help (" cw "): ")
1854 maxima-symbols nil nil nil nil cw))))
1855 (condition-case nil
1856 (maxima-get-info-on-subject subj)
1857 (error nil))))
1859 (defun maxima-help-at-point ()
1860 (interactive)
1861 (maxima-help t))
1863 (defun maxima-apropos (&optional arg)
1864 "Get help on a certain subject"
1865 (interactive "P")
1866 (let* ((cw (current-word))
1867 (expr (if arg
1869 (read-string (concat "Maxima help (" cw "): ")
1870 nil nil cw)))
1871 (maxima-help-buffer
1872 (get-buffer-create (concat "*Maxima Help*")))
1873 (have-info nil)
1874 expr-line
1875 (lmark))
1876 (set-buffer maxima-help-buffer)
1877 (maxima-remove-kill-buffer-hooks)
1878 (setq buffer-read-only nil)
1879 (erase-buffer)
1880 (insert "Maxima help for " expr "\n\n")
1881 (insert "[RET] will get help on the subject on the given line\n")
1882 (insert "q in the *info* buffer will return you here.\n")
1883 (insert "q in this buffer will exit Maxima help\n\n")
1884 (with-temp-buffer
1885 (require 'info)
1886 (Info-mode)
1887 (Info-goto-node "(Maxima)Function and Variable Index")
1888 (goto-char (point-min))
1889 (search-forward "Menu:")
1890 (forward-line 1)
1891 (beginning-of-line)
1892 (while (re-search-forward (concat "\\*.*" expr ".*:") nil t)
1893 (setq have-info t)
1894 (setq expr-line (buffer-substring-no-properties
1895 (maxima-line-beginning-position)
1896 (maxima-line-end-position)))
1897 (save-excursion
1898 (set-buffer maxima-help-buffer)
1899 (insert expr-line "\n"))))
1900 (if have-info
1901 (progn
1902 (set-buffer maxima-help-buffer)
1903 (defun maxima-help-subject ()
1904 (interactive)
1905 (maxima-get-help))
1906 (defun maxima-kill-help ()
1907 (interactive)
1908 (let ((buf (current-buffer)))
1909 (delete-window)
1910 (maxima-remove-kill-buffer-hooks)
1911 (kill-buffer buf)))
1912 (defun maxima-next-subject ()
1913 (interactive)
1914 (forward-char 1)
1915 (if (re-search-forward "^\\*" nil t)
1917 (goto-char (point-min))
1918 (re-search-forward "^\\*" nil t))
1919 (forward-char -1))
1920 (use-local-map (make-sparse-keymap))
1921 (define-key (current-local-map) "\C-m" 'maxima-help-subject)
1922 (define-key (current-local-map) "q" 'maxima-kill-help)
1923 (define-key (current-local-map) "\t" 'maxima-next-subject)
1924 (goto-char (point-min))
1925 (re-search-forward "^\\*")
1926 (forward-char -1)
1927 (pop-to-buffer maxima-help-buffer)
1928 (setq buffer-read-only t))
1929 (kill-buffer maxima-help-buffer)
1930 (message (concat "No help for \"" expr "\"")))))
1932 (defun maxima-apropos-at-point ()
1933 (interactive)
1934 (maxima-apropos t))
1936 (defun maxima-apropos-help ()
1937 (interactive)
1938 (maxima-help-dispatcher nil nil))
1940 (defun maxima-completion-help ()
1941 (interactive)
1942 (maxima-help-dispatcher nil t))
1944 (defun maxima-help-dispatcher (&optional arg1 arg2)
1945 (interactive)
1946 (cond
1947 ((or (looking-at "[a-zA-Z_]")
1948 (looking-at "?[a-zA-Z]")
1949 (looking-at "%[a-zA-Z]"))
1950 (if arg2
1951 (maxima-context-help)
1952 (maxima-help (current-word))))
1953 ((looking-at "?")
1954 (maxima-get-info-on-subject "\"\\?\""))
1955 ((looking-at "#")
1956 (maxima-get-info-on-subject "\"#\""))
1957 ((looking-at "\\.")
1958 (maxima-get-info-on-subject "\"\\.\""))
1959 ((looking-at "[:=!%']")
1960 (let ((expr (buffer-substring-no-properties
1961 (maxima-special-symbol-beginning) (maxima-special-symbol-end))))
1962 (cond
1963 ((or (string= expr "%") (string= expr "%%"))
1964 (maxima-get-info-on-subject expr)) ; % and %% are without double quotes
1965 ((string= expr "''")
1966 (maxima-get-info-on-subject "\"")) ; "''" is called """ in the manual
1967 ((or (string= expr ":") (string= expr "::")
1968 (string= expr ":=") (string= expr "::=")
1969 (string= expr "=") (string= expr "!") (string= expr "!!"))
1970 (maxima-get-info-on-subject (concat "\"" expr "\"")))
1971 (t (error "no help for %s" expr)))))
1972 (arg1
1973 (error "No help for %s" (char-to-string (char-after (point)))))
1974 (t ; point is behind a name?
1975 (save-excursion
1976 (progn
1977 (backward-char 1)
1978 (maxima-help-dispatcher t arg2))))))
1980 (defun maxima-context-help ()
1981 (interactive)
1982 (let* ((stub (current-word))
1983 (completions (all-completions (downcase stub) maxima-symbols)))
1984 (setq completions
1985 (mapcar
1986 (function upcase) completions))
1987 (if (member (upcase stub) completions)
1988 (setq completions (list (upcase stub))))
1989 (cond ((null completions)
1990 (message "No help for %s" stub))
1991 ((= 1 (length completions))
1992 (maxima-get-info-on-subject (car completions)))
1993 (t ; There's no unique completion.
1994 (maxima-help-variation completions)))))
1996 (defun maxima-help-variation (completions)
1997 "Get help on certain subjects."
1998 (let* ((maxima-help-buffer
1999 (get-buffer-create (concat "*Maxima Help*")))
2000 expr-line
2001 (lmark))
2002 (set-buffer maxima-help-buffer)
2003 (erase-buffer)
2004 (insert "Maxima help\n")
2005 (insert "[RET] will get help on the subject on the given line\n")
2006 (insert "q in the *info* buffer will return you here.\n")
2007 (insert "q in this buffer will exit Maxima help\n\n")
2008 (defun maxima-help-insert-line (expr)
2009 (re-search-forward (concat "\\* " expr ":") nil t)
2010 (setq expr-line (buffer-substring-no-properties
2011 (maxima-line-beginning-position)
2012 (maxima-line-end-position)))
2013 (save-excursion
2014 (set-buffer maxima-help-buffer)
2015 (insert expr-line "\n")))
2016 (with-temp-buffer
2017 (require 'info nil t)
2018 (Info-mode)
2019 (Info-goto-node "(Maxima)Function and Variable Index")
2020 (goto-char (point-min))
2021 (search-forward "Menu:")
2022 (forward-line 1)
2023 (beginning-of-line)
2024 (mapcar (function maxima-help-insert-line) completions))
2025 (goto-char (point-min))
2026 (defun maxima-help-subject ()
2027 (interactive)
2028 (maxima-get-help))
2029 (defun maxima-kill-help ()
2030 (interactive)
2031 (let ((buf (current-buffer)))
2032 (delete-window)
2033 (maxima-remove-kill-buffer-hooks)
2034 (kill-buffer buf)))
2035 (use-local-map (append (make-sparse-keymap) (current-local-map)))
2036 (define-key (current-local-map) "\C-m" 'maxima-help-subject)
2037 (define-key (current-local-map) "q" 'maxima-kill-help)
2038 (goto-char 1)
2039 (pop-to-buffer maxima-help-buffer)))
2041 (defun maxima-info ()
2042 "Read the info file for Maxima."
2043 (interactive)
2044 (if maxima-running-xemacs
2045 (info "Maxima")
2046 (info-other-window "Maxima")))
2048 ;;; The help map
2050 (defvar maxima-help-map nil)
2051 (if maxima-help-map
2053 (let ((map (make-sparse-keymap)))
2054 (define-key map "h" 'maxima-help)
2055 (define-key map "d" 'maxima-completion-help)
2056 (define-key map "\C-d" 'maxima-completion-help)
2057 (define-key map "i" 'maxima-info)
2058 (define-key map "\C-i" 'maxima-info)
2059 (define-key map "m" 'maxima-info)
2060 (define-key map "\C-m" 'maxima-info)
2061 (define-key map "a" 'maxima-apropos)
2062 (define-key map "\C-a" 'maxima-apropos)
2063 (define-key map "p" 'maxima-apropos-help)
2064 (define-key map "\C-p" 'maxima-apropos-help)
2065 (setq maxima-help-map map)))
2067 ;;;; Completion
2069 ;;; This next functions are from hippie-expand.el
2070 (defun maxima-he-capitalize-first (str)
2071 (save-match-data
2072 (if (string-match "\\Sw*\\(\\sw\\).*" str)
2073 (let ((res (downcase str))
2074 (no (match-beginning 1)))
2075 (aset res no (upcase (aref str no)))
2076 res)
2077 str)))
2079 (defun maxima-he-ordinary-case-p (str)
2080 (or (string= str (downcase str))
2081 (string= str (upcase str))
2082 (string= str (capitalize str))
2083 (string= str (maxima-he-capitalize-first str))))
2086 (defun maxima-he-transfer-case (from-str to-str)
2087 (cond ((string= from-str (substring to-str 0 (min (length from-str)
2088 (length to-str))))
2089 to-str)
2090 ((not (maxima-he-ordinary-case-p to-str))
2091 to-str)
2092 ((string= from-str (downcase from-str))
2093 (downcase to-str))
2094 ((string= from-str (upcase from-str))
2095 (upcase to-str))
2096 ((string= from-str (maxima-he-capitalize-first from-str))
2097 (maxima-he-capitalize-first to-str))
2098 ((string= from-str (capitalize from-str))
2099 (capitalize to-str))
2101 to-str)))
2103 ;;; The next functions are from comint.el in cvs emacs
2104 (if (and
2105 (not maxima-running-xemacs)
2106 (<= emacs-major-version 21)
2108 (< emacs-major-version 21)
2109 (< emacs-minor-version 3)))
2110 (defun comint-dynamic-list-completions (completions)
2111 "List in help buffer sorted COMPLETIONS.
2112 Typing SPC flushes the help buffer."
2113 (let ((window (get-buffer-window "*Completions*")))
2114 (setq completions (sort completions 'string-lessp))
2115 (if (and (eq last-command this-command)
2116 window (window-live-p window) (window-buffer window)
2117 (buffer-name (window-buffer window))
2118 ;; The above tests are not sufficient to detect the case where we
2119 ;; should scroll, because the top-level interactive command may
2120 ;; not have displayed a completions window the last time it was
2121 ;; invoked, and there may be such a window left over from a
2122 ;; previous completion command with a different set of
2123 ;; completions. To detect that case, we also test that the set
2124 ;; of displayed completions is in fact the same as the previously
2125 ;; displayed set.
2126 (equal completions
2127 (buffer-local-value 'comint-displayed-dynamic-completions
2128 (window-buffer window))))
2129 ;; If this command was repeated, and
2130 ;; there's a fresh completion window with a live buffer,
2131 ;; and this command is repeated, scroll that window.
2132 (with-current-buffer (window-buffer window)
2133 (if (pos-visible-in-window-p (point-max) window)
2134 (set-window-start window (point-min))
2135 (save-selected-window
2136 (select-window window)
2137 (scroll-up))))
2138 ;; Display a completion list for the first time.
2139 (setq comint-dynamic-list-completions-config
2140 (current-window-configuration))
2141 (with-output-to-temp-buffer "*Completions*"
2142 (display-completion-list completions))
2143 (message "Type space to flush; repeat completion command to scroll"))
2145 ;; Read the next key, to process SPC.
2146 (let (key first)
2147 (if (with-current-buffer (get-buffer "*Completions*")
2148 (set (make-local-variable 'comint-displayed-dynamic-completions)
2149 completions)
2150 (setq key (read-key-sequence nil)
2151 first (aref key 0))
2152 (and (consp first) (consp (event-start first))
2153 (eq (window-buffer (posn-window (event-start first)))
2154 (get-buffer "*Completions*"))
2155 (eq (key-binding key) 'mouse-choose-completion)))
2156 ;; If the user does mouse-choose-completion with the mouse,
2157 ;; execute the command, then delete the completion window.
2158 (progn
2159 (mouse-choose-completion first)
2160 (set-window-configuration comint-dynamic-list-completions-config))
2161 (unless (eq first ?\ )
2162 (setq unread-command-events (listify-key-sequence key)))
2163 (unless (eq first ?\t)
2164 (set-window-configuration comint-dynamic-list-completions-config)))))))
2167 (defun maxima-complete-symbol ()
2168 "Complete word from list of candidates.
2169 A completions listing will be shown in a help buffer
2170 if completion is ambiguous."
2171 (let* ((comint-completion-addsuffix nil)
2172 (stub (buffer-substring-no-properties
2173 (maxima-name-beginning) (point)))
2174 (completions (all-completions (downcase stub) maxima-symbols)))
2175 (comint-dynamic-simple-complete stub completions)))
2177 (defun maxima-complete-filename ()
2178 "Complete the filename."
2179 (comint-dynamic-complete-filename))
2181 (defun maxima-complete ()
2182 "Complete the current object, depending on context."
2183 (interactive)
2184 (let* ((pmin (maxima-form-beginning-position))
2185 (pps (parse-partial-sexp pmin (point))))
2186 (cond
2187 ;; complete filename if the point is in a string
2188 ((nth 3 pps)
2189 (maxima-complete-filename))
2190 ;; Otherwise, complete the symbol
2192 (maxima-complete-symbol)))))
2194 ;; ;;; Use hippie-expand to help with completions
2195 ;; (require 'hippie-exp)
2197 ;; (defun maxima-he-try (old)
2198 ;; (interactive)
2199 ;; (if (not old)
2200 ;; ;;; let beg be the beginning of the word
2201 ;; (progn
2202 ;; (he-init-string (maxima-name-beginning) (point))
2203 ;; (setq he-expand-list
2204 ;; (all-completions (downcase he-search-string) maxima-symbols))
2205 ;; (setq he-expand-list
2206 ;; (mapcar (function
2207 ;; (lambda (x) (he-transfer-case he-search-string x)))
2208 ;; he-expand-list))
2209 ;; (if he-expand-list
2210 ;; (he-substitute-string (car he-expand-list))
2211 ;; nil))
2212 ;; (setq he-expand-list (cdr he-expand-list))
2213 ;; (if he-expand-list
2214 ;; (he-substitute-string (car he-expand-list))
2215 ;; (he-reset-string)
2216 ;; nil)))
2218 ;; (fset 'maxima-dynamic-complete
2219 ;; (make-hippie-expand-function '(maxima-he-try)))
2221 ;;;; Miscellaneous
2223 (defun maxima-mark-form ()
2224 "Make the current form as the region."
2225 (interactive)
2226 (maxima-goto-beginning-of-form)
2227 (set-mark (maxima-form-end-position-or-point-max)))
2229 (defun maxima-check-commas (beg end)
2230 "Check to see if there is a stray comma at the beginning or end."
2231 (let ((commapt nil))
2232 (save-excursion
2233 (goto-char beg)
2234 (maxima-forward-over-comment-whitespace)
2235 (if (looking-at ",")
2236 (setq commapt (point))
2237 (goto-char end)
2238 (maxima-back-over-comment-whitespace)
2239 (when (save-excursion
2240 (forward-char -1)
2241 (looking-at "[;$]"))
2242 (forward-char -1)
2243 (maxima-back-over-comment-whitespace))
2244 (forward-char -1)
2245 (if (looking-at ",")
2246 (setq commapt (point)))))
2247 (if commapt
2248 (progn
2249 (message "Misplaced comma")
2250 (goto-char commapt)
2251 nil)
2252 t)))
2254 (defun maxima-check-parens (beg end)
2255 "Check to make sure that the parentheses are balanced in the region."
2256 (interactive "r")
2257 (let* ((tmpfile (maxima-make-temp-name))
2258 (tmpbuf (get-buffer-create tmpfile))
2259 (string (buffer-substring-no-properties beg end))
2260 (keep-going t)
2261 (match)
2262 (pt)
2263 (errmessage nil)
2264 (parenstack nil))
2265 (save-excursion
2266 (set-buffer tmpbuf)
2267 (maxima-mode)
2268 (maxima-remove-kill-buffer-hooks)
2269 (modify-syntax-entry ?/ ". 14")
2270 (modify-syntax-entry ?* ". 23")
2271 (insert string)
2272 (goto-char (point-min))
2273 (while (and (not errmessage)
2274 (setq match (maxima-re-search-forward "[][()]" end)))
2275 (unless (save-excursion
2276 (forward-char -1)
2277 (maxima-escaped-char-p))
2278 (cond
2279 ((string= match "(")
2280 (setq parenstack (cons (cons 1 (1- (point))) parenstack)))
2281 ((string= match "[")
2282 (setq parenstack (cons (cons 2 (1- (point))) parenstack)))
2283 ((string= match ")")
2284 (cond
2285 ((not parenstack)
2286 (setq errmessage "Unmatched close parenthesis")
2287 (setq pt (1- (point))))
2288 ((= (caar parenstack) 1)
2289 (setq parenstack (cdr parenstack)))
2290 ((= (caar parenstack) 2)
2291 (setq errmessage "Open bracket closed by parenthesis")
2292 (setq pt (1- (point))))))
2293 ((string= match "]")
2294 (cond
2295 ((not parenstack)
2296 (setq errmessage "Unmatched close bracket")
2297 (setq pt (1- (point))))
2298 ((= (caar parenstack) 2)
2299 (setq parenstack (cdr parenstack)))
2300 ((= (caar parenstack) 1)
2301 (setq errmessage "Open parenthesis closed by bracket")
2302 (setq pt (1- (point))))))))))
2303 (kill-buffer tmpbuf)
2304 (cond
2305 ((not (or parenstack errmessage))
2306 ; (message "Parenthesis and brackets match")
2308 (errmessage
2309 (message errmessage)
2310 (goto-char (1- (+ beg pt)))
2311 nil)
2313 (cond
2314 ((= (caar parenstack) 1)
2315 (message "Unmatched open parenthesis")
2316 (goto-char (1- (+ beg (cdar parenstack))))
2317 nil)
2319 (message "Unmatched open bracket")
2320 (goto-char (+ beg (cdar parenstack)))
2321 nil))))))
2324 (defun maxima-check-form-parens ()
2325 "Check to see if the parentheses in the current form are balanced."
2326 (interactive)
2327 (maxima-check-parens (maxima-form-beginning-position)
2328 (maxima-form-end-position-or-point-max)))
2330 (defun maxima-load-file (file)
2331 "Prompt for a Maxima file to load."
2332 (interactive "fMaxima file: ")
2333 (maxima-string (concat "load(\"" (expand-file-name file) "\");")))
2335 (defun maxima-load-current-file ()
2336 "Load the current file into Maxima."
2337 (interactive)
2338 (maxima-string (concat "load(\"" buffer-file-name "\");")))
2340 ;;; For highlighting the region being sent
2342 (defun maxima-mode-add-highlight ()
2343 (maxima-mode-remove-highlight)
2344 (if (and maxima-mode-region-begin maxima-mode-region-end)
2345 (if maxima-running-xemacs
2346 (progn
2347 (setq maxima-mode-highlight
2348 (make-extent
2349 maxima-mode-region-begin
2350 maxima-mode-region-end))
2351 (set-extent-property maxima-mode-highlight 'face 'highlight))
2352 (setq maxima-mode-highlight
2353 (make-overlay
2354 maxima-mode-region-begin
2355 maxima-mode-region-end))
2356 (overlay-put maxima-mode-highlight 'face 'highlight)))
2357 (setq maxima-mode-region-begin nil)
2358 (setq maxima-mode-region-end nil))
2360 (defun maxima-mode-remove-highlight ()
2361 (when maxima-mode-highlight
2362 (if maxima-running-xemacs
2363 (delete-extent maxima-mode-highlight)
2364 (delete-overlay maxima-mode-highlight))
2365 (setq maxima-mode-highlight nil)))
2367 (defun maxima-mode-add-remove-highlight ()
2368 (if (or
2369 (eq this-command 'maxima-send-region)
2370 (eq this-command 'maxima-send-buffer)
2371 (eq this-command 'maxima-send-line)
2372 (eq this-command 'maxima-send-form)
2373 (eq this-command 'maxima-send-previous-form)
2374 (eq this-command 'maxima-send-previous-form-and-goto-end-of-form)
2375 (eq this-command 'maxima-send-full-line)
2376 (eq this-command 'maxima-send-full-line-and-goto-next-form)
2377 (eq this-command 'maxima-send-completed-region)
2378 (eq this-command 'maxima-send-completed-region-and-goto-next-form)
2379 (eq this-command 'maxima-minibuffer-on-region)
2380 (eq this-command 'maxima-minibuffer-on-form)
2381 (eq this-command 'maxima-minibuffer-on-line))
2382 (maxima-mode-add-highlight)
2383 (maxima-mode-remove-highlight)))
2385 ;;;; Syntax table
2387 (defvar maxima-mode-syntax-table nil "")
2389 (if (not maxima-mode-syntax-table)
2390 (let ((i 0))
2391 (setq maxima-mode-syntax-table (make-syntax-table))
2392 (modify-syntax-entry ?_ "w" maxima-mode-syntax-table)
2393 (modify-syntax-entry ?% "w" maxima-mode-syntax-table)
2394 (modify-syntax-entry ?? "w" maxima-mode-syntax-table)
2395 ; (modify-syntax-entry ?\_ "w" maxima-mode-syntax-table)
2396 ;; (while (< i ?0)
2397 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2398 ;; (setq i (1+ i)))
2399 ;; (setq i (1+ ?9))
2400 ;; (while (< i ?A)
2401 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2402 ;; (setq i (1+ i)))
2403 ;; (setq i (1+ ?Z))
2404 ;; (while (< i ?a)
2405 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2406 ;; (setq i (1+ i)))
2407 ;; (setq i (1+ ?z))
2408 ;; (while (< i 128)
2409 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2410 ;; (setq i (1+ i)))
2411 (modify-syntax-entry ? " " maxima-mode-syntax-table)
2412 (modify-syntax-entry ?\t " " maxima-mode-syntax-table)
2413 (modify-syntax-entry ?` "' " maxima-mode-syntax-table)
2414 (modify-syntax-entry ?' "' " maxima-mode-syntax-table)
2415 (modify-syntax-entry ?, "' " maxima-mode-syntax-table)
2416 (modify-syntax-entry ?. "w" maxima-mode-syntax-table)
2417 (modify-syntax-entry ?# "' " maxima-mode-syntax-table)
2418 (modify-syntax-entry ?\\ "\\" maxima-mode-syntax-table)
2419 (modify-syntax-entry ?/ ". 14" maxima-mode-syntax-table)
2420 (modify-syntax-entry ?* ". 23" maxima-mode-syntax-table)
2421 (modify-syntax-entry ?+ "." maxima-mode-syntax-table)
2422 (modify-syntax-entry ?- "." maxima-mode-syntax-table)
2423 (modify-syntax-entry ?= "." maxima-mode-syntax-table)
2424 (modify-syntax-entry ?< "." maxima-mode-syntax-table)
2425 (modify-syntax-entry ?> "." maxima-mode-syntax-table)
2426 (modify-syntax-entry ?& "." maxima-mode-syntax-table)
2427 (modify-syntax-entry ?| "." maxima-mode-syntax-table)
2428 (modify-syntax-entry ?\" "\" " maxima-mode-syntax-table)
2429 (modify-syntax-entry ?\\ "\\ " maxima-mode-syntax-table)
2430 (modify-syntax-entry ?\( "() " maxima-mode-syntax-table)
2431 (modify-syntax-entry ?\) ")( " maxima-mode-syntax-table)
2432 (modify-syntax-entry ?\[ "(] " maxima-mode-syntax-table)
2433 (modify-syntax-entry ?\] ")[ " maxima-mode-syntax-table)))
2436 ;;;; Keymap
2438 (defvar maxima-mode-map nil
2439 "The keymap for maxima-mode")
2441 (if maxima-mode-map
2443 (let ((map (make-sparse-keymap)))
2444 ;; Motion
2445 (define-key map "\M-\C-a" 'maxima-goto-beginning-of-form-interactive)
2446 (define-key map "\M-\C-e" 'maxima-goto-end-of-form-interactive)
2447 (define-key map "\M-\C-b" 'maxima-goto-beginning-of-list-interactive)
2448 (define-key map "\M-\C-f" 'maxima-goto-end-of-list-interactive)
2449 ;; Process
2450 (define-key map "\C-c\C-p" 'maxima-display-buffer)
2451 (define-key map "\C-c\C-r" 'maxima-send-region)
2452 (define-key map "\C-c\C-b" 'maxima-send-buffer)
2453 (define-key map "\C-c\C-c" 'maxima-send-line)
2454 (define-key map "\C-c\C-e" 'maxima-send-previous-form)
2455 (define-key map "\C-c\C-s" 'maxima-send-previous-form-and-goto-end-of-form)
2456 (define-key map [(control return)]
2457 'maxima-send-full-line-and-goto-next-form)
2458 (define-key map [(meta return)]
2459 'maxima-send-completed-region-and-goto-next-form)
2460 (define-key map [(control meta return)] 'maxima-send-buffer)
2461 (define-key map "\C-c\C-k" 'maxima-stop)
2462 (define-key map "\C-c\C-q" 'maxima-clear-queue)
2463 (define-key map "\C-c\C-l" 'maxima-load-file)
2464 (define-key map "\C-c\C-f" 'maxima-load-current-file)
2465 ;; Completion
2466 ;(if maxima-use-dynamic-complete
2467 ; (define-key map (kbd "M-TAB") 'maxima-dynamic-complete)
2468 (define-key map (kbd "M-TAB") 'maxima-complete)
2469 ;; Commenting
2470 (define-key map "\C-c;" 'comment-region)
2471 (define-key map "\C-c:" 'maxima-uncomment-region)
2472 (define-key map "\M-;" 'maxima-insert-short-comment)
2473 (define-key map "\C-c*" 'maxima-insert-long-comment)
2474 ;; Indentation
2475 ; (define-key map "\t" 'maxima-reindent-line)
2476 (define-key map "\C-m" 'maxima-return)
2477 (define-key map "\M-\C-q" 'maxima-indent-form)
2478 ; (define-key map [(control tab)] 'maxima-untab)
2479 ;; Help
2480 (define-key map "\C-c\C-d" maxima-help-map)
2481 (define-key map [(f12)] 'maxima-help)
2482 (define-key map [(meta f12)] 'maxima-apropos)
2483 ;; Minibuffer
2484 (define-key map "\C-c\C-nr" 'maxima-minibuffer-on-region)
2485 (define-key map "\C-c\C-nl" 'maxima-minibuffer-on-line)
2486 (define-key map "\C-c\C-nf" 'maxima-minibuffer-on-form)
2487 ;; Misc
2488 (define-key map "\M-h" 'maxima-mark-form)
2489 (define-key map "\C-c\)" 'maxima-check-parens)
2490 (define-key map [(control c) (control \))] 'maxima-check-form-parens)
2491 ; (define-key map "\C-cC-\)" 'maxima-check-form-parens)
2492 (define-key map "\177" 'backward-delete-char-untabify)
2493 (setq maxima-mode-map map)))
2495 ;;;; Menu
2497 (easy-menu-define maxima-mode-menu maxima-mode-map "Maxima mode menu"
2498 '("Maxima"
2499 ("Motion"
2500 ["Beginning of form" maxima-goto-beginning-of-form-interactive t]
2501 ["End of form" maxima-goto-end-of-form-interactive t]
2502 ["Beginning of sexp" maxima-goto-beginning-of-list-interactive t]
2503 ["End of sexp" maxima-goto-end-of-list-interactive t])
2504 ("Process"
2505 ["Start process" maxima-start t]
2506 ["Send region" maxima-send-region t]
2507 ["Send buffer" maxima-send-buffer t]
2508 ["Send line" maxima-send-line t]
2509 ["Send form" maxima-send-form t]
2510 ["Load file" maxima-load-file t]
2511 "----"
2512 ["Display buffer" maxima-display-buffer t]
2513 "----"
2514 ["Kill process" maxima-stop t])
2515 ("Indentation"
2516 ; ["Change to basic" (maxima-change-indent-style "b")
2517 ; (not (eq maxima-newline-style 'basic))]
2518 ["Change to standard" (maxima-change-indent-style "s")
2519 (not (eq maxima-indent-style 'standard))]
2520 ["Change to smart" (maxima-change-indent-style "p")
2521 (not (eq maxima-indent-style 'perhaps-smart))])
2522 ("Misc"
2523 ["Mark form" maxima-mark-form t]
2524 ["Check parens in region" maxima-check-parens t]
2525 ["Check parens in form" maxima-check-form-parens t]
2526 ["Comment region" comment-region t]
2527 ["Uncomment region" maxima-uncomment-region t])
2528 ("Help"
2529 ["Maxima info" maxima-info t]
2530 ["Help" maxima-help t])))
2533 ;;;; Variable setup
2534 ;;;; (These are used in both maxima-mode and inferior-maxima-mode).
2536 (defvar maxima-mode-abbrev-table nil "")
2538 (defun maxima-mode-variables ()
2539 "Sets all the necessary variables for maxima-mode"
2540 (set-syntax-table maxima-mode-syntax-table)
2541 (setq local-abbrev-table maxima-mode-abbrev-table)
2542 (make-local-variable 'paragraph-start)
2543 (setq paragraph-start (concat "^$\\|" page-delimiter))
2544 (make-local-variable 'paragraph-separate)
2545 (setq paragraph-separate paragraph-start)
2546 (make-local-variable 'indent-line-function)
2547 (setq indent-line-function 'maxima-indent-line)
2548 (make-local-variable 'indent-tabs-mode)
2549 (unless maxima-use-tabs
2550 (setq indent-tabs-mode nil))
2551 (make-local-variable 'case-fold-search)
2552 (setq case-fold-search t)
2553 (make-local-variable 'comment-start)
2554 (setq comment-start "/*")
2555 (make-local-variable 'comment-end)
2556 (setq comment-end "*/")
2557 (make-local-variable 'comment-start-skip)
2558 (setq comment-start-skip "/\\*+ *")
2559 (make-local-variable 'comment-column)
2560 (setq comment-column 40)
2561 (make-local-variable 'comment-indent-function)
2562 (setq comment-indent-function 'comment-indent-default)
2563 (setq imenu-generic-expression
2564 (list '(nil "^ *\\([a-zA-Z0-9_]*\\) *(.*) *:=" 1))))
2567 ;;;; Maxima mode
2569 (defun maxima-mode ()
2570 "Major mode for editing Maxima code.
2572 Maxima mode provides the following motion commands:
2573 \\[maxima-goto-beginning-of-form-interactive]: Move to the beginning of the form.
2574 \\[maxima-goto-end-of-form-interactive]: Move to the end of the form.
2575 \\[maxima-goto-beginning-of-list-interactive]: Move to the beginning of the sexp.
2576 \\[maxima-goto-end-of-list-interactive]: Move to the end of the sexp.
2578 and the following miscellaneous commands.
2579 \\[maxima-mark-form]: Mark the current form
2580 \\[maxima-check-parens]: Check the current region for balanced parentheses.
2581 \\[maxima-check-form-parens]: Check the current form for balanced parentheses.
2583 Maxima mode has the following completions commands:
2584 M-TAB: Complete the Maxima symbol as much as possible, providing
2585 a completion buffer if there is more than one possible completion.
2587 Portions of the buffer can be sent to a Maxima process. (If a process is
2588 not running, one will be started.)
2589 \\[maxima-send-region]: Send the region to Maxima.
2590 \\[maxima-send-buffer]: Send the buffer to Maxima.
2591 \\[maxima-send-line]: Send the line to Maxima.
2592 \\[maxima-send-form]: Send the form to Maxima.
2593 \\[maxima-send-full-line-and-goto-next-form]: Send the smallest set of lines which contains
2594 the cursor and contains no incomplete forms, and go to the next form.
2595 \\[maxima-send-completed-region-and-goto-next-form]: As above, but with
2596 the region instead of the current line.
2597 \\[maxima-load-file] will prompt for a filename and load it into Maxima
2598 When something is sent to Maxima, a buffer running an inferior Maxima
2599 process will appear. It can also be made to appear by using the command
2600 \\[maxima-display-buffer].
2601 If an argument is given to a command to send information to Maxima,
2602 the region (buffer, line, form) will first be checked to make sure
2603 the parentheses are balanced.
2604 The Maxima process can be killed, after asking for confirmation
2605 with \\[maxima-stop]. To kill without confirmation, give \\[maxima-stop]
2606 an argument.
2608 By default, indentation will be to the same level as the
2609 previous line, with an additional space added for open parentheses.
2610 The behaviour of indent can be changed by the command
2611 \\[maxima-change-indent-style]. The possibilities are:
2612 Standard: Standard indentation.
2613 Perhaps smart: Tries to guess an appropriate indentation, based on
2614 open parentheses, \"do\" loops, etc.
2615 The default can be set by setting the value of the variable
2616 \"maxima-indent-style\" to either 'standard or 'perhaps-smart.
2617 In both cases, \\[maxima-untab] will remove a level of indentation.
2619 To get help on a Maxima topic, use:
2620 \\[maxima-help].
2621 To read the Maxima info manual, use:
2622 \\[maxima-info].
2623 To get help with the symbol under point, use:
2624 \\[maxima-completion-help].
2625 To get apropos with the symbol under point, use:
2626 \\[maxima-apropos-help].
2628 \\{maxima-mode-map}
2630 (interactive)
2631 (kill-all-local-variables)
2632 (setq major-mode 'maxima-mode)
2633 (setq mode-name "Maxima")
2634 (use-local-map maxima-mode-map)
2635 (maxima-mode-variables)
2636 (cond
2637 ((eq maxima-newline-style 'basic)
2638 (setq maxima-indent-style 'standard))
2639 ((eq maxima-newline-style 'standard)
2640 (setq maxima-indent-style 'standard))
2641 ((eq maxima-newline-style 'perhaps-smart)
2642 (setq maxima-indent-style 'perhaps-smart)))
2643 (easy-menu-add maxima-mode-menu maxima-mode-map)
2644 (if maxima-running-xemacs
2645 (add-local-hook 'post-command-hook
2646 'maxima-mode-add-remove-highlight)
2647 (add-hook 'post-command-hook
2648 'maxima-mode-add-remove-highlight nil t))
2649 (run-hooks 'maxima-mode-hook))
2651 (define-derived-mode maxima-noweb-mode maxima-mode
2652 "Maxima Noweb Mode"
2653 (setq maxima-mode-type 'maxima-noweb-mode))
2655 ;;;; Interacting with the Maxima process
2657 ;;; Checking on the process
2658 (defun inferior-maxima-running ()
2659 (and (processp inferior-maxima-process)
2660 (eq (process-status inferior-maxima-process) 'run)))
2662 ;;; Sending the information
2663 (defun inferior-maxima-get-old-input ()
2664 (let (pt pt1)
2665 (save-excursion
2666 (if (re-search-forward
2667 (concat "\\(^(\\(" maxima-outchar "\\|" maxima-linechar "\\)[0-9]*) \\)")
2668 nil 1)
2669 (goto-char (match-beginning 0)))
2670 (skip-chars-backward " \t\n")
2671 (setq pt (point)))
2672 (save-excursion
2673 (re-search-backward inferior-maxima-prompt)
2674 (setq pt1 (match-end 0)))
2675 (buffer-substring-no-properties pt1 pt)))
2677 (defun inferior-maxima-comint-send-input (&optional query)
2678 "Take note of position, then send the input"
2679 (unless query
2680 (setq inferior-maxima-input-end (point)))
2681 (setq inferior-maxima-waiting-for-output t)
2682 (comint-send-input))
2684 ;;; This next function is a modified version of comint-strip-ctrl-m
2685 (defun inferior-maxima-remove-double-prompt (&optional string)
2686 "Fix the double prompt that occasionally appears in XEmacs."
2687 (let ((pmark (process-mark inferior-maxima-process))
2688 (pos))
2689 (set-buffer (process-buffer inferior-maxima-process))
2690 (setq pos comint-last-output-start)
2691 (if (marker-position pos)
2692 (save-excursion
2693 (goto-char pos)
2694 (beginning-of-line)
2695 (while (re-search-forward
2696 (concat "(" maxima-inchar "[0-9]+).*\r") pmark t)
2697 (replace-match "" t t))))))
2699 (defun inferior-maxima-remove-double-input-prompt (&optional string)
2700 "Fix the double prompt that occasionally appears in Emacs."
2701 (let ((pmark (process-mark inferior-maxima-process))
2702 (pos))
2703 (save-excursion
2704 (set-buffer (process-buffer inferior-maxima-process))
2705 (goto-char inferior-maxima-input-end)
2706 (forward-line 1)
2707 (if (looking-at (concat "(" maxima-inchar "[0-9]+)"))
2708 (kill-line 1))
2709 (if (looking-at " ")
2710 (delete-char 1)))))
2712 ;;; This next function will replace tabs in the output by spaces
2713 ;; untabify isn't defined in xemacs
2714 (unless (fboundp 'untabify)
2715 (defun untabify (start end)
2716 "Convert all tabs in region to multiple spaces, preserving columns.
2717 Called non-interactively, the region is specified by arguments
2718 START and END, rather than by the position of point and mark.
2719 The variable `tab-width' controls the spacing of tab stops."
2720 (interactive "r")
2721 (save-excursion
2722 (save-restriction
2723 (narrow-to-region (point-min) end)
2724 (goto-char start)
2725 (while (search-forward "\t" nil t) ; faster than re-search
2726 (forward-char -1)
2727 (let ((tab-beg (point))
2728 (indent-tabs-mode nil)
2729 column)
2730 (skip-chars-forward "\t")
2731 (setq column (current-column))
2732 (delete-region tab-beg (point))
2733 (indent-to column)))))))
2735 (defun inferior-maxima-replace-tabs-by-spaces (&optional string)
2736 "Replace tabs in the Maxima output by spaces."
2737 (let ((beg))
2738 (set-buffer (process-buffer inferior-maxima-process))
2739 (if (marker-position comint-last-output-start)
2740 (setq beg comint-last-output-start)
2741 (setq beg (point-min)))
2742 (untabify beg
2743 (process-mark inferior-maxima-process))))
2745 (defun inferior-maxima-wait-for-output ()
2746 "Wait for output from the Maxima process."
2747 (when (and
2748 inferior-maxima-waiting-for-output
2749 (inferior-maxima-running))
2750 (accept-process-output inferior-maxima-process))
2751 (if maxima-running-xemacs
2752 (sleep-for 0.1)
2753 (sit-for 0 inferior-maxima-after-output-wait)))
2755 (defun inferior-maxima-output-filter (str)
2756 "Look for a new input prompt"
2757 (cond ((and
2758 (string-match "? *$" str)
2759 (not (string-match (concat "(" maxima-outchar "[0-9]+)") str)))
2760 (maxima-ask-question str))
2761 ((string-match inferior-maxima-prompt str)
2762 (if (and inferior-maxima-process (not (string= maxima-block "")))
2763 (maxima-single-string (maxima-get-command))
2764 (if (not inferior-maxima-process)
2765 (maxima-clear-queue))
2766 (setq inferior-maxima-waiting-for-output nil)))))
2768 (defun inferior-maxima-sentinel (proc state)
2769 "Write the input history when the process ends"
2770 (unless (string-match "^run" state)
2771 (comint-write-input-ring)))
2773 (defun maxima-start ()
2774 "Start the Maxima process."
2775 (interactive)
2776 (if (processp inferior-maxima-process)
2777 (unless (eq (process-status inferior-maxima-process) 'run)
2778 (delete-process inferior-maxima-process)
2779 (if (get-buffer "*maxima*")
2780 (save-excursion
2781 (set-buffer "*maxima*")
2782 (erase-buffer)))
2783 (setq inferior-maxima-process nil)))
2784 (unless (processp inferior-maxima-process)
2785 (setq inferior-maxima-input-end 0)
2786 (setq inferior-maxima-waiting-for-output t)
2787 (let ((mbuf)
2788 (cmd))
2789 (setq mbuf (apply #'make-comint "maxima" maxima-command nil maxima-args))
2790 (save-excursion
2791 (set-buffer mbuf)
2792 (setq inferior-maxima-process (get-buffer-process mbuf))
2793 (add-hook 'comint-output-filter-functions
2794 'inferior-maxima-output-filter nil t)
2795 (add-hook 'comint-output-filter-functions
2796 'inferior-maxima-replace-tabs-by-spaces nil t)
2797 ; (add-hook 'comint-output-filter-functions
2798 ; 'inferior-maxima-remove-double-input-prompt nil t)
2799 (if maxima-fix-double-prompt
2800 (add-hook 'comint-output-filter-functions
2801 'inferior-maxima-remove-double-prompt nil t))
2802 (inferior-maxima-wait-for-output)
2803 (inferior-maxima-mode)))))
2805 (defun maxima-stop (&optional arg)
2806 "Kill the currently running Maxima process."
2807 (interactive "P")
2808 (if (processp inferior-maxima-process)
2809 (if arg
2810 (progn
2811 (delete-process inferior-maxima-process)
2812 (kill-buffer "*maxima*")
2813 (setq inferior-maxima-process nil))
2814 (if (y-or-n-p "Really quit Maxima? ")
2815 (progn
2816 (delete-process inferior-maxima-process)
2817 (kill-buffer "*maxima*")
2818 (setq inferior-maxima-process nil))))))
2820 ;;; Sending information to the process
2822 (defun maxima-single-string (string)
2823 "Send a string to the Maxima process."
2824 (setq string (maxima-strip-string-add-semicolon string))
2825 (maxima-start)
2826 ; (inferior-maxima-wait-for-output)
2827 (save-current-buffer
2828 (set-buffer (process-buffer inferior-maxima-process))
2829 (goto-char (point-max))
2830 (let ((start (point)))
2831 (insert string)
2832 (untabify start (point)))
2833 (goto-char (point-max))
2834 (inferior-maxima-comint-send-input)
2835 (goto-char (point-max))))
2837 (defun maxima-ask-question (string)
2838 "Ask the question maxima wants answered."
2839 (let ((ans (read-string
2840 (concat (maxima-strip-string string) " " ))))
2841 (unless (string-match "[;$]" ans)
2842 (setq ans (concat ans ";")))
2843 (setq ans (maxima-strip-string ans))
2844 (save-current-buffer
2845 (set-buffer (process-buffer inferior-maxima-process))
2846 (goto-char (point-max))
2847 (insert ans)
2848 (inferior-maxima-comint-send-input t)
2849 (goto-char (point-max)))))
2851 (defun maxima-get-command (&optional arg)
2852 "Return the maxima command that's at the front of maxima-block.
2853 Remove it from the front of maxima-block.
2854 With an argument, use maxima-block-wait instead of maxima-block."
2855 (let* ((tmpfile (maxima-make-temp-name))
2856 (tmpbuf (get-buffer-create tmpfile))
2857 (pt)
2858 (command))
2859 (save-excursion
2860 (set-buffer tmpbuf)
2861 (maxima-remove-kill-buffer-hooks)
2862 (if arg
2863 (insert maxima-block-wait)
2864 (insert maxima-block))
2865 (goto-char (point-min))
2866 (maxima-forward-over-comment-whitespace)
2867 (setq pt (point))
2868 (if (string-match "[$;]\\|:lisp"
2869 (buffer-substring-no-properties (point) (point-max)))
2870 (progn
2871 (if (looking-at ":lisp")
2872 (progn
2873 (search-forward ":lisp")
2874 (forward-sexp)
2875 (setq command (buffer-substring-no-properties pt (point))))
2876 (maxima-goto-end-of-form)
2877 (setq command (buffer-substring-no-properties pt (point))))
2878 (maxima-forward-over-comment-whitespace)
2879 (if arg
2880 (setq maxima-block-wait
2881 (maxima-strip-string-add-semicolon
2882 (buffer-substring-no-properties (point) (point-max))))
2883 (setq maxima-block
2884 (maxima-strip-string-add-semicolon
2885 (buffer-substring-no-properties (point) (point-max)))))
2886 (setq command (buffer-substring-no-properties pt (point))))
2887 (if arg
2888 (setq maxima-block-wait "")
2889 (setq maxima-block "")))
2890 (if arg
2891 (if (string= maxima-block-wait ";") (setq maxima-block-wait ""))
2892 (if (string= maxima-block ";") (setq maxima-block "")))
2893 (kill-buffer tmpbuf))
2894 command))
2896 (defun maxima-send-block (stuff)
2897 "Send a block of code to Maxima."
2898 (maxima-start)
2899 (setq stuff (maxima-strip-string-add-semicolon stuff))
2900 ; (unless (string-match (substring stuff -1) ";$")
2901 ; (setq stuff (concat stuff ";")))
2902 (if (string= maxima-block "")
2903 (progn
2904 (setq maxima-block stuff)
2905 (maxima-single-string (maxima-get-command)))
2906 (setq maxima-block (concat maxima-block stuff))))
2908 (defun maxima-send-block-wait (stuff)
2909 "Send a block of code to Maxima; wait for it to finish.
2910 Return the last string sent."
2911 (maxima-start)
2912 (if (not (string= maxima-block ""))
2913 (message "Maxima process currently busy.")
2914 (setq maxima-block-wait (maxima-strip-string-add-semicolon stuff))
2915 (while (not (string= maxima-block-wait ""))
2916 (maxima-single-string-wait (maxima-get-command t)))))
2918 (defun maxima-clear-queue ()
2919 "Clear out the queue of commands to send to the maxima process."
2920 (interactive)
2921 (setq maxima-block "")
2922 (setq maxima-block-wait ""))
2924 ;;; Getting information back from Maxima.
2926 (defun maxima-last-output ()
2927 "Get the most recent output from Maxima."
2928 (interactive)
2929 (inferior-maxima-wait-for-output)
2930 (save-excursion
2931 (set-buffer (process-buffer inferior-maxima-process))
2932 (let* ((pt (point))
2933 (pmark (progn (goto-char (process-mark inferior-maxima-process))
2934 (forward-line 0)
2935 (point-marker)))
2936 (beg (progn
2937 (goto-char inferior-maxima-input-end)
2938 (forward-line 1)
2939 (point)))
2940 (output (buffer-substring-no-properties beg pmark)))
2941 (goto-char pt)
2942 output)))
2944 (defun maxima-last-output-noprompt ()
2945 "Return the last Maxima output, without the prompts"
2946 (interactive)
2947 (if (not (inferior-maxima-running))
2948 (maxima-last-output)
2949 (let* ((output (maxima-last-output))
2950 (newstring)
2951 (i 0)
2952 (beg)
2953 (end)
2954 (k))
2955 ;; Replace the output prompt with spaces
2956 (setq beg (string-match
2957 (concat "\\(^(" maxima-outchar "[0-9]*) \\)") output))
2958 (if (not beg)
2959 output
2960 (setq end (1+ (string-match ")" output beg)))
2961 (setq newstring (substring output 0 beg))
2962 (setq k (- end beg))
2963 (while (< i k)
2964 (setq newstring (concat newstring " "))
2965 (setq i (1+ i)))
2966 (concat newstring
2967 (substring output
2968 end))))))
2970 (defun maxima-last-output-tex-noprompt ()
2971 "Return the last Maxima output, between the dollar signs."
2972 (interactive)
2973 (let* ((output (maxima-last-output))
2974 (begtex (string-match "\\$\\$" output))
2975 (endtex (string-match "\\$\\$" output (1+ begtex))))
2976 (concat
2977 (substring output begtex (+ endtex 2))
2978 "\n")))
2981 ;;; Sending information to the process should be done through these
2982 ;; next five commands
2984 (defun maxima-single-string-wait (string)
2985 "Send a single string to the maxima process,
2986 waiting for output after."
2987 (inferior-maxima-wait-for-output)
2988 (maxima-single-string string)
2989 (inferior-maxima-wait-for-output))
2991 (defun maxima-string (string)
2992 "Send a string to the Maxima process."
2993 (maxima-send-block string))
2995 (defun maxima-region (beg end)
2996 "Send the region to the Maxima process."
2997 (setq maxima-mode-region-begin beg)
2998 (setq maxima-mode-region-end end)
2999 (maxima-string
3000 (buffer-substring-no-properties beg end)))
3002 ;;; Some functions to send commands to the process.
3004 (defun maxima-send-region (beg end &optional arg)
3005 "Send the current region to the Maxima process.
3006 With an argument, don't check the parentheses first."
3007 (interactive "r\nP")
3008 (if arg
3009 (maxima-region beg end)
3010 (if (maxima-check-parens beg end)
3011 (maxima-region beg end)))
3012 (maxima-display-buffer))
3014 (defun maxima-send-buffer (&optional arg)
3015 "Send the buffer to the Maxima process, after checking the parentheses.
3016 With an argument, don't check the parentheses."
3017 (interactive "P")
3018 (maxima-send-region (point-min) (point-max) arg))
3020 (defun maxima-send-line (&optional arg)
3021 "Send the current line to the Maxima process, after checking parentheses.
3022 With an argument, don't check parentheses."
3023 (interactive "P")
3024 (let ((b (maxima-line-beginning-position))
3025 (e (maxima-line-end-position)))
3026 (maxima-send-region b e arg)))
3028 (defun maxima-send-form (&optional arg)
3029 "Send the current form to the Maxima process, checking parentheses.
3030 With an argument, don't check parentheses."
3031 (interactive "P")
3032 (maxima-send-region (maxima-form-beginning-position)
3033 (maxima-form-end-position-or-point-max) arg))
3035 (defun maxima-send-previous-form (&optional arg)
3036 "Send the previous form to the Maxima process, checking parentheses.
3037 With an argument, don't check parentheses."
3038 (interactive "P")
3039 (save-excursion
3040 (if (maxima-re-search-backward "[;$]")
3041 (maxima-send-region (maxima-form-beginning-position)
3042 (maxima-form-end-position-or-point-max) arg)
3043 (message "No previous form."))))
3045 (defun maxima-send-previous-form-and-goto-end-of-form (&optional arg)
3046 "Send the previous form to the Maxima process and go to the end of form."
3047 (interactive "P")
3048 (maxima-send-previous-form arg)
3049 (maxima-goto-end-of-form-interactive))
3051 (defun maxima-send-full-line ()
3052 "Send the minimum number of lines such that the current is one of them,
3053 and such that no line contains an incomplete form."
3054 (interactive)
3055 (let ((beg (point)) (end (point)))
3056 (save-excursion
3057 (goto-char beg)
3058 (beginning-of-line)
3059 (setq beg (point))
3060 (maxima-goto-beginning-of-form)
3061 (while (< (point) beg)
3062 (progn
3063 (beginning-of-line)
3064 (setq beg (point))
3065 (maxima-goto-beginning-of-form)))
3066 (goto-char end)
3067 (end-of-line)
3068 (setq end (point))
3069 (while (and (< (maxima-form-beginning-position) end) (< end (point-max)))
3070 (progn
3071 (forward-line 1)
3072 (end-of-line)
3073 (setq end (point))))
3074 (skip-chars-backward " \t;$")
3075 (if (re-search-forward "[;$]" end t)
3076 (maxima-send-region beg (point))
3077 (error "No ; or $ at end"))
3078 end)))
3080 (defun maxima-send-full-line-and-goto-next-form ()
3081 "Do a maxima-send-full-line and go to the beginning of the next form."
3082 (interactive)
3083 (goto-char (maxima-send-full-line))
3084 (maxima-goto-beginning-of-form))
3086 (defun maxima-send-completed-region (beg end)
3087 "Send the marked region, but complete possibly non-complete forms at the bounderies."
3088 (interactive "r\nP")
3089 (let ((beg1)
3090 (end1))
3091 (save-excursion
3092 (goto-char beg)
3093 (setq beg1 (maxima-form-beginning-position))
3094 (goto-char end)
3095 (setq end1 (maxima-form-end-position-or-point-max))
3096 (maxima-send-region beg1 end1)
3097 end1)))
3099 (defun maxima-send-completed-region-and-goto-next-form (beg end)
3100 "Do a maxima-send-completed-region and go to the beginning of the next form."
3101 (interactive "r\nP")
3102 (goto-char (maxima-send-completed-region beg end))
3103 (maxima-goto-beginning-of-form))
3105 (defun maxima-display-buffer ()
3106 "Display the inferior-maxima-process buffer so the recent output is visible."
3107 (interactive)
3108 (let ((origbuffer (current-buffer)))
3109 (if (not (processp inferior-maxima-process))
3110 (maxima-start))
3111 (pop-to-buffer (process-buffer inferior-maxima-process))
3112 (goto-char (point-max))
3113 ; (recenter (universal-argument))
3114 (pop-to-buffer origbuffer)))
3117 ;;;; The inferior Maxima process
3119 ;;; Completions from previous input
3121 ;; First, a function to take the comint-input-ring and return a
3122 ;; list of previous inputs
3124 (defun inferior-maxima-previous-inputs ()
3125 "Return a list of previous inputs."
3126 (interactive)
3127 (let* ((inputs nil)
3128 (comint-inputs (cddr comint-input-ring))
3129 (i 0))
3130 (while (and (< i comint-input-ring-size)
3131 (not (null (aref comint-inputs i))))
3132 (unless (member (aref comint-inputs i) inputs)
3133 (setq inputs (cons (aref comint-inputs i) inputs)))
3134 (setq i (1+ i)))
3135 (reverse inputs)))
3137 (defun inferior-maxima-input-complete ()
3138 "Complete line from list of previous input."
3139 (interactive)
3140 (let* ((stub (buffer-substring-no-properties
3141 (inferior-maxima-bol-position) (point)))
3142 (completions (all-completions (downcase stub)
3143 (inferior-maxima-previous-inputs))))
3144 (setq completions
3145 (mapcar
3146 (function (lambda (x) (maxima-he-transfer-case stub x))) completions))
3147 (cond ((null completions)
3148 (message "No completions of %s" stub))
3149 ((= 1 (length completions)) ; Gotcha!
3150 (let ((completion (car completions)))
3151 (if (string-equal completion stub)
3152 (message "Sole completion")
3153 (insert (substring completion (length stub)))
3154 (message "Completed"))))
3155 (t ; There's no unique completion.
3156 (comint-dynamic-list-completions completions)))))
3158 (defun inferior-maxima-complete ()
3159 "Complete the current object, depending on context."
3160 (interactive)
3161 (let* ((pmin (save-excursion
3162 (re-search-backward inferior-maxima-prompt)
3163 (point)))
3164 (pps (parse-partial-sexp pmin (point))))
3165 (cond
3166 ;; complete filename if the point is in a string
3167 ((nth 3 pps)
3168 (maxima-complete-filename))
3169 ;; Otherwise, complete the symbol
3171 (maxima-complete-symbol)))))
3173 ;; (defun maxima-input-he-try (old)
3174 ;; (interactive)
3175 ;; (if (not old)
3176 ;; ;;; let beg be the beginning of the word
3177 ;; (progn
3178 ;; (he-init-string (inferior-maxima-bol-position) (point))
3179 ;; (setq he-expand-list
3180 ;; (all-completions (downcase he-search-string) (maxima-previous-inputs)))
3181 ;; (setq he-expand-list
3182 ;; (mapcar (function
3183 ;; (lambda (x) (he-transfer-case he-search-string x)))
3184 ;; he-expand-list))
3185 ;; (if he-expand-list
3186 ;; (he-substitute-string (car he-expand-list))
3187 ;; nil))
3188 ;; (setq he-expand-list (cdr he-expand-list))
3189 ;; (if he-expand-list
3190 ;; (he-substitute-string (car he-expand-list))
3191 ;; (he-reset-string)
3192 ;; nil)))
3194 ;; (fset 'maxima-dynamic-input-complete
3195 ;; (make-hippie-expand-function '(maxima-input-he-try)))
3197 ;;; Sending a line to the process while in the process buffer
3199 (defun inferior-maxima-check-and-send-line ()
3200 "Check the lines for mis-matched parentheses, then send the line."
3201 (interactive)
3202 (let ((ok nil)
3203 (pt (point))
3204 pt1)
3205 (save-excursion
3206 (end-of-line)
3207 (skip-chars-backward " \t")
3208 (forward-char -1)
3209 (when (looking-at "[$;]")
3210 (setq pt (point))
3211 (setq ok t)))
3212 (if ok
3213 (progn
3214 (save-excursion
3215 (re-search-backward inferior-maxima-prompt)
3216 (setq pt1 (match-end 0)))
3217 (if (maxima-check-parens pt1 pt)
3218 (inferior-maxima-comint-send-input)))
3219 (inferior-maxima-comint-send-input))))
3221 (defun inferior-maxima-send-line ()
3222 "Send the line to the Maxima process."
3223 (interactive)
3224 (inferior-maxima-comint-send-input))
3226 (defun inferior-maxima-bol ()
3227 "Go to the beginning of the line, but past the prompt."
3228 (interactive)
3229 (let ((eol (save-excursion (end-of-line) (point))))
3230 (forward-line 0)
3231 (if (and (looking-at inferior-maxima-prompt)
3232 (<= (match-end 0) eol))
3233 (goto-char (match-end 0)))))
3235 (defun inferior-maxima-bol-position ()
3236 (save-excursion
3237 (inferior-maxima-bol)
3238 (point)))
3241 ;;;; Inferior Maxima mode
3243 (define-derived-mode inferior-maxima-mode
3244 comint-mode
3245 "Inferior Maxima"
3246 "Major mode for interacting with an inferior Maxima process.
3248 Return will check the line for balanced parentheses, and send line as input.
3249 Control return will send the line as input without checking for balanced
3250 parentheses.
3252 M-TAB will complete the Maxima symbol as much as possible, providing
3253 a completion buffer if there is more than one possible completion.
3255 \\[maxima-smart-complete] will complete the input line, based on previous input lines.
3256 \\[maxima-help] will get help on a Maxima topic.
3257 \\[maxima-info] will bring up the Maxima info manual.
3258 \\[maxima-stop] will kill the process and the buffer, after asking for
3259 confirmation. To kill without confirmation, give \\[maxima-stop] an
3260 argument.
3262 To scroll through previous commands,
3263 \\[comint-previous-input] will bring the previous input to the current prompt,
3264 \\[comint-next-input] will bring the next input to the prompt.
3265 \\[comint-previous-matching-input] will bring the previous input matching
3266 a regular expression to the prompt,
3267 \\[comint-next-matching-input] will bring the next input matching
3268 a regular expression to the prompt.
3270 ; (if maxima-use-full-color-in-process-buffer
3271 ; (inferior-maxima-font-setup))
3272 (setq comint-prompt-regexp inferior-maxima-prompt)
3273 (setq comint-get-old-input (function inferior-maxima-get-old-input))
3274 (setq mode-line-process '(": %s"))
3275 (maxima-mode-variables)
3276 (setq tab-width 8)
3277 (if (and (not maxima-running-xemacs) (< emacs-major-version 21))
3278 (make-local-hook 'kill-buffer-hook))
3279 (if maxima-running-xemacs
3280 (add-local-hook 'kill-buffer-hook
3281 (function
3282 (lambda ()
3283 (maxima-clear-queue)
3284 (if (processp inferior-maxima-process)
3285 (delete-process inferior-maxima-process))
3286 (setq inferior-maxima-process nil)
3287 (run-hooks 'inferior-maxima-exit-hook))))
3288 (add-hook 'kill-buffer-hook
3289 (function
3290 (lambda ()
3291 (maxima-clear-queue)
3292 (if (processp inferior-maxima-process)
3293 (delete-process inferior-maxima-process))
3294 (setq inferior-maxima-process nil)
3295 (run-hooks 'inferior-maxima-exit-hook))) t t))
3296 (setq comint-input-ring-size maxima-input-history-length)
3297 (if maxima-save-input-history
3298 (progn
3299 (setq comint-input-ring-file-name maxima-input-history-file)
3300 (comint-read-input-ring t)
3301 (set-process-sentinel inferior-maxima-process
3302 'inferior-maxima-sentinel)))
3303 (set (make-local-variable 'comint-prompt-read-only) t)
3304 (run-hooks 'inferior-maxima-mode-hook))
3306 ;;;; Keymap
3308 (define-key inferior-maxima-mode-map "\C-a"
3309 'inferior-maxima-bol)
3310 (define-key inferior-maxima-mode-map "\C-m"
3311 'inferior-maxima-check-and-send-line)
3312 (define-key inferior-maxima-mode-map [(control return)]
3313 'inferior-maxima-send-line)
3314 (define-key inferior-maxima-mode-map [(meta control tab)]
3315 'inferior-maxima-input-complete)
3316 (define-key inferior-maxima-mode-map "\e\t" 'inferior-maxima-complete)
3317 (define-key inferior-maxima-mode-map "\177" 'backward-delete-char-untabify)
3318 (define-key inferior-maxima-mode-map "\C-c\C-k" 'maxima-stop)
3319 (define-key inferior-maxima-mode-map "\C-c\C-d" maxima-help-map)
3321 ;;;; Menu
3323 (easy-menu-define inferior-maxima-mode-menu inferior-maxima-mode-map
3324 "Maxima mode menu"
3325 '("Maxima"
3326 ("Help"
3327 ["Maxima info" maxima-info t]
3328 ["Help" maxima-help t])
3329 ("Quit"
3330 ["Kill process" maxima-stop t])))
3332 ;;;; Running Maxima
3334 (defun maxima ()
3335 "Run Maxima interactively inside a buffer."
3336 (interactive)
3337 (maxima-start)
3338 (switch-to-buffer (process-buffer inferior-maxima-process)))
3340 ;;; Interacting with Maxima outside of a maxima buffer
3342 (defun maxima-minibuffer ()
3343 "Communicate with Maxima through the minibuffer"
3344 (interactive)
3345 (maxima-start)
3346 (let ((input (read-string "Maxima: " nil maxima-minibuffer-history))
3347 (output nil)
3348 (twod (and maxima-minibuffer-2d (not maxima-running-xemacs))))
3349 (setq input (maxima-strip-string-add-semicolon input))
3350 (if twod
3351 (maxima-single-string-wait
3352 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3353 (maxima-single-string-wait
3354 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3355 (maxima-single-string-wait input)
3356 (setq output (maxima-last-output-noprompt))
3357 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3358 (if (not twod)
3359 (setq output (maxima-remove-whitespace-from-ends output))
3360 ;; Strip the beginning and trailing newline
3361 (while (string-match "\\` *\n" output)
3362 (setq output (substring output (match-end 0))))
3363 (while (string-match "\n *\\'" output)
3364 (setq output (substring output 0 (match-beginning 0)))))
3365 (setq output (maxima-replace-in-string "%" "%%" output))
3366 (message output)))
3368 (defun maxima-minibuffer-delete-output (beg end)
3369 (let ((mmom (maxima-minor-output-mark))
3370 (mmoe (maxima-minor-output-mark-end)))
3371 (if (or
3372 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmom)
3373 (string= (match-string 0 mmom) mmom))
3374 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmoe)
3375 (string= (match-string 0 mmoe) mmoe)))
3376 (message "Old output not deleted (improper delimiter).")
3377 (let (pt)
3378 (save-excursion
3379 (goto-char beg)
3380 (if (search-forward mmom end t)
3381 (progn
3382 (setq pt (match-beginning 0))
3383 (search-forward mmoe)
3384 (kill-region pt (point)))
3385 (goto-char end)
3386 (if (looking-at (concat "[ \n]*" (regexp-quote mmom)))
3387 (progn
3388 (search-forward mmoe)
3389 (kill-region end (point)))))
3390 (point))))))
3392 (defun maxima-minibuffer-on-region (beg end &optional arg)
3393 "Send the current region to Maxima; display last output in minibuffer.
3394 With an argument, insert \" ==> \" into the current buffer,
3395 followed by the output, followed by \"\\\". In this case, any previous output
3396 will be deleted."
3397 (interactive "r\nP")
3398 (let ((output nil)
3399 (minibufferoutput)
3400 (input)
3401 (realend nil)
3402 (realbeg)
3403 (outputbeg)
3404 (delreg)
3405 (delregbeg)
3406 (delregend)
3407 (twod (and maxima-minibuffer-2d (not maxima-running-xemacs))))
3408 (save-excursion
3409 (goto-char beg)
3410 (maxima-forward-over-comment-whitespace)
3411 (setq realbeg (point))
3412 (if (re-search-forward (maxima-minor-output-mark) end t)
3413 (setq realend
3414 (if (eq major-mode 'maxima-mode)
3415 (- (point) (length maxima-mode-minor-output))
3416 (- (point) (length maxima-minor-output))))
3417 (goto-char end)
3418 (maxima-back-over-comment-whitespace)
3419 (setq realend (point))))
3420 (setq input (maxima-strip-string-add-semicolon
3421 (buffer-substring-no-properties realbeg realend)))
3422 (if arg
3423 (maxima-minibuffer-delete-output beg end))
3424 (setq maxima-minor-mode-region-begin realbeg)
3425 (setq maxima-minor-mode-region-end realend)
3426 (when (or (not maxima-minor-mode-check-input)
3427 (and
3428 (maxima-check-parens realbeg realend)
3429 (maxima-check-commas realbeg realend)))
3430 (maxima-start)
3431 (if twod
3432 (maxima-single-string-wait
3433 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3434 (maxima-single-string-wait
3435 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3436 (maxima-send-block-wait input)
3437 (setq output (maxima-last-output-noprompt))
3438 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3439 (if (not twod)
3440 (setq output (maxima-remove-whitespace-from-ends output))
3441 ;; Strip the beginning and trailing newline
3442 (while (string-match "\\` *\n" output)
3443 (setq output (substring output (match-end 0))))
3444 (while (string-match "\n *\\'" output)
3445 (setq output (substring output 0 (match-beginning 0)))))
3446 (unless arg
3447 (setq minibufferoutput (maxima-replace-in-string "%" "%%" output))
3448 (message minibufferoutput))
3449 (if (and arg
3450 (not twod))
3451 (save-excursion
3452 (goto-char realend)
3453 (if (looking-at "^")
3454 (setq realend (1- realend)))
3455 ;(delete-region realend end)
3456 (goto-char realend)
3457 (skip-chars-backward " \t\n")
3458 (when (not (= (point) realend))
3459 (setq delreg (buffer-substring-no-properties (point) realend))
3460 (kill-region (point) realend)
3461 (cond
3462 ((< (length delreg) 15)
3463 (setq delreg (maxima-replace-in-string "\n" " " delreg))
3464 (message (concat "\"" delreg "\" killed")))
3466 (setq delregbeg
3467 (maxima-replace-in-string "\n" " "(substring delreg 0 5)))
3468 (setq delregend
3469 (maxima-replace-in-string "\n" " "(substring delreg -5)))
3470 (message (concat "\"" delregbeg " ... " delregend "\" killed")))))
3471 (let ((ind (save-excursion
3472 (goto-char realbeg)
3473 (current-column)))
3474 (here (point))
3475 (there (make-marker)))
3476 (if (or
3477 (string-match "\n" output)
3478 (> (+ (current-column) (length output)) fill-column))
3479 (progn
3480 (insert "\n")
3481 (setq here (point)))
3482 (insert " "))
3483 (insert (maxima-minor-output-mark) " " output
3484 (maxima-minor-output-mark-end))
3485 (set-marker there (point))
3486 (goto-char here)
3487 (goto-char (line-end-position))
3488 ; (fill-region (line-beginning-position) (point))
3489 (if (string-match
3490 "\n"
3491 (buffer-substring-no-properties here (point)))
3492 (forward-line -1)
3493 (forward-line 1))
3494 (indent-region (point) there ind)))
3495 (if (and arg twod)
3496 (let ((ind (save-excursion
3497 (goto-char realbeg)
3498 (current-column)))
3499 (here))
3500 (save-excursion
3501 (goto-char realend)
3502 (insert (maxima-minor-output-mark) "\n")
3503 (setq here (point))
3504 (insert output (maxima-minor-output-mark-end))
3505 (indent-region here (point) ind))))))))
3507 (defun maxima-minibuffer-on-line (&optional arg)
3508 "Send the current line to Maxima; display last output in minibuffer.
3509 With an argument, insert \" ==> \" into the current buffer,
3510 followed by the output. In this case, anything in the line
3511 after any occurrence of \" ==> \" will be deleted."
3512 (interactive "P")
3513 (maxima-minibuffer-on-region
3514 (maxima-line-beginning-position)
3515 (maxima-line-end-position)
3516 arg))
3518 (defun maxima-minibuffer-on-form (&optional arg)
3519 "Send the current form to Maxima; display last output in minibuffer.
3520 With an argument, insert \" ==> \" into the current buffer,
3521 followed by the output."
3522 (interactive "P")
3523 (let ((beg (maxima-form-beginning-position))
3524 (end (maxima-form-end-position)))
3525 (save-excursion
3526 (when (re-search-backward "^[ \t]*$" beg t)
3527 (maxima-forward-over-comment-whitespace)
3528 (setq beg (point))))
3529 (maxima-minibuffer-on-region beg end arg)))
3531 (defun maxima-minibuffer-on-determined-region (&optional arg)
3532 "Send a determined region to Maxima; display the output in the minibuffer.
3533 The region is the region between `maxima-minor-prefix' and `maxima-minor-postfix'
3534 With an argument, insert \" ==> \" into the current buffer,
3535 followed by the output. In this case, anything in the determined region
3536 after any occurrence of \" ==> \" will be deleted."
3537 (interactive "P")
3538 (let ((beg)
3539 (end)
3540 (pt (point)))
3541 (save-excursion
3542 (if (re-search-backward maxima-minor-prefix nil t)
3543 (setq beg (match-end 0))
3544 (error "No beginning to determined region"))
3545 (goto-char pt)
3546 (if (re-search-forward maxima-minor-prefix nil t)
3547 (setq end (match-beginning 0))))
3548 (maxima-minibuffer-on-region beg end arg)))
3550 (defun maxima-insert-last-output ()
3551 (interactive)
3552 (maxima-single-string-wait
3553 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);")
3554 (let ((output (maxima-last-output-noprompt)))
3555 (maxima-single-string "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3556 (insert (maxima-remove-whitespace-from-ends output))))
3558 (defun maxima-insert-last-output-tex ()
3559 (interactive)
3560 (maxima-single-string-wait "tex(%);")
3561 (let ((output (substring (maxima-last-output-tex-noprompt) 2 -3)))
3562 (maxima-single-string "block(linenum:linenum-2,%th(2));")
3563 (insert output)))
3565 ;;; The Maxima minor mode
3567 (defvar maxima-minor-mode-map nil
3568 "The keymap for maxima-minor-mode.")
3570 (if maxima-minor-mode-map
3572 (let ((map (make-sparse-keymap)))
3573 (define-key map "\C-c=m" 'maxima-minibuffer)
3574 (define-key map "\C-c=e" 'maxima-minibuffer-on-determined-region)
3575 (define-key map "\C-c=l" 'maxima-minibuffer-on-line)
3576 (define-key map "\C-c=r" 'maxima-minibuffer-on-region)
3577 (define-key map "\C-c=f" 'maxima-minibuffer-on-form)
3578 (define-key map "\C-c=o" 'maxima-insert-last-output)
3579 (define-key map "\C-c=t" 'maxima-insert-last-output-tex)
3580 (define-key map "\C-c=d" maxima-help-map)
3581 (setq maxima-minor-mode-map map)))
3583 (unless (fboundp 'define-minor-mode)
3584 (defalias 'define-minor-mode 'easy-mmode-define-minor-mode))
3586 (define-minor-mode maxima-minor-mode
3587 "Toggle Maxima minor mode.
3588 With no argument, this command toggles the mode.
3589 Non-null prefix argument turns on the mode.
3590 Null prefix argument turns off the mode.
3592 When Maxima minor mode is enabled, the following keystrokes
3593 are in effect:
3594 \\[maxima-minibuffer-on-determined-region]
3595 Send the region between the customizable regexps
3596 `maxima-minor-prefix' and `maxima-minor-postfix' to Maxima
3597 and display the result in the minibuffer.
3598 \\[maxima-minibuffer-on-line]
3599 Send the current line to Maxima and display the result in the minibuffer.
3600 \\[maxima-minibuffer-on-region]
3601 Send the current region to Maxima and display the result in the minibuffer.
3602 \\[maxima-minibuffer-on-form]
3603 Send the current form to Maxima and display the result in the minibuffer.
3604 (The form is the region between the preceding ; or $ and the subsequent
3605 ; or $)
3606 With a prefix, the above commands will insert the output in the current
3607 buffer, preceded by \" ==> \" (customizable with `maxima-minor-output').
3608 \\[maxima-minibuffer]
3609 Prompt for an expression in the minibuffer, return result in minibuffer.
3610 \\[maxima-insert-last-output]
3611 Insert the last Maxima result into the current buffer.
3612 \\[maxima-insert-last-output-tex]
3613 Insert the last Maxima result in TeX form into the current buffer."
3614 ;; The initial value.; :initial-value
3616 ;; The indicator for the mode line.; :lighter
3617 " maxima"
3618 nil)
3620 ;;; For highlighting the region being sent
3622 (defun maxima-minor-mode-add-highlight ()
3623 (maxima-minor-mode-remove-highlight)
3624 (when (and maxima-minor-mode-region-begin
3625 maxima-minor-mode-region-end)
3626 (if maxima-running-xemacs
3627 (progn
3628 (setq maxima-minor-mode-highlight
3629 (make-extent
3630 maxima-minor-mode-region-begin
3631 maxima-minor-mode-region-end))
3632 (set-extent-property maxima-minor-mode-highlight 'face 'highlight))
3633 (setq maxima-minor-mode-highlight
3634 (make-overlay
3635 maxima-minor-mode-region-begin
3636 maxima-minor-mode-region-end))
3637 (overlay-put maxima-minor-mode-highlight 'face 'highlight))
3638 (setq maxima-minor-mode-region-begin nil)
3639 (setq maxima-minor-mode-region-end nil)))
3641 (defun maxima-minor-mode-remove-highlight ()
3642 (when maxima-minor-mode-highlight
3643 (if maxima-running-xemacs
3644 (delete-extent maxima-minor-mode-highlight)
3645 (delete-overlay maxima-minor-mode-highlight))
3646 (setq maxima-minor-mode-highlight nil)))
3648 (defun maxima-minor-mode-add-remove-highlight ()
3649 (if (or
3650 (eq this-command 'maxima-minibuffer-on-region)
3651 (eq this-command 'maxima-minibuffer-on-determined-region)
3652 (eq this-command 'maxima-minibuffer-on-form)
3653 (eq this-command 'maxima-minibuffer-on-line))
3654 (maxima-minor-mode-add-highlight)
3655 (maxima-minor-mode-remove-highlight)))
3657 (defun maxima-minor-mode-highlighting ()
3658 (if maxima-running-xemacs
3659 (add-local-hook 'post-command-hook
3660 'maxima-minor-mode-add-remove-highlight)
3661 (add-hook 'post-command-hook
3662 'maxima-minor-mode-add-remove-highlight nil t)))
3664 (add-hook 'maxima-minor-mode-hook
3665 'maxima-minor-mode-highlighting)
3667 (provide 'maxima)
3668 ;;; maxima.el ends here