PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/old-archive/modes/cmushell/cmuscheme.el

https://github.com/emacsmirror/ohio-archive
Emacs Lisp | 428 lines | 265 code | 50 blank | 113 comment | 18 complexity | c2f6df4b57c53e43a058d60dd452a75a MD5 | raw file
  1. ;;; cmuscheme.el -- Scheme process in a buffer. Adapted from tea.el.
  2. ;;; Copyright Olin Shivers (1988)
  3. ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
  4. ;;; notice appearing here to the effect that you may use this code any
  5. ;;; way you like, as long as you don't charge money for it, remove this
  6. ;;; notice, or hold me liable for its results.
  7. ;;;
  8. ;;; This is a customisation of comint-mode (see comint.el)
  9. ;;;
  10. ;;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  11. ;;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  12. ;;; 8/88
  13. ;;;
  14. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  15. ;;; merge them into the master source.
  16. ;;;
  17. ;;; The changelog is at the end of this file.
  18. ;;;
  19. ;;; NOTE: MIT Cscheme, when invoked with the -emacs flag, has a special user
  20. ;;; interface that communicates process state back to the superior emacs by
  21. ;;; outputting special control sequences. The gnumacs package, xscheme.el, has
  22. ;;; lots and lots of special purpose code to read these control sequences, and
  23. ;;; so is very tightly integrated with the cscheme process. The cscheme
  24. ;;; interrupt handler and debugger read single character commands in cbreak
  25. ;;; mode; when this happens, xscheme.el switches to special keymaps that bind
  26. ;;; the single letter command keys to emacs functions that directly send the
  27. ;;; character to the scheme process. Cmuscheme mode does *not* provide this
  28. ;;; functionality. If you are a cscheme user, you may prefer to use the
  29. ;;; xscheme.el/cscheme -emacs interaction.
  30. ;;;
  31. ;;; Here's a summary of the pros and cons, as I see them.
  32. ;;; xscheme: Tightly integrated with inferior cscheme process! A few commands
  33. ;;; not in cmuscheme. But. Integration is a bit of a hack. Input
  34. ;;; history only keeps the immediately prior input. Bizarre
  35. ;;; keybindings.
  36. ;;;
  37. ;;; cmuscheme: Not tightly integrated with inferior cscheme process. But.
  38. ;;; Carefully integrated functionality with the entire suite of
  39. ;;; comint-derived CMU process modes. Keybindings reminiscent of
  40. ;;; Zwei and Hemlock. Good input history. A few commands not in
  41. ;;; xscheme.
  42. ;;;
  43. ;;; It's a tradeoff. Pay your money; take your choice. If you use a Scheme
  44. ;;; that isn't Cscheme, of course, there isn't a choice. Xscheme.el is *very*
  45. ;;; Cscheme-specific; you must use cmuscheme.el. Interested parties are
  46. ;;; invited to port xscheme functionality on top of comint mode...
  47. ;; YOUR .EMACS FILE
  48. ;;=============================================================================
  49. ;; Some suggestions for your .emacs file.
  50. ;;
  51. ;; ; If cmuscheme lives in some non-standard directory, you must tell emacs
  52. ;; ; where to get it. This may or may not be necessary.
  53. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  54. ;;
  55. ;; ; Autoload run-scheme from file cmuscheme.el
  56. ;; (autoload 'run-scheme "cmuscheme"
  57. ;; "Run an inferior Scheme process."
  58. ;; t)
  59. ;;
  60. ;; ; Files ending in ".scm" are Scheme source,
  61. ;; ; so put their buffers in scheme-mode.
  62. ;; (setq auto-mode-alist
  63. ;; (cons '("\\.scm$" . scheme-mode)
  64. ;; auto-mode-alist))
  65. ;;
  66. ;; ; Define C-c C-t to run my favorite command in inferior scheme mode:
  67. ;; (setq cmuscheme-load-hook
  68. ;; '((lambda () (define-key inferior-scheme-mode-map "\C-c\C-t"
  69. ;; 'favorite-cmd))))
  70. ;;;
  71. ;;; Unfortunately, scheme.el defines run-scheme to autoload from xscheme.el.
  72. ;;; This will womp your declaration to autoload run-scheme from cmuscheme.el
  73. ;;; if you haven't loaded cmuscheme in before scheme. Three fixes:
  74. ;;; - Put the autoload on your scheme mode hook and in your .emacs toplevel:
  75. ;;; (setq scheme-mode-hook
  76. ;;; '((lambda () (autoload 'run-scheme "cmuscheme"
  77. ;;; "Run an inferior Scheme" t))))
  78. ;;; (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  79. ;;; Now when scheme.el autoloads, it will restore the run-scheme autoload.
  80. ;;; - Load cmuscheme.el in your .emacs: (load-library 'cmuscheme)
  81. ;;; - Change autoload declaration in scheme.el to point to cmuscheme.el:
  82. ;;; (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  83. ;;; *or* just delete the autoload declaration from scheme.el altogether,
  84. ;;; which will allow the autoload in your .emacs to have its say.
  85. (provide 'cmuscheme)
  86. (require 'scheme)
  87. (require 'comint)
  88. ;;; INFERIOR SCHEME MODE STUFF
  89. ;;;============================================================================
  90. (defvar inferior-scheme-mode-hook nil
  91. "*Hook for customising inferior-scheme mode.")
  92. (defvar inferior-scheme-mode-map nil)
  93. (cond ((not inferior-scheme-mode-map)
  94. (setq inferior-scheme-mode-map
  95. (full-copy-sparse-keymap comint-mode-map))
  96. (define-key inferior-scheme-mode-map "\M-\C-x" ;gnu convention
  97. 'scheme-send-definition)
  98. (define-key inferior-scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp)
  99. (define-key inferior-scheme-mode-map "\C-cl" 'scheme-load-file)
  100. (define-key inferior-scheme-mode-map "\C-ck" 'scheme-compile-file)
  101. (scheme-mode-commands inferior-scheme-mode-map)))
  102. ;; Install the process communication commands in the scheme-mode keymap.
  103. (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
  104. (define-key scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp);gnu convention
  105. (define-key scheme-mode-map "\C-ce" 'scheme-send-definition)
  106. (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition-and-go)
  107. (define-key scheme-mode-map "\C-cr" 'scheme-send-region)
  108. (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region-and-go)
  109. (define-key scheme-mode-map "\C-cc" 'scheme-compile-definition)
  110. (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
  111. (define-key scheme-mode-map "\C-cz" 'switch-to-scheme)
  112. (define-key scheme-mode-map "\C-cl" 'scheme-load-file)
  113. (define-key scheme-mode-map "\C-ck" 'scheme-compile-file) ;k for "kompile"
  114. (defun inferior-scheme-mode ()
  115. "Major mode for interacting with an inferior Scheme process.
  116. The following commands are available:
  117. \\{inferior-scheme-mode-map}
  118. A Scheme process can be fired up with M-x run-scheme.
  119. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  120. inferior-scheme-mode-hook (in that order).
  121. You can send text to the inferior Scheme process from other buffers containing
  122. Scheme source.
  123. switch-to-scheme switches the current buffer to the Scheme process buffer.
  124. scheme-send-definition sends the current definition to the Scheme process.
  125. scheme-compile-definition compiles the current definition.
  126. scheme-send-region sends the current region to the Scheme process.
  127. scheme-compile-region compiles the current region.
  128. scheme-send-definition-and-go, scheme-compile-definition-and-go,
  129. scheme-send-region-and-go, and scheme-compile-region-and-go
  130. switch to the Scheme process buffer after sending their text.
  131. For information on running multiple processes in multiple buffers, see
  132. documentation for variable scheme-buffer.
  133. Commands:
  134. Return after the end of the process' output sends the text from the
  135. end of process to point.
  136. Return before the end of the process' output copies the sexp ending at point
  137. to the end of the process' output, and sends it.
  138. Delete converts tabs to spaces as it moves back.
  139. Tab indents for Scheme; with argument, shifts rest
  140. of expression rigidly with the current line.
  141. C-M-q does Tab on each line starting within following expression.
  142. Paragraphs are separated only by blank lines. Semicolons start comments.
  143. If you accidentally suspend your process, use \\[comint-continue-subjob]
  144. to continue it."
  145. (interactive)
  146. (comint-mode)
  147. ;; Customise in inferior-scheme-mode-hook
  148. (setq comint-prompt-regexp "^[^>]*>+ *") ; OK for cscheme, oaklisp, T,...
  149. (scheme-mode-variables)
  150. (setq major-mode 'inferior-scheme-mode)
  151. (setq mode-name "Inferior Scheme")
  152. (setq mode-line-process '(": %s"))
  153. (use-local-map inferior-scheme-mode-map)
  154. (setq comint-input-filter (function scheme-input-filter))
  155. (setq comint-input-sentinel (function ignore))
  156. (setq comint-get-old-input (function scheme-get-old-input))
  157. (run-hooks 'inferior-scheme-mode-hook))
  158. (defun scheme-input-filter (str)
  159. "Don't save anything matching inferior-scheme-filter-regexp"
  160. (not (string-match inferior-scheme-filter-regexp str)))
  161. (defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  162. "*Input matching this regexp are not saved on the history list.
  163. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  164. (defun scheme-get-old-input ()
  165. "Snarf the sexp ending at point"
  166. (save-excursion
  167. (let ((end (point)))
  168. (backward-sexp)
  169. (buffer-substring (point) end))))
  170. (defun scheme-args-to-list (string)
  171. (let ((where (string-match "[ \t]" string)))
  172. (cond ((null where) (list string))
  173. ((not (= where 0))
  174. (cons (substring string 0 where)
  175. (scheme-args-to-list (substring string (+ 1 where)
  176. (length string)))))
  177. (t (let ((pos (string-match "[^ \t]" string)))
  178. (if (null pos)
  179. nil
  180. (scheme-args-to-list (substring string pos
  181. (length string)))))))))
  182. (defvar scheme-program-name "scheme"
  183. "*Program invoked by the run-scheme command")
  184. ;;; Obsolete
  185. (defun scheme (&rest foo)
  186. "Use run-scheme"
  187. (interactive)
  188. (message "Use run-scheme")
  189. (ding))
  190. (defun run-scheme (cmd)
  191. "Run an inferior Scheme process, input and output via buffer *scheme*.
  192. If there is a process already running in *scheme*, just switch to that buffer.
  193. With argument, allows you to edit the command line (default is value
  194. of scheme-program-name). Runs the hooks from inferior-scheme-mode-hook
  195. \(after the comint-mode-hook is run).
  196. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  197. (interactive (list (if current-prefix-arg
  198. (read-string "Run Scheme: " scheme-program-name)
  199. scheme-program-name)))
  200. (if (not (comint-check-proc "*scheme*"))
  201. (let ((cmdlist (scheme-args-to-list cmd)))
  202. (set-buffer (apply 'make-comint "scheme" (car cmdlist)
  203. nil (cdr cmdlist)))
  204. (inferior-scheme-mode)))
  205. (setq scheme-buffer "*scheme*")
  206. (switch-to-buffer "*scheme*"))
  207. (defun scheme-send-region (start end)
  208. "Send the current region to the inferior Scheme process."
  209. (interactive "r")
  210. (comint-send-region (scheme-proc) start end)
  211. (comint-send-string (scheme-proc) "\n"))
  212. (defun scheme-send-definition ()
  213. "Send the current definition to the inferior Scheme process."
  214. (interactive)
  215. (save-excursion
  216. (end-of-defun)
  217. (let ((end (point)))
  218. (beginning-of-defun)
  219. (scheme-send-region (point) end))))
  220. (defun scheme-send-last-sexp ()
  221. "Send the previous sexp to the inferior Scheme process."
  222. (interactive)
  223. (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
  224. (defvar scheme-compile-exp-command "(compile '%s)"
  225. "*Template for issuing commands to compile arbitrary Scheme expressions.")
  226. (defun scheme-compile-region (start end)
  227. "Compile the current region in the inferior Scheme process
  228. \(A BEGIN is wrapped around the region: (BEGIN <region>))"
  229. (interactive "r")
  230. (comint-send-string (scheme-proc) (format scheme-compile-exp-command
  231. (format "(begin %s)"
  232. (buffer-substring start end))))
  233. (comint-send-string (scheme-proc) "\n"))
  234. (defun scheme-compile-definition ()
  235. "Compile the current definition in the inferior Scheme process."
  236. (interactive)
  237. (save-excursion
  238. (end-of-defun)
  239. (let ((end (point)))
  240. (beginning-of-defun)
  241. (scheme-compile-region (point) end))))
  242. (defun switch-to-scheme (eob-p)
  243. "Switch to the scheme process buffer.
  244. With argument, positions cursor at end of buffer."
  245. (interactive "P")
  246. (if (get-buffer scheme-buffer)
  247. (pop-to-buffer scheme-buffer)
  248. (error "No current process buffer. See variable scheme-buffer."))
  249. (cond (eob-p
  250. (push-mark)
  251. (goto-char (point-max)))))
  252. (defun scheme-send-region-and-go (start end)
  253. "Send the current region to the inferior Scheme process,
  254. and switch to the process buffer."
  255. (interactive "r")
  256. (scheme-send-region start end)
  257. (switch-to-scheme t))
  258. (defun scheme-send-definition-and-go ()
  259. "Send the current definition to the inferior Scheme,
  260. and switch to the process buffer."
  261. (interactive)
  262. (scheme-send-definition)
  263. (switch-to-scheme t))
  264. (defun scheme-compile-definition-and-go ()
  265. "Compile the current definition in the inferior Scheme,
  266. and switch to the process buffer."
  267. (interactive)
  268. (scheme-compile-definition)
  269. (switch-to-scheme t))
  270. (defun scheme-compile-region-and-go (start end)
  271. "Compile the current region in the inferior Scheme,
  272. and switch to the process buffer."
  273. (interactive "r")
  274. (scheme-compile-region start end)
  275. (switch-to-scheme t))
  276. (defvar scheme-source-modes '(scheme-mode)
  277. "*Used to determine if a buffer contains Scheme source code.
  278. If it's loaded into a buffer that is in one of these major modes, it's
  279. considered a scheme source file by scheme-load-file and scheme-compile-file.
  280. Used by these commands to determine defaults.")
  281. (defvar scheme-prev-l/c-dir/file nil
  282. "Caches the (directory . file) pair used in the last scheme-load-file or
  283. scheme-compile-file command. Used for determining the default in the
  284. next one.")
  285. (defun scheme-load-file (file-name)
  286. "Load a Scheme file into the inferior Scheme process."
  287. (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
  288. scheme-source-modes t)) ; T because LOAD
  289. ; needs an exact name
  290. (comint-check-source file-name) ; Check to see if buffer needs saved.
  291. (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
  292. (file-name-nondirectory file-name)))
  293. (comint-send-string (scheme-proc) (concat "(load \""
  294. file-name
  295. "\"\)\n"))
  296. (switch-to-scheme t))
  297. (defun scheme-compile-file (file-name)
  298. "Compile a Scheme file in the inferior Scheme process."
  299. (interactive (comint-get-source "Compile Scheme file: "
  300. scheme-prev-l/c-dir/file
  301. scheme-source-modes
  302. nil)) ; NIL because COMPILE doesn't
  303. ; need an exact name.
  304. (comint-check-source file-name) ; Check to see if buffer needs saved.
  305. (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
  306. (file-name-nondirectory file-name)))
  307. (comint-send-string (scheme-proc) (concat "(compile-file \""
  308. file-name
  309. "\"\)\n"))
  310. (switch-to-scheme t))
  311. (defvar scheme-buffer nil "*The current scheme process buffer.
  312. MULTIPLE PROCESS SUPPORT
  313. ===========================================================================
  314. Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
  315. processes. To run multiple Scheme processes, you start the first up with
  316. \\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
  317. with \\[rename-buffer]. You may now start up a new process with another
  318. \\[run-scheme]. It will be in a new buffer, named *scheme*. You can
  319. switch between the different process buffers with \\[switch-to-buffer].
  320. Commands that send text from source buffers to Scheme processes --
  321. like scheme-send-definition or scheme-compile-region -- have to choose a
  322. process to send to, when you have more than one Scheme process around. This
  323. is determined by the global variable scheme-buffer. Suppose you
  324. have three inferior Schemes running:
  325. Buffer Process
  326. foo scheme
  327. bar scheme<2>
  328. *scheme* scheme<3>
  329. If you do a \\[scheme-send-definition-and-go] command on some Scheme source
  330. code, what process do you send it to?
  331. - If you're in a process buffer (foo, bar, or *scheme*),
  332. you send it to that process.
  333. - If you're in some other buffer (e.g., a source file), you
  334. send it to the process attached to buffer scheme-buffer.
  335. This process selection is performed by function scheme-proc.
  336. Whenever \\[run-scheme] fires up a new process, it resets scheme-buffer
  337. to be the new process's buffer. If you only run one process, this will
  338. do the right thing. If you run multiple processes, you can change
  339. scheme-buffer to another process buffer with \\[set-variable].
  340. More sophisticated approaches are, of course, possible. If you find youself
  341. needing to switch back and forth between multiple processes frequently,
  342. you may wish to consider ilisp.el, a larger, more sophisticated package
  343. for running inferior Lisp and Scheme processes. The approach taken here is
  344. for a minimal, simple implementation. Feel free to extend it.")
  345. (defun scheme-proc ()
  346. "Returns the current scheme process. See variable scheme-buffer."
  347. (let ((proc (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
  348. (current-buffer)
  349. scheme-buffer))))
  350. (or proc
  351. (error "No current process. See variable scheme-buffer"))))
  352. ;;; Do the user's customisation...
  353. (defvar cmuscheme-load-hook nil
  354. "This hook is run when cmuscheme is loaded in.
  355. This is a good place to put keybindings.")
  356. (run-hooks 'cmuscheme-load-hook)
  357. ;;; CHANGE LOG
  358. ;;; ===========================================================================
  359. ;;; 8/88 Olin
  360. ;;; Created.
  361. ;;;
  362. ;;; 2/15/89 Olin
  363. ;;; Removed -emacs flag from process invocation. It's only useful for
  364. ;;; cscheme, and makes cscheme assume it's running under xscheme.el,
  365. ;;; which messes things up royally. A bug.
  366. ;;;
  367. ;;; 5/22/90 Olin
  368. ;;; - Upgraded to use comint-send-string and comint-send-region.
  369. ;;; - run-scheme now offers to let you edit the command line if
  370. ;;; you invoke it with a prefix-arg. M-x scheme is redundant, and
  371. ;;; has been removed.
  372. ;;; - Explicit references to process "scheme" have been replaced with
  373. ;;; (scheme-proc). This allows better handling of multiple process bufs.
  374. ;;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
  375. ;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
  376. ;;; and friends, but interested hackers might find a useful application
  377. ;;; of this facility.