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