1 ;; Run Scheme under Emacs
2 ;; Copyright (C) 1986, 1987, 1989, 1990 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
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)
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.
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.
20 ;;; Requires C-Scheme release 5 or later
21 ;;; Changes to Control-G handler require runtime version 13.85 or later
23 ;;; $Header: xscheme.el,v 1.26 90/09/11 01:51:20 GMT cph Exp $
27 (defvar scheme-program-name "scheme"
28 "*Program invoked by the `run-scheme' command.")
30 (defvar scheme-band-name nil
31 "*Band loaded by the `run-scheme' command.")
33 (defvar scheme-program-arguments nil
34 "*Arguments passed to the Scheme program by the `run-scheme' command.")
36 (defvar xscheme-allow-pipelined-evaluation t
37 "If non-nil, an expression may be transmitted while another is evaluating.
38 Otherwise, attempting to evaluate an expression before the previous expression
39 has finished evaluating will signal an error.")
41 (defvar xscheme-startup-message
42 "This is the Scheme process buffer.
43 Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point.
44 Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
45 Type \\[describe-mode] for more information.
48 "String to insert into Scheme process buffer first time it is started.
49 Is processed with `substitute-command-keys' first.")
51 (defvar xscheme-signal-death-message nil
52 "If non-nil, causes a message to be generated when the Scheme process dies.")
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))
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))
69 (xscheme-evaluation-commands scheme-mode-map)
70 (xscheme-interrupt-commands scheme-mode-map)
72 (defun run-scheme (command-line)
73 "Run an inferior Scheme process.
74 Output goes to the buffer `*scheme*'.
75 With argument, asks for a command line."
78 (or xscheme-process-command-line
79 (xscheme-default-command-line))))
80 (if current-prefix-arg
81 (read-string "Run Scheme: " default)
83 (setq xscheme-process-command-line command-line)
84 (switch-to-buffer (xscheme-start-process command-line)))
86 (defun reset-scheme ()
87 "Reset the Scheme process."
89 (let ((process (get-process "scheme")))
90 (cond ((or (not process)
91 (not (eq (process-status process) 'run))
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")))))
99 (defun xscheme-default-command-line ()
100 (concat scheme-program-name " -emacs"
101 (if scheme-program-arguments
102 (concat " " scheme-program-arguments)
105 (concat " -band " scheme-band-name)
108 ;;;; Interaction Mode
110 (defun scheme-interaction-mode ()
111 "Major mode for interacting with the inferior Scheme process.
112 Like scheme-mode except that:
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
117 All output from the Scheme process is written in the Scheme process
118 buffer, which is initially named \"*scheme*\". The result of
119 evaluating a Scheme expression is also printed in the process buffer,
120 preceded by the string \";Value: \" to highlight it. If the process
121 buffer is not visible at that time, the value will also be displayed
122 in the minibuffer. If an error occurs, the process buffer will
123 automatically pop up to show you the error message.
125 While the Scheme process is running, the modelines of all buffers in
126 scheme-mode are modified to show the state of the process. The
127 possible states and their meanings are:
129 input waiting for input
131 gc garbage collecting
133 The process buffer's modeline contains additional information where
134 the buffer's name is normally displayed: the command interpreter level
137 Scheme maintains a stack of command interpreters. Every time an error
138 or breakpoint occurs, the current command interpreter is pushed on the
139 command interpreter stack, and a new command interpreter is started.
140 One example of why this is done is so that an error that occurs while
141 you are debugging another error will not destroy the state of the
142 initial error, allowing you to return to it after the second error has
145 The command interpreter level indicates how many interpreters are in
146 the command interpreter stack. It is initially set to one, and it is
147 incremented every time that stack is pushed, and decremented every
148 time it is popped. The following commands are useful for manipulating
149 the command interpreter stack:
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
156 Some possible command interpreter types and their meanings are:
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
162 Starting with release 6.2 of Scheme, the latter two types of command
163 interpreters will change the major mode of the Scheme process buffer
164 to scheme-debugger-mode , in which the evaluation commands are
165 disabled, and the keys which normally self insert instead send
166 themselves to the Scheme process. The command character ? will list
167 the available commands.
169 For older releases of Scheme, the major mode will be be
170 scheme-interaction-mode , and the command characters must be sent as
171 if they were expressions.
174 Delete converts tabs to spaces as it moves back.
175 Blank lines separate paragraphs. Semicolons start comments.
176 \\{scheme-interaction-mode-map}
178 Entry to this mode calls the value of scheme-interaction-mode-hook
179 with 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."
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))
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"))
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))
199 (defvar scheme-interaction-mode-map nil)
200 (if (not scheme-interaction-mode-map)
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)))
208 (defun xscheme-enter-interaction-mode ()
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)))))
216 (fset 'advertised-xscheme-send-previous-expression
217 'xscheme-send-previous-expression)
221 (defun scheme-debugger-mode ()
222 "Major mode for executing the Scheme debugger.
223 Like scheme-mode except that the evaluation commands
224 are disabled, and characters that would normally be self inserting are
225 sent to the Scheme process instead. Typing ? will show you which
226 characters perform useful functions.
229 \\{scheme-debugger-mode-map}"
230 (error "Illegal entry to scheme-debugger-mode"))
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"))
237 (defun scheme-debugger-mode-commands (keymap)
240 (define-key keymap (char-to-string char) 'scheme-debugger-self-insert)
241 (setq char (1+ char)))))
243 (defvar scheme-debugger-mode-map nil)
244 (if (not scheme-debugger-mode-map)
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)))
251 (defun scheme-debugger-self-insert ()
252 "Transmit this character to the Scheme process."
254 (xscheme-send-char last-command-char))
256 (defun xscheme-enter-debugger-mode (prompt-string)
258 (set-buffer (xscheme-process-buffer))
259 (if (not (eq major-mode 'scheme-debugger-mode))
261 (if (not (eq major-mode 'scheme-interaction-mode))
262 (scheme-interaction-mode))
263 (scheme-debugger-mode-initialize)))))
265 (defun xscheme-debugger-mode-p ()
266 (let ((buffer (xscheme-process-buffer)))
270 (eq major-mode 'scheme-debugger-mode)))))
272 ;;;; Evaluation Commands
274 (defun xscheme-send-string (&rest strings)
275 "Send the string arguments to the Scheme process.
276 The 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? ")
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)
288 (error "No sends allowed while Scheme running"))
289 (t (xscheme-send-string-1 strings))))
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))))
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)))))
303 (defun xscheme-yank-previous-send ()
304 "Insert the most recent expression at point."
307 (insert xscheme-previous-send))
309 (defun xscheme-select-process-buffer ()
310 "Select the Scheme process buffer and move to its output point."
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)))
316 (select-window window)
317 (switch-to-buffer buffer))
318 (goto-char (process-mark process))))))
320 (defun xscheme-send-region (start end)
321 "Send the current region to the Scheme process.
322 The region is sent terminated by a newline."
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)))
329 (defun xscheme-send-definition ()
330 "Send the current definition to the Scheme process.
331 If the current line begins with a non-whitespace character,
332 parse an expression from the beginning of the line and send that instead."
334 (let ((start nil) (end nil))
338 (if (re-search-backward "^\\s(" nil t)
340 (error "Can't find definition")))
341 (xscheme-send-region start end)))
343 (defun xscheme-send-next-expression ()
344 "Send the expression to the right of `point' to the Scheme process."
346 (let ((start (point)))
347 (xscheme-send-region start (save-excursion (forward-sexp) (point)))))
349 (defun xscheme-send-previous-expression ()
350 "Send the expression to the left of `point' to the Scheme process."
353 (xscheme-send-region (save-excursion (backward-sexp) (point)) end)))
355 (defun xscheme-send-current-line ()
356 "Send the current line to the Scheme process.
357 Useful for working with debugging Scheme under adb."
362 (let ((start (point)))
364 (buffer-substring start (point))))))
367 (xscheme-send-string-2 line)))
369 (defun xscheme-send-buffer ()
370 "Send the current buffer to the Scheme process."
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)))
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)))
383 (defun xscheme-send-breakpoint-interrupt ()
384 "Cause the Scheme process to enter a breakpoint."
386 (xscheme-send-interrupt ?b nil))
388 (defun xscheme-send-proceed ()
389 "Cause the Scheme process to proceed from a breakpoint."
391 (send-string "scheme" "(proceed)\n"))
393 (defun xscheme-send-control-g-interrupt ()
394 "Cause the Scheme processor to halt and flush input.
395 Control returns to the top level rep loop."
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..."))
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))))))
408 (defun xscheme-send-control-u-interrupt ()
409 "Cause the Scheme process to halt, returning to previous rep loop."
411 (xscheme-send-interrupt ?u t))
413 (defun xscheme-send-control-x-interrupt ()
414 "Cause the Scheme process to halt, returning to current rep loop."
416 (xscheme-send-interrupt ?x t))
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.
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))))
431 ;;;; Internal Variables
433 (defvar xscheme-process-command-line nil
434 "Command used to start the most recent Scheme process.")
436 (defvar xscheme-previous-send ""
437 "Most recent expression transmitted to the Scheme process.")
439 (defvar xscheme-process-filter-state 'idle
440 "State of scheme process escape reader state machine:
441 idle waiting for an escape sequence
442 reading-type received an altmode but nothing else
443 reading-string reading prompt string")
445 (defvar xscheme-running-p nil
446 "This variable, if nil, indicates that the scheme process is
447 waiting for input. Otherwise, it is busy evaluating something.")
449 (defconst xscheme-control-g-synchronization-p t
450 "If non-nil, insert markers in the scheme input stream to indicate when
451 control-g interrupts were signalled. Do not allow more control-g's to be
452 signalled until the scheme process acknowledges receipt.")
454 (defvar xscheme-control-g-disabled-p nil
455 "This variable, if non-nil, indicates that a control-g is being processed
456 by the scheme process, so additional control-g's are to be ignored.")
458 (defvar xscheme-allow-output-p t
459 "This variable, if nil, prevents output from the scheme process
460 from being inserted into the process-buffer.")
462 (defvar xscheme-prompt ""
463 "The current scheme prompt string.")
465 (defvar xscheme-string-accumulator ""
466 "Accumulator for the string being received from the scheme process.")
468 (defvar xscheme-string-receiver nil
469 "Procedure to send the string argument from the scheme process.")
471 (defvar xscheme-start-hook nil
472 "If non-nil, a procedure to call when the Scheme process is started.
473 When called, the current buffer will be the Scheme process-buffer.")
475 (defvar xscheme-runlight-string nil)
476 (defvar xscheme-mode-string nil)
477 (defvar xscheme-filter-input nil)
479 ;;;; Basic Process Control
481 (defun xscheme-start-process (command-line)
482 (let ((buffer (get-buffer-create "*scheme*")))
483 (let ((process (get-buffer-process 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)
492 (insert-before-markers
493 (substitute-command-keys xscheme-startup-message)))
495 (let ((process-connection-type nil))
496 (apply 'start-process
499 (xscheme-parse-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)))))
509 (defun xscheme-parse-command-line (string)
510 (setq string (substitute-in-file-name string))
514 (let ((index (string-match "[ \t]" string start)))
518 (cons (substring string start)
522 (string-match "[^ \t]" string start))
525 (cons (substring string start index)
530 (defun xscheme-wait-for-process ()
532 (while xscheme-running-p
535 (defun xscheme-process-running-p ()
536 "True iff there is a Scheme process whose status is `run'."
537 (let ((process (get-process "scheme")))
539 (eq (process-status process) 'run))))
541 (defun xscheme-process-buffer ()
542 (let ((process (get-process "scheme")))
543 (and process (process-buffer process))))
545 (defun xscheme-process-buffer-window ()
546 (let ((buffer (xscheme-process-buffer)))
547 (and buffer (get-buffer-window buffer))))
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)))
555 (defun xscheme-process-sentinel (proc reason)
556 (xscheme-process-filter-initialize (eq reason 'run))
558 (xscheme-modeline-initialize)
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)
566 "The Scheme process has died! Do M-x reset-scheme to restart it"))))
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)))
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)))
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)))
599 (funcall (nth 2 entry) (nth 1 entry))
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)))
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))
614 (setq xscheme-string-accumulator
615 (concat xscheme-string-accumulator
616 xscheme-filter-input))
617 (setq xscheme-filter-input nil)))))
619 (error "Scheme process filter -- bad state"))))))
621 ;;;; Process Filter Output
623 (defun xscheme-process-filter-output (&rest args)
624 (if xscheme-allow-output-p
625 (let ((string (apply 'concat args)))
627 (xscheme-goto-output-point)
628 (while (string-match "\\(\007\\|\f\\)" string)
629 (let ((start (match-beginning 0))
631 (insert-before-markers (substring string 0 start))
632 (if (= ?\f (aref string start))
635 (insert-before-markers ?\n))
636 (insert-before-markers ?\f))
638 (setq string (substring string (1+ start)))))
639 (insert-before-markers string)))))
641 (defun xscheme-guarantee-newlines (n)
642 (if xscheme-allow-output-p
644 (xscheme-goto-output-point)
646 (while (and (not stop)
652 (xscheme-goto-output-point)
654 (insert-before-markers ?\n)
657 (defun xscheme-goto-output-point ()
658 (let ((process (get-process "scheme")))
659 (set-buffer (process-buffer process))
660 (goto-char (process-mark process))))
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)))
667 (defun xscheme-set-runlight (runlight)
668 (setq xscheme-runlight-string runlight)
669 (xscheme-modeline-redisplay))
671 (defun xscheme-modeline-redisplay ()
672 (save-excursion (set-buffer (other-buffer)))
673 (set-buffer-modified-p (buffer-modified-p))
676 ;;;; Process Filter Operations
678 (defvar xscheme-process-filter-alist
679 '((?D xscheme-enter-debugger-mode
680 xscheme-process-filter:string-action)
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)
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)
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)
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.
716 Value is a list of entries, each entry is a list of three items.
718 The first item is the character that the process filter dispatches on.
719 The second item is the action to be taken, a function.
720 The third item is the handler for the entry, a function.
722 When the process filter sees a command whose character matches a
723 particular entry, it calls the handler with two arguments: the action
724 and the string containing the rest of the process filter's input
725 stream. It is the responsibility of the handler to invoke the action
726 with the appropriate arguments, and to reenter the process filter with
727 the remaining input.")
729 (defun xscheme-process-filter:simple-action (action)
730 (setq xscheme-process-filter-state 'idle)
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))
738 (defconst xscheme-runlight:running "run"
739 "The character displayed when the Scheme process is running.")
741 (defconst xscheme-runlight:input "input"
742 "The character displayed when the Scheme process is waiting for input.")
744 (defconst xscheme-runlight:gc "gc"
745 "The character displayed when the Scheme process is garbage collecting.")
747 (defun xscheme-start-gc ()
748 (xscheme-set-runlight xscheme-runlight:gc))
750 (defun xscheme-finish-gc ()
751 (xscheme-set-runlight
752 (if xscheme-running-p xscheme-runlight:running xscheme-runlight:input)))
754 (defun xscheme-enter-input-wait ()
755 (xscheme-set-runlight xscheme-runlight:input)
756 (setq xscheme-running-p nil))
758 (defun xscheme-exit-input-wait ()
759 (xscheme-set-runlight xscheme-runlight:running)
760 (setq xscheme-running-p t))
762 (defun xscheme-enable-control-g ()
763 (setq xscheme-control-g-disabled-p nil))
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)))))
774 (defun xscheme-unsolicited-read-char ()
777 (defun xscheme-eval (string)
778 (eval (car (read-from-string string))))
780 (defun xscheme-message (string)
781 (if (not (zerop (length string)))
782 (xscheme-write-message-1 string (format ";%s" string))))
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))))
789 (defun xscheme-write-message-1 (message-string output-string)
790 (let* ((process (get-process "scheme"))
791 (window (get-buffer-window (process-buffer process))))
793 (not (pos-visible-in-window-p (process-mark process)
795 (message "%s" message-string)))
796 (xscheme-guarantee-newlines 1)
797 (xscheme-process-filter-output output-string))
799 (defun xscheme-set-prompt-variable (string)
800 (setq xscheme-prompt string))
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))
808 (defun xscheme-output-goto ()
809 (xscheme-goto-output-point)
810 (xscheme-guarantee-newlines 2))
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)))
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.")
832 (defun xscheme-cd (directory-string)
834 (set-buffer (xscheme-process-buffer))
835 (cd directory-string)))
837 (defun xscheme-prompt-for-confirmation (prompt-string)
838 (xscheme-send-char (if (y-or-n-p prompt-string) ?y ?n)))
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)))
844 (defvar xscheme-prompt-for-expression-map nil)
845 (if (not xscheme-prompt-for-expression-map)
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)))
853 (defun xscheme-prompt-for-expression-exit ()
855 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one)
857 (error "input must be a single, complete expression")))
859 (defun xscheme-region-expression-p (start end)
861 (let ((old-syntax-table (syntax-table)))
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)))))