1 ;;; xscheme.el --- run MIT Scheme under Emacs
3 ;; Copyright (C) 1986-1987, 1989-1990, 2001-2011 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; A major mode for interacting with MIT Scheme.
27 ;; Requires MIT Scheme release 5 or later.
28 ;; Changes to Control-G handler require runtime version 13.85 or later.
34 ;;;; Internal Variables
36 (defvar xscheme-previous-mode)
37 (defvar xscheme-previous-process-state)
38 (defvar xscheme-last-input-end)
40 (defvar xscheme-process-command-line nil
41 "Command used to start the most recent Scheme process.")
43 (defvar xscheme-process-name "scheme"
44 "Name of xscheme process that we're currently interacting with.")
46 (defvar xscheme-buffer-name "*scheme*"
47 "Name of xscheme buffer that we're currently interacting with.")
49 (defvar xscheme-expressions-ring-max 30
50 "*Maximum length of Scheme expressions ring.")
52 (defvar xscheme-expressions-ring nil
53 "List of expressions recently transmitted to the Scheme process.")
55 (defvar xscheme-expressions-ring-yank-pointer nil
56 "The tail of the Scheme expressions ring whose car is the last thing yanked.")
58 (defvar xscheme-running-p nil
59 "This variable, if nil, indicates that the scheme process is
60 waiting for input. Otherwise, it is busy evaluating something.")
62 (defconst xscheme-control-g-synchronization-p t
63 "If non-nil, insert markers in the scheme input stream to indicate when
64 control-g interrupts were signaled. Do not allow more control-g's to be
65 signaled until the scheme process acknowledges receipt.")
67 (defvar xscheme-control-g-disabled-p nil
68 "This variable, if non-nil, indicates that a control-g is being processed
69 by the scheme process, so additional control-g's are to be ignored.")
71 (defvar xscheme-string-receiver nil
72 "Procedure to send the string argument from the scheme process.")
74 (defconst default-xscheme-runlight
75 '(": " xscheme-runlight-string)
76 "Default global (shared) xscheme-runlight modeline format.")
78 (defvar xscheme-runlight "")
79 (defvar xscheme-runlight-string nil)
81 (defvar xscheme-process-filter-state 'idle
82 "State of scheme process escape reader state machine:
83 idle waiting for an escape sequence
84 reading-type received an altmode but nothing else
85 reading-string reading prompt string")
87 (defvar xscheme-allow-output-p t
88 "This variable, if nil, prevents output from the scheme process
89 from being inserted into the process-buffer.")
91 (defvar xscheme-prompt ""
92 "The current scheme prompt string.")
94 (defvar xscheme-string-accumulator ""
95 "Accumulator for the string being received from the scheme process.")
97 (defvar xscheme-mode-string nil)
98 (setq-default scheme-mode-line-process
99 '("" xscheme-runlight))
101 (mapc 'make-variable-buffer-local
102 '(xscheme-expressions-ring
103 xscheme-expressions-ring-yank-pointer
104 xscheme-process-filter-state
106 xscheme-control-g-disabled-p
107 xscheme-allow-output-p
109 xscheme-string-accumulator
111 scheme-mode-line-process))
113 (defgroup xscheme nil
114 "Major mode for editing Scheme and interacting with MIT's C-Scheme."
117 (defcustom scheme-band-name nil
118 "*Band loaded by the `run-scheme' command."
119 :type '(choice (const nil) string)
122 (defcustom scheme-program-arguments nil
123 "*Arguments passed to the Scheme program by the `run-scheme' command."
124 :type '(choice (const nil) string)
127 (defcustom xscheme-allow-pipelined-evaluation t
128 "If non-nil, an expression may be transmitted while another is evaluating.
129 Otherwise, attempting to evaluate an expression before the previous expression
130 has finished evaluating will signal an error."
134 (defcustom xscheme-startup-message
135 "This is the Scheme process buffer.
136 Type \\[xscheme-send-previous-expression] to evaluate the expression before point.
137 Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
138 Type \\[describe-mode] for more information.
141 "String to insert into Scheme process buffer first time it is started.
142 Is processed with `substitute-command-keys' first."
146 (defcustom xscheme-signal-death-message nil
147 "If non-nil, causes a message to be generated when the Scheme process dies."
151 (defcustom xscheme-start-hook nil
152 "If non-nil, a procedure to call when the Scheme process is started.
153 When called, the current buffer will be the Scheme process-buffer."
158 (defun xscheme-evaluation-commands (keymap)
159 (define-key keymap "\e\C-x" 'xscheme-send-definition)
160 (define-key keymap "\C-x\C-e" 'xscheme-send-previous-expression)
161 (put 'xscheme-send-previous-expression :advertised-binding "\C-x\C-e")
162 (define-key keymap "\eo" 'xscheme-send-buffer)
163 (define-key keymap "\ez" 'xscheme-send-definition)
164 (define-key keymap "\e\C-m" 'xscheme-send-previous-expression)
165 (define-key keymap "\e\C-z" 'xscheme-send-region))
167 (defun xscheme-interrupt-commands (keymap)
168 (define-key keymap "\C-c\C-s" 'xscheme-select-process-buffer)
169 (define-key keymap "\C-c\C-b" 'xscheme-send-breakpoint-interrupt)
170 (define-key keymap "\C-c\C-c" 'xscheme-send-control-g-interrupt)
171 (define-key keymap "\C-c\C-u" 'xscheme-send-control-u-interrupt)
172 (define-key keymap "\C-c\C-x" 'xscheme-send-control-x-interrupt))
174 (xscheme-evaluation-commands scheme-mode-map)
175 (xscheme-interrupt-commands scheme-mode-map)
177 (defun run-scheme (command-line)
178 "Run MIT Scheme in an inferior process.
179 Output goes to the buffer `*scheme*'.
180 With argument, asks for a command line."
181 (interactive (list (xscheme-read-command-line current-prefix-arg)))
182 (xscheme-start command-line xscheme-process-name xscheme-buffer-name))
184 (defun xscheme-start (command-line process-name buffer-name)
185 (setq-default xscheme-process-command-line command-line)
187 (xscheme-start-process command-line process-name buffer-name))
188 (set (make-local-variable 'xscheme-process-command-line) command-line))
190 (defun xscheme-read-command-line (arg)
192 (or xscheme-process-command-line
193 (xscheme-default-command-line))))
195 (read-string "Run Scheme: " default)
198 (defun xscheme-default-command-line ()
199 (concat scheme-program-name " -emacs"
200 (if scheme-program-arguments
201 (concat " " scheme-program-arguments)
204 (concat " -band " scheme-band-name)
207 (defun reset-scheme ()
208 "Reset the Scheme process."
210 (let ((process (get-process xscheme-process-name)))
211 (cond ((or (not process)
212 (not (eq (process-status process) 'run))
214 "The Scheme process is running, are you SURE you want to reset it? "))
215 (message "Resetting Scheme process...")
218 (kill-process process t)
219 (delete-process process)))
220 (xscheme-start-process xscheme-process-command-line
223 (message "Resetting Scheme process...done")))))
225 ;;;; Multiple Scheme buffer management commands
227 (defun start-scheme (buffer-name &optional globally)
228 "Choose a scheme interaction buffer, or create a new one."
229 ;; (interactive "BScheme interaction buffer: \nP")
231 (list (read-buffer "Scheme interaction buffer: "
235 (let ((buffer (get-buffer-create buffer-name)))
236 (let ((process (get-buffer-process buffer)))
238 (switch-to-buffer buffer)
239 (if (or (not (buffer-file-name buffer))
240 (yes-or-no-p (concat "Buffer "
243 (buffer-file-name buffer)
244 "; start scheme in it? ")))
246 (xscheme-start (xscheme-read-command-line t)
250 (global-set-scheme-interaction-buffer buffer-name)))
251 (message "start-scheme aborted"))))))
253 (fset 'select-scheme 'start-scheme)
255 (defun global-set-scheme-interaction-buffer (buffer-name)
256 "Set the default scheme interaction buffer."
258 (list (read-buffer "Scheme interaction buffer: "
261 (let ((process-name (verify-xscheme-buffer buffer-name nil)))
262 (setq-default xscheme-buffer-name buffer-name)
263 (setq-default xscheme-process-name process-name)
264 (setq-default xscheme-runlight-string
265 (with-current-buffer buffer-name
266 xscheme-runlight-string))
267 (setq-default xscheme-runlight
268 (if (eq (process-status process-name) 'run)
269 default-xscheme-runlight
272 (defun local-set-scheme-interaction-buffer (buffer-name)
273 "Set the scheme interaction buffer for the current buffer."
275 (list (read-buffer "Scheme interaction buffer: "
278 (let ((process-name (verify-xscheme-buffer buffer-name t)))
279 (set (make-local-variable 'xscheme-buffer-name) buffer-name)
280 (set (make-local-variable 'xscheme-process-name) process-name)
281 (set (make-local-variable 'xscheme-runlight)
282 (with-current-buffer buffer-name
285 (defun local-clear-scheme-interaction-buffer ()
286 "Make the current buffer use the default scheme interaction buffer."
288 (if (xscheme-process-buffer-current-p)
289 (error "Cannot change the interaction buffer of an interaction buffer"))
290 (kill-local-variable 'xscheme-buffer-name)
291 (kill-local-variable 'xscheme-process-name)
292 (kill-local-variable 'xscheme-runlight))
294 (defun verify-xscheme-buffer (buffer-name localp)
295 (if (and localp (xscheme-process-buffer-current-p))
296 (error "Cannot change the interaction buffer of an interaction buffer"))
297 (let* ((buffer (get-buffer buffer-name))
298 (process (and buffer (get-buffer-process buffer))))
300 (error "Buffer `%s' does not exist" buffer-name))
302 (error "Buffer `%s' is not a scheme interaction buffer" buffer-name))
304 (with-current-buffer buffer
305 (if (not (xscheme-process-buffer-current-p))
306 (error "Buffer `%s' is not a scheme interaction buffer"
308 (process-name process)))))
310 ;;;; Interaction Mode
312 (defun scheme-interaction-mode (&optional preserve)
313 "Major mode for interacting with an inferior MIT Scheme process.
314 Like scheme-mode except that:
316 \\[xscheme-send-previous-expression] sends the expression before point to the Scheme process as input
317 \\[xscheme-yank-pop] yanks an expression previously sent to Scheme
318 \\[xscheme-yank-push] yanks an expression more recently sent to Scheme
320 All output from the Scheme process is written in the Scheme process
321 buffer, which is initially named \"*scheme*\". The result of
322 evaluating a Scheme expression is also printed in the process buffer,
323 preceded by the string \";Value: \" to highlight it. If the process
324 buffer is not visible at that time, the value will also be displayed
325 in the minibuffer. If an error occurs, the process buffer will
326 automatically pop up to show you the error message.
328 While the Scheme process is running, the modelines of all buffers in
329 scheme-mode are modified to show the state of the process. The
330 possible states and their meanings are:
332 input waiting for input
334 gc garbage collecting
336 The process buffer's modeline contains additional information where
337 the buffer's name is normally displayed: the command interpreter level
340 Scheme maintains a stack of command interpreters. Every time an error
341 or breakpoint occurs, the current command interpreter is pushed on the
342 command interpreter stack, and a new command interpreter is started.
343 One example of why this is done is so that an error that occurs while
344 you are debugging another error will not destroy the state of the
345 initial error, allowing you to return to it after the second error has
348 The command interpreter level indicates how many interpreters are in
349 the command interpreter stack. It is initially set to one, and it is
350 incremented every time that stack is pushed, and decremented every
351 time it is popped. The following commands are useful for manipulating
352 the command interpreter stack:
354 \\[xscheme-send-breakpoint-interrupt] pushes the stack once
355 \\[xscheme-send-control-u-interrupt] pops the stack once
356 \\[xscheme-send-control-g-interrupt] pops everything off
357 \\[xscheme-send-control-x-interrupt] aborts evaluation, doesn't affect stack
359 Some possible command interpreter types and their meanings are:
361 \[Evaluator] read-eval-print loop for evaluating expressions
362 \[Debugger] single character commands for debugging errors
363 \[Where] single character commands for examining environments
365 Starting with release 6.2 of Scheme, the latter two types of command
366 interpreters will change the major mode of the Scheme process buffer
367 to scheme-debugger-mode , in which the evaluation commands are
368 disabled, and the keys which normally self insert instead send
369 themselves to the Scheme process. The command character ? will list
370 the available commands.
372 For older releases of Scheme, the major mode will be be
373 scheme-interaction-mode , and the command characters must be sent as
374 if they were expressions.
377 Delete converts tabs to spaces as it moves back.
378 Blank lines separate paragraphs. Semicolons start comments.
379 \\{scheme-interaction-mode-map}
381 Entry to this mode calls the value of scheme-interaction-mode-hook
382 with no args, if that value is non-nil.
383 Likewise with the value of scheme-mode-hook.
384 scheme-interaction-mode-hook is called after scheme-mode-hook."
385 ;; FIXME: Use define-derived-mode.
388 (let ((previous-mode major-mode))
389 (kill-all-local-variables)
390 (make-local-variable 'xscheme-process-name)
391 (make-local-variable 'xscheme-previous-process-state)
392 (make-local-variable 'xscheme-runlight-string)
393 (make-local-variable 'xscheme-runlight)
394 (set (make-local-variable 'xscheme-previous-mode) previous-mode)
395 (let ((buffer (current-buffer)))
396 (set (make-local-variable 'xscheme-buffer-name) (buffer-name buffer))
397 (set (make-local-variable 'xscheme-last-input-end) (make-marker))
398 (let ((process (get-buffer-process buffer)))
401 (setq xscheme-process-name (process-name process))
402 (setq xscheme-previous-process-state
403 (cons (process-filter process)
404 (process-sentinel process)))
405 (xscheme-process-filter-initialize t)
406 (xscheme-modeline-initialize xscheme-buffer-name)
407 (set-process-sentinel process 'xscheme-process-sentinel)
408 (set-process-filter process 'xscheme-process-filter))
409 (setq xscheme-previous-process-state (cons nil nil)))))))
410 (scheme-interaction-mode-initialize)
411 (scheme-mode-variables)
412 (run-mode-hooks 'scheme-mode-hook 'scheme-interaction-mode-hook))
414 (defun exit-scheme-interaction-mode ()
415 "Take buffer out of scheme interaction mode"
417 (if (not (derived-mode-p 'scheme-interaction-mode))
418 (error "Buffer not in scheme interaction mode"))
419 (let ((previous-state xscheme-previous-process-state))
420 (funcall xscheme-previous-mode)
421 (let ((process (get-buffer-process (current-buffer))))
424 (if (eq (process-filter process) 'xscheme-process-filter)
425 (set-process-filter process (car previous-state)))
426 (if (eq (process-sentinel process) 'xscheme-process-sentinel)
427 (set-process-sentinel process (cdr previous-state))))))))
429 (defvar scheme-interaction-mode-commands-alist nil)
430 (defvar scheme-interaction-mode-map nil)
432 (defun scheme-interaction-mode-initialize ()
433 (use-local-map scheme-interaction-mode-map)
434 (setq major-mode 'scheme-interaction-mode) ;FIXME: Use define-derived-mode.
435 (setq mode-name "Scheme Interaction"))
437 (defun scheme-interaction-mode-commands (keymap)
438 (let ((entries scheme-interaction-mode-commands-alist))
442 (car (cdr (car entries))))
443 (setq entries (cdr entries)))))
445 ;; Initialize the command alist
446 (setq scheme-interaction-mode-commands-alist
447 (append scheme-interaction-mode-commands-alist
448 '(("\C-c\C-m" xscheme-send-current-line)
449 ("\C-c\C-o" xscheme-delete-output)
450 ("\C-c\C-p" xscheme-send-proceed)
451 ("\C-c\C-y" xscheme-yank)
452 ("\ep" xscheme-yank-pop)
453 ("\en" xscheme-yank-push))))
455 ;; Initialize the mode map
456 (if (not scheme-interaction-mode-map)
458 (setq scheme-interaction-mode-map (make-keymap))
459 (scheme-mode-commands scheme-interaction-mode-map)
460 (xscheme-interrupt-commands scheme-interaction-mode-map)
461 (xscheme-evaluation-commands scheme-interaction-mode-map)
462 (scheme-interaction-mode-commands scheme-interaction-mode-map)))
464 (defun xscheme-enter-interaction-mode ()
465 (with-current-buffer (xscheme-process-buffer)
466 (if (not (derived-mode-p 'scheme-interaction-mode))
467 (if (derived-mode-p 'scheme-debugger-mode)
468 (scheme-interaction-mode-initialize)
469 (scheme-interaction-mode t)))))
471 (define-obsolete-function-alias 'advertised-xscheme-send-previous-expression
472 'xscheme-send-previous-expression "23.2")
476 (defun scheme-debugger-mode ()
477 "Major mode for executing the Scheme debugger.
478 Like scheme-mode except that the evaluation commands
479 are disabled, and characters that would normally be self inserting are
480 sent to the Scheme process instead. Typing ? will show you which
481 characters perform useful functions.
484 \\{scheme-debugger-mode-map}"
485 (error "Invalid entry to scheme-debugger-mode"))
487 (defvar scheme-debugger-mode-map nil)
489 (defun scheme-debugger-mode-initialize ()
490 (use-local-map scheme-debugger-mode-map)
491 (setq major-mode 'scheme-debugger-mode) ;FIXME: Use define-derived-mode.
492 (setq mode-name "Scheme Debugger"))
494 (defun scheme-debugger-mode-commands (keymap)
497 (define-key keymap (char-to-string char) 'scheme-debugger-self-insert)
498 (setq char (1+ char)))))
500 ;; Initialize the debugger mode map
501 (if (not scheme-debugger-mode-map)
503 (setq scheme-debugger-mode-map (make-keymap))
504 (scheme-mode-commands scheme-debugger-mode-map)
505 (xscheme-interrupt-commands scheme-debugger-mode-map)
506 (scheme-debugger-mode-commands scheme-debugger-mode-map)))
508 (defun scheme-debugger-self-insert ()
509 "Transmit this character to the Scheme process."
511 (xscheme-send-char last-command-event))
513 (defun xscheme-enter-debugger-mode (prompt-string)
514 (with-current-buffer (xscheme-process-buffer)
515 (if (not (derived-mode-p 'scheme-debugger-mode))
517 (if (not (derived-mode-p 'scheme-interaction-mode))
518 (scheme-interaction-mode t))
519 (scheme-debugger-mode-initialize)))))
521 (defun xscheme-debugger-mode-p ()
522 (let ((buffer (xscheme-process-buffer)))
524 (with-current-buffer buffer
525 (derived-mode-p 'scheme-debugger-mode)))))
527 ;;;; Evaluation Commands
529 (defun xscheme-send-string (&rest strings)
530 "Send the string arguments to the Scheme process.
531 The strings are concatenated and terminated by a newline."
532 (cond ((not (xscheme-process-running-p))
533 (if (yes-or-no-p "The Scheme process has died. Reset it? ")
536 (xscheme-wait-for-process)
537 (xscheme-send-string-1 strings))))
538 ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode"))
539 ((and (not xscheme-allow-pipelined-evaluation)
541 (error "No sends allowed while Scheme running"))
542 (t (xscheme-send-string-1 strings))))
544 (defun xscheme-send-string-1 (strings)
545 (let ((string (apply 'concat strings)))
546 (xscheme-send-string-2 string)
547 (if (derived-mode-p 'scheme-interaction-mode)
548 (xscheme-insert-expression string))))
550 (defun xscheme-send-string-2 (string)
551 (let ((process (get-process xscheme-process-name)))
552 (process-send-string process (concat string "\n"))
553 (if (xscheme-process-buffer-current-p)
554 (set-marker (process-mark process) (point)))))
556 (defun xscheme-select-process-buffer ()
557 "Select the Scheme process buffer and move to its output point."
560 (or (get-process xscheme-process-name)
561 (error "No scheme process"))))
562 (let ((buffer (or (process-buffer process) (error "No process buffer"))))
563 (let ((window (get-buffer-window buffer)))
565 (select-window window)
566 (switch-to-buffer buffer))
567 (goto-char (process-mark process))))))
569 ;;;; Scheme expressions ring
571 (defun xscheme-insert-expression (string)
572 (setq xscheme-expressions-ring-yank-pointer
573 (add-to-history 'xscheme-expressions-ring string
574 xscheme-expressions-ring-max)))
576 (defun xscheme-rotate-yank-pointer (arg)
577 "Rotate the yanking point in the kill ring."
579 (let ((length (length xscheme-expressions-ring)))
581 (error "Scheme expression ring is empty")
582 (setq xscheme-expressions-ring-yank-pointer
586 (length xscheme-expressions-ring-yank-pointer)))
588 (nthcdr (if (< index 0)
591 xscheme-expressions-ring))))))
593 (defun xscheme-yank (&optional arg)
594 "Insert the most recent expression at point.
595 With just C-U as argument, same but put point in front (and mark at end).
596 With argument n, reinsert the nth most recently sent expression.
597 See also the commands \\[xscheme-yank-pop] and \\[xscheme-yank-push]."
599 (xscheme-rotate-yank-pointer (if (listp arg) 0
603 (insert (car xscheme-expressions-ring-yank-pointer))
605 (exchange-point-and-mark)))
607 ;; Old name, to avoid errors in users' init files.
608 (fset 'xscheme-yank-previous-send
611 (defun xscheme-yank-pop (arg)
612 "Insert or replace a just-yanked expression with an older expression.
613 If the previous command was not a yank, it yanks.
614 Otherwise, the region contains a stretch of reinserted
615 expression. yank-pop deletes that text and inserts in its
616 place a different expression.
618 With no argument, the next older expression is inserted.
619 With argument n, the n'th older expression is inserted.
620 If n is negative, this is a more recent expression.
622 The sequence of expressions wraps around, so that after the oldest one
623 comes the newest one."
625 (setq this-command 'xscheme-yank)
626 (if (not (eq last-command 'xscheme-yank))
629 (setq arg (- arg 1))))
631 (let ((before (< (point) (mark))))
632 (delete-region (point) (mark))
633 (xscheme-rotate-yank-pointer arg)
635 (insert (car xscheme-expressions-ring-yank-pointer))
636 (if before (exchange-point-and-mark)))))
638 (defun xscheme-yank-push (arg)
639 "Insert or replace a just-yanked expression with a more recent expression.
640 If the previous command was not a yank, it yanks.
641 Otherwise, the region contains a stretch of reinserted
642 expression. yank-pop deletes that text and inserts in its
643 place a different expression.
645 With no argument, the next more recent expression is inserted.
646 With argument n, the n'th more recent expression is inserted.
647 If n is negative, a less recent expression is used.
649 The sequence of expressions wraps around, so that after the oldest one
650 comes the newest one."
652 (xscheme-yank-pop (- 0 arg)))
654 (defun xscheme-send-region (start end)
655 "Send the current region to the Scheme process.
656 The region is sent terminated by a newline."
658 (if (xscheme-process-buffer-current-p)
662 (insert-before-markers ?\n))
663 (set-marker (process-mark (get-process xscheme-process-name))
665 (set-marker xscheme-last-input-end (point))))
666 (xscheme-send-string (buffer-substring start end)))
668 (defun xscheme-send-definition ()
669 "Send the current definition to the Scheme process.
670 If the current line begins with a non-whitespace character,
671 parse an expression from the beginning of the line and send that instead."
673 (let ((start nil) (end nil))
677 (if (re-search-backward "^\\s(" nil t)
679 (error "Can't find definition")))
680 (xscheme-send-region start end)))
682 (defun xscheme-send-next-expression ()
683 "Send the expression to the right of `point' to the Scheme process."
685 (let ((start (point)))
686 (xscheme-send-region start (save-excursion (forward-sexp) (point)))))
688 (defun xscheme-send-previous-expression ()
689 "Send the expression to the left of `point' to the Scheme process."
692 (xscheme-send-region (save-excursion (backward-sexp) (point)) end)))
694 (defun xscheme-send-current-line ()
695 "Send the current line to the Scheme process.
696 Useful for working with debugging Scheme under adb."
698 (let ((line (buffer-substring (line-beginning-position) (line-end-position))))
701 (xscheme-send-string-2 line)))
703 (defun xscheme-send-buffer ()
704 "Send the current buffer to the Scheme process."
706 (if (xscheme-process-buffer-current-p)
707 (error "Not allowed to send this buffer's contents to Scheme"))
708 (xscheme-send-region (point-min) (point-max)))
710 (defun xscheme-send-char (char)
711 "Prompt for a character and send it to the Scheme process."
712 (interactive "cCharacter to send: ")
713 (process-send-string xscheme-process-name (char-to-string char)))
715 (defun xscheme-delete-output ()
716 "Delete all output from interpreter since last input."
718 (let ((proc (get-buffer-process (current-buffer))))
720 (goto-char (process-mark proc))
722 "^;\\(Unspecified return value$\\|Value\\( [0-9]+\\)?: \\|\\(Abort\\|Up\\|Quit\\)!$\\)"
723 xscheme-last-input-end
726 (if (< (marker-position xscheme-last-input-end) (point))
728 (delete-region xscheme-last-input-end (point))
729 (insert-before-markers "*** output flushed ***\n"))))))
733 (defun xscheme-send-breakpoint-interrupt ()
734 "Cause the Scheme process to enter a breakpoint."
736 (xscheme-send-interrupt ?b nil))
738 (defun xscheme-send-proceed ()
739 "Cause the Scheme process to proceed from a breakpoint."
741 (process-send-string xscheme-process-name "(proceed)\n"))
743 (defconst xscheme-control-g-message-string
744 "Sending C-G interrupt to Scheme...")
746 (defun xscheme-send-control-g-interrupt ()
747 "Cause the Scheme processor to halt and flush input.
748 Control returns to the top level rep loop."
750 (let ((inhibit-quit t))
751 (cond ((not xscheme-control-g-synchronization-p)
752 (interrupt-process xscheme-process-name))
753 ((with-current-buffer xscheme-buffer-name
754 xscheme-control-g-disabled-p)
755 (message "Relax..."))
757 (with-current-buffer xscheme-buffer-name
758 (setq xscheme-control-g-disabled-p t))
759 (message xscheme-control-g-message-string)
760 (interrupt-process xscheme-process-name)
762 (xscheme-send-char 0)))))
764 (defun xscheme-send-control-u-interrupt ()
765 "Cause the Scheme process to halt, returning to previous rep loop."
767 (xscheme-send-interrupt ?u t))
769 (defun xscheme-send-control-x-interrupt ()
770 "Cause the Scheme process to halt, returning to current rep loop."
772 (xscheme-send-interrupt ?x t))
774 ;;; This doesn't really work right -- Scheme just gobbles the first
775 ;;; character in the input. There is no way for us to guarantee that
776 ;;; the argument to this procedure is the first char unless we put
777 ;;; some kind of marker in the input stream.
779 (defun xscheme-send-interrupt (char mark-p)
780 "Send a ^A type interrupt to the Scheme process."
781 (interactive "cInterrupt character to send: ")
782 (quit-process xscheme-process-name)
784 (xscheme-send-char char)
785 (if (and mark-p xscheme-control-g-synchronization-p)
786 (xscheme-send-char 0)))
788 ;;;; Basic Process Control
790 (defun xscheme-start-process (command-line the-process the-buffer)
791 (let ((buffer (get-buffer-create the-buffer)))
792 (let ((process (get-buffer-process buffer)))
793 (with-current-buffer buffer
794 (if (and process (memq (process-status process) '(run stop)))
795 (set-marker (process-mark process) (point-max))
796 (progn (if process (delete-process process))
797 (goto-char (point-max))
798 (scheme-interaction-mode nil)
799 (setq xscheme-process-name the-process)
801 (insert-before-markers
802 (substitute-command-keys xscheme-startup-message)))
804 (let ((process-connection-type nil))
805 (apply 'start-process
808 (xscheme-parse-command-line
810 (if (not (equal (process-name process) the-process))
811 (setq xscheme-process-name (process-name process)))
812 (if (not (equal (buffer-name buffer) the-buffer))
813 (setq xscheme-buffer-name (buffer-name buffer)))
814 (message "Starting process %s in buffer %s"
817 (set-marker (process-mark process) (point-max))
818 (xscheme-process-filter-initialize t)
819 (xscheme-modeline-initialize xscheme-buffer-name)
820 (set-process-sentinel process 'xscheme-process-sentinel)
821 (set-process-filter process 'xscheme-process-filter)
822 (run-hooks 'xscheme-start-hook)))))
825 (defun xscheme-parse-command-line (string)
826 (setq string (substitute-in-file-name string))
830 (let ((index (string-match "[ \t]" string start)))
834 (cons (substring string start)
838 (string-match "[^ \t]" string start))
841 (cons (substring string start index)
846 (defun xscheme-wait-for-process ()
848 (while xscheme-running-p
851 (defun xscheme-process-running-p ()
852 "True if there is a Scheme process whose status is `run'."
853 (let ((process (get-process xscheme-process-name)))
855 (eq (process-status process) 'run))))
857 (defun xscheme-process-buffer ()
858 (let ((process (get-process xscheme-process-name)))
859 (and process (process-buffer process))))
861 (defun xscheme-process-buffer-window ()
862 (let ((buffer (xscheme-process-buffer)))
863 (and buffer (get-buffer-window buffer))))
865 (defun xscheme-process-buffer-current-p ()
866 "True if the current buffer is the Scheme process buffer."
867 (eq (xscheme-process-buffer) (current-buffer)))
869 ;;;; Process Filter Operations
871 (defvar xscheme-process-filter-alist
873 xscheme-process-filter:string-action-noexcursion)
874 (?D xscheme-enter-debugger-mode
875 xscheme-process-filter:string-action)
877 xscheme-process-filter:string-action)
878 (?P xscheme-set-prompt-variable
879 xscheme-process-filter:string-action)
880 (?R xscheme-enter-interaction-mode
881 xscheme-process-filter:simple-action)
883 xscheme-process-filter:simple-action)
884 (?c xscheme-unsolicited-read-char
885 xscheme-process-filter:simple-action)
886 (?e xscheme-finish-gc
887 xscheme-process-filter:simple-action)
888 (?f xscheme-exit-input-wait
889 xscheme-process-filter:simple-action)
890 (?g xscheme-enable-control-g
891 xscheme-process-filter:simple-action)
892 (?i xscheme-prompt-for-expression
893 xscheme-process-filter:string-action)
895 xscheme-process-filter:string-action)
896 (?n xscheme-prompt-for-confirmation
897 xscheme-process-filter:string-action)
898 (?o xscheme-output-goto
899 xscheme-process-filter:simple-action)
900 (?p xscheme-set-prompt
901 xscheme-process-filter:string-action)
902 (?s xscheme-enter-input-wait
903 xscheme-process-filter:simple-action)
904 (?v xscheme-write-value
905 xscheme-process-filter:string-action)
907 xscheme-process-filter:string-action)
908 (?z xscheme-display-process-buffer
909 xscheme-process-filter:simple-action))
910 "Table used to decide how to handle process filter commands.
911 Value is a list of entries, each entry is a list of three items.
913 The first item is the character that the process filter dispatches on.
914 The second item is the action to be taken, a function.
915 The third item is the handler for the entry, a function.
917 When the process filter sees a command whose character matches a
918 particular entry, it calls the handler with two arguments: the action
919 and the string containing the rest of the process filter's input
920 stream. It is the responsibility of the handler to invoke the action
921 with the appropriate arguments, and to reenter the process filter with
922 the remaining input.")
926 (defun xscheme-process-sentinel (proc reason)
927 (let* ((buffer (process-buffer proc))
928 (name (buffer-name buffer)))
929 (with-current-buffer buffer
930 (xscheme-process-filter-initialize (eq reason 'run))
931 (if (not (eq reason 'run))
933 (setq scheme-mode-line-process "")
934 (setq xscheme-mode-string "no process")
935 (if (equal name (default-value 'xscheme-buffer-name))
936 (setq-default xscheme-runlight ""))))
937 (if (and (not (memq reason '(run stop)))
938 xscheme-signal-death-message)
942 "The Scheme process has died! Do M-x reset-scheme to restart it"))))))
944 (defun xscheme-process-filter-initialize (running-p)
945 (setq xscheme-process-filter-state 'idle)
946 (setq xscheme-running-p running-p)
947 (setq xscheme-control-g-disabled-p nil)
948 (setq xscheme-allow-output-p t)
949 (setq xscheme-prompt "")
951 (let ((name (buffer-name (current-buffer))))
952 (setq scheme-mode-line-process '(": " xscheme-runlight-string))
953 (xscheme-modeline-initialize name)
954 (if (equal name (default-value 'xscheme-buffer-name))
955 (setq-default xscheme-runlight default-xscheme-runlight))))
956 (if (or (eq xscheme-runlight default-xscheme-runlight)
957 (equal xscheme-runlight ""))
958 (setq xscheme-runlight (list ": " 'xscheme-buffer-name ": " "?")))
959 (rplaca (nthcdr 3 xscheme-runlight)
960 (if running-p "?" "no process")))
962 (defun xscheme-process-filter (proc string)
963 (let ((xscheme-filter-input string)
964 (call-noexcursion nil))
965 (while xscheme-filter-input
966 (setq call-noexcursion nil)
967 (with-current-buffer (process-buffer proc)
968 (cond ((eq xscheme-process-filter-state 'idle)
969 (let ((start (string-match "\e" xscheme-filter-input)))
972 (xscheme-process-filter-output
973 (substring xscheme-filter-input 0 start))
974 (setq xscheme-filter-input
975 (substring xscheme-filter-input (1+ start)))
976 (setq xscheme-process-filter-state 'reading-type))
977 (let ((string xscheme-filter-input))
978 (setq xscheme-filter-input nil)
979 (xscheme-process-filter-output string)))))
980 ((eq xscheme-process-filter-state 'reading-type)
981 (if (zerop (length xscheme-filter-input))
982 (setq xscheme-filter-input nil)
983 (let ((char (aref xscheme-filter-input 0)))
984 (setq xscheme-filter-input
985 (substring xscheme-filter-input 1))
986 (let ((entry (assoc char xscheme-process-filter-alist)))
988 (funcall (nth 2 entry) (nth 1 entry))
990 (xscheme-process-filter-output ?\e char)
991 (setq xscheme-process-filter-state 'idle)))))))
992 ((eq xscheme-process-filter-state 'reading-string)
993 (let ((start (string-match "\e" xscheme-filter-input)))
996 (concat xscheme-string-accumulator
997 (substring xscheme-filter-input 0 start))))
998 (setq xscheme-filter-input
999 (substring xscheme-filter-input (1+ start)))
1000 (setq xscheme-process-filter-state 'idle)
1001 (if (listp xscheme-string-receiver)
1003 (setq xscheme-string-receiver
1004 (car xscheme-string-receiver))
1005 (setq call-noexcursion string))
1006 (funcall xscheme-string-receiver string)))
1008 (setq xscheme-string-accumulator
1009 (concat xscheme-string-accumulator
1010 xscheme-filter-input))
1011 (setq xscheme-filter-input nil)))))
1013 (error "Scheme process filter -- bad state"))))
1014 (if call-noexcursion
1015 (funcall xscheme-string-receiver call-noexcursion)))))
1017 ;;;; Process Filter Output
1019 (defun xscheme-process-filter-output (&rest args)
1020 (if xscheme-allow-output-p
1021 (let ((string (apply 'concat args)))
1023 (xscheme-goto-output-point)
1024 (let ((old-point (point)))
1025 (while (string-match "\\(\007\\|\f\\)" string)
1026 (let ((start (match-beginning 0))
1027 (end (match-end 0)))
1028 (insert-before-markers (substring string 0 start))
1029 (if (= ?\f (aref string start))
1032 (insert-before-markers ?\n))
1033 (insert-before-markers ?\f))
1035 (setq string (substring string (1+ start)))))
1036 (insert-before-markers string)
1037 (if (and xscheme-last-input-end
1038 (equal (marker-position xscheme-last-input-end) (point)))
1039 (set-marker xscheme-last-input-end old-point)))))))
1041 (defun xscheme-guarantee-newlines (n)
1042 (if xscheme-allow-output-p
1044 (xscheme-goto-output-point)
1046 (while (and (not stop)
1052 (xscheme-goto-output-point)
1054 (insert-before-markers ?\n)
1057 (defun xscheme-goto-output-point ()
1058 (let ((process (get-process xscheme-process-name)))
1059 (set-buffer (process-buffer process))
1060 (goto-char (process-mark process))))
1062 (defun xscheme-modeline-initialize (name)
1063 (setq xscheme-runlight-string "")
1064 (if (equal name (default-value 'xscheme-buffer-name))
1065 (setq-default xscheme-runlight-string ""))
1066 (setq xscheme-mode-string "")
1067 (setq mode-line-buffer-identification
1068 (list (concat name ": ")
1069 'xscheme-mode-string)))
1071 (defun xscheme-set-runlight (runlight)
1072 (setq xscheme-runlight-string runlight)
1073 (if (equal (buffer-name (current-buffer))
1074 (default-value 'xscheme-buffer-name))
1075 (setq-default xscheme-runlight-string runlight))
1076 (rplaca (nthcdr 3 xscheme-runlight) runlight)
1077 (force-mode-line-update t))
1079 (defun xscheme-process-filter:simple-action (action)
1080 (setq xscheme-process-filter-state 'idle)
1083 (defun xscheme-process-filter:string-action (action)
1084 (setq xscheme-string-receiver action)
1085 (setq xscheme-string-accumulator "")
1086 (setq xscheme-process-filter-state 'reading-string))
1088 (defun xscheme-process-filter:string-action-noexcursion (action)
1089 (xscheme-process-filter:string-action (cons action nil)))
1091 (defconst xscheme-runlight:running "run"
1092 "The character displayed when the Scheme process is running.")
1094 (defconst xscheme-runlight:input "input"
1095 "The character displayed when the Scheme process is waiting for input.")
1097 (defconst xscheme-runlight:gc "gc"
1098 "The character displayed when the Scheme process is garbage collecting.")
1100 (defun xscheme-start-gc ()
1101 (xscheme-set-runlight xscheme-runlight:gc))
1103 (defun xscheme-finish-gc ()
1104 (xscheme-set-runlight
1105 (if xscheme-running-p xscheme-runlight:running xscheme-runlight:input)))
1107 (defun xscheme-enter-input-wait ()
1108 (xscheme-set-runlight xscheme-runlight:input)
1109 (setq xscheme-control-g-disabled-p nil)
1110 (setq xscheme-running-p nil))
1112 (defun xscheme-exit-input-wait ()
1113 (xscheme-set-runlight xscheme-runlight:running)
1114 (setq xscheme-running-p t))
1116 (defun xscheme-enable-control-g ()
1117 (setq xscheme-control-g-disabled-p nil)
1118 (if (string= (current-message) xscheme-control-g-message-string)
1121 (defun xscheme-display-process-buffer ()
1122 (let ((window (or (xscheme-process-buffer-window)
1123 (display-buffer (xscheme-process-buffer)))))
1124 (save-window-excursion
1125 (select-window window)
1126 (xscheme-goto-output-point)
1127 (if (xscheme-debugger-mode-p)
1128 (xscheme-enter-interaction-mode)))))
1130 (defun xscheme-unsolicited-read-char ()
1133 (defun xscheme-eval (string)
1134 (eval (car (read-from-string string))))
1136 (defun xscheme-message (string)
1137 (if (not (zerop (length string)))
1138 (xscheme-write-message-1 string (format ";%s" string))))
1140 (defun xscheme-write-value (string)
1141 (if (zerop (length string))
1142 (xscheme-write-message-1 "(no value)" ";Unspecified return value")
1143 (xscheme-write-message-1 string (format ";Value: %s" string))))
1145 (defun xscheme-write-message-1 (message-string output-string)
1146 (let* ((process (get-process xscheme-process-name))
1147 (window (get-buffer-window (process-buffer process))))
1148 (if (or (not window)
1149 (not (pos-visible-in-window-p (process-mark process)
1151 (message "%s" message-string)))
1152 (xscheme-guarantee-newlines 1)
1153 (xscheme-process-filter-output output-string))
1155 (defun xscheme-set-prompt-variable (string)
1156 (setq xscheme-prompt string))
1158 (defun xscheme-set-prompt (string)
1159 (setq xscheme-prompt string)
1160 (xscheme-guarantee-newlines 2)
1161 (setq xscheme-mode-string (xscheme-coerce-prompt string))
1162 (force-mode-line-update t))
1164 (defun xscheme-output-goto ()
1165 (xscheme-goto-output-point)
1166 (xscheme-guarantee-newlines 2))
1168 (defun xscheme-coerce-prompt (string)
1169 (if (string-match "^[0-9]+ \\[[^]]+\\] " string)
1170 (let ((end (match-end 0)))
1171 (xscheme-process-filter-output (substring string end))
1172 (substring string 0 (- end 1)))
1175 (defun xscheme-cd (directory-string)
1176 (with-current-buffer (xscheme-process-buffer)
1177 (cd directory-string)))
1179 (defun xscheme-prompt-for-confirmation (prompt-string)
1180 (xscheme-send-char (if (y-or-n-p prompt-string) ?y ?n)))
1182 (defvar xscheme-prompt-for-expression-map nil)
1183 (if (not xscheme-prompt-for-expression-map)
1185 (setq xscheme-prompt-for-expression-map
1186 (copy-keymap minibuffer-local-map))
1187 (substitute-key-definition 'exit-minibuffer
1188 'xscheme-prompt-for-expression-exit
1189 xscheme-prompt-for-expression-map)))
1191 (defun xscheme-prompt-for-expression (prompt-string)
1192 (xscheme-send-string-2
1193 (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map)))
1195 (defun xscheme-prompt-for-expression-exit ()
1197 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one)
1199 (error "input must be a single, complete expression")))
1201 (defun xscheme-region-expression-p (start end)
1203 (let ((old-syntax-table (syntax-table)))
1206 (set-syntax-table scheme-mode-syntax-table)
1207 (let ((state (parse-partial-sexp start end)))
1208 (and (zerop (car state)) ;depth = 0
1209 (nth 2 state) ;last-sexp exists, i.e. >= 1 sexps
1210 (let ((state (parse-partial-sexp start (nth 2 state))))
1211 (if (nth 2 state) 'many 'one)))))
1212 (set-syntax-table old-syntax-table)))))
1216 ;;; xscheme.el ends here