1 ;;; xscheme.el --- run MIT Scheme under Emacs
3 ;; Copyright (C) 1986, 1987, 1989, 1990 Free Software Foundation, Inc.
6 ;; Keywords: languages, lisp
8 ;; This file is part of GNU Emacs.
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)
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.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; A major mode for editing Scheme and interacting with MIT's C-Scheme.
29 ;; Requires C-Scheme release 5 or later
30 ;; Changes to Control-G handler require runtime version 13.85 or later
37 "Major mode for editing Scheme and interacting with MIT's C-Scheme."
40 (defcustom scheme-band-name nil
41 "*Band loaded by the `run-scheme' command."
42 :type
'(choice (const nil
) string
)
45 (defcustom scheme-program-arguments nil
46 "*Arguments passed to the Scheme program by the `run-scheme' command."
47 :type
'(choice (const nil
) string
)
50 (defcustom xscheme-allow-pipelined-evaluation t
51 "If non-nil, an expression may be transmitted while another is evaluating.
52 Otherwise, attempting to evaluate an expression before the previous expression
53 has finished evaluating will signal an error."
57 (defcustom xscheme-startup-message
58 "This is the Scheme process buffer.
59 Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point.
60 Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
61 Type \\[describe-mode] for more information.
64 "String to insert into Scheme process buffer first time it is started.
65 Is processed with `substitute-command-keys' first."
69 (defcustom xscheme-signal-death-message nil
70 "If non-nil, causes a message to be generated when the Scheme process dies."
74 (defun xscheme-evaluation-commands (keymap)
75 (define-key keymap
"\e\C-x" 'xscheme-send-definition
)
76 (define-key keymap
"\C-x\C-e" 'advertised-xscheme-send-previous-expression
)
77 (define-key keymap
"\eo" 'xscheme-send-buffer
)
78 (define-key keymap
"\ez" 'xscheme-send-definition
)
79 (define-key keymap
"\e\C-m" 'xscheme-send-previous-expression
)
80 (define-key keymap
"\e\C-z" 'xscheme-send-region
))
82 (defun xscheme-interrupt-commands (keymap)
83 (define-key keymap
"\C-c\C-s" 'xscheme-select-process-buffer
)
84 (define-key keymap
"\C-c\C-b" 'xscheme-send-breakpoint-interrupt
)
85 (define-key keymap
"\C-c\C-c" 'xscheme-send-control-g-interrupt
)
86 (define-key keymap
"\C-c\C-u" 'xscheme-send-control-u-interrupt
)
87 (define-key keymap
"\C-c\C-x" 'xscheme-send-control-x-interrupt
))
89 (xscheme-evaluation-commands scheme-mode-map
)
90 (xscheme-interrupt-commands scheme-mode-map
)
92 (defun run-scheme (command-line)
93 "Run MIT Scheme in an inferior process.
94 Output goes to the buffer `*scheme*'.
95 With argument, asks for a command line."
98 (or xscheme-process-command-line
99 (xscheme-default-command-line))))
100 (if current-prefix-arg
101 (read-string "Run Scheme: " default
)
103 (setq xscheme-process-command-line command-line
)
104 (pop-to-buffer (xscheme-start-process command-line
)))
106 (defun reset-scheme ()
107 "Reset the Scheme process."
109 (let ((process (get-process "scheme")))
110 (cond ((or (not process
)
111 (not (eq (process-status process
) 'run
))
113 "The Scheme process is running, are you SURE you want to reset it? "))
114 (message "Resetting Scheme process...")
115 (if process
(kill-process process t
))
116 (xscheme-start-process xscheme-process-command-line
)
117 (message "Resetting Scheme process...done")))))
119 (defun xscheme-default-command-line ()
120 (concat scheme-program-name
" -emacs"
121 (if scheme-program-arguments
122 (concat " " scheme-program-arguments
)
125 (concat " -band " scheme-band-name
)
128 ;;;; Interaction Mode
130 (defun scheme-interaction-mode ()
131 "Major mode for interacting with the inferior Scheme process.
132 Like scheme-mode except that:
134 \\[advertised-xscheme-send-previous-expression] sends the expression before point to the Scheme process as input
135 \\[xscheme-yank-previous-send] yanks the expression most recently sent to Scheme
137 All output from the Scheme process is written in the Scheme process
138 buffer, which is initially named \"*scheme*\". The result of
139 evaluating a Scheme expression is also printed in the process buffer,
140 preceded by the string \";Value: \" to highlight it. If the process
141 buffer is not visible at that time, the value will also be displayed
142 in the minibuffer. If an error occurs, the process buffer will
143 automatically pop up to show you the error message.
145 While the Scheme process is running, the modelines of all buffers in
146 scheme-mode are modified to show the state of the process. The
147 possible states and their meanings are:
149 input waiting for input
151 gc garbage collecting
153 The process buffer's modeline contains additional information where
154 the buffer's name is normally displayed: the command interpreter level
157 Scheme maintains a stack of command interpreters. Every time an error
158 or breakpoint occurs, the current command interpreter is pushed on the
159 command interpreter stack, and a new command interpreter is started.
160 One example of why this is done is so that an error that occurs while
161 you are debugging another error will not destroy the state of the
162 initial error, allowing you to return to it after the second error has
165 The command interpreter level indicates how many interpreters are in
166 the command interpreter stack. It is initially set to one, and it is
167 incremented every time that stack is pushed, and decremented every
168 time it is popped. The following commands are useful for manipulating
169 the command interpreter stack:
171 \\[xscheme-send-breakpoint-interrupt] pushes the stack once
172 \\[xscheme-send-control-u-interrupt] pops the stack once
173 \\[xscheme-send-control-g-interrupt] pops everything off
174 \\[xscheme-send-control-x-interrupt] aborts evaluation, doesn't affect stack
176 Some possible command interpreter types and their meanings are:
178 [Evaluator] read-eval-print loop for evaluating expressions
179 [Debugger] single character commands for debugging errors
180 [Where] single character commands for examining environments
182 Starting with release 6.2 of Scheme, the latter two types of command
183 interpreters will change the major mode of the Scheme process buffer
184 to scheme-debugger-mode , in which the evaluation commands are
185 disabled, and the keys which normally self insert instead send
186 themselves to the Scheme process. The command character ? will list
187 the available commands.
189 For older releases of Scheme, the major mode will be be
190 scheme-interaction-mode , and the command characters must be sent as
191 if they were expressions.
194 Delete converts tabs to spaces as it moves back.
195 Blank lines separate paragraphs. Semicolons start comments.
196 \\{scheme-interaction-mode-map}
198 Entry to this mode calls the value of scheme-interaction-mode-hook
199 with no args, if that value is non-nil.
200 Likewise with the value of scheme-mode-hook.
201 scheme-interaction-mode-hook is called after scheme-mode-hook."
203 (kill-all-local-variables)
204 (scheme-interaction-mode-initialize)
205 (scheme-mode-variables)
206 (make-local-variable 'xscheme-previous-send
)
207 (run-hooks 'scheme-mode-hook
'scheme-interaction-mode-hook
))
209 (defun scheme-interaction-mode-initialize ()
210 (use-local-map scheme-interaction-mode-map
)
211 (setq major-mode
'scheme-interaction-mode
)
212 (setq mode-name
"Scheme Interaction"))
214 (defun scheme-interaction-mode-commands (keymap)
215 (define-key keymap
"\C-c\C-m" 'xscheme-send-current-line
)
216 (define-key keymap
"\C-c\C-p" 'xscheme-send-proceed
)
217 (define-key keymap
"\C-c\C-y" 'xscheme-yank-previous-send
))
219 (defvar scheme-interaction-mode-map nil
)
220 (if (not scheme-interaction-mode-map
)
222 (setq scheme-interaction-mode-map
(make-keymap))
223 (scheme-mode-commands scheme-interaction-mode-map
)
224 (xscheme-interrupt-commands scheme-interaction-mode-map
)
225 (xscheme-evaluation-commands scheme-interaction-mode-map
)
226 (scheme-interaction-mode-commands scheme-interaction-mode-map
)))
228 (defun xscheme-enter-interaction-mode ()
230 (set-buffer (xscheme-process-buffer))
231 (if (not (eq major-mode
'scheme-interaction-mode
))
232 (if (eq major-mode
'scheme-debugger-mode
)
233 (scheme-interaction-mode-initialize)
234 (scheme-interaction-mode)))))
236 (fset 'advertised-xscheme-send-previous-expression
237 'xscheme-send-previous-expression
)
241 (defun scheme-debugger-mode ()
242 "Major mode for executing the Scheme debugger.
243 Like scheme-mode except that the evaluation commands
244 are disabled, and characters that would normally be self inserting are
245 sent to the Scheme process instead. Typing ? will show you which
246 characters perform useful functions.
249 \\{scheme-debugger-mode-map}"
250 (error "Illegal entry to scheme-debugger-mode"))
252 (defun scheme-debugger-mode-initialize ()
253 (use-local-map scheme-debugger-mode-map
)
254 (setq major-mode
'scheme-debugger-mode
)
255 (setq mode-name
"Scheme Debugger"))
257 (defun scheme-debugger-mode-commands (keymap)
260 (define-key keymap
(char-to-string char
) 'scheme-debugger-self-insert
)
261 (setq char
(1+ char
)))))
263 (defvar scheme-debugger-mode-map nil
)
264 (if (not scheme-debugger-mode-map
)
266 (setq scheme-debugger-mode-map
(make-keymap))
267 (scheme-mode-commands scheme-debugger-mode-map
)
268 (xscheme-interrupt-commands scheme-debugger-mode-map
)
269 (scheme-debugger-mode-commands scheme-debugger-mode-map
)))
271 (defun scheme-debugger-self-insert ()
272 "Transmit this character to the Scheme process."
274 (xscheme-send-char last-command-char
))
276 (defun xscheme-enter-debugger-mode (prompt-string)
278 (set-buffer (xscheme-process-buffer))
279 (if (not (eq major-mode
'scheme-debugger-mode
))
281 (if (not (eq major-mode
'scheme-interaction-mode
))
282 (scheme-interaction-mode))
283 (scheme-debugger-mode-initialize)))))
285 (defun xscheme-debugger-mode-p ()
286 (let ((buffer (xscheme-process-buffer)))
290 (eq major-mode
'scheme-debugger-mode
)))))
292 ;;;; Evaluation Commands
294 (defun xscheme-send-string (&rest strings
)
295 "Send the string arguments to the Scheme process.
296 The strings are concatenated and terminated by a newline."
297 (cond ((not (xscheme-process-running-p))
298 (if (yes-or-no-p "The Scheme process has died. Reset it? ")
301 (xscheme-wait-for-process)
302 (goto-char (point-max))
303 (apply 'insert-before-markers strings
)
304 (xscheme-send-string-1 strings
))))
305 ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode"))
306 ((and (not xscheme-allow-pipelined-evaluation
)
308 (error "No sends allowed while Scheme running"))
309 (t (xscheme-send-string-1 strings
))))
311 (defun xscheme-send-string-1 (strings)
312 (let ((string (apply 'concat strings
)))
313 (xscheme-send-string-2 string
)
314 (if (eq major-mode
'scheme-interaction-mode
)
315 (setq xscheme-previous-send string
))))
317 (defun xscheme-send-string-2 (string)
318 (let ((process (get-process "scheme")))
319 (send-string process
(concat string
"\n"))
320 (if (xscheme-process-buffer-current-p)
321 (set-marker (process-mark process
) (point)))))
323 (defun xscheme-yank-previous-send ()
324 "Insert the most recent expression at point."
327 (insert xscheme-previous-send
))
329 (defun xscheme-select-process-buffer ()
330 "Select the Scheme process buffer and move to its output point."
332 (let ((process (or (get-process "scheme") (error "No scheme process"))))
333 (let ((buffer (or (process-buffer process
) (error "No process buffer"))))
334 (let ((window (get-buffer-window buffer
)))
336 (select-window window
)
337 (switch-to-buffer buffer
))
338 (goto-char (process-mark process
))))))
340 (defun xscheme-send-region (start end
)
341 "Send the current region to the Scheme process.
342 The region is sent terminated by a newline."
344 (if (xscheme-process-buffer-current-p)
345 (progn (goto-char end
)
346 (set-marker (process-mark (get-process "scheme")) end
)))
347 (xscheme-send-string (buffer-substring start end
)))
349 (defun xscheme-send-definition ()
350 "Send the current definition to the Scheme process.
351 If the current line begins with a non-whitespace character,
352 parse an expression from the beginning of the line and send that instead."
354 (let ((start nil
) (end nil
))
358 (if (re-search-backward "^\\s(" nil t
)
360 (error "Can't find definition")))
361 (xscheme-send-region start end
)))
363 (defun xscheme-send-next-expression ()
364 "Send the expression to the right of `point' to the Scheme process."
366 (let ((start (point)))
367 (xscheme-send-region start
(save-excursion (forward-sexp) (point)))))
369 (defun xscheme-send-previous-expression ()
370 "Send the expression to the left of `point' to the Scheme process."
373 (xscheme-send-region (save-excursion (backward-sexp) (point)) end
)))
375 (defun xscheme-send-current-line ()
376 "Send the current line to the Scheme process.
377 Useful for working with debugging Scheme under adb."
382 (let ((start (point)))
384 (buffer-substring start
(point))))))
387 (xscheme-send-string-2 line
)))
389 (defun xscheme-send-buffer ()
390 "Send the current buffer to the Scheme process."
392 (if (xscheme-process-buffer-current-p)
393 (error "Not allowed to send this buffer's contents to Scheme"))
394 (xscheme-send-region (point-min) (point-max)))
396 (defun xscheme-send-char (char)
397 "Prompt for a character and send it to the Scheme process."
398 (interactive "cCharacter to send: ")
399 (send-string "scheme" (char-to-string char
)))
403 (defun xscheme-send-breakpoint-interrupt ()
404 "Cause the Scheme process to enter a breakpoint."
406 (xscheme-send-interrupt ?b nil
))
408 (defun xscheme-send-proceed ()
409 "Cause the Scheme process to proceed from a breakpoint."
411 (send-string "scheme" "(proceed)\n"))
413 (defun xscheme-send-control-g-interrupt ()
414 "Cause the Scheme processor to halt and flush input.
415 Control returns to the top level rep loop."
417 (let ((inhibit-quit t
))
418 (cond ((not xscheme-control-g-synchronization-p
)
419 (interrupt-process "scheme"))
420 (xscheme-control-g-disabled-p
421 (message "Relax..."))
423 (setq xscheme-control-g-disabled-p t
)
424 (message "Sending C-G interrupt to Scheme...")
425 (interrupt-process "scheme")
426 (send-string "scheme" (char-to-string 0))))))
428 (defun xscheme-send-control-u-interrupt ()
429 "Cause the Scheme process to halt, returning to previous rep loop."
431 (xscheme-send-interrupt ?u t
))
433 (defun xscheme-send-control-x-interrupt ()
434 "Cause the Scheme process to halt, returning to current rep loop."
436 (xscheme-send-interrupt ?x t
))
438 ;;; This doesn't really work right -- Scheme just gobbles the first
439 ;;; character in the input. There is no way for us to guarantee that
440 ;;; the argument to this procedure is the first char unless we put
441 ;;; some kind of marker in the input stream.
443 (defun xscheme-send-interrupt (char mark-p
)
444 "Send a ^A type interrupt to the Scheme process."
445 (interactive "cInterrupt character to send: ")
446 (quit-process "scheme")
447 (send-string "scheme" (char-to-string char
))
448 (if (and mark-p xscheme-control-g-synchronization-p
)
449 (send-string "scheme" (char-to-string 0))))
451 ;;;; Internal Variables
453 (defvar xscheme-process-command-line nil
454 "Command used to start the most recent Scheme process.")
456 (defvar xscheme-previous-send
""
457 "Most recent expression transmitted to the Scheme process.")
459 (defvar xscheme-process-filter-state
'idle
460 "State of scheme process escape reader state machine:
461 idle waiting for an escape sequence
462 reading-type received an altmode but nothing else
463 reading-string reading prompt string")
465 (defvar xscheme-running-p nil
466 "This variable, if nil, indicates that the scheme process is
467 waiting for input. Otherwise, it is busy evaluating something.")
469 (defconst xscheme-control-g-synchronization-p t
470 "If non-nil, insert markers in the scheme input stream to indicate when
471 control-g interrupts were signaled. Do not allow more control-g's to be
472 signaled until the scheme process acknowledges receipt.")
474 (defvar xscheme-control-g-disabled-p nil
475 "This variable, if non-nil, indicates that a control-g is being processed
476 by the scheme process, so additional control-g's are to be ignored.")
478 (defvar xscheme-allow-output-p t
479 "This variable, if nil, prevents output from the scheme process
480 from being inserted into the process-buffer.")
482 (defvar xscheme-prompt
""
483 "The current scheme prompt string.")
485 (defvar xscheme-string-accumulator
""
486 "Accumulator for the string being received from the scheme process.")
488 (defvar xscheme-string-receiver nil
489 "Procedure to send the string argument from the scheme process.")
491 (defcustom xscheme-start-hook nil
492 "If non-nil, a procedure to call when the Scheme process is started.
493 When called, the current buffer will be the Scheme process-buffer."
497 (defvar xscheme-runlight-string nil
)
498 (defvar xscheme-mode-string nil
)
499 (defvar xscheme-filter-input nil
)
501 ;;;; Basic Process Control
503 (defun xscheme-start-process (command-line)
504 (let ((buffer (get-buffer-create "*scheme*")))
505 (let ((process (get-buffer-process buffer
)))
508 (if (and process
(memq (process-status process
) '(run stop
)))
509 (set-marker (process-mark process
) (point-max))
510 (progn (if process
(delete-process process
))
511 (goto-char (point-max))
512 (scheme-interaction-mode)
514 (insert-before-markers
515 (substitute-command-keys xscheme-startup-message
)))
517 (let ((process-connection-type nil
))
518 (apply 'start-process
521 (xscheme-parse-command-line
523 (set-marker (process-mark process
) (point-max))
524 (xscheme-process-filter-initialize t
)
525 (xscheme-modeline-initialize)
526 (set-process-sentinel process
'xscheme-process-sentinel
)
527 (set-process-filter process
'xscheme-process-filter
)
528 (run-hooks 'xscheme-start-hook
)))))
531 (defun xscheme-parse-command-line (string)
532 (setq string
(substitute-in-file-name string
))
536 (let ((index (string-match "[ \t]" string start
)))
540 (cons (substring string start
)
544 (string-match "[^ \t]" string start
))
547 (cons (substring string start index
)
552 (defun xscheme-wait-for-process ()
554 (while xscheme-running-p
557 (defun xscheme-process-running-p ()
558 "True iff there is a Scheme process whose status is `run'."
559 (let ((process (get-process "scheme")))
561 (eq (process-status process
) 'run
))))
563 (defun xscheme-process-buffer ()
564 (let ((process (get-process "scheme")))
565 (and process
(process-buffer process
))))
567 (defun xscheme-process-buffer-window ()
568 (let ((buffer (xscheme-process-buffer)))
569 (and buffer
(get-buffer-window buffer
))))
571 (defun xscheme-process-buffer-current-p ()
572 "True iff the current buffer is the Scheme process buffer."
573 (eq (xscheme-process-buffer) (current-buffer)))
577 (defun xscheme-process-sentinel (proc reason
)
578 (xscheme-process-filter-initialize (eq reason
'run
))
580 (xscheme-modeline-initialize)
582 (setq scheme-mode-line-process
"")
583 (setq xscheme-mode-string
"no process")))
584 (if (and (not (memq reason
'(run stop
)))
585 xscheme-signal-death-message
)
588 "The Scheme process has died! Do M-x reset-scheme to restart it"))))
590 (defun xscheme-process-filter-initialize (running-p)
591 (setq xscheme-process-filter-state
'idle
)
592 (setq xscheme-running-p running-p
)
593 (setq xscheme-control-g-disabled-p nil
)
594 (setq xscheme-allow-output-p t
)
595 (setq xscheme-prompt
"")
596 (setq scheme-mode-line-process
'(": " xscheme-runlight-string
)))
598 (defun xscheme-process-filter (proc string
)
599 (let ((xscheme-filter-input string
))
600 (while xscheme-filter-input
601 (cond ((eq xscheme-process-filter-state
'idle
)
602 (let ((start (string-match "\e" xscheme-filter-input
)))
605 (xscheme-process-filter-output
606 (substring xscheme-filter-input
0 start
))
607 (setq xscheme-filter-input
608 (substring xscheme-filter-input
(1+ start
)))
609 (setq xscheme-process-filter-state
'reading-type
))
610 (let ((string xscheme-filter-input
))
611 (setq xscheme-filter-input nil
)
612 (xscheme-process-filter-output string
)))))
613 ((eq xscheme-process-filter-state
'reading-type
)
614 (if (zerop (length xscheme-filter-input
))
615 (setq xscheme-filter-input nil
)
616 (let ((char (aref xscheme-filter-input
0)))
617 (setq xscheme-filter-input
618 (substring xscheme-filter-input
1))
619 (let ((entry (assoc char xscheme-process-filter-alist
)))
621 (funcall (nth 2 entry
) (nth 1 entry
))
623 (xscheme-process-filter-output ?\e char
)
624 (setq xscheme-process-filter-state
'idle
)))))))
625 ((eq xscheme-process-filter-state
'reading-string
)
626 (let ((start (string-match "\e" xscheme-filter-input
)))
629 (concat xscheme-string-accumulator
630 (substring xscheme-filter-input
0 start
))))
631 (setq xscheme-filter-input
632 (substring xscheme-filter-input
(1+ start
)))
633 (setq xscheme-process-filter-state
'idle
)
634 (funcall xscheme-string-receiver string
))
636 (setq xscheme-string-accumulator
637 (concat xscheme-string-accumulator
638 xscheme-filter-input
))
639 (setq xscheme-filter-input nil
)))))
641 (error "Scheme process filter -- bad state"))))))
643 ;;;; Process Filter Output
645 (defun xscheme-process-filter-output (&rest args
)
646 (if xscheme-allow-output-p
647 (let ((string (apply 'concat args
)))
649 (xscheme-goto-output-point)
650 (while (string-match "\\(\007\\|\f\\)" string
)
651 (let ((start (match-beginning 0))
653 (insert-before-markers (substring string
0 start
))
654 (if (= ?
\f (aref string start
))
657 (insert-before-markers ?
\n))
658 (insert-before-markers ?
\f))
660 (setq string
(substring string
(1+ start
)))))
661 (insert-before-markers string
)))))
663 (defun xscheme-guarantee-newlines (n)
664 (if xscheme-allow-output-p
666 (xscheme-goto-output-point)
668 (while (and (not stop
)
674 (xscheme-goto-output-point)
676 (insert-before-markers ?
\n)
679 (defun xscheme-goto-output-point ()
680 (let ((process (get-process "scheme")))
681 (set-buffer (process-buffer process
))
682 (goto-char (process-mark process
))))
684 (defun xscheme-modeline-initialize ()
685 (setq xscheme-runlight-string
"")
686 (setq xscheme-mode-string
"")
687 (setq mode-line-buffer-identification
'("Scheme: " xscheme-mode-string
)))
689 (defun xscheme-set-runlight (runlight)
690 (setq xscheme-runlight-string runlight
)
691 (force-mode-line-update t
))
693 ;;;; Process Filter Operations
695 (defvar xscheme-process-filter-alist
696 '((?D xscheme-enter-debugger-mode
697 xscheme-process-filter
:string-action
)
699 xscheme-process-filter
:string-action
)
700 (?P xscheme-set-prompt-variable
701 xscheme-process-filter
:string-action
)
702 (?R xscheme-enter-interaction-mode
703 xscheme-process-filter
:simple-action
)
705 xscheme-process-filter
:simple-action
)
706 (?e xscheme-finish-gc
707 xscheme-process-filter
:simple-action
)
708 (?f xscheme-exit-input-wait
709 xscheme-process-filter
:simple-action
)
710 (?g xscheme-enable-control-g
711 xscheme-process-filter
:simple-action
)
712 (?i xscheme-prompt-for-expression
713 xscheme-process-filter
:string-action
)
715 xscheme-process-filter
:string-action
)
716 (?n xscheme-prompt-for-confirmation
717 xscheme-process-filter
:string-action
)
718 (?o xscheme-output-goto
719 xscheme-process-filter
:simple-action
)
720 (?p xscheme-set-prompt
721 xscheme-process-filter
:string-action
)
722 (?s xscheme-enter-input-wait
723 xscheme-process-filter
:simple-action
)
724 (?v xscheme-write-value
725 xscheme-process-filter
:string-action
)
727 xscheme-process-filter
:string-action
)
728 (?z xscheme-display-process-buffer
729 xscheme-process-filter
:simple-action
)
730 (?c xscheme-unsolicited-read-char
731 xscheme-process-filter
:simple-action
))
732 "Table used to decide how to handle process filter commands.
733 Value is a list of entries, each entry is a list of three items.
735 The first item is the character that the process filter dispatches on.
736 The second item is the action to be taken, a function.
737 The third item is the handler for the entry, a function.
739 When the process filter sees a command whose character matches a
740 particular entry, it calls the handler with two arguments: the action
741 and the string containing the rest of the process filter's input
742 stream. It is the responsibility of the handler to invoke the action
743 with the appropriate arguments, and to reenter the process filter with
744 the remaining input.")
746 (defun xscheme-process-filter:simple-action
(action)
747 (setq xscheme-process-filter-state
'idle
)
750 (defun xscheme-process-filter:string-action
(action)
751 (setq xscheme-string-receiver action
)
752 (setq xscheme-string-accumulator
"")
753 (setq xscheme-process-filter-state
'reading-string
))
755 (defconst xscheme-runlight
:running
"run"
756 "The character displayed when the Scheme process is running.")
758 (defconst xscheme-runlight
:input
"input"
759 "The character displayed when the Scheme process is waiting for input.")
761 (defconst xscheme-runlight
:gc
"gc"
762 "The character displayed when the Scheme process is garbage collecting.")
764 (defun xscheme-start-gc ()
765 (xscheme-set-runlight xscheme-runlight
:gc
))
767 (defun xscheme-finish-gc ()
768 (xscheme-set-runlight
769 (if xscheme-running-p xscheme-runlight
:running xscheme-runlight
:input
)))
771 (defun xscheme-enter-input-wait ()
772 (xscheme-set-runlight xscheme-runlight
:input
)
773 (setq xscheme-running-p nil
))
775 (defun xscheme-exit-input-wait ()
776 (xscheme-set-runlight xscheme-runlight
:running
)
777 (setq xscheme-running-p t
))
779 (defun xscheme-enable-control-g ()
780 (setq xscheme-control-g-disabled-p nil
))
782 (defun xscheme-display-process-buffer ()
783 (let ((window (or (xscheme-process-buffer-window)
784 (display-buffer (xscheme-process-buffer)))))
785 (save-window-excursion
786 (select-window window
)
787 (xscheme-goto-output-point)
788 (if (xscheme-debugger-mode-p)
789 (xscheme-enter-interaction-mode)))))
791 (defun xscheme-unsolicited-read-char ()
794 (defun xscheme-eval (string)
795 (eval (car (read-from-string string
))))
797 (defun xscheme-message (string)
798 (if (not (zerop (length string
)))
799 (xscheme-write-message-1 string
(format ";%s" string
))))
801 (defun xscheme-write-value (string)
802 (if (zerop (length string
))
803 (xscheme-write-message-1 "(no value)" ";No value")
804 (xscheme-write-message-1 string
(format ";Value: %s" string
))))
806 (defun xscheme-write-message-1 (message-string output-string
)
807 (let* ((process (get-process "scheme"))
808 (window (get-buffer-window (process-buffer process
))))
810 (not (pos-visible-in-window-p (process-mark process
)
812 (message "%s" message-string
)))
813 (xscheme-guarantee-newlines 1)
814 (xscheme-process-filter-output output-string
))
816 (defun xscheme-set-prompt-variable (string)
817 (setq xscheme-prompt string
))
819 (defun xscheme-set-prompt (string)
820 (setq xscheme-prompt string
)
821 (xscheme-guarantee-newlines 2)
822 (setq xscheme-mode-string
(xscheme-coerce-prompt string
))
823 (force-mode-line-update t
))
825 (defun xscheme-output-goto ()
826 (xscheme-goto-output-point)
827 (xscheme-guarantee-newlines 2))
829 (defun xscheme-coerce-prompt (string)
830 (if (string-match "^[0-9]+ " string
)
831 (let ((end (match-end 0)))
832 (concat (substring string
0 end
)
833 (let ((prompt (substring string end
)))
834 (let ((entry (assoc prompt xscheme-prompt-alist
)))
840 (defvar xscheme-prompt-alist
841 '(("[Normal REPL]" .
"[Evaluator]")
842 ("[Error REPL]" .
"[Evaluator]")
843 ("[Breakpoint REPL]" .
"[Evaluator]")
844 ("[Debugger REPL]" .
"[Evaluator]")
845 ("[Visiting environment]" .
"[Evaluator]")
846 ("[Environment Inspector]" .
"[Where]"))
847 "An alist which maps the Scheme command interpreter type to a print string.")
849 (defun xscheme-cd (directory-string)
851 (set-buffer (xscheme-process-buffer))
852 (cd directory-string
)))
854 (defun xscheme-prompt-for-confirmation (prompt-string)
855 (xscheme-send-char (if (y-or-n-p prompt-string
) ?y ?n
)))
857 (defun xscheme-prompt-for-expression (prompt-string)
858 (xscheme-send-string-2
859 (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map
)))
861 (defvar xscheme-prompt-for-expression-map nil
)
862 (if (not xscheme-prompt-for-expression-map
)
864 (setq xscheme-prompt-for-expression-map
865 (copy-keymap minibuffer-local-map
))
866 (substitute-key-definition 'exit-minibuffer
867 'xscheme-prompt-for-expression-exit
868 xscheme-prompt-for-expression-map
)))
870 (defun xscheme-prompt-for-expression-exit ()
872 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one
)
874 (error "input must be a single, complete expression")))
876 (defun xscheme-region-expression-p (start end
)
878 (let ((old-syntax-table (syntax-table)))
881 (set-syntax-table scheme-mode-syntax-table
)
882 (let ((state (parse-partial-sexp start end
)))
883 (and (zerop (car state
)) ;depth = 0
884 (nth 2 state
) ;last-sexp exists, i.e. >= 1 sexps
885 (let ((state (parse-partial-sexp start
(nth 2 state
))))
886 (if (nth 2 state
) 'many
'one
)))))
887 (set-syntax-table old-syntax-table
)))))
891 ;;; xscheme.el ends here