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