home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / xscheme.el < prev    next >
Encoding:
Text File  |  1992-08-18  |  30.0 KB  |  863 lines

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