1 ;;; xscheme.el --- run MIT Scheme under Emacs
3 ;; Copyright (C) 1986, 1987, 1989, 1990, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
7 ;; Keywords: languages, lisp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; A major mode for interacting with MIT Scheme.
30 ;; Requires MIT Scheme release 5 or later.
31 ;; Changes to Control-G handler require runtime version 13.85 or later.
37 ;;;; Internal Variables
39 (defvar xscheme-previous-mode
)
40 (defvar xscheme-previous-process-state
)
41 (defvar xscheme-last-input-end
)
43 (defvar xscheme-process-command-line nil
44 "Command used to start the most recent Scheme process.")
46 (defvar xscheme-process-name
"scheme"
47 "Name of xscheme process that we're currently interacting with.")
49 (defvar xscheme-buffer-name
"*scheme*"
50 "Name of xscheme buffer that we're currently interacting with.")
52 (defvar xscheme-expressions-ring-max
30
53 "*Maximum length of Scheme expressions ring.")
55 (defvar xscheme-expressions-ring nil
56 "List of expressions recently transmitted to the Scheme process.")
58 (defvar xscheme-expressions-ring-yank-pointer nil
59 "The tail of the Scheme expressions ring whose car is the last thing yanked.")
61 (defvar xscheme-running-p nil
62 "This variable, if nil, indicates that the scheme process is
63 waiting for input. Otherwise, it is busy evaluating something.")
65 (defconst xscheme-control-g-synchronization-p t
66 "If non-nil, insert markers in the scheme input stream to indicate when
67 control-g interrupts were signaled. Do not allow more control-g's to be
68 signaled until the scheme process acknowledges receipt.")
70 (defvar xscheme-control-g-disabled-p nil
71 "This variable, if non-nil, indicates that a control-g is being processed
72 by the scheme process, so additional control-g's are to be ignored.")
74 (defvar xscheme-string-receiver nil
75 "Procedure to send the string argument from the scheme process.")
77 (defconst default-xscheme-runlight
78 '(": " xscheme-runlight-string
)
79 "Default global (shared) xscheme-runlight modeline format.")
81 (defvar xscheme-runlight
"")
82 (defvar xscheme-runlight-string nil
)
84 (defvar xscheme-process-filter-state
'idle
85 "State of scheme process escape reader state machine:
86 idle waiting for an escape sequence
87 reading-type received an altmode but nothing else
88 reading-string reading prompt string")
90 (defvar xscheme-allow-output-p t
91 "This variable, if nil, prevents output from the scheme process
92 from being inserted into the process-buffer.")
94 (defvar xscheme-prompt
""
95 "The current scheme prompt string.")
97 (defvar xscheme-string-accumulator
""
98 "Accumulator for the string being received from the scheme process.")
100 (defvar xscheme-mode-string nil
)
101 (setq-default scheme-mode-line-process
102 '("" xscheme-runlight
))
104 (mapc 'make-variable-buffer-local
105 '(xscheme-expressions-ring
106 xscheme-expressions-ring-yank-pointer
107 xscheme-process-filter-state
109 xscheme-control-g-disabled-p
110 xscheme-allow-output-p
112 xscheme-string-accumulator
114 scheme-mode-line-process
))
116 (defgroup xscheme nil
117 "Major mode for editing Scheme and interacting with MIT's C-Scheme."
120 (defcustom scheme-band-name nil
121 "*Band loaded by the `run-scheme' command."
122 :type
'(choice (const nil
) string
)
125 (defcustom scheme-program-arguments nil
126 "*Arguments passed to the Scheme program by the `run-scheme' command."
127 :type
'(choice (const nil
) string
)
130 (defcustom xscheme-allow-pipelined-evaluation t
131 "If non-nil, an expression may be transmitted while another is evaluating.
132 Otherwise, attempting to evaluate an expression before the previous expression
133 has finished evaluating will signal an error."
137 (defcustom xscheme-startup-message
138 "This is the Scheme process buffer.
139 Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point.
140 Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
141 Type \\[describe-mode] for more information.
144 "String to insert into Scheme process buffer first time it is started.
145 Is processed with `substitute-command-keys' first."
149 (defcustom xscheme-signal-death-message nil
150 "If non-nil, causes a message to be generated when the Scheme process dies."
154 (defcustom xscheme-start-hook nil
155 "If non-nil, a procedure to call when the Scheme process is started.
156 When called, the current buffer will be the Scheme process-buffer."
161 (defun xscheme-evaluation-commands (keymap)
162 (define-key keymap
"\e\C-x" 'xscheme-send-definition
)
163 (define-key keymap
"\C-x\C-e" 'advertised-xscheme-send-previous-expression
)
164 (define-key keymap
"\eo" 'xscheme-send-buffer
)
165 (define-key keymap
"\ez" 'xscheme-send-definition
)
166 (define-key keymap
"\e\C-m" 'xscheme-send-previous-expression
)
167 (define-key keymap
"\e\C-z" 'xscheme-send-region
))
169 (defun xscheme-interrupt-commands (keymap)
170 (define-key keymap
"\C-c\C-s" 'xscheme-select-process-buffer
)
171 (define-key keymap
"\C-c\C-b" 'xscheme-send-breakpoint-interrupt
)
172 (define-key keymap
"\C-c\C-c" 'xscheme-send-control-g-interrupt
)
173 (define-key keymap
"\C-c\C-u" 'xscheme-send-control-u-interrupt
)
174 (define-key keymap
"\C-c\C-x" 'xscheme-send-control-x-interrupt
))
176 (xscheme-evaluation-commands scheme-mode-map
)
177 (xscheme-interrupt-commands scheme-mode-map
)
179 (defun run-scheme (command-line)
180 "Run MIT Scheme in an inferior process.
181 Output goes to the buffer `*scheme*'.
182 With argument, asks for a command line."
183 (interactive (list (xscheme-read-command-line current-prefix-arg
)))
184 (xscheme-start command-line xscheme-process-name xscheme-buffer-name
))
186 (defun xscheme-start (command-line process-name buffer-name
)
187 (setq-default xscheme-process-command-line command-line
)
189 (xscheme-start-process command-line process-name buffer-name
))
190 (make-local-variable 'xscheme-process-command-line
)
191 (setq xscheme-process-command-line command-line
))
193 (defun xscheme-read-command-line (arg)
195 (or xscheme-process-command-line
196 (xscheme-default-command-line))))
198 (read-string "Run Scheme: " default
)
201 (defun xscheme-default-command-line ()
202 (concat scheme-program-name
" -emacs"
203 (if scheme-program-arguments
204 (concat " " scheme-program-arguments
)
207 (concat " -band " scheme-band-name
)
210 (defun reset-scheme ()
211 "Reset the Scheme process."
213 (let ((process (get-process xscheme-process-name
)))
214 (cond ((or (not process
)
215 (not (eq (process-status process
) 'run
))
217 "The Scheme process is running, are you SURE you want to reset it? "))
218 (message "Resetting Scheme process...")
221 (kill-process process t
)
222 (delete-process process
)))
223 (xscheme-start-process xscheme-process-command-line
226 (message "Resetting Scheme process...done")))))
228 ;;;; Multiple Scheme buffer management commands
230 (defun start-scheme (buffer-name &optional globally
)
231 "Choose a scheme interaction buffer, or create a new one."
232 ;; (interactive "BScheme interaction buffer: \nP")
234 (list (read-buffer "Scheme interaction buffer: "
238 (let ((buffer (get-buffer-create buffer-name
)))
239 (let ((process (get-buffer-process buffer
)))
241 (switch-to-buffer buffer
)
242 (if (or (not (buffer-file-name buffer
))
243 (yes-or-no-p (concat "Buffer "
246 (buffer-file-name buffer
)
247 "; start scheme in it? ")))
249 (xscheme-start (xscheme-read-command-line t
)
253 (global-set-scheme-interaction-buffer buffer-name
)))
254 (message "start-scheme aborted"))))))
256 (fset 'select-scheme
'start-scheme
)
258 (defun global-set-scheme-interaction-buffer (buffer-name)
259 "Set the default scheme interaction buffer."
261 (list (read-buffer "Scheme interaction buffer: "
264 (let ((process-name (verify-xscheme-buffer buffer-name nil
)))
265 (setq-default xscheme-buffer-name buffer-name
)
266 (setq-default xscheme-process-name process-name
)
267 (setq-default xscheme-runlight-string
268 (save-excursion (set-buffer buffer-name
)
269 xscheme-runlight-string
))
270 (setq-default xscheme-runlight
271 (if (eq (process-status process-name
) 'run
)
272 default-xscheme-runlight
275 (defun local-set-scheme-interaction-buffer (buffer-name)
276 "Set the scheme interaction buffer for the current buffer."
278 (list (read-buffer "Scheme interaction buffer: "
281 (let ((process-name (verify-xscheme-buffer buffer-name t
)))
282 (make-local-variable 'xscheme-buffer-name
)
283 (setq xscheme-buffer-name buffer-name
)
284 (make-local-variable 'xscheme-process-name
)
285 (setq xscheme-process-name process-name
)
286 (make-local-variable 'xscheme-runlight
)
287 (setq xscheme-runlight
(save-excursion (set-buffer buffer-name
)
290 (defun local-clear-scheme-interaction-buffer ()
291 "Make the current buffer use the default scheme interaction buffer."
293 (if (xscheme-process-buffer-current-p)
294 (error "Cannot change the interaction buffer of an interaction buffer"))
295 (kill-local-variable 'xscheme-buffer-name
)
296 (kill-local-variable 'xscheme-process-name
)
297 (kill-local-variable 'xscheme-runlight
))
299 (defun verify-xscheme-buffer (buffer-name localp
)
300 (if (and localp
(xscheme-process-buffer-current-p))
301 (error "Cannot change the interaction buffer of an interaction buffer"))
302 (let* ((buffer (get-buffer buffer-name
))
303 (process (and buffer
(get-buffer-process buffer
))))
305 (error "Buffer `%s' does not exist" buffer-name
))
307 (error "Buffer `%s' is not a scheme interaction buffer" buffer-name
))
311 (if (not (xscheme-process-buffer-current-p))
312 (error "Buffer `%s' is not a scheme interaction buffer"
314 (process-name process
)))))
316 ;;;; Interaction Mode
318 (defun scheme-interaction-mode (&optional preserve
)
319 "Major mode for interacting with an inferior MIT Scheme process.
320 Like scheme-mode except that:
322 \\[advertised-xscheme-send-previous-expression] sends the expression before point to the Scheme process as input
323 \\[xscheme-yank-pop] yanks an expression previously sent to Scheme
324 \\[xscheme-yank-push] yanks an expression more recently sent to Scheme
326 All output from the Scheme process is written in the Scheme process
327 buffer, which is initially named \"*scheme*\". The result of
328 evaluating a Scheme expression is also printed in the process buffer,
329 preceded by the string \";Value: \" to highlight it. If the process
330 buffer is not visible at that time, the value will also be displayed
331 in the minibuffer. If an error occurs, the process buffer will
332 automatically pop up to show you the error message.
334 While the Scheme process is running, the modelines of all buffers in
335 scheme-mode are modified to show the state of the process. The
336 possible states and their meanings are:
338 input waiting for input
340 gc garbage collecting
342 The process buffer's modeline contains additional information where
343 the buffer's name is normally displayed: the command interpreter level
346 Scheme maintains a stack of command interpreters. Every time an error
347 or breakpoint occurs, the current command interpreter is pushed on the
348 command interpreter stack, and a new command interpreter is started.
349 One example of why this is done is so that an error that occurs while
350 you are debugging another error will not destroy the state of the
351 initial error, allowing you to return to it after the second error has
354 The command interpreter level indicates how many interpreters are in
355 the command interpreter stack. It is initially set to one, and it is
356 incremented every time that stack is pushed, and decremented every
357 time it is popped. The following commands are useful for manipulating
358 the command interpreter stack:
360 \\[xscheme-send-breakpoint-interrupt] pushes the stack once
361 \\[xscheme-send-control-u-interrupt] pops the stack once
362 \\[xscheme-send-control-g-interrupt] pops everything off
363 \\[xscheme-send-control-x-interrupt] aborts evaluation, doesn't affect stack
365 Some possible command interpreter types and their meanings are:
367 \[Evaluator] read-eval-print loop for evaluating expressions
368 \[Debugger] single character commands for debugging errors
369 \[Where] single character commands for examining environments
371 Starting with release 6.2 of Scheme, the latter two types of command
372 interpreters will change the major mode of the Scheme process buffer
373 to scheme-debugger-mode , in which the evaluation commands are
374 disabled, and the keys which normally self insert instead send
375 themselves to the Scheme process. The command character ? will list
376 the available commands.
378 For older releases of Scheme, the major mode will be be
379 scheme-interaction-mode , and the command characters must be sent as
380 if they were expressions.
383 Delete converts tabs to spaces as it moves back.
384 Blank lines separate paragraphs. Semicolons start comments.
385 \\{scheme-interaction-mode-map}
387 Entry to this mode calls the value of scheme-interaction-mode-hook
388 with no args, if that value is non-nil.
389 Likewise with the value of scheme-mode-hook.
390 scheme-interaction-mode-hook is called after scheme-mode-hook."
393 (let ((previous-mode major-mode
))
394 (kill-all-local-variables)
395 (make-local-variable 'xscheme-previous-mode
)
396 (make-local-variable 'xscheme-buffer-name
)
397 (make-local-variable 'xscheme-process-name
)
398 (make-local-variable 'xscheme-previous-process-state
)
399 (make-local-variable 'xscheme-runlight-string
)
400 (make-local-variable 'xscheme-runlight
)
401 (make-local-variable 'xscheme-last-input-end
)
402 (setq xscheme-previous-mode previous-mode
)
403 (let ((buffer (current-buffer)))
404 (setq xscheme-buffer-name
(buffer-name buffer
))
405 (setq xscheme-last-input-end
(make-marker))
406 (let ((process (get-buffer-process buffer
)))
409 (setq xscheme-process-name
(process-name process
))
410 (setq xscheme-previous-process-state
411 (cons (process-filter process
)
412 (process-sentinel process
)))
413 (xscheme-process-filter-initialize t
)
414 (xscheme-modeline-initialize xscheme-buffer-name
)
415 (set-process-sentinel process
'xscheme-process-sentinel
)
416 (set-process-filter process
'xscheme-process-filter
))
417 (setq xscheme-previous-process-state
(cons nil nil
)))))))
418 (scheme-interaction-mode-initialize)
419 (scheme-mode-variables)
420 (run-mode-hooks 'scheme-mode-hook
'scheme-interaction-mode-hook
))
422 (defun exit-scheme-interaction-mode ()
423 "Take buffer out of scheme interaction mode"
425 (if (not (eq major-mode
'scheme-interaction-mode
))
426 (error "Buffer not in scheme interaction mode"))
427 (let ((previous-state xscheme-previous-process-state
))
428 (funcall xscheme-previous-mode
)
429 (let ((process (get-buffer-process (current-buffer))))
432 (if (eq (process-filter process
) 'xscheme-process-filter
)
433 (set-process-filter process
(car previous-state
)))
434 (if (eq (process-sentinel process
) 'xscheme-process-sentinel
)
435 (set-process-sentinel process
(cdr previous-state
))))))))
437 (defvar scheme-interaction-mode-commands-alist nil
)
438 (defvar scheme-interaction-mode-map nil
)
440 (defun scheme-interaction-mode-initialize ()
441 (use-local-map scheme-interaction-mode-map
)
442 (setq major-mode
'scheme-interaction-mode
)
443 (setq mode-name
"Scheme Interaction"))
445 (defun scheme-interaction-mode-commands (keymap)
446 (let ((entries scheme-interaction-mode-commands-alist
))
450 (car (cdr (car entries
))))
451 (setq entries
(cdr entries
)))))
453 ;; Initialize the command alist
454 (setq scheme-interaction-mode-commands-alist
455 (append scheme-interaction-mode-commands-alist
456 '(("\C-c\C-m" xscheme-send-current-line
)
457 ("\C-c\C-o" xscheme-delete-output
)
458 ("\C-c\C-p" xscheme-send-proceed
)
459 ("\C-c\C-y" xscheme-yank
)
460 ("\ep" xscheme-yank-pop
)
461 ("\en" xscheme-yank-push
))))
463 ;; Initialize the mode map
464 (if (not scheme-interaction-mode-map
)
466 (setq scheme-interaction-mode-map
(make-keymap))
467 (scheme-mode-commands scheme-interaction-mode-map
)
468 (xscheme-interrupt-commands scheme-interaction-mode-map
)
469 (xscheme-evaluation-commands scheme-interaction-mode-map
)
470 (scheme-interaction-mode-commands scheme-interaction-mode-map
)))
472 (defun xscheme-enter-interaction-mode ()
474 (set-buffer (xscheme-process-buffer))
475 (if (not (eq major-mode
'scheme-interaction-mode
))
476 (if (eq major-mode
'scheme-debugger-mode
)
477 (scheme-interaction-mode-initialize)
478 (scheme-interaction-mode t
)))))
480 (fset 'advertised-xscheme-send-previous-expression
481 'xscheme-send-previous-expression
)
485 (defun scheme-debugger-mode ()
486 "Major mode for executing the Scheme debugger.
487 Like scheme-mode except that the evaluation commands
488 are disabled, and characters that would normally be self inserting are
489 sent to the Scheme process instead. Typing ? will show you which
490 characters perform useful functions.
493 \\{scheme-debugger-mode-map}"
494 (error "Invalid entry to scheme-debugger-mode"))
496 (defvar scheme-debugger-mode-map nil
)
498 (defun scheme-debugger-mode-initialize ()
499 (use-local-map scheme-debugger-mode-map
)
500 (setq major-mode
'scheme-debugger-mode
)
501 (setq mode-name
"Scheme Debugger"))
503 (defun scheme-debugger-mode-commands (keymap)
506 (define-key keymap
(char-to-string char
) 'scheme-debugger-self-insert
)
507 (setq char
(1+ char
)))))
509 ;; Initialize the debugger mode map
510 (if (not scheme-debugger-mode-map
)
512 (setq scheme-debugger-mode-map
(make-keymap))
513 (scheme-mode-commands scheme-debugger-mode-map
)
514 (xscheme-interrupt-commands scheme-debugger-mode-map
)
515 (scheme-debugger-mode-commands scheme-debugger-mode-map
)))
517 (defun scheme-debugger-self-insert ()
518 "Transmit this character to the Scheme process."
520 (xscheme-send-char last-command-char
))
522 (defun xscheme-enter-debugger-mode (prompt-string)
524 (set-buffer (xscheme-process-buffer))
525 (if (not (eq major-mode
'scheme-debugger-mode
))
527 (if (not (eq major-mode
'scheme-interaction-mode
))
528 (scheme-interaction-mode t
))
529 (scheme-debugger-mode-initialize)))))
531 (defun xscheme-debugger-mode-p ()
532 (let ((buffer (xscheme-process-buffer)))
536 (eq major-mode
'scheme-debugger-mode
)))))
538 ;;;; Evaluation Commands
540 (defun xscheme-send-string (&rest strings
)
541 "Send the string arguments to the Scheme process.
542 The strings are concatenated and terminated by a newline."
543 (cond ((not (xscheme-process-running-p))
544 (if (yes-or-no-p "The Scheme process has died. Reset it? ")
547 (xscheme-wait-for-process)
548 (xscheme-send-string-1 strings
))))
549 ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode"))
550 ((and (not xscheme-allow-pipelined-evaluation
)
552 (error "No sends allowed while Scheme running"))
553 (t (xscheme-send-string-1 strings
))))
555 (defun xscheme-send-string-1 (strings)
556 (let ((string (apply 'concat strings
)))
557 (xscheme-send-string-2 string
)
558 (if (eq major-mode
'scheme-interaction-mode
)
559 (xscheme-insert-expression string
))))
561 (defun xscheme-send-string-2 (string)
562 (let ((process (get-process xscheme-process-name
)))
563 (process-send-string process
(concat string
"\n"))
564 (if (xscheme-process-buffer-current-p)
565 (set-marker (process-mark process
) (point)))))
567 (defun xscheme-select-process-buffer ()
568 "Select the Scheme process buffer and move to its output point."
571 (or (get-process xscheme-process-name
)
572 (error "No scheme process"))))
573 (let ((buffer (or (process-buffer process
) (error "No process buffer"))))
574 (let ((window (get-buffer-window buffer
)))
576 (select-window window
)
577 (switch-to-buffer buffer
))
578 (goto-char (process-mark process
))))))
580 ;;;; Scheme expressions ring
582 (defun xscheme-insert-expression (string)
583 (setq xscheme-expressions-ring-yank-pointer
584 (add-to-history 'xscheme-expressions-ring string
585 xscheme-expressions-ring-max
)))
587 (defun xscheme-rotate-yank-pointer (arg)
588 "Rotate the yanking point in the kill ring."
590 (let ((length (length xscheme-expressions-ring
)))
592 (error "Scheme expression ring is empty")
593 (setq xscheme-expressions-ring-yank-pointer
597 (length xscheme-expressions-ring-yank-pointer
)))
599 (nthcdr (if (< index
0)
602 xscheme-expressions-ring
))))))
604 (defun xscheme-yank (&optional arg
)
605 "Insert the most recent expression at point.
606 With just C-U as argument, same but put point in front (and mark at end).
607 With argument n, reinsert the nth most recently sent expression.
608 See also the commands \\[xscheme-yank-pop] and \\[xscheme-yank-push]."
610 (xscheme-rotate-yank-pointer (if (listp arg
) 0
614 (insert (car xscheme-expressions-ring-yank-pointer
))
616 (exchange-point-and-mark)))
618 ;; Old name, to avoid errors in users' init files.
619 (fset 'xscheme-yank-previous-send
622 (defun xscheme-yank-pop (arg)
623 "Insert or replace a just-yanked expression with an older expression.
624 If the previous command was not a yank, it yanks.
625 Otherwise, the region contains a stretch of reinserted
626 expression. yank-pop deletes that text and inserts in its
627 place a different expression.
629 With no argument, the next older expression is inserted.
630 With argument n, the n'th older expression is inserted.
631 If n is negative, this is a more recent expression.
633 The sequence of expressions wraps around, so that after the oldest one
634 comes the newest one."
636 (setq this-command
'xscheme-yank
)
637 (if (not (eq last-command
'xscheme-yank
))
640 (setq arg
(- arg
1))))
642 (let ((before (< (point) (mark))))
643 (delete-region (point) (mark))
644 (xscheme-rotate-yank-pointer arg
)
646 (insert (car xscheme-expressions-ring-yank-pointer
))
647 (if before
(exchange-point-and-mark)))))
649 (defun xscheme-yank-push (arg)
650 "Insert or replace a just-yanked expression with a more recent expression.
651 If the previous command was not a yank, it yanks.
652 Otherwise, the region contains a stretch of reinserted
653 expression. yank-pop deletes that text and inserts in its
654 place a different expression.
656 With no argument, the next more recent expression is inserted.
657 With argument n, the n'th more recent expression is inserted.
658 If n is negative, a less recent expression is used.
660 The sequence of expressions wraps around, so that after the oldest one
661 comes the newest one."
663 (xscheme-yank-pop (- 0 arg
)))
665 (defun xscheme-send-region (start end
)
666 "Send the current region to the Scheme process.
667 The region is sent terminated by a newline."
669 (if (xscheme-process-buffer-current-p)
673 (insert-before-markers ?
\n))
674 (set-marker (process-mark (get-process xscheme-process-name
))
676 (set-marker xscheme-last-input-end
(point))))
677 (xscheme-send-string (buffer-substring start end
)))
679 (defun xscheme-send-definition ()
680 "Send the current definition to the Scheme process.
681 If the current line begins with a non-whitespace character,
682 parse an expression from the beginning of the line and send that instead."
684 (let ((start nil
) (end nil
))
688 (if (re-search-backward "^\\s(" nil t
)
690 (error "Can't find definition")))
691 (xscheme-send-region start end
)))
693 (defun xscheme-send-next-expression ()
694 "Send the expression to the right of `point' to the Scheme process."
696 (let ((start (point)))
697 (xscheme-send-region start
(save-excursion (forward-sexp) (point)))))
699 (defun xscheme-send-previous-expression ()
700 "Send the expression to the left of `point' to the Scheme process."
703 (xscheme-send-region (save-excursion (backward-sexp) (point)) end
)))
705 (defun xscheme-send-current-line ()
706 "Send the current line to the Scheme process.
707 Useful for working with debugging Scheme under adb."
712 (let ((start (point)))
714 (buffer-substring start
(point))))))
717 (xscheme-send-string-2 line
)))
719 (defun xscheme-send-buffer ()
720 "Send the current buffer to the Scheme process."
722 (if (xscheme-process-buffer-current-p)
723 (error "Not allowed to send this buffer's contents to Scheme"))
724 (xscheme-send-region (point-min) (point-max)))
726 (defun xscheme-send-char (char)
727 "Prompt for a character and send it to the Scheme process."
728 (interactive "cCharacter to send: ")
729 (process-send-string xscheme-process-name
(char-to-string char
)))
731 (defun xscheme-delete-output ()
732 "Delete all output from interpreter since last input."
734 (let ((proc (get-buffer-process (current-buffer))))
736 (goto-char (process-mark proc
))
738 "^;\\(Unspecified return value$\\|Value\\( [0-9]+\\)?: \\|\\(Abort\\|Up\\|Quit\\)!$\\)"
739 xscheme-last-input-end
742 (if (< (marker-position xscheme-last-input-end
) (point))
744 (delete-region xscheme-last-input-end
(point))
745 (insert-before-markers "*** output flushed ***\n"))))))
749 (defun xscheme-send-breakpoint-interrupt ()
750 "Cause the Scheme process to enter a breakpoint."
752 (xscheme-send-interrupt ?b nil
))
754 (defun xscheme-send-proceed ()
755 "Cause the Scheme process to proceed from a breakpoint."
757 (process-send-string xscheme-process-name
"(proceed)\n"))
759 (defconst xscheme-control-g-message-string
760 "Sending C-G interrupt to Scheme...")
762 (defun xscheme-send-control-g-interrupt ()
763 "Cause the Scheme processor to halt and flush input.
764 Control returns to the top level rep loop."
766 (let ((inhibit-quit t
))
767 (cond ((not xscheme-control-g-synchronization-p
)
768 (interrupt-process xscheme-process-name
))
770 (set-buffer xscheme-buffer-name
)
771 xscheme-control-g-disabled-p
)
772 (message "Relax..."))
775 (set-buffer xscheme-buffer-name
)
776 (setq xscheme-control-g-disabled-p t
))
777 (message xscheme-control-g-message-string
)
778 (interrupt-process xscheme-process-name
)
780 (xscheme-send-char 0)))))
782 (defun xscheme-send-control-u-interrupt ()
783 "Cause the Scheme process to halt, returning to previous rep loop."
785 (xscheme-send-interrupt ?u t
))
787 (defun xscheme-send-control-x-interrupt ()
788 "Cause the Scheme process to halt, returning to current rep loop."
790 (xscheme-send-interrupt ?x t
))
792 ;;; This doesn't really work right -- Scheme just gobbles the first
793 ;;; character in the input. There is no way for us to guarantee that
794 ;;; the argument to this procedure is the first char unless we put
795 ;;; some kind of marker in the input stream.
797 (defun xscheme-send-interrupt (char mark-p
)
798 "Send a ^A type interrupt to the Scheme process."
799 (interactive "cInterrupt character to send: ")
800 (quit-process xscheme-process-name
)
802 (xscheme-send-char char
)
803 (if (and mark-p xscheme-control-g-synchronization-p
)
804 (xscheme-send-char 0)))
806 ;;;; Basic Process Control
808 (defun xscheme-start-process (command-line the-process the-buffer
)
809 (let ((buffer (get-buffer-create the-buffer
)))
810 (let ((process (get-buffer-process buffer
)))
813 (if (and process
(memq (process-status process
) '(run stop
)))
814 (set-marker (process-mark process
) (point-max))
815 (progn (if process
(delete-process process
))
816 (goto-char (point-max))
817 (scheme-interaction-mode nil
)
818 (setq xscheme-process-name the-process
)
820 (insert-before-markers
821 (substitute-command-keys xscheme-startup-message
)))
823 (let ((process-connection-type nil
))
824 (apply 'start-process
827 (xscheme-parse-command-line
829 (if (not (equal (process-name process
) the-process
))
830 (setq xscheme-process-name
(process-name process
)))
831 (if (not (equal (buffer-name buffer
) the-buffer
))
832 (setq xscheme-buffer-name
(buffer-name buffer
)))
833 (message "Starting process %s in buffer %s"
836 (set-marker (process-mark process
) (point-max))
837 (xscheme-process-filter-initialize t
)
838 (xscheme-modeline-initialize xscheme-buffer-name
)
839 (set-process-sentinel process
'xscheme-process-sentinel
)
840 (set-process-filter process
'xscheme-process-filter
)
841 (run-hooks 'xscheme-start-hook
)))))
844 (defun xscheme-parse-command-line (string)
845 (setq string
(substitute-in-file-name string
))
849 (let ((index (string-match "[ \t]" string start
)))
853 (cons (substring string start
)
857 (string-match "[^ \t]" string start
))
860 (cons (substring string start index
)
865 (defun xscheme-wait-for-process ()
867 (while xscheme-running-p
870 (defun xscheme-process-running-p ()
871 "True if there is a Scheme process whose status is `run'."
872 (let ((process (get-process xscheme-process-name
)))
874 (eq (process-status process
) 'run
))))
876 (defun xscheme-process-buffer ()
877 (let ((process (get-process xscheme-process-name
)))
878 (and process
(process-buffer process
))))
880 (defun xscheme-process-buffer-window ()
881 (let ((buffer (xscheme-process-buffer)))
882 (and buffer
(get-buffer-window buffer
))))
884 (defun xscheme-process-buffer-current-p ()
885 "True if the current buffer is the Scheme process buffer."
886 (eq (xscheme-process-buffer) (current-buffer)))
888 ;;;; Process Filter Operations
890 (defvar xscheme-process-filter-alist
892 xscheme-process-filter
:string-action-noexcursion
)
893 (?D xscheme-enter-debugger-mode
894 xscheme-process-filter
:string-action
)
896 xscheme-process-filter
:string-action
)
897 (?P xscheme-set-prompt-variable
898 xscheme-process-filter
:string-action
)
899 (?R xscheme-enter-interaction-mode
900 xscheme-process-filter
:simple-action
)
902 xscheme-process-filter
:simple-action
)
903 (?c xscheme-unsolicited-read-char
904 xscheme-process-filter
:simple-action
)
905 (?e xscheme-finish-gc
906 xscheme-process-filter
:simple-action
)
907 (?f xscheme-exit-input-wait
908 xscheme-process-filter
:simple-action
)
909 (?g xscheme-enable-control-g
910 xscheme-process-filter
:simple-action
)
911 (?i xscheme-prompt-for-expression
912 xscheme-process-filter
:string-action
)
914 xscheme-process-filter
:string-action
)
915 (?n xscheme-prompt-for-confirmation
916 xscheme-process-filter
:string-action
)
917 (?o xscheme-output-goto
918 xscheme-process-filter
:simple-action
)
919 (?p xscheme-set-prompt
920 xscheme-process-filter
:string-action
)
921 (?s xscheme-enter-input-wait
922 xscheme-process-filter
:simple-action
)
923 (?v xscheme-write-value
924 xscheme-process-filter
:string-action
)
926 xscheme-process-filter
:string-action
)
927 (?z xscheme-display-process-buffer
928 xscheme-process-filter
:simple-action
))
929 "Table used to decide how to handle process filter commands.
930 Value is a list of entries, each entry is a list of three items.
932 The first item is the character that the process filter dispatches on.
933 The second item is the action to be taken, a function.
934 The third item is the handler for the entry, a function.
936 When the process filter sees a command whose character matches a
937 particular entry, it calls the handler with two arguments: the action
938 and the string containing the rest of the process filter's input
939 stream. It is the responsibility of the handler to invoke the action
940 with the appropriate arguments, and to reenter the process filter with
941 the remaining input.")
945 (defun xscheme-process-sentinel (proc reason
)
946 (let* ((buffer (process-buffer proc
))
947 (name (buffer-name buffer
)))
950 (xscheme-process-filter-initialize (eq reason
'run
))
951 (if (not (eq reason
'run
))
953 (setq scheme-mode-line-process
"")
954 (setq xscheme-mode-string
"no process")
955 (if (equal name
(default-value 'xscheme-buffer-name
))
956 (setq-default xscheme-runlight
""))))
957 (if (and (not (memq reason
'(run stop
)))
958 xscheme-signal-death-message
)
962 "The Scheme process has died! Do M-x reset-scheme to restart it"))))))
964 (defun xscheme-process-filter-initialize (running-p)
965 (setq xscheme-process-filter-state
'idle
)
966 (setq xscheme-running-p running-p
)
967 (setq xscheme-control-g-disabled-p nil
)
968 (setq xscheme-allow-output-p t
)
969 (setq xscheme-prompt
"")
971 (let ((name (buffer-name (current-buffer))))
972 (setq scheme-mode-line-process
'(": " xscheme-runlight-string
))
973 (xscheme-modeline-initialize name
)
974 (if (equal name
(default-value 'xscheme-buffer-name
))
975 (setq-default xscheme-runlight default-xscheme-runlight
))))
976 (if (or (eq xscheme-runlight default-xscheme-runlight
)
977 (equal xscheme-runlight
""))
978 (setq xscheme-runlight
(list ": " 'xscheme-buffer-name
": " "?")))
979 (rplaca (nthcdr 3 xscheme-runlight
)
980 (if running-p
"?" "no process")))
982 (defun xscheme-process-filter (proc string
)
983 (let ((xscheme-filter-input string
)
984 (call-noexcursion nil
))
985 (while xscheme-filter-input
986 (setq call-noexcursion nil
)
988 (set-buffer (process-buffer proc
))
989 (cond ((eq xscheme-process-filter-state
'idle
)
990 (let ((start (string-match "\e" xscheme-filter-input
)))
993 (xscheme-process-filter-output
994 (substring xscheme-filter-input
0 start
))
995 (setq xscheme-filter-input
996 (substring xscheme-filter-input
(1+ start
)))
997 (setq xscheme-process-filter-state
'reading-type
))
998 (let ((string xscheme-filter-input
))
999 (setq xscheme-filter-input nil
)
1000 (xscheme-process-filter-output string
)))))
1001 ((eq xscheme-process-filter-state
'reading-type
)
1002 (if (zerop (length xscheme-filter-input
))
1003 (setq xscheme-filter-input nil
)
1004 (let ((char (aref xscheme-filter-input
0)))
1005 (setq xscheme-filter-input
1006 (substring xscheme-filter-input
1))
1007 (let ((entry (assoc char xscheme-process-filter-alist
)))
1009 (funcall (nth 2 entry
) (nth 1 entry
))
1011 (xscheme-process-filter-output ?\e char
)
1012 (setq xscheme-process-filter-state
'idle
)))))))
1013 ((eq xscheme-process-filter-state
'reading-string
)
1014 (let ((start (string-match "\e" xscheme-filter-input
)))
1017 (concat xscheme-string-accumulator
1018 (substring xscheme-filter-input
0 start
))))
1019 (setq xscheme-filter-input
1020 (substring xscheme-filter-input
(1+ start
)))
1021 (setq xscheme-process-filter-state
'idle
)
1022 (if (listp xscheme-string-receiver
)
1024 (setq xscheme-string-receiver
1025 (car xscheme-string-receiver
))
1026 (setq call-noexcursion string
))
1027 (funcall xscheme-string-receiver string
)))
1029 (setq xscheme-string-accumulator
1030 (concat xscheme-string-accumulator
1031 xscheme-filter-input
))
1032 (setq xscheme-filter-input nil
)))))
1034 (error "Scheme process filter -- bad state"))))
1035 (if call-noexcursion
1036 (funcall xscheme-string-receiver call-noexcursion
)))))
1038 ;;;; Process Filter Output
1040 (defun xscheme-process-filter-output (&rest args
)
1041 (if xscheme-allow-output-p
1042 (let ((string (apply 'concat args
)))
1044 (xscheme-goto-output-point)
1045 (let ((old-point (point)))
1046 (while (string-match "\\(\007\\|\f\\)" string
)
1047 (let ((start (match-beginning 0))
1048 (end (match-end 0)))
1049 (insert-before-markers (substring string
0 start
))
1050 (if (= ?
\f (aref string start
))
1053 (insert-before-markers ?
\n))
1054 (insert-before-markers ?
\f))
1056 (setq string
(substring string
(1+ start
)))))
1057 (insert-before-markers string
)
1058 (if (and xscheme-last-input-end
1059 (equal (marker-position xscheme-last-input-end
) (point)))
1060 (set-marker xscheme-last-input-end old-point
)))))))
1062 (defun xscheme-guarantee-newlines (n)
1063 (if xscheme-allow-output-p
1065 (xscheme-goto-output-point)
1067 (while (and (not stop
)
1073 (xscheme-goto-output-point)
1075 (insert-before-markers ?
\n)
1078 (defun xscheme-goto-output-point ()
1079 (let ((process (get-process xscheme-process-name
)))
1080 (set-buffer (process-buffer process
))
1081 (goto-char (process-mark process
))))
1083 (defun xscheme-modeline-initialize (name)
1084 (setq xscheme-runlight-string
"")
1085 (if (equal name
(default-value 'xscheme-buffer-name
))
1086 (setq-default xscheme-runlight-string
""))
1087 (setq xscheme-mode-string
"")
1088 (setq mode-line-buffer-identification
1089 (list (concat name
": ")
1090 'xscheme-mode-string
)))
1092 (defun xscheme-set-runlight (runlight)
1093 (setq xscheme-runlight-string runlight
)
1094 (if (equal (buffer-name (current-buffer))
1095 (default-value 'xscheme-buffer-name
))
1096 (setq-default xscheme-runlight-string runlight
))
1097 (rplaca (nthcdr 3 xscheme-runlight
) runlight
)
1098 (force-mode-line-update t
))
1100 (defun xscheme-process-filter:simple-action
(action)
1101 (setq xscheme-process-filter-state
'idle
)
1104 (defun xscheme-process-filter:string-action
(action)
1105 (setq xscheme-string-receiver action
)
1106 (setq xscheme-string-accumulator
"")
1107 (setq xscheme-process-filter-state
'reading-string
))
1109 (defun xscheme-process-filter:string-action-noexcursion
(action)
1110 (xscheme-process-filter:string-action
(cons action nil
)))
1112 (defconst xscheme-runlight
:running
"run"
1113 "The character displayed when the Scheme process is running.")
1115 (defconst xscheme-runlight
:input
"input"
1116 "The character displayed when the Scheme process is waiting for input.")
1118 (defconst xscheme-runlight
:gc
"gc"
1119 "The character displayed when the Scheme process is garbage collecting.")
1121 (defun xscheme-start-gc ()
1122 (xscheme-set-runlight xscheme-runlight
:gc
))
1124 (defun xscheme-finish-gc ()
1125 (xscheme-set-runlight
1126 (if xscheme-running-p xscheme-runlight
:running xscheme-runlight
:input
)))
1128 (defun xscheme-enter-input-wait ()
1129 (xscheme-set-runlight xscheme-runlight
:input
)
1130 (setq xscheme-control-g-disabled-p nil
)
1131 (setq xscheme-running-p nil
))
1133 (defun xscheme-exit-input-wait ()
1134 (xscheme-set-runlight xscheme-runlight
:running
)
1135 (setq xscheme-running-p t
))
1137 (defun xscheme-enable-control-g ()
1138 (setq xscheme-control-g-disabled-p nil
)
1139 (if (string= (current-message) xscheme-control-g-message-string
)
1142 (defun xscheme-display-process-buffer ()
1143 (let ((window (or (xscheme-process-buffer-window)
1144 (display-buffer (xscheme-process-buffer)))))
1145 (save-window-excursion
1146 (select-window window
)
1147 (xscheme-goto-output-point)
1148 (if (xscheme-debugger-mode-p)
1149 (xscheme-enter-interaction-mode)))))
1151 (defun xscheme-unsolicited-read-char ()
1154 (defun xscheme-eval (string)
1155 (eval (car (read-from-string string
))))
1157 (defun xscheme-message (string)
1158 (if (not (zerop (length string
)))
1159 (xscheme-write-message-1 string
(format ";%s" string
))))
1161 (defun xscheme-write-value (string)
1162 (if (zerop (length string
))
1163 (xscheme-write-message-1 "(no value)" ";Unspecified return value")
1164 (xscheme-write-message-1 string
(format ";Value: %s" string
))))
1166 (defun xscheme-write-message-1 (message-string output-string
)
1167 (let* ((process (get-process xscheme-process-name
))
1168 (window (get-buffer-window (process-buffer process
))))
1169 (if (or (not window
)
1170 (not (pos-visible-in-window-p (process-mark process
)
1172 (message "%s" message-string
)))
1173 (xscheme-guarantee-newlines 1)
1174 (xscheme-process-filter-output output-string
))
1176 (defun xscheme-set-prompt-variable (string)
1177 (setq xscheme-prompt string
))
1179 (defun xscheme-set-prompt (string)
1180 (setq xscheme-prompt string
)
1181 (xscheme-guarantee-newlines 2)
1182 (setq xscheme-mode-string
(xscheme-coerce-prompt string
))
1183 (force-mode-line-update t
))
1185 (defun xscheme-output-goto ()
1186 (xscheme-goto-output-point)
1187 (xscheme-guarantee-newlines 2))
1189 (defun xscheme-coerce-prompt (string)
1190 (if (string-match "^[0-9]+ \\[[^]]+\\] " string
)
1191 (let ((end (match-end 0)))
1192 (xscheme-process-filter-output (substring string end
))
1193 (substring string
0 (- end
1)))
1196 (defun xscheme-cd (directory-string)
1198 (set-buffer (xscheme-process-buffer))
1199 (cd directory-string
)))
1201 (defun xscheme-prompt-for-confirmation (prompt-string)
1202 (xscheme-send-char (if (y-or-n-p prompt-string
) ?y ?n
)))
1204 (defvar xscheme-prompt-for-expression-map nil
)
1205 (if (not xscheme-prompt-for-expression-map
)
1207 (setq xscheme-prompt-for-expression-map
1208 (copy-keymap minibuffer-local-map
))
1209 (substitute-key-definition 'exit-minibuffer
1210 'xscheme-prompt-for-expression-exit
1211 xscheme-prompt-for-expression-map
)))
1213 (defun xscheme-prompt-for-expression (prompt-string)
1214 (xscheme-send-string-2
1215 (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map
)))
1217 (defun xscheme-prompt-for-expression-exit ()
1219 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one
)
1221 (error "input must be a single, complete expression")))
1223 (defun xscheme-region-expression-p (start end
)
1225 (let ((old-syntax-table (syntax-table)))
1228 (set-syntax-table scheme-mode-syntax-table
)
1229 (let ((state (parse-partial-sexp start end
)))
1230 (and (zerop (car state
)) ;depth = 0
1231 (nth 2 state
) ;last-sexp exists, i.e. >= 1 sexps
1232 (let ((state (parse-partial-sexp start
(nth 2 state
))))
1233 (if (nth 2 state
) 'many
'one
)))))
1234 (set-syntax-table old-syntax-table
)))))
1238 ;;; arch-tag: cfc14adc-2917-409e-ad16-432e8d0017de
1239 ;;; xscheme.el ends here