(inferior-lisp-prompt): Don't match more than one line.
[bpt/emacs.git] / lisp / cmuscheme.el
CommitLineData
c8472948
ER
1;;; cmuscheme.el --- Scheme process in a buffer. Adapted from tea.el.
2
e41b2db1
ER
3
4;; Copyright (C) 1988 Free Software Foundation, Inc.
c0274f38 5
07168830 6;; Author: Olin Shivers <olin.shivers@cs.cmu.edu>
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
120 (full-copy-sparse-keymap comint-mode-map))
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
183 (setq comint-prompt-regexp "^[^>]*>+ *") ; OK for cscheme, oaklisp, T,...
184 (scheme-mode-variables)
185 (setq major-mode 'inferior-scheme-mode)
186 (setq mode-name "Inferior Scheme")
187 (setq mode-line-process '(": %s"))
188 (use-local-map inferior-scheme-mode-map)
189 (setq comint-input-filter (function scheme-input-filter))
190 (setq comint-input-sentinel (function ignore))
191 (setq comint-get-old-input (function scheme-get-old-input))
192 (run-hooks 'inferior-scheme-mode-hook))
193
2f790b20
JB
194(defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
195 "*Input matching this regexp are not saved on the history list.
196Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
197
67f3e2fd
RS
198(defun scheme-input-filter (str)
199 "Don't save anything matching inferior-scheme-filter-regexp"
200 (not (string-match inferior-scheme-filter-regexp str)))
201
2f790b20
JB
202(defun scheme-get-old-input ()
203 "Snarf the sexp ending at point"
204 (save-excursion
205 (let ((end (point)))
206 (backward-sexp)
207 (buffer-substring (point) end))))
208
209(defun scheme-args-to-list (string)
210 (let ((where (string-match "[ \t]" string)))
211 (cond ((null where) (list string))
212 ((not (= where 0))
213 (cons (substring string 0 where)
214 (scheme-args-to-list (substring string (+ 1 where)
215 (length string)))))
216 (t (let ((pos (string-match "[^ \t]" string)))
217 (if (null pos)
218 nil
219 (scheme-args-to-list (substring string pos
220 (length string)))))))))
221
222(defvar scheme-program-name "scheme"
223 "*Program invoked by the run-scheme command")
224
225;;; Obsolete
226(defun scheme (&rest foo)
227 "Use run-scheme"
228 (interactive)
229 (message "Use run-scheme")
230 (ding))
231
232(defun run-scheme (cmd)
233 "Run an inferior Scheme process, input and output via buffer *scheme*.
234If there is a process already running in *scheme*, just switch to that buffer.
235With argument, allows you to edit the command line (default is value
236of scheme-program-name). Runs the hooks from inferior-scheme-mode-hook
237\(after the comint-mode-hook is run).
238\(Type \\[describe-mode] in the process buffer for a list of commands.)"
239
240 (interactive (list (if current-prefix-arg
241 (read-string "Run Scheme: " scheme-program-name)
242 scheme-program-name)))
243 (if (not (comint-check-proc "*scheme*"))
244 (let ((cmdlist (scheme-args-to-list cmd)))
245 (set-buffer (apply 'make-comint "scheme" (car cmdlist)
246 nil (cdr cmdlist)))
247 (inferior-scheme-mode)))
248 (setq scheme-buffer "*scheme*")
249 (switch-to-buffer "*scheme*"))
250
251
252(defun scheme-send-region (start end)
253 "Send the current region to the inferior Scheme process."
254 (interactive "r")
255 (comint-send-region (scheme-proc) start end)
256 (comint-send-string (scheme-proc) "\n"))
257
258(defun scheme-send-definition ()
259 "Send the current definition to the inferior Scheme process."
260 (interactive)
261 (save-excursion
262 (end-of-defun)
263 (let ((end (point)))
264 (beginning-of-defun)
265 (scheme-send-region (point) end))))
266
267(defun scheme-send-last-sexp ()
268 "Send the previous sexp to the inferior Scheme process."
269 (interactive)
270 (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
271
272(defvar scheme-compile-exp-command "(compile '%s)"
273 "*Template for issuing commands to compile arbitrary Scheme expressions.")
274
275(defun scheme-compile-region (start end)
ff11d0d9 276 "Compile the current region in the inferior Scheme process.
2f790b20
JB
277\(A BEGIN is wrapped around the region: (BEGIN <region>))"
278 (interactive "r")
279 (comint-send-string (scheme-proc) (format scheme-compile-exp-command
280 (format "(begin %s)"
281 (buffer-substring start end))))
282 (comint-send-string (scheme-proc) "\n"))
283
284(defun scheme-compile-definition ()
285 "Compile the current definition in the inferior Scheme process."
286 (interactive)
287 (save-excursion
288 (end-of-defun)
289 (let ((end (point)))
290 (beginning-of-defun)
291 (scheme-compile-region (point) end))))
292
293(defun switch-to-scheme (eob-p)
294 "Switch to the scheme process buffer.
295With argument, positions cursor at end of buffer."
296 (interactive "P")
297 (if (get-buffer scheme-buffer)
298 (pop-to-buffer scheme-buffer)
299 (error "No current process buffer. See variable scheme-buffer."))
300 (cond (eob-p
301 (push-mark)
302 (goto-char (point-max)))))
303
304(defun scheme-send-region-and-go (start end)
ff11d0d9
CZ
305 "Send the current region to the inferior Scheme process.
306Then switch to the process buffer."
2f790b20
JB
307 (interactive "r")
308 (scheme-send-region start end)
309 (switch-to-scheme t))
310
311(defun scheme-send-definition-and-go ()
ff11d0d9
CZ
312 "Send the current definition to the inferior Scheme.
313Then switch to the process buffer."
2f790b20
JB
314 (interactive)
315 (scheme-send-definition)
316 (switch-to-scheme t))
317
318(defun scheme-compile-definition-and-go ()
ff11d0d9
CZ
319 "Compile the current definition in the inferior Scheme.
320Then switch to the process buffer."
2f790b20
JB
321 (interactive)
322 (scheme-compile-definition)
323 (switch-to-scheme t))
324
325(defun scheme-compile-region-and-go (start end)
ff11d0d9
CZ
326 "Compile the current region in the inferior Scheme.
327Then switch to the process buffer."
2f790b20
JB
328 (interactive "r")
329 (scheme-compile-region start end)
330 (switch-to-scheme t))
331
332(defvar scheme-source-modes '(scheme-mode)
333 "*Used to determine if a buffer contains Scheme source code.
334If it's loaded into a buffer that is in one of these major modes, it's
335considered a scheme source file by scheme-load-file and scheme-compile-file.
336Used by these commands to determine defaults.")
337
338(defvar scheme-prev-l/c-dir/file nil
ff11d0d9
CZ
339 "Caches the last (directory . file) pair.
340Caches the last pair used in the last scheme-load-file or
2f790b20
JB
341scheme-compile-file command. Used for determining the default in the
342next one.")
343
344(defun scheme-load-file (file-name)
345 "Load a Scheme file into the inferior Scheme process."
346 (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
347 scheme-source-modes t)) ; T because LOAD
348 ; needs an exact name
349 (comint-check-source file-name) ; Check to see if buffer needs saved.
350 (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
351 (file-name-nondirectory file-name)))
352 (comint-send-string (scheme-proc) (concat "(load \""
353 file-name
354 "\"\)\n")))
355
356(defun scheme-compile-file (file-name)
357 "Compile a Scheme file in the inferior Scheme process."
358 (interactive (comint-get-source "Compile Scheme file: "
359 scheme-prev-l/c-dir/file
360 scheme-source-modes
361 nil)) ; NIL because COMPILE doesn't
362 ; need an exact name.
363 (comint-check-source file-name) ; Check to see if buffer needs saved.
364 (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
365 (file-name-nondirectory file-name)))
366 (comint-send-string (scheme-proc) (concat "(compile-file \""
367 file-name
368 "\"\)\n")))
369
370\f
371(defvar scheme-buffer nil "*The current scheme process buffer.
372
373MULTIPLE PROCESS SUPPORT
374===========================================================================
375Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
376processes. To run multiple Scheme processes, you start the first up with
377\\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
378with \\[rename-buffer]. You may now start up a new process with another
379\\[run-scheme]. It will be in a new buffer, named *scheme*. You can
380switch between the different process buffers with \\[switch-to-buffer].
381
382Commands that send text from source buffers to Scheme processes --
383like scheme-send-definition or scheme-compile-region -- have to choose a
384process to send to, when you have more than one Scheme process around. This
385is determined by the global variable scheme-buffer. Suppose you
386have three inferior Schemes running:
387 Buffer Process
388 foo scheme
389 bar scheme<2>
390 *scheme* scheme<3>
391If you do a \\[scheme-send-definition-and-go] command on some Scheme source
392code, what process do you send it to?
393
394- If you're in a process buffer (foo, bar, or *scheme*),
395 you send it to that process.
396- If you're in some other buffer (e.g., a source file), you
397 send it to the process attached to buffer scheme-buffer.
398This process selection is performed by function scheme-proc.
399
400Whenever \\[run-scheme] fires up a new process, it resets scheme-buffer
401to be the new process's buffer. If you only run one process, this will
402do the right thing. If you run multiple processes, you can change
403scheme-buffer to another process buffer with \\[set-variable].
404
eb8c3be9 405More sophisticated approaches are, of course, possible. If you find yourself
2f790b20
JB
406needing to switch back and forth between multiple processes frequently,
407you may wish to consider ilisp.el, a larger, more sophisticated package
408for running inferior Lisp and Scheme processes. The approach taken here is
409for a minimal, simple implementation. Feel free to extend it.")
410
411(defun scheme-proc ()
412 "Returns the current scheme process. See variable scheme-buffer."
413 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
414 (current-buffer)
415 scheme-buffer))))
416 (or proc
417 (error "No current process. See variable scheme-buffer"))))
418
419
420;;; Do the user's customisation...
421
422(defvar cmuscheme-load-hook nil
423 "This hook is run when cmuscheme is loaded in.
424This is a good place to put keybindings.")
425
426(run-hooks 'cmuscheme-load-hook)
427
428
429;;; CHANGE LOG
430;;; ===========================================================================
431;;; 8/88 Olin
432;;; Created.
433;;;
434;;; 2/15/89 Olin
435;;; Removed -emacs flag from process invocation. It's only useful for
436;;; cscheme, and makes cscheme assume it's running under xscheme.el,
437;;; which messes things up royally. A bug.
438;;;
439;;; 5/22/90 Olin
440;;; - Upgraded to use comint-send-string and comint-send-region.
441;;; - run-scheme now offers to let you edit the command line if
442;;; you invoke it with a prefix-arg. M-x scheme is redundant, and
443;;; has been removed.
444;;; - Explicit references to process "scheme" have been replaced with
445;;; (scheme-proc). This allows better handling of multiple process bufs.
446;;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
447;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
448;;; and friends, but interested hackers might find a useful application
449;;; of this facility.
450;;;
451;;; 3/12/90 Olin
452;;; - scheme-load-file and scheme-compile-file no longer switch-to-scheme.
453;;; Tale suggested this.
49116ac0
JB
454
455(provide 'cmuscheme)
c0274f38
ER
456
457;;; cmuscheme.el ends here