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