Add defgroup; use defcustom for user vars.
[emacs.git] / lisp / ielm.el
bloba5ce307a3d1a82b2f10112b56abd7a99d95a36a0
1 ;;; ielm.el --- interaction mode for Emacs Lisp
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
5 ;; Author: David Smith <maa036@lancaster.ac.uk>
6 ;; Created: 25 Feb 1994
7 ;; Keywords: lisp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Provides a nice interface to evaluating Emacs Lisp expressions.
29 ;; Input is handled by the comint package, and output is passed
30 ;; through the pretty-printer.
32 ;; To install: copy this file to a directory in your load-path, and
33 ;; add the following line to your .emacs file:
35 ;; (autoload 'ielm "ielm" "Start an inferior Emacs Lisp session" t)
37 ;; For completion to work, the comint.el from FSF Emacs 19.23 is
38 ;; required. If you do not have it, or if you are running Lemacs,
39 ;; also add the following code to your .emacs:
41 ;; (setq ielm-mode-hook
42 ;; '(lambda nil
43 ;; (define-key ielm-map "\t"
44 ;; '(lambda nil (interactive) (or (ielm-tab)
45 ;; (lisp-complete-symbol))))))
47 ;; To start: M-x ielm. Type C-h m in the *ielm* buffer for more info.
49 ;; The latest version is available by WWW from
50 ;; http://mathssun5.lancs.ac.uk:2080/~maa036/elisp/dir.html
51 ;; or by anonymous FTP from
52 ;; /anonymous@wingra.stat.wisc.edu:pub/src/emacs-lisp/ielm.el.gz
53 ;; or from the author: David M. Smith <maa036@lancaster.ac.uk>
55 ;;; Code:
57 (require 'comint)
58 (require 'pp)
60 ;;; User variables
62 (defvar ielm-noisy t
63 "*If non-nil, IELM will beep on error.")
65 (defvar ielm-prompt "ELISP> "
66 "Prompt used in IELM.")
68 (defvar ielm-dynamic-return t
69 "*Controls whether \\<ielm-map>\\[ielm-return] has intelligent behaviour in IELM.
70 If non-nil, \\[ielm-return] evaluates input for complete sexps, or inserts a newline
71 and indents for incomplete sexps. If nil, always inserts newlines.")
73 (defvar ielm-dynamic-multiline-inputs t
74 "*Force multiline inputs to start from column zero?
75 If non-nil, after entering the first line of an incomplete sexp, a newline
76 will be inserted after the prompt, moving the input to the next line.
77 This gives more frame width for large indented sexps, and allows functions
78 such as `edebug-defun' to work with such inputs.")
80 (defvar ielm-mode-hook nil
81 "*Hooks to be run when IELM (`inferior-emacs-lisp-mode') is started.")
83 ;;; System variables
85 (defvar ielm-working-buffer nil
86 "Buffer in which IELM sexps will be evaluated.
87 This variable is buffer-local.")
89 (defvar ielm-header
90 (concat
91 "*** Welcome to IELM version "
92 (substring "$Revision: 1.8 $" 11 -2)
93 " *** Type (describe-mode) for help.\n"
94 "IELM has ABSOLUTELY NO WARRANTY; type (describe-no-warranty) for details.\n")
95 "Message to display when IELM is started.")
97 (defvar ielm-map nil)
98 (if ielm-map nil
99 (if (string-match "Lucid" emacs-version)
100 ;; Lemacs
101 (progn
102 (setq ielm-map (make-sparse-keymap))
103 (set-keymap-parent ielm-map comint-mode-map))
104 ;; FSF
105 (setq ielm-map (cons 'keymap comint-mode-map)))
106 (define-key ielm-map "\t" 'comint-dynamic-complete)
107 (define-key ielm-map "\C-m" 'ielm-return)
108 (define-key ielm-map "\C-j" 'ielm-send-input)
109 (define-key ielm-map "\e\C-x" 'eval-defun) ; for consistency with
110 (define-key ielm-map "\e\t" 'lisp-complete-symbol) ; lisp-interaction-mode
111 ;; These bindings are from shared-lisp-mode-map -- can you inherit
112 ;; from more than one keymap??
113 (define-key ielm-map "\e\C-q" 'indent-sexp)
114 (define-key ielm-map "\177" 'backward-delete-char-untabify)
115 ;; Some convenience bindings for setting the working buffer
116 (define-key ielm-map "\C-c\C-b" 'ielm-change-working-buffer)
117 (define-key ielm-map "\C-c\C-f" 'ielm-display-working-buffer)
118 (define-key ielm-map "\C-c\C-v" 'ielm-print-working-buffer))
120 (defvar ielm-font-lock-keywords
121 (list
122 (cons (concat "^" (regexp-quote ielm-prompt)) 'font-lock-keyword-face)
123 '("\\(^\\*\\*\\*[^*]+\\*\\*\\*\\)\\(.*$\\)" (1 font-lock-comment-face) (2 font-lock-reference-face)))
124 "Additional expressions to highlight in ielm buffers.")
126 ;;; Completion stuff
128 (defun ielm-tab nil
129 "Possibly indent the current line as lisp code."
130 (interactive)
131 (if (or (eq (preceding-char) ?\n)
132 (eq (char-syntax (preceding-char)) ? ))
133 (progn
134 (ielm-indent-line)
135 t)))
137 (defun ielm-complete-symbol nil
138 "Complete the lisp symbol before point."
139 ;; A wrapper for lisp-complete symbol that returns non-nil if
140 ;; completion has occurred
141 (let* ((btick (buffer-modified-tick))
142 (cbuffer (get-buffer "*Completions*"))
143 (ctick (and cbuffer (buffer-modified-tick cbuffer))))
144 (lisp-complete-symbol)
145 ;; completion has occurred if:
146 (or
147 ;; the buffer has been modified
148 (not (= btick (buffer-modified-tick)))
149 ;; a completions buffer has been modified or created
150 (if cbuffer
151 (not (= ctick (buffer-modified-tick cbuffer)))
152 (get-buffer "*Completions*")))))
154 (defun ielm-complete-filename nil
155 "Dynamically complete filename before point, if in a string."
156 (if (nth 3 (parse-partial-sexp comint-last-input-start (point)))
157 (comint-dynamic-complete-filename)))
159 (defun ielm-indent-line nil
160 "Indent the current line as Lisp code if it is not a prompt line."
161 (if (save-excursion
162 (beginning-of-line)
163 (looking-at comint-prompt-regexp)) nil
164 (lisp-indent-line)))
166 ;;; Working buffer manipulation
168 (defun ielm-print-working-buffer nil
169 "Print the current IELM working buffer's name in the echo area."
170 (interactive)
171 (message "The current working buffer is: %s" (buffer-name ielm-working-buffer)))
173 (defun ielm-display-working-buffer nil
174 "Display the current IELM working buffer.
175 Don't forget that selecting that buffer will change its value of `point'
176 to its value of `window-point'!"
177 (interactive)
178 (display-buffer ielm-working-buffer)
179 (ielm-print-working-buffer))
181 (defun ielm-change-working-buffer (buf)
182 "Change the current IELM working buffer to BUF.
183 This is the buffer in which all sexps entered at the IELM prompt are
184 evaluated. You can achieve the same effect with a call to
185 `set-buffer' at the IELM prompt."
186 (interactive "bSet working buffer to: ")
187 (setq ielm-working-buffer (or (get-buffer buf) (error "No such buffer")))
188 (ielm-print-working-buffer))
190 ;;; Other bindings
192 (defun ielm-return nil
193 "Newline and indent, or evaluate the sexp before the prompt.
194 Complete sexps are evaluated; for incomplete sexps inserts a newline
195 and indents. If however `ielm-dynamic-return' is nil, this always
196 simply inserts a newline."
197 (interactive)
198 (if ielm-dynamic-return
199 (let ((state
200 (save-excursion
201 (end-of-line)
202 (parse-partial-sexp (ielm-pm)
203 (point)))))
204 (if (and (< (car state) 1) (not (nth 3 state)))
205 (ielm-send-input)
206 (if (and ielm-dynamic-multiline-inputs
207 (save-excursion
208 (beginning-of-line)
209 (looking-at comint-prompt-regexp)))
210 (save-excursion
211 (goto-char (ielm-pm))
212 (newline 1)))
213 (newline-and-indent)))
214 (newline)))
216 (defun ielm-input-sender (proc input)
217 ;; Just sets the variable ielm-input, which is in the scope of
218 ;; `ielm-send-input's call.
219 (setq ielm-input input))
221 (defun ielm-send-input nil
222 "Evaluate the Emacs Lisp expression after the prompt."
223 (interactive)
224 (let ((buf (current-buffer))
225 ielm-input) ; set by ielm-input-sender
226 (comint-send-input) ; update history, markers etc.
227 (ielm-eval-input ielm-input)))
229 ;;; Utility functions
231 (defun ielm-is-whitespace (string)
232 "Return non-nil if STRING is all whitespace."
233 (or (string= string "") (string-match "\\`[ \t\n]+\\'" string)))
235 (defun ielm-format-errors (errlist)
236 (let ((result ""))
237 (while errlist
238 (setq result (concat result (prin1-to-string (car errlist)) ", "))
239 (setq errlist (cdr errlist)))
240 (substring result 0 -2)))
243 (defun ielm-format-error (err)
244 ;; Return a string form of the error ERR.
245 (format "%s%s"
246 (or (get (car err) 'error-message) "Peculiar error")
247 (if (cdr err)
248 (format ": %s" (ielm-format-errors (cdr err)))
249 "")))
251 ;;; Evaluation
253 (defun ielm-eval-input (ielm-string)
254 "Evaluate the Lisp expression IELM-STRING, and pretty-print the result."
255 ;; This is the function that actually `sends' the input to the
256 ;; `inferior Lisp process'. All comint-send-input does is works out
257 ;; what that input is. What this function does is evaluates that
258 ;; input and produces `output' which gets inserted into the buffer,
259 ;; along with a new prompt. A better way of doing this might have
260 ;; been to actually send the output to the `cat' process, and write
261 ;; this as in output filter that converted sexps in the output
262 ;; stream to their evaluated value. But that would have involved
263 ;; more process coordination than I was happy to deal with.
265 ;; NOTE: all temporary variables in this function will be in scope
266 ;; during the eval, and so need to have non-clashing names.
267 (let (ielm-form ; form to evaluate
268 ielm-pos ; End posn of parse in string
269 ielm-result ; Result, or error message
270 ielm-error-type ; string, nil if no error
271 (ielm-output "") ; result to display
272 (ielm-wbuf ielm-working-buffer) ; current buffer after evaluation
273 (ielm-pmark (ielm-pm)))
274 (if (not (ielm-is-whitespace ielm-string))
275 (progn
276 (condition-case err
277 (let (rout)
278 (setq rout (read-from-string ielm-string))
279 (setq ielm-form (car rout))
280 (setq ielm-pos (cdr rout)))
281 (error (setq ielm-result (ielm-format-error err))
282 (setq ielm-error-type "Read error")))
283 (if ielm-error-type nil
284 ;; Make sure working buffer has not been killed
285 (if (not (buffer-name ielm-working-buffer))
286 (setq ielm-result "Working buffer has been killed"
287 ielm-error-type "IELM Error"
288 ielm-wbuf (current-buffer))
289 (if (ielm-is-whitespace (substring ielm-string ielm-pos))
290 ;; need this awful let convolution to work around
291 ;; an Emacs bug involving local vbls and let binding
292 (let ((:save :)
293 (::save ::)
294 (:::save :::))
295 (save-excursion
296 (set-buffer ielm-working-buffer)
297 (condition-case err
298 (let ((: :save)
299 (:: ::save)
300 (::: :::save)
301 (ielm-obuf (current-buffer)))
302 (setq ielm-result (eval ielm-form))
303 (setq ielm-wbuf (current-buffer))
304 ;; The eval may have changed current-buffer;
305 ;; need to set it back here to avoid a bug
306 ;; in let. Don't want to use save-excursion
307 ;; because we want to allow changes in point.
308 (set-buffer ielm-obuf))
309 (error (setq ielm-result (ielm-format-error err))
310 (setq ielm-error-type "Eval error"))
311 (quit (setq ielm-result "Quit during evaluation")
312 (setq ielm-error-type "Eval error")))))
313 (setq ielm-error-type "IELM error")
314 (setq ielm-result "More than one sexp in input"))))
316 ;; If the eval changed the current buffer, mention it here
317 (if (eq ielm-wbuf ielm-working-buffer) nil
318 (message "current buffer is now: %s" ielm-wbuf)
319 (setq ielm-working-buffer ielm-wbuf))
321 (goto-char ielm-pmark)
322 (if (not ielm-error-type)
323 (condition-case err
324 ;; Self-referential objects cause loops in the printer, so
325 ;; trap quits here. May as well do errors, too
326 (setq ielm-output (concat ielm-output (pp-to-string ielm-result)))
327 (error (setq ielm-error-type "IELM Error")
328 (setq ielm-result "Error during pretty-printing (bug in pp)"))
329 (quit (setq ielm-error-type "IELM Error")
330 (setq ielm-result "Quit during pretty-printing"))))
331 (if ielm-error-type
332 (progn
333 (if ielm-noisy (ding))
334 (setq ielm-output (concat ielm-output "*** " ielm-error-type " *** "))
335 (setq ielm-output (concat ielm-output ielm-result)))
336 ;; There was no error, so shift the ::: values
337 (setq ::: ::)
338 (setq :: :)
339 (setq : ielm-result))
340 (setq ielm-output (concat ielm-output "\n"))))
341 (setq ielm-output (concat ielm-output ielm-prompt))
342 (comint-output-filter (ielm-process) ielm-output)))
344 ;;; Process and marker utilities
346 (defun ielm-process nil
347 ;; Return the current buffer's process.
348 (get-buffer-process (current-buffer)))
350 (defun ielm-pm nil
351 ;; Return the process mark of the current buffer.
352 (process-mark (get-buffer-process (current-buffer))))
354 (defun ielm-set-pm (pos)
355 ;; Set the process mark in the current buffer to POS.
356 (set-marker (process-mark (get-buffer-process (current-buffer))) pos))
358 ;;; Major mode
360 (defun inferior-emacs-lisp-mode nil
361 "Major mode for interactively evaluating Emacs Lisp expressions.
362 Uses the interface provided by `comint-mode' (which see).
364 * \\<ielm-map>\\[ielm-send-input] evaluates the sexp following the prompt. There must be at most
365 one top-level sexp per prompt.
367 * \\[ielm-return] inserts a newline and indents, or evaluates a
368 complete expression (but see variable `ielm-dynamic-return').
369 Inputs longer than one line are moved to the line following the
370 prompt (but see variable `ielm-dynamic-multiline-inputs').
372 * \\[comint-dynamic-complete] completes Lisp symbols (or filenames, within strings),
373 or indents the line if there is nothing to complete.
375 During evaluations, the values of the variables `:', `::', and `:::'
376 are the results of the previous, second previous and third previous
377 evaluations respectively.
379 The current working buffer may be changed (with a call to
380 `set-buffer', or with \\[ielm-change-working-buffer]), and its value
381 is preserved between successive evaluations. In this way, expressions
382 may be evaluated in a different buffer than the *ielm* buffer.
383 Display the name of the working buffer with \\[ielm-print-working-buffer],
384 or the buffer itself with \\[ielm-display-working-buffer].
386 Expressions evaluated by IELM are not subject to `debug-on-quit' or
387 `debug-on-error'.
389 The behaviour of IELM may be customised with the following variables:
390 * To stop beeping on error, set `ielm-noisy' to nil
391 * If you don't like the prompt, you can change it by setting `ielm-prompt'.
392 * Set `ielm-dynamic-return' to nil for bindings like `lisp-interaction-mode'
393 * Entry to this mode runs `comint-mode-hook' and `ielm-mode-hook'
394 (in that order).
396 Customised bindings may be defined in `ielm-map', which currently contains:
397 \\{ielm-map}"
398 (interactive)
399 (comint-mode)
400 (setq comint-prompt-regexp (concat "^" (regexp-quote ielm-prompt)))
401 (make-local-variable 'paragraph-start)
402 (setq paragraph-start comint-prompt-regexp)
403 (setq comint-input-sender 'ielm-input-sender)
404 (setq comint-process-echoes nil)
405 (setq comint-dynamic-complete-functions
406 '(ielm-tab comint-replace-by-expanded-history ielm-complete-filename ielm-complete-symbol))
407 (setq comint-get-old-input 'ielm-get-old-input)
408 (make-local-variable 'comint-completion-addsuffix)
409 (setq comint-completion-addsuffix
410 (cons (char-to-string directory-sep-char) ""))
412 (setq major-mode 'inferior-emacs-lisp-mode)
413 (setq mode-name "IELM")
414 (use-local-map ielm-map)
415 (set-syntax-table emacs-lisp-mode-syntax-table)
417 (make-local-variable 'indent-line-function)
418 (make-local-variable 'ielm-working-buffer)
419 (setq ielm-working-buffer (current-buffer))
420 (setq indent-line-function 'ielm-indent-line)
421 (make-local-variable 'fill-paragraph-function)
422 (setq fill-paragraph-function 'lisp-fill-paragraph)
424 ;; Value holders
425 (setq : nil)
426 (make-local-variable ':)
427 (setq :: nil)
428 (make-local-variable '::)
429 (setq ::: nil)
430 (make-local-variable ':::)
432 ;; font-lock support
433 (make-local-variable 'font-lock-defaults)
434 (setq font-lock-defaults
435 '(ielm-font-lock-keywords nil nil ((?: . "w") (?- . "w") (?* . "w"))))
437 ;; A dummy process to keep comint happy. It will never get any input
438 (if (comint-check-proc (current-buffer)) nil
439 (start-process "ielm" (current-buffer) "cat")
440 (process-kill-without-query (ielm-process))
441 (goto-char (point-max))
442 ;; Add a silly header
443 (insert ielm-header)
444 (ielm-set-pm (point-max))
445 (comint-output-filter (ielm-process) ielm-prompt)
446 (set-marker comint-last-input-start (ielm-pm))
447 (set-process-filter (get-buffer-process (current-buffer)) 'comint-output-filter))
448 (run-hooks 'ielm-mode-hook))
450 (defun ielm-get-old-input nil
451 ;; Return the previous input surrounding point
452 (save-excursion
453 (beginning-of-line)
454 (if (looking-at comint-prompt-regexp) nil
455 (re-search-backward comint-prompt-regexp))
456 (comint-skip-prompt)
457 (buffer-substring (point) (progn (forward-sexp 1) (point)))))
459 ;;; User command
461 ;;;###autoload (add-hook 'same-window-buffer-names "*ielm*")
463 ;;;###autoload
464 (defun ielm nil
465 "Interactively evaluate Emacs Lisp expressions.
466 Switches to the buffer `*ielm*', or creates it if it does not exist."
467 (interactive)
468 (if (comint-check-proc "*ielm*")
470 (save-excursion
471 (set-buffer (get-buffer-create "*ielm*"))
472 (inferior-emacs-lisp-mode)))
473 (pop-to-buffer "*ielm*"))
475 ;;; ielm.el ends here