* s/freebsd.h: Restore osreldate.h include.
[emacs.git] / lisp / ielm.el
blob40e87cd6709a9e0bbad27732d6927cfe0a9c5bc0
1 ;;; ielm.el --- interaction mode for Emacs Lisp
3 ;; Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Smith <maa036@lancaster.ac.uk>
7 ;; Maintainer: FSF
8 ;; Created: 25 Feb 1994
9 ;; Keywords: lisp
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 start: M-x ielm. Type C-h m in the *ielm* buffer for more info.
34 ;;; Code:
36 (require 'comint)
37 (require 'pp)
39 ;;; User variables
41 (defgroup ielm nil
42 "Interaction mode for Emacs Lisp."
43 :group 'lisp)
46 (defcustom ielm-noisy t
47 "If non-nil, IELM will beep on error."
48 :type 'boolean
49 :group 'ielm)
51 (defcustom ielm-prompt-read-only t
52 "If non-nil, the IELM prompt is read only.
53 The read only region includes the newline before the prompt.
54 Setting this variable does not affect existing IELM runs.
55 This works by setting the buffer-local value of `comint-prompt-read-only'.
56 Setting that value directly affects new prompts in the current buffer.
58 If this option is enabled, then the safe way to temporarily
59 override the read-only-ness of IELM prompts is to call
60 `comint-kill-whole-line' or `comint-kill-region' with no
61 narrowing in effect. This way you will be certain that none of
62 the remaining prompts will be accidentally messed up. You may
63 wish to put something like the following in your `.emacs' file:
65 \(add-hook 'ielm-mode-hook
66 '(lambda ()
67 (define-key ielm-map \"\\C-w\" 'comint-kill-region)
68 (define-key ielm-map [C-S-backspace]
69 'comint-kill-whole-line)))
71 If you set `comint-prompt-read-only' to t, you might wish to use
72 `comint-mode-hook' and `comint-mode-map' instead of
73 `ielm-mode-hook' and `ielm-map'. That will affect all comint
74 buffers, including IELM buffers. If you sometimes use IELM on
75 text-only terminals or with `emacs -nw', you might wish to use
76 another binding for `comint-kill-whole-line'."
77 :type 'boolean
78 :group 'ielm
79 :version "22.1")
81 (defcustom ielm-prompt "ELISP> "
82 "Prompt used in IELM.
83 Setting this variable does not affect existing IELM runs.
85 Interrupting the IELM process with \\<ielm-map>\\[comint-interrupt-subjob],
86 and then restarting it using \\[ielm], makes the then current
87 default value affect _new_ prompts. Unless the new prompt
88 differs only in text properties from the old one, IELM will no
89 longer recognize the old prompts. However, executing \\[ielm]
90 does not update the prompt of an *ielm* buffer with a running process.
91 For IELM buffers that are not called `*ielm*', you can execute
92 \\[inferior-emacs-lisp-mode] in that IELM buffer to update the value,
93 for new prompts. This works even if the buffer has a running process."
94 :type 'string
95 :group 'ielm)
97 (defvar ielm-prompt-internal "ELISP> "
98 "Stored value of `ielm-prompt' in the current IELM buffer.
99 This is an internal variable used by IELM. Its purpose is to
100 prevent a running IELM process from being messed up when the user
101 customizes `ielm-prompt'.")
103 (defcustom ielm-dynamic-return t
104 "Controls whether \\<ielm-map>\\[ielm-return] has intelligent behavior in IELM.
105 If non-nil, \\[ielm-return] evaluates input for complete sexps, or inserts a newline
106 and indents for incomplete sexps. If nil, always inserts newlines."
107 :type 'boolean
108 :group 'ielm)
110 (defcustom ielm-dynamic-multiline-inputs t
111 "Force multiline inputs to start from column zero?
112 If non-nil, after entering the first line of an incomplete sexp, a newline
113 will be inserted after the prompt, moving the input to the next line.
114 This gives more frame width for large indented sexps, and allows functions
115 such as `edebug-defun' to work with such inputs."
116 :type 'boolean
117 :group 'ielm)
119 (defcustom ielm-mode-hook nil
120 "Hooks to be run when IELM (`inferior-emacs-lisp-mode') is started."
121 :options '(turn-on-eldoc-mode)
122 :type 'hook
123 :group 'ielm)
124 (defvaralias 'inferior-emacs-lisp-mode-hook 'ielm-mode-hook)
126 (defvar * nil
127 "Most recent value evaluated in IELM.")
129 (defvar ** nil
130 "Second-most-recent value evaluated in IELM.")
132 (defvar *** nil
133 "Third-most-recent value evaluated in IELM.")
135 (defvar ielm-match-data nil
136 "Match data saved at the end of last command.")
138 (defvar *1 nil
139 "During IELM evaluation, most recent value evaluated in IELM.
140 Normally identical to `*'. However, if the working buffer is an IELM
141 buffer, distinct from the process buffer, then `*' gives the value in
142 the working buffer, `*1' the value in the process buffer.
143 The intended value is only accessible during IELM evaluation.")
145 (defvar *2 nil
146 "During IELM evaluation, second-most-recent value evaluated in IELM.
147 Normally identical to `**'. However, if the working buffer is an IELM
148 buffer, distinct from the process buffer, then `**' gives the value in
149 the working buffer, `*2' the value in the process buffer.
150 The intended value is only accessible during IELM evaluation.")
152 (defvar *3 nil
153 "During IELM evaluation, third-most-recent value evaluated in IELM.
154 Normally identical to `***'. However, if the working buffer is an IELM
155 buffer, distinct from the process buffer, then `***' gives the value in
156 the working buffer, `*3' the value in the process buffer.
157 The intended value is only accessible during IELM evaluation.")
159 ;;; System variables
161 (defvar ielm-working-buffer nil
162 "Buffer in which IELM sexps will be evaluated.
163 This variable is buffer-local.")
165 (defvar ielm-header
166 "*** Welcome to IELM *** Type (describe-mode) for help.\n"
167 "Message to display when IELM is started.")
169 (defvar ielm-map
170 (let ((map (make-sparse-keymap)))
171 (define-key map "\t" 'comint-dynamic-complete)
172 (define-key map "\C-m" 'ielm-return)
173 (define-key map "\C-j" 'ielm-send-input)
174 (define-key map "\e\C-x" 'eval-defun) ; for consistency with
175 (define-key map "\e\t" 'completion-at-point) ; lisp-interaction-mode
176 ;; These bindings are from `lisp-mode-shared-map' -- can you inherit
177 ;; from more than one keymap??
178 (define-key map "\e\C-q" 'indent-sexp)
179 (define-key map "\177" 'backward-delete-char-untabify)
180 ;; Some convenience bindings for setting the working buffer
181 (define-key map "\C-c\C-b" 'ielm-change-working-buffer)
182 (define-key map "\C-c\C-f" 'ielm-display-working-buffer)
183 (define-key map "\C-c\C-v" 'ielm-print-working-buffer)
184 map)
185 "Keymap for IELM mode.")
186 (defvaralias 'inferior-emacs-lisp-mode-map 'ielm-map)
188 (defvar ielm-font-lock-keywords
189 '(("\\(^\\*\\*\\*[^*]+\\*\\*\\*\\)\\(.*$\\)"
190 (1 font-lock-comment-face)
191 (2 font-lock-constant-face)))
192 "Additional expressions to highlight in IELM buffers.")
194 ;;; Completion stuff
196 (defun ielm-tab nil
197 "Possibly indent the current line as Lisp code."
198 (interactive)
199 (when (or (eq (preceding-char) ?\n)
200 (eq (char-syntax (preceding-char)) ?\s))
201 (ielm-indent-line)
204 (defun ielm-complete-symbol nil
205 "Complete the Lisp symbol before point."
206 ;; A wrapper for lisp-complete symbol that returns non-nil if
207 ;; completion has occurred
208 (let* ((btick (buffer-modified-tick))
209 (cbuffer (get-buffer "*Completions*"))
210 (ctick (and cbuffer (buffer-modified-tick cbuffer))))
211 (lisp-complete-symbol)
212 ;; completion has occurred if:
214 ;; the buffer has been modified
215 (not (= btick (buffer-modified-tick)))
216 ;; a completions buffer has been modified or created
217 (if cbuffer
218 (not (= ctick (buffer-modified-tick cbuffer)))
219 (get-buffer "*Completions*")))))
221 (defun ielm-complete-filename nil
222 "Dynamically complete filename before point, if in a string."
223 (when (nth 3 (parse-partial-sexp comint-last-input-start (point)))
224 (comint-dynamic-complete-filename)))
226 (defun ielm-indent-line nil
227 "Indent the current line as Lisp code if it is not a prompt line."
228 (when (save-excursion (comint-bol) (bolp))
229 (lisp-indent-line)))
231 ;;; Working buffer manipulation
233 (defun ielm-print-working-buffer nil
234 "Print the current IELM working buffer's name in the echo area."
235 (interactive)
236 (message "The current working buffer is: %s" (buffer-name ielm-working-buffer)))
238 (defun ielm-display-working-buffer nil
239 "Display the current IELM working buffer.
240 Don't forget that selecting that buffer will change its value of `point'
241 to its value of `window-point'!"
242 (interactive)
243 (display-buffer ielm-working-buffer)
244 (ielm-print-working-buffer))
246 (defun ielm-change-working-buffer (buf)
247 "Change the current IELM working buffer to BUF.
248 This is the buffer in which all sexps entered at the IELM prompt are
249 evaluated. You can achieve the same effect with a call to
250 `set-buffer' at the IELM prompt."
251 (interactive "bSet working buffer to: ")
252 (let ((buffer (get-buffer buf)))
253 (if (and buffer (buffer-live-p buffer))
254 (setq ielm-working-buffer buffer)
255 (error "No such buffer: %S" buf)))
256 (ielm-print-working-buffer))
258 ;;; Other bindings
260 (defun ielm-return nil
261 "Newline and indent, or evaluate the sexp before the prompt.
262 Complete sexps are evaluated; for incomplete sexps inserts a newline
263 and indents. If however `ielm-dynamic-return' is nil, this always
264 simply inserts a newline."
265 (interactive)
266 (if ielm-dynamic-return
267 (let ((state
268 (save-excursion
269 (end-of-line)
270 (parse-partial-sexp (ielm-pm)
271 (point)))))
272 (if (and (< (car state) 1) (not (nth 3 state)))
273 (ielm-send-input)
274 (when (and ielm-dynamic-multiline-inputs
275 (save-excursion
276 (beginning-of-line)
277 (looking-at-p comint-prompt-regexp)))
278 (save-excursion
279 (goto-char (ielm-pm))
280 (newline 1)))
281 (newline-and-indent)))
282 (newline)))
284 (defvar ielm-input)
286 (defun ielm-input-sender (proc input)
287 ;; Just sets the variable ielm-input, which is in the scope of
288 ;; `ielm-send-input's call.
289 (setq ielm-input input))
291 (defun ielm-send-input nil
292 "Evaluate the Emacs Lisp expression after the prompt."
293 (interactive)
294 (let (ielm-input) ; set by ielm-input-sender
295 (comint-send-input) ; update history, markers etc.
296 (ielm-eval-input ielm-input)))
298 ;;; Utility functions
300 (defun ielm-is-whitespace-or-comment (string)
301 "Return non-nil if STRING is all whitespace or a comment."
302 (or (string= string "")
303 (string-match-p "\\`[ \t\n]*\\(?:;.*\\)*\\'" string)))
305 ;;; Evaluation
307 (defun ielm-eval-input (ielm-string)
308 "Evaluate the Lisp expression IELM-STRING, and pretty-print the result."
309 ;; This is the function that actually `sends' the input to the
310 ;; `inferior Lisp process'. All comint-send-input does is works out
311 ;; what that input is. What this function does is evaluates that
312 ;; input and produces `output' which gets inserted into the buffer,
313 ;; along with a new prompt. A better way of doing this might have
314 ;; been to actually send the output to the `cat' process, and write
315 ;; this as in output filter that converted sexps in the output
316 ;; stream to their evaluated value. But that would have involved
317 ;; more process coordination than I was happy to deal with.
319 ;; NOTE: all temporary variables in this function will be in scope
320 ;; during the eval, and so need to have non-clashing names.
321 (let (ielm-form ; form to evaluate
322 ielm-pos ; End posn of parse in string
323 ielm-result ; Result, or error message
324 ielm-error-type ; string, nil if no error
325 (ielm-output "") ; result to display
326 (ielm-wbuf ielm-working-buffer) ; current buffer after evaluation
327 (ielm-pmark (ielm-pm)))
328 (unless (ielm-is-whitespace-or-comment ielm-string)
329 (condition-case err
330 (let ((rout (read-from-string ielm-string)))
331 (setq ielm-form (car rout)
332 ielm-pos (cdr rout)))
333 (error (setq ielm-result (error-message-string err))
334 (setq ielm-error-type "Read error")))
335 (unless ielm-error-type
336 ;; Make sure working buffer has not been killed
337 (if (not (buffer-name ielm-working-buffer))
338 (setq ielm-result "Working buffer has been killed"
339 ielm-error-type "IELM Error"
340 ielm-wbuf (current-buffer))
341 (if (ielm-is-whitespace-or-comment (substring ielm-string ielm-pos))
342 ;; To correctly handle the ielm-local variables *,
343 ;; ** and ***, we need a temporary buffer to be
344 ;; current at entry to the inner of the next two let
345 ;; forms. We need another temporary buffer to exit
346 ;; that same let. To avoid problems, neither of
347 ;; these buffers should be alive during the
348 ;; evaluation of ielm-form.
349 (let ((*1 *)
350 (*2 **)
351 (*3 ***)
352 ielm-temp-buffer)
353 (set-match-data ielm-match-data)
354 (save-excursion
355 (with-temp-buffer
356 (condition-case err
357 (unwind-protect
358 ;; The next let form creates default
359 ;; bindings for *, ** and ***. But
360 ;; these default bindings are
361 ;; identical to the ielm-local
362 ;; bindings. Hence, during the
363 ;; evaluation of ielm-form, the
364 ;; ielm-local values are going to be
365 ;; used in all buffers except for
366 ;; other ielm buffers, which override
367 ;; them. Normally, the variables *1,
368 ;; *2 and *3 also have default
369 ;; bindings, which are not overridden.
370 (let ((* *1)
371 (** *2)
372 (*** *3))
373 (kill-buffer (current-buffer))
374 (set-buffer ielm-wbuf)
375 (setq ielm-result (eval ielm-form))
376 (setq ielm-wbuf (current-buffer))
377 (setq
378 ielm-temp-buffer
379 (generate-new-buffer " *ielm-temp*"))
380 (set-buffer ielm-temp-buffer))
381 (when ielm-temp-buffer
382 (kill-buffer ielm-temp-buffer)))
383 (error (setq ielm-result (error-message-string err))
384 (setq ielm-error-type "Eval error"))
385 (quit (setq ielm-result "Quit during evaluation")
386 (setq ielm-error-type "Eval error")))))
387 (setq ielm-match-data (match-data)))
388 (setq ielm-error-type "IELM error")
389 (setq ielm-result "More than one sexp in input"))))
391 ;; If the eval changed the current buffer, mention it here
392 (unless (eq ielm-wbuf ielm-working-buffer)
393 (message "current buffer is now: %s" ielm-wbuf)
394 (setq ielm-working-buffer ielm-wbuf))
396 (goto-char ielm-pmark)
397 (unless ielm-error-type
398 (condition-case err
399 ;; Self-referential objects cause loops in the printer, so
400 ;; trap quits here. May as well do errors, too
401 (setq ielm-output (concat ielm-output (pp-to-string ielm-result)))
402 (error (setq ielm-error-type "IELM Error")
403 (setq ielm-result "Error during pretty-printing (bug in pp)"))
404 (quit (setq ielm-error-type "IELM Error")
405 (setq ielm-result "Quit during pretty-printing"))))
406 (if ielm-error-type
407 (progn
408 (when ielm-noisy (ding))
409 (setq ielm-output (concat ielm-output "*** " ielm-error-type " *** "))
410 (setq ielm-output (concat ielm-output ielm-result)))
411 ;; There was no error, so shift the *** values
412 (setq *** **)
413 (setq ** *)
414 (setq * ielm-result))
415 (setq ielm-output (concat ielm-output "\n")))
416 (setq ielm-output (concat ielm-output ielm-prompt-internal))
417 (comint-output-filter (ielm-process) ielm-output)))
419 ;;; Process and marker utilities
421 (defun ielm-process nil
422 ;; Return the current buffer's process.
423 (get-buffer-process (current-buffer)))
425 (defun ielm-pm nil
426 ;; Return the process mark of the current buffer.
427 (process-mark (get-buffer-process (current-buffer))))
429 (defun ielm-set-pm (pos)
430 ;; Set the process mark in the current buffer to POS.
431 (set-marker (process-mark (get-buffer-process (current-buffer))) pos))
433 ;;; Major mode
435 (define-derived-mode inferior-emacs-lisp-mode comint-mode "IELM"
436 "Major mode for interactively evaluating Emacs Lisp expressions.
437 Uses the interface provided by `comint-mode' (which see).
439 * \\<ielm-map>\\[ielm-send-input] evaluates the sexp following the prompt. There must be at most
440 one top level sexp per prompt.
442 * \\[ielm-return] inserts a newline and indents, or evaluates a
443 complete expression (but see variable `ielm-dynamic-return').
444 Inputs longer than one line are moved to the line following the
445 prompt (but see variable `ielm-dynamic-multiline-inputs').
447 * \\[comint-dynamic-complete] completes Lisp symbols (or filenames, within strings),
448 or indents the line if there is nothing to complete.
450 The current working buffer may be changed (with a call to `set-buffer',
451 or with \\[ielm-change-working-buffer]), and its value is preserved between successive
452 evaluations. In this way, expressions may be evaluated in a different
453 buffer than the *ielm* buffer. By default, its name is shown on the
454 mode line; you can always display it with \\[ielm-print-working-buffer], or the buffer itself
455 with \\[ielm-display-working-buffer].
457 During evaluations, the values of the variables `*', `**', and `***'
458 are the results of the previous, second previous and third previous
459 evaluations respectively. If the working buffer is another IELM
460 buffer, then the values in the working buffer are used. The variables
461 `*1', `*2' and `*3', yield the process buffer values.
463 Expressions evaluated by IELM are not subject to `debug-on-quit' or
464 `debug-on-error'.
466 The behavior of IELM may be customized with the following variables:
467 * To stop beeping on error, set `ielm-noisy' to nil.
468 * If you don't like the prompt, you can change it by setting `ielm-prompt'.
469 * If you do not like that the prompt is (by default) read-only, set
470 `ielm-prompt-read-only' to nil.
471 * Set `ielm-dynamic-return' to nil for bindings like `lisp-interaction-mode'.
472 * Entry to this mode runs `comint-mode-hook' and `ielm-mode-hook'
473 (in that order).
475 Customized bindings may be defined in `ielm-map', which currently contains:
476 \\{ielm-map}"
477 :syntax-table emacs-lisp-mode-syntax-table
479 (setq comint-prompt-regexp (concat "^" (regexp-quote ielm-prompt)))
480 (set (make-local-variable 'paragraph-separate) "\\'")
481 (set (make-local-variable 'paragraph-start) comint-prompt-regexp)
482 (setq comint-input-sender 'ielm-input-sender)
483 (setq comint-process-echoes nil)
484 (set (make-local-variable 'comint-dynamic-complete-functions)
485 '(ielm-tab comint-replace-by-expanded-history
486 ielm-complete-filename ielm-complete-symbol))
487 (set (make-local-variable 'ielm-prompt-internal) ielm-prompt)
488 (set (make-local-variable 'comint-prompt-read-only) ielm-prompt-read-only)
489 (setq comint-get-old-input 'ielm-get-old-input)
490 (set (make-local-variable 'comint-completion-addsuffix) '("/" . ""))
491 (setq mode-line-process '(":%s on " (:eval (buffer-name ielm-working-buffer))))
493 (set (make-local-variable 'indent-line-function) 'ielm-indent-line)
494 (set (make-local-variable 'ielm-working-buffer) (current-buffer))
495 (set (make-local-variable 'fill-paragraph-function) 'lisp-fill-paragraph)
496 (add-hook 'completion-at-point-functions
497 'lisp-completion-at-point nil 'local)
499 ;; Value holders
500 (set (make-local-variable '*) nil)
501 (set (make-local-variable '**) nil)
502 (set (make-local-variable '***) nil)
503 (set (make-local-variable 'ielm-match-data) nil)
505 ;; font-lock support
506 (set (make-local-variable 'font-lock-defaults)
507 '(ielm-font-lock-keywords nil nil ((?: . "w") (?- . "w") (?* . "w"))))
509 ;; A dummy process to keep comint happy. It will never get any input
510 (unless (comint-check-proc (current-buffer))
511 ;; Was cat, but on non-Unix platforms that might not exist, so
512 ;; use hexl instead, which is part of the Emacs distribution.
513 (condition-case nil
514 (start-process "ielm" (current-buffer) "hexl")
515 (file-error (start-process "ielm" (current-buffer) "cat")))
516 (set-process-query-on-exit-flag (ielm-process) nil)
517 (goto-char (point-max))
519 ;; Lisp output can include raw characters that confuse comint's
520 ;; carriage control code.
521 (set (make-local-variable 'comint-inhibit-carriage-motion) t)
523 ;; Add a silly header
524 (insert ielm-header)
525 (ielm-set-pm (point-max))
526 (unless comint-use-prompt-regexp
527 (let ((inhibit-read-only t))
528 (add-text-properties
529 (point-min) (point-max)
530 '(rear-nonsticky t field output inhibit-line-move-field-capture t))))
531 (comint-output-filter (ielm-process) ielm-prompt-internal)
532 (set-marker comint-last-input-start (ielm-pm))
533 (set-process-filter (get-buffer-process (current-buffer)) 'comint-output-filter)))
535 (defun ielm-get-old-input nil
536 ;; Return the previous input surrounding point
537 (save-excursion
538 (beginning-of-line)
539 (unless (looking-at-p comint-prompt-regexp)
540 (re-search-backward comint-prompt-regexp))
541 (comint-skip-prompt)
542 (buffer-substring (point) (progn (forward-sexp 1) (point)))))
544 ;;; User command
546 ;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*ielm*"))
548 ;;;###autoload
549 (defun ielm nil
550 "Interactively evaluate Emacs Lisp expressions.
551 Switches to the buffer `*ielm*', or creates it if it does not exist."
552 (interactive)
553 (let (old-point)
554 (unless (comint-check-proc "*ielm*")
555 (with-current-buffer (get-buffer-create "*ielm*")
556 (unless (zerop (buffer-size)) (setq old-point (point)))
557 (inferior-emacs-lisp-mode)))
558 (pop-to-buffer "*ielm*")
559 (when old-point (push-mark old-point))))
561 (provide 'ielm)
563 ;; arch-tag: ef60e4c0-9c4f-4bdb-8402-271313329790
564 ;;; ielm.el ends here