*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / inf-lisp.el
1 ;;; cmulisp.el --- improved version of standard inferior-lisp mode
2
3 ;;; Copyright Olin Shivers (1988).
4 ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
5 ;;; notice appearing here to the effect that you may use this code any
6 ;;; way you like, as long as you don't charge money for it, remove this
7 ;;; notice, or hold me liable for its results.
8
9 ;;; This replaces the standard inferior-lisp mode.
10 ;;; Hacked from tea.el by Olin Shivers (shivers@cs.cmu.edu). 8/88
11 ;;; Please send me bug reports, bug fixes, and extensions, so that I can
12 ;;; merge them into the master source.
13 ;;;
14 ;;; Change log at end of file.
15
16 ;;; This file defines a a lisp-in-a-buffer package (cmulisp mode) built on top
17 ;;; of comint mode. Cmulisp mode is similar to, and intended to replace, its
18 ;;; counterpart in the standard gnu emacs release. This replacements is more
19 ;;; featureful, robust, and uniform than the released version. The key
20 ;;; bindings are also more compatible with the bindings of Hemlock and Zwei
21 ;;; (the Lisp Machine emacs).
22
23 ;;; Since this mode is built on top of the general command-interpreter-in-
24 ;;; a-buffer mode (comint mode), it shares a common base functionality,
25 ;;; and a common set of bindings, with all modes derived from comint mode.
26 ;;; This makes these modes easier to use.
27
28 ;;; For documentation on the functionality provided by comint mode, and
29 ;;; the hooks available for customising it, see the file comint.el.
30 ;;; For further information on cmulisp mode, see the comments below.
31
32 ;;; Needs fixin:
33 ;;; The load-file/compile-file default mechanism could be smarter -- it
34 ;;; doesn't know about the relationship between filename extensions and
35 ;;; whether the file is source or executable. If you compile foo.lisp
36 ;;; with compile-file, then the next load-file should use foo.bin for
37 ;;; the default, not foo.lisp. This is tricky to do right, particularly
38 ;;; because the extension for executable files varies so much (.o, .bin,
39 ;;; .lbin, .mo, .vo, .ao, ...).
40 ;;;
41 ;;; It would be nice if cmulisp (and inferior scheme, T, ...) modes
42 ;;; had a verbose minor mode wherein sending or compiling defuns, etc.
43 ;;; would be reflected in the transcript with suitable comments, e.g.
44 ;;; ";;; redefining fact". Several ways to do this. Which is right?
45 ;;;
46 ;;; When sending text from a source file to a subprocess, the process-mark can
47 ;;; move off the window, so you can lose sight of the process interactions.
48 ;;; Maybe I should ensure the process mark is in the window when I send
49 ;;; text to the process? Switch selectable?
50
51 (require 'comint)
52 ;; YOUR .EMACS FILE
53 ;;=============================================================================
54 ;; Some suggestions for your .emacs file.
55 ;;
56 ;; ; If cmulisp lives in some non-standard directory, you must tell emacs
57 ;; ; where to get it. This may or may not be necessary.
58 ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
59 ;;
60 ;; ; Autoload cmulisp from file cmulisp.el
61 ;; (autoload 'cmulisp "cmulisp"
62 ;; "Run an inferior Lisp process."
63 ;; t)
64 ;;
65 ;; ; Define C-c t to run my favorite command in cmulisp mode:
66 ;; (setq cmulisp-load-hook
67 ;; '((lambda ()
68 ;; (define-key cmulisp-mode-map "\C-ct" 'favorite-cmd))))
69
70 \f
71 ;;; Brief Command Documentation:
72 ;;;============================================================================
73 ;;; Comint Mode Commands: (common to cmulisp and all comint-derived modes)
74 ;;;
75 ;;; m-p comint-previous-input Cycle backwards in input history
76 ;;; m-n comint-next-input Cycle forwards
77 ;;; m-c-r comint-previous-input-matching Search backwards in input history
78 ;;; return comint-send-input
79 ;;; c-a comint-bol Beginning of line; skip prompt.
80 ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
81 ;;; c-c c-u comint-kill-input ^u
82 ;;; c-c c-w backward-kill-word ^w
83 ;;; c-c c-c comint-interrupt-subjob ^c
84 ;;; c-c c-z comint-stop-subjob ^z
85 ;;; c-c c-\ comint-quit-subjob ^\
86 ;;; c-c c-o comint-kill-output Delete last batch of process output
87 ;;; c-c c-r comint-show-output Show last batch of process output
88 ;;; send-invisible Read line w/o echo & send to proc
89 ;;; comint-continue-subjob Useful if you accidentally suspend
90 ;;; top-level job.
91 ;;; comint-mode-hook is the comint mode hook.
92
93 ;;; CMU Lisp Mode Commands:
94 ;;; c-m-x lisp-send-defun This binding is a gnu convention.
95 ;;; c-c c-l lisp-load-file Prompt for file name; tell Lisp to load it.
96 ;;; c-c c-k lisp-compile-file Prompt for file name; tell Lisp to kompile it.
97 ;;; Filename completion is available, of course.
98 ;;;
99 ;;; Additionally, these commands are added to the key bindings of Lisp mode:
100 ;;; c-m-x lisp-eval-defun This binding is a gnu convention.
101 ;;; c-c c-e lisp-eval-defun Send the current defun to Lisp process.
102 ;;; c-x c-e lisp-eval-last-sexp Send the previous sexp to Lisp process.
103 ;;; c-c c-r lisp-eval-region Send the current region to Lisp process.
104 ;;; c-c c-c lisp-compile-defun Compile the current defun in Lisp process.
105 ;;; c-c c-z switch-to-lisp Switch to the Lisp process buffer.
106 ;;; c-c c-l lisp-load-file (See above. In a Lisp file buffer, default
107 ;;; c-c c-k lisp-compile-file is to load/compile the current file.)
108 ;;; c-c c-d lisp-describe-sym Query Lisp for a symbol's description.
109 ;;; c-c c-a lisp-show-arglist Query Lisp for function's arglist.
110 ;;; c-c c-f lisp-show-function-documentation Query Lisp for a function's doc.
111 ;;; c-c c-v lisp-show-variable-documentation Query Lisp for a variable's doc.
112
113 ;;; cmulisp Fires up the Lisp process.
114 ;;; lisp-compile-region Compile all forms in the current region.
115 ;;;
116 ;;; CMU Lisp Mode Variables:
117 ;;; cmulisp-filter-regexp Match this => don't get saved on input hist
118 ;;; inferior-lisp-program Name of Lisp program run-lisp executes
119 ;;; inferior-lisp-load-command Customises lisp-load-file
120 ;;; cmulisp-mode-hook
121 ;;; inferior-lisp-prompt Initialises comint-prompt-regexp.
122 ;;; Backwards compatibility.
123 ;;; lisp-source-modes Anything loaded into a buffer that's in
124 ;;; one of these modes is considered Lisp
125 ;;; source by lisp-load/compile-file.
126
127 ;;; Read the rest of this file for more information.
128 \f
129 (defvar cmulisp-filter-regexp "\\`\\s *\\(:\\(\\w\\|\\s_\\)\\)?\\s *\\'"
130 "*What not to save on inferior Lisp's input history
131 Input matching this regexp is not saved on the input history in cmulisp
132 mode. Default is whitespace followed by 0 or 1 single-letter :keyword
133 (as in :a, :c, etc.)")
134
135 (defvar cmulisp-mode-map nil)
136 (cond ((not cmulisp-mode-map)
137 (setq cmulisp-mode-map
138 (full-copy-sparse-keymap comint-mode-map))
139 (lisp-mode-commands cmulisp-mode-map)
140 (define-key cmulisp-mode-map "\C-x\C-e" 'lisp-eval-last-sexp)
141 (define-key cmulisp-mode-map "\C-c\C-l" 'lisp-load-file)
142 (define-key cmulisp-mode-map "\C-c\C-k" 'lisp-compile-file)
143 (define-key cmulisp-mode-map "\C-c\C-a" 'lisp-show-arglist)
144 (define-key cmulisp-mode-map "\C-c\C-d" 'lisp-describe-sym)
145 (define-key cmulisp-mode-map "\C-c\C-f" 'lisp-show-function-documentation)
146 (define-key cmulisp-mode-map "\C-c\C-v" 'lisp-show-variable-documentation)))
147
148 ;;; These commands augment Lisp mode, so you can process Lisp code in
149 ;;; the source files.
150 (define-key lisp-mode-map "\M-\C-x" 'lisp-eval-defun) ; Gnu convention
151 (define-key lisp-mode-map "\C-x\C-e" 'lisp-eval-last-sexp) ; Gnu convention
152 (define-key lisp-mode-map "\C-c\C-e" 'lisp-eval-defun)
153 (define-key lisp-mode-map "\C-c\C-r" 'lisp-eval-region)
154 (define-key lisp-mode-map "\C-c\C-c" 'lisp-compile-defun)
155 (define-key lisp-mode-map "\C-c\C-z" 'switch-to-lisp)
156 (define-key lisp-mode-map "\C-c\C-l" 'lisp-load-file)
157 (define-key lisp-mode-map "\C-c\C-k" 'lisp-compile-file) ; "kompile" file
158 (define-key lisp-mode-map "\C-c\C-a" 'lisp-show-arglist)
159 (define-key lisp-mode-map "\C-c\C-d" 'lisp-describe-sym)
160 (define-key lisp-mode-map "\C-c\C-f" 'lisp-show-function-documentation)
161 (define-key lisp-mode-map "\C-c\C-v" 'lisp-show-variable-documentation)
162
163
164 ;;; This function exists for backwards compatibility.
165 ;;; Previous versions of this package bound commands to C-c <letter>
166 ;;; bindings, which is not allowed by the gnumacs standard.
167
168 (defun cmulisp-install-letter-bindings ()
169 "This function binds many cmulisp commands to C-c <letter> bindings,
170 where they are more accessible. C-c <letter> bindings are reserved for the
171 user, so these bindings are non-standard. If you want them, you should
172 have this function called by the cmulisp-load-hook:
173 (setq cmulisp-load-hook '(cmulisp-install-letter-bindings))
174 You can modify this function to install just the bindings you want."
175
176 (define-key lisp-mode-map "\C-ce" 'lisp-eval-defun-and-go)
177 (define-key lisp-mode-map "\C-cr" 'lisp-eval-region-and-go)
178 (define-key lisp-mode-map "\C-cc" 'lisp-compile-defun-and-go)
179 (define-key lisp-mode-map "\C-cz" 'switch-to-lisp)
180 (define-key lisp-mode-map "\C-cl" 'lisp-load-file)
181 (define-key lisp-mode-map "\C-ck" 'lisp-compile-file)
182 (define-key lisp-mode-map "\C-ca" 'lisp-show-arglist)
183 (define-key lisp-mode-map "\C-cd" 'lisp-describe-sym)
184 (define-key lisp-mode-map "\C-cf" 'lisp-show-function-documentation)
185 (define-key lisp-mode-map "\C-cv" 'lisp-show-variable-documentation)
186
187 (define-key cmulisp-mode-map "\C-cl" 'lisp-load-file)
188 (define-key cmulisp-mode-map "\C-ck" 'lisp-compile-file)
189 (define-key cmulisp-mode-map "\C-ca" 'lisp-show-arglist)
190 (define-key cmulisp-mode-map "\C-cd" 'lisp-describe-sym)
191 (define-key cmulisp-mode-map "\C-cf" 'lisp-show-function-documentation)
192 (define-key cmulisp-mode-map "\C-cv" 'lisp-show-variable-documentation))
193
194
195 (defvar inferior-lisp-program "lisp"
196 "*Program name for invoking an inferior Lisp with `cmulisp'.")
197
198 (defvar inferior-lisp-load-command "(load \"%s\")\n"
199 "*Format-string for building a Lisp expression to load a file.
200 This format string should use %s to substitute a file name
201 and should result in a Lisp expression that will command the inferior Lisp
202 to load that file. The default works acceptably on most Lisps.
203 The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\\n\"
204 produces cosmetically superior output for this application,
205 but it works only in Common Lisp.")
206
207 (defvar inferior-lisp-prompt "^[^> ]*>+:? *"
208 "Regexp to recognise prompts in the inferior Lisp.
209 Defaults to \"^[^> ]*>+:? *\", which works pretty good for Lucid, kcl,
210 and franz. This variable is used to initialise comint-prompt-regexp in the
211 cmulisp buffer.
212
213 More precise choices:
214 Lucid Common Lisp: \"^\\(>\\|\\(->\\)+\\) *\"
215 franz: \"^\\(->\\|<[0-9]*>:\\) *\"
216 kcl: \"^>+ *\"
217
218 This is a fine thing to set in your .emacs file.")
219
220 (defvar cmulisp-mode-hook '()
221 "*Hook for customising cmulisp mode")
222
223 (defun cmulisp-mode ()
224 "Major mode for interacting with an inferior Lisp process.
225 Runs a Lisp interpreter as a subprocess of Emacs, with Lisp I/O through an
226 Emacs buffer. Variable inferior-lisp-program controls which Lisp interpreter
227 is run. Variables inferior-lisp-prompt, cmulisp-filter-regexp and
228 inferior-lisp-load-command can customize this mode for different Lisp
229 interpreters.
230
231 For information on running multiple processes in multiple buffers, see
232 documentation for variable cmulisp-buffer.
233
234 \\{cmulisp-mode-map}
235
236 Customisation: Entry to this mode runs the hooks on comint-mode-hook and
237 cmulisp-mode-hook (in that order).
238
239 You can send text to the inferior Lisp process from other buffers containing
240 Lisp source.
241 switch-to-lisp switches the current buffer to the Lisp process buffer.
242 lisp-eval-defun sends the current defun to the Lisp process.
243 lisp-compile-defun compiles the current defun.
244 lisp-eval-region sends the current region to the Lisp process.
245 lisp-compile-region compiles the current region.
246
247 Prefixing the lisp-eval/compile-defun/region commands with
248 a \\[universal-argument] causes a switch to the Lisp process buffer after sending
249 the text.
250
251 Commands:
252 Return after the end of the process' output sends the text from the
253 end of process to point.
254 Return before the end of the process' output copies the sexp ending at point
255 to the end of the process' output, and sends it.
256 Delete converts tabs to spaces as it moves back.
257 Tab indents for Lisp; with argument, shifts rest
258 of expression rigidly with the current line.
259 C-M-q does Tab on each line starting within following expression.
260 Paragraphs are separated only by blank lines. Semicolons start comments.
261 If you accidentally suspend your process, use \\[comint-continue-subjob]
262 to continue it."
263 (interactive)
264 (comint-mode)
265 (setq comint-prompt-regexp inferior-lisp-prompt)
266 (setq major-mode 'cmulisp-mode)
267 (setq mode-name "CMU Lisp")
268 (setq mode-line-process '(": %s"))
269 (if (string-match "^18.4" emacs-version) ; hack.
270 (lisp-mode-variables) ; This is right for 18.49
271 (lisp-mode-variables t)) ; This is right for 18.50
272 (use-local-map cmulisp-mode-map) ;c-c c-k for "kompile" file
273 (setq comint-get-old-input (function lisp-get-old-input))
274 (setq comint-input-filter (function lisp-input-filter))
275 (setq comint-input-sentinel 'ignore)
276 (run-hooks 'cmulisp-mode-hook))
277
278 (defun lisp-get-old-input ()
279 "Snarf the sexp ending at point"
280 (save-excursion
281 (let ((end (point)))
282 (backward-sexp)
283 (buffer-substring (point) end))))
284
285 (defun lisp-input-filter (str)
286 "Don't save anything matching cmulisp-filter-regexp"
287 (not (string-match cmulisp-filter-regexp str)))
288
289 (defun cmulisp (cmd)
290 "Run an inferior Lisp process, input and output via buffer *cmulisp*.
291 If there is a process already running in *cmulisp*, just switch to that buffer.
292 With argument, allows you to edit the command line (default is value
293 of inferior-lisp-program). Runs the hooks from cmulisp-mode-hook (after the
294 comint-mode-hook is run).
295 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
296 (interactive (list (if current-prefix-arg
297 (read-string "Run lisp: " inferior-lisp-program)
298 inferior-lisp-program)))
299 (if (not (comint-check-proc "*cmulisp*"))
300 (let ((cmdlist (cmulisp-args-to-list cmd)))
301 (set-buffer (apply (function make-comint) "cmulisp" (car cmdlist) nil
302 (cdr cmdlist)))
303 (cmulisp-mode)))
304 (setq cmulisp-buffer "*cmulisp*")
305 (switch-to-buffer "*cmulisp*"))
306
307 ;;; Break a string up into a list of arguments.
308 ;;; This will break if you have an argument with whitespace, as in
309 ;;; string = "-ab +c -x 'you lose'".
310 (defun cmulisp-args-to-list (string)
311 (let ((where (string-match "[ \t]" string)))
312 (cond ((null where) (list string))
313 ((not (= where 0))
314 (cons (substring string 0 where)
315 (tea-args-to-list (substring string (+ 1 where)
316 (length string)))))
317 (t (let ((pos (string-match "[^ \t]" string)))
318 (if (null pos)
319 nil
320 (cmulsip-args-to-list (substring string pos
321 (length string)))))))))
322
323 (defun lisp-eval-region (start end &optional and-go)
324 "Send the current region to the inferior Lisp process.
325 Prefix argument means switch-to-lisp afterwards."
326 (interactive "r\nP")
327 (comint-send-region (cmulisp-proc) start end)
328 (comint-send-string (cmulisp-proc) "\n")
329 (if and-go (switch-to-lisp t)))
330
331 (defun lisp-eval-defun (&optional and-go)
332 "Send the current defun to the inferior Lisp process.
333 Prefix argument means switch-to-lisp afterwards."
334 (interactive "P")
335 (save-excursion
336 (end-of-defun)
337 (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
338 (let ((end (point)))
339 (beginning-of-defun)
340 (lisp-eval-region (point) end)))
341 (if and-go (switch-to-lisp t)))
342
343 (defun lisp-eval-last-sexp (&optional and-go)
344 "Send the previous sexp to the inferior Lisp process.
345 Prefix argument means switch-to-lisp afterwards."
346 (interactive "P")
347 (lisp-eval-region (save-excursion (backward-sexp) (point)) (point) and-go))
348
349 ;;; Common Lisp COMPILE sux.
350 (defun lisp-compile-region (start end &optional and-go)
351 "Compile the current region in the inferior Lisp process.
352 Prefix argument means switch-to-lisp afterwards."
353 (interactive "r\nP")
354 (comint-send-string (cmulisp-proc)
355 (format "(funcall (compile nil `(lambda () (progn 'compile %s))))\n"
356 (buffer-substring start end)))
357 (if and-go (switch-to-lisp t)))
358
359 (defun lisp-compile-defun (&optional and-go)
360 "Compile the current defun in the inferior Lisp process.
361 Prefix argument means switch-to-lisp afterwards."
362 (interactive "P")
363 (save-excursion
364 (end-of-defun)
365 (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
366 (let ((e (point)))
367 (beginning-of-defun)
368 (lisp-compile-region (point) e)))
369 (if and-go (switch-to-lisp t)))
370
371 (defun switch-to-lisp (eob-p)
372 "Switch to the inferior Lisp process buffer.
373 With argument, positions cursor at end of buffer."
374 (interactive "P")
375 (if (get-buffer cmulisp-buffer)
376 (pop-to-buffer cmulisp-buffer)
377 (error "No current process buffer. See variable cmulisp-buffer."))
378 (cond (eob-p
379 (push-mark)
380 (goto-char (point-max)))))
381
382
383 ;;; Now that lisp-compile/eval-defun/region takes an optional prefix arg,
384 ;;; these commands are redundant. But they are kept around for the user
385 ;;; to bind if he wishes, for backwards functionality, and because it's
386 ;;; easier to type C-c e than C-u C-c C-e.
387
388 (defun lisp-eval-region-and-go (start end)
389 "Send the current region to the inferior Lisp,
390 and switch to the process buffer."
391 (interactive "r")
392 (lisp-eval-region start end t))
393
394 (defun lisp-eval-defun-and-go ()
395 "Send the current defun to the inferior Lisp,
396 and switch to the process buffer."
397 (interactive)
398 (lisp-eval-defun t))
399
400 (defun lisp-compile-region-and-go (start end)
401 "Compile the current region in the inferior Lisp,
402 and switch to the process buffer."
403 (interactive "r")
404 (lisp-compile-region start end t))
405
406 (defun lisp-compile-defun-and-go ()
407 "Compile the current defun in the inferior Lisp,
408 and switch to the process buffer."
409 (interactive)
410 (lisp-compile-defun t))
411
412 ;;; A version of the form in H. Shevis' soar-mode.el package. Less robust.
413 ;(defun lisp-compile-sexp (start end)
414 ; "Compile the s-expression bounded by START and END in the inferior lisp.
415 ;If the sexp isn't a DEFUN form, it is evaluated instead."
416 ; (cond ((looking-at "(defun\\s +")
417 ; (goto-char (match-end 0))
418 ; (let ((name-start (point)))
419 ; (forward-sexp 1)
420 ; (process-send-string "cmulisp" (format "(compile '%s #'(lambda "
421 ; (buffer-substring name-start
422 ; (point)))))
423 ; (let ((body-start (point)))
424 ; (goto-char start) (forward-sexp 1) ; Can't use end-of-defun.
425 ; (process-send-region "cmulisp" (buffer-substring body-start (point))))
426 ; (process-send-string "cmulisp" ")\n"))
427 ; (t (lisp-eval-region start end)))))
428 ;
429 ;(defun lisp-compile-region (start end)
430 ; "Each s-expression in the current region is compiled (if a DEFUN)
431 ;or evaluated (if not) in the inferior lisp."
432 ; (interactive "r")
433 ; (save-excursion
434 ; (goto-char start) (end-of-defun) (beginning-of-defun) ; error check
435 ; (if (< (point) start) (error "region begins in middle of defun"))
436 ; (goto-char start)
437 ; (let ((s start))
438 ; (end-of-defun)
439 ; (while (<= (point) end) ; Zip through
440 ; (lisp-compile-sexp s (point)) ; compiling up defun-sized chunks.
441 ; (setq s (point))
442 ; (end-of-defun))
443 ; (if (< s end) (lisp-compile-sexp s end)))))
444 ;;;
445 ;;; End of HS-style code
446
447
448 (defvar lisp-prev-l/c-dir/file nil
449 "Saves the (directory . file) pair used in the last lisp-load-file or
450 lisp-compile-file command. Used for determining the default in the
451 next one.")
452
453 (defvar lisp-source-modes '(lisp-mode)
454 "*Used to determine if a buffer contains Lisp source code.
455 If it's loaded into a buffer that is in one of these major modes, it's
456 considered a Lisp source file by lisp-load-file and lisp-compile-file.
457 Used by these commands to determine defaults.")
458
459 (defun lisp-load-file (file-name)
460 "Load a Lisp file into the inferior Lisp process."
461 (interactive (comint-get-source "Load Lisp file: " lisp-prev-l/c-dir/file
462 lisp-source-modes nil)) ; NIL because LOAD
463 ; doesn't need an exact name
464 (comint-check-source file-name) ; Check to see if buffer needs saved.
465 (setq lisp-prev-l/c-dir/file (cons (file-name-directory file-name)
466 (file-name-nondirectory file-name)))
467 (comint-send-string (cmulisp-proc)
468 (format inferior-lisp-load-command file-name))
469 (switch-to-lisp t))
470
471
472 (defun lisp-compile-file (file-name)
473 "Compile a Lisp file in the inferior Lisp process."
474 (interactive (comint-get-source "Compile Lisp file: " lisp-prev-l/c-dir/file
475 lisp-source-modes nil)) ; NIL = don't need
476 ; suffix .lisp
477 (comint-check-source file-name) ; Check to see if buffer needs saved.
478 (setq lisp-prev-l/c-dir/file (cons (file-name-directory file-name)
479 (file-name-nondirectory file-name)))
480 (comint-send-string (cmulisp-proc) (concat "(compile-file \""
481 file-name
482 "\"\)\n"))
483 (switch-to-lisp t))
484
485
486 \f
487 ;;; Documentation functions: function doc, var doc, arglist, and
488 ;;; describe symbol.
489 ;;; ===========================================================================
490
491 ;;; Command strings
492 ;;; ===============
493
494 (defvar lisp-function-doc-command
495 "(let ((fn '%s))
496 (format t \"Documentation for ~a:~&~a\"
497 fn (documentation fn 'function))
498 (values))\n"
499 "Command to query inferior Lisp for a function's documentation.")
500
501 (defvar lisp-var-doc-command
502 "(let ((v '%s))
503 (format t \"Documentation for ~a:~&~a\"
504 v (documentation v 'variable))
505 (values))\n"
506 "Command to query inferior Lisp for a variable's documentation.")
507
508 (defvar lisp-arglist-command
509 "(let ((fn '%s))
510 (format t \"Arglist for ~a: ~a\" fn (arglist fn))
511 (values))\n"
512 "Command to query inferior Lisp for a function's arglist.")
513
514 (defvar lisp-describe-sym-command
515 "(describe '%s)\n"
516 "Command to query inferior Lisp for a variable's documentation.")
517
518
519 ;;; Ancillary functions
520 ;;; ===================
521
522 ;;; Reads a string from the user.
523 (defun lisp-symprompt (prompt default)
524 (list (let* ((prompt (if default
525 (format "%s (default %s): " prompt default)
526 (concat prompt ": ")))
527 (ans (read-string prompt)))
528 (if (zerop (length ans)) default ans))))
529
530
531 ;;; Adapted from function-called-at-point in help.el.
532 (defun lisp-fn-called-at-pt ()
533 "Returns the name of the function called in the current call.
534 Nil if it can't find one."
535 (condition-case nil
536 (save-excursion
537 (save-restriction
538 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
539 (backward-up-list 1)
540 (forward-char 1)
541 (let ((obj (read (current-buffer))))
542 (and (symbolp obj) obj))))
543 (error nil)))
544
545
546 ;;; Adapted from variable-at-point in help.el.
547 (defun lisp-var-at-pt ()
548 (condition-case ()
549 (save-excursion
550 (forward-sexp -1)
551 (skip-chars-forward "'")
552 (let ((obj (read (current-buffer))))
553 (and (symbolp obj) obj)))
554 (error nil)))
555
556
557 ;;; Documentation functions: fn and var doc, arglist, and symbol describe.
558 ;;; ======================================================================
559
560 (defun lisp-show-function-documentation (fn)
561 "Send a command to the inferior Lisp to give documentation for function FN.
562 See variable lisp-function-doc-command."
563 (interactive (lisp-symprompt "Function doc" (lisp-fn-called-at-pt)))
564 (comint-proc-query (cmulisp-proc) (format lisp-function-doc-command fn)))
565
566 (defun lisp-show-variable-documentation (var)
567 "Send a command to the inferior Lisp to give documentation for function FN.
568 See variable lisp-var-doc-command."
569 (interactive (lisp-symprompt "Variable doc" (lisp-var-at-pt)))
570 (comint-proc-query (cmulisp-proc) (format lisp-var-doc-command var)))
571
572 (defun lisp-show-arglist (fn)
573 "Sends an query to the inferior Lisp for the arglist for function FN.
574 See variable lisp-arglist-command."
575 (interactive (lisp-symprompt "Arglist" (lisp-fn-called-at-pt)))
576 (comint-proc-query (cmulisp-proc) (format lisp-arglist-command fn)))
577
578 (defun lisp-describe-sym (sym)
579 "Send a command to the inferior Lisp to describe symbol SYM.
580 See variable lisp-describe-sym-command."
581 (interactive (lisp-symprompt "Describe" (lisp-var-at-pt)))
582 (comint-proc-query (cmulisp-proc) (format lisp-describe-sym-command sym)))
583
584 \f
585 (defvar cmulisp-buffer nil "*The current cmulisp process buffer.
586
587 MULTIPLE PROCESS SUPPORT
588 ===========================================================================
589 Cmulisp.el supports, in a fairly simple fashion, running multiple Lisp
590 processes. To run multiple Lisp processes, you start the first up with
591 \\[cmulisp]. It will be in a buffer named *cmulisp*. Rename this buffer
592 with \\[rename-buffer]. You may now start up a new process with another
593 \\[cmulisp]. It will be in a new buffer, named *cmulisp*. You can
594 switch between the different process buffers with \\[switch-to-buffer].
595
596 Commands that send text from source buffers to Lisp processes --
597 like lisp-eval-defun or lisp-show-arglist -- have to choose a process
598 to send to, when you have more than one Lisp process around. This
599 is determined by the global variable cmulisp-buffer. Suppose you
600 have three inferior lisps running:
601 Buffer Process
602 foo cmulisp
603 bar cmulisp<2>
604 *cmulisp* cmulisp<3>
605 If you do a \\[lisp-eval-defun] command on some Lisp source code,
606 what process do you send it to?
607
608 - If you're in a process buffer (foo, bar, or *cmulisp*),
609 you send it to that process.
610 - If you're in some other buffer (e.g., a source file), you
611 send it to the process attached to buffer cmulisp-buffer.
612 This process selection is performed by function cmulisp-proc.
613
614 Whenever \\[cmulisp] fires up a new process, it resets cmulisp-buffer
615 to be the new process's buffer. If you only run one process, this will
616 do the right thing. If you run multiple processes, you can change
617 cmulisp-buffer to another process buffer with \\[set-variable].
618
619 More sophisticated approaches are, of course, possible. If you find youself
620 needing to switch back and forth between multiple processes frequently,
621 you may wish to consider ilisp.el, a larger, more sophisticated package
622 for running inferior Lisp processes. The approach taken here is for a
623 minimal, simple implementation. Feel free to extend it.")
624
625 (defun cmulisp-proc ()
626 "Returns the current cmulisp process. See variable cmulisp-buffer."
627 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-lisp-mode)
628 (current-buffer)
629 cmulisp-buffer))))
630 (or proc
631 (error "No current process. See variable cmulisp-buffer"))))
632
633
634 ;;; Do the user's customisation...
635 ;;;===============================
636 (defvar cmulisp-load-hook nil
637 "This hook is run when cmulisp is loaded in.
638 This is a good place to put keybindings.")
639
640 (run-hooks 'cmulisp-load-hook)
641
642 ;;; CHANGE LOG
643 ;;; ===========================================================================
644 ;;; 5/24/90 Olin
645 ;;; - Split cmulisp and cmushell modes into separate files.
646 ;;; Not only is this a good idea, it's apparently the way it'll be rel 19.
647 ;;; - Upgraded process sends to use comint-send-string instead of
648 ;;; process-send-string.
649 ;;; - Explicit references to process "cmulisp" have been replaced with
650 ;;; (cmulisp-proc). This allows better handling of multiple process bufs.
651 ;;; - Added process query and var/function/symbol documentation
652 ;;; commands. Based on code written by Douglas Roberts.
653 ;;; - Added lisp-eval-last-sexp, bound to C-x C-e.
654 ;;;
655 ;;; 9/20/90 Olin
656 ;;; Added a save-restriction to lisp-fn-called-at-pt. This bug and fix
657 ;;; reported by Lennart Staflin.
658 ;;;
659 ;;; 3/12/90 Olin
660 ;;; - lisp-load-file and lisp-compile-file no longer switch-to-lisp.
661 ;;; Tale suggested this.
662 ;;; - Reversed this decision 7/15/91. You need the visual feedback.
663 ;;;
664 ;;; 7/25/91 Olin
665 ;;; Changed all keybindings of the form C-c <letter>. These are
666 ;;; supposed to be reserved for the user to bind. This affected
667 ;;; mainly the compile/eval-defun/region[-and-go] commands.
668 ;;; This was painful, but necessary to adhere to the gnumacs standard.
669 ;;; For some backwards compatibility, see the
670 ;;; cmulisp-install-letter-bindings
671 ;;; function.
672 ;;;
673 ;;; 8/2/91 Olin
674 ;;; - The lisp-compile/eval-defun/region commands now take a prefix arg,
675 ;;; which means switch-to-lisp after sending the text to the Lisp process.
676 ;;; This obsoletes all the -and-go commands. The -and-go commands are
677 ;;; kept around for historical reasons, and because the user can bind
678 ;;; them to key sequences shorter than C-u C-c C-<letter>.
679 ;;; - If M-x cmulisp is invoked with a prefix arg, it allows you to
680 ;;; edit the command line.
681
682 (provide 'cmulisp)
683
684 ;;; cmulisp.el ends here