xscheme.el 43 KB

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