initial version of bigclean-emacs,from svn to git
[bigclean-emacs.git] / emacs / .emacs.d / site-lisp / maxima.el
blob7353100790cd8006c2924b1f9c9445214b6d5aae
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 `.max' to begin
42 ;; in `maxima-mode', for example, put the line
43 ;; (setq auto-mode-alist (cons '("\\.max" . 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."
338 :group 'maxima
339 :type 'string)
341 (defcustom maxima-args nil
342 "*Extra arguments to pass to the maxima-command."
343 :group 'maxima
344 :type 'string)
346 (defcustom maxima-use-tabs nil
347 "*If non-nil, indentation will use tabs."
348 :group 'maxima
349 :type 'boolean)
351 (defcustom maxima-minibuffer-2d nil
352 "*If non-nil, use 2D output for maxima-minibuffer."
353 :group 'maxima
354 :type 'boolean)
356 (defcustom maxima-use-full-color-in-process-buffer nil
357 "*If non-nil, font-lock the maxima process buffer."
358 :group 'maxima
359 :type 'boolean)
361 (defcustom maxima-fix-double-prompt maxima-running-xemacs
362 "*If non-nil, fix the double prompt that sometimes appears in XEmacs."
363 :group 'maxima
364 :type 'boolean)
366 (defcustom maxima-save-input-history nil
367 "*If non-nil, save the input history in a file."
368 :group 'maxima
369 :type 'boolean)
371 (defcustom maxima-input-history-file "~/.maxima_history"
372 "*A file to save the input history in."
373 :group 'maxima
374 :type 'file)
376 (defcustom maxima-input-history-length 50
377 "*How many lines of history to save."
378 :group 'maxima
379 :type 'integer)
381 (defcustom maxima-minor-prefix "^[ \t]*$"
382 "*A regexp to indicate the beginning of a region to send to Maxima
383 in maxima minor mode."
384 :group 'maxima
385 :type 'string)
387 (defcustom maxima-minor-postfix "^[ \t]*$"
388 "*A regexp to indicate the end of a region to send to Maxima
389 in maxima minor mode."
390 :group 'maxima
391 :type 'string)
393 (defcustom maxima-minor-output "==>"
394 "*A string to insert in the buffer right before the output."
395 :group 'maxima
396 :type 'string)
398 (defcustom maxima-minor-output-end " //"
399 "*A string to insert in the buffer right after the output."
400 :group 'maxima
401 :type 'string)
403 (defcustom maxima-mode-minor-output "/*==>"
404 "*A string to insert in the buffer right before the output."
405 :group 'maxima
406 :type 'string)
408 (defcustom maxima-mode-minor-output-end " <==*/"
409 "*A string to insert in the buffer right after the output."
410 :group 'maxima
411 :type 'string)
413 (defcustom maxima-minor-mode-check-input t
414 "*Non-nil means check the input in Maxima minor mode before sending it."
415 :group 'maxima
416 :type 'boolean)
418 (defun maxima-minor-output-mark ()
419 (if (and
420 (eq major-mode 'maxima-mode)
421 (not (maxima-in-comment-p)))
422 maxima-mode-minor-output
423 maxima-minor-output))
425 (defun maxima-minor-output-mark-end ()
426 (if (and
427 (eq major-mode 'maxima-mode)
428 (not (maxima-in-comment-p)))
429 maxima-mode-minor-output-end
430 maxima-minor-output-end))
432 ;;;; The other variables
434 ;; This variable seems to be necessary ...
435 (defvar inferior-maxima-after-output-wait 100)
437 (defvar maxima-temp-suffix 0
438 "Temporary filename suffix. Incremented by 1 for each filename.")
440 (defvar maxima-special-symbol-letters "!:='")
442 (defvar maxima-minibuffer-history nil)
444 (defvar maxima-block "")
446 (defvar maxima-block-wait "")
448 (defvar inferior-maxima-process nil
449 "The Maxima process.")
451 (defvar inferior-maxima-input-end 0
452 "The end of the latest input that was sent to Maxima.")
454 (defvar inferior-maxima-output-end 0)
456 (defvar inferior-maxima-waiting-for-output nil)
458 (defvar inferior-maxima-exit-hook nil)
460 (defvar inferior-maxima-prompt
461 (concat "\\(^(" maxima-inchar
462 "[0-9]*) \\)\\|\\(^MAXIMA> \\)\\|\\(^(dbm:[0-9]*) \\)")
463 ; \\(^[^#%)>]*[#%)>]+ *\\)"
464 "*Regexp to recognize prompts from the inferior Maxima") ; or lisp")
467 (defvar maxima-mode-highlight nil)
469 (defvar maxima-mode-region-begin nil)
471 (defvar maxima-mode-region-end nil)
473 (defvar maxima-minor-mode-region-begin nil)
475 (defvar maxima-minor-mode-region-end nil)
477 (defvar maxima-minor-mode-highlight nil)
479 (defvar maxima-minor-mode-bad-delimiter-regexp "\\([ \t\n]+\\|[0-9]+\\)")
481 ;;;; Utility functions
483 (defun maxima-string-equal (str1 str2)
484 (string= (downcase str1) (downcase str2)))
486 ;; This was taken from `replace-regexp-in-string' from subr.el in GNU emacs.
487 (defun maxima-replace-in-string (regexp rep string)
488 "Replace all matches for REGEXP with REP in STRING."
489 (let ((l (length string))
490 (start 0)
491 matches str mb me)
492 (save-match-data
493 (while (and (< start l) (string-match regexp string start))
494 (setq mb (match-beginning 0)
495 me (match-end 0))
496 (when (= me mb) (setq me (min l (1+ mb))))
497 (string-match regexp (setq str (substring string mb me)))
498 (setq matches
499 (cons (replace-match (if (stringp rep)
501 (funcall rep (match-string 0 str)))
502 nil nil str)
503 (cons (substring string start mb) ; unmatched prefix
504 matches)))
505 (setq start me))
506 ;; Reconstruct a string from the pieces.
507 (setq matches (cons (substring string start l) matches)) ; leftover
508 (apply #'concat (nreverse matches)))))
510 (defun maxima-remove-kill-buffer-hooks ()
511 "Remove the kill-buffer-hooks locally"
512 (if (or maxima-running-xemacs (< emacs-major-version 21))
513 (progn
514 (make-local-hook 'kill-buffer-hook)
515 (setq kill-buffer-hook nil))
516 (let ((hooks kill-buffer-hook))
517 (while hooks
518 (remove-hook 'kill-buffer-hook (car hooks) t)
519 (setq hooks (cdr hooks))))))
521 (defun maxima-make-temp-name ()
522 "Return a unique filename."
523 (setq maxima-temp-suffix (+ maxima-temp-suffix 1))
524 (concat (concat (make-temp-name "#mz") "-")
525 (int-to-string maxima-temp-suffix)
526 ".max"))
528 (defun maxima-strip-string-beginning (string)
529 "Return STRING with whitespace and comments removed from the beginning."
530 (let* ((tmpfile (maxima-make-temp-name))
531 (tmpbuf (get-buffer-create tmpfile))
532 (out))
533 (save-excursion
534 (set-buffer tmpbuf)
535 (maxima-remove-kill-buffer-hooks)
536 (modify-syntax-entry ?/ ". 14")
537 (modify-syntax-entry ?* ". 23")
538 (insert string)
539 (goto-char (point-min))
540 (maxima-forward-over-comment-whitespace)
541 (setq out (buffer-substring-no-properties (point) (point-max))))
542 (kill-buffer tmpbuf)
543 out))
545 (defun maxima-strip-string-end (string)
546 "Return STRING with whitespace and comments removed from the end."
547 (let* ((tmpfile (maxima-make-temp-name))
548 (tmpbuf (get-buffer-create tmpfile))
549 (out))
550 (save-excursion
551 (set-buffer tmpbuf)
552 (maxima-remove-kill-buffer-hooks)
553 (modify-syntax-entry ?/ ". 14")
554 (modify-syntax-entry ?* ". 23")
555 (insert string)
556 (goto-char (point-max))
557 (maxima-back-over-comment-whitespace)
558 (setq out (buffer-substring-no-properties (point-min) (point))))
559 (kill-buffer tmpbuf)
560 out))
562 (defun maxima-strip-string (string)
563 "Return STRING with whitespace and comments removed from the ends."
564 (maxima-strip-string-beginning (maxima-strip-string-end string)))
566 (defun maxima-strip-string-add-semicolon (string)
567 "Return STRING with whitespace and comments removed from the ends."
568 (setq string
569 (maxima-strip-string-beginning (maxima-strip-string-end string)))
570 (unless (or
571 (string= string "")
572 (and (>= (length string) 5)
573 (string= (substring string 0 5) ":lisp"))
574 (string= (substring string -1) ";")
575 (string= (substring string -1) "$"))
576 (setq string (concat string ";")))
577 string)
579 (defun maxima-remove-whitespace-from-ends (string)
580 "Return STRING with whitespace from the ends."
581 (let* ((tmpfile (maxima-make-temp-name))
582 (tmpbuf (get-buffer-create tmpfile))
583 (str string)
584 (out)
585 (beg)
586 (end))
587 (save-excursion
588 (set-buffer tmpbuf)
589 (maxima-remove-kill-buffer-hooks)
590 (insert str)
591 (goto-char (point-min))
592 (skip-chars-forward " \t\n")
593 (setq beg (point))
594 (goto-char (point-max))
595 (skip-chars-backward " \t\n")
596 (setq end (point))
597 (setq out (buffer-substring-no-properties beg end)))
598 (kill-buffer tmpbuf)
599 out))
601 (defun maxima-remove-whitespace-from-beg (string)
602 "Return STRING with whitespace removed from the beginning."
603 (let* ((tmpfile (maxima-make-temp-name))
604 (tmpbuf (get-buffer-create tmpfile))
605 (out)
606 (str string)
607 (beg)
608 (end))
609 (save-excursion
610 (set-buffer tmpbuf)
611 (maxima-remove-kill-buffer-hooks)
612 (insert str)
613 (goto-char (point-min))
614 (skip-chars-forward " \t\n")
615 (setq beg (point))
616 (setq out (buffer-substring-no-properties beg (point-max))))
617 (kill-buffer tmpbuf)
618 out))
620 (defun maxima-remove-whitespace-from-end (string)
621 "Return STRING with whitespace removed from the end."
622 (let* ((tmpfile (maxima-make-temp-name))
623 (tmpbuf (get-buffer-create tmpfile))
624 (out)
625 (str string)
626 (beg)
627 (end))
628 (save-excursion
629 (set-buffer tmpbuf)
630 (maxima-remove-kill-buffer-hooks)
631 (insert str)
632 (goto-char (point-max))
633 (skip-chars-backward " \t\n")
634 (setq end (point))
635 (setq out (buffer-substring-no-properties (point-min) end)))
636 (kill-buffer tmpbuf)
637 out))
639 ;;;; Functions that query position
640 (defun maxima-in-comment-p ()
641 "Non-nil means that the point is in a comment."
642 (let ((pt (point)))
643 (save-excursion
644 (and
645 (search-backward "/*" nil t)
646 (not (search-forward "*/" pt t))))))
648 (defun maxima-in-output-p ()
649 "Non-nil means that the point is in minibuffer output."
650 (let ((pt (point)))
651 (save-excursion
652 (and
653 (search-backward (maxima-minor-output-mark) nil t)
654 (not (search-forward (maxima-minor-output-mark-end) pt t))))))
656 ;;; Functions that search
658 ;; Some additions to help with different types of searches
659 (defvar maxima-mode-type 'maxima-mode)
660 (make-variable-buffer-local 'maxima-mode-type)
662 (defvar maxima-noweb-ignore-bounds '("<<" ">>"))
664 (defun maxima-noweb-in-ignore-bounds-p ()
665 (if (not maxima-noweb-ignore-bounds)
667 (let ((pt (point)))
668 (save-excursion
669 (if (not (re-search-backward (car maxima-noweb-ignore-bounds) nil t))
671 (not (re-search-forward (cadr maxima-noweb-ignore-bounds) pt t)))))))
673 (defun maxima-noweb-forward-out-of-ignore-bounds (&optional pmax)
674 (re-search-forward (cadr maxima-noweb-ignore-bounds) pmax 1))
676 (defun maxima-noweb-backward-out-of-ignore-bounds (&optional pmin)
677 (re-search-backward (car maxima-noweb-ignore-bounds) pmin 1))
680 (defun maxima-standard-re-search-forward (regexp &optional pmax)
681 "Search forward for REGEXP, bounded by PMAX.
682 Ignore matches found in comments and strings."
683 (let ((keep-looking t)
684 (didnt-find nil)
685 (match nil)
686 (ppe)
687 (pt)
688 (origpt (point)))
689 (setq pt origpt)
690 (while (and keep-looking
691 (not didnt-find)
692 (re-search-forward regexp pmax t))
693 (setq match (match-string 0))
694 (setq ppe (parse-partial-sexp pt (point)))
695 (cond
696 ((nth 3 ppe) ;; In a string
697 (if (maxima-goto-end-of-string)
698 (setq pt (point))
699 (setq didnt-find t)))
700 ((nth 4 ppe) ;; In a comment
701 (if (maxima-goto-end-of-comment)
702 (setq pt (point))
703 (setq didnt-find t)))
704 (t ;; not in a comment or string
705 (setq keep-looking nil))))
706 (if (or didnt-find keep-looking)
707 (progn
708 (goto-char origpt)
709 nil)
710 match)))
713 (defun maxima-noweb-re-search-forward (regexp &optional pmax)
714 (let ((match
715 (maxima-standard-re-search-forward regexp pmax)))
716 (while (maxima-noweb-in-ignore-bounds-p)
717 (maxima-noweb-forward-out-of-ignore-bounds pmax)
718 (setq match
719 (maxima-standard-re-search-forward regexp pmax)))
720 match))
722 (defun maxima-re-search-forward (regexp &optional pmax)
723 (cond
724 ((eq maxima-mode-type 'maxima-noweb-mode)
725 (maxima-noweb-re-search-forward regexp pmax))
727 (maxima-standard-re-search-forward regexp pmax))))
729 (defun maxima-re-search-forward-skip-blocks (regexp &optional pmax)
730 "Search forward for REGEXP, bounded by PMAX.
731 Ignore matches found in comments and strings, and skip over
732 parenthesized or bracketed blocks."
733 (let ((keep-looking t)
734 (didnt-find nil)
735 (match nil)
736 (pt (point)))
737 (while (and keep-looking
738 (setq match (maxima-re-search-forward
739 (concat regexp "\\|[[(]") pmax)))
740 (cond
741 ((or
742 (string= match "[")
743 (string= match "("))
744 (unless
745 (maxima-goto-end-of-list)
746 (setq didnt-find t)
747 (setq keep-looking nil)))
749 (setq keep-looking nil))))
750 (if (or keep-looking didnt-find)
751 (progn
752 (goto-char pt)
753 nil)
754 match)))
756 (defun maxima-standard-re-search-backward (regexp &optional pmin)
757 "Search backward for REGEXP, bounded by PMIN.
758 Ignore matches found in comments and strings."
759 (let ((keep-looking t)
760 (match nil)
761 (ppe)
762 (origpt (point)))
763 (unless pmin
764 (setq pmin (point-min)))
765 (while (and keep-looking
766 (re-search-backward regexp pmin t))
767 (setq match (match-string 0))
768 (setq ppe (parse-partial-sexp pmin (point)))
769 (cond
770 ((nth 8 ppe) ;; In a string or comment
771 (goto-char (nth 8 ppe)))
772 (t ;; not in a comment or string
773 (setq keep-looking nil))))
774 (if keep-looking
775 (progn
776 (goto-char origpt)
777 nil)
778 match)))
780 (defun maxima-noweb-re-search-backward (regexp &optional pmin)
781 (let ((match
782 (maxima-standard-re-search-backward regexp pmin)))
783 (while (maxima-noweb-in-ignore-bounds-p)
784 (maxima-noweb-backward-out-of-ignore-bounds pmin)
785 (setq match
786 (maxima-standard-re-search-backward regexp pmin)))
787 match))
789 (defun maxima-re-search-backward (regexp &optional pmin)
790 (cond
791 ((eq maxima-mode-type 'maxima-noweb-mode)
792 (maxima-noweb-re-search-backward regexp pmin))
794 (maxima-standard-re-search-backward regexp pmin))))
796 (defun maxima-re-search-backward-skip-blocks (regexp &optional pmin)
797 "Search forward for REGEXP, bounded by PMIN.
798 Ignore matches found in comments and strings, and skip over
799 parenthesized and bracketed blocks."
800 (let ((keep-looking t)
801 (didnt-find nil)
802 (match nil)
803 (pt (point)))
804 (while (and keep-looking
805 (setq match (maxima-re-search-backward
806 (concat regexp "\\|[])]") pmin)))
807 (cond
808 ((or
809 (string= match ")")
810 (string= match "]"))
811 (unless
812 (maxima-goto-beginning-of-list)
813 (setq didnt-find t)
814 (setq keep-looking nil)))
816 (setq keep-looking nil))))
817 (if (or keep-looking didnt-find)
818 (progn
819 (goto-char pt)
820 nil)
821 match)))
823 (defun maxima-escaped-char-p ()
824 "Returns non-nil if the character after point is escaped"
825 (let ((pm (point-min))
826 (esc-chars 0))
827 (when (> (point) pm)
828 (save-excursion
829 (forward-char -1)
830 (while (and
831 (looking-at "\\\\")
832 (setq esc-chars (1+ esc-chars))
833 (> (point) pm))
834 (forward-char -1))))
835 (if (= (% esc-chars 2) 0)
837 t)))
839 (defun maxima-goto-end-of-string ()
840 "Go to the end of the string that the point is in.
841 Assumes that point is in a string."
842 (interactive)
843 (let ((keep-looking t))
844 (while (and keep-looking (search-forward "\"" nil t))
845 (forward-char -2)
846 (unless (maxima-escaped-char-p)
847 (setq keep-looking nil))
848 (forward-char 2))
849 (if keep-looking
851 t)))
853 (defun maxima-goto-beginning-of-string ()
854 "Go to the beginning of the string that the point is in.
855 Assumes that point is in a string."
856 (interactive)
857 (let ((keep-looking t))
858 (while (and keep-looking (search-backward "\"" nil t))
859 (forward-char -1)
860 (unless (maxima-escaped-char-p)
861 (setq keep-looking nil))
862 (forward-char 1))))
864 (defun maxima-goto-end-of-comment ()
865 "Go to the end of the comment that the point is in.
866 Assumes that point is in a comment."
867 (interactive)
868 (search-forward "*/" nil t))
870 (defun maxima-goto-beginning-of-comment ()
871 "Go to the beginning of the comment that the point is in.
872 Assumes that point is in a comment."
873 (interactive)
874 (search-backward "/*"))
876 (defun maxima-find-next-nonnested-close-char ()
877 "Search forward for next , ; $ or closing ), skipping over balanced parens.
878 If character is in a string or a list, ignore it."
879 (interactive)
880 (maxima-re-search-forward-skip-blocks "[,;$)]"))
883 ;;; Functions for dealing with words
885 ;; (defun maxima-number-of-preceding-backslashes ()
886 ;; "The number of backslashes (including the one being looked at)."
887 ;; (let ((pt (point)))
888 ;; (if (not (looking-at "\\\\"))
889 ;; 0
890 ;; (save-excursion
891 ;; (skip-chars-backward "\\\\")
892 ;; (1+ (- pt (point)))))))
894 (defun maxima-standard-next-char-word-part-p ()
895 "Non-nil if next char is a a word part."
897 (looking-at "\\w")
898 (looking-at "\\\\")
899 (save-excursion
900 (forward-char -1)
901 (looking-at "\\\\"))))
903 (defun maxima-noweb-next-char-word-part-p ()
904 "Non-nil if next char is a a word part."
906 (looking-at "\\w")
907 (looking-at "\\\\")
908 (and
909 (looking-at ">")
910 (save-excursion
911 (forward-char -1)
912 (looking-at ">")))
913 (save-excursion
914 (forward-char -1)
915 (looking-at "\\\\"))))
917 (defun maxima-next-char-word-part-p ()
918 (cond
919 ((eq maxima-mode-type 'maxima-noweb-mode)
920 (maxima-noweb-next-char-word-part-p))
922 (maxima-standard-next-char-word-part-p))))
924 (defun maxima-previous-char-word-part-p ()
925 "Non-nil if previous character is a word part."
926 (save-excursion
927 (forward-char -1)
928 (maxima-next-char-word-part-p)))
930 (defun maxima-standard-forward-word ()
931 "Go to the end of the current word."
932 (let ((keep-going t))
933 (while keep-going
934 (cond
935 ((looking-at "\\w")
936 (forward-word 1))
937 ((looking-at "\\\\")
938 (forward-char 2))
940 (setq keep-going nil))))))
942 (defun maxima-noweb-forward-word ()
943 "Go to the end of the current word."
944 (if (maxima-noweb-in-ignore-bounds-p)
945 (maxima-noweb-forward-out-of-ignore-bounds))
946 (let ((keep-going t))
947 (while keep-going
948 (cond
949 ((looking-at "\\w")
950 (forward-word 1))
951 ((looking-at "\\\\")
952 (forward-char 2))
953 ((looking-at "<<")
954 (forward-char 2)
955 (maxima-noweb-forward-out-of-ignore-bounds))
957 (setq keep-going nil))))))
959 (defun maxima-forward-word ()
960 (cond
961 ((eq maxima-mode-type 'maxima-noweb-mode)
962 (maxima-noweb-forward-word))
964 (maxima-standard-forward-word))))
966 (defun maxima-standard-backward-word ()
967 "Go to the beginning of the current word."
968 (let ((keep-going t))
969 (while keep-going
970 (cond
971 ((and
972 (> (point) (point-min))
973 (save-excursion
974 (forward-char -1)
975 (looking-at "\\w")))
976 (backward-word 1))
977 ((and
978 (> (point) (1+ (point-min)))
979 (save-excursion
980 (forward-char -2)
981 (looking-at "\\\\")))
982 (forward-char -2))
984 (setq keep-going nil))))))
986 (defun maxima-noweb-backward-word ()
987 "Go to the beginning of the current word."
988 (if (maxima-noweb-in-ignore-bounds-p)
989 (maxima-noweb-backward-out-of-ignore-bounds))
990 (let ((keep-going t))
991 (while keep-going
992 (cond
993 ((and
994 (> (point) (point-min))
995 (save-excursion
996 (forward-char -1)
997 (looking-at "\\w")))
998 (backward-word 1))
999 ((and
1000 (> (point) (1+ (point-min)))
1001 (save-excursion
1002 (forward-char -2)
1003 (looking-at "\\\\")))
1004 (forward-char -2))
1005 ((and
1006 (> (point) (1+ (point-min)))
1007 (save-excursion
1008 (forward-char -2)
1009 (looking-at ">>")))
1010 (forward-char -2)
1011 (maxima-noweb-backward-out-of-ignore-bounds))
1013 (setq keep-going nil))))))
1015 (defun maxima-backward-word ()
1016 (cond
1017 ((eq maxima-mode-type 'maxima-noweb-mode)
1018 (maxima-noweb-backward-word))
1020 (maxima-standard-backward-word))))
1022 ;;;; Functions that return special positions
1024 (defun maxima-line-beginning-position ()
1025 (if (not (fboundp 'line-beginning-position))
1026 (save-excursion
1027 (beginning-of-line)
1028 (point))
1029 (line-beginning-position)))
1031 (defun maxima-line-end-position ()
1032 (if (not (fboundp 'line-end-position))
1033 (save-excursion
1034 (end-of-line)
1035 (point))
1036 (line-end-position)))
1038 (defun maxima-name-beginning ()
1039 (save-excursion
1040 (maxima-backward-word)
1041 (point)))
1043 (defun maxima-special-symbol-beginning ()
1044 (save-excursion
1045 (skip-chars-backward maxima-special-symbol-letters)
1046 (point)))
1048 (defun maxima-special-symbol-end ()
1049 (save-excursion
1050 (skip-chars-forward maxima-special-symbol-letters)
1051 (point)))
1053 (defun maxima-form-beginning-position ()
1054 (save-excursion
1055 (maxima-goto-beginning-of-form)
1056 (point)))
1058 (defun maxima-form-end-position ()
1059 (save-excursion
1060 (if (maxima-goto-end-of-form)
1061 (point)
1062 nil)))
1064 (defun maxima-form-end-position-or-point-max ()
1065 (let ((mfep (maxima-form-end-position)))
1066 (if mfep
1067 mfep
1068 (point-max))))
1070 (defun maxima-expression-end-position ()
1071 "Return the point where the current expression ends,
1072 or nil."
1073 (save-excursion
1074 (if (maxima-goto-end-of-expression)
1075 (point)
1076 nil)))
1078 (defun maxima-begin-if-position (pmin)
1079 "Find the point of the opening \"if\" for the current point."
1080 (let ((nest 0)
1081 (match)
1082 (keep-looking t)
1083 (pt (point)))
1084 (save-excursion
1085 (while (and keep-looking
1086 (setq match
1087 (maxima-re-search-backward-skip-blocks
1088 "\\<if\\>\\|\\<then\\>" pmin)))
1089 (setq match (downcase match))
1090 (cond ((maxima-string-equal match "if") (setq nest (1+ nest)))
1091 (t (setq nest (1- nest))))
1092 (when (= nest 1)
1093 (setq pt (point))
1094 (setq keep-looking nil))))
1095 (if keep-looking
1097 pt)))
1099 (defun maxima-begin-then-position (pmin)
1100 "Find the point of the opening \"then\" for the current \"else\"."
1101 (let ((keep-going t)
1102 (pt (point))
1103 (begin-then nil))
1104 (save-excursion
1105 (while (and keep-going
1106 (maxima-re-search-backward-skip-blocks "\\<then\\>" pmin))
1107 ;; A potential "then". Let's see if it is.
1108 (let ((meep (maxima-expression-end-position)))
1109 (when (or (not meep) (<= pt meep))
1110 ;; This "then" is looking pretty good.
1111 ;; Now we need to make sure that there aren't any "else"s
1112 ;; in the way.
1113 (let ((level 0)
1114 (match))
1115 (save-excursion
1116 (while (setq match (maxima-re-search-forward-skip-blocks
1117 "\\<then\\>\\|\\<else\\>" pt))
1118 (cond ((maxima-string-equal match "then")
1119 (setq level (1+ level)))
1120 ((maxima-string-equal match "else")
1121 (setq level (1- level))))))
1122 (when (= level 1)
1123 (setq begin-then (point))
1124 (setq keep-going nil))))))
1125 begin-then)))
1127 ;;;; Functions that move the position
1128 (defun maxima-forward-over-comment-whitespace ()
1129 "Move forward over comments and whitespace."
1130 (forward-comment (buffer-size))
1131 (let ((mmo (maxima-remove-whitespace-from-beg (maxima-minor-output-mark))))
1132 (when (and (> (- (point-max) (point)) (length mmo))
1133 (string=
1134 (buffer-substring-no-properties
1135 (point) (+ (point) (length mmo)))
1136 mmo))
1137 (search-forward (maxima-minor-output-mark-end))
1138 (forward-comment (buffer-size)))))
1140 (defun maxima-back-over-comment-whitespace ()
1141 "Move backward over comments and whitespace."
1142 (forward-comment (- (buffer-size)))
1143 (let ((mme (maxima-remove-whitespace-from-end (maxima-minor-output-mark-end))))
1144 (when (and (> (- (point) (point-min)) (length mme))
1145 (string=
1146 (buffer-substring-no-properties
1147 (- (point) (length mme)) (point))
1148 mme))
1149 (search-backward (maxima-minor-output-mark))
1150 (forward-comment (- (buffer-size))))))
1152 (defun maxima-standard-goto-beginning-of-form ()
1153 "Move to the beginning of the form."
1154 (let ((pt (point))
1155 (keep-looking t))
1156 (while (and keep-looking
1157 (maxima-re-search-backward "[;$]" nil))
1158 (forward-char -1)
1159 (unless (looking-at "\\\\\\$")
1160 (forward-char 2)
1161 (setq keep-looking nil)))
1162 (if keep-looking
1163 (goto-char (point-min)))
1164 (maxima-forward-over-comment-whitespace)
1165 (if (< pt (point))
1166 (goto-char pt))
1167 (point)))
1169 (defun maxima-noweb-goto-beginning-of-form ()
1170 "Move to the beginning of the form."
1171 (if (re-search-backward "^<<.*?>>= *$" (point-min) 1)
1172 (forward-line 1))
1173 (maxima-forward-over-comment-whitespace))
1175 (defun maxima-goto-beginning-of-form ()
1176 (cond
1177 ((eq maxima-mode-type 'maxima-noweb-mode)
1178 (maxima-noweb-goto-beginning-of-form))
1180 (maxima-standard-goto-beginning-of-form))))
1182 (defun maxima-goto-beginning-of-form-interactive ()
1183 "Move to the beginning of the form."
1184 (interactive)
1185 (maxima-goto-beginning-of-form))
1187 (defun maxima-standard-goto-end-of-form ()
1188 "Move to the end of the form."
1189 (let ((keep-looking t)
1190 (pt (point)))
1191 (while (and keep-looking
1192 (maxima-re-search-forward "[;$]" nil))
1193 (forward-char -2)
1194 (unless (looking-at "\\\\\\$")
1195 (setq keep-looking nil))
1196 (forward-char 2))
1197 (if (not keep-looking)
1198 (point)
1199 (goto-char pt)
1200 nil)))
1202 (defun maxima-noweb-goto-end-of-form ()
1203 "Move to the end of the form."
1204 (when (re-search-forward "\\(^@\\( \\|$\\)\\|^<<.*>>= *$\\)" nil 1)
1205 (forward-line -1)
1206 (end-of-line)
1207 (maxima-back-over-comment-whitespace)))
1209 (defun maxima-goto-end-of-form ()
1210 (cond
1211 ((eq maxima-mode-type 'maxima-noweb-mode)
1212 (maxima-noweb-goto-end-of-form))
1214 (maxima-standard-goto-end-of-form))))
1216 (defun maxima-goto-end-of-form-interactive ()
1217 "Move to the end of the form."
1218 (interactive)
1219 (unless (maxima-goto-end-of-form)
1220 (message "No end of form")))
1222 (defun maxima-goto-end-of-expression ()
1223 "Find the point that ends the expression that begins at point.
1224 The expression is assumed to begin with \"if\", \"then\", \"do\"
1225 \"else\" or \"(\". Return nil if the end is not found."
1226 ; (interactive)
1227 ;; To find the end of the expression:
1228 ;; if looking at (, look for )
1229 ;; otherwise look for a , ; or $ at the same nesting level of
1230 ;; parentheses or a closing ).
1231 (cond ((or (looking-at "(")
1232 (looking-at "\\["))
1233 (maxima-forward-list))
1235 (maxima-find-next-nonnested-close-char))))
1237 (defun maxima-goto-beginning-of-construct (pmin)
1238 "Go to the point the begins the current construct."
1239 (let ((keep-looking t)
1240 (pt (point)))
1241 (while (and keep-looking
1242 (maxima-re-search-backward-skip-blocks
1243 "\\<if\\>\\|\\<then\\>\\|\\<do\\>\\|\\<else\\>\\|(\\|\\[" pmin))
1244 (save-excursion
1245 (when (or (not (maxima-goto-end-of-expression)) (<= pt (point)))
1246 (setq keep-looking nil))))
1247 (if keep-looking
1248 (goto-char pmin))
1249 (point)))
1251 (defun maxima-goto-end-of-list ()
1252 "Go up a list.
1253 Return t if possible, nil otherwise."
1254 (interactive)
1256 (condition-case nil
1257 (up-list 1)
1258 (error t))
1262 (defun maxima-goto-end-of-list-interactive ()
1263 "Go up a list."
1264 (interactive)
1265 (if (maxima-goto-end-of-list)
1267 (error "No list to end.")))
1269 (defun maxima-goto-beginning-of-list ()
1270 "Go up a list backwards.
1271 Return t if possible, nil otherwise."
1273 (condition-case nil
1274 (up-list -1)
1275 (error t))
1279 (defun maxima-goto-beginning-of-list-interactive ()
1280 "Go up a list."
1281 (interactive)
1282 (if (maxima-goto-beginning-of-list)
1284 (error "No list to begin.")))
1286 (defun maxima-forward-list ()
1287 "Go forward a list.
1288 Return t if possible, nil otherwise."
1290 (condition-case nil
1291 (forward-list 1)
1292 (error nil))
1294 nil))
1296 (defun maxima-backward-list ()
1297 "Go backward a list.
1298 Return t if possible, nil otherwise."
1300 (condition-case nil
1301 (backward-list 1)
1302 (error nil))
1304 nil))
1306 ;;; Newlines and indents
1307 (defun maxima-indent-form ()
1308 "Indent the entire form."
1309 (interactive)
1310 (indent-region
1311 (maxima-form-beginning-position)
1312 (maxima-form-end-position-or-point-max)
1313 nil))
1315 ;;; 'standard
1316 (defun maxima-standard-indent ()
1317 "Indent the line based on the previous line.
1318 If the previous line opened a parenthesis, `maxima-indent-amount' is
1319 added to the indentation, if the previous line closed a parenthesis,
1320 `maxima-indent-amount' is subtracted, otherwise the indentation
1321 is the same as the previous line."
1322 (interactive)
1323 (let ((indent 0)
1324 (match)
1325 (pt))
1326 (save-excursion
1327 (when (= (forward-line -1) 0)
1328 (progn
1329 (setq indent (current-indentation))
1330 (setq pt (maxima-line-end-position))
1331 (while (setq match (maxima-re-search-forward "[()]" pt))
1332 (cond ((string= match ")")
1333 (setq indent (- indent maxima-indent-amount)))
1334 ((string= match "(")
1335 (setq indent (+ indent maxima-indent-amount))))))))
1336 (save-excursion
1337 (beginning-of-line)
1338 (delete-horizontal-space)
1339 (indent-line-to (max indent 0)))
1340 (skip-chars-forward " \t")))
1342 (defun maxima-untab ()
1343 "Delete a level of indentation"
1344 (interactive)
1345 ;; Check to see if the previous maxima-indent-amount spaces are
1346 ;; on the same line and blank
1347 (let ((i maxima-indent-amount)
1348 (ok t)
1349 (pt (point)))
1350 (save-excursion
1351 (while (and (> i 0) ok)
1352 (setq i (- i 1))
1353 (forward-char -1)
1354 (if (not (looking-at " "))
1355 (setq ok nil))
1356 (if (looking-at "\n")
1357 (setq ok nil))))
1358 (if ok
1359 (delete-region pt (- pt maxima-indent-amount)))))
1362 ;;; Find the beginning of a function
1364 (defun maxima-open-paren-is-function ()
1365 "Check to see if the point is before an opening paren,
1366 and if the previous character is a close paren or word part."
1367 (and
1368 (looking-at "[ \t]*(")
1369 (save-excursion
1370 (maxima-back-over-comment-whitespace)
1371 (forward-char -1)
1372 (looking-at ")\\|\\w"))))
1374 (defun maxima-close-paren-before-open-paren ()
1375 "Return point of close paren before the current open paren,
1376 or nil if there is none."
1377 (save-excursion
1378 (maxima-back-over-comment-whitespace)
1379 (forward-char -1)
1380 (if (looking-at ")")
1381 (1+ (point))
1382 nil)))
1384 (defun maxima-back-over-paren-groups ()
1385 "Go over any paren groups.
1386 Assume point is at \"(\", as long as preceding character is
1387 \")\", go to open parentheses."
1388 (let ((cpbop (maxima-close-paren-before-open-paren)))
1389 (while cpbop
1390 (goto-char cpbop)
1391 (maxima-backward-list)
1392 (setq cpbop
1393 (maxima-close-paren-before-open-paren)))))
1395 (defun maxima-word-on-previous-line ()
1396 "Go to the previous word part, return nil if "
1397 (let ((pt (point)))
1398 (save-excursion
1399 (skip-chars-backward " \t\n")
1400 ;; Same line?
1401 (and
1402 (maxima-previous-char-word-part-p)
1403 (string-match "\n"
1404 (buffer-substring-no-properties (point) pt))))))
1406 (defun maxima-string-on-previous-line ()
1407 "Go to the previous word part, return nil if "
1408 (let ((pt (point)))
1409 (save-excursion
1410 (skip-chars-backward " \t\n")
1411 ;; Same line?
1412 (forward-char -1)
1413 (and
1414 (looking-at "\"")
1415 (string-match "\n"
1416 (buffer-substring-no-properties (point) pt))))))
1418 (defun maxima-back-over-function-name ()
1419 "Go back over the `foo' in 'foo(x,y)', return the length of `foo'.
1420 Return nil if open paren is not part of function.
1421 Assumes the point is right before the open parenthesis."
1422 (let ((pt (point))
1423 (endpt))
1424 (maxima-back-over-paren-groups)
1425 (setq endpt (point))
1426 ;; Let's see what's before this
1427 (cond
1428 ;; There is a word right before this
1429 ((maxima-previous-char-word-part-p)
1430 (maxima-backward-word)
1431 (- endpt (point)))
1432 ;; There is a string before this
1433 ((save-excursion
1434 (forward-char -1)
1435 (looking-at "\""))
1436 (maxima-goto-beginning-of-string)
1437 (- endpt (point)))
1438 ;; There is a word before this on the previous line
1439 ((maxima-word-on-previous-line)
1440 (skip-chars-backward " \t\n")
1441 (setq endpt (point))
1442 (maxima-backward-word)
1443 (- endpt (point)))
1444 ;; There is a string before this on the previous line
1445 ((maxima-string-on-previous-line)
1446 (skip-chars-backward " \t\n")
1447 (setq endpt (point))
1448 (maxima-goto-beginning-of-string)
1449 (- endpt (point)))
1450 ;; This point is the original point
1451 ((= endpt pt)
1452 nil)
1453 ;; Finally, the last parenthesized expression is the function
1455 (save-excursion
1456 (maxima-forward-list)
1457 (setq endpt (point)))
1458 (- endpt (point))))))
1460 ;;; 'perhaps-smart
1462 (defun maxima-after-lisp-expression-p ()
1463 "Return non-nil if the point is right after a lisp expression."
1464 (let ((pm (point-min))
1465 (pt))
1466 (save-excursion
1467 (maxima-back-over-comment-whitespace)
1468 (setq pt (point))
1469 (condition-case nil
1470 (backward-sexp)
1471 (error t))
1472 (when (< (point) pt)
1473 (maxima-back-over-comment-whitespace)
1474 (if (< (point) (+ 5 pm))
1476 (forward-char -5)
1477 (if (looking-at ":lisp")
1478 (current-column)
1479 nil))))))
1481 (defun maxima-standard-perhaps-smart-calculate-indent ()
1482 "Return appropriate indentation for current line as Maxima code.
1483 Returns an integer: the column to indent to."
1484 (let ((indent 0)
1485 (pmin)
1486 (tmpchar)
1487 (pt)
1488 (le)
1489 (comma-line)
1490 (len)
1491 (pps))
1492 (save-excursion
1493 (beginning-of-line)
1494 (setq pt (point))
1495 (setq pmin (maxima-form-beginning-position))
1496 (setq pps (parse-partial-sexp pmin (point)))
1497 (setq le (maxima-after-lisp-expression-p))
1498 (when (nth 1 pps)
1499 (setq pmin (nth 1 pps))
1500 (unless (looking-at "^[ \t]*,")
1501 (save-excursion
1502 (when (maxima-re-search-backward-skip-blocks "," pmin)
1503 (save-excursion
1504 (let ((lep (maxima-line-end-position)))
1505 (forward-char 1)
1506 (maxima-forward-over-comment-whitespace)
1507 (unless (>= (point) lep)
1508 (setq pmin (point)))))))))
1509 (cond
1510 ;; First, take care of the cases where the indentation is clear
1511 ;; No indentation at the beginning of the buffer
1512 ((= pt (point-min))
1513 (setq indent 0))
1514 ;; Don't change the indentation if in a string
1515 ((nth 3 pps)
1516 (setq indent -1))
1517 ;; If the line begins a comment and comments aren't reindented,
1518 ;; don't reindent it
1519 ((and
1520 (looking-at "[ \t]*/\\*[ \t]*$")
1521 maxima-dont-reindent-some-comments)
1522 (setq indent -1))
1523 ;; Deal with comments separately
1524 ((maxima-perhaps-smart-in-comment-p (nth 4 pps) pmin pt)
1525 (setq indent (maxima-perhaps-smart-comment-calculate-indent)))
1526 ;; If the current point is the beginning of the form, the level is 0
1527 ((= (point) pmin)
1528 (setq indent 0))
1529 ;; If the current point is in maxima minor output, don't reindent it
1530 ((maxima-in-output-p)
1531 (setq indent -1))
1532 ;; A line beginning "then" is indented like the opening "if"
1533 ((and
1534 (looking-at "[ \t]*\\<then\\>")
1535 (setq tmpchar (maxima-begin-if-position pmin)))
1536 (goto-char tmpchar)
1537 (setq indent (+ maxima-if-extra-indent-amount (current-column))))
1538 ;; A line beginning "else" is indented like the corresponding "then"
1539 ((and
1540 (looking-at "[ \t]*\\<else\\>")
1541 (setq tmpchar (maxima-begin-then-position pmin)))
1542 (goto-char tmpchar)
1543 (setq indent (current-column)))
1544 ;; A line beginning with an open paren that is the
1545 ;; beginning of a function argument is indented according to
1546 ;; the function
1547 ((and
1548 (looking-at "[ \t]*(")
1549 (setq len (maxima-back-over-function-name)))
1550 (setq indent (+ (current-column)
1551 (min len maxima-function-indent-amount))))
1552 ;; A line beginning with a closing paren is indented like the open paren
1553 ((looking-at "[ \t]*)")
1554 ;; Here, pmin should be the opening paren position
1555 (goto-char pmin)
1556 (if (looking-at "( *$")
1557 (progn
1558 (setq len (maxima-back-over-function-name))
1559 (setq indent (+
1560 (if len
1561 (min len maxima-function-indent-amount)
1563 (current-column))))
1564 (setq indent (current-column))))
1565 ;; A line beginning with a closing bracket is indented like the open bracket
1566 ((looking-at "[ \t]*\\]")
1567 ;; Here, pmin should be the opening bracket position
1568 (goto-char pmin)
1569 (setq indent (current-column)))
1570 ; ;; A line beginning with a comma is indented like the opening paren
1571 ; ((looking-at "[ \t]*,")
1572 ; (goto-char pmin)
1573 ; (setq indent (current-column)))
1574 ;; The point is at the end of a lisp expression
1576 (setq indent le))
1577 ;; Otherwise, the correct indentation needs to be computed.
1579 ;; Find the indentation of beginning of the current construct
1580 ;; If begin-construct is nil, indent according to the opening paren
1581 (setq comma-line (looking-at "[ \t]*,"))
1582 (save-excursion
1583 (maxima-goto-beginning-of-construct pmin)
1584 (cond
1585 ;; The construct begins with a bracket
1586 ((looking-at "\\[")
1587 ; (if comma-line
1588 ; (setq indent (current-column))
1589 (setq indent (+ maxima-paren-indent-amount (current-column)))
1590 (forward-char 1)
1591 (skip-chars-forward " \t")
1592 (unless (looking-at "\n")
1593 (setq indent (current-column))));)
1594 ;; The construct begins with a paren
1595 ((looking-at "(")
1596 (cond
1597 ; (comma-line
1598 ; (setq indent (current-column)))
1599 ((save-excursion
1600 (let ((lep (maxima-line-end-position)))
1601 (forward-char 1)
1602 (maxima-forward-over-comment-whitespace)
1603 (>= (point) lep)))
1604 ;(looking-at "( *$")
1605 ;; Check to see if there is anything before the (
1606 (if (save-excursion
1607 (re-search-backward "\\(?:^[ \t]*\\)\\=" nil t))
1608 (setq tmpchar maxima-paren-indent-amount)
1609 (setq tmpchar 0))
1610 ;; If there is nothing after the (, there are two
1611 ;; cases
1612 ;; First, there is a function before it
1613 (if (setq len (maxima-back-over-function-name))
1614 (setq indent (+ (min len maxima-function-indent-amount)
1615 tmpchar
1616 (current-column)))
1617 ;; Otherwise,
1618 (setq indent (+ maxima-paren-indent-amount (current-column)))))
1619 ;; If there is something after the (, indent according to that
1621 (forward-char 1)
1622 (skip-chars-forward " \t")
1623 (setq indent (current-column)))))
1624 ;; The construct does not begin with a paren
1626 (setq indent (current-column)))))
1627 ;; Now, we need to possibly do some adjustments.
1628 ;; If the previous column doesn't end with close-char or a
1629 ;; parenthesis, assume that the current line in a continuation
1630 ;; of that line, and add to the indentation, unless the
1631 ;; previous line was the beginning of the construct containing
1632 ;; the point or only an open parenthesis.
1633 (if comma-line
1634 (let ((bol-pt (point))
1635 comma-pt
1636 diff)
1637 (skip-chars-forward " \t")
1638 (setq comma-pt (point))
1639 (forward-char 1)
1640 (skip-chars-forward " \t")
1641 (setq diff (- (point) comma-pt))
1642 (if (> diff indent)
1643 (let ((lineno
1644 (save-excursion
1645 (forward-line 0)
1646 (1+ (count-lines (point-min) (point))))))
1647 (message
1648 (format "Leading comma prevents proper indentation on line %d"
1649 lineno))
1650 (setq indent 0))
1651 (setq indent (- indent diff))))
1652 (maxima-back-over-comment-whitespace)
1653 (when (not (looking-at "^"))
1654 (forward-char -1)
1655 (if (not (or le (looking-at "[,;$]")))
1656 (setq indent (+ maxima-continuation-indent-amount indent))))))))
1657 indent))
1659 (defun maxima-noweb-perhaps-smart-calculate-indent ()
1660 (let ((indent nil)
1661 (pt))
1662 (save-excursion
1663 (beginning-of-line)
1664 (cond
1665 ((looking-at "^<<.*?>>=[ \t]*$")
1666 (setq indent -1))
1667 ((looking-at "^@[ \n]")
1668 (setq indent -1))
1670 (forward-line -1)
1671 (if (looking-at "^<<.*?>>=[ \t]*$")
1672 (setq indent -1)))))
1673 (if indent
1674 indent
1675 (maxima-standard-perhaps-smart-calculate-indent))))
1677 (defun maxima-perhaps-smart-calculate-indent ()
1678 (cond
1679 ((eq maxima-mode-type 'maxima-mode)
1680 (maxima-standard-perhaps-smart-calculate-indent))
1681 ((eq maxima-mode-type 'maxima-noweb-mode)
1682 (maxima-noweb-perhaps-smart-calculate-indent))))
1685 (defun maxima-perhaps-smart-in-comment-p (incomment pmin pt)
1686 "Determine if the point is in a comment or not."
1687 (cond
1688 ;; If we're told it's in a comment, then it is.
1689 (incomment
1691 ;; Otherwise, if pmin is less than point, we're not in a comment
1692 ((> pt pmin)
1693 nil)
1694 ;; Otherwise, we have to check it out
1696 (maxima-in-comment-p))))
1698 (defun maxima-perhaps-smart-comment-calculate-indent ()
1699 "Calculate the indentation of the current line,
1700 which is in a comment which begins on a previous line."
1701 (let ((indent 0)
1702 (blankline nil)
1703 (endcomment nil))
1704 (cond
1705 ((looking-at "^[ \t]*$")
1706 (setq blankline t))
1707 ((looking-at "^[ \t]*\\*/")
1708 (setq endcomment t)))
1709 ;; First of all, if the comment begins with `/*' and nothing
1710 ;; else on the line, don't indent it.
1711 (save-excursion
1712 (maxima-goto-beginning-of-comment)
1713 (cond
1714 ;; Take care of the case when comments won't be indented
1715 ((and
1716 (looking-at "/\\*[ \t]*$")
1717 maxima-dont-reindent-some-comments)
1718 (if blankline
1719 (setq indent (+ (current-column)
1720 maxima-multiline-comment-indent-amount))
1721 (setq indent -1)))
1722 ;; Now, the other cases
1723 ;; If the current line ends a column, indent it like the opening line
1724 (endcomment
1725 (setq indent (current-column)))
1726 ;; If the opening line has a blank after the `/*'
1727 ((looking-at "/\\*[ \t\n]")
1728 (forward-char 2)
1729 (skip-chars-forward " \t")
1730 (if (not (looking-at "\n"))
1731 (setq indent (current-column))
1732 (search-backward "/*")
1733 (setq indent (+ (current-column)
1734 maxima-multiline-comment-indent-amount))))
1736 (setq indent (+ (current-column)
1737 maxima-multiline-comment-indent-amount)))))
1738 indent))
1740 (defun maxima-perhaps-smart-indent-line ()
1741 "Reindent the current line."
1742 (interactive)
1743 (let ((indent (maxima-perhaps-smart-calculate-indent)))
1744 (unless (= indent -1)
1745 (save-excursion
1746 (beginning-of-line)
1747 (delete-horizontal-space)
1748 (indent-to indent)))
1749 (skip-chars-forward " \t")))
1751 ;;;; Indentation according to style
1753 (defun maxima-indent-line ()
1754 (interactive)
1755 (cond
1756 ((eq maxima-newline-style 'basic)
1757 (maxima-standard-indent))
1758 ((eq maxima-indent-style 'standard)
1759 (maxima-standard-indent))
1760 ((eq maxima-indent-style 'perhaps-smart)
1761 (maxima-perhaps-smart-indent-line))))
1763 (defun maxima-change-indent-style (new-style)
1764 "Change the newline style."
1765 (interactive "sNewline style (insert \"b\" for basic, \"s\" for standard, or \"p\" for perhaps-smart): ")
1766 (cond
1767 ((string= new-style "b")
1768 (setq maxima-indent-style 'basic))
1769 ((string= new-style "s")
1770 (setq maxima-indent-style 'standard))
1771 ((string= new-style "p")
1772 (setq maxima-indent-style 'perhaps-smart))))
1774 (defun maxima-return ()
1775 (interactive)
1776 (cond
1777 ((eq maxima-return-style 'newline)
1778 (newline))
1779 ((eq maxima-return-style 'newline-and-indent)
1780 (newline-and-indent))
1781 ((eq maxima-return-style 'reindent-then-newline-and-indent)
1782 (reindent-then-newline-and-indent))))
1784 ;;;; Commenting
1786 (defun maxima-insert-short-comment ()
1787 "Prompt for a comment."
1788 (interactive)
1789 (let ((comment (read-string "Comment: ")))
1790 (insert "/* " comment " */")
1791 (newline-and-indent)))
1793 (defun maxima-insert-long-comment ()
1794 "Insert a comment enviroment"
1795 (interactive)
1796 (indent-for-tab-command)
1797 (insert "/*")
1798 (newline)
1799 (newline)
1800 (insert "*/")
1801 (indent-for-tab-command)
1802 (forward-line -1)
1803 (indent-for-tab-command))
1805 (defun maxima-uncomment-region (beg end)
1806 "`uncomment-region' to use with the menu."
1807 (interactive "r")
1808 (uncomment-region beg end (universal-argument)))
1810 ;;;; Help functions
1812 (defun maxima-goto-info-node (node)
1813 (if maxima-running-xemacs
1814 (info "Maxima")
1815 (info-other-window (concat "(Maxima)" node))))
1817 (defun maxima-get-info-on-subject (subject &optional same-window)
1818 (if (or
1819 maxima-running-xemacs
1820 same-window)
1821 (progn
1822 (info "Maxima")
1823 (Info-menu "Function and Variable Index"))
1824 (info-other-window "(Maxima)Function and Variable Index"))
1825 (search-forward subject)
1826 (if maxima-running-xemacs
1827 (Info-follow-nearest-node (point))
1828 (Info-follow-nearest-node))
1829 (re-search-forward (concat "-.*: *" subject "\\( \\|$\\)"))
1830 (if (looking-at "^")
1831 (forward-line -1)
1832 (beginning-of-line)))
1834 (defun maxima-get-help ()
1835 "Get help on a given subject"
1836 (let ((pt)
1837 (place))
1838 (save-excursion
1839 (beginning-of-line)
1840 (skip-chars-forward "* ")
1841 (setq pt (point))
1842 (search-forward ":")
1843 (skip-chars-backward ": ")
1844 (setq name (buffer-substring-no-properties pt (point))))
1845 (maxima-get-info-on-subject name t)))
1846 ; (if (not maxima-running-xemacs)
1847 ; (info-other-window (concat "(Maxima)" place))
1848 ; (info "Maxima")
1849 ; (search-forward place)
1850 ; (Info-follow-nearest-node))
1851 ; (re-search-forward (concat "-.*: *" name "\\>"))
1852 ; (beginning-of-line)))
1854 (defun maxima-help (&optional arg)
1855 (interactive "P")
1856 (let* ((cw (current-word))
1857 (subj (if arg
1859 (completing-read (concat "Maxima help (" cw "): ")
1860 maxima-symbols nil nil nil nil cw))))
1861 (if (member (list subj) maxima-symbols)
1862 (maxima-get-info-on-subject subj)
1863 (message (concat "No help for \"" subj "\"")))))
1865 (defun maxima-help-at-point ()
1866 (interactive)
1867 (maxima-help t))
1869 (defun maxima-apropos (&optional arg)
1870 "Get help on a certain subject"
1871 (interactive "P")
1872 (let* ((cw (current-word))
1873 (expr (if arg
1875 (read-string (concat "Maxima help (" cw "): ")
1876 nil nil cw)))
1877 (maxima-help-buffer
1878 (get-buffer-create (concat "*Maxima Help*")))
1879 (have-info nil)
1880 expr-line
1881 (lmark))
1882 (set-buffer maxima-help-buffer)
1883 (maxima-remove-kill-buffer-hooks)
1884 (setq buffer-read-only nil)
1885 (erase-buffer)
1886 (insert "Maxima help for " expr "\n\n")
1887 (insert "[RET] will get help on the subject on the given line\n")
1888 (insert "q in the *info* buffer will return you here.\n")
1889 (insert "q in this buffer will exit Maxima help\n\n")
1890 (with-temp-buffer
1891 (require 'info)
1892 (Info-mode)
1893 (Info-goto-node "(Maxima)Function and Variable Index")
1894 (goto-char (point-min))
1895 (search-forward "Menu:")
1896 (forward-line 1)
1897 (beginning-of-line)
1898 (while (re-search-forward (concat "\\*.*" expr ".*:") nil t)
1899 (setq have-info t)
1900 (setq expr-line (buffer-substring-no-properties
1901 (maxima-line-beginning-position)
1902 (maxima-line-end-position)))
1903 (save-excursion
1904 (set-buffer maxima-help-buffer)
1905 (insert expr-line "\n"))))
1906 (if have-info
1907 (progn
1908 (set-buffer maxima-help-buffer)
1909 (defun maxima-help-subject ()
1910 (interactive)
1911 (maxima-get-help))
1912 (defun maxima-kill-help ()
1913 (interactive)
1914 (let ((buf (current-buffer)))
1915 (delete-window)
1916 (maxima-remove-kill-buffer-hooks)
1917 (kill-buffer buf)))
1918 (defun maxima-next-subject ()
1919 (interactive)
1920 (forward-char 1)
1921 (if (re-search-forward "^\\*" nil t)
1923 (goto-char (point-min))
1924 (re-search-forward "^\\*" nil t))
1925 (forward-char -1))
1926 (use-local-map (make-sparse-keymap))
1927 (define-key (current-local-map) "\C-m" 'maxima-help-subject)
1928 (define-key (current-local-map) "q" 'maxima-kill-help)
1929 (define-key (current-local-map) "\t" 'maxima-next-subject)
1930 (goto-char (point-min))
1931 (re-search-forward "^\\*")
1932 (forward-char -1)
1933 (pop-to-buffer maxima-help-buffer)
1934 (setq buffer-read-only t))
1935 (kill-buffer maxima-help-buffer)
1936 (message (concat "No help for \"" expr "\"")))))
1938 (defun maxima-apropos-at-point ()
1939 (interactive)
1940 (maxima-apropos t))
1942 (defun maxima-apropos-help ()
1943 (interactive)
1944 (maxima-help-dispatcher nil nil))
1946 (defun maxima-completion-help ()
1947 (interactive)
1948 (maxima-help-dispatcher nil t))
1950 (defun maxima-help-dispatcher (&optional arg1 arg2)
1951 (interactive)
1952 (cond
1953 ((or (looking-at "[a-zA-Z_]")
1954 (looking-at "?[a-zA-Z]")
1955 (looking-at "%[a-zA-Z]"))
1956 (if arg2
1957 (maxima-context-help)
1958 (maxima-help (current-word))))
1959 ((looking-at "?")
1960 (maxima-get-info-on-subject "\"\\?\""))
1961 ((looking-at "#")
1962 (maxima-get-info-on-subject "\"#\""))
1963 ((looking-at "\\.")
1964 (maxima-get-info-on-subject "\"\\.\""))
1965 ((looking-at "[:=!%']")
1966 (let ((expr (buffer-substring-no-properties
1967 (maxima-special-symbol-beginning) (maxima-special-symbol-end))))
1968 (cond
1969 ((or (string= expr "%") (string= expr "%%"))
1970 (maxima-get-info-on-subject expr)) ; % and %% are without double quotes
1971 ((string= expr "''")
1972 (maxima-get-info-on-subject "\"")) ; "''" is called """ in the manual
1973 ((or (string= expr ":") (string= expr "::")
1974 (string= expr ":=") (string= expr "::=")
1975 (string= expr "=") (string= expr "!") (string= expr "!!"))
1976 (maxima-get-info-on-subject (concat "\"" expr "\"")))
1977 (t (error "no help for %s" expr)))))
1978 (arg1
1979 (error "No help for %s" (char-to-string (char-after (point)))))
1980 (t ; point is behind a name?
1981 (save-excursion
1982 (progn
1983 (backward-char 1)
1984 (maxima-help-dispatcher t arg2))))))
1986 (defun maxima-context-help ()
1987 (interactive)
1988 (let* ((stub (current-word))
1989 (completions (all-completions (downcase stub) maxima-symbols)))
1990 (setq completions
1991 (mapcar
1992 (function upcase) completions))
1993 (if (member (upcase stub) completions)
1994 (setq completions (list (upcase stub))))
1995 (cond ((null completions)
1996 (message "No help for %s" stub))
1997 ((= 1 (length completions))
1998 (maxima-get-info-on-subject (car completions)))
1999 (t ; There's no unique completion.
2000 (maxima-help-variation completions)))))
2002 (defun maxima-help-variation (completions)
2003 "Get help on certain subjects."
2004 (let* ((maxima-help-buffer
2005 (get-buffer-create (concat "*Maxima Help*")))
2006 expr-line
2007 (lmark))
2008 (set-buffer maxima-help-buffer)
2009 (erase-buffer)
2010 (insert "Maxima help\n")
2011 (insert "[RET] will get help on the subject on the given line\n")
2012 (insert "q in the *info* buffer will return you here.\n")
2013 (insert "q in this buffer will exit Maxima help\n\n")
2014 (defun maxima-help-insert-line (expr)
2015 (re-search-forward (concat "\\* " expr ":") nil t)
2016 (setq expr-line (buffer-substring-no-properties
2017 (maxima-line-beginning-position)
2018 (maxima-line-end-position)))
2019 (save-excursion
2020 (set-buffer maxima-help-buffer)
2021 (insert expr-line "\n")))
2022 (with-temp-buffer
2023 (require 'info nil t)
2024 (Info-mode)
2025 (Info-goto-node "(Maxima)Function and Variable Index")
2026 (goto-char (point-min))
2027 (search-forward "Menu:")
2028 (forward-line 1)
2029 (beginning-of-line)
2030 (mapcar (function maxima-help-insert-line) completions))
2031 (goto-char (point-min))
2032 (defun maxima-help-subject ()
2033 (interactive)
2034 (maxima-get-help))
2035 (defun maxima-kill-help ()
2036 (interactive)
2037 (let ((buf (current-buffer)))
2038 (delete-window)
2039 (maxima-remove-kill-buffer-hooks)
2040 (kill-buffer buf)))
2041 (use-local-map (append (make-sparse-keymap) (current-local-map)))
2042 (define-key (current-local-map) "\C-m" 'maxima-help-subject)
2043 (define-key (current-local-map) "q" 'maxima-kill-help)
2044 (goto-char 1)
2045 (pop-to-buffer maxima-help-buffer)))
2047 (defun maxima-info ()
2048 "Read the info file for Maxima."
2049 (interactive)
2050 (if maxima-running-xemacs
2051 (info "Maxima")
2052 (info-other-window "Maxima")))
2054 ;;; The help map
2056 (defvar maxima-help-map nil)
2057 (if maxima-help-map
2059 (let ((map (make-sparse-keymap)))
2060 (define-key map "h" 'maxima-help)
2061 (define-key map "d" 'maxima-completion-help)
2062 (define-key map "\C-d" 'maxima-completion-help)
2063 (define-key map "i" 'maxima-info)
2064 (define-key map "\C-i" 'maxima-info)
2065 (define-key map "m" 'maxima-info)
2066 (define-key map "\C-m" 'maxima-info)
2067 (define-key map "a" 'maxima-apropos)
2068 (define-key map "\C-a" 'maxima-apropos)
2069 (define-key map "p" 'maxima-apropos-help)
2070 (define-key map "\C-p" 'maxima-apropos-help)
2071 (setq maxima-help-map map)))
2073 ;;;; Completion
2075 ;;; This next functions are from hippie-expand.el
2076 (defun maxima-he-capitalize-first (str)
2077 (save-match-data
2078 (if (string-match "\\Sw*\\(\\sw\\).*" str)
2079 (let ((res (downcase str))
2080 (no (match-beginning 1)))
2081 (aset res no (upcase (aref str no)))
2082 res)
2083 str)))
2085 (defun maxima-he-ordinary-case-p (str)
2086 (or (string= str (downcase str))
2087 (string= str (upcase str))
2088 (string= str (capitalize str))
2089 (string= str (maxima-he-capitalize-first str))))
2092 (defun maxima-he-transfer-case (from-str to-str)
2093 (cond ((string= from-str (substring to-str 0 (min (length from-str)
2094 (length to-str))))
2095 to-str)
2096 ((not (maxima-he-ordinary-case-p to-str))
2097 to-str)
2098 ((string= from-str (downcase from-str))
2099 (downcase to-str))
2100 ((string= from-str (upcase from-str))
2101 (upcase to-str))
2102 ((string= from-str (maxima-he-capitalize-first from-str))
2103 (maxima-he-capitalize-first to-str))
2104 ((string= from-str (capitalize from-str))
2105 (capitalize to-str))
2107 to-str)))
2109 ;;; The next functions are from comint.el in cvs emacs
2110 (if (and
2111 (not maxima-running-xemacs)
2113 (< emacs-major-version 21)
2114 (< emacs-minor-version 3)))
2115 (defun comint-dynamic-list-completions (completions)
2116 "List in help buffer sorted COMPLETIONS.
2117 Typing SPC flushes the help buffer."
2118 (let ((window (get-buffer-window "*Completions*")))
2119 (setq completions (sort completions 'string-lessp))
2120 (if (and (eq last-command this-command)
2121 window (window-live-p window) (window-buffer window)
2122 (buffer-name (window-buffer window))
2123 ;; The above tests are not sufficient to detect the case where we
2124 ;; should scroll, because the top-level interactive command may
2125 ;; not have displayed a completions window the last time it was
2126 ;; invoked, and there may be such a window left over from a
2127 ;; previous completion command with a different set of
2128 ;; completions. To detect that case, we also test that the set
2129 ;; of displayed completions is in fact the same as the previously
2130 ;; displayed set.
2131 (equal completions
2132 (buffer-local-value 'comint-displayed-dynamic-completions
2133 (window-buffer window))))
2134 ;; If this command was repeated, and
2135 ;; there's a fresh completion window with a live buffer,
2136 ;; and this command is repeated, scroll that window.
2137 (with-current-buffer (window-buffer window)
2138 (if (pos-visible-in-window-p (point-max) window)
2139 (set-window-start window (point-min))
2140 (save-selected-window
2141 (select-window window)
2142 (scroll-up))))
2143 ;; Display a completion list for the first time.
2144 (setq comint-dynamic-list-completions-config
2145 (current-window-configuration))
2146 (with-output-to-temp-buffer "*Completions*"
2147 (display-completion-list completions))
2148 (message "Type space to flush; repeat completion command to scroll"))
2150 ;; Read the next key, to process SPC.
2151 (let (key first)
2152 (if (with-current-buffer (get-buffer "*Completions*")
2153 (set (make-local-variable 'comint-displayed-dynamic-completions)
2154 completions)
2155 (setq key (read-key-sequence nil)
2156 first (aref key 0))
2157 (and (consp first) (consp (event-start first))
2158 (eq (window-buffer (posn-window (event-start first)))
2159 (get-buffer "*Completions*"))
2160 (eq (key-binding key) 'mouse-choose-completion)))
2161 ;; If the user does mouse-choose-completion with the mouse,
2162 ;; execute the command, then delete the completion window.
2163 (progn
2164 (mouse-choose-completion first)
2165 (set-window-configuration comint-dynamic-list-completions-config))
2166 (unless (eq first ?\ )
2167 (setq unread-command-events (listify-key-sequence key)))
2168 (unless (eq first ?\t)
2169 (set-window-configuration comint-dynamic-list-completions-config)))))))
2172 (defun maxima-complete-symbol ()
2173 "Complete word from list of candidates.
2174 A completions listing will be shown in a help buffer
2175 if completion is ambiguous."
2176 (let* ((comint-completion-addsuffix nil)
2177 (stub (buffer-substring-no-properties
2178 (maxima-name-beginning) (point)))
2179 (completions (all-completions (downcase stub) maxima-symbols)))
2180 (comint-dynamic-simple-complete stub completions)))
2182 (defun maxima-complete-filename ()
2183 "Complete the filename."
2184 (comint-dynamic-complete-filename))
2186 (defun maxima-complete ()
2187 "Complete the current object, depending on context."
2188 (interactive)
2189 (let* ((pmin (maxima-form-beginning-position))
2190 (pps (parse-partial-sexp pmin (point))))
2191 (cond
2192 ;; complete filename if the point is in a string
2193 ((nth 3 pps)
2194 (maxima-complete-filename))
2195 ;; Otherwise, complete the symbol
2197 (maxima-complete-symbol)))))
2199 ;; ;;; Use hippie-expand to help with completions
2200 ;; (require 'hippie-exp)
2202 ;; (defun maxima-he-try (old)
2203 ;; (interactive)
2204 ;; (if (not old)
2205 ;; ;;; let beg be the beginning of the word
2206 ;; (progn
2207 ;; (he-init-string (maxima-name-beginning) (point))
2208 ;; (setq he-expand-list
2209 ;; (all-completions (downcase he-search-string) maxima-symbols))
2210 ;; (setq he-expand-list
2211 ;; (mapcar (function
2212 ;; (lambda (x) (he-transfer-case he-search-string x)))
2213 ;; he-expand-list))
2214 ;; (if he-expand-list
2215 ;; (he-substitute-string (car he-expand-list))
2216 ;; nil))
2217 ;; (setq he-expand-list (cdr he-expand-list))
2218 ;; (if he-expand-list
2219 ;; (he-substitute-string (car he-expand-list))
2220 ;; (he-reset-string)
2221 ;; nil)))
2223 ;; (fset 'maxima-dynamic-complete
2224 ;; (make-hippie-expand-function '(maxima-he-try)))
2226 ;;;; Miscellaneous
2228 (defun maxima-mark-form ()
2229 "Make the current form as the region."
2230 (interactive)
2231 (maxima-goto-beginning-of-form)
2232 (set-mark (maxima-form-end-position-or-point-max)))
2234 (defun maxima-check-commas (beg end)
2235 "Check to see if there is a stray comma at the beginning or end."
2236 (let ((commapt nil))
2237 (save-excursion
2238 (goto-char beg)
2239 (maxima-forward-over-comment-whitespace)
2240 (if (looking-at ",")
2241 (setq commapt (point))
2242 (goto-char end)
2243 (maxima-back-over-comment-whitespace)
2244 (when (save-excursion
2245 (forward-char -1)
2246 (looking-at "[;$]"))
2247 (forward-char -1)
2248 (maxima-back-over-comment-whitespace))
2249 (forward-char -1)
2250 (if (looking-at ",")
2251 (setq commapt (point)))))
2252 (if commapt
2253 (progn
2254 (message "Misplaced comma")
2255 (goto-char commapt)
2256 nil)
2257 t)))
2259 (defun maxima-check-parens (beg end)
2260 "Check to make sure that the parentheses are balanced in the region."
2261 (interactive "r")
2262 (let* ((tmpfile (maxima-make-temp-name))
2263 (tmpbuf (get-buffer-create tmpfile))
2264 (string (buffer-substring-no-properties beg end))
2265 (keep-going t)
2266 (match)
2267 (pt)
2268 (errmessage nil)
2269 (parenstack nil))
2270 (save-excursion
2271 (set-buffer tmpbuf)
2272 (maxima-mode)
2273 (maxima-remove-kill-buffer-hooks)
2274 (modify-syntax-entry ?/ ". 14")
2275 (modify-syntax-entry ?* ". 23")
2276 (insert string)
2277 (goto-char (point-min))
2278 (while (and (not errmessage)
2279 (setq match (maxima-re-search-forward "[][()]" end)))
2280 (unless (save-excursion
2281 (forward-char -1)
2282 (maxima-escaped-char-p))
2283 (cond
2284 ((string= match "(")
2285 (setq parenstack (cons (cons 1 (1- (point))) parenstack)))
2286 ((string= match "[")
2287 (setq parenstack (cons (cons 2 (1- (point))) parenstack)))
2288 ((string= match ")")
2289 (cond
2290 ((not parenstack)
2291 (setq errmessage "Unmatched close parenthesis")
2292 (setq pt (1- (point))))
2293 ((= (caar parenstack) 1)
2294 (setq parenstack (cdr parenstack)))
2295 ((= (caar parenstack) 2)
2296 (setq errmessage "Open bracket closed by parenthesis")
2297 (setq pt (1- (point))))))
2298 ((string= match "]")
2299 (cond
2300 ((not parenstack)
2301 (setq errmessage "Unmatched close bracket")
2302 (setq pt (1- (point))))
2303 ((= (caar parenstack) 2)
2304 (setq parenstack (cdr parenstack)))
2305 ((= (caar parenstack) 1)
2306 (setq errmessage "Open parenthesis closed by bracket")
2307 (setq pt (1- (point))))))))))
2308 (kill-buffer tmpbuf)
2309 (cond
2310 ((not (or parenstack errmessage))
2311 ; (message "Parenthesis and brackets match")
2313 (errmessage
2314 (message errmessage)
2315 (goto-char (1- (+ beg pt)))
2316 nil)
2318 (cond
2319 ((= (caar parenstack) 1)
2320 (message "Unmatched open parenthesis")
2321 (goto-char (1- (+ beg (cdar parenstack))))
2322 nil)
2324 (message "Unmatched open bracket")
2325 (goto-char (+ beg (cdar parenstack)))
2326 nil))))))
2329 (defun maxima-check-form-parens ()
2330 "Check to see if the parentheses in the current form are balanced."
2331 (interactive)
2332 (maxima-check-parens (maxima-form-beginning-position)
2333 (maxima-form-end-position-or-point-max)))
2335 (defun maxima-load-file (file)
2336 "Prompt for a Maxima file to load."
2337 (interactive "fMaxima file: ")
2338 (maxima-string (concat "load(\"" (expand-file-name file) "\");")))
2340 (defun maxima-load-current-file ()
2341 "Load the current file into Maxima."
2342 (interactive)
2343 (maxima-string (concat "load(\"" buffer-file-name "\");")))
2345 ;;; For highlighting the region being sent
2347 (defun maxima-mode-add-highlight ()
2348 (maxima-mode-remove-highlight)
2349 (if (and maxima-mode-region-begin maxima-mode-region-end)
2350 (if maxima-running-xemacs
2351 (progn
2352 (setq maxima-mode-highlight
2353 (make-extent
2354 maxima-mode-region-begin
2355 maxima-mode-region-end))
2356 (set-extent-property maxima-mode-highlight 'face 'highlight))
2357 (setq maxima-mode-highlight
2358 (make-overlay
2359 maxima-mode-region-begin
2360 maxima-mode-region-end))
2361 (overlay-put maxima-mode-highlight 'face 'highlight)))
2362 (setq maxima-mode-region-begin nil)
2363 (setq maxima-mode-region-end nil))
2365 (defun maxima-mode-remove-highlight ()
2366 (when maxima-mode-highlight
2367 (if maxima-running-xemacs
2368 (delete-extent maxima-mode-highlight)
2369 (delete-overlay maxima-mode-highlight))
2370 (setq maxima-mode-highlight nil)))
2372 (defun maxima-mode-add-remove-highlight ()
2373 (if (or
2374 (eq this-command 'maxima-send-region)
2375 (eq this-command 'maxima-send-buffer)
2376 (eq this-command 'maxima-send-line)
2377 (eq this-command 'maxima-send-form)
2378 (eq this-command 'maxima-send-previous-form)
2379 (eq this-command 'maxima-send-previous-form-and-goto-end-of-form)
2380 (eq this-command 'maxima-send-full-line)
2381 (eq this-command 'maxima-send-full-line-and-goto-next-form)
2382 (eq this-command 'maxima-send-completed-region)
2383 (eq this-command 'maxima-send-completed-region-and-goto-next-form)
2384 (eq this-command 'maxima-minibuffer-on-region)
2385 (eq this-command 'maxima-minibuffer-on-form)
2386 (eq this-command 'maxima-minibuffer-on-line))
2387 (maxima-mode-add-highlight)
2388 (maxima-mode-remove-highlight)))
2390 ;;;; Syntax table
2392 (defvar maxima-mode-syntax-table nil "")
2394 (if (not maxima-mode-syntax-table)
2395 (let ((i 0))
2396 (setq maxima-mode-syntax-table (make-syntax-table))
2397 (modify-syntax-entry ?_ "w" maxima-mode-syntax-table)
2398 (modify-syntax-entry ?% "w" maxima-mode-syntax-table)
2399 (modify-syntax-entry ?? "w" maxima-mode-syntax-table)
2400 ; (modify-syntax-entry ?\_ "w" maxima-mode-syntax-table)
2401 ;; (while (< i ?0)
2402 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2403 ;; (setq i (1+ i)))
2404 ;; (setq i (1+ ?9))
2405 ;; (while (< i ?A)
2406 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2407 ;; (setq i (1+ i)))
2408 ;; (setq i (1+ ?Z))
2409 ;; (while (< i ?a)
2410 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2411 ;; (setq i (1+ i)))
2412 ;; (setq i (1+ ?z))
2413 ;; (while (< i 128)
2414 ;; (modify-syntax-entry i "_ " maxima-mode-syntax-table)
2415 ;; (setq i (1+ i)))
2416 (modify-syntax-entry ? " " maxima-mode-syntax-table)
2417 (modify-syntax-entry ?\t " " maxima-mode-syntax-table)
2418 (modify-syntax-entry ?` "' " maxima-mode-syntax-table)
2419 (modify-syntax-entry ?' "' " maxima-mode-syntax-table)
2420 (modify-syntax-entry ?, "' " maxima-mode-syntax-table)
2421 (modify-syntax-entry ?. "w" maxima-mode-syntax-table)
2422 (modify-syntax-entry ?# "' " maxima-mode-syntax-table)
2423 (modify-syntax-entry ?\\ "\\" maxima-mode-syntax-table)
2424 (modify-syntax-entry ?/ ". 14" maxima-mode-syntax-table)
2425 (modify-syntax-entry ?* ". 23" 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)
2434 (modify-syntax-entry ?\\ "\\ " maxima-mode-syntax-table)
2435 (modify-syntax-entry ?\( "() " maxima-mode-syntax-table)
2436 (modify-syntax-entry ?\) ")( " maxima-mode-syntax-table)
2437 (modify-syntax-entry ?\[ "(] " maxima-mode-syntax-table)
2438 (modify-syntax-entry ?\] ")[ " maxima-mode-syntax-table)))
2441 ;;;; Keymap
2443 (defvar maxima-mode-map nil
2444 "The keymap for maxima-mode")
2446 (if maxima-mode-map
2448 (let ((map (make-sparse-keymap)))
2449 ;; Motion
2450 (define-key map "\M-\C-a" 'maxima-goto-beginning-of-form-interactive)
2451 (define-key map "\M-\C-e" 'maxima-goto-end-of-form-interactive)
2452 (define-key map "\M-\C-b" 'maxima-goto-beginning-of-list-interactive)
2453 (define-key map "\M-\C-f" 'maxima-goto-end-of-list-interactive)
2454 ;; Process
2455 (define-key map "\C-c\C-p" 'maxima-display-buffer)
2456 (define-key map "\C-c\C-r" 'maxima-send-region)
2457 (define-key map "\C-c\C-b" 'maxima-send-buffer)
2458 (define-key map "\C-c\C-c" 'maxima-send-line)
2459 (define-key map "\C-c\C-e" 'maxima-send-previous-form)
2460 (define-key map "\C-c\C-s" 'maxima-send-previous-form-and-goto-end-of-form)
2461 (define-key map [(control return)]
2462 'maxima-send-full-line-and-goto-next-form)
2463 (define-key map [(meta return)]
2464 'maxima-send-completed-region-and-goto-next-form)
2465 (define-key map [(control meta return)] 'maxima-send-buffer)
2466 (define-key map "\C-c\C-k" 'maxima-stop)
2467 (define-key map "\C-c\C-q" 'maxima-clear-queue)
2468 (define-key map "\C-c\C-l" 'maxima-load-file)
2469 (define-key map "\C-c\C-f" 'maxima-load-current-file)
2470 ;; Completion
2471 ;(if maxima-use-dynamic-complete
2472 ; (define-key map (kbd "M-TAB") 'maxima-dynamic-complete)
2473 (define-key map (kbd "M-TAB") 'maxima-complete)
2474 ;; Commenting
2475 (define-key map "\C-c;" 'comment-region)
2476 (define-key map "\C-c:" 'maxima-uncomment-region)
2477 (define-key map "\M-;" 'maxima-insert-short-comment)
2478 (define-key map "\C-c*" 'maxima-insert-long-comment)
2479 ;; Indentation
2480 ; (define-key map "\t" 'maxima-reindent-line)
2481 (define-key map "\C-m" 'maxima-return)
2482 (define-key map "\M-\C-q" 'maxima-indent-form)
2483 ; (define-key map [(control tab)] 'maxima-untab)
2484 ;; Help
2485 (define-key map "\C-c\C-d" maxima-help-map)
2486 (define-key map [(f12)] 'maxima-help)
2487 (define-key map [(meta f12)] 'maxima-apropos)
2488 ;; Minibuffer
2489 (define-key map "\C-c\C-nr" 'maxima-minibuffer-on-region)
2490 (define-key map "\C-c\C-nl" 'maxima-minibuffer-on-line)
2491 (define-key map "\C-c\C-nf" 'maxima-minibuffer-on-form)
2492 ;; Misc
2493 (define-key map "\M-h" 'maxima-mark-form)
2494 (define-key map "\C-c\)" 'maxima-check-parens)
2495 (define-key map [(control c) (control \))] 'maxima-check-form-parens)
2496 ; (define-key map "\C-cC-\)" 'maxima-check-form-parens)
2497 (define-key map "\177" 'backward-delete-char-untabify)
2498 (setq maxima-mode-map map)))
2500 ;;;; Menu
2502 (easy-menu-define maxima-mode-menu maxima-mode-map "Maxima mode menu"
2503 '("Maxima"
2504 ("Motion"
2505 ["Beginning of form" maxima-goto-beginning-of-form-interactive t]
2506 ["End of form" maxima-goto-end-of-form-interactive t]
2507 ["Beginning of sexp" maxima-goto-beginning-of-list-interactive t]
2508 ["End of sexp" maxima-goto-end-of-list-interactive t])
2509 ("Process"
2510 ["Start process" maxima-start t]
2511 ["Send region" maxima-send-region t]
2512 ["Send buffer" maxima-send-buffer t]
2513 ["Send line" maxima-send-line t]
2514 ["Send form" maxima-send-form t]
2515 ["Load file" maxima-load-file t]
2516 "----"
2517 ["Display buffer" maxima-display-buffer t]
2518 "----"
2519 ["Kill process" maxima-stop t])
2520 ("Indentation"
2521 ; ["Change to basic" (maxima-change-indent-style "b")
2522 ; (not (eq maxima-newline-style 'basic))]
2523 ["Change to standard" (maxima-change-indent-style "s")
2524 (not (eq maxima-indent-style 'standard))]
2525 ["Change to smart" (maxima-change-indent-style "p")
2526 (not (eq maxima-indent-style 'perhaps-smart))])
2527 ("Misc"
2528 ["Mark form" maxima-mark-form t]
2529 ["Check parens in region" maxima-check-parens t]
2530 ["Check parens in form" maxima-check-form-parens t]
2531 ["Comment region" comment-region t]
2532 ["Uncomment region" maxima-uncomment-region t])
2533 ("Help"
2534 ["Maxima info" maxima-info t]
2535 ["Help" maxima-help t])))
2538 ;;;; Variable setup
2539 ;;;; (These are used in both maxima-mode and inferior-maxima-mode).
2541 (defvar maxima-mode-abbrev-table nil "")
2543 (defun maxima-mode-variables ()
2544 "Sets all the necessary variables for maxima-mode"
2545 (set-syntax-table maxima-mode-syntax-table)
2546 (setq local-abbrev-table maxima-mode-abbrev-table)
2547 (make-local-variable 'paragraph-start)
2548 (setq paragraph-start (concat "^$\\|" page-delimiter))
2549 (make-local-variable 'paragraph-separate)
2550 (setq paragraph-separate paragraph-start)
2551 (make-local-variable 'indent-line-function)
2552 (setq indent-line-function 'maxima-indent-line)
2553 (make-local-variable 'indent-tabs-mode)
2554 (unless maxima-use-tabs
2555 (setq indent-tabs-mode nil))
2556 (make-local-variable 'case-fold-search)
2557 (setq case-fold-search t)
2558 (make-local-variable 'comment-start)
2559 (setq comment-start "/*")
2560 (make-local-variable 'comment-end)
2561 (setq comment-end "*/")
2562 (make-local-variable 'comment-start-skip)
2563 (setq comment-start-skip "/\\*+ *")
2564 (make-local-variable 'comment-column)
2565 (setq comment-column 40)
2566 (make-local-variable 'comment-indent-function)
2567 (setq comment-indent-function 'maxima-comment-indent)
2568 (setq imenu-generic-expression
2569 (list '(nil "^ *\\([a-zA-Z0-9_]*\\) *(.*) *:=" 1))))
2572 ;;;; Maxima mode
2574 (defun maxima-mode ()
2575 "Major mode for editing Maxima code.
2577 Maxima mode provides the following motion commands:
2578 \\[maxima-goto-beginning-of-form-interactive]: Move to the beginning of the form.
2579 \\[maxima-goto-end-of-form-interactive]: Move to the end of the form.
2580 \\[maxima-goto-beginning-of-list-interactive]: Move to the beginning of the sexp.
2581 \\[maxima-goto-end-of-list-interactive]: Move to the end of the sexp.
2583 and the following miscellaneous commands.
2584 \\[maxima-mark-form]: Mark the current form
2585 \\[maxima-check-parens]: Check the current region for balanced parentheses.
2586 \\[maxima-check-form-parens]: Check the current form for balanced parentheses.
2588 Maxima mode has the following completions commands:
2589 M-TAB: Complete the Maxima symbol as much as possible, providing
2590 a completion buffer if there is more than one possible completion.
2592 Portions of the buffer can be sent to a Maxima process. (If a process is
2593 not running, one will be started.)
2594 \\[maxima-send-region]: Send the region to Maxima.
2595 \\[maxima-send-buffer]: Send the buffer to Maxima.
2596 \\[maxima-send-line]: Send the line to Maxima.
2597 \\[maxima-send-form]: Send the form to Maxima.
2598 \\[maxima-send-full-line-and-goto-next-form]: Send the smallest set of lines which contains
2599 the cursor and contains no incomplete forms, and go to the next form.
2600 \\[maxima-send-completed-region-and-goto-next-form]: As above, but with
2601 the region instead of the current line.
2602 \\[maxima-load-file] will prompt for a filename and load it into Maxima
2603 When something is sent to Maxima, a buffer running an inferior Maxima
2604 process will appear. It can also be made to appear by using the command
2605 \\[maxima-display-buffer].
2606 If an argument is given to a command to send information to Maxima,
2607 the region (buffer, line, form) will first be checked to make sure
2608 the parentheses are balanced.
2609 The Maxima process can be killed, after asking for confirmation
2610 with \\[maxima-stop]. To kill without confirmation, give \\[maxima-stop]
2611 an argument.
2613 By default, indentation will be to the same level as the
2614 previous line, with an additional space added for open parentheses.
2615 The behaviour of indent can be changed by the command
2616 \\[maxima-change-indent-style]. The possibilities are:
2617 Standard: Standard indentation.
2618 Perhaps smart: Tries to guess an appropriate indentation, based on
2619 open parentheses, \"do\" loops, etc.
2620 The default can be set by setting the value of the variable
2621 \"maxima-indent-style\" to either 'standard or 'perhaps-smart.
2622 In both cases, \\[maxima-untab] will remove a level of indentation.
2624 To get help on a Maxima topic, use:
2625 \\[maxima-help].
2626 To read the Maxima info manual, use:
2627 \\[maxima-info].
2628 To get help with the symbol under point, use:
2629 \\[maxima-completion-help].
2630 To get apropos with the symbol under point, use:
2631 \\[maxima-apropos-help].
2633 \\{maxima-mode-map}
2635 (interactive)
2636 (kill-all-local-variables)
2637 (setq major-mode 'maxima-mode)
2638 (setq mode-name "Maxima")
2639 (use-local-map maxima-mode-map)
2640 (maxima-mode-variables)
2641 (cond
2642 ((eq maxima-newline-style 'basic)
2643 (setq maxima-indent-style 'standard))
2644 ((eq maxima-newline-style 'standard)
2645 (setq maxima-indent-style 'standard))
2646 ((eq maxima-newline-style 'perhaps-smart)
2647 (setq maxima-indent-style 'perhaps-smart)))
2648 (easy-menu-add maxima-mode-menu maxima-mode-map)
2649 (if maxima-running-xemacs
2650 (add-local-hook 'post-command-hook
2651 'maxima-mode-add-remove-highlight)
2652 (add-hook 'post-command-hook
2653 'maxima-mode-add-remove-highlight nil t))
2654 (run-hooks 'maxima-mode-hook))
2656 (define-derived-mode maxima-noweb-mode maxima-mode
2657 "Maxima Noweb Mode"
2658 (setq maxima-mode-type 'maxima-noweb-mode))
2660 ;;;; Interacting with the Maxima process
2662 ;;; Checking on the process
2663 (defun inferior-maxima-running ()
2664 (and (processp inferior-maxima-process)
2665 (eq (process-status inferior-maxima-process) 'run)))
2667 ;;; Sending the information
2668 (defun inferior-maxima-get-old-input ()
2669 (let (pt pt1)
2670 (save-excursion
2671 (if (re-search-forward
2672 (concat "\\(^(\\(" maxima-outchar "\\|" maxima-linechar "\\)[0-9]*) \\)")
2673 nil 1)
2674 (goto-char (match-beginning 0)))
2675 (skip-chars-backward " \t\n")
2676 (setq pt (point)))
2677 (save-excursion
2678 (re-search-backward inferior-maxima-prompt)
2679 (setq pt1 (match-end 0)))
2680 (buffer-substring-no-properties pt1 pt)))
2682 (defun inferior-maxima-comint-send-input (&optional query)
2683 "Take note of position, then send the input"
2684 (unless query
2685 (setq inferior-maxima-input-end (point)))
2686 (setq inferior-maxima-waiting-for-output t)
2687 (comint-send-input))
2689 ;;; This next function is a modified version of comint-strip-ctrl-m
2690 (defun inferior-maxima-remove-double-prompt (&optional string)
2691 "Fix the double prompt that occasionally appears in XEmacs."
2692 (let ((pmark (process-mark inferior-maxima-process))
2693 (pos))
2694 (set-buffer (process-buffer inferior-maxima-process))
2695 (setq pos comint-last-output-start)
2696 (if (marker-position pos)
2697 (save-excursion
2698 (goto-char pos)
2699 (beginning-of-line)
2700 (while (re-search-forward
2701 (concat "(" maxima-inchar "[0-9]+).*\r") pmark t)
2702 (replace-match "" t t))))))
2704 (defun inferior-maxima-remove-double-input-prompt (&optional string)
2705 "Fix the double prompt that occasionally appears in Emacs."
2706 (let ((pmark (process-mark inferior-maxima-process))
2707 (pos))
2708 (save-excursion
2709 (set-buffer (process-buffer inferior-maxima-process))
2710 (goto-char inferior-maxima-input-end)
2711 (forward-line 1)
2712 (if (looking-at (concat "(" maxima-inchar "[0-9]+)"))
2713 (kill-line 1))
2714 (if (looking-at " ")
2715 (delete-char 1)))))
2717 ;;; This next function will replace tabs in the output by spaces
2718 ;; untabify isn't defined in xemacs
2719 (unless (fboundp 'untabify)
2720 (defun untabify (start end)
2721 "Convert all tabs in region to multiple spaces, preserving columns.
2722 Called non-interactively, the region is specified by arguments
2723 START and END, rather than by the position of point and mark.
2724 The variable `tab-width' controls the spacing of tab stops."
2725 (interactive "r")
2726 (save-excursion
2727 (save-restriction
2728 (narrow-to-region (point-min) end)
2729 (goto-char start)
2730 (while (search-forward "\t" nil t) ; faster than re-search
2731 (forward-char -1)
2732 (let ((tab-beg (point))
2733 (indent-tabs-mode nil)
2734 column)
2735 (skip-chars-forward "\t")
2736 (setq column (current-column))
2737 (delete-region tab-beg (point))
2738 (indent-to column)))))))
2740 (defun inferior-maxima-replace-tabs-by-spaces (&optional string)
2741 "Replace tabs in the Maxima output by spaces."
2742 (let ((beg))
2743 (set-buffer (process-buffer inferior-maxima-process))
2744 (if (marker-position comint-last-output-start)
2745 (setq beg comint-last-output-start)
2746 (setq beg (point-min)))
2747 (untabify beg
2748 (process-mark inferior-maxima-process))))
2750 (defun inferior-maxima-wait-for-output ()
2751 "Wait for output from the Maxima process."
2752 (when (and
2753 inferior-maxima-waiting-for-output
2754 (inferior-maxima-running))
2755 (accept-process-output inferior-maxima-process))
2756 (if maxima-running-xemacs
2757 (sleep-for 0.1)
2758 (sit-for 0 inferior-maxima-after-output-wait)))
2760 (defun inferior-maxima-output-filter (str)
2761 "Look for a new input prompt"
2762 (cond ((and
2763 (string-match "? *$" str)
2764 (not (string-match (concat "(" maxima-outchar "[0-9]+)") str)))
2765 (maxima-ask-question str))
2766 ((string-match inferior-maxima-prompt str)
2767 (if (and inferior-maxima-process (not (string= maxima-block "")))
2768 (maxima-single-string (maxima-get-command))
2769 (if (not inferior-maxima-process)
2770 (maxima-clear-queue))
2771 (setq inferior-maxima-waiting-for-output nil)))))
2773 (defun inferior-maxima-sentinel (proc state)
2774 "Write the input history when the process ends"
2775 (unless (string-match "^run" state)
2776 (comint-write-input-ring)))
2778 (defun maxima-start ()
2779 "Start the Maxima process."
2780 (interactive)
2781 (if (processp inferior-maxima-process)
2782 (unless (eq (process-status inferior-maxima-process) 'run)
2783 (delete-process inferior-maxima-process)
2784 (if (get-buffer "*maxima*")
2785 (save-excursion
2786 (set-buffer "*maxima*")
2787 (erase-buffer)))
2788 (setq inferior-maxima-process nil)))
2789 (unless (processp inferior-maxima-process)
2790 (setq inferior-maxima-input-end 0)
2791 (setq inferior-maxima-waiting-for-output t)
2792 (let ((mbuf)
2793 (cmd))
2794 (if maxima-args
2795 (setq cmd
2796 (append (list 'make-comint "maxima" maxima-command
2797 nil) (split-string maxima-args)))
2798 (setq cmd (list 'make-comint "maxima" maxima-command)))
2799 (setq mbuf (eval cmd))
2800 (save-excursion
2801 (set-buffer mbuf)
2802 (setq inferior-maxima-process (get-buffer-process mbuf))
2803 (add-hook 'comint-output-filter-functions
2804 'inferior-maxima-output-filter nil t)
2805 (add-hook 'comint-output-filter-functions
2806 'inferior-maxima-replace-tabs-by-spaces nil t)
2807 ; (add-hook 'comint-output-filter-functions
2808 ; 'inferior-maxima-remove-double-input-prompt nil t)
2809 (if maxima-fix-double-prompt
2810 (add-hook 'comint-output-filter-functions
2811 'inferior-maxima-remove-double-prompt nil t))
2812 (inferior-maxima-wait-for-output)
2813 (inferior-maxima-mode)))))
2815 (defun maxima-stop (&optional arg)
2816 "Kill the currently running Maxima process."
2817 (interactive "P")
2818 (if (processp inferior-maxima-process)
2819 (if arg
2820 (progn
2821 (delete-process inferior-maxima-process)
2822 (kill-buffer "*maxima*")
2823 (setq inferior-maxima-process nil))
2824 (if (y-or-n-p "Really quit Maxima? ")
2825 (progn
2826 (delete-process inferior-maxima-process)
2827 (kill-buffer "*maxima*")
2828 (setq inferior-maxima-process nil))))))
2830 ;;; Sending information to the process
2832 (defun maxima-single-string (string)
2833 "Send a string to the Maxima process."
2834 (setq string (maxima-strip-string-add-semicolon string))
2835 (maxima-start)
2836 ; (inferior-maxima-wait-for-output)
2837 (save-current-buffer
2838 (set-buffer (process-buffer inferior-maxima-process))
2839 (goto-char (point-max))
2840 (insert string)
2841 (inferior-maxima-comint-send-input)
2842 (goto-char (point-max))))
2844 (defun maxima-ask-question (string)
2845 "Ask the question maxima wants answered."
2846 (let ((ans (read-string
2847 (concat (maxima-strip-string string) " " ))))
2848 (unless (string-match "[;$]" ans)
2849 (setq ans (concat ans ";")))
2850 (setq ans (maxima-strip-string ans))
2851 (save-current-buffer
2852 (set-buffer (process-buffer inferior-maxima-process))
2853 (goto-char (point-max))
2854 (insert ans)
2855 (inferior-maxima-comint-send-input t)
2856 (goto-char (point-max)))))
2858 (defun maxima-get-command (&optional arg)
2859 "Return the maxima command that's at the front of maxima-block.
2860 Remove it from the front of maxima-block.
2861 With an argument, use maxima-block-wait instead of maxima-block."
2862 (let* ((tmpfile (maxima-make-temp-name))
2863 (tmpbuf (get-buffer-create tmpfile))
2864 (pt)
2865 (command))
2866 (save-excursion
2867 (set-buffer tmpbuf)
2868 (maxima-remove-kill-buffer-hooks)
2869 (if arg
2870 (insert maxima-block-wait)
2871 (insert maxima-block))
2872 (goto-char (point-min))
2873 (maxima-forward-over-comment-whitespace)
2874 (setq pt (point))
2875 (if (string-match "[$;]\\|:lisp"
2876 (buffer-substring-no-properties (point) (point-max)))
2877 (progn
2878 (if (looking-at ":lisp")
2879 (progn
2880 (search-forward ":lisp")
2881 (forward-sexp)
2882 (setq command (buffer-substring-no-properties pt (point))))
2883 (maxima-goto-end-of-form)
2884 (setq command (buffer-substring-no-properties pt (point))))
2885 (maxima-forward-over-comment-whitespace)
2886 (if arg
2887 (setq maxima-block-wait
2888 (maxima-strip-string-add-semicolon
2889 (buffer-substring-no-properties (point) (point-max))))
2890 (setq maxima-block
2891 (maxima-strip-string-add-semicolon
2892 (buffer-substring-no-properties (point) (point-max)))))
2893 (setq command (buffer-substring-no-properties pt (point))))
2894 (if arg
2895 (setq maxima-block-wait "")
2896 (setq maxima-block "")))
2897 (if arg
2898 (if (string= maxima-block-wait ";") (setq maxima-block-wait ""))
2899 (if (string= maxima-block ";") (setq maxima-block "")))
2900 (kill-buffer tmpbuf))
2901 command))
2903 (defun maxima-send-block (stuff)
2904 "Send a block of code to Maxima."
2905 (maxima-start)
2906 (setq stuff (maxima-strip-string-add-semicolon stuff))
2907 ; (unless (string-match (substring stuff -1) ";$")
2908 ; (setq stuff (concat stuff ";")))
2909 (if (string= maxima-block "")
2910 (progn
2911 (setq maxima-block stuff)
2912 (maxima-single-string (maxima-get-command)))
2913 (setq maxima-block (concat maxima-block stuff))))
2915 (defun maxima-send-block-wait (stuff)
2916 "Send a block of code to Maxima; wait for it to finish.
2917 Return the last string sent."
2918 (maxima-start)
2919 (if (not (string= maxima-block ""))
2920 (message "Maxima process currently busy.")
2921 (setq maxima-block-wait (maxima-strip-string-add-semicolon stuff))
2922 (while (not (string= maxima-block-wait ""))
2923 (maxima-single-string-wait (maxima-get-command t)))))
2925 (defun maxima-clear-queue ()
2926 "Clear out the queue of commands to send to the maxima process."
2927 (interactive)
2928 (setq maxima-block "")
2929 (setq maxima-block-wait ""))
2931 ;;; Getting information back from Maxima.
2933 (defun maxima-last-output ()
2934 "Get the most recent output from Maxima."
2935 (interactive)
2936 (inferior-maxima-wait-for-output)
2937 (save-excursion
2938 (set-buffer (process-buffer inferior-maxima-process))
2939 (let* ((pt (point))
2940 (pmark (progn (goto-char (process-mark inferior-maxima-process))
2941 (forward-line 0)
2942 (point-marker)))
2943 (beg (progn
2944 (goto-char inferior-maxima-input-end)
2945 (forward-line 1)
2946 (point)))
2947 (output (buffer-substring-no-properties beg pmark)))
2948 (goto-char pt)
2949 output)))
2951 (defun maxima-last-output-noprompt ()
2952 "Return the last Maxima output, without the prompts"
2953 (interactive)
2954 (if (not (inferior-maxima-running))
2955 (maxima-last-output)
2956 (let* ((output (maxima-last-output))
2957 (newstring)
2958 (i 0)
2959 (beg)
2960 (end)
2961 (k))
2962 ;; Replace the output prompt with spaces
2963 (setq beg (string-match
2964 (concat "\\(^(" maxima-outchar "[0-9]*) \\)") output))
2965 (if (not beg)
2966 output
2967 (setq end (1+ (string-match ")" output beg)))
2968 (setq newstring (substring output 0 beg))
2969 (setq k (- end beg))
2970 (while (< i k)
2971 (setq newstring (concat newstring " "))
2972 (setq i (1+ i)))
2973 (concat newstring
2974 (substring output
2975 end))))))
2977 (defun maxima-last-output-tex-noprompt ()
2978 "Return the last Maxima output, between the dollar signs."
2979 (interactive)
2980 (let* ((output (maxima-last-output))
2981 (begtex (string-match "\\$\\$" output))
2982 (endtex (string-match "\\$\\$" output (1+ begtex))))
2983 (concat
2984 (substring output begtex (+ endtex 2))
2985 "\n")))
2988 ;;; Sending information to the process should be done through these
2989 ;; next five commands
2991 (defun maxima-single-string-wait (string)
2992 "Send a single string to the maxima process,
2993 waiting for output after."
2994 (inferior-maxima-wait-for-output)
2995 (maxima-single-string string)
2996 (inferior-maxima-wait-for-output))
2998 (defun maxima-string (string)
2999 "Send a string to the Maxima process."
3000 (maxima-send-block string))
3002 (defun maxima-region (beg end)
3003 "Send the region to the Maxima process."
3004 (setq maxima-mode-region-begin beg)
3005 (setq maxima-mode-region-end end)
3006 (maxima-string
3007 (buffer-substring-no-properties beg end)))
3009 ;;; Some functions to send commands to the process.
3011 (defun maxima-send-region (beg end &optional arg)
3012 "Send the current region to the Maxima process.
3013 With an argument, don't check the parentheses first."
3014 (interactive "r\nP")
3015 (if arg
3016 (maxima-region beg end)
3017 (if (maxima-check-parens beg end)
3018 (maxima-region beg end)))
3019 (maxima-display-buffer))
3021 (defun maxima-send-buffer (&optional arg)
3022 "Send the buffer to the Maxima process, after checking the parentheses.
3023 With an argument, don't check the parentheses."
3024 (interactive "P")
3025 (maxima-send-region (point-min) (point-max) arg))
3027 (defun maxima-send-line (&optional arg)
3028 "Send the current line to the Maxima process, after checking parentheses.
3029 With an argument, don't check parentheses."
3030 (interactive "P")
3031 (let ((b (maxima-line-beginning-position))
3032 (e (maxima-line-end-position)))
3033 (maxima-send-region b e arg)))
3035 (defun maxima-send-form (&optional arg)
3036 "Send the current form to the Maxima process, checking parentheses.
3037 With an argument, don't check parentheses."
3038 (interactive "P")
3039 (maxima-send-region (maxima-form-beginning-position)
3040 (maxima-form-end-position-or-point-max) arg))
3042 (defun maxima-send-previous-form (&optional arg)
3043 "Send the previous form to the Maxima process, checking parentheses.
3044 With an argument, don't check parentheses."
3045 (interactive "P")
3046 (save-excursion
3047 (if (maxima-re-search-backward "[;$]")
3048 (maxima-send-region (maxima-form-beginning-position)
3049 (maxima-form-end-position-or-point-max) arg)
3050 (message "No previous form."))))
3052 (defun maxima-send-previous-form-and-goto-end-of-form (&optional arg)
3053 "Send the previous form to the Maxima process and go to the end of form."
3054 (interactive "P")
3055 (maxima-send-previous-form arg)
3056 (maxima-goto-end-of-form-interactive))
3058 (defun maxima-send-full-line ()
3059 "Send the minimum number of lines such that the current is one of them,
3060 and such that no line contains an incomplete form."
3061 (interactive)
3062 (let ((beg (point)) (end (point)))
3063 (save-excursion
3064 (goto-char beg)
3065 (beginning-of-line)
3066 (setq beg (point))
3067 (maxima-goto-beginning-of-form)
3068 (while (< (point) beg)
3069 (progn
3070 (beginning-of-line)
3071 (setq beg (point))
3072 (maxima-goto-beginning-of-form)))
3073 (goto-char end)
3074 (end-of-line)
3075 (setq end (point))
3076 (while (and (< (maxima-form-beginning-position) end) (< end (point-max)))
3077 (progn
3078 (forward-line 1)
3079 (end-of-line)
3080 (setq end (point))))
3081 (skip-chars-backward " \t;$")
3082 (if (re-search-forward "[;$]" end t)
3083 (maxima-send-region beg (point))
3084 (error "No ; or $ at end"))
3085 end)))
3087 (defun maxima-send-full-line-and-goto-next-form ()
3088 "Do a maxima-send-full-line and go to the beginning of the next form."
3089 (interactive)
3090 (goto-char (maxima-send-full-line))
3091 (maxima-goto-beginning-of-form))
3093 (defun maxima-send-completed-region (beg end)
3094 "Send the marked region, but complete possibly non-complete forms at the bounderies."
3095 (interactive "r\nP")
3096 (let ((beg1)
3097 (end1))
3098 (save-excursion
3099 (goto-char beg)
3100 (setq beg1 (maxima-form-beginning-position))
3101 (goto-char end)
3102 (setq end1 (maxima-form-end-position-or-point-max))
3103 (maxima-send-region beg1 end1)
3104 end1)))
3106 (defun maxima-send-completed-region-and-goto-next-form (beg end)
3107 "Do a maxima-send-completed-region and go to the beginning of the next form."
3108 (interactive "r\nP")
3109 (goto-char (maxima-send-completed-region beg end))
3110 (maxima-goto-beginning-of-form))
3112 (defun maxima-display-buffer ()
3113 "Display the inferior-maxima-process buffer so the recent output is visible."
3114 (interactive)
3115 (let ((origbuffer (current-buffer)))
3116 (if (not (processp inferior-maxima-process))
3117 (maxima-start))
3118 (pop-to-buffer (process-buffer inferior-maxima-process))
3119 (goto-char (point-max))
3120 ; (recenter (universal-argument))
3121 (pop-to-buffer origbuffer)))
3124 ;;;; The inferior Maxima process
3126 ;;; Completions from previous input
3128 ;; First, a function to take the comint-input-ring and return a
3129 ;; list of previous inputs
3131 (defun inferior-maxima-previous-inputs ()
3132 "Return a list of previous inputs."
3133 (interactive)
3134 (let* ((inputs nil)
3135 (comint-inputs (cddr comint-input-ring))
3136 (i 0))
3137 (while (and (< i comint-input-ring-size)
3138 (not (null (aref comint-inputs i))))
3139 (unless (member (aref comint-inputs i) inputs)
3140 (setq inputs (cons (aref comint-inputs i) inputs)))
3141 (setq i (1+ i)))
3142 (reverse inputs)))
3144 (defun inferior-maxima-input-complete ()
3145 "Complete line from list of previous input."
3146 (interactive)
3147 (let* ((stub (buffer-substring-no-properties
3148 (inferior-maxima-bol-position) (point)))
3149 (completions (all-completions (downcase stub)
3150 (inferior-maxima-previous-inputs))))
3151 (setq completions
3152 (mapcar
3153 (function (lambda (x) (maxima-he-transfer-case stub x))) completions))
3154 (cond ((null completions)
3155 (message "No completions of %s" stub))
3156 ((= 1 (length completions)) ; Gotcha!
3157 (let ((completion (car completions)))
3158 (if (string-equal completion stub)
3159 (message "Sole completion")
3160 (insert (substring completion (length stub)))
3161 (message "Completed"))))
3162 (t ; There's no unique completion.
3163 (comint-dynamic-list-completions completions)))))
3165 (defun inferior-maxima-complete ()
3166 "Complete the current object, depending on context."
3167 (interactive)
3168 (let* ((pmin (save-excursion
3169 (re-search-backward inferior-maxima-prompt)
3170 (point)))
3171 (pps (parse-partial-sexp pmin (point))))
3172 (cond
3173 ;; complete filename if the point is in a string
3174 ((nth 3 pps)
3175 (maxima-complete-filename))
3176 ;; Otherwise, complete the symbol
3178 (maxima-complete-symbol)))))
3180 ;; (defun maxima-input-he-try (old)
3181 ;; (interactive)
3182 ;; (if (not old)
3183 ;; ;;; let beg be the beginning of the word
3184 ;; (progn
3185 ;; (he-init-string (inferior-maxima-bol-position) (point))
3186 ;; (setq he-expand-list
3187 ;; (all-completions (downcase he-search-string) (maxima-previous-inputs)))
3188 ;; (setq he-expand-list
3189 ;; (mapcar (function
3190 ;; (lambda (x) (he-transfer-case he-search-string x)))
3191 ;; he-expand-list))
3192 ;; (if he-expand-list
3193 ;; (he-substitute-string (car he-expand-list))
3194 ;; nil))
3195 ;; (setq he-expand-list (cdr he-expand-list))
3196 ;; (if he-expand-list
3197 ;; (he-substitute-string (car he-expand-list))
3198 ;; (he-reset-string)
3199 ;; nil)))
3201 ;; (fset 'maxima-dynamic-input-complete
3202 ;; (make-hippie-expand-function '(maxima-input-he-try)))
3204 ;;; Sending a line to the process while in the process buffer
3206 (defun inferior-maxima-check-and-send-line ()
3207 "Check the lines for mis-matched parentheses, then send the line."
3208 (interactive)
3209 (let ((ok nil)
3210 (pt (point))
3211 pt1)
3212 (save-excursion
3213 (end-of-line)
3214 (skip-chars-backward " \t")
3215 (forward-char -1)
3216 (when (looking-at "[$;]")
3217 (setq pt (point))
3218 (setq ok t)))
3219 (if ok
3220 (progn
3221 (save-excursion
3222 (re-search-backward inferior-maxima-prompt)
3223 (setq pt1 (match-end 0)))
3224 (if (maxima-check-parens pt1 pt)
3225 (inferior-maxima-comint-send-input)))
3226 (inferior-maxima-comint-send-input))))
3228 (defun inferior-maxima-send-line ()
3229 "Send the line to the Maxima process."
3230 (interactive)
3231 (inferior-maxima-comint-send-input))
3233 (defun inferior-maxima-bol ()
3234 "Go to the beginning of the line, but past the prompt."
3235 (interactive)
3236 (let ((eol (save-excursion (end-of-line) (point))))
3237 (forward-line 0)
3238 (if (and (looking-at inferior-maxima-prompt)
3239 (<= (match-end 0) eol))
3240 (goto-char (match-end 0)))))
3242 (defun inferior-maxima-bol-position ()
3243 (save-excursion
3244 (inferior-maxima-bol)
3245 (point)))
3248 ;;;; Inferior Maxima mode
3250 (define-derived-mode inferior-maxima-mode
3251 comint-mode
3252 "Inferior Maxima"
3253 "Major mode for interacting with an inferior Maxima process.
3255 Return will check the line for balanced parentheses, and send line as input.
3256 Control return will send the line as input without checking for balanced
3257 parentheses.
3259 M-TAB will complete the Maxima symbol as much as possible, providing
3260 a completion buffer if there is more than one possible completion.
3262 \\[maxima-smart-complete] will complete the input line, based on previous input lines.
3263 \\[maxima-help] will get help on a Maxima topic.
3264 \\[maxima-info] will bring up the Maxima info manual.
3265 \\[maxima-stop] will kill the process and the buffer, after asking for
3266 confirmation. To kill without confirmation, give \\[maxima-stop] an
3267 argument.
3269 To scroll through previous commands,
3270 \\[comint-previous-input] will bring the previous input to the current prompt,
3271 \\[comint-next-input] will bring the next input to the prompt.
3272 \\[comint-previous-matching-input] will bring the previous input matching
3273 a regular expression to the prompt,
3274 \\[comint-next-matching-input] will bring the next input matching
3275 a regular expression to the prompt.
3277 ; (if maxima-use-full-color-in-process-buffer
3278 ; (inferior-maxima-font-setup))
3279 (setq comint-prompt-regexp inferior-maxima-prompt)
3280 (setq comint-get-old-input (function inferior-maxima-get-old-input))
3281 (setq mode-line-process '(": %s"))
3282 (maxima-mode-variables)
3283 (setq tab-width 8)
3284 (if (and (not maxima-running-xemacs) (< emacs-major-version 21))
3285 (make-local-hook 'kill-buffer-hook))
3286 (if maxima-running-xemacs
3287 (add-local-hook 'kill-buffer-hook
3288 (function
3289 (lambda ()
3290 (maxima-clear-queue)
3291 (if (processp inferior-maxima-process)
3292 (delete-process inferior-maxima-process))
3293 (setq inferior-maxima-process nil)
3294 (run-hooks 'inferior-maxima-exit-hook))))
3295 (add-hook 'kill-buffer-hook
3296 (function
3297 (lambda ()
3298 (maxima-clear-queue)
3299 (if (processp inferior-maxima-process)
3300 (delete-process inferior-maxima-process))
3301 (setq inferior-maxima-process nil)
3302 (run-hooks 'inferior-maxima-exit-hook))) t t))
3303 (setq comint-input-ring-size maxima-input-history-length)
3304 (if maxima-save-input-history
3305 (progn
3306 (setq comint-input-ring-file-name maxima-input-history-file)
3307 (comint-read-input-ring t)
3308 (set-process-sentinel inferior-maxima-process
3309 'inferior-maxima-sentinel)))
3310 (set (make-local-variable 'comint-prompt-read-only) t)
3311 (run-hooks 'inferior-maxima-mode-hook))
3313 ;;;; Keymap
3315 (define-key inferior-maxima-mode-map "\C-a"
3316 'inferior-maxima-bol)
3317 (define-key inferior-maxima-mode-map "\C-m"
3318 'inferior-maxima-check-and-send-line)
3319 (define-key inferior-maxima-mode-map [(control return)]
3320 'inferior-maxima-send-line)
3321 (define-key inferior-maxima-mode-map [(meta control tab)]
3322 'inferior-maxima-input-complete)
3323 (define-key inferior-maxima-mode-map "\e\t" 'inferior-maxima-complete)
3324 (define-key inferior-maxima-mode-map "\177" 'backward-delete-char-untabify)
3325 (define-key inferior-maxima-mode-map "\C-c\C-k" 'maxima-stop)
3326 (define-key inferior-maxima-mode-map "\C-c\C-d" maxima-help-map)
3328 ;;;; Menu
3330 (easy-menu-define inferior-maxima-mode-menu inferior-maxima-mode-map
3331 "Maxima mode menu"
3332 '("Maxima"
3333 ("Help"
3334 ["Maxima info" maxima-info t]
3335 ["Help" maxima-help t])
3336 ("Quit"
3337 ["Kill process" maxima-stop t])))
3339 ;;;; Running Maxima
3341 (defun maxima ()
3342 "Run Maxima interactively inside a buffer."
3343 (interactive)
3344 (maxima-start)
3345 (switch-to-buffer (process-buffer inferior-maxima-process)))
3347 ;;; Interacting with Maxima outside of a maxima buffer
3349 (defun maxima-minibuffer ()
3350 "Communicate with Maxima through the minibuffer"
3351 (interactive)
3352 (maxima-start)
3353 (let ((input (read-string "Maxima: " nil maxima-minibuffer-history))
3354 (output nil)
3355 (twod (and maxima-minibuffer-2d (not maxima-running-xemacs))))
3356 (setq input (maxima-strip-string-add-semicolon input))
3357 (if twod
3358 (maxima-single-string-wait
3359 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3360 (maxima-single-string-wait
3361 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3362 (maxima-single-string-wait input)
3363 (setq output (maxima-last-output-noprompt))
3364 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3365 (if (not twod)
3366 (setq output (maxima-remove-whitespace-from-ends output))
3367 ;; Strip the beginning and trailing newline
3368 (while (string-match "\\` *\n" output)
3369 (setq output (substring output (match-end 0))))
3370 (while (string-match "\n *\\'" output)
3371 (setq output (substring output 0 (match-beginning 0)))))
3372 (setq output (maxima-replace-in-string "%" "%%" output))
3373 (message output)))
3375 (defun maxima-minibuffer-delete-output (beg end)
3376 (let ((mmom (maxima-minor-output-mark))
3377 (mmoe (maxima-minor-output-mark-end)))
3378 (if (or
3379 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmom)
3380 (string= (match-string 0 mmom) mmom))
3381 (and (string-match maxima-minor-mode-bad-delimiter-regexp mmoe)
3382 (string= (match-string 0 mmoe) mmoe)))
3383 (message "Old output not deleted (improper delimiter).")
3384 (let (pt)
3385 (save-excursion
3386 (goto-char beg)
3387 (if (search-forward mmom end t)
3388 (progn
3389 (setq pt (match-beginning 0))
3390 (search-forward mmoe)
3391 (kill-region pt (point)))
3392 (goto-char end)
3393 (if (looking-at (concat "[ \n]*" (regexp-quote mmom)))
3394 (progn
3395 (search-forward mmoe)
3396 (kill-region end (point)))))
3397 (point))))))
3399 (defun maxima-minibuffer-on-region (beg end &optional arg)
3400 "Send the current region to Maxima; display last output in minibuffer.
3401 With an argument, insert \" ==> \" into the current buffer,
3402 followed by the output, followed by \"\\\". In this case, any previous output
3403 will be deleted."
3404 (interactive "r\nP")
3405 (let ((output nil)
3406 (minibufferoutput)
3407 (input)
3408 (realend nil)
3409 (realbeg)
3410 (outputbeg)
3411 (delreg)
3412 (delregbeg)
3413 (delregend)
3414 (twod (and maxima-minibuffer-2d (not maxima-running-xemacs))))
3415 (save-excursion
3416 (goto-char beg)
3417 (maxima-forward-over-comment-whitespace)
3418 (setq realbeg (point))
3419 (if (re-search-forward (maxima-minor-output-mark) end t)
3420 (setq realend
3421 (if (eq major-mode 'maxima-mode)
3422 (- (point) (length maxima-mode-minor-output))
3423 (- (point) (length maxima-minor-output))))
3424 (goto-char end)
3425 (maxima-back-over-comment-whitespace)
3426 (setq realend (point))))
3427 (setq input (maxima-strip-string-add-semicolon
3428 (buffer-substring-no-properties realbeg realend)))
3429 (if arg
3430 (maxima-minibuffer-delete-output beg end))
3431 (setq maxima-minor-mode-region-begin realbeg)
3432 (setq maxima-minor-mode-region-end realend)
3433 (when (or (not maxima-minor-mode-check-input)
3434 (and
3435 (maxima-check-parens realbeg realend)
3436 (maxima-check-commas realbeg realend)))
3437 (maxima-start)
3438 (if twod
3439 (maxima-single-string-wait
3440 "block(emacsdisplay:display2d,display2d:true,linenum:linenum-1,%);")
3441 (maxima-single-string-wait
3442 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);"))
3443 (maxima-send-block-wait input)
3444 (setq output (maxima-last-output-noprompt))
3445 (maxima-single-string-wait "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3446 (if (not twod)
3447 (setq output (maxima-remove-whitespace-from-ends output))
3448 ;; Strip the beginning and trailing newline
3449 (while (string-match "\\` *\n" output)
3450 (setq output (substring output (match-end 0))))
3451 (while (string-match "\n *\\'" output)
3452 (setq output (substring output 0 (match-beginning 0)))))
3453 (unless arg
3454 (setq minibufferoutput (maxima-replace-in-string "%" "%%" output))
3455 (message minibufferoutput))
3456 (if (and arg
3457 (not twod))
3458 (save-excursion
3459 (goto-char realend)
3460 (if (looking-at "^")
3461 (setq realend (1- realend)))
3462 ;(delete-region realend end)
3463 (goto-char realend)
3464 (skip-chars-backward " \t\n")
3465 (when (not (= (point) realend))
3466 (setq delreg (buffer-substring-no-properties (point) realend))
3467 (kill-region (point) realend)
3468 (cond
3469 ((< (length delreg) 15)
3470 (setq delreg (maxima-replace-in-string "\n" " " delreg))
3471 (message (concat "\"" delreg "\" killed")))
3473 (setq delregbeg
3474 (maxima-replace-in-string "\n" " "(substring delreg 0 5)))
3475 (setq delregend
3476 (maxima-replace-in-string "\n" " "(substring delreg -5)))
3477 (message (concat "\"" delregbeg " ... " delregend "\" killed")))))
3478 (let ((ind (save-excursion
3479 (goto-char realbeg)
3480 (current-column)))
3481 (here (point))
3482 (there (make-marker)))
3483 (if (or
3484 (string-match "\n" output)
3485 (> (+ (current-column) (length output)) fill-column))
3486 (progn
3487 (insert "\n")
3488 (setq here (point)))
3489 (insert " "))
3490 (insert (maxima-minor-output-mark) " " output
3491 (maxima-minor-output-mark-end))
3492 (set-marker there (point))
3493 (goto-char here)
3494 (goto-char (line-end-position))
3495 ; (fill-region (line-beginning-position) (point))
3496 (if (string-match
3497 "\n"
3498 (buffer-substring-no-properties here (point)))
3499 (forward-line -1)
3500 (forward-line 1))
3501 (indent-region (point) there ind)))
3502 (if (and arg twod)
3503 (let ((ind (save-excursion
3504 (goto-char realbeg)
3505 (current-column)))
3506 (here))
3507 (save-excursion
3508 (goto-char realend)
3509 (insert (maxima-minor-output-mark) "\n")
3510 (setq here (point))
3511 (insert output (maxima-minor-output-mark-end))
3512 (indent-region here (point) ind))))))))
3514 (defun maxima-minibuffer-on-line (&optional arg)
3515 "Send the current line to Maxima; display last output in minibuffer.
3516 With an argument, insert \" ==> \" into the current buffer,
3517 followed by the output. In this case, anything in the line
3518 after any occurrence of \" ==> \" will be deleted."
3519 (interactive "P")
3520 (maxima-minibuffer-on-region
3521 (maxima-line-beginning-position)
3522 (maxima-line-end-position)
3523 arg))
3525 (defun maxima-minibuffer-on-form (&optional arg)
3526 "Send the current form to Maxima; display last output in minibuffer.
3527 With an argument, insert \" ==> \" into the current buffer,
3528 followed by the output."
3529 (interactive "P")
3530 (let ((beg (maxima-form-beginning-position))
3531 (end (maxima-form-end-position)))
3532 (save-excursion
3533 (when (re-search-backward "^[ \t]*$" beg t)
3534 (maxima-forward-over-comment-whitespace)
3535 (setq beg (point))))
3536 (maxima-minibuffer-on-region beg end arg)))
3538 (defun maxima-minibuffer-on-determined-region (&optional arg)
3539 "Send a determined region to Maxima; display the output in the minibuffer.
3540 The region is the region between `maxima-minor-prefix' and `maxima-minor-postfix'
3541 With an argument, insert \" ==> \" into the current buffer,
3542 followed by the output. In this case, anything in the determined region
3543 after any occurrence of \" ==> \" will be deleted."
3544 (interactive "P")
3545 (let ((beg)
3546 (end)
3547 (pt (point)))
3548 (save-excursion
3549 (if (re-search-backward maxima-minor-prefix nil t)
3550 (setq beg (match-end 0))
3551 (error "No beginning to determined region"))
3552 (goto-char pt)
3553 (if (re-search-forward maxima-minor-prefix nil t)
3554 (setq end (match-beginning 0))))
3555 (maxima-minibuffer-on-region beg end arg)))
3557 (defun maxima-insert-last-output ()
3558 (interactive)
3559 (maxima-single-string-wait
3560 "block(emacsdisplay:display2d,display2d:false,linenum:linenum-1,%);")
3561 (let ((output (maxima-last-output-noprompt)))
3562 (maxima-single-string "block(display2d:emacsdisplay,linenum:linenum-1,%);")
3563 (insert (maxima-remove-whitespace-from-ends output))))
3565 (defun maxima-insert-last-output-tex ()
3566 (interactive)
3567 (maxima-single-string-wait "tex(%);")
3568 (let ((output (substring (maxima-last-output-tex-noprompt) 2 -3)))
3569 (maxima-single-string "block(linenum:linenum-2,%th(2));")
3570 (insert output)))
3572 ;;; The Maxima minor mode
3574 (defvar maxima-minor-mode-map nil
3575 "The keymap for maxima-minor-mode.")
3577 (if maxima-minor-mode-map
3579 (let ((map (make-sparse-keymap)))
3580 (define-key map "\C-c=m" 'maxima-minibuffer)
3581 (define-key map "\C-c=e" 'maxima-minibuffer-on-determined-region)
3582 (define-key map "\C-c=l" 'maxima-minibuffer-on-line)
3583 (define-key map "\C-c=r" 'maxima-minibuffer-on-region)
3584 (define-key map "\C-c=f" 'maxima-minibuffer-on-form)
3585 (define-key map "\C-c=o" 'maxima-insert-last-output)
3586 (define-key map "\C-c=t" 'maxima-insert-last-output-tex)
3587 (define-key map "\C-c=d" maxima-help-map)
3588 (setq maxima-minor-mode-map map)))
3590 (unless (fboundp 'define-minor-mode)
3591 (defalias 'define-minor-mode 'easy-mmode-define-minor-mode))
3593 (define-minor-mode maxima-minor-mode
3594 "Toggle Maxima minor mode.
3595 With no argument, this command toggles the mode.
3596 Non-null prefix argument turns on the mode.
3597 Null prefix argument turns off the mode.
3599 When Maxima minor mode is enabled, the following keystrokes
3600 are in effect:
3601 \\[maxima-minibuffer-on-determined-region]
3602 Send the region between the customizable regexps
3603 `maxima-minor-prefix' and `maxima-minor-postfix' to Maxima
3604 and display the result in the minibuffer.
3605 \\[maxima-minibuffer-on-line]
3606 Send the current line to Maxima and display the result in the minibuffer.
3607 \\[maxima-minibuffer-on-region]
3608 Send the current region to Maxima and display the result in the minibuffer.
3609 \\[maxima-minibuffer-on-form]
3610 Send the current form to Maxima and display the result in the minibuffer.
3611 (The form is the region between the preceding ; or $ and the subsequent
3612 ; or $)
3613 With a prefix, the above commands will insert the output in the current
3614 buffer, preceded by \" ==> \" (customizable with `maxima-minor-output').
3615 \\[maxima-minibuffer]
3616 Prompt for an expression in the minibuffer, return result in minibuffer.
3617 \\[maxima-insert-last-output]
3618 Insert the last Maxima result into the current buffer.
3619 \\[maxima-insert-last-output-tex]
3620 Insert the last Maxima result in TeX form into the current buffer."
3621 ;; The initial value.; :initial-value
3623 ;; The indicator for the mode line.; :lighter
3624 " maxima"
3625 nil)
3627 ;;; For highlighting the region being sent
3629 (defun maxima-minor-mode-add-highlight ()
3630 (maxima-minor-mode-remove-highlight)
3631 (when (and maxima-minor-mode-region-begin
3632 maxima-minor-mode-region-end)
3633 (if maxima-running-xemacs
3634 (progn
3635 (setq maxima-minor-mode-highlight
3636 (make-extent
3637 maxima-minor-mode-region-begin
3638 maxima-minor-mode-region-end))
3639 (set-extent-property maxima-minor-mode-highlight 'face 'highlight))
3640 (setq maxima-minor-mode-highlight
3641 (make-overlay
3642 maxima-minor-mode-region-begin
3643 maxima-minor-mode-region-end))
3644 (overlay-put maxima-minor-mode-highlight 'face 'highlight))
3645 (setq maxima-minor-mode-region-begin nil)
3646 (setq maxima-minor-mode-region-end nil)))
3648 (defun maxima-minor-mode-remove-highlight ()
3649 (when maxima-minor-mode-highlight
3650 (if maxima-running-xemacs
3651 (delete-extent maxima-minor-mode-highlight)
3652 (delete-overlay maxima-minor-mode-highlight))
3653 (setq maxima-minor-mode-highlight nil)))
3655 (defun maxima-minor-mode-add-remove-highlight ()
3656 (if (or
3657 (eq this-command 'maxima-minibuffer-on-region)
3658 (eq this-command 'maxima-minibuffer-on-determined-region)
3659 (eq this-command 'maxima-minibuffer-on-form)
3660 (eq this-command 'maxima-minibuffer-on-line))
3661 (maxima-minor-mode-add-highlight)
3662 (maxima-minor-mode-remove-highlight)))
3664 (defun maxima-minor-mode-highlighting ()
3665 (if maxima-running-xemacs
3666 (add-local-hook 'post-command-hook
3667 'maxima-minor-mode-add-remove-highlight)
3668 (add-hook 'post-command-hook
3669 'maxima-minor-mode-add-remove-highlight nil t)))
3671 (add-hook 'maxima-minor-mode-hook
3672 'maxima-minor-mode-highlighting)
3674 (provide 'maxima)
3675 ;;; maxima.el ends here