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