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