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