*** empty log message ***
[emacs.git] / lisp / cmuscheme.el
blobb817b42e6626ddda9c2d3672e33e1612e29276e7
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...
48 ;; YOUR .EMACS FILE
49 ;;=============================================================================
50 ;; Some suggestions for your .emacs file.
52 ;; ; If cmuscheme lives in some non-standard directory, you must tell emacs
53 ;; ; where to get it. This may or may not be necessary.
54 ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
56 ;; ; Autoload run-scheme from file cmuscheme.el
57 ;; (autoload 'run-scheme "cmuscheme"
58 ;; "Run an inferior Scheme process."
59 ;; t)
61 ;; ; Files ending in ".scm" are Scheme source,
62 ;; ; so put their buffers in scheme-mode.
63 ;; (setq auto-mode-alist
64 ;; (cons '("\\.scm$" . scheme-mode)
65 ;; auto-mode-alist))
67 ;; ; Define C-c t to run my favorite command in inferior scheme mode:
68 ;; (setq cmuscheme-load-hook
69 ;; '((lambda () (define-key inferior-scheme-mode-map "\C-ct"
70 ;; 'favorite-cmd))))
71 ;;;
72 ;;; Unfortunately, scheme.el defines run-scheme to autoload from xscheme.el.
73 ;;; This will womp your declaration to autoload run-scheme from cmuscheme.el
74 ;;; if you haven't loaded cmuscheme in before scheme. Three fixes:
75 ;;; - Put the autoload on your scheme mode hook and in your .emacs toplevel:
76 ;;; (setq scheme-mode-hook
77 ;;; '((lambda () (autoload 'run-scheme "cmuscheme"
78 ;;; "Run an inferior Scheme" t))))
79 ;;; (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
80 ;;; Now when scheme.el autoloads, it will restore the run-scheme autoload.
81 ;;; - Load cmuscheme.el in your .emacs: (load-library 'cmuscheme)
82 ;;; - Change autoload declaration in scheme.el to point to cmuscheme.el:
83 ;;; (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
84 ;;; *or* just delete the autoload declaration from scheme.el altogether,
85 ;;; which will allow the autoload in your .emacs to have its say.
87 (require 'scheme)
88 (require 'comint)
90 ;;; INFERIOR SCHEME MODE STUFF
91 ;;;============================================================================
93 (defvar inferior-scheme-mode-hook nil
94 "*Hook for customising inferior-scheme mode.")
95 (defvar inferior-scheme-mode-map nil)
97 (cond ((not inferior-scheme-mode-map)
98 (setq inferior-scheme-mode-map
99 (full-copy-sparse-keymap comint-mode-map))
100 (define-key inferior-scheme-mode-map "\M-\C-x" ;gnu convention
101 'scheme-send-definition)
102 (define-key inferior-scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp)
103 (define-key inferior-scheme-mode-map "\C-c\C-l" 'scheme-load-file)
104 (define-key inferior-scheme-mode-map "\C-c\C-k" 'scheme-compile-file)
105 (scheme-mode-commands inferior-scheme-mode-map)))
107 ;; Install the process communication commands in the scheme-mode keymap.
108 (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
109 (define-key scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp);gnu convention
110 (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition)
111 (define-key scheme-mode-map "\C-c\M-e" 'scheme-send-definition-and-go)
112 (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region)
113 (define-key scheme-mode-map "\C-c\M-r" 'scheme-send-region-and-go)
114 (define-key scheme-mode-map "\C-c\M-c" 'scheme-compile-definition)
115 (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
116 (define-key scheme-mode-map "\C-c\C-z" 'switch-to-scheme)
117 (define-key scheme-mode-map "\C-c\C-l" 'scheme-load-file)
118 (define-key scheme-mode-map "\C-c\C-k" 'scheme-compile-file) ;k for "kompile"
120 (defun inferior-scheme-mode ()
121 "Major mode for interacting with an inferior Scheme process.
123 The following commands are available:
124 \\{inferior-scheme-mode-map}
126 A Scheme process can be fired up with M-x run-scheme.
128 Customisation: Entry to this mode runs the hooks on comint-mode-hook and
129 inferior-scheme-mode-hook (in that order).
131 You can send text to the inferior Scheme process from other buffers containing
132 Scheme source.
133 switch-to-scheme switches the current buffer to the Scheme process buffer.
134 scheme-send-definition sends the current definition to the Scheme process.
135 scheme-compile-definition compiles the current definition.
136 scheme-send-region sends the current region to the Scheme process.
137 scheme-compile-region compiles the current region.
139 scheme-send-definition-and-go, scheme-compile-definition-and-go,
140 scheme-send-region-and-go, and scheme-compile-region-and-go
141 switch to the Scheme process buffer after sending their text.
142 For information on running multiple processes in multiple buffers, see
143 documentation for variable scheme-buffer.
145 Commands:
146 Return after the end of the process' output sends the text from the
147 end of process to point.
148 Return before the end of the process' output copies the sexp ending at point
149 to the end of the process' output, and sends it.
150 Delete converts tabs to spaces as it moves back.
151 Tab indents for Scheme; with argument, shifts rest
152 of expression rigidly with the current line.
153 C-M-q does Tab on each line starting within following expression.
154 Paragraphs are separated only by blank lines. Semicolons start comments.
155 If you accidentally suspend your process, use \\[comint-continue-subjob]
156 to continue it."
157 (interactive)
158 (comint-mode)
159 ;; Customise in inferior-scheme-mode-hook
160 (setq comint-prompt-regexp "^[^>]*>+ *") ; OK for cscheme, oaklisp, T,...
161 (scheme-mode-variables)
162 (setq major-mode 'inferior-scheme-mode)
163 (setq mode-name "Inferior Scheme")
164 (setq mode-line-process '(": %s"))
165 (use-local-map inferior-scheme-mode-map)
166 (setq comint-input-filter (function scheme-input-filter))
167 (setq comint-input-sentinel (function ignore))
168 (setq comint-get-old-input (function scheme-get-old-input))
169 (run-hooks 'inferior-scheme-mode-hook))
171 (defun scheme-input-filter (str)
172 "Don't save anything matching inferior-scheme-filter-regexp"
173 (not (string-match inferior-scheme-filter-regexp str)))
175 (defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
176 "*Input matching this regexp are not saved on the history list.
177 Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
179 (defun scheme-get-old-input ()
180 "Snarf the sexp ending at point"
181 (save-excursion
182 (let ((end (point)))
183 (backward-sexp)
184 (buffer-substring (point) end))))
186 (defun scheme-args-to-list (string)
187 (let ((where (string-match "[ \t]" string)))
188 (cond ((null where) (list string))
189 ((not (= where 0))
190 (cons (substring string 0 where)
191 (scheme-args-to-list (substring string (+ 1 where)
192 (length string)))))
193 (t (let ((pos (string-match "[^ \t]" string)))
194 (if (null pos)
196 (scheme-args-to-list (substring string pos
197 (length string)))))))))
199 (defvar scheme-program-name "scheme"
200 "*Program invoked by the run-scheme command")
202 ;;; Obsolete
203 (defun scheme (&rest foo)
204 "Use run-scheme"
205 (interactive)
206 (message "Use run-scheme")
207 (ding))
209 (defun run-scheme (cmd)
210 "Run an inferior Scheme process, input and output via buffer *scheme*.
211 If there is a process already running in *scheme*, just switch to that buffer.
212 With argument, allows you to edit the command line (default is value
213 of scheme-program-name). Runs the hooks from inferior-scheme-mode-hook
214 \(after the comint-mode-hook is run).
215 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
217 (interactive (list (if current-prefix-arg
218 (read-string "Run Scheme: " scheme-program-name)
219 scheme-program-name)))
220 (if (not (comint-check-proc "*scheme*"))
221 (let ((cmdlist (scheme-args-to-list cmd)))
222 (set-buffer (apply 'make-comint "scheme" (car cmdlist)
223 nil (cdr cmdlist)))
224 (inferior-scheme-mode)))
225 (setq scheme-buffer "*scheme*")
226 (switch-to-buffer "*scheme*"))
229 (defun scheme-send-region (start end)
230 "Send the current region to the inferior Scheme process."
231 (interactive "r")
232 (comint-send-region (scheme-proc) start end)
233 (comint-send-string (scheme-proc) "\n"))
235 (defun scheme-send-definition ()
236 "Send the current definition to the inferior Scheme process."
237 (interactive)
238 (save-excursion
239 (end-of-defun)
240 (let ((end (point)))
241 (beginning-of-defun)
242 (scheme-send-region (point) end))))
244 (defun scheme-send-last-sexp ()
245 "Send the previous sexp to the inferior Scheme process."
246 (interactive)
247 (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
249 (defvar scheme-compile-exp-command "(compile '%s)"
250 "*Template for issuing commands to compile arbitrary Scheme expressions.")
252 (defun scheme-compile-region (start end)
253 "Compile the current region in the inferior Scheme process
254 \(A BEGIN is wrapped around the region: (BEGIN <region>))"
255 (interactive "r")
256 (comint-send-string (scheme-proc) (format scheme-compile-exp-command
257 (format "(begin %s)"
258 (buffer-substring start end))))
259 (comint-send-string (scheme-proc) "\n"))
261 (defun scheme-compile-definition ()
262 "Compile the current definition in the inferior Scheme process."
263 (interactive)
264 (save-excursion
265 (end-of-defun)
266 (let ((end (point)))
267 (beginning-of-defun)
268 (scheme-compile-region (point) end))))
270 (defun switch-to-scheme (eob-p)
271 "Switch to the scheme process buffer.
272 With argument, positions cursor at end of buffer."
273 (interactive "P")
274 (if (get-buffer scheme-buffer)
275 (pop-to-buffer scheme-buffer)
276 (error "No current process buffer. See variable scheme-buffer."))
277 (cond (eob-p
278 (push-mark)
279 (goto-char (point-max)))))
281 (defun scheme-send-region-and-go (start end)
282 "Send the current region to the inferior Scheme process,
283 and switch to the process buffer."
284 (interactive "r")
285 (scheme-send-region start end)
286 (switch-to-scheme t))
288 (defun scheme-send-definition-and-go ()
289 "Send the current definition to the inferior Scheme,
290 and switch to the process buffer."
291 (interactive)
292 (scheme-send-definition)
293 (switch-to-scheme t))
295 (defun scheme-compile-definition-and-go ()
296 "Compile the current definition in the inferior Scheme,
297 and switch to the process buffer."
298 (interactive)
299 (scheme-compile-definition)
300 (switch-to-scheme t))
302 (defun scheme-compile-region-and-go (start end)
303 "Compile the current region in the inferior Scheme,
304 and switch to the process buffer."
305 (interactive "r")
306 (scheme-compile-region start end)
307 (switch-to-scheme t))
309 (defvar scheme-source-modes '(scheme-mode)
310 "*Used to determine if a buffer contains Scheme source code.
311 If it's loaded into a buffer that is in one of these major modes, it's
312 considered a scheme source file by scheme-load-file and scheme-compile-file.
313 Used by these commands to determine defaults.")
315 (defvar scheme-prev-l/c-dir/file nil
316 "Caches the (directory . file) pair used in the last scheme-load-file or
317 scheme-compile-file command. Used for determining the default in the
318 next one.")
320 (defun scheme-load-file (file-name)
321 "Load a Scheme file into the inferior Scheme process."
322 (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
323 scheme-source-modes t)) ; T because LOAD
324 ; needs an exact name
325 (comint-check-source file-name) ; Check to see if buffer needs saved.
326 (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
327 (file-name-nondirectory file-name)))
328 (comint-send-string (scheme-proc) (concat "(load \""
329 file-name
330 "\"\)\n")))
332 (defun scheme-compile-file (file-name)
333 "Compile a Scheme file in the inferior Scheme process."
334 (interactive (comint-get-source "Compile Scheme file: "
335 scheme-prev-l/c-dir/file
336 scheme-source-modes
337 nil)) ; NIL because COMPILE doesn't
338 ; need an exact name.
339 (comint-check-source file-name) ; Check to see if buffer needs saved.
340 (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
341 (file-name-nondirectory file-name)))
342 (comint-send-string (scheme-proc) (concat "(compile-file \""
343 file-name
344 "\"\)\n")))
347 (defvar scheme-buffer nil "*The current scheme process buffer.
349 MULTIPLE PROCESS SUPPORT
350 ===========================================================================
351 Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
352 processes. To run multiple Scheme processes, you start the first up with
353 \\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
354 with \\[rename-buffer]. You may now start up a new process with another
355 \\[run-scheme]. It will be in a new buffer, named *scheme*. You can
356 switch between the different process buffers with \\[switch-to-buffer].
358 Commands that send text from source buffers to Scheme processes --
359 like scheme-send-definition or scheme-compile-region -- have to choose a
360 process to send to, when you have more than one Scheme process around. This
361 is determined by the global variable scheme-buffer. Suppose you
362 have three inferior Schemes running:
363 Buffer Process
364 foo scheme
365 bar scheme<2>
366 *scheme* scheme<3>
367 If you do a \\[scheme-send-definition-and-go] command on some Scheme source
368 code, what process do you send it to?
370 - If you're in a process buffer (foo, bar, or *scheme*),
371 you send it to that process.
372 - If you're in some other buffer (e.g., a source file), you
373 send it to the process attached to buffer scheme-buffer.
374 This process selection is performed by function scheme-proc.
376 Whenever \\[run-scheme] fires up a new process, it resets scheme-buffer
377 to be the new process's buffer. If you only run one process, this will
378 do the right thing. If you run multiple processes, you can change
379 scheme-buffer to another process buffer with \\[set-variable].
381 More sophisticated approaches are, of course, possible. If you find youself
382 needing to switch back and forth between multiple processes frequently,
383 you may wish to consider ilisp.el, a larger, more sophisticated package
384 for running inferior Lisp and Scheme processes. The approach taken here is
385 for a minimal, simple implementation. Feel free to extend it.")
387 (defun scheme-proc ()
388 "Returns the current scheme process. See variable scheme-buffer."
389 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
390 (current-buffer)
391 scheme-buffer))))
392 (or proc
393 (error "No current process. See variable scheme-buffer"))))
396 ;;; Do the user's customisation...
398 (defvar cmuscheme-load-hook nil
399 "This hook is run when cmuscheme is loaded in.
400 This is a good place to put keybindings.")
402 (run-hooks 'cmuscheme-load-hook)
405 ;;; CHANGE LOG
406 ;;; ===========================================================================
407 ;;; 8/88 Olin
408 ;;; Created.
410 ;;; 2/15/89 Olin
411 ;;; Removed -emacs flag from process invocation. It's only useful for
412 ;;; cscheme, and makes cscheme assume it's running under xscheme.el,
413 ;;; which messes things up royally. A bug.
415 ;;; 5/22/90 Olin
416 ;;; - Upgraded to use comint-send-string and comint-send-region.
417 ;;; - run-scheme now offers to let you edit the command line if
418 ;;; you invoke it with a prefix-arg. M-x scheme is redundant, and
419 ;;; has been removed.
420 ;;; - Explicit references to process "scheme" have been replaced with
421 ;;; (scheme-proc). This allows better handling of multiple process bufs.
422 ;;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
423 ;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
424 ;;; and friends, but interested hackers might find a useful application
425 ;;; of this facility.
427 ;;; 3/12/90 Olin
428 ;;; - scheme-load-file and scheme-compile-file no longer switch-to-scheme.
429 ;;; Tale suggested this.
431 (provide 'cmuscheme)