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