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