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