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