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