Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / progmodes / octave-inf.el
CommitLineData
092af6d8 1;;; octave-inf.el --- running Octave as an inferior Emacs process
081bf30e 2
5df4f04c 3;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
034babe1 4;; Free Software Foundation, Inc.
081bf30e 5
d0943d40 6;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
081bf30e 7;; Author: John Eaton <jwe@bevo.che.wisc.edu>
d0943d40 8;; Maintainer: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
081bf30e
RS
9;; Keywords: languages
10
11;; This file is part of GNU Emacs.
12
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
081bf30e 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
081bf30e
RS
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
081bf30e 25
e8af40ee
PJ
26;;; Commentary:
27
081bf30e
RS
28;;; Code:
29
f7bbab75 30(require 'octave-mod)
081bf30e
RS
31(require 'comint)
32
28d16ed3
AS
33(defgroup octave-inferior nil
34 "Running Octave as an inferior Emacs process."
35 :group 'octave)
081bf30e 36
28d16ed3 37(defcustom inferior-octave-program "octave"
e2536e57 38 "Program invoked by `inferior-octave'."
28d16ed3
AS
39 :type 'string
40 :group 'octave-inferior)
41
42(defcustom inferior-octave-prompt
6145f7fd 43 "\\(^octave\\(\\|.bin\\|.exe\\)\\(-[.0-9]+\\)?\\(:[0-9]+\\)?\\|^debug\\|^\\)>+ "
e2536e57 44 "Regexp to match prompts for the inferior Octave process."
28d16ed3
AS
45 :type 'regexp
46 :group 'octave-inferior)
081bf30e 47
28d16ed3 48(defcustom inferior-octave-startup-file nil
e2536e57 49 "Name of the inferior Octave startup file.
081bf30e 50The contents of this file are sent to the inferior Octave process on
28d16ed3
AS
51startup."
52 :type '(choice (const :tag "None" nil)
53 file)
54 :group 'octave-inferior)
081bf30e 55
3a5a5d36 56(defcustom inferior-octave-startup-args nil
e2536e57 57 "List of command line arguments for the inferior Octave process.
081bf30e 58For example, for suppressing the startup message and using `traditional'
28d16ed3
AS
59mode, set this to (\"-q\" \"--traditional\")."
60 :type '(repeat string)
61 :group 'octave-inferior)
081bf30e 62
e2536e57
SM
63(defvar inferior-octave-mode-map
64 (let ((map (make-sparse-keymap)))
65 (set-keymap-parent map comint-mode-map)
081bf30e
RS
66 (define-key map "\t" 'comint-dynamic-complete)
67 (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
68 (define-key map "\C-c\C-l" 'inferior-octave-dynamic-list-input-ring)
69 (define-key map [menu-bar inout list-history]
70 '("List Input History" . inferior-octave-dynamic-list-input-ring))
71 (define-key map "\C-c\C-h" 'octave-help)
e2536e57
SM
72 map)
73 "Keymap used in Inferior Octave mode.")
081bf30e 74
e2536e57 75(defvar inferior-octave-mode-syntax-table
081bf30e
RS
76 (let ((table (make-syntax-table)))
77 (modify-syntax-entry ?\` "w" table)
78 (modify-syntax-entry ?\# "<" table)
79 (modify-syntax-entry ?\n ">" table)
e2536e57
SM
80 table)
81 "Syntax table in use in inferior-octave-mode buffers.")
081bf30e 82
28d16ed3
AS
83(defcustom inferior-octave-mode-hook nil
84 "*Hook to be run when Inferior Octave mode is started."
85 :type 'hook
86 :group 'octave-inferior)
081bf30e
RS
87
88(defvar inferior-octave-font-lock-keywords
89 (list
90 (cons inferior-octave-prompt 'font-lock-type-face))
91 ;; Could certainly do more font locking in inferior Octave ...
92 "Additional expressions to highlight in Inferior Octave mode.")
93
9d245da5
MB
94
95;;; Compatibility functions
96(if (not (fboundp 'comint-line-beginning-position))
97 ;; comint-line-beginning-position is defined in Emacs 21
98 (defun comint-line-beginning-position ()
99 "Returns the buffer position of the beginning of the line, after any prompt.
100The prompt is assumed to be any text at the beginning of the line matching
101the regular expression `comint-prompt-regexp', a buffer local variable."
102 (save-excursion (comint-bol nil) (point))))
103
104
081bf30e
RS
105(defvar inferior-octave-output-list nil)
106(defvar inferior-octave-output-string nil)
107(defvar inferior-octave-receive-in-progress nil)
108
109(defvar inferior-octave-startup-hook nil)
110
111(defvar inferior-octave-complete-impossible nil
112 "Non-nil means that `inferior-octave-complete' is impossible.")
113
d32f600d
CY
114(defvar inferior-octave-has-built-in-variables nil
115 "Non-nil means that Octave has built-in variables.")
116
081bf30e 117(defvar inferior-octave-dynamic-complete-functions
a1506d29 118 '(inferior-octave-complete comint-dynamic-complete-filename)
081bf30e
RS
119 "List of functions called to perform completion for inferior Octave.
120This variable is used to initialize `comint-dynamic-complete-functions'
121in the Inferior Octave buffer.")
122
123(defun inferior-octave-mode ()
124 "Major mode for interacting with an inferior Octave process.
125Runs Octave as a subprocess of Emacs, with Octave I/O through an Emacs
126buffer.
127
128Entry to this mode successively runs the hooks `comint-mode-hook' and
129`inferior-octave-mode-hook'."
130 (interactive)
cb1b44b0 131 (delay-mode-hooks (comint-mode))
081bf30e
RS
132 (setq comint-prompt-regexp inferior-octave-prompt
133 major-mode 'inferior-octave-mode
134 mode-name "Inferior Octave"
135 mode-line-process '(":%s")
136 local-abbrev-table octave-abbrev-table)
137 (use-local-map inferior-octave-mode-map)
138 (set-syntax-table inferior-octave-mode-syntax-table)
139
a1506d29 140 (make-local-variable 'comment-start)
081bf30e
RS
141 (setq comment-start octave-comment-start)
142 (make-local-variable 'comment-end)
143 (setq comment-end "")
144 (make-local-variable 'comment-column)
a1506d29 145 (setq comment-column 32)
081bf30e
RS
146 (make-local-variable 'comment-start-skip)
147 (setq comment-start-skip octave-comment-start-skip)
148
149 (make-local-variable 'font-lock-defaults)
150 (setq font-lock-defaults '(inferior-octave-font-lock-keywords nil nil))
151
152 (setq comint-input-ring-file-name
153 (or (getenv "OCTAVE_HISTFILE") "~/.octave_hist")
e9886e88 154 comint-input-ring-size (or (getenv "OCTAVE_HISTSIZE") 1024))
e2536e57
SM
155 (set (make-local-variable 'comint-dynamic-complete-functions)
156 inferior-octave-dynamic-complete-functions)
e9886e88
FP
157 (add-hook 'comint-input-filter-functions
158 'inferior-octave-directory-tracker nil t)
081bf30e
RS
159 (comint-read-input-ring t)
160
9a969196 161 (run-mode-hooks 'inferior-octave-mode-hook))
081bf30e
RS
162
163;;;###autoload
164(defun inferior-octave (&optional arg)
165 "Run an inferior Octave process, I/O via `inferior-octave-buffer'.
166This buffer is put in Inferior Octave mode. See `inferior-octave-mode'.
167
168Unless ARG is non-nil, switches to this buffer.
169
170The elements of the list `inferior-octave-startup-args' are sent as
171command line arguments to the inferior Octave process on startup.
172
173Additional commands to be executed on startup can be provided either in
174the file specified by `inferior-octave-startup-file' or by the default
175startup file, `~/.emacs-octave'."
176 (interactive "P")
177 (let ((buffer inferior-octave-buffer))
178 (get-buffer-create buffer)
179 (if (comint-check-proc buffer)
180 ()
9a529312 181 (with-current-buffer buffer
081bf30e
RS
182 (comint-mode)
183 (inferior-octave-startup)
184 (inferior-octave-mode)))
185 (if (not arg)
186 (pop-to-buffer buffer))))
187
188;;;###autoload
189(defalias 'run-octave 'inferior-octave)
190
191(defun inferior-octave-startup ()
192 "Start an inferior Octave process."
193 (let ((proc (comint-exec-1
194 (substring inferior-octave-buffer 1 -1)
195 inferior-octave-buffer
196 inferior-octave-program
3a5a5d36
SE
197 (append (list "-i" "--no-line-editing")
198 inferior-octave-startup-args))))
081bf30e
RS
199 (set-process-filter proc 'inferior-octave-output-digest)
200 (setq comint-ptyp process-connection-type
201 inferior-octave-process proc
202 inferior-octave-output-list nil
203 inferior-octave-output-string nil
204 inferior-octave-receive-in-progress t)
205
206 ;; This may look complicated ... However, we need to make sure that
207 ;; we additional startup code only AFTER Octave is ready (otherwise,
208 ;; output may be mixed up). Hence, we need to digest the Octave
209 ;; output to see when it issues a prompt.
210 (while inferior-octave-receive-in-progress
211 (accept-process-output inferior-octave-process))
212 (goto-char (point-max))
213 (set-marker (process-mark proc) (point))
214 (insert-before-markers
215 (concat
216 (if (not (bobp)) "\f\n")
217 (if inferior-octave-output-list
218 (concat (mapconcat
219 'identity inferior-octave-output-list "\n")
220 "\n"))))
107dd21b 221
d32f600d
CY
222 ;; Find out whether Octave has built-in variables.
223 (inferior-octave-send-list-and-digest
224 (list "exist \"LOADPATH\"\n"))
225 (setq inferior-octave-has-built-in-variables
226 (string-match "101$" (car inferior-octave-output-list)))
227
107dd21b
EZ
228 ;; An empty secondary prompt, as e.g. obtained by '--braindead',
229 ;; means trouble.
230 (inferior-octave-send-list-and-digest (list "PS2\n"))
d32f600d
CY
231 (if (string-match "\\(PS2\\|ans\\) = *$" (car inferior-octave-output-list))
232 (inferior-octave-send-list-and-digest
233 (list (if inferior-octave-has-built-in-variables
234 "PS2 = \"> \"\n"
235 "PS2 (\"> \");\n"))))
107dd21b 236
081bf30e
RS
237 ;; O.k., now we are ready for the Inferior Octave startup commands.
238 (let* (commands
239 (program (file-name-nondirectory inferior-octave-program))
240 (file (or inferior-octave-startup-file
241 (concat "~/.emacs-" program))))
242 (setq commands
d32f600d 243 (list "more off;\n"
081bf30e 244 (if (not (string-equal
a1506d29 245 inferior-octave-output-string ">> "))
d32f600d
CY
246 (if inferior-octave-has-built-in-variables
247 "PS1=\"\\\\s> \";\n"
248 "PS1 (\"\\\\s> \");\n"))
081bf30e
RS
249 (if (file-exists-p file)
250 (format "source (\"%s\");\n" file))))
251 (inferior-octave-send-list-and-digest commands))
252 (insert-before-markers
253 (concat
254 (if inferior-octave-output-list
255 (concat (mapconcat
256 'identity inferior-octave-output-list "\n")
257 "\n"))
258 inferior-octave-output-string))
259 ;; Next, we check whether Octave supports `completion_matches' ...
260 (inferior-octave-send-list-and-digest
261 (list "exist \"completion_matches\"\n"))
262 (setq inferior-octave-complete-impossible
263 (not (string-match "5$" (car inferior-octave-output-list))))
264
265 ;; And finally, everything is back to normal.
266 (set-process-filter proc 'inferior-octave-output-filter)
4ca7c468
EZ
267 (run-hooks 'inferior-octave-startup-hook)
268 (run-hooks 'inferior-octave-startup-hook)
269 ;; Just in case, to be sure a cd in the startup file
270 ;; won't have detrimental effects.
271 (inferior-octave-resync-dirs)))
081bf30e
RS
272
273\f
274(defun inferior-octave-complete ()
275 "Perform completion on the Octave symbol preceding point.
276This is implemented using the Octave command `completion_matches' which
277is NOT available with versions of Octave prior to 2.0."
278 (interactive)
279 (let* ((end (point))
502166d2
MB
280 (command
281 (save-excursion
282 (skip-syntax-backward "w_" (comint-line-beginning-position))
283 (buffer-substring-no-properties (point) end)))
e2536e57 284 (proc (get-buffer-process inferior-octave-buffer)))
081bf30e
RS
285 (cond (inferior-octave-complete-impossible
286 (error (concat
287 "Your Octave does not have `completion_matches'. "
288 "Please upgrade to version 2.X.")))
289 ((string-equal command "")
290 (message "Cannot complete an empty string"))
291 (t
292 (inferior-octave-send-list-and-digest
293 (list (concat "completion_matches (\"" command "\");\n")))
294 ;; Sort the list
295 (setq inferior-octave-output-list
296 (sort inferior-octave-output-list 'string-lessp))
297 ;; Remove duplicates
298 (let* ((x inferior-octave-output-list)
299 (y (cdr x)))
300 (while y
301 (if (string-equal (car x) (car y))
302 (setcdr x (setq y (cdr y)))
303 (setq x y
304 y (cdr y)))))
305 ;; And let comint handle the rest
306 (comint-dynamic-simple-complete
307 command inferior-octave-output-list)))))
308
309(defun inferior-octave-dynamic-list-input-ring ()
e2536e57 310 "List the buffer's input history in a help buffer."
081bf30e
RS
311 ;; We cannot use `comint-dynamic-list-input-ring', because it replaces
312 ;; "completion" by "history reference" ...
313 (interactive)
314 (if (or (not (ring-p comint-input-ring))
315 (ring-empty-p comint-input-ring))
316 (message "No history")
317 (let ((history nil)
318 (history-buffer " *Input History*")
319 (index (1- (ring-length comint-input-ring)))
320 (conf (current-window-configuration)))
321 ;; We have to build up a list ourselves from the ring vector.
322 (while (>= index 0)
323 (setq history (cons (ring-ref comint-input-ring index) history)
324 index (1- index)))
325 ;; Change "completion" to "history reference"
326 ;; to make the display accurate.
327 (with-output-to-temp-buffer history-buffer
328 (display-completion-list history)
329 (set-buffer history-buffer))
330 (message "Hit space to flush")
331 (let ((ch (read-event)))
332 (if (eq ch ?\ )
333 (set-window-configuration conf)
334 (setq unread-command-events (list ch)))))))
335
336(defun inferior-octave-strip-ctrl-g (string)
337 "Strip leading `^G' character.
338If STRING starts with a `^G', ring the bell and strip it."
339 (if (string-match "^\a" string)
340 (progn
341 (ding)
342 (setq string (substring string 1))))
343 string)
344
345(defun inferior-octave-output-filter (proc string)
346 "Standard output filter for the inferior Octave process.
347Ring Emacs bell if process output starts with an ASCII bell, and pass
348the rest to `comint-output-filter'."
349 (comint-output-filter proc (inferior-octave-strip-ctrl-g string)))
350
351(defun inferior-octave-output-digest (proc string)
352 "Special output filter for the inferior Octave process.
353Save all output between newlines into `inferior-octave-output-list', and
354the rest to `inferior-octave-output-string'."
355 (setq string (concat inferior-octave-output-string string))
356 (while (string-match "\n" string)
357 (setq inferior-octave-output-list
358 (append inferior-octave-output-list
359 (list (substring string 0 (match-beginning 0))))
360 string (substring string (match-end 0))))
361 (if (string-match inferior-octave-prompt string)
362 (setq inferior-octave-receive-in-progress nil))
363 (setq inferior-octave-output-string string))
364
365(defun inferior-octave-send-list-and-digest (list)
366 "Send LIST to the inferior Octave process and digest the output.
367The elements of LIST have to be strings and are sent one by one. All
368output is passed to the filter `inferior-octave-output-digest'."
369 (let* ((proc inferior-octave-process)
370 (filter (process-filter proc))
371 string)
372 (set-process-filter proc 'inferior-octave-output-digest)
373 (setq inferior-octave-output-list nil)
374 (unwind-protect
375 (while (setq string (car list))
376 (setq inferior-octave-output-string nil
377 inferior-octave-receive-in-progress t)
378 (comint-send-string proc string)
379 (while inferior-octave-receive-in-progress
380 (accept-process-output proc))
381 (setq list (cdr list)))
382 (set-process-filter proc filter))))
383
384(defun inferior-octave-directory-tracker (string)
385 "Tracks `cd' commands issued to the inferior Octave process.
386Use \\[inferior-octave-resync-dirs] to resync if Emacs gets confused."
df287ddb
SE
387 (cond
388 ((string-match "^[ \t]*cd[ \t;]*$" string)
389 (cd "~"))
390 ((string-match "^[ \t]*cd[ \t]+\\([^ \t\n;]*\\)[ \t\n;]*" string)
391 (cd (substring string (match-beginning 1) (match-end 1))))))
081bf30e
RS
392
393(defun inferior-octave-resync-dirs ()
394 "Resync the buffer's idea of the current directory.
395This command queries the inferior Octave process about its current
396directory and makes this the current buffer's default directory."
397 (interactive)
d32f600d 398 (inferior-octave-send-list-and-digest '("disp (pwd ())\n"))
081bf30e
RS
399 (cd (car inferior-octave-output-list)))
400
15c2d736
KH
401;;; provide ourself
402
403(provide 'octave-inf)
404
e2536e57 405;; arch-tag: bdce0395-24d1-4bb4-bfba-6fb1eeb1a660
e96a83c8 406;;; octave-inf.el ends here