(Frename_file): Undo last change: no need to ifdef away
[bpt/emacs.git] / lisp / progmodes / xscheme.el
CommitLineData
c5c3d778
DL
1;;; xscheme.el --- run MIT Scheme under Emacs
2
eac9c0ef
WL
3;; Copyright (C) 1986, 1987, 1989, 1990, 2001, 2004, 2005
4;; Free Software Foundation, Inc.
c5c3d778
DL
5
6;; Maintainer: FSF
7;; Keywords: languages, lisp
8
9;; This file is part of GNU Emacs.
10
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)
14;; any later version.
15
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.
20
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., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
26;;; Commentary:
27
28;; A major mode for interacting with MIT Scheme.
29;;
30;; Requires MIT Scheme release 5 or later.
31;; Changes to Control-G handler require runtime version 13.85 or later.
32
33;;; Code:
34
35(require 'scheme)
36\f
37(defgroup xscheme nil
38 "Major mode for editing Scheme and interacting with MIT's C-Scheme."
39 :group 'lisp)
40
41(defcustom scheme-band-name nil
42 "*Band loaded by the `run-scheme' command."
43 :type '(choice (const nil) string)
44 :group 'xscheme)
45
46(defcustom scheme-program-arguments nil
47 "*Arguments passed to the Scheme program by the `run-scheme' command."
48 :type '(choice (const nil) string)
49 :group 'xscheme)
50
51(defcustom xscheme-allow-pipelined-evaluation t
52 "If non-nil, an expression may be transmitted while another is evaluating.
53Otherwise, attempting to evaluate an expression before the previous expression
54has finished evaluating will signal an error."
55 :type 'boolean
56 :group 'xscheme)
57
58(defcustom xscheme-startup-message
59 "This is the Scheme process buffer.
60Type \\[advertised-xscheme-send-previous-expression] to evaluate the expression before point.
61Type \\[xscheme-send-control-g-interrupt] to abort evaluation.
62Type \\[describe-mode] for more information.
63
64"
65 "String to insert into Scheme process buffer first time it is started.
66Is processed with `substitute-command-keys' first."
67 :type 'string
68 :group 'xscheme)
69
70(defcustom xscheme-signal-death-message nil
71 "If non-nil, causes a message to be generated when the Scheme process dies."
72 :type 'boolean
73 :group 'xscheme)
74
75(defcustom xscheme-start-hook nil
76 "If non-nil, a procedure to call when the Scheme process is started.
77When called, the current buffer will be the Scheme process-buffer."
78 :type 'hook
79 :group 'xscheme
80 :version "20.3")
81
82(defun xscheme-evaluation-commands (keymap)
83 (define-key keymap "\e\C-x" 'xscheme-send-definition)
84 (define-key keymap "\C-x\C-e" 'advertised-xscheme-send-previous-expression)
85 (define-key keymap "\eo" 'xscheme-send-buffer)
86 (define-key keymap "\ez" 'xscheme-send-definition)
87 (define-key keymap "\e\C-m" 'xscheme-send-previous-expression)
88 (define-key keymap "\e\C-z" 'xscheme-send-region))
89
90(defun xscheme-interrupt-commands (keymap)
91 (define-key keymap "\C-c\C-s" 'xscheme-select-process-buffer)
92 (define-key keymap "\C-c\C-b" 'xscheme-send-breakpoint-interrupt)
93 (define-key keymap "\C-c\C-c" 'xscheme-send-control-g-interrupt)
94 (define-key keymap "\C-c\C-u" 'xscheme-send-control-u-interrupt)
95 (define-key keymap "\C-c\C-x" 'xscheme-send-control-x-interrupt))
96
97(xscheme-evaluation-commands scheme-mode-map)
98(xscheme-interrupt-commands scheme-mode-map)
99\f
100(defun run-scheme (command-line)
101 "Run MIT Scheme in an inferior process.
102Output goes to the buffer `*scheme*'.
103With argument, asks for a command line."
104 (interactive (list (xscheme-read-command-line current-prefix-arg)))
105 (xscheme-start command-line xscheme-process-name xscheme-buffer-name))
106
107(defun xscheme-start (command-line process-name buffer-name)
108 (setq-default xscheme-process-command-line command-line)
109 (switch-to-buffer
110 (xscheme-start-process command-line process-name buffer-name))
111 (make-local-variable 'xscheme-process-command-line)
112 (setq xscheme-process-command-line command-line))
113
114(defun xscheme-read-command-line (arg)
115 (let ((default
116 (or xscheme-process-command-line
117 (xscheme-default-command-line))))
118 (if arg
119 (read-string "Run Scheme: " default)
120 default)))
121
122(defun xscheme-default-command-line ()
123 (concat scheme-program-name " -emacs"
124 (if scheme-program-arguments
125 (concat " " scheme-program-arguments)
126 "")
127 (if scheme-band-name
128 (concat " -band " scheme-band-name)
129 "")))
130
131(defun reset-scheme ()
132 "Reset the Scheme process."
133 (interactive)
134 (let ((process (get-process xscheme-process-name)))
135 (cond ((or (not process)
136 (not (eq (process-status process) 'run))
137 (yes-or-no-p
138"The Scheme process is running, are you SURE you want to reset it? "))
139 (message "Resetting Scheme process...")
140 (if process
141 (progn
142 (kill-process process t)
143 (delete-process process)))
144 (xscheme-start-process xscheme-process-command-line
145 xscheme-process-name
146 xscheme-buffer-name)
147 (message "Resetting Scheme process...done")))))
148\f
149;;;; Multiple Scheme buffer management commands
150
151(defun start-scheme (buffer-name &optional globally)
152 "Choose a scheme interaction buffer, or create a new one."
153 ;; (interactive "BScheme interaction buffer: \nP")
154 (interactive
155 (list (read-buffer "Scheme interaction buffer: "
156 xscheme-buffer-name
157 nil)
158 current-prefix-arg))
159 (let ((buffer (get-buffer-create buffer-name)))
160 (let ((process (get-buffer-process buffer)))
161 (if process
162 (switch-to-buffer buffer)
163 (if (or (not (buffer-file-name buffer))
164 (yes-or-no-p (concat "Buffer "
165 (buffer-name buffer)
166 " contains file "
167 (buffer-file-name buffer)
168 "; start scheme in it? ")))
169 (progn
170 (xscheme-start (xscheme-read-command-line t)
171 buffer-name
172 buffer-name)
173 (if globally
174 (global-set-scheme-interaction-buffer buffer-name)))
175 (message "start-scheme aborted"))))))
176
177(fset 'select-scheme 'start-scheme)
178
179(defun global-set-scheme-interaction-buffer (buffer-name)
180 "Set the default scheme interaction buffer."
181 (interactive
182 (list (read-buffer "Scheme interaction buffer: "
183 xscheme-buffer-name
184 t)))
185 (let ((process-name (verify-xscheme-buffer buffer-name nil)))
186 (setq-default xscheme-buffer-name buffer-name)
187 (setq-default xscheme-process-name process-name)
188 (setq-default xscheme-runlight-string
189 (save-excursion (set-buffer buffer-name)
190 xscheme-runlight-string))
191 (setq-default xscheme-runlight
192 (if (eq (process-status process-name) 'run)
193 default-xscheme-runlight
194 ""))))
195
196(defun local-set-scheme-interaction-buffer (buffer-name)
197 "Set the scheme interaction buffer for the current buffer."
198 (interactive
199 (list (read-buffer "Scheme interaction buffer: "
200 xscheme-buffer-name
201 t)))
202 (let ((process-name (verify-xscheme-buffer buffer-name t)))
203 (make-local-variable 'xscheme-buffer-name)
204 (setq xscheme-buffer-name buffer-name)
205 (make-local-variable 'xscheme-process-name)
206 (setq xscheme-process-name process-name)
207 (make-local-variable 'xscheme-runlight)
208 (setq xscheme-runlight (save-excursion (set-buffer buffer-name)
209 xscheme-runlight))))
210
211(defun local-clear-scheme-interaction-buffer ()
212 "Make the current buffer use the default scheme interaction buffer."
213 (interactive)
214 (if (xscheme-process-buffer-current-p)
215 (error "Cannot change the interaction buffer of an interaction buffer"))
216 (kill-local-variable 'xscheme-buffer-name)
217 (kill-local-variable 'xscheme-process-name)
218 (kill-local-variable 'xscheme-runlight))
219
220(defun verify-xscheme-buffer (buffer-name localp)
221 (if (and localp (xscheme-process-buffer-current-p))
222 (error "Cannot change the interaction buffer of an interaction buffer"))
223 (let* ((buffer (get-buffer buffer-name))
224 (process (and buffer (get-buffer-process buffer))))
225 (cond ((not buffer)
ac72d80b 226 (error "Buffer `%s' does not exist" buffer-name))
c5c3d778 227 ((not process)
ac72d80b 228 (error "Buffer `%s' is not a scheme interaction buffer" buffer-name))
c5c3d778
DL
229 (t
230 (save-excursion
231 (set-buffer buffer)
232 (if (not (xscheme-process-buffer-current-p))
ac72d80b 233 (error "Buffer `%s' is not a scheme interaction buffer"
c5c3d778
DL
234 buffer-name)))
235 (process-name process)))))
236\f
237;;;; Interaction Mode
238
239(defun scheme-interaction-mode (&optional preserve)
240 "Major mode for interacting with an inferior MIT Scheme process.
241Like scheme-mode except that:
242
243\\[advertised-xscheme-send-previous-expression] sends the expression before point to the Scheme process as input
244\\[xscheme-yank-pop] yanks an expression previously sent to Scheme
245\\[xscheme-yank-push] yanks an expression more recently sent to Scheme
246
247All output from the Scheme process is written in the Scheme process
248buffer, which is initially named \"*scheme*\". The result of
249evaluating a Scheme expression is also printed in the process buffer,
250preceded by the string \";Value: \" to highlight it. If the process
251buffer is not visible at that time, the value will also be displayed
252in the minibuffer. If an error occurs, the process buffer will
253automatically pop up to show you the error message.
254
255While the Scheme process is running, the modelines of all buffers in
256scheme-mode are modified to show the state of the process. The
257possible states and their meanings are:
258
259input waiting for input
260run evaluating
261gc garbage collecting
262
263The process buffer's modeline contains additional information where
264the buffer's name is normally displayed: the command interpreter level
265and type.
266
267Scheme maintains a stack of command interpreters. Every time an error
268or breakpoint occurs, the current command interpreter is pushed on the
269command interpreter stack, and a new command interpreter is started.
270One example of why this is done is so that an error that occurs while
271you are debugging another error will not destroy the state of the
272initial error, allowing you to return to it after the second error has
273been fixed.
274
275The command interpreter level indicates how many interpreters are in
276the command interpreter stack. It is initially set to one, and it is
277incremented every time that stack is pushed, and decremented every
278time it is popped. The following commands are useful for manipulating
279the command interpreter stack:
280
281\\[xscheme-send-breakpoint-interrupt] pushes the stack once
282\\[xscheme-send-control-u-interrupt] pops the stack once
283\\[xscheme-send-control-g-interrupt] pops everything off
284\\[xscheme-send-control-x-interrupt] aborts evaluation, doesn't affect stack
285
286Some possible command interpreter types and their meanings are:
287
288\[Evaluator] read-eval-print loop for evaluating expressions
289\[Debugger] single character commands for debugging errors
290\[Where] single character commands for examining environments
291
292Starting with release 6.2 of Scheme, the latter two types of command
293interpreters will change the major mode of the Scheme process buffer
294to scheme-debugger-mode , in which the evaluation commands are
295disabled, and the keys which normally self insert instead send
296themselves to the Scheme process. The command character ? will list
297the available commands.
298
299For older releases of Scheme, the major mode will be be
300scheme-interaction-mode , and the command characters must be sent as
301if they were expressions.
302
303Commands:
304Delete converts tabs to spaces as it moves back.
305Blank lines separate paragraphs. Semicolons start comments.
306\\{scheme-interaction-mode-map}
307
308Entry to this mode calls the value of scheme-interaction-mode-hook
309with no args, if that value is non-nil.
310 Likewise with the value of scheme-mode-hook.
311 scheme-interaction-mode-hook is called after scheme-mode-hook."
312 (interactive "P")
313 (if (not preserve)
314 (let ((previous-mode major-mode))
315 (kill-all-local-variables)
316 (make-local-variable 'xscheme-previous-mode)
317 (make-local-variable 'xscheme-buffer-name)
318 (make-local-variable 'xscheme-process-name)
319 (make-local-variable 'xscheme-previous-process-state)
320 (make-local-variable 'xscheme-runlight-string)
321 (make-local-variable 'xscheme-runlight)
322 (make-local-variable 'xscheme-last-input-end)
323 (setq xscheme-previous-mode previous-mode)
324 (let ((buffer (current-buffer)))
325 (setq xscheme-buffer-name (buffer-name buffer))
326 (setq xscheme-last-input-end (make-marker))
327 (let ((process (get-buffer-process buffer)))
328 (if process
329 (progn
330 (setq xscheme-process-name (process-name process))
331 (setq xscheme-previous-process-state
332 (cons (process-filter process)
333 (process-sentinel process)))
334 (xscheme-process-filter-initialize t)
335 (xscheme-modeline-initialize xscheme-buffer-name)
336 (set-process-sentinel process 'xscheme-process-sentinel)
337 (set-process-filter process 'xscheme-process-filter))
338 (setq xscheme-previous-process-state (cons nil nil)))))))
339 (scheme-interaction-mode-initialize)
340 (scheme-mode-variables)
9a969196 341 (run-mode-hooks 'scheme-mode-hook 'scheme-interaction-mode-hook))
c5c3d778
DL
342
343(defun exit-scheme-interaction-mode ()
344 "Take buffer out of scheme interaction mode"
345 (interactive)
346 (if (not (eq major-mode 'scheme-interaction-mode))
347 (error "Buffer not in scheme interaction mode"))
348 (let ((previous-state xscheme-previous-process-state))
349 (funcall xscheme-previous-mode)
350 (let ((process (get-buffer-process (current-buffer))))
351 (if process
352 (progn
353 (if (eq (process-filter process) 'xscheme-process-filter)
354 (set-process-filter process (car previous-state)))
355 (if (eq (process-sentinel process) 'xscheme-process-sentinel)
356 (set-process-sentinel process (cdr previous-state))))))))
357
358(defun scheme-interaction-mode-initialize ()
359 (use-local-map scheme-interaction-mode-map)
360 (setq major-mode 'scheme-interaction-mode)
361 (setq mode-name "Scheme Interaction"))
362
363(defun scheme-interaction-mode-commands (keymap)
364 (let ((entries scheme-interaction-mode-commands-alist))
365 (while entries
366 (define-key keymap
367 (car (car entries))
368 (car (cdr (car entries))))
369 (setq entries (cdr entries)))))
370
371(defvar scheme-interaction-mode-commands-alist nil)
372(setq scheme-interaction-mode-commands-alist
373 (append scheme-interaction-mode-commands-alist
374 '(("\C-c\C-m" xscheme-send-current-line)
375 ("\C-c\C-o" xscheme-delete-output)
376 ("\C-c\C-p" xscheme-send-proceed)
377 ("\C-c\C-y" xscheme-yank)
378 ("\ep" xscheme-yank-pop)
379 ("\en" xscheme-yank-push))))
380
381(defvar scheme-interaction-mode-map nil)
382(if (not scheme-interaction-mode-map)
383 (progn
384 (setq scheme-interaction-mode-map (make-keymap))
385 (scheme-mode-commands scheme-interaction-mode-map)
386 (xscheme-interrupt-commands scheme-interaction-mode-map)
387 (xscheme-evaluation-commands scheme-interaction-mode-map)
388 (scheme-interaction-mode-commands scheme-interaction-mode-map)))
389
390(defun xscheme-enter-interaction-mode ()
391 (save-excursion
392 (set-buffer (xscheme-process-buffer))
393 (if (not (eq major-mode 'scheme-interaction-mode))
394 (if (eq major-mode 'scheme-debugger-mode)
395 (scheme-interaction-mode-initialize)
396 (scheme-interaction-mode t)))))
397
398(fset 'advertised-xscheme-send-previous-expression
399 'xscheme-send-previous-expression)
400\f
401;;;; Debugger Mode
402
403(defun scheme-debugger-mode ()
404 "Major mode for executing the Scheme debugger.
405Like scheme-mode except that the evaluation commands
406are disabled, and characters that would normally be self inserting are
407sent to the Scheme process instead. Typing ? will show you which
408characters perform useful functions.
409
410Commands:
411\\{scheme-debugger-mode-map}"
eac9c0ef 412 (error "Invalid entry to scheme-debugger-mode"))
c5c3d778
DL
413
414(defun scheme-debugger-mode-initialize ()
415 (use-local-map scheme-debugger-mode-map)
416 (setq major-mode 'scheme-debugger-mode)
417 (setq mode-name "Scheme Debugger"))
418
419(defun scheme-debugger-mode-commands (keymap)
420 (let ((char ? ))
421 (while (< char 127)
422 (define-key keymap (char-to-string char) 'scheme-debugger-self-insert)
423 (setq char (1+ char)))))
424
425(defvar scheme-debugger-mode-map nil)
426(if (not scheme-debugger-mode-map)
427 (progn
428 (setq scheme-debugger-mode-map (make-keymap))
429 (scheme-mode-commands scheme-debugger-mode-map)
430 (xscheme-interrupt-commands scheme-debugger-mode-map)
431 (scheme-debugger-mode-commands scheme-debugger-mode-map)))
432
433(defun scheme-debugger-self-insert ()
434 "Transmit this character to the Scheme process."
435 (interactive)
436 (xscheme-send-char last-command-char))
437
438(defun xscheme-enter-debugger-mode (prompt-string)
439 (save-excursion
440 (set-buffer (xscheme-process-buffer))
441 (if (not (eq major-mode 'scheme-debugger-mode))
442 (progn
443 (if (not (eq major-mode 'scheme-interaction-mode))
444 (scheme-interaction-mode t))
445 (scheme-debugger-mode-initialize)))))
446
447(defun xscheme-debugger-mode-p ()
448 (let ((buffer (xscheme-process-buffer)))
449 (and buffer
450 (save-excursion
451 (set-buffer buffer)
452 (eq major-mode 'scheme-debugger-mode)))))
453\f
454;;;; Evaluation Commands
455
456(defun xscheme-send-string (&rest strings)
457 "Send the string arguments to the Scheme process.
458The strings are concatenated and terminated by a newline."
459 (cond ((not (xscheme-process-running-p))
460 (if (yes-or-no-p "The Scheme process has died. Reset it? ")
461 (progn
462 (reset-scheme)
463 (xscheme-wait-for-process)
464 (xscheme-send-string-1 strings))))
465 ((xscheme-debugger-mode-p) (error "No sends allowed in debugger mode"))
466 ((and (not xscheme-allow-pipelined-evaluation)
467 xscheme-running-p)
468 (error "No sends allowed while Scheme running"))
469 (t (xscheme-send-string-1 strings))))
470
471(defun xscheme-send-string-1 (strings)
472 (let ((string (apply 'concat strings)))
473 (xscheme-send-string-2 string)
474 (if (eq major-mode 'scheme-interaction-mode)
475 (xscheme-insert-expression string))))
476
477(defun xscheme-send-string-2 (string)
478 (let ((process (get-process xscheme-process-name)))
479 (process-send-string process (concat string "\n"))
480 (if (xscheme-process-buffer-current-p)
481 (set-marker (process-mark process) (point)))))
482
483(defun xscheme-select-process-buffer ()
484 "Select the Scheme process buffer and move to its output point."
485 (interactive)
486 (let ((process
487 (or (get-process xscheme-process-name)
488 (error "No scheme process"))))
489 (let ((buffer (or (process-buffer process) (error "No process buffer"))))
490 (let ((window (get-buffer-window buffer)))
491 (if window
492 (select-window window)
493 (switch-to-buffer buffer))
494 (goto-char (process-mark process))))))
495\f
496;;;; Scheme expressions ring
497
498(defun xscheme-insert-expression (string)
499 (setq xscheme-expressions-ring (cons string xscheme-expressions-ring))
500 (if (> (length xscheme-expressions-ring) xscheme-expressions-ring-max)
501 (setcdr (nthcdr (1- xscheme-expressions-ring-max)
502 xscheme-expressions-ring)
503 nil))
504 (setq xscheme-expressions-ring-yank-pointer xscheme-expressions-ring))
505
506(defun xscheme-rotate-yank-pointer (arg)
507 "Rotate the yanking point in the kill ring."
508 (interactive "p")
509 (let ((length (length xscheme-expressions-ring)))
510 (if (zerop length)
511 (error "Scheme expression ring is empty")
512 (setq xscheme-expressions-ring-yank-pointer
513 (let ((index
514 (% (+ arg
515 (- length
516 (length xscheme-expressions-ring-yank-pointer)))
517 length)))
518 (nthcdr (if (< index 0)
519 (+ index length)
520 index)
521 xscheme-expressions-ring))))))
522
523(defun xscheme-yank (&optional arg)
524 "Insert the most recent expression at point.
525With just C-U as argument, same but put point in front (and mark at end).
526With argument n, reinsert the nth most recently sent expression.
527See also the commands \\[xscheme-yank-pop] and \\[xscheme-yank-push]."
528 (interactive "*P")
529 (xscheme-rotate-yank-pointer (if (listp arg) 0
530 (if (eq arg '-) -1
531 (1- arg))))
532 (push-mark (point))
533 (insert (car xscheme-expressions-ring-yank-pointer))
534 (if (consp arg)
535 (exchange-point-and-mark)))
536
537;; Old name, to avoid errors in users' init files.
538(fset 'xscheme-yank-previous-send
539 'xscheme-yank)
540
541(defun xscheme-yank-pop (arg)
542 "Insert or replace a just-yanked expression with an older expression.
543If the previous command was not a yank, it yanks.
544Otherwise, the region contains a stretch of reinserted
545expression. yank-pop deletes that text and inserts in its
546place a different expression.
547
548With no argument, the next older expression is inserted.
549With argument n, the n'th older expression is inserted.
550If n is negative, this is a more recent expression.
551
552The sequence of expressions wraps around, so that after the oldest one
553comes the newest one."
554 (interactive "*p")
555 (setq this-command 'xscheme-yank)
556 (if (not (eq last-command 'xscheme-yank))
557 (progn
558 (xscheme-yank)
559 (setq arg (- arg 1))))
560 (if (not (= arg 0))
561 (let ((before (< (point) (mark))))
562 (delete-region (point) (mark))
563 (xscheme-rotate-yank-pointer arg)
564 (set-mark (point))
565 (insert (car xscheme-expressions-ring-yank-pointer))
566 (if before (exchange-point-and-mark)))))
567
568(defun xscheme-yank-push (arg)
569 "Insert or replace a just-yanked expression with a more recent expression.
570If the previous command was not a yank, it yanks.
571Otherwise, the region contains a stretch of reinserted
572expression. yank-pop deletes that text and inserts in its
573place a different expression.
574
575With no argument, the next more recent expression is inserted.
576With argument n, the n'th more recent expression is inserted.
577If n is negative, a less recent expression is used.
578
579The sequence of expressions wraps around, so that after the oldest one
580comes the newest one."
581 (interactive "*p")
582 (xscheme-yank-pop (- 0 arg)))
583\f
584(defun xscheme-send-region (start end)
585 "Send the current region to the Scheme process.
586The region is sent terminated by a newline."
587 (interactive "r")
588 (if (xscheme-process-buffer-current-p)
589 (progn
590 (goto-char end)
591 (if (not (bolp))
592 (insert-before-markers ?\n))
593 (set-marker (process-mark (get-process xscheme-process-name))
594 (point))
595 (set-marker xscheme-last-input-end (point))))
596 (xscheme-send-string (buffer-substring start end)))
597
598(defun xscheme-send-definition ()
599 "Send the current definition to the Scheme process.
600If the current line begins with a non-whitespace character,
601parse an expression from the beginning of the line and send that instead."
602 (interactive)
603 (let ((start nil) (end nil))
604 (save-excursion
605 (end-of-defun)
606 (setq end (point))
607 (if (re-search-backward "^\\s(" nil t)
608 (setq start (point))
609 (error "Can't find definition")))
610 (xscheme-send-region start end)))
611
612(defun xscheme-send-next-expression ()
613 "Send the expression to the right of `point' to the Scheme process."
614 (interactive)
615 (let ((start (point)))
616 (xscheme-send-region start (save-excursion (forward-sexp) (point)))))
617
618(defun xscheme-send-previous-expression ()
619 "Send the expression to the left of `point' to the Scheme process."
620 (interactive)
621 (let ((end (point)))
622 (xscheme-send-region (save-excursion (backward-sexp) (point)) end)))
623\f
624(defun xscheme-send-current-line ()
625 "Send the current line to the Scheme process.
626Useful for working with debugging Scheme under adb."
627 (interactive)
628 (let ((line
629 (save-excursion
630 (beginning-of-line)
631 (let ((start (point)))
632 (end-of-line)
633 (buffer-substring start (point))))))
634 (end-of-line)
635 (insert ?\n)
636 (xscheme-send-string-2 line)))
637
638(defun xscheme-send-buffer ()
639 "Send the current buffer to the Scheme process."
640 (interactive)
641 (if (xscheme-process-buffer-current-p)
642 (error "Not allowed to send this buffer's contents to Scheme"))
643 (xscheme-send-region (point-min) (point-max)))
644
645(defun xscheme-send-char (char)
646 "Prompt for a character and send it to the Scheme process."
647 (interactive "cCharacter to send: ")
648 (process-send-string xscheme-process-name (char-to-string char)))
649
650(defun xscheme-delete-output ()
651 "Delete all output from interpreter since last input."
652 (interactive)
653 (let ((proc (get-buffer-process (current-buffer))))
654 (save-excursion
655 (goto-char (process-mark proc))
656 (re-search-backward
657 "^;\\(Unspecified return value$\\|Value\\( [0-9]+\\)?: \\|\\(Abort\\|Up\\|Quit\\)!$\\)"
658 xscheme-last-input-end
659 t)
660 (forward-line 0)
661 (if (< (marker-position xscheme-last-input-end) (point))
662 (progn
663 (delete-region xscheme-last-input-end (point))
664 (insert-before-markers "*** output flushed ***\n"))))))
665\f
666;;;; Interrupts
667
668(defun xscheme-send-breakpoint-interrupt ()
669 "Cause the Scheme process to enter a breakpoint."
670 (interactive)
671 (xscheme-send-interrupt ?b nil))
672
673(defun xscheme-send-proceed ()
674 "Cause the Scheme process to proceed from a breakpoint."
675 (interactive)
676 (process-send-string xscheme-process-name "(proceed)\n"))
677
678(defun xscheme-send-control-g-interrupt ()
679 "Cause the Scheme processor to halt and flush input.
680Control returns to the top level rep loop."
681 (interactive)
682 (let ((inhibit-quit t))
683 (cond ((not xscheme-control-g-synchronization-p)
684 (interrupt-process xscheme-process-name))
685 ((save-excursion
686 (set-buffer xscheme-buffer-name)
687 xscheme-control-g-disabled-p)
688 (message "Relax..."))
689 (t
690 (save-excursion
691 (set-buffer xscheme-buffer-name)
692 (setq xscheme-control-g-disabled-p t))
693 (message xscheme-control-g-message-string)
694 (interrupt-process xscheme-process-name)
695 (sleep-for 0.1)
696 (xscheme-send-char 0)))))
697
698(defconst xscheme-control-g-message-string
699 "Sending C-G interrupt to Scheme...")
700
701(defun xscheme-send-control-u-interrupt ()
702 "Cause the Scheme process to halt, returning to previous rep loop."
703 (interactive)
704 (xscheme-send-interrupt ?u t))
705
706(defun xscheme-send-control-x-interrupt ()
707 "Cause the Scheme process to halt, returning to current rep loop."
708 (interactive)
709 (xscheme-send-interrupt ?x t))
710
711;;; This doesn't really work right -- Scheme just gobbles the first
712;;; character in the input. There is no way for us to guarantee that
713;;; the argument to this procedure is the first char unless we put
714;;; some kind of marker in the input stream.
715
716(defun xscheme-send-interrupt (char mark-p)
717 "Send a ^A type interrupt to the Scheme process."
718 (interactive "cInterrupt character to send: ")
719 (quit-process xscheme-process-name)
720 (sleep-for 0.1)
721 (xscheme-send-char char)
722 (if (and mark-p xscheme-control-g-synchronization-p)
723 (xscheme-send-char 0)))
724\f
725;;;; Internal Variables
726
727(defvar xscheme-process-command-line nil
728 "Command used to start the most recent Scheme process.")
729
730(defvar xscheme-process-name "scheme"
731 "Name of xscheme process that we're currently interacting with.")
732
733(defvar xscheme-buffer-name "*scheme*"
734 "Name of xscheme buffer that we're currently interacting with.")
735
736(defvar xscheme-expressions-ring-max 30
737 "*Maximum length of Scheme expressions ring.")
738
739(defvar xscheme-expressions-ring nil
740 "List of expressions recently transmitted to the Scheme process.")
741
742(defvar xscheme-expressions-ring-yank-pointer nil
743 "The tail of the Scheme expressions ring whose car is the last thing yanked.")
744
745(defvar xscheme-last-input-end)
746
747(defvar xscheme-process-filter-state 'idle
748 "State of scheme process escape reader state machine:
749idle waiting for an escape sequence
750reading-type received an altmode but nothing else
751reading-string reading prompt string")
752
753(defvar xscheme-running-p nil
754 "This variable, if nil, indicates that the scheme process is
755waiting for input. Otherwise, it is busy evaluating something.")
756
757(defconst xscheme-control-g-synchronization-p t
758 "If non-nil, insert markers in the scheme input stream to indicate when
759control-g interrupts were signaled. Do not allow more control-g's to be
760signaled until the scheme process acknowledges receipt.")
761
762(defvar xscheme-control-g-disabled-p nil
763 "This variable, if non-nil, indicates that a control-g is being processed
764by the scheme process, so additional control-g's are to be ignored.")
765
766(defvar xscheme-allow-output-p t
767 "This variable, if nil, prevents output from the scheme process
768from being inserted into the process-buffer.")
769
770(defvar xscheme-prompt ""
771 "The current scheme prompt string.")
772
773(defvar xscheme-string-accumulator ""
774 "Accumulator for the string being received from the scheme process.")
775
776(defvar xscheme-string-receiver nil
777 "Procedure to send the string argument from the scheme process.")
778
779(defconst default-xscheme-runlight
780 '(": " xscheme-runlight-string)
781 "Default global (shared) xscheme-runlight modeline format.")
782
783(defvar xscheme-runlight "")
784(defvar xscheme-runlight-string nil)
785(defvar xscheme-mode-string nil)
786(setq-default scheme-mode-line-process
787 '("" xscheme-runlight))
788
789(mapcar 'make-variable-buffer-local
790 '(xscheme-expressions-ring
791 xscheme-expressions-ring-yank-pointer
792 xscheme-process-filter-state
793 xscheme-running-p
794 xscheme-control-g-disabled-p
795 xscheme-allow-output-p
796 xscheme-prompt
797 xscheme-string-accumulator
798 xscheme-mode-string
799 scheme-mode-line-process))
800\f
801;;;; Basic Process Control
802
803(defun xscheme-start-process (command-line the-process the-buffer)
804 (let ((buffer (get-buffer-create the-buffer)))
805 (let ((process (get-buffer-process buffer)))
806 (save-excursion
807 (set-buffer buffer)
808 (if (and process (memq (process-status process) '(run stop)))
809 (set-marker (process-mark process) (point-max))
810 (progn (if process (delete-process process))
811 (goto-char (point-max))
812 (scheme-interaction-mode nil)
813 (setq xscheme-process-name the-process)
814 (if (bobp)
815 (insert-before-markers
816 (substitute-command-keys xscheme-startup-message)))
817 (setq process
818 (let ((process-connection-type nil))
819 (apply 'start-process
820 (cons the-process
821 (cons buffer
822 (xscheme-parse-command-line
823 command-line))))))
824 (if (not (equal (process-name process) the-process))
825 (setq xscheme-process-name (process-name process)))
826 (if (not (equal (buffer-name buffer) the-buffer))
827 (setq xscheme-buffer-name (buffer-name buffer)))
828 (message "Starting process %s in buffer %s"
829 xscheme-process-name
830 xscheme-buffer-name)
831 (set-marker (process-mark process) (point-max))
832 (xscheme-process-filter-initialize t)
833 (xscheme-modeline-initialize xscheme-buffer-name)
834 (set-process-sentinel process 'xscheme-process-sentinel)
835 (set-process-filter process 'xscheme-process-filter)
836 (run-hooks 'xscheme-start-hook)))))
837 buffer))
838
839(defun xscheme-parse-command-line (string)
840 (setq string (substitute-in-file-name string))
841 (let ((start 0)
842 (result '()))
843 (while start
844 (let ((index (string-match "[ \t]" string start)))
845 (setq start
846 (cond ((not index)
847 (setq result
848 (cons (substring string start)
849 result))
850 nil)
851 ((= index start)
852 (string-match "[^ \t]" string start))
853 (t
854 (setq result
855 (cons (substring string start index)
856 result))
857 (1+ index))))))
858 (nreverse result)))
859\f
860(defun xscheme-wait-for-process ()
861 (sleep-for 2)
862 (while xscheme-running-p
863 (sleep-for 1)))
864
865(defun xscheme-process-running-p ()
866 "True iff there is a Scheme process whose status is `run'."
867 (let ((process (get-process xscheme-process-name)))
868 (and process
869 (eq (process-status process) 'run))))
870
871(defun xscheme-process-buffer ()
872 (let ((process (get-process xscheme-process-name)))
873 (and process (process-buffer process))))
874
875(defun xscheme-process-buffer-window ()
876 (let ((buffer (xscheme-process-buffer)))
877 (and buffer (get-buffer-window buffer))))
878
879(defun xscheme-process-buffer-current-p ()
880 "True iff the current buffer is the Scheme process buffer."
881 (eq (xscheme-process-buffer) (current-buffer)))
882\f
883;;;; Process Filter
884
885(defun xscheme-process-sentinel (proc reason)
886 (let* ((buffer (process-buffer proc))
887 (name (buffer-name buffer)))
888 (save-excursion
889 (set-buffer buffer)
890 (xscheme-process-filter-initialize (eq reason 'run))
891 (if (not (eq reason 'run))
892 (progn
893 (setq scheme-mode-line-process "")
894 (setq xscheme-mode-string "no process")
895 (if (equal name (default-value 'xscheme-buffer-name))
896 (setq-default xscheme-runlight ""))))
897 (if (and (not (memq reason '(run stop)))
898 xscheme-signal-death-message)
899 (progn
900 (beep)
901 (message
902"The Scheme process has died! Do M-x reset-scheme to restart it"))))))
903
904(defun xscheme-process-filter-initialize (running-p)
905 (setq xscheme-process-filter-state 'idle)
906 (setq xscheme-running-p running-p)
907 (setq xscheme-control-g-disabled-p nil)
908 (setq xscheme-allow-output-p t)
909 (setq xscheme-prompt "")
910 (if running-p
911 (let ((name (buffer-name (current-buffer))))
912 (setq scheme-mode-line-process '(": " xscheme-runlight-string))
913 (xscheme-modeline-initialize name)
914 (if (equal name (default-value 'xscheme-buffer-name))
915 (setq-default xscheme-runlight default-xscheme-runlight))))
916 (if (or (eq xscheme-runlight default-xscheme-runlight)
917 (equal xscheme-runlight ""))
918 (setq xscheme-runlight (list ": " 'xscheme-buffer-name ": " "?")))
919 (rplaca (nthcdr 3 xscheme-runlight)
920 (if running-p "?" "no process")))
921
922(defun xscheme-process-filter (proc string)
923 (let ((xscheme-filter-input string)
924 (call-noexcursion nil))
925 (while xscheme-filter-input
926 (setq call-noexcursion nil)
927 (save-excursion
928 (set-buffer (process-buffer proc))
929 (cond ((eq xscheme-process-filter-state 'idle)
930 (let ((start (string-match "\e" xscheme-filter-input)))
931 (if start
932 (progn
933 (xscheme-process-filter-output
934 (substring xscheme-filter-input 0 start))
935 (setq xscheme-filter-input
936 (substring xscheme-filter-input (1+ start)))
937 (setq xscheme-process-filter-state 'reading-type))
938 (let ((string xscheme-filter-input))
939 (setq xscheme-filter-input nil)
940 (xscheme-process-filter-output string)))))
941 ((eq xscheme-process-filter-state 'reading-type)
942 (if (zerop (length xscheme-filter-input))
943 (setq xscheme-filter-input nil)
944 (let ((char (aref xscheme-filter-input 0)))
945 (setq xscheme-filter-input
946 (substring xscheme-filter-input 1))
947 (let ((entry (assoc char xscheme-process-filter-alist)))
948 (if entry
949 (funcall (nth 2 entry) (nth 1 entry))
950 (progn
951 (xscheme-process-filter-output ?\e char)
952 (setq xscheme-process-filter-state 'idle)))))))
953 ((eq xscheme-process-filter-state 'reading-string)
954 (let ((start (string-match "\e" xscheme-filter-input)))
955 (if start
956 (let ((string
957 (concat xscheme-string-accumulator
958 (substring xscheme-filter-input 0 start))))
959 (setq xscheme-filter-input
960 (substring xscheme-filter-input (1+ start)))
961 (setq xscheme-process-filter-state 'idle)
962 (if (listp xscheme-string-receiver)
963 (progn
964 (setq xscheme-string-receiver
965 (car xscheme-string-receiver))
966 (setq call-noexcursion string))
967 (funcall xscheme-string-receiver string)))
968 (progn
969 (setq xscheme-string-accumulator
970 (concat xscheme-string-accumulator
971 xscheme-filter-input))
972 (setq xscheme-filter-input nil)))))
973 (t
974 (error "Scheme process filter -- bad state"))))
975 (if call-noexcursion
976 (funcall xscheme-string-receiver call-noexcursion)))))
977\f
978;;;; Process Filter Output
979
980(defun xscheme-process-filter-output (&rest args)
981 (if xscheme-allow-output-p
982 (let ((string (apply 'concat args)))
983 (save-excursion
984 (xscheme-goto-output-point)
985 (let ((old-point (point)))
986 (while (string-match "\\(\007\\|\f\\)" string)
987 (let ((start (match-beginning 0))
988 (end (match-end 0)))
989 (insert-before-markers (substring string 0 start))
990 (if (= ?\f (aref string start))
991 (progn
992 (if (not (bolp))
993 (insert-before-markers ?\n))
994 (insert-before-markers ?\f))
995 (beep))
996 (setq string (substring string (1+ start)))))
997 (insert-before-markers string)
998 (if (and xscheme-last-input-end
999 (equal (marker-position xscheme-last-input-end) (point)))
1000 (set-marker xscheme-last-input-end old-point)))))))
1001
1002(defun xscheme-guarantee-newlines (n)
1003 (if xscheme-allow-output-p
1004 (save-excursion
1005 (xscheme-goto-output-point)
1006 (let ((stop nil))
1007 (while (and (not stop)
1008 (bolp))
1009 (setq n (1- n))
1010 (if (bobp)
1011 (setq stop t)
1012 (backward-char))))
1013 (xscheme-goto-output-point)
1014 (while (> n 0)
1015 (insert-before-markers ?\n)
1016 (setq n (1- n))))))
1017
1018(defun xscheme-goto-output-point ()
1019 (let ((process (get-process xscheme-process-name)))
1020 (set-buffer (process-buffer process))
1021 (goto-char (process-mark process))))
1022
1023(defun xscheme-modeline-initialize (name)
1024 (setq xscheme-runlight-string "")
1025 (if (equal name (default-value 'xscheme-buffer-name))
1026 (setq-default xscheme-runlight-string ""))
1027 (setq xscheme-mode-string "")
1028 (setq mode-line-buffer-identification
1029 (list (concat name ": ")
1030 'xscheme-mode-string)))
1031
1032(defun xscheme-set-runlight (runlight)
1033 (setq xscheme-runlight-string runlight)
1034 (if (equal (buffer-name (current-buffer))
1035 (default-value 'xscheme-buffer-name))
1036 (setq-default xscheme-runlight-string runlight))
1037 (rplaca (nthcdr 3 xscheme-runlight) runlight)
1038 (force-mode-line-update t))
1039\f
1040;;;; Process Filter Operations
1041
1042(defvar xscheme-process-filter-alist
1043 '((?A xscheme-eval
1044 xscheme-process-filter:string-action-noexcursion)
1045 (?D xscheme-enter-debugger-mode
1046 xscheme-process-filter:string-action)
1047 (?E xscheme-eval
1048 xscheme-process-filter:string-action)
1049 (?P xscheme-set-prompt-variable
1050 xscheme-process-filter:string-action)
1051 (?R xscheme-enter-interaction-mode
1052 xscheme-process-filter:simple-action)
1053 (?b xscheme-start-gc
1054 xscheme-process-filter:simple-action)
1055 (?c xscheme-unsolicited-read-char
1056 xscheme-process-filter:simple-action)
1057 (?e xscheme-finish-gc
1058 xscheme-process-filter:simple-action)
1059 (?f xscheme-exit-input-wait
1060 xscheme-process-filter:simple-action)
1061 (?g xscheme-enable-control-g
1062 xscheme-process-filter:simple-action)
1063 (?i xscheme-prompt-for-expression
1064 xscheme-process-filter:string-action)
1065 (?m xscheme-message
1066 xscheme-process-filter:string-action)
1067 (?n xscheme-prompt-for-confirmation
1068 xscheme-process-filter:string-action)
1069 (?o xscheme-output-goto
1070 xscheme-process-filter:simple-action)
1071 (?p xscheme-set-prompt
1072 xscheme-process-filter:string-action)
1073 (?s xscheme-enter-input-wait
1074 xscheme-process-filter:simple-action)
1075 (?v xscheme-write-value
1076 xscheme-process-filter:string-action)
1077 (?w xscheme-cd
1078 xscheme-process-filter:string-action)
1079 (?z xscheme-display-process-buffer
1080 xscheme-process-filter:simple-action))
1081 "Table used to decide how to handle process filter commands.
1082Value is a list of entries, each entry is a list of three items.
1083
1084The first item is the character that the process filter dispatches on.
1085The second item is the action to be taken, a function.
1086The third item is the handler for the entry, a function.
1087
1088When the process filter sees a command whose character matches a
1089particular entry, it calls the handler with two arguments: the action
1090and the string containing the rest of the process filter's input
1091stream. It is the responsibility of the handler to invoke the action
1092with the appropriate arguments, and to reenter the process filter with
1093the remaining input.")
1094\f
1095(defun xscheme-process-filter:simple-action (action)
1096 (setq xscheme-process-filter-state 'idle)
1097 (funcall action))
1098
1099(defun xscheme-process-filter:string-action (action)
1100 (setq xscheme-string-receiver action)
1101 (setq xscheme-string-accumulator "")
1102 (setq xscheme-process-filter-state 'reading-string))
1103
1104(defun xscheme-process-filter:string-action-noexcursion (action)
1105 (xscheme-process-filter:string-action (cons action nil)))
1106
1107(defconst xscheme-runlight:running "run"
1108 "The character displayed when the Scheme process is running.")
1109
1110(defconst xscheme-runlight:input "input"
1111 "The character displayed when the Scheme process is waiting for input.")
1112
1113(defconst xscheme-runlight:gc "gc"
1114 "The character displayed when the Scheme process is garbage collecting.")
1115
1116(defun xscheme-start-gc ()
1117 (xscheme-set-runlight xscheme-runlight:gc))
1118
1119(defun xscheme-finish-gc ()
1120 (xscheme-set-runlight
1121 (if xscheme-running-p xscheme-runlight:running xscheme-runlight:input)))
1122
1123(defun xscheme-enter-input-wait ()
1124 (xscheme-set-runlight xscheme-runlight:input)
1125 (setq xscheme-control-g-disabled-p nil)
1126 (setq xscheme-running-p nil))
1127
1128(defun xscheme-exit-input-wait ()
1129 (xscheme-set-runlight xscheme-runlight:running)
1130 (setq xscheme-running-p t))
1131
1132(defun xscheme-enable-control-g ()
1133 (setq xscheme-control-g-disabled-p nil)
1134 (if (string= (current-message) xscheme-control-g-message-string)
1135 (message nil)))
1136
1137(defun xscheme-display-process-buffer ()
1138 (let ((window (or (xscheme-process-buffer-window)
1139 (display-buffer (xscheme-process-buffer)))))
1140 (save-window-excursion
1141 (select-window window)
1142 (xscheme-goto-output-point)
1143 (if (xscheme-debugger-mode-p)
1144 (xscheme-enter-interaction-mode)))))
1145
1146(defun xscheme-unsolicited-read-char ()
1147 nil)
1148\f
1149(defun xscheme-eval (string)
1150 (eval (car (read-from-string string))))
1151
1152(defun xscheme-message (string)
1153 (if (not (zerop (length string)))
1154 (xscheme-write-message-1 string (format ";%s" string))))
1155
1156(defun xscheme-write-value (string)
1157 (if (zerop (length string))
1158 (xscheme-write-message-1 "(no value)" ";Unspecified return value")
1159 (xscheme-write-message-1 string (format ";Value: %s" string))))
1160
1161(defun xscheme-write-message-1 (message-string output-string)
1162 (let* ((process (get-process xscheme-process-name))
1163 (window (get-buffer-window (process-buffer process))))
1164 (if (or (not window)
1165 (not (pos-visible-in-window-p (process-mark process)
1166 window)))
1167 (message "%s" message-string)))
1168 (xscheme-guarantee-newlines 1)
1169 (xscheme-process-filter-output output-string))
1170
1171(defun xscheme-set-prompt-variable (string)
1172 (setq xscheme-prompt string))
1173
1174(defun xscheme-set-prompt (string)
1175 (setq xscheme-prompt string)
1176 (xscheme-guarantee-newlines 2)
1177 (setq xscheme-mode-string (xscheme-coerce-prompt string))
1178 (force-mode-line-update t))
1179
1180(defun xscheme-output-goto ()
1181 (xscheme-goto-output-point)
1182 (xscheme-guarantee-newlines 2))
1183
1184(defun xscheme-coerce-prompt (string)
1185 (if (string-match "^[0-9]+ \\[[^]]+\\] " string)
1186 (let ((end (match-end 0)))
1187 (xscheme-process-filter-output (substring string end))
1188 (substring string 0 (- end 1)))
1189 string))
1190
1191(defun xscheme-cd (directory-string)
1192 (save-excursion
1193 (set-buffer (xscheme-process-buffer))
1194 (cd directory-string)))
1195\f
1196(defun xscheme-prompt-for-confirmation (prompt-string)
1197 (xscheme-send-char (if (y-or-n-p prompt-string) ?y ?n)))
1198
1199(defun xscheme-prompt-for-expression (prompt-string)
1200 (xscheme-send-string-2
1201 (read-from-minibuffer prompt-string nil xscheme-prompt-for-expression-map)))
1202
1203(defvar xscheme-prompt-for-expression-map nil)
1204(if (not xscheme-prompt-for-expression-map)
1205 (progn
1206 (setq xscheme-prompt-for-expression-map
1207 (copy-keymap minibuffer-local-map))
1208 (substitute-key-definition 'exit-minibuffer
1209 'xscheme-prompt-for-expression-exit
1210 xscheme-prompt-for-expression-map)))
1211
1212(defun xscheme-prompt-for-expression-exit ()
1213 (interactive)
1214 (if (eq (xscheme-region-expression-p (point-min) (point-max)) 'one)
1215 (exit-minibuffer)
1216 (error "input must be a single, complete expression")))
1217
1218(defun xscheme-region-expression-p (start end)
1219 (save-excursion
1220 (let ((old-syntax-table (syntax-table)))
1221 (unwind-protect
1222 (progn
1223 (set-syntax-table scheme-mode-syntax-table)
1224 (let ((state (parse-partial-sexp start end)))
1225 (and (zerop (car state)) ;depth = 0
1226 (nth 2 state) ;last-sexp exists, i.e. >= 1 sexps
1227 (let ((state (parse-partial-sexp start (nth 2 state))))
1228 (if (nth 2 state) 'many 'one)))))
1229 (set-syntax-table old-syntax-table)))))
1230
1231(provide 'xscheme)
1232
ab5796a9 1233;;; arch-tag: cfc14adc-2917-409e-ad16-432e8d0017de
c5c3d778 1234;;; xscheme.el ends here