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