(insert-string): Moved from mocklisp.c, reimplemented in Lisp. Obsoleted.
[bpt/emacs.git] / lisp / xscheme.el
CommitLineData
7e722af3 1;;; xscheme.el --- run MIT Scheme under Emacs
76d7458e 2
eea8d4ef
ER
3;; Copyright (C) 1986, 1987, 1989, 1990 Free Software Foundation, Inc.
4
d46bac56 5;; Maintainer: FSF
b7f66977 6;; Keywords: languages, lisp
6cf0d154 7
a1ad21ca
CH
8;; This file is part of GNU Emacs.
9
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
a1ad21ca
CH
13;; any later version.
14
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.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
a1ad21ca 24
6cf0d154
ER
25;;; Commentary:
26
c91c4e6d
ER
27;; A major mode for editing Scheme and interacting with MIT's C-Scheme.
28;;
29;; Requires C-Scheme release 5 or later
30;; Changes to Control-G handler require runtime version 13.85 or later
a1ad21ca 31
6cf0d154
ER
32;;; Code:
33
a1ad21ca
CH
34(require 'scheme)
35\f
4bef9110
SE
36(defgroup xscheme nil
37 "Major mode for editing Scheme and interacting with MIT's C-Scheme."
38 :group 'lisp)
39
4bef9110
SE
40(defcustom scheme-band-name nil
41 "*Band loaded by the `run-scheme' command."
42 :type '(choice (const nil) string)
43 :group 'xscheme)
44
45(defcustom scheme-program-arguments nil
46 "*Arguments passed to the Scheme program by the `run-scheme' command."
47 :type '(choice (const nil) string)
48 :group 'xscheme)
49
50(defcustom xscheme-allow-pipelined-evaluation t
a1ad21ca
CH
51 "If non-nil, an expression may be transmitted while another is evaluating.
52Otherwise, attempting to evaluate an expression before the previous expression
4bef9110
SE
53has finished evaluating will signal an error."
54 :type 'boolean
55 :group 'xscheme)
a1ad21ca 56
4bef9110 57(defcustom xscheme-startup-message
a1ad21ca
CH
58 "This is the Scheme process buffer.
59Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point.
60Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
61Type \\[describe-mode] for more information.
62
63"
64 "String to insert into Scheme process buffer first time it is started.
4bef9110
SE
65Is processed with `substitute-command-keys' first."
66 :type 'string
67 :group 'xscheme)
a1ad21ca 68
4bef9110
SE
69(defcustom xscheme-signal-death-message nil
70 "If non-nil, causes a message to be generated when the Scheme process dies."
71 :type 'boolean
72 :group 'xscheme)
a1ad21ca
CH
73
74(defun xscheme-evaluation-commands (keymap)
75 (define-key keymap "\e\C-x" 'xscheme-send-definition)
76 (define-key keymap "\C-x\C-e" 'advertised-xscheme-send-previous-expression)
77 (define-key keymap "\eo" 'xscheme-send-buffer)
78 (define-key keymap "\ez" 'xscheme-send-definition)
79 (define-key keymap "\e\C-m" 'xscheme-send-previous-expression)
80 (define-key keymap "\e\C-z" 'xscheme-send-region))
81
82(defun xscheme-interrupt-commands (keymap)
83 (define-key keymap "\C-c\C-s" 'xscheme-select-process-buffer)
84 (define-key keymap "\C-c\C-b" 'xscheme-send-breakpoint-interrupt)
85 (define-key keymap "\C-c\C-c" 'xscheme-send-control-g-interrupt)
86 (define-key keymap "\C-c\C-u" 'xscheme-send-control-u-interrupt)
87 (define-key keymap "\C-c\C-x" 'xscheme-send-control-x-interrupt))
88
89(xscheme-evaluation-commands scheme-mode-map)
90(xscheme-interrupt-commands scheme-mode-map)
91\f
92(defun run-scheme (command-line)
0178cafa 93 "Run MIT Scheme in an inferior process.
a1ad21ca
CH
94Output goes to the buffer `*scheme*'.
95With argument, asks for a command line."
96 (interactive
97 (list (let ((default
98 (or xscheme-process-command-line
99 (xscheme-default-command-line))))
100 (if current-prefix-arg
101 (read-string "Run Scheme: " default)
102 default))))
103 (setq xscheme-process-command-line command-line)
f0e30fe8 104 (pop-to-buffer (xscheme-start-process command-line)))
a1ad21ca
CH
105
106(defun reset-scheme ()
107 "Reset the Scheme process."
108 (interactive)
109 (let ((process (get-process "scheme")))
110 (cond ((or (not process)
111 (not (eq (process-status process) 'run))
112 (yes-or-no-p
113"The Scheme process is running, are you SURE you want to reset it? "))
114 (message "Resetting Scheme process...")
115 (if process (kill-process process t))
116 (xscheme-start-process xscheme-process-command-line)
117 (message "Resetting Scheme process...done")))))
118
119(defun xscheme-default-command-line ()
120 (concat scheme-program-name " -emacs"
121 (if scheme-program-arguments
122 (concat " " scheme-program-arguments)
123 "")
124 (if scheme-band-name
125 (concat " -band " scheme-band-name)
126 "")))
127\f
128;;;; Interaction Mode
129
130(defun scheme-interaction-mode ()
131 "Major mode for interacting with the inferior Scheme process.
132Like scheme-mode except that:
133
134\\[advertised-xscheme-send-previous-expression] sends the expression before point to the Scheme process as input
135\\[xscheme-yank-previous-send] yanks the expression most recently sent to Scheme
136
137All output from the Scheme process is written in the Scheme process
138buffer, which is initially named \"*scheme*\". The result of
139evaluating a Scheme expression is also printed in the process buffer,
140preceded by the string \";Value: \" to highlight it. If the process
141buffer is not visible at that time, the value will also be displayed
142in the minibuffer. If an error occurs, the process buffer will
143automatically pop up to show you the error message.
144
145While the Scheme process is running, the modelines of all buffers in
146scheme-mode are modified to show the state of the process. The
147possible states and their meanings are:
148
149input waiting for input
150run evaluating
151gc garbage collecting
152
153The process buffer's modeline contains additional information where
154the buffer's name is normally displayed: the command interpreter level
155and type.
156
157Scheme maintains a stack of command interpreters. Every time an error
158or breakpoint occurs, the current command interpreter is pushed on the
159command interpreter stack, and a new command interpreter is started.
160One example of why this is done is so that an error that occurs while
161you are debugging another error will not destroy the state of the
162initial error, allowing you to return to it after the second error has
163been fixed.
164
165The command interpreter level indicates how many interpreters are in
166the command interpreter stack. It is initially set to one, and it is
167incremented every time that stack is pushed, and decremented every
168time it is popped. The following commands are useful for manipulating
169the command interpreter stack:
170
171\\[xscheme-send-breakpoint-interrupt] pushes the stack once
172\\[xscheme-send-control-u-interrupt] pops the stack once
173\\[xscheme-send-control-g-interrupt] pops everything off
174\\[xscheme-send-control-x-interrupt] aborts evaluation, doesn't affect stack
175
176Some possible command interpreter types and their meanings are:
177
178[Evaluator] read-eval-print loop for evaluating expressions
179[Debugger] single character commands for debugging errors
180[Where] single character commands for examining environments
181
182Starting with release 6.2 of Scheme, the latter two types of command
183interpreters will change the major mode of the Scheme process buffer
184to scheme-debugger-mode , in which the evaluation commands are
185disabled, and the keys which normally self insert instead send
186themselves to the Scheme process. The command character ? will list
187the available commands.
188
189For older releases of Scheme, the major mode will be be
190scheme-interaction-mode , and the command characters must be sent as
191if they were expressions.
192
193Commands:
194Delete converts tabs to spaces as it moves back.
195Blank lines separate paragraphs. Semicolons start comments.
196\\{scheme-interaction-mode-map}
197
198Entry to this mode calls the value of scheme-interaction-mode-hook
199with no args, if that value is non-nil.
200 Likewise with the value of scheme-mode-hook.
201 scheme-interaction-mode-hook is called after scheme-mode-hook."
202 (interactive)
203 (kill-all-local-variables)
204 (scheme-interaction-mode-initialize)
205 (scheme-mode-variables)
206 (make-local-variable 'xscheme-previous-send)
207 (run-hooks 'scheme-mode-hook 'scheme-interaction-mode-hook))
208
209(defun scheme-interaction-mode-initialize ()
210 (use-local-map scheme-interaction-mode-map)
211 (setq major-mode 'scheme-interaction-mode)
212 (setq mode-name "Scheme Interaction"))
213
214(defun scheme-interaction-mode-commands (keymap)
215 (define-key keymap "\C-c\C-m" 'xscheme-send-current-line)
216 (define-key keymap "\C-c\C-p" 'xscheme-send-proceed)
217 (define-key keymap "\C-c\C-y" 'xscheme-yank-previous-send))
218
219(defvar scheme-interaction-mode-map nil)
220(if (not scheme-interaction-mode-map)
221 (progn
222 (setq scheme-interaction-mode-map (make-keymap))
223 (scheme-mode-commands scheme-interaction-mode-map)
224 (xscheme-interrupt-commands scheme-interaction-mode-map)
225 (xscheme-evaluation-commands scheme-interaction-mode-map)
226 (scheme-interaction-mode-commands scheme-interaction-mode-map)))
227
228(defun xscheme-enter-interaction-mode ()
229 (save-excursion
230 (set-buffer (xscheme-process-buffer))
231 (if (not (eq major-mode 'scheme-interaction-mode))
232 (if (eq major-mode 'scheme-debugger-mode)
233 (scheme-interaction-mode-initialize)
234 (scheme-interaction-mode)))))
235
236(fset 'advertised-xscheme-send-previous-expression
237 'xscheme-send-previous-expression)
238\f
239;;;; Debugger Mode
240
241(defun scheme-debugger-mode ()
242 "Major mode for executing the Scheme debugger.
243Like scheme-mode except that the evaluation commands
244are disabled, and characters that would normally be self inserting are
245sent to the Scheme process instead. Typing ? will show you which
246characters perform useful functions.
247
248Commands:
249\\{scheme-debugger-mode-map}"
250 (error "Illegal entry to scheme-debugger-mode"))
251
252(defun scheme-debugger-mode-initialize ()
253 (use-local-map scheme-debugger-mode-map)
254 (setq major-mode 'scheme-debugger-mode)
255 (setq mode-name "Scheme Debugger"))
256
257(defun scheme-debugger-mode-commands (keymap)
258 (let ((char ? ))
259 (while (< char 127)
260 (define-key keymap (char-to-string char) 'scheme-debugger-self-insert)
261 (setq char (1+ char)))))
262
263(defvar scheme-debugger-mode-map nil)
264(if (not scheme-debugger-mode-map)
265 (progn
266 (setq scheme-debugger-mode-map (make-keymap))
267 (scheme-mode-commands scheme-debugger-mode-map)
268 (xscheme-interrupt-commands scheme-debugger-mode-map)
269 (scheme-debugger-mode-commands scheme-debugger-mode-map)))
270
271(defun scheme-debugger-self-insert ()
272 "Transmit this character to the Scheme process."
273 (interactive)
274 (xscheme-send-char last-command-char))
275
276(defun xscheme-enter-debugger-mode (prompt-string)
277 (save-excursion
278 (set-buffer (xscheme-process-buffer))
279 (if (not (eq major-mode 'scheme-debugger-mode))
280 (progn
281 (if (not (eq major-mode 'scheme-interaction-mode))
282 (scheme-interaction-mode))
283 (scheme-debugger-mode-initialize)))))
284
285(defun xscheme-debugger-mode-p ()
286 (let ((buffer (xscheme-process-buffer)))
287 (and buffer
288 (save-excursion
289 (set-buffer buffer)
290 (eq major-mode 'scheme-debugger-mode)))))
291\f
292;;;; Evaluation Commands
293
294(defun xscheme-send-string (&rest strings)
295 "Send the string arguments to the Scheme process.
296The strings are concatenated and terminated by a newline."
297 (cond ((not (xscheme-process-running-p))
298 (if (yes-or-no-p "The Scheme process has died. Reset it? ")
299 (progn
300 (reset-scheme)
301 (xscheme-wait-for-process)
302 (goto-char (point-max))
303 (apply 'insert-before-markers strings)
304 (xscheme-send-string-1 strings))))
305 ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode"))
306 ((and (not xscheme-allow-pipelined-evaluation)
307 xscheme-running-p)
308 (error "No sends allowed while Scheme running"))
309 (t (xscheme-send-string-1 strings))))
310
311(defun xscheme-send-string-1 (strings)
312 (let ((string (apply 'concat strings)))
313 (xscheme-send-string-2 string)
314 (if (eq major-mode 'scheme-interaction-mode)
315 (setq xscheme-previous-send string))))
316
317(defun xscheme-send-string-2 (string)
318 (let ((process (get-process "scheme")))
319 (send-string process (concat string "\n"))
320 (if (xscheme-process-buffer-current-p)
321 (set-marker (process-mark process) (point)))))
322
323(defun xscheme-yank-previous-send ()
324 "Insert the most recent expression at point."
325 (interactive)
326 (push-mark)
327 (insert xscheme-previous-send))
328
329(defun xscheme-select-process-buffer ()
330 "Select the Scheme process buffer and move to its output point."
331 (interactive)
332 (let ((process (or (get-process "scheme") (error "No scheme process"))))
333 (let ((buffer (or (process-buffer process) (error "No process buffer"))))
334 (let ((window (get-buffer-window buffer)))
335 (if window
336 (select-window window)
337 (switch-to-buffer buffer))
338 (goto-char (process-mark process))))))
339\f
340(defun xscheme-send-region (start end)
341 "Send the current region to the Scheme process.
342The region is sent terminated by a newline."
343 (interactive "r")
344 (if (xscheme-process-buffer-current-p)
345 (progn (goto-char end)
346 (set-marker (process-mark (get-process "scheme")) end)))
347 (xscheme-send-string (buffer-substring start end)))
348
349(defun xscheme-send-definition ()
350 "Send the current definition to the Scheme process.
351If the current line begins with a non-whitespace character,
352parse an expression from the beginning of the line and send that instead."
353 (interactive)
354 (let ((start nil) (end nil))
355 (save-excursion
356 (end-of-defun)
357 (setq end (point))
358 (if (re-search-backward "^\\s(" nil t)
359 (setq start (point))
360 (error "Can't find definition")))
361 (xscheme-send-region start end)))
362
363(defun xscheme-send-next-expression ()
364 "Send the expression to the right of `point' to the Scheme process."
365 (interactive)
366 (let ((start (point)))
367 (xscheme-send-region start (save-excursion (forward-sexp) (point)))))
368
369(defun xscheme-send-previous-expression ()
370 "Send the expression to the left of `point' to the Scheme process."
371 (interactive)
372 (let ((end (point)))
373 (xscheme-send-region (save-excursion (backward-sexp) (point)) end)))
374\f
375(defun xscheme-send-current-line ()
376 "Send the current line to the Scheme process.
377Useful for working with debugging Scheme under adb."
378 (interactive)
379 (let ((line
380 (save-excursion
381 (beginning-of-line)
382 (let ((start (point)))
383 (end-of-line)
384 (buffer-substring start (point))))))
385 (end-of-line)
386 (insert ?\n)
387 (xscheme-send-string-2 line)))
388
389(defun xscheme-send-buffer ()
390 "Send the current buffer to the Scheme process."
391 (interactive)
392 (if (xscheme-process-buffer-current-p)
393 (error "Not allowed to send this buffer's contents to Scheme"))
394 (xscheme-send-region (point-min) (point-max)))
395
396(defun xscheme-send-char (char)
397 "Prompt for a character and send it to the Scheme process."
398 (interactive "cCharacter to send: ")
399 (send-string "scheme" (char-to-string char)))
400\f
401;;;; Interrupts
402
403(defun xscheme-send-breakpoint-interrupt ()
404 "Cause the Scheme process to enter a breakpoint."
405 (interactive)
406 (xscheme-send-interrupt ?b nil))
407
408(defun xscheme-send-proceed ()
409 "Cause the Scheme process to proceed from a breakpoint."
410 (interactive)
411 (send-string "scheme" "(proceed)\n"))
412
413(defun xscheme-send-control-g-interrupt ()
414 "Cause the Scheme processor to halt and flush input.
415Control returns to the top level rep loop."
416 (interactive)
417 (let ((inhibit-quit t))
418 (cond ((not xscheme-control-g-synchronization-p)
419 (interrupt-process "scheme"))
420 (xscheme-control-g-disabled-p
421 (message "Relax..."))
422 (t
423 (setq xscheme-control-g-disabled-p t)
424 (message "Sending C-G interrupt to Scheme...")
425 (interrupt-process "scheme")
426 (send-string "scheme" (char-to-string 0))))))
427
428(defun xscheme-send-control-u-interrupt ()
429 "Cause the Scheme process to halt, returning to previous rep loop."
430 (interactive)
431 (xscheme-send-interrupt ?u t))
432
433(defun xscheme-send-control-x-interrupt ()
434 "Cause the Scheme process to halt, returning to current rep loop."
435 (interactive)
436 (xscheme-send-interrupt ?x t))
437
438;;; This doesn't really work right -- Scheme just gobbles the first
439;;; character in the input. There is no way for us to guarantee that
440;;; the argument to this procedure is the first char unless we put
441;;; some kind of marker in the input stream.
442
443(defun xscheme-send-interrupt (char mark-p)
444 "Send a ^A type interrupt to the Scheme process."
445 (interactive "cInterrupt character to send: ")
446 (quit-process "scheme")
447 (send-string "scheme" (char-to-string char))
448 (if (and mark-p xscheme-control-g-synchronization-p)
449 (send-string "scheme" (char-to-string 0))))
450\f
451;;;; Internal Variables
452
453(defvar xscheme-process-command-line nil
454 "Command used to start the most recent Scheme process.")
455
456(defvar xscheme-previous-send ""
457 "Most recent expression transmitted to the Scheme process.")
458
459(defvar xscheme-process-filter-state 'idle
460 "State of scheme process escape reader state machine:
461idle waiting for an escape sequence
462reading-type received an altmode but nothing else
463reading-string reading prompt string")
464
465(defvar xscheme-running-p nil
466 "This variable, if nil, indicates that the scheme process is
467waiting for input. Otherwise, it is busy evaluating something.")
468
469(defconst xscheme-control-g-synchronization-p t
470 "If non-nil, insert markers in the scheme input stream to indicate when
0a40ffd2
KH
471control-g interrupts were signaled. Do not allow more control-g's to be
472signaled until the scheme process acknowledges receipt.")
a1ad21ca
CH
473
474(defvar xscheme-control-g-disabled-p nil
475 "This variable, if non-nil, indicates that a control-g is being processed
476by the scheme process, so additional control-g's are to be ignored.")
477
478(defvar xscheme-allow-output-p t
479 "This variable, if nil, prevents output from the scheme process
480from being inserted into the process-buffer.")
481
482(defvar xscheme-prompt ""
483 "The current scheme prompt string.")
484
485(defvar xscheme-string-accumulator ""
486 "Accumulator for the string being received from the scheme process.")
487
488(defvar xscheme-string-receiver nil
489 "Procedure to send the string argument from the scheme process.")
490
4bef9110 491(defcustom xscheme-start-hook nil
a1ad21ca 492 "If non-nil, a procedure to call when the Scheme process is started.
4bef9110
SE
493When called, the current buffer will be the Scheme process-buffer."
494 :type 'hook
495 :group 'xscheme)
a1ad21ca
CH
496
497(defvar xscheme-runlight-string nil)
498(defvar xscheme-mode-string nil)
499(defvar xscheme-filter-input nil)
500\f
501;;;; Basic Process Control
502
503(defun xscheme-start-process (command-line)
504 (let ((buffer (get-buffer-create "*scheme*")))
505 (let ((process (get-buffer-process buffer)))
506 (save-excursion
507 (set-buffer buffer)
508 (if (and process (memq (process-status process) '(run stop)))
509 (set-marker (process-mark process) (point-max))
510 (progn (if process (delete-process process))
511 (goto-char (point-max))
512 (scheme-interaction-mode)
513 (if (bobp)
514 (insert-before-markers
515 (substitute-command-keys xscheme-startup-message)))
516 (setq process
517 (let ((process-connection-type nil))
518 (apply 'start-process
519 (cons "scheme"
520 (cons buffer
521 (xscheme-parse-command-line
522 command-line))))))
523 (set-marker (process-mark process) (point-max))
524 (xscheme-process-filter-initialize t)
525 (xscheme-modeline-initialize)
526 (set-process-sentinel process 'xscheme-process-sentinel)
527 (set-process-filter process 'xscheme-process-filter)
528 (run-hooks 'xscheme-start-hook)))))
529 buffer))
530
531(defun xscheme-parse-command-line (string)
532 (setq string (substitute-in-file-name string))
533 (let ((start 0)
534 (result '()))
535 (while start
536 (let ((index (string-match "[ \t]" string start)))
537 (setq start
538 (cond ((not index)
539 (setq result
540 (cons (substring string start)
541 result))
542 nil)
543 ((= index start)
544 (string-match "[^ \t]" string start))
545 (t
546 (setq result
547 (cons (substring string start index)
548 result))
549 (1+ index))))))
550 (nreverse result)))
551\f
552(defun xscheme-wait-for-process ()
553 (sleep-for 2)
554 (while xscheme-running-p
555 (sleep-for 1)))
556
557(defun xscheme-process-running-p ()
558 "True iff there is a Scheme process whose status is `run'."
559 (let ((process (get-process "scheme")))
560 (and process
561 (eq (process-status process) 'run))))
562
563(defun xscheme-process-buffer ()
564 (let ((process (get-process "scheme")))
565 (and process (process-buffer process))))
566
567(defun xscheme-process-buffer-window ()
568 (let ((buffer (xscheme-process-buffer)))
569 (and buffer (get-buffer-window buffer))))
570
571(defun xscheme-process-buffer-current-p ()
572 "True iff the current buffer is the Scheme process buffer."
573 (eq (xscheme-process-buffer) (current-buffer)))
574\f
575;;;; Process Filter
576
577(defun xscheme-process-sentinel (proc reason)
578 (xscheme-process-filter-initialize (eq reason 'run))
579 (if (eq reason 'run)
580 (xscheme-modeline-initialize)
581 (progn
582 (setq scheme-mode-line-process "")
583 (setq xscheme-mode-string "no process")))
584 (if (and (not (memq reason '(run stop)))
585 xscheme-signal-death-message)
586 (progn (beep)
587 (message
588"The Scheme process has died! Do M-x reset-scheme to restart it"))))
589
590(defun xscheme-process-filter-initialize (running-p)
591 (setq xscheme-process-filter-state 'idle)
592 (setq xscheme-running-p running-p)
593 (setq xscheme-control-g-disabled-p nil)
594 (setq xscheme-allow-output-p t)
595 (setq xscheme-prompt "")
596 (setq scheme-mode-line-process '(": " xscheme-runlight-string)))
597
598(defun xscheme-process-filter (proc string)
599 (let ((xscheme-filter-input string))
600 (while xscheme-filter-input
601 (cond ((eq xscheme-process-filter-state 'idle)
602 (let ((start (string-match "\e" xscheme-filter-input)))
603 (if start
604 (progn
605 (xscheme-process-filter-output
606 (substring xscheme-filter-input 0 start))
607 (setq xscheme-filter-input
608 (substring xscheme-filter-input (1+ start)))
609 (setq xscheme-process-filter-state 'reading-type))
610 (let ((string xscheme-filter-input))
611 (setq xscheme-filter-input nil)
612 (xscheme-process-filter-output string)))))
613 ((eq xscheme-process-filter-state 'reading-type)
614 (if (zerop (length xscheme-filter-input))
615 (setq xscheme-filter-input nil)
616 (let ((char (aref xscheme-filter-input 0)))
617 (setq xscheme-filter-input
618 (substring xscheme-filter-input 1))
619 (let ((entry (assoc char xscheme-process-filter-alist)))
620 (if entry
621 (funcall (nth 2 entry) (nth 1 entry))
622 (progn
623 (xscheme-process-filter-output ?\e char)
624 (setq xscheme-process-filter-state 'idle)))))))
625 ((eq xscheme-process-filter-state 'reading-string)
626 (let ((start (string-match "\e" xscheme-filter-input)))
627 (if start
628 (let ((string
629 (concat xscheme-string-accumulator
630 (substring xscheme-filter-input 0 start))))
631 (setq xscheme-filter-input
632 (substring xscheme-filter-input (1+ start)))
633 (setq xscheme-process-filter-state 'idle)
634 (funcall xscheme-string-receiver string))
635 (progn
636 (setq xscheme-string-accumulator
637 (concat xscheme-string-accumulator
638 xscheme-filter-input))
639 (setq xscheme-filter-input nil)))))
640 (t
641 (error "Scheme process filter -- bad state"))))))
642\f
643;;;; Process Filter Output
644
645(defun xscheme-process-filter-output (&rest args)
646 (if xscheme-allow-output-p
647 (let ((string (apply 'concat args)))
648 (save-excursion
649 (xscheme-goto-output-point)
650 (while (string-match "\\(\007\\|\f\\)" string)
651 (let ((start (match-beginning 0))
652 (end (match-end 0)))
653 (insert-before-markers (substring string 0 start))
654 (if (= ?\f (aref string start))
655 (progn
656 (if (not (bolp))
657 (insert-before-markers ?\n))
658 (insert-before-markers ?\f))
659 (beep))
660 (setq string (substring string (1+ start)))))
661 (insert-before-markers string)))))
662
663(defun xscheme-guarantee-newlines (n)
664 (if xscheme-allow-output-p
665 (save-excursion
666 (xscheme-goto-output-point)
667 (let ((stop nil))
668 (while (and (not stop)
669 (bolp))
670 (setq n (1- n))
671 (if (bobp)
672 (setq stop t)
673 (backward-char))))
674 (xscheme-goto-output-point)
675 (while (> n 0)
676 (insert-before-markers ?\n)
677 (setq n (1- n))))))
678
679(defun xscheme-goto-output-point ()
680 (let ((process (get-process "scheme")))
681 (set-buffer (process-buffer process))
682 (goto-char (process-mark process))))
683
684(defun xscheme-modeline-initialize ()
685 (setq xscheme-runlight-string "")
686 (setq xscheme-mode-string "")
687 (setq mode-line-buffer-identification '("Scheme: " xscheme-mode-string)))
688
689(defun xscheme-set-runlight (runlight)
690 (setq xscheme-runlight-string runlight)
390d1087 691 (force-mode-line-update t))
a1ad21ca
CH
692\f
693;;;; Process Filter Operations
694
695(defvar xscheme-process-filter-alist
696 '((?D xscheme-enter-debugger-mode
697 xscheme-process-filter:string-action)
698 (?E xscheme-eval
699 xscheme-process-filter:string-action)
700 (?P xscheme-set-prompt-variable
701 xscheme-process-filter:string-action)
702 (?R xscheme-enter-interaction-mode
703 xscheme-process-filter:simple-action)
704 (?b xscheme-start-gc
705 xscheme-process-filter:simple-action)
706 (?e xscheme-finish-gc
707 xscheme-process-filter:simple-action)
708 (?f xscheme-exit-input-wait
709 xscheme-process-filter:simple-action)
710 (?g xscheme-enable-control-g
711 xscheme-process-filter:simple-action)
712 (?i xscheme-prompt-for-expression
713 xscheme-process-filter:string-action)
714 (?m xscheme-message
715 xscheme-process-filter:string-action)
716 (?n xscheme-prompt-for-confirmation
717 xscheme-process-filter:string-action)
718 (?o xscheme-output-goto
719 xscheme-process-filter:simple-action)
720 (?p xscheme-set-prompt
721 xscheme-process-filter:string-action)
722 (?s xscheme-enter-input-wait
723 xscheme-process-filter:simple-action)
724 (?v xscheme-write-value
725 xscheme-process-filter:string-action)
726 (?w xscheme-cd
727 xscheme-process-filter:string-action)
728 (?z xscheme-display-process-buffer
729 xscheme-process-filter:simple-action)
730 (?c xscheme-unsolicited-read-char
731 xscheme-process-filter:simple-action))
732 "Table used to decide how to handle process filter commands.
733Value is a list of entries, each entry is a list of three items.
734
735The first item is the character that the process filter dispatches on.
736The second item is the action to be taken, a function.
737The third item is the handler for the entry, a function.
738
739When the process filter sees a command whose character matches a
740particular entry, it calls the handler with two arguments: the action
741and the string containing the rest of the process filter's input
742stream. It is the responsibility of the handler to invoke the action
743with the appropriate arguments, and to reenter the process filter with
744the remaining input.")
745\f
746(defun xscheme-process-filter:simple-action (action)
747 (setq xscheme-process-filter-state 'idle)
748 (funcall action))
749
750(defun xscheme-process-filter:string-action (action)
751 (setq xscheme-string-receiver action)
752 (setq xscheme-string-accumulator "")
753 (setq xscheme-process-filter-state 'reading-string))
754
755(defconst xscheme-runlight:running "run"
756 "The character displayed when the Scheme process is running.")
757
758(defconst xscheme-runlight:input "input"
759 "The character displayed when the Scheme process is waiting for input.")
760
761(defconst xscheme-runlight:gc "gc"
762 "The character displayed when the Scheme process is garbage collecting.")
763
764(defun xscheme-start-gc ()
765 (xscheme-set-runlight xscheme-runlight:gc))
766
767(defun xscheme-finish-gc ()
768 (xscheme-set-runlight
769 (if xscheme-running-p xscheme-runlight:running xscheme-runlight:input)))
770
771(defun xscheme-enter-input-wait ()
772 (xscheme-set-runlight xscheme-runlight:input)
773 (setq xscheme-running-p nil))
774
775(defun xscheme-exit-input-wait ()
776 (xscheme-set-runlight xscheme-runlight:running)
777 (setq xscheme-running-p t))
778
779(defun xscheme-enable-control-g ()
780 (setq xscheme-control-g-disabled-p nil))
781
782(defun xscheme-display-process-buffer ()
783 (let ((window (or (xscheme-process-buffer-window)
784 (display-buffer (xscheme-process-buffer)))))
785 (save-window-excursion
786 (select-window window)
787 (xscheme-goto-output-point)
788 (if (xscheme-debugger-mode-p)
789 (xscheme-enter-interaction-mode)))))
790
791(defun xscheme-unsolicited-read-char ()
792 nil)
793\f
794(defun xscheme-eval (string)
795 (eval (car (read-from-string string))))
796
797(defun xscheme-message (string)
798 (if (not (zerop (length string)))
799 (xscheme-write-message-1 string (format ";%s" string))))
800
801(defun xscheme-write-value (string)
802 (if (zerop (length string))
803 (xscheme-write-message-1 "(no value)" ";No value")
804 (xscheme-write-message-1 string (format ";Value: %s" string))))
805
806(defun xscheme-write-message-1 (message-string output-string)
807 (let* ((process (get-process "scheme"))
808 (window (get-buffer-window (process-buffer process))))
809 (if (or (not window)
810 (not (pos-visible-in-window-p (process-mark process)
811 window)))
812 (message "%s" message-string)))
813 (xscheme-guarantee-newlines 1)
814 (xscheme-process-filter-output output-string))
815
816(defun xscheme-set-prompt-variable (string)
817 (setq xscheme-prompt string))
818
819(defun xscheme-set-prompt (string)
820 (setq xscheme-prompt string)
821 (xscheme-guarantee-newlines 2)
822 (setq xscheme-mode-string (xscheme-coerce-prompt string))
390d1087 823 (force-mode-line-update t))
a1ad21ca
CH
824
825(defun xscheme-output-goto ()
826 (xscheme-goto-output-point)
827 (xscheme-guarantee-newlines 2))
828
829(defun xscheme-coerce-prompt (string)
830 (if (string-match "^[0-9]+ " string)
831 (let ((end (match-end 0)))
832 (concat (substring string 0 end)
833 (let ((prompt (substring string end)))
834 (let ((entry (assoc prompt xscheme-prompt-alist)))
835 (if entry
836 (cdr entry)
837 prompt)))))
838 string))
839
840(defvar xscheme-prompt-alist
841 '(("[Normal REPL]" . "[Evaluator]")
842 ("[Error REPL]" . "[Evaluator]")
843 ("[Breakpoint REPL]" . "[Evaluator]")
844 ("[Debugger REPL]" . "[Evaluator]")
845 ("[Visiting environment]" . "[Evaluator]")
846 ("[Environment Inspector]" . "[Where]"))
847 "An alist which maps the Scheme command interpreter type to a print string.")
848
849(defun xscheme-cd (directory-string)
850 (save-excursion
851 (set-buffer (xscheme-process-buffer))
852 (cd directory-string)))
853\f
854(defun xscheme-prompt-for-confirmation (prompt-string)
855 (xscheme-send-char (if (y-or-n-p prompt-string) ?y ?n)))
856
857(defun xscheme-prompt-for-expression (prompt-string)
858 (xscheme-send-string-2
859 (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map)))
860
861(defvar xscheme-prompt-for-expression-map nil)
862(if (not xscheme-prompt-for-expression-map)
863 (progn
864 (setq xscheme-prompt-for-expression-map
865 (copy-keymap minibuffer-local-map))
866 (substitute-key-definition 'exit-minibuffer
867 'xscheme-prompt-for-expression-exit
868 xscheme-prompt-for-expression-map)))
869
870(defun xscheme-prompt-for-expression-exit ()
871 (interactive)
872 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one)
873 (exit-minibuffer)
874 (error "input must be a single, complete expression")))
875
876(defun xscheme-region-expression-p (start end)
877 (save-excursion
878 (let ((old-syntax-table (syntax-table)))
879 (unwind-protect
880 (progn
881 (set-syntax-table scheme-mode-syntax-table)
882 (let ((state (parse-partial-sexp start end)))
883 (and (zerop (car state)) ;depth = 0
884 (nth 2 state) ;last-sexp exists, i.e. >= 1 sexps
885 (let ((state (parse-partial-sexp start (nth 2 state))))
886 (if (nth 2 state) 'many 'one)))))
887 (set-syntax-table old-syntax-table)))))
76d7458e 888
ff383ac0
RS
889(provide 'xscheme)
890
76d7458e 891;;; xscheme.el ends here