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