1 ;;; xscheme.el --- run MIT Scheme under Emacs
3 ;; Copyright (C) 1986, 1987, 1989, 1990, 2001, 2002, 2003, 2004, 2005
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 2, 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 (mapcar '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
(cons string xscheme-expressions-ring
))
584 (if (> (length xscheme-expressions-ring
) xscheme-expressions-ring-max
)
585 (setcdr (nthcdr (1- xscheme-expressions-ring-max
)
586 xscheme-expressions-ring
)
588 (setq xscheme-expressions-ring-yank-pointer xscheme-expressions-ring
))
590 (defun xscheme-rotate-yank-pointer (arg)
591 "Rotate the yanking point in the kill ring."
593 (let ((length (length xscheme-expressions-ring
)))
595 (error "Scheme expression ring is empty")
596 (setq xscheme-expressions-ring-yank-pointer
600 (length xscheme-expressions-ring-yank-pointer
)))
602 (nthcdr (if (< index
0)
605 xscheme-expressions-ring
))))))
607 (defun xscheme-yank (&optional arg
)
608 "Insert the most recent expression at point.
609 With just C-U as argument, same but put point in front (and mark at end).
610 With argument n, reinsert the nth most recently sent expression.
611 See also the commands \\[xscheme-yank-pop] and \\[xscheme-yank-push]."
613 (xscheme-rotate-yank-pointer (if (listp arg
) 0
617 (insert (car xscheme-expressions-ring-yank-pointer
))
619 (exchange-point-and-mark)))
621 ;; Old name, to avoid errors in users' init files.
622 (fset 'xscheme-yank-previous-send
625 (defun xscheme-yank-pop (arg)
626 "Insert or replace a just-yanked expression with an older expression.
627 If the previous command was not a yank, it yanks.
628 Otherwise, the region contains a stretch of reinserted
629 expression. yank-pop deletes that text and inserts in its
630 place a different expression.
632 With no argument, the next older expression is inserted.
633 With argument n, the n'th older expression is inserted.
634 If n is negative, this is a more recent expression.
636 The sequence of expressions wraps around, so that after the oldest one
637 comes the newest one."
639 (setq this-command
'xscheme-yank
)
640 (if (not (eq last-command
'xscheme-yank
))
643 (setq arg
(- arg
1))))
645 (let ((before (< (point) (mark))))
646 (delete-region (point) (mark))
647 (xscheme-rotate-yank-pointer arg
)
649 (insert (car xscheme-expressions-ring-yank-pointer
))
650 (if before
(exchange-point-and-mark)))))
652 (defun xscheme-yank-push (arg)
653 "Insert or replace a just-yanked expression with a more recent expression.
654 If the previous command was not a yank, it yanks.
655 Otherwise, the region contains a stretch of reinserted
656 expression. yank-pop deletes that text and inserts in its
657 place a different expression.
659 With no argument, the next more recent expression is inserted.
660 With argument n, the n'th more recent expression is inserted.
661 If n is negative, a less recent expression is used.
663 The sequence of expressions wraps around, so that after the oldest one
664 comes the newest one."
666 (xscheme-yank-pop (- 0 arg
)))
668 (defun xscheme-send-region (start end
)
669 "Send the current region to the Scheme process.
670 The region is sent terminated by a newline."
672 (if (xscheme-process-buffer-current-p)
676 (insert-before-markers ?
\n))
677 (set-marker (process-mark (get-process xscheme-process-name
))
679 (set-marker xscheme-last-input-end
(point))))
680 (xscheme-send-string (buffer-substring start end
)))
682 (defun xscheme-send-definition ()
683 "Send the current definition to the Scheme process.
684 If the current line begins with a non-whitespace character,
685 parse an expression from the beginning of the line and send that instead."
687 (let ((start nil
) (end nil
))
691 (if (re-search-backward "^\\s(" nil t
)
693 (error "Can't find definition")))
694 (xscheme-send-region start end
)))
696 (defun xscheme-send-next-expression ()
697 "Send the expression to the right of `point' to the Scheme process."
699 (let ((start (point)))
700 (xscheme-send-region start
(save-excursion (forward-sexp) (point)))))
702 (defun xscheme-send-previous-expression ()
703 "Send the expression to the left of `point' to the Scheme process."
706 (xscheme-send-region (save-excursion (backward-sexp) (point)) end
)))
708 (defun xscheme-send-current-line ()
709 "Send the current line to the Scheme process.
710 Useful for working with debugging Scheme under adb."
715 (let ((start (point)))
717 (buffer-substring start
(point))))))
720 (xscheme-send-string-2 line
)))
722 (defun xscheme-send-buffer ()
723 "Send the current buffer to the Scheme process."
725 (if (xscheme-process-buffer-current-p)
726 (error "Not allowed to send this buffer's contents to Scheme"))
727 (xscheme-send-region (point-min) (point-max)))
729 (defun xscheme-send-char (char)
730 "Prompt for a character and send it to the Scheme process."
731 (interactive "cCharacter to send: ")
732 (process-send-string xscheme-process-name
(char-to-string char
)))
734 (defun xscheme-delete-output ()
735 "Delete all output from interpreter since last input."
737 (let ((proc (get-buffer-process (current-buffer))))
739 (goto-char (process-mark proc
))
741 "^;\\(Unspecified return value$\\|Value\\( [0-9]+\\)?: \\|\\(Abort\\|Up\\|Quit\\)!$\\)"
742 xscheme-last-input-end
745 (if (< (marker-position xscheme-last-input-end
) (point))
747 (delete-region xscheme-last-input-end
(point))
748 (insert-before-markers "*** output flushed ***\n"))))))
752 (defun xscheme-send-breakpoint-interrupt ()
753 "Cause the Scheme process to enter a breakpoint."
755 (xscheme-send-interrupt ?b nil
))
757 (defun xscheme-send-proceed ()
758 "Cause the Scheme process to proceed from a breakpoint."
760 (process-send-string xscheme-process-name
"(proceed)\n"))
762 (defconst xscheme-control-g-message-string
763 "Sending C-G interrupt to Scheme...")
765 (defun xscheme-send-control-g-interrupt ()
766 "Cause the Scheme processor to halt and flush input.
767 Control returns to the top level rep loop."
769 (let ((inhibit-quit t
))
770 (cond ((not xscheme-control-g-synchronization-p
)
771 (interrupt-process xscheme-process-name
))
773 (set-buffer xscheme-buffer-name
)
774 xscheme-control-g-disabled-p
)
775 (message "Relax..."))
778 (set-buffer xscheme-buffer-name
)
779 (setq xscheme-control-g-disabled-p t
))
780 (message xscheme-control-g-message-string
)
781 (interrupt-process xscheme-process-name
)
783 (xscheme-send-char 0)))))
785 (defun xscheme-send-control-u-interrupt ()
786 "Cause the Scheme process to halt, returning to previous rep loop."
788 (xscheme-send-interrupt ?u t
))
790 (defun xscheme-send-control-x-interrupt ()
791 "Cause the Scheme process to halt, returning to current rep loop."
793 (xscheme-send-interrupt ?x t
))
795 ;;; This doesn't really work right -- Scheme just gobbles the first
796 ;;; character in the input. There is no way for us to guarantee that
797 ;;; the argument to this procedure is the first char unless we put
798 ;;; some kind of marker in the input stream.
800 (defun xscheme-send-interrupt (char mark-p
)
801 "Send a ^A type interrupt to the Scheme process."
802 (interactive "cInterrupt character to send: ")
803 (quit-process xscheme-process-name
)
805 (xscheme-send-char char
)
806 (if (and mark-p xscheme-control-g-synchronization-p
)
807 (xscheme-send-char 0)))
809 ;;;; Basic Process Control
811 (defun xscheme-start-process (command-line the-process the-buffer
)
812 (let ((buffer (get-buffer-create the-buffer
)))
813 (let ((process (get-buffer-process buffer
)))
816 (if (and process
(memq (process-status process
) '(run stop
)))
817 (set-marker (process-mark process
) (point-max))
818 (progn (if process
(delete-process process
))
819 (goto-char (point-max))
820 (scheme-interaction-mode nil
)
821 (setq xscheme-process-name the-process
)
823 (insert-before-markers
824 (substitute-command-keys xscheme-startup-message
)))
826 (let ((process-connection-type nil
))
827 (apply 'start-process
830 (xscheme-parse-command-line
832 (if (not (equal (process-name process
) the-process
))
833 (setq xscheme-process-name
(process-name process
)))
834 (if (not (equal (buffer-name buffer
) the-buffer
))
835 (setq xscheme-buffer-name
(buffer-name buffer
)))
836 (message "Starting process %s in buffer %s"
839 (set-marker (process-mark process
) (point-max))
840 (xscheme-process-filter-initialize t
)
841 (xscheme-modeline-initialize xscheme-buffer-name
)
842 (set-process-sentinel process
'xscheme-process-sentinel
)
843 (set-process-filter process
'xscheme-process-filter
)
844 (run-hooks 'xscheme-start-hook
)))))
847 (defun xscheme-parse-command-line (string)
848 (setq string
(substitute-in-file-name string
))
852 (let ((index (string-match "[ \t]" string start
)))
856 (cons (substring string start
)
860 (string-match "[^ \t]" string start
))
863 (cons (substring string start index
)
868 (defun xscheme-wait-for-process ()
870 (while xscheme-running-p
873 (defun xscheme-process-running-p ()
874 "True iff there is a Scheme process whose status is `run'."
875 (let ((process (get-process xscheme-process-name
)))
877 (eq (process-status process
) 'run
))))
879 (defun xscheme-process-buffer ()
880 (let ((process (get-process xscheme-process-name
)))
881 (and process
(process-buffer process
))))
883 (defun xscheme-process-buffer-window ()
884 (let ((buffer (xscheme-process-buffer)))
885 (and buffer
(get-buffer-window buffer
))))
887 (defun xscheme-process-buffer-current-p ()
888 "True iff the current buffer is the Scheme process buffer."
889 (eq (xscheme-process-buffer) (current-buffer)))
891 ;;;; Process Filter Operations
893 (defvar xscheme-process-filter-alist
895 xscheme-process-filter
:string-action-noexcursion
)
896 (?D xscheme-enter-debugger-mode
897 xscheme-process-filter
:string-action
)
899 xscheme-process-filter
:string-action
)
900 (?P xscheme-set-prompt-variable
901 xscheme-process-filter
:string-action
)
902 (?R xscheme-enter-interaction-mode
903 xscheme-process-filter
:simple-action
)
905 xscheme-process-filter
:simple-action
)
906 (?c xscheme-unsolicited-read-char
907 xscheme-process-filter
:simple-action
)
908 (?e xscheme-finish-gc
909 xscheme-process-filter
:simple-action
)
910 (?f xscheme-exit-input-wait
911 xscheme-process-filter
:simple-action
)
912 (?g xscheme-enable-control-g
913 xscheme-process-filter
:simple-action
)
914 (?i xscheme-prompt-for-expression
915 xscheme-process-filter
:string-action
)
917 xscheme-process-filter
:string-action
)
918 (?n xscheme-prompt-for-confirmation
919 xscheme-process-filter
:string-action
)
920 (?o xscheme-output-goto
921 xscheme-process-filter
:simple-action
)
922 (?p xscheme-set-prompt
923 xscheme-process-filter
:string-action
)
924 (?s xscheme-enter-input-wait
925 xscheme-process-filter
:simple-action
)
926 (?v xscheme-write-value
927 xscheme-process-filter
:string-action
)
929 xscheme-process-filter
:string-action
)
930 (?z xscheme-display-process-buffer
931 xscheme-process-filter
:simple-action
))
932 "Table used to decide how to handle process filter commands.
933 Value is a list of entries, each entry is a list of three items.
935 The first item is the character that the process filter dispatches on.
936 The second item is the action to be taken, a function.
937 The third item is the handler for the entry, a function.
939 When the process filter sees a command whose character matches a
940 particular entry, it calls the handler with two arguments: the action
941 and the string containing the rest of the process filter's input
942 stream. It is the responsibility of the handler to invoke the action
943 with the appropriate arguments, and to reenter the process filter with
944 the remaining input.")
948 (defun xscheme-process-sentinel (proc reason
)
949 (let* ((buffer (process-buffer proc
))
950 (name (buffer-name buffer
)))
953 (xscheme-process-filter-initialize (eq reason
'run
))
954 (if (not (eq reason
'run
))
956 (setq scheme-mode-line-process
"")
957 (setq xscheme-mode-string
"no process")
958 (if (equal name
(default-value 'xscheme-buffer-name
))
959 (setq-default xscheme-runlight
""))))
960 (if (and (not (memq reason
'(run stop
)))
961 xscheme-signal-death-message
)
965 "The Scheme process has died! Do M-x reset-scheme to restart it"))))))
967 (defun xscheme-process-filter-initialize (running-p)
968 (setq xscheme-process-filter-state
'idle
)
969 (setq xscheme-running-p running-p
)
970 (setq xscheme-control-g-disabled-p nil
)
971 (setq xscheme-allow-output-p t
)
972 (setq xscheme-prompt
"")
974 (let ((name (buffer-name (current-buffer))))
975 (setq scheme-mode-line-process
'(": " xscheme-runlight-string
))
976 (xscheme-modeline-initialize name
)
977 (if (equal name
(default-value 'xscheme-buffer-name
))
978 (setq-default xscheme-runlight default-xscheme-runlight
))))
979 (if (or (eq xscheme-runlight default-xscheme-runlight
)
980 (equal xscheme-runlight
""))
981 (setq xscheme-runlight
(list ": " 'xscheme-buffer-name
": " "?")))
982 (rplaca (nthcdr 3 xscheme-runlight
)
983 (if running-p
"?" "no process")))
985 (defun xscheme-process-filter (proc string
)
986 (let ((xscheme-filter-input string
)
987 (call-noexcursion nil
))
988 (while xscheme-filter-input
989 (setq call-noexcursion nil
)
991 (set-buffer (process-buffer proc
))
992 (cond ((eq xscheme-process-filter-state
'idle
)
993 (let ((start (string-match "\e" xscheme-filter-input
)))
996 (xscheme-process-filter-output
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
'reading-type
))
1001 (let ((string xscheme-filter-input
))
1002 (setq xscheme-filter-input nil
)
1003 (xscheme-process-filter-output string
)))))
1004 ((eq xscheme-process-filter-state
'reading-type
)
1005 (if (zerop (length xscheme-filter-input
))
1006 (setq xscheme-filter-input nil
)
1007 (let ((char (aref xscheme-filter-input
0)))
1008 (setq xscheme-filter-input
1009 (substring xscheme-filter-input
1))
1010 (let ((entry (assoc char xscheme-process-filter-alist
)))
1012 (funcall (nth 2 entry
) (nth 1 entry
))
1014 (xscheme-process-filter-output ?\e char
)
1015 (setq xscheme-process-filter-state
'idle
)))))))
1016 ((eq xscheme-process-filter-state
'reading-string
)
1017 (let ((start (string-match "\e" xscheme-filter-input
)))
1020 (concat xscheme-string-accumulator
1021 (substring xscheme-filter-input
0 start
))))
1022 (setq xscheme-filter-input
1023 (substring xscheme-filter-input
(1+ start
)))
1024 (setq xscheme-process-filter-state
'idle
)
1025 (if (listp xscheme-string-receiver
)
1027 (setq xscheme-string-receiver
1028 (car xscheme-string-receiver
))
1029 (setq call-noexcursion string
))
1030 (funcall xscheme-string-receiver string
)))
1032 (setq xscheme-string-accumulator
1033 (concat xscheme-string-accumulator
1034 xscheme-filter-input
))
1035 (setq xscheme-filter-input nil
)))))
1037 (error "Scheme process filter -- bad state"))))
1038 (if call-noexcursion
1039 (funcall xscheme-string-receiver call-noexcursion
)))))
1041 ;;;; Process Filter Output
1043 (defun xscheme-process-filter-output (&rest args
)
1044 (if xscheme-allow-output-p
1045 (let ((string (apply 'concat args
)))
1047 (xscheme-goto-output-point)
1048 (let ((old-point (point)))
1049 (while (string-match "\\(\007\\|\f\\)" string
)
1050 (let ((start (match-beginning 0))
1051 (end (match-end 0)))
1052 (insert-before-markers (substring string
0 start
))
1053 (if (= ?
\f (aref string start
))
1056 (insert-before-markers ?
\n))
1057 (insert-before-markers ?
\f))
1059 (setq string
(substring string
(1+ start
)))))
1060 (insert-before-markers string
)
1061 (if (and xscheme-last-input-end
1062 (equal (marker-position xscheme-last-input-end
) (point)))
1063 (set-marker xscheme-last-input-end old-point
)))))))
1065 (defun xscheme-guarantee-newlines (n)
1066 (if xscheme-allow-output-p
1068 (xscheme-goto-output-point)
1070 (while (and (not stop
)
1076 (xscheme-goto-output-point)
1078 (insert-before-markers ?
\n)
1081 (defun xscheme-goto-output-point ()
1082 (let ((process (get-process xscheme-process-name
)))
1083 (set-buffer (process-buffer process
))
1084 (goto-char (process-mark process
))))
1086 (defun xscheme-modeline-initialize (name)
1087 (setq xscheme-runlight-string
"")
1088 (if (equal name
(default-value 'xscheme-buffer-name
))
1089 (setq-default xscheme-runlight-string
""))
1090 (setq xscheme-mode-string
"")
1091 (setq mode-line-buffer-identification
1092 (list (concat name
": ")
1093 'xscheme-mode-string
)))
1095 (defun xscheme-set-runlight (runlight)
1096 (setq xscheme-runlight-string runlight
)
1097 (if (equal (buffer-name (current-buffer))
1098 (default-value 'xscheme-buffer-name
))
1099 (setq-default xscheme-runlight-string runlight
))
1100 (rplaca (nthcdr 3 xscheme-runlight
) runlight
)
1101 (force-mode-line-update t
))
1103 (defun xscheme-process-filter:simple-action
(action)
1104 (setq xscheme-process-filter-state
'idle
)
1107 (defun xscheme-process-filter:string-action
(action)
1108 (setq xscheme-string-receiver action
)
1109 (setq xscheme-string-accumulator
"")
1110 (setq xscheme-process-filter-state
'reading-string
))
1112 (defun xscheme-process-filter:string-action-noexcursion
(action)
1113 (xscheme-process-filter:string-action
(cons action nil
)))
1115 (defconst xscheme-runlight
:running
"run"
1116 "The character displayed when the Scheme process is running.")
1118 (defconst xscheme-runlight
:input
"input"
1119 "The character displayed when the Scheme process is waiting for input.")
1121 (defconst xscheme-runlight
:gc
"gc"
1122 "The character displayed when the Scheme process is garbage collecting.")
1124 (defun xscheme-start-gc ()
1125 (xscheme-set-runlight xscheme-runlight
:gc
))
1127 (defun xscheme-finish-gc ()
1128 (xscheme-set-runlight
1129 (if xscheme-running-p xscheme-runlight
:running xscheme-runlight
:input
)))
1131 (defun xscheme-enter-input-wait ()
1132 (xscheme-set-runlight xscheme-runlight
:input
)
1133 (setq xscheme-control-g-disabled-p nil
)
1134 (setq xscheme-running-p nil
))
1136 (defun xscheme-exit-input-wait ()
1137 (xscheme-set-runlight xscheme-runlight
:running
)
1138 (setq xscheme-running-p t
))
1140 (defun xscheme-enable-control-g ()
1141 (setq xscheme-control-g-disabled-p nil
)
1142 (if (string= (current-message) xscheme-control-g-message-string
)
1145 (defun xscheme-display-process-buffer ()
1146 (let ((window (or (xscheme-process-buffer-window)
1147 (display-buffer (xscheme-process-buffer)))))
1148 (save-window-excursion
1149 (select-window window
)
1150 (xscheme-goto-output-point)
1151 (if (xscheme-debugger-mode-p)
1152 (xscheme-enter-interaction-mode)))))
1154 (defun xscheme-unsolicited-read-char ()
1157 (defun xscheme-eval (string)
1158 (eval (car (read-from-string string
))))
1160 (defun xscheme-message (string)
1161 (if (not (zerop (length string
)))
1162 (xscheme-write-message-1 string
(format ";%s" string
))))
1164 (defun xscheme-write-value (string)
1165 (if (zerop (length string
))
1166 (xscheme-write-message-1 "(no value)" ";Unspecified return value")
1167 (xscheme-write-message-1 string
(format ";Value: %s" string
))))
1169 (defun xscheme-write-message-1 (message-string output-string
)
1170 (let* ((process (get-process xscheme-process-name
))
1171 (window (get-buffer-window (process-buffer process
))))
1172 (if (or (not window
)
1173 (not (pos-visible-in-window-p (process-mark process
)
1175 (message "%s" message-string
)))
1176 (xscheme-guarantee-newlines 1)
1177 (xscheme-process-filter-output output-string
))
1179 (defun xscheme-set-prompt-variable (string)
1180 (setq xscheme-prompt string
))
1182 (defun xscheme-set-prompt (string)
1183 (setq xscheme-prompt string
)
1184 (xscheme-guarantee-newlines 2)
1185 (setq xscheme-mode-string
(xscheme-coerce-prompt string
))
1186 (force-mode-line-update t
))
1188 (defun xscheme-output-goto ()
1189 (xscheme-goto-output-point)
1190 (xscheme-guarantee-newlines 2))
1192 (defun xscheme-coerce-prompt (string)
1193 (if (string-match "^[0-9]+ \\[[^]]+\\] " string
)
1194 (let ((end (match-end 0)))
1195 (xscheme-process-filter-output (substring string end
))
1196 (substring string
0 (- end
1)))
1199 (defun xscheme-cd (directory-string)
1201 (set-buffer (xscheme-process-buffer))
1202 (cd directory-string
)))
1204 (defun xscheme-prompt-for-confirmation (prompt-string)
1205 (xscheme-send-char (if (y-or-n-p prompt-string
) ?y ?n
)))
1207 (defvar xscheme-prompt-for-expression-map nil
)
1208 (if (not xscheme-prompt-for-expression-map
)
1210 (setq xscheme-prompt-for-expression-map
1211 (copy-keymap minibuffer-local-map
))
1212 (substitute-key-definition 'exit-minibuffer
1213 'xscheme-prompt-for-expression-exit
1214 xscheme-prompt-for-expression-map
)))
1216 (defun xscheme-prompt-for-expression (prompt-string)
1217 (xscheme-send-string-2
1218 (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map
)))
1220 (defun xscheme-prompt-for-expression-exit ()
1222 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one
)
1224 (error "input must be a single, complete expression")))
1226 (defun xscheme-region-expression-p (start end
)
1228 (let ((old-syntax-table (syntax-table)))
1231 (set-syntax-table scheme-mode-syntax-table
)
1232 (let ((state (parse-partial-sexp start end
)))
1233 (and (zerop (car state
)) ;depth = 0
1234 (nth 2 state
) ;last-sexp exists, i.e. >= 1 sexps
1235 (let ((state (parse-partial-sexp start
(nth 2 state
))))
1236 (if (nth 2 state
) 'many
'one
)))))
1237 (set-syntax-table old-syntax-table
)))))
1241 ;;; arch-tag: cfc14adc-2917-409e-ad16-432e8d0017de
1242 ;;; xscheme.el ends here