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