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