Commit | Line | Data |
---|---|---|
a1ad21ca CH |
1 | ;; Run Scheme under Emacs |
2 | ;; Copyright (C) 1986, 1987, 1989, 1990 Free Software Foundation, Inc. | |
3 | ||
4 | ;; This file is part of GNU Emacs. | |
5 | ||
6 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
7 | ;; it under the terms of the GNU General Public License as published by | |
8 | ;; the Free Software Foundation; either version 1, or (at your option) | |
9 | ;; any later version. | |
10 | ||
11 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
12 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;; GNU General Public License for more details. | |
15 | ||
16 | ;; You should have received a copy of the GNU General Public License | |
17 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
18 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | ||
20 | ;;; Requires C-Scheme release 5 or later | |
21 | ;;; Changes to Control-G handler require runtime version 13.85 or later | |
22 | ||
23 | ;;; $Header: xscheme.el,v 1.26 90/09/11 01:51:20 GMT cph Exp $ | |
24 | ||
25 | (require 'scheme) | |
26 | \f | |
27 | (defvar scheme-program-name "scheme" | |
28 | "*Program invoked by the `run-scheme' command.") | |
29 | ||
30 | (defvar scheme-band-name nil | |
31 | "*Band loaded by the `run-scheme' command.") | |
32 | ||
33 | (defvar scheme-program-arguments nil | |
34 | "*Arguments passed to the Scheme program by the `run-scheme' command.") | |
35 | ||
36 | (defvar xscheme-allow-pipelined-evaluation t | |
37 | "If non-nil, an expression may be transmitted while another is evaluating. | |
38 | Otherwise, attempting to evaluate an expression before the previous expression | |
39 | has finished evaluating will signal an error.") | |
40 | ||
41 | (defvar xscheme-startup-message | |
42 | "This is the Scheme process buffer. | |
43 | Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point. | |
44 | Type \\[xscheme-send-control-g-interrupt] to abort evaluation. | |
45 | Type \\[describe-mode] for more information. | |
46 | ||
47 | " | |
48 | "String to insert into Scheme process buffer first time it is started. | |
49 | Is processed with `substitute-command-keys' first.") | |
50 | ||
51 | (defvar xscheme-signal-death-message nil | |
52 | "If non-nil, causes a message to be generated when the Scheme process dies.") | |
53 | ||
54 | (defun xscheme-evaluation-commands (keymap) | |
55 | (define-key keymap "\e\C-x" 'xscheme-send-definition) | |
56 | (define-key keymap "\C-x\C-e" 'advertised-xscheme-send-previous-expression) | |
57 | (define-key keymap "\eo" 'xscheme-send-buffer) | |
58 | (define-key keymap "\ez" 'xscheme-send-definition) | |
59 | (define-key keymap "\e\C-m" 'xscheme-send-previous-expression) | |
60 | (define-key keymap "\e\C-z" 'xscheme-send-region)) | |
61 | ||
62 | (defun xscheme-interrupt-commands (keymap) | |
63 | (define-key keymap "\C-c\C-s" 'xscheme-select-process-buffer) | |
64 | (define-key keymap "\C-c\C-b" 'xscheme-send-breakpoint-interrupt) | |
65 | (define-key keymap "\C-c\C-c" 'xscheme-send-control-g-interrupt) | |
66 | (define-key keymap "\C-c\C-u" 'xscheme-send-control-u-interrupt) | |
67 | (define-key keymap "\C-c\C-x" 'xscheme-send-control-x-interrupt)) | |
68 | ||
69 | (xscheme-evaluation-commands scheme-mode-map) | |
70 | (xscheme-interrupt-commands scheme-mode-map) | |
71 | \f | |
f9f9507e | 72 | ;;;###autoload |
a1ad21ca CH |
73 | (defun run-scheme (command-line) |
74 | "Run an inferior Scheme process. | |
75 | Output goes to the buffer `*scheme*'. | |
76 | With argument, asks for a command line." | |
77 | (interactive | |
78 | (list (let ((default | |
79 | (or xscheme-process-command-line | |
80 | (xscheme-default-command-line)))) | |
81 | (if current-prefix-arg | |
82 | (read-string "Run Scheme: " default) | |
83 | default)))) | |
84 | (setq xscheme-process-command-line command-line) | |
85 | (switch-to-buffer (xscheme-start-process command-line))) | |
86 | ||
87 | (defun reset-scheme () | |
88 | "Reset the Scheme process." | |
89 | (interactive) | |
90 | (let ((process (get-process "scheme"))) | |
91 | (cond ((or (not process) | |
92 | (not (eq (process-status process) 'run)) | |
93 | (yes-or-no-p | |
94 | "The Scheme process is running, are you SURE you want to reset it? ")) | |
95 | (message "Resetting Scheme process...") | |
96 | (if process (kill-process process t)) | |
97 | (xscheme-start-process xscheme-process-command-line) | |
98 | (message "Resetting Scheme process...done"))))) | |
99 | ||
100 | (defun xscheme-default-command-line () | |
101 | (concat scheme-program-name " -emacs" | |
102 | (if scheme-program-arguments | |
103 | (concat " " scheme-program-arguments) | |
104 | "") | |
105 | (if scheme-band-name | |
106 | (concat " -band " scheme-band-name) | |
107 | ""))) | |
108 | \f | |
109 | ;;;; Interaction Mode | |
110 | ||
111 | (defun scheme-interaction-mode () | |
112 | "Major mode for interacting with the inferior Scheme process. | |
113 | Like scheme-mode except that: | |
114 | ||
115 | \\[advertised-xscheme-send-previous-expression] sends the expression before point to the Scheme process as input | |
116 | \\[xscheme-yank-previous-send] yanks the expression most recently sent to Scheme | |
117 | ||
118 | All output from the Scheme process is written in the Scheme process | |
119 | buffer, which is initially named \"*scheme*\". The result of | |
120 | evaluating a Scheme expression is also printed in the process buffer, | |
121 | preceded by the string \";Value: \" to highlight it. If the process | |
122 | buffer is not visible at that time, the value will also be displayed | |
123 | in the minibuffer. If an error occurs, the process buffer will | |
124 | automatically pop up to show you the error message. | |
125 | ||
126 | While the Scheme process is running, the modelines of all buffers in | |
127 | scheme-mode are modified to show the state of the process. The | |
128 | possible states and their meanings are: | |
129 | ||
130 | input waiting for input | |
131 | run evaluating | |
132 | gc garbage collecting | |
133 | ||
134 | The process buffer's modeline contains additional information where | |
135 | the buffer's name is normally displayed: the command interpreter level | |
136 | and type. | |
137 | ||
138 | Scheme maintains a stack of command interpreters. Every time an error | |
139 | or breakpoint occurs, the current command interpreter is pushed on the | |
140 | command interpreter stack, and a new command interpreter is started. | |
141 | One example of why this is done is so that an error that occurs while | |
142 | you are debugging another error will not destroy the state of the | |
143 | initial error, allowing you to return to it after the second error has | |
144 | been fixed. | |
145 | ||
146 | The command interpreter level indicates how many interpreters are in | |
147 | the command interpreter stack. It is initially set to one, and it is | |
148 | incremented every time that stack is pushed, and decremented every | |
149 | time it is popped. The following commands are useful for manipulating | |
150 | the command interpreter stack: | |
151 | ||
152 | \\[xscheme-send-breakpoint-interrupt] pushes the stack once | |
153 | \\[xscheme-send-control-u-interrupt] pops the stack once | |
154 | \\[xscheme-send-control-g-interrupt] pops everything off | |
155 | \\[xscheme-send-control-x-interrupt] aborts evaluation, doesn't affect stack | |
156 | ||
157 | Some possible command interpreter types and their meanings are: | |
158 | ||
159 | [Evaluator] read-eval-print loop for evaluating expressions | |
160 | [Debugger] single character commands for debugging errors | |
161 | [Where] single character commands for examining environments | |
162 | ||
163 | Starting with release 6.2 of Scheme, the latter two types of command | |
164 | interpreters will change the major mode of the Scheme process buffer | |
165 | to scheme-debugger-mode , in which the evaluation commands are | |
166 | disabled, and the keys which normally self insert instead send | |
167 | themselves to the Scheme process. The command character ? will list | |
168 | the available commands. | |
169 | ||
170 | For older releases of Scheme, the major mode will be be | |
171 | scheme-interaction-mode , and the command characters must be sent as | |
172 | if they were expressions. | |
173 | ||
174 | Commands: | |
175 | Delete converts tabs to spaces as it moves back. | |
176 | Blank lines separate paragraphs. Semicolons start comments. | |
177 | \\{scheme-interaction-mode-map} | |
178 | ||
179 | Entry to this mode calls the value of scheme-interaction-mode-hook | |
180 | with no args, if that value is non-nil. | |
181 | Likewise with the value of scheme-mode-hook. | |
182 | scheme-interaction-mode-hook is called after scheme-mode-hook." | |
183 | (interactive) | |
184 | (kill-all-local-variables) | |
185 | (scheme-interaction-mode-initialize) | |
186 | (scheme-mode-variables) | |
187 | (make-local-variable 'xscheme-previous-send) | |
188 | (run-hooks 'scheme-mode-hook 'scheme-interaction-mode-hook)) | |
189 | ||
190 | (defun scheme-interaction-mode-initialize () | |
191 | (use-local-map scheme-interaction-mode-map) | |
192 | (setq major-mode 'scheme-interaction-mode) | |
193 | (setq mode-name "Scheme Interaction")) | |
194 | ||
195 | (defun scheme-interaction-mode-commands (keymap) | |
196 | (define-key keymap "\C-c\C-m" 'xscheme-send-current-line) | |
197 | (define-key keymap "\C-c\C-p" 'xscheme-send-proceed) | |
198 | (define-key keymap "\C-c\C-y" 'xscheme-yank-previous-send)) | |
199 | ||
200 | (defvar scheme-interaction-mode-map nil) | |
201 | (if (not scheme-interaction-mode-map) | |
202 | (progn | |
203 | (setq scheme-interaction-mode-map (make-keymap)) | |
204 | (scheme-mode-commands scheme-interaction-mode-map) | |
205 | (xscheme-interrupt-commands scheme-interaction-mode-map) | |
206 | (xscheme-evaluation-commands scheme-interaction-mode-map) | |
207 | (scheme-interaction-mode-commands scheme-interaction-mode-map))) | |
208 | ||
209 | (defun xscheme-enter-interaction-mode () | |
210 | (save-excursion | |
211 | (set-buffer (xscheme-process-buffer)) | |
212 | (if (not (eq major-mode 'scheme-interaction-mode)) | |
213 | (if (eq major-mode 'scheme-debugger-mode) | |
214 | (scheme-interaction-mode-initialize) | |
215 | (scheme-interaction-mode))))) | |
216 | ||
217 | (fset 'advertised-xscheme-send-previous-expression | |
218 | 'xscheme-send-previous-expression) | |
219 | \f | |
220 | ;;;; Debugger Mode | |
221 | ||
222 | (defun scheme-debugger-mode () | |
223 | "Major mode for executing the Scheme debugger. | |
224 | Like scheme-mode except that the evaluation commands | |
225 | are disabled, and characters that would normally be self inserting are | |
226 | sent to the Scheme process instead. Typing ? will show you which | |
227 | characters perform useful functions. | |
228 | ||
229 | Commands: | |
230 | \\{scheme-debugger-mode-map}" | |
231 | (error "Illegal entry to scheme-debugger-mode")) | |
232 | ||
233 | (defun scheme-debugger-mode-initialize () | |
234 | (use-local-map scheme-debugger-mode-map) | |
235 | (setq major-mode 'scheme-debugger-mode) | |
236 | (setq mode-name "Scheme Debugger")) | |
237 | ||
238 | (defun scheme-debugger-mode-commands (keymap) | |
239 | (let ((char ? )) | |
240 | (while (< char 127) | |
241 | (define-key keymap (char-to-string char) 'scheme-debugger-self-insert) | |
242 | (setq char (1+ char))))) | |
243 | ||
244 | (defvar scheme-debugger-mode-map nil) | |
245 | (if (not scheme-debugger-mode-map) | |
246 | (progn | |
247 | (setq scheme-debugger-mode-map (make-keymap)) | |
248 | (scheme-mode-commands scheme-debugger-mode-map) | |
249 | (xscheme-interrupt-commands scheme-debugger-mode-map) | |
250 | (scheme-debugger-mode-commands scheme-debugger-mode-map))) | |
251 | ||
252 | (defun scheme-debugger-self-insert () | |
253 | "Transmit this character to the Scheme process." | |
254 | (interactive) | |
255 | (xscheme-send-char last-command-char)) | |
256 | ||
257 | (defun xscheme-enter-debugger-mode (prompt-string) | |
258 | (save-excursion | |
259 | (set-buffer (xscheme-process-buffer)) | |
260 | (if (not (eq major-mode 'scheme-debugger-mode)) | |
261 | (progn | |
262 | (if (not (eq major-mode 'scheme-interaction-mode)) | |
263 | (scheme-interaction-mode)) | |
264 | (scheme-debugger-mode-initialize))))) | |
265 | ||
266 | (defun xscheme-debugger-mode-p () | |
267 | (let ((buffer (xscheme-process-buffer))) | |
268 | (and buffer | |
269 | (save-excursion | |
270 | (set-buffer buffer) | |
271 | (eq major-mode 'scheme-debugger-mode))))) | |
272 | \f | |
273 | ;;;; Evaluation Commands | |
274 | ||
275 | (defun xscheme-send-string (&rest strings) | |
276 | "Send the string arguments to the Scheme process. | |
277 | The strings are concatenated and terminated by a newline." | |
278 | (cond ((not (xscheme-process-running-p)) | |
279 | (if (yes-or-no-p "The Scheme process has died. Reset it? ") | |
280 | (progn | |
281 | (reset-scheme) | |
282 | (xscheme-wait-for-process) | |
283 | (goto-char (point-max)) | |
284 | (apply 'insert-before-markers strings) | |
285 | (xscheme-send-string-1 strings)))) | |
286 | ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode")) | |
287 | ((and (not xscheme-allow-pipelined-evaluation) | |
288 | xscheme-running-p) | |
289 | (error "No sends allowed while Scheme running")) | |
290 | (t (xscheme-send-string-1 strings)))) | |
291 | ||
292 | (defun xscheme-send-string-1 (strings) | |
293 | (let ((string (apply 'concat strings))) | |
294 | (xscheme-send-string-2 string) | |
295 | (if (eq major-mode 'scheme-interaction-mode) | |
296 | (setq xscheme-previous-send string)))) | |
297 | ||
298 | (defun xscheme-send-string-2 (string) | |
299 | (let ((process (get-process "scheme"))) | |
300 | (send-string process (concat string "\n")) | |
301 | (if (xscheme-process-buffer-current-p) | |
302 | (set-marker (process-mark process) (point))))) | |
303 | ||
304 | (defun xscheme-yank-previous-send () | |
305 | "Insert the most recent expression at point." | |
306 | (interactive) | |
307 | (push-mark) | |
308 | (insert xscheme-previous-send)) | |
309 | ||
310 | (defun xscheme-select-process-buffer () | |
311 | "Select the Scheme process buffer and move to its output point." | |
312 | (interactive) | |
313 | (let ((process (or (get-process "scheme") (error "No scheme process")))) | |
314 | (let ((buffer (or (process-buffer process) (error "No process buffer")))) | |
315 | (let ((window (get-buffer-window buffer))) | |
316 | (if window | |
317 | (select-window window) | |
318 | (switch-to-buffer buffer)) | |
319 | (goto-char (process-mark process)))))) | |
320 | \f | |
321 | (defun xscheme-send-region (start end) | |
322 | "Send the current region to the Scheme process. | |
323 | The region is sent terminated by a newline." | |
324 | (interactive "r") | |
325 | (if (xscheme-process-buffer-current-p) | |
326 | (progn (goto-char end) | |
327 | (set-marker (process-mark (get-process "scheme")) end))) | |
328 | (xscheme-send-string (buffer-substring start end))) | |
329 | ||
330 | (defun xscheme-send-definition () | |
331 | "Send the current definition to the Scheme process. | |
332 | If the current line begins with a non-whitespace character, | |
333 | parse an expression from the beginning of the line and send that instead." | |
334 | (interactive) | |
335 | (let ((start nil) (end nil)) | |
336 | (save-excursion | |
337 | (end-of-defun) | |
338 | (setq end (point)) | |
339 | (if (re-search-backward "^\\s(" nil t) | |
340 | (setq start (point)) | |
341 | (error "Can't find definition"))) | |
342 | (xscheme-send-region start end))) | |
343 | ||
344 | (defun xscheme-send-next-expression () | |
345 | "Send the expression to the right of `point' to the Scheme process." | |
346 | (interactive) | |
347 | (let ((start (point))) | |
348 | (xscheme-send-region start (save-excursion (forward-sexp) (point))))) | |
349 | ||
350 | (defun xscheme-send-previous-expression () | |
351 | "Send the expression to the left of `point' to the Scheme process." | |
352 | (interactive) | |
353 | (let ((end (point))) | |
354 | (xscheme-send-region (save-excursion (backward-sexp) (point)) end))) | |
355 | \f | |
356 | (defun xscheme-send-current-line () | |
357 | "Send the current line to the Scheme process. | |
358 | Useful for working with debugging Scheme under adb." | |
359 | (interactive) | |
360 | (let ((line | |
361 | (save-excursion | |
362 | (beginning-of-line) | |
363 | (let ((start (point))) | |
364 | (end-of-line) | |
365 | (buffer-substring start (point)))))) | |
366 | (end-of-line) | |
367 | (insert ?\n) | |
368 | (xscheme-send-string-2 line))) | |
369 | ||
370 | (defun xscheme-send-buffer () | |
371 | "Send the current buffer to the Scheme process." | |
372 | (interactive) | |
373 | (if (xscheme-process-buffer-current-p) | |
374 | (error "Not allowed to send this buffer's contents to Scheme")) | |
375 | (xscheme-send-region (point-min) (point-max))) | |
376 | ||
377 | (defun xscheme-send-char (char) | |
378 | "Prompt for a character and send it to the Scheme process." | |
379 | (interactive "cCharacter to send: ") | |
380 | (send-string "scheme" (char-to-string char))) | |
381 | \f | |
382 | ;;;; Interrupts | |
383 | ||
384 | (defun xscheme-send-breakpoint-interrupt () | |
385 | "Cause the Scheme process to enter a breakpoint." | |
386 | (interactive) | |
387 | (xscheme-send-interrupt ?b nil)) | |
388 | ||
389 | (defun xscheme-send-proceed () | |
390 | "Cause the Scheme process to proceed from a breakpoint." | |
391 | (interactive) | |
392 | (send-string "scheme" "(proceed)\n")) | |
393 | ||
394 | (defun xscheme-send-control-g-interrupt () | |
395 | "Cause the Scheme processor to halt and flush input. | |
396 | Control returns to the top level rep loop." | |
397 | (interactive) | |
398 | (let ((inhibit-quit t)) | |
399 | (cond ((not xscheme-control-g-synchronization-p) | |
400 | (interrupt-process "scheme")) | |
401 | (xscheme-control-g-disabled-p | |
402 | (message "Relax...")) | |
403 | (t | |
404 | (setq xscheme-control-g-disabled-p t) | |
405 | (message "Sending C-G interrupt to Scheme...") | |
406 | (interrupt-process "scheme") | |
407 | (send-string "scheme" (char-to-string 0)))))) | |
408 | ||
409 | (defun xscheme-send-control-u-interrupt () | |
410 | "Cause the Scheme process to halt, returning to previous rep loop." | |
411 | (interactive) | |
412 | (xscheme-send-interrupt ?u t)) | |
413 | ||
414 | (defun xscheme-send-control-x-interrupt () | |
415 | "Cause the Scheme process to halt, returning to current rep loop." | |
416 | (interactive) | |
417 | (xscheme-send-interrupt ?x t)) | |
418 | ||
419 | ;;; This doesn't really work right -- Scheme just gobbles the first | |
420 | ;;; character in the input. There is no way for us to guarantee that | |
421 | ;;; the argument to this procedure is the first char unless we put | |
422 | ;;; some kind of marker in the input stream. | |
423 | ||
424 | (defun xscheme-send-interrupt (char mark-p) | |
425 | "Send a ^A type interrupt to the Scheme process." | |
426 | (interactive "cInterrupt character to send: ") | |
427 | (quit-process "scheme") | |
428 | (send-string "scheme" (char-to-string char)) | |
429 | (if (and mark-p xscheme-control-g-synchronization-p) | |
430 | (send-string "scheme" (char-to-string 0)))) | |
431 | \f | |
432 | ;;;; Internal Variables | |
433 | ||
434 | (defvar xscheme-process-command-line nil | |
435 | "Command used to start the most recent Scheme process.") | |
436 | ||
437 | (defvar xscheme-previous-send "" | |
438 | "Most recent expression transmitted to the Scheme process.") | |
439 | ||
440 | (defvar xscheme-process-filter-state 'idle | |
441 | "State of scheme process escape reader state machine: | |
442 | idle waiting for an escape sequence | |
443 | reading-type received an altmode but nothing else | |
444 | reading-string reading prompt string") | |
445 | ||
446 | (defvar xscheme-running-p nil | |
447 | "This variable, if nil, indicates that the scheme process is | |
448 | waiting for input. Otherwise, it is busy evaluating something.") | |
449 | ||
450 | (defconst xscheme-control-g-synchronization-p t | |
451 | "If non-nil, insert markers in the scheme input stream to indicate when | |
452 | control-g interrupts were signalled. Do not allow more control-g's to be | |
453 | signalled until the scheme process acknowledges receipt.") | |
454 | ||
455 | (defvar xscheme-control-g-disabled-p nil | |
456 | "This variable, if non-nil, indicates that a control-g is being processed | |
457 | by the scheme process, so additional control-g's are to be ignored.") | |
458 | ||
459 | (defvar xscheme-allow-output-p t | |
460 | "This variable, if nil, prevents output from the scheme process | |
461 | from being inserted into the process-buffer.") | |
462 | ||
463 | (defvar xscheme-prompt "" | |
464 | "The current scheme prompt string.") | |
465 | ||
466 | (defvar xscheme-string-accumulator "" | |
467 | "Accumulator for the string being received from the scheme process.") | |
468 | ||
469 | (defvar xscheme-string-receiver nil | |
470 | "Procedure to send the string argument from the scheme process.") | |
471 | ||
472 | (defvar xscheme-start-hook nil | |
473 | "If non-nil, a procedure to call when the Scheme process is started. | |
474 | When called, the current buffer will be the Scheme process-buffer.") | |
475 | ||
476 | (defvar xscheme-runlight-string nil) | |
477 | (defvar xscheme-mode-string nil) | |
478 | (defvar xscheme-filter-input nil) | |
479 | \f | |
480 | ;;;; Basic Process Control | |
481 | ||
482 | (defun xscheme-start-process (command-line) | |
483 | (let ((buffer (get-buffer-create "*scheme*"))) | |
484 | (let ((process (get-buffer-process buffer))) | |
485 | (save-excursion | |
486 | (set-buffer buffer) | |
487 | (if (and process (memq (process-status process) '(run stop))) | |
488 | (set-marker (process-mark process) (point-max)) | |
489 | (progn (if process (delete-process process)) | |
490 | (goto-char (point-max)) | |
491 | (scheme-interaction-mode) | |
492 | (if (bobp) | |
493 | (insert-before-markers | |
494 | (substitute-command-keys xscheme-startup-message))) | |
495 | (setq process | |
496 | (let ((process-connection-type nil)) | |
497 | (apply 'start-process | |
498 | (cons "scheme" | |
499 | (cons buffer | |
500 | (xscheme-parse-command-line | |
501 | command-line)))))) | |
502 | (set-marker (process-mark process) (point-max)) | |
503 | (xscheme-process-filter-initialize t) | |
504 | (xscheme-modeline-initialize) | |
505 | (set-process-sentinel process 'xscheme-process-sentinel) | |
506 | (set-process-filter process 'xscheme-process-filter) | |
507 | (run-hooks 'xscheme-start-hook))))) | |
508 | buffer)) | |
509 | ||
510 | (defun xscheme-parse-command-line (string) | |
511 | (setq string (substitute-in-file-name string)) | |
512 | (let ((start 0) | |
513 | (result '())) | |
514 | (while start | |
515 | (let ((index (string-match "[ \t]" string start))) | |
516 | (setq start | |
517 | (cond ((not index) | |
518 | (setq result | |
519 | (cons (substring string start) | |
520 | result)) | |
521 | nil) | |
522 | ((= index start) | |
523 | (string-match "[^ \t]" string start)) | |
524 | (t | |
525 | (setq result | |
526 | (cons (substring string start index) | |
527 | result)) | |
528 | (1+ index)))))) | |
529 | (nreverse result))) | |
530 | \f | |
531 | (defun xscheme-wait-for-process () | |
532 | (sleep-for 2) | |
533 | (while xscheme-running-p | |
534 | (sleep-for 1))) | |
535 | ||
536 | (defun xscheme-process-running-p () | |
537 | "True iff there is a Scheme process whose status is `run'." | |
538 | (let ((process (get-process "scheme"))) | |
539 | (and process | |
540 | (eq (process-status process) 'run)))) | |
541 | ||
542 | (defun xscheme-process-buffer () | |
543 | (let ((process (get-process "scheme"))) | |
544 | (and process (process-buffer process)))) | |
545 | ||
546 | (defun xscheme-process-buffer-window () | |
547 | (let ((buffer (xscheme-process-buffer))) | |
548 | (and buffer (get-buffer-window buffer)))) | |
549 | ||
550 | (defun xscheme-process-buffer-current-p () | |
551 | "True iff the current buffer is the Scheme process buffer." | |
552 | (eq (xscheme-process-buffer) (current-buffer))) | |
553 | \f | |
554 | ;;;; Process Filter | |
555 | ||
556 | (defun xscheme-process-sentinel (proc reason) | |
557 | (xscheme-process-filter-initialize (eq reason 'run)) | |
558 | (if (eq reason 'run) | |
559 | (xscheme-modeline-initialize) | |
560 | (progn | |
561 | (setq scheme-mode-line-process "") | |
562 | (setq xscheme-mode-string "no process"))) | |
563 | (if (and (not (memq reason '(run stop))) | |
564 | xscheme-signal-death-message) | |
565 | (progn (beep) | |
566 | (message | |
567 | "The Scheme process has died! Do M-x reset-scheme to restart it")))) | |
568 | ||
569 | (defun xscheme-process-filter-initialize (running-p) | |
570 | (setq xscheme-process-filter-state 'idle) | |
571 | (setq xscheme-running-p running-p) | |
572 | (setq xscheme-control-g-disabled-p nil) | |
573 | (setq xscheme-allow-output-p t) | |
574 | (setq xscheme-prompt "") | |
575 | (setq scheme-mode-line-process '(": " xscheme-runlight-string))) | |
576 | ||
577 | (defun xscheme-process-filter (proc string) | |
578 | (let ((xscheme-filter-input string)) | |
579 | (while xscheme-filter-input | |
580 | (cond ((eq xscheme-process-filter-state 'idle) | |
581 | (let ((start (string-match "\e" xscheme-filter-input))) | |
582 | (if start | |
583 | (progn | |
584 | (xscheme-process-filter-output | |
585 | (substring xscheme-filter-input 0 start)) | |
586 | (setq xscheme-filter-input | |
587 | (substring xscheme-filter-input (1+ start))) | |
588 | (setq xscheme-process-filter-state 'reading-type)) | |
589 | (let ((string xscheme-filter-input)) | |
590 | (setq xscheme-filter-input nil) | |
591 | (xscheme-process-filter-output string))))) | |
592 | ((eq xscheme-process-filter-state 'reading-type) | |
593 | (if (zerop (length xscheme-filter-input)) | |
594 | (setq xscheme-filter-input nil) | |
595 | (let ((char (aref xscheme-filter-input 0))) | |
596 | (setq xscheme-filter-input | |
597 | (substring xscheme-filter-input 1)) | |
598 | (let ((entry (assoc char xscheme-process-filter-alist))) | |
599 | (if entry | |
600 | (funcall (nth 2 entry) (nth 1 entry)) | |
601 | (progn | |
602 | (xscheme-process-filter-output ?\e char) | |
603 | (setq xscheme-process-filter-state 'idle))))))) | |
604 | ((eq xscheme-process-filter-state 'reading-string) | |
605 | (let ((start (string-match "\e" xscheme-filter-input))) | |
606 | (if start | |
607 | (let ((string | |
608 | (concat xscheme-string-accumulator | |
609 | (substring xscheme-filter-input 0 start)))) | |
610 | (setq xscheme-filter-input | |
611 | (substring xscheme-filter-input (1+ start))) | |
612 | (setq xscheme-process-filter-state 'idle) | |
613 | (funcall xscheme-string-receiver string)) | |
614 | (progn | |
615 | (setq xscheme-string-accumulator | |
616 | (concat xscheme-string-accumulator | |
617 | xscheme-filter-input)) | |
618 | (setq xscheme-filter-input nil))))) | |
619 | (t | |
620 | (error "Scheme process filter -- bad state")))))) | |
621 | \f | |
622 | ;;;; Process Filter Output | |
623 | ||
624 | (defun xscheme-process-filter-output (&rest args) | |
625 | (if xscheme-allow-output-p | |
626 | (let ((string (apply 'concat args))) | |
627 | (save-excursion | |
628 | (xscheme-goto-output-point) | |
629 | (while (string-match "\\(\007\\|\f\\)" string) | |
630 | (let ((start (match-beginning 0)) | |
631 | (end (match-end 0))) | |
632 | (insert-before-markers (substring string 0 start)) | |
633 | (if (= ?\f (aref string start)) | |
634 | (progn | |
635 | (if (not (bolp)) | |
636 | (insert-before-markers ?\n)) | |
637 | (insert-before-markers ?\f)) | |
638 | (beep)) | |
639 | (setq string (substring string (1+ start))))) | |
640 | (insert-before-markers string))))) | |
641 | ||
642 | (defun xscheme-guarantee-newlines (n) | |
643 | (if xscheme-allow-output-p | |
644 | (save-excursion | |
645 | (xscheme-goto-output-point) | |
646 | (let ((stop nil)) | |
647 | (while (and (not stop) | |
648 | (bolp)) | |
649 | (setq n (1- n)) | |
650 | (if (bobp) | |
651 | (setq stop t) | |
652 | (backward-char)))) | |
653 | (xscheme-goto-output-point) | |
654 | (while (> n 0) | |
655 | (insert-before-markers ?\n) | |
656 | (setq n (1- n)))))) | |
657 | ||
658 | (defun xscheme-goto-output-point () | |
659 | (let ((process (get-process "scheme"))) | |
660 | (set-buffer (process-buffer process)) | |
661 | (goto-char (process-mark process)))) | |
662 | ||
663 | (defun xscheme-modeline-initialize () | |
664 | (setq xscheme-runlight-string "") | |
665 | (setq xscheme-mode-string "") | |
666 | (setq mode-line-buffer-identification '("Scheme: " xscheme-mode-string))) | |
667 | ||
668 | (defun xscheme-set-runlight (runlight) | |
669 | (setq xscheme-runlight-string runlight) | |
670 | (xscheme-modeline-redisplay)) | |
671 | ||
672 | (defun xscheme-modeline-redisplay () | |
673 | (save-excursion (set-buffer (other-buffer))) | |
674 | (set-buffer-modified-p (buffer-modified-p)) | |
675 | (sit-for 0)) | |
676 | \f | |
677 | ;;;; Process Filter Operations | |
678 | ||
679 | (defvar xscheme-process-filter-alist | |
680 | '((?D xscheme-enter-debugger-mode | |
681 | xscheme-process-filter:string-action) | |
682 | (?E xscheme-eval | |
683 | xscheme-process-filter:string-action) | |
684 | (?P xscheme-set-prompt-variable | |
685 | xscheme-process-filter:string-action) | |
686 | (?R xscheme-enter-interaction-mode | |
687 | xscheme-process-filter:simple-action) | |
688 | (?b xscheme-start-gc | |
689 | xscheme-process-filter:simple-action) | |
690 | (?e xscheme-finish-gc | |
691 | xscheme-process-filter:simple-action) | |
692 | (?f xscheme-exit-input-wait | |
693 | xscheme-process-filter:simple-action) | |
694 | (?g xscheme-enable-control-g | |
695 | xscheme-process-filter:simple-action) | |
696 | (?i xscheme-prompt-for-expression | |
697 | xscheme-process-filter:string-action) | |
698 | (?m xscheme-message | |
699 | xscheme-process-filter:string-action) | |
700 | (?n xscheme-prompt-for-confirmation | |
701 | xscheme-process-filter:string-action) | |
702 | (?o xscheme-output-goto | |
703 | xscheme-process-filter:simple-action) | |
704 | (?p xscheme-set-prompt | |
705 | xscheme-process-filter:string-action) | |
706 | (?s xscheme-enter-input-wait | |
707 | xscheme-process-filter:simple-action) | |
708 | (?v xscheme-write-value | |
709 | xscheme-process-filter:string-action) | |
710 | (?w xscheme-cd | |
711 | xscheme-process-filter:string-action) | |
712 | (?z xscheme-display-process-buffer | |
713 | xscheme-process-filter:simple-action) | |
714 | (?c xscheme-unsolicited-read-char | |
715 | xscheme-process-filter:simple-action)) | |
716 | "Table used to decide how to handle process filter commands. | |
717 | Value is a list of entries, each entry is a list of three items. | |
718 | ||
719 | The first item is the character that the process filter dispatches on. | |
720 | The second item is the action to be taken, a function. | |
721 | The third item is the handler for the entry, a function. | |
722 | ||
723 | When the process filter sees a command whose character matches a | |
724 | particular entry, it calls the handler with two arguments: the action | |
725 | and the string containing the rest of the process filter's input | |
726 | stream. It is the responsibility of the handler to invoke the action | |
727 | with the appropriate arguments, and to reenter the process filter with | |
728 | the remaining input.") | |
729 | \f | |
730 | (defun xscheme-process-filter:simple-action (action) | |
731 | (setq xscheme-process-filter-state 'idle) | |
732 | (funcall action)) | |
733 | ||
734 | (defun xscheme-process-filter:string-action (action) | |
735 | (setq xscheme-string-receiver action) | |
736 | (setq xscheme-string-accumulator "") | |
737 | (setq xscheme-process-filter-state 'reading-string)) | |
738 | ||
739 | (defconst xscheme-runlight:running "run" | |
740 | "The character displayed when the Scheme process is running.") | |
741 | ||
742 | (defconst xscheme-runlight:input "input" | |
743 | "The character displayed when the Scheme process is waiting for input.") | |
744 | ||
745 | (defconst xscheme-runlight:gc "gc" | |
746 | "The character displayed when the Scheme process is garbage collecting.") | |
747 | ||
748 | (defun xscheme-start-gc () | |
749 | (xscheme-set-runlight xscheme-runlight:gc)) | |
750 | ||
751 | (defun xscheme-finish-gc () | |
752 | (xscheme-set-runlight | |
753 | (if xscheme-running-p xscheme-runlight:running xscheme-runlight:input))) | |
754 | ||
755 | (defun xscheme-enter-input-wait () | |
756 | (xscheme-set-runlight xscheme-runlight:input) | |
757 | (setq xscheme-running-p nil)) | |
758 | ||
759 | (defun xscheme-exit-input-wait () | |
760 | (xscheme-set-runlight xscheme-runlight:running) | |
761 | (setq xscheme-running-p t)) | |
762 | ||
763 | (defun xscheme-enable-control-g () | |
764 | (setq xscheme-control-g-disabled-p nil)) | |
765 | ||
766 | (defun xscheme-display-process-buffer () | |
767 | (let ((window (or (xscheme-process-buffer-window) | |
768 | (display-buffer (xscheme-process-buffer))))) | |
769 | (save-window-excursion | |
770 | (select-window window) | |
771 | (xscheme-goto-output-point) | |
772 | (if (xscheme-debugger-mode-p) | |
773 | (xscheme-enter-interaction-mode))))) | |
774 | ||
775 | (defun xscheme-unsolicited-read-char () | |
776 | nil) | |
777 | \f | |
778 | (defun xscheme-eval (string) | |
779 | (eval (car (read-from-string string)))) | |
780 | ||
781 | (defun xscheme-message (string) | |
782 | (if (not (zerop (length string))) | |
783 | (xscheme-write-message-1 string (format ";%s" string)))) | |
784 | ||
785 | (defun xscheme-write-value (string) | |
786 | (if (zerop (length string)) | |
787 | (xscheme-write-message-1 "(no value)" ";No value") | |
788 | (xscheme-write-message-1 string (format ";Value: %s" string)))) | |
789 | ||
790 | (defun xscheme-write-message-1 (message-string output-string) | |
791 | (let* ((process (get-process "scheme")) | |
792 | (window (get-buffer-window (process-buffer process)))) | |
793 | (if (or (not window) | |
794 | (not (pos-visible-in-window-p (process-mark process) | |
795 | window))) | |
796 | (message "%s" message-string))) | |
797 | (xscheme-guarantee-newlines 1) | |
798 | (xscheme-process-filter-output output-string)) | |
799 | ||
800 | (defun xscheme-set-prompt-variable (string) | |
801 | (setq xscheme-prompt string)) | |
802 | ||
803 | (defun xscheme-set-prompt (string) | |
804 | (setq xscheme-prompt string) | |
805 | (xscheme-guarantee-newlines 2) | |
806 | (setq xscheme-mode-string (xscheme-coerce-prompt string)) | |
807 | (xscheme-modeline-redisplay)) | |
808 | ||
809 | (defun xscheme-output-goto () | |
810 | (xscheme-goto-output-point) | |
811 | (xscheme-guarantee-newlines 2)) | |
812 | ||
813 | (defun xscheme-coerce-prompt (string) | |
814 | (if (string-match "^[0-9]+ " string) | |
815 | (let ((end (match-end 0))) | |
816 | (concat (substring string 0 end) | |
817 | (let ((prompt (substring string end))) | |
818 | (let ((entry (assoc prompt xscheme-prompt-alist))) | |
819 | (if entry | |
820 | (cdr entry) | |
821 | prompt))))) | |
822 | string)) | |
823 | ||
824 | (defvar xscheme-prompt-alist | |
825 | '(("[Normal REPL]" . "[Evaluator]") | |
826 | ("[Error REPL]" . "[Evaluator]") | |
827 | ("[Breakpoint REPL]" . "[Evaluator]") | |
828 | ("[Debugger REPL]" . "[Evaluator]") | |
829 | ("[Visiting environment]" . "[Evaluator]") | |
830 | ("[Environment Inspector]" . "[Where]")) | |
831 | "An alist which maps the Scheme command interpreter type to a print string.") | |
832 | ||
833 | (defun xscheme-cd (directory-string) | |
834 | (save-excursion | |
835 | (set-buffer (xscheme-process-buffer)) | |
836 | (cd directory-string))) | |
837 | \f | |
838 | (defun xscheme-prompt-for-confirmation (prompt-string) | |
839 | (xscheme-send-char (if (y-or-n-p prompt-string) ?y ?n))) | |
840 | ||
841 | (defun xscheme-prompt-for-expression (prompt-string) | |
842 | (xscheme-send-string-2 | |
843 | (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map))) | |
844 | ||
845 | (defvar xscheme-prompt-for-expression-map nil) | |
846 | (if (not xscheme-prompt-for-expression-map) | |
847 | (progn | |
848 | (setq xscheme-prompt-for-expression-map | |
849 | (copy-keymap minibuffer-local-map)) | |
850 | (substitute-key-definition 'exit-minibuffer | |
851 | 'xscheme-prompt-for-expression-exit | |
852 | xscheme-prompt-for-expression-map))) | |
853 | ||
854 | (defun xscheme-prompt-for-expression-exit () | |
855 | (interactive) | |
856 | (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one) | |
857 | (exit-minibuffer) | |
858 | (error "input must be a single, complete expression"))) | |
859 | ||
860 | (defun xscheme-region-expression-p (start end) | |
861 | (save-excursion | |
862 | (let ((old-syntax-table (syntax-table))) | |
863 | (unwind-protect | |
864 | (progn | |
865 | (set-syntax-table scheme-mode-syntax-table) | |
866 | (let ((state (parse-partial-sexp start end))) | |
867 | (and (zerop (car state)) ;depth = 0 | |
868 | (nth 2 state) ;last-sexp exists, i.e. >= 1 sexps | |
869 | (let ((state (parse-partial-sexp start (nth 2 state)))) | |
870 | (if (nth 2 state) 'many 'one))))) | |
871 | (set-syntax-table old-syntax-table))))) |