xscheme.el 42 KB

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