1 ;;; ielm.el --- interaction mode for Emacs Lisp -*- lexical-binding: t -*-
3 ;; Copyright (C) 1994, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: David Smith <maa036@lancaster.ac.uk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 25 Feb 1994
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 ;; Provides a nice interface to evaluating Emacs Lisp expressions.
28 ;; Input is handled by the comint package, and output is passed
29 ;; through the pretty-printer.
31 ;; To start: M-x ielm. Type C-h m in the *ielm* buffer for more info.
41 "Interaction mode for Emacs Lisp."
45 (defcustom ielm-noisy t
46 "If non-nil, IELM will beep on error."
50 (defcustom ielm-prompt-read-only t
51 "If non-nil, the IELM prompt is read only.
52 The read only region includes the newline before the prompt.
53 Setting this variable does not affect existing IELM runs.
54 This works by setting the buffer-local value of `comint-prompt-read-only'.
55 Setting that value directly affects new prompts in the current buffer.
57 If this option is enabled, then the safe way to temporarily
58 override the read-only-ness of IELM prompts is to call
59 `comint-kill-whole-line' or `comint-kill-region' with no
60 narrowing in effect. This way you will be certain that none of
61 the remaining prompts will be accidentally messed up. You may
62 wish to put something like the following in your init file:
64 \(add-hook \\='ielm-mode-hook
66 (define-key ielm-map \"\\C-w\" \\='comint-kill-region)
67 (define-key ielm-map [C-S-backspace]
68 \\='comint-kill-whole-line)))
70 If you set `comint-prompt-read-only' to t, you might wish to use
71 `comint-mode-hook' and `comint-mode-map' instead of
72 `ielm-mode-hook' and `ielm-map'. That will affect all comint
73 buffers, including IELM buffers. If you sometimes use IELM on
74 text-only terminals or with `emacs -nw', you might wish to use
75 another binding for `comint-kill-whole-line'."
80 (defcustom ielm-prompt
"ELISP> "
82 Setting this variable does not affect existing IELM runs.
84 Interrupting the IELM process with \\<ielm-map>\\[comint-interrupt-subjob],
85 and then restarting it using \\[ielm], makes the then current
86 default value affect _new_ prompts. Unless the new prompt
87 differs only in text properties from the old one, IELM will no
88 longer recognize the old prompts. However, executing \\[ielm]
89 does not update the prompt of an *ielm* buffer with a running process.
90 For IELM buffers that are not called `*ielm*', you can execute
91 \\[inferior-emacs-lisp-mode] in that IELM buffer to update the value,
92 for new prompts. This works even if the buffer has a running process."
96 (defvar ielm-prompt-internal
"ELISP> "
97 "Stored value of `ielm-prompt' in the current IELM buffer.
98 This is an internal variable used by IELM. Its purpose is to
99 prevent a running IELM process from being messed up when the user
100 customizes `ielm-prompt'.")
102 (defcustom ielm-dynamic-return t
103 "Controls whether \\<ielm-map>\\[ielm-return] has intelligent behavior in IELM.
104 If non-nil, \\[ielm-return] evaluates input for complete sexps, or inserts a newline
105 and indents for incomplete sexps. If nil, always inserts newlines."
109 (defcustom ielm-dynamic-multiline-inputs t
110 "Force multiline inputs to start from column zero?
111 If non-nil, after entering the first line of an incomplete sexp, a newline
112 will be inserted after the prompt, moving the input to the next line.
113 This gives more frame width for large indented sexps, and allows functions
114 such as `edebug-defun' to work with such inputs."
118 (defcustom ielm-mode-hook nil
119 "Hooks to be run when IELM (`inferior-emacs-lisp-mode') is started."
120 :options
'(eldoc-mode)
123 (defvaralias 'inferior-emacs-lisp-mode-hook
'ielm-mode-hook
)
126 "Most recent value evaluated in IELM.")
129 "Second-most-recent value evaluated in IELM.")
132 "Third-most-recent value evaluated in IELM.")
134 (defvar ielm-match-data nil
135 "Match data saved at the end of last command.")
138 "During IELM evaluation, most recent value evaluated in IELM.
139 Normally identical to `*'. However, if the working buffer is an IELM
140 buffer, distinct from the process buffer, then `*' gives the value in
141 the working buffer, `*1' the value in the process buffer.
142 The intended value is only accessible during IELM evaluation.")
145 "During IELM evaluation, second-most-recent value evaluated in IELM.
146 Normally identical to `**'. However, if the working buffer is an IELM
147 buffer, distinct from the process buffer, then `**' gives the value in
148 the working buffer, `*2' the value in the process buffer.
149 The intended value is only accessible during IELM evaluation.")
152 "During IELM evaluation, third-most-recent value evaluated in IELM.
153 Normally identical to `***'. However, if the working buffer is an IELM
154 buffer, distinct from the process buffer, then `***' gives the value in
155 the working buffer, `*3' the value in the process buffer.
156 The intended value is only accessible during IELM evaluation.")
160 (defvar ielm-working-buffer nil
161 "Buffer in which IELM sexps will be evaluated.
162 This variable is buffer-local.")
165 "*** Welcome to IELM *** Type (describe-mode) for help.\n"
166 "Message to display when IELM is started.")
169 (let ((map (make-sparse-keymap)))
170 (define-key map
"\t" 'ielm-tab
)
171 (define-key map
"\C-m" 'ielm-return
)
172 (define-key map
"\e\C-m" 'ielm-return-for-effect
)
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
)
185 "Keymap for IELM mode.")
186 (defvaralias 'inferior-emacs-lisp-mode-map
'ielm-map
)
188 (easy-menu-define ielm-menu ielm-map
191 ["Change Working Buffer" ielm-change-working-buffer t
]
192 ["Display Working Buffer" ielm-display-working-buffer t
]
193 ["Print Working Buffer" ielm-print-working-buffer t
]))
195 (defvar ielm-font-lock-keywords
196 '(("\\(^\\*\\*\\*[^*]+\\*\\*\\*\\)\\(.*$\\)"
197 (1 font-lock-comment-face
)
198 (2 font-lock-constant-face
)))
199 "Additional expressions to highlight in IELM buffers.")
204 "Indent or complete."
206 (if (or (eq (preceding-char) ?
\n)
207 (eq (char-syntax (preceding-char)) ?\s
))
209 (completion-at-point)))
212 (defun ielm-complete-filename nil
213 "Dynamically complete filename before point, if in a string."
214 (when (nth 3 (parse-partial-sexp comint-last-input-start
(point)))
215 (comint-filename-completion)))
217 (defun ielm-indent-line nil
218 "Indent the current line as Lisp code if it is not a prompt line."
219 (when (save-excursion (comint-bol t
) (bolp))
222 ;;; Working buffer manipulation
224 (defun ielm-print-working-buffer nil
225 "Print the current IELM working buffer's name in the echo area."
227 (message "The current working buffer is: %s" (buffer-name ielm-working-buffer
)))
229 (defun ielm-display-working-buffer nil
230 "Display the current IELM working buffer.
231 Don't forget that selecting that buffer will change its value of `point'
232 to its value of `window-point'!"
234 (display-buffer ielm-working-buffer
)
235 (ielm-print-working-buffer))
237 (defun ielm-change-working-buffer (buf)
238 "Change the current IELM working buffer to BUF.
239 This is the buffer in which all sexps entered at the IELM prompt are
240 evaluated. You can achieve the same effect with a call to
241 `set-buffer' at the IELM prompt."
242 (interactive "bSet working buffer to: ")
243 (let ((buffer (get-buffer buf
)))
244 (if (and buffer
(buffer-live-p buffer
))
245 (setq ielm-working-buffer buffer
)
246 (error "No such buffer: %S" buf
)))
247 (ielm-print-working-buffer))
251 (defun ielm-return (&optional for-effect
)
252 "Newline and indent, or evaluate the sexp before the prompt.
253 Complete sexps are evaluated; for incomplete sexps inserts a newline
254 and indents. If however `ielm-dynamic-return' is nil, this always
255 simply inserts a newline."
257 (if ielm-dynamic-return
261 (parse-partial-sexp (ielm-pm)
263 (if (and (< (car state
) 1) (not (nth 3 state
)))
264 (ielm-send-input for-effect
)
265 (when (and ielm-dynamic-multiline-inputs
268 (looking-at-p comint-prompt-regexp
)))
270 (goto-char (ielm-pm))
272 (newline-and-indent)))
275 (defun ielm-return-for-effect ()
276 "Like `ielm-return', but do not print the result."
282 (defun ielm-input-sender (_proc input
)
283 ;; Just sets the variable ielm-input, which is in the scope of
284 ;; `ielm-send-input's call.
285 (setq ielm-input input
))
287 (defun ielm-send-input (&optional for-effect
)
288 "Evaluate the Emacs Lisp expression after the prompt."
290 (let (ielm-input) ; set by ielm-input-sender
291 (comint-send-input) ; update history, markers etc.
292 (ielm-eval-input ielm-input for-effect
)))
294 ;;; Utility functions
296 (defun ielm-is-whitespace-or-comment (string)
297 "Return non-nil if STRING is all whitespace or a comment."
298 (or (string= string
"")
299 (string-match-p "\\`[ \t\n]*\\(?:;.*\\)*\\'" string
)))
303 (defun ielm-standard-output-impl (process)
304 "Return a function to use for `standard-output' while in ielm eval.
305 The returned function takes one character as input. Passing nil
306 to this function instead of a character flushes the output
307 buffer. Passing t appends a terminating newline if the buffer is
308 nonempty, then flushes the buffer."
309 ;; Use an intermediate output buffer because doing redisplay for
310 ;; each character we output is too expensive. Set up a flush timer
311 ;; so that users don't have to wait for whole lines to appear before
313 (let* ((output-buffer nil
)
317 (comint-output-filter
319 (apply #'string
(nreverse output-buffer
)))
321 (setf output-buffer nil
)
323 (cancel-timer flush-timer
)
324 (setf flush-timer nil
)))))
327 (cond ((and (eq char t
) output-buffer
)
328 (push ?
\n output-buffer
)
331 (push char output-buffer
)))
333 (funcall flush-buffer
)
335 (setf flush-timer
(run-with-timer 0.1 nil flush-buffer
))))))))
337 (defun ielm-eval-input (input-string &optional for-effect
)
338 "Evaluate the Lisp expression INPUT-STRING, and pretty-print the result."
339 ;; This is the function that actually `sends' the input to the
340 ;; `inferior Lisp process'. All comint-send-input does is works out
341 ;; what that input is. What this function does is evaluates that
342 ;; input and produces `output' which gets inserted into the buffer,
343 ;; along with a new prompt. A better way of doing this might have
344 ;; been to actually send the output to the `cat' process, and write
345 ;; this as in output filter that converted sexps in the output
346 ;; stream to their evaluated value. But that would have involved
347 ;; more process coordination than I was happy to deal with.
348 (let ((string input-string
) ; input expression, as a string
349 form
; form to evaluate
350 pos
; End posn of parse in string
351 result
; Result, or error message
352 error-type
; string, nil if no error
353 (output "") ; result to display
354 (wbuf ielm-working-buffer
) ; current buffer after evaluation
356 (unless (ielm-is-whitespace-or-comment string
)
358 (let ((rout (read-from-string string
)))
359 (setq form
(car rout
)
361 (error (setq result
(error-message-string err
))
362 (setq error-type
"Read error")))
364 ;; Make sure working buffer has not been killed
365 (if (not (buffer-name ielm-working-buffer
))
366 (setq result
"Working buffer has been killed"
367 error-type
"IELM Error"
368 wbuf
(current-buffer))
369 (if (ielm-is-whitespace-or-comment (substring string pos
))
370 ;; To correctly handle the ielm-local variables *,
371 ;; ** and ***, we need a temporary buffer to be
372 ;; current at entry to the inner of the next two let
373 ;; forms. We need another temporary buffer to exit
374 ;; that same let. To avoid problems, neither of
375 ;; these buffers should be alive during the
376 ;; evaluation of form.
380 (active-process (ielm-process))
381 (old-standard-output standard-output
)
384 (set-match-data ielm-match-data
)
389 ;; The next let form creates default
390 ;; bindings for *, ** and ***. But
391 ;; these default bindings are
392 ;; identical to the ielm-local
393 ;; bindings. Hence, during the
394 ;; evaluation of form, the
395 ;; ielm-local values are going to be
396 ;; used in all buffers except for
397 ;; other ielm buffers, which override
398 ;; them. Normally, the variables *1,
399 ;; *2 and *3 also have default
400 ;; bindings, which are not overridden.
404 (when (eq standard-output t
)
405 (setf new-standard-output
406 (ielm-standard-output-impl
408 (setf standard-output new-standard-output
))
409 (kill-buffer (current-buffer))
412 (eval form lexical-binding
))
413 (setq wbuf
(current-buffer))
416 (generate-new-buffer " *ielm-temp*"))
417 (set-buffer ielm-temp-buffer
))
418 (when ielm-temp-buffer
419 (kill-buffer ielm-temp-buffer
))
420 (when (eq new-standard-output standard-output
)
422 (funcall standard-output t
))
423 (setf standard-output old-standard-output
)))
424 (error (setq result
(error-message-string err
))
425 (setq error-type
"Eval error"))
426 (quit (setq result
"Quit during evaluation")
427 (setq error-type
"Eval error")))))
428 (setq ielm-match-data
(match-data)))
429 (setq error-type
"IELM error")
430 (setq result
"More than one sexp in input"))))
432 ;; If the eval changed the current buffer, mention it here
433 (unless (eq wbuf ielm-working-buffer
)
434 (message "current buffer is now: %s" wbuf
)
435 (setq ielm-working-buffer wbuf
))
440 ;; Self-referential objects cause loops in the printer, so
441 ;; trap quits here. May as well do errors, too
443 (setq output
(concat output
(pp-to-string result
)
444 (let ((str (eval-expression-print-format result
)))
445 (if str
(propertize str
'font-lock-face
'shadow
))))))
446 (error (setq error-type
"IELM Error")
447 (setq result
"Error during pretty-printing (bug in pp)"))
448 (quit (setq error-type
"IELM Error")
449 (setq result
"Quit during pretty-printing"))))
452 (when ielm-noisy
(ding))
453 (setq output
(concat output
"*** " error-type
" *** "))
454 (setq output
(concat output result
)))
455 ;; There was no error, so shift the *** values
459 (when (or (not for-effect
) (not (equal output
"")))
460 (setq output
(concat output
"\n"))))
461 (setq output
(concat output ielm-prompt-internal
))
462 (comint-output-filter (ielm-process) output
)))
464 ;;; Process and marker utilities
466 (defun ielm-process nil
467 ;; Return the current buffer's process.
468 (get-buffer-process (current-buffer)))
471 ;; Return the process mark of the current buffer.
472 (process-mark (get-buffer-process (current-buffer))))
474 (defun ielm-set-pm (pos)
475 ;; Set the process mark in the current buffer to POS.
476 (set-marker (process-mark (get-buffer-process (current-buffer))) pos
))
480 (define-derived-mode inferior-emacs-lisp-mode comint-mode
"IELM"
481 "Major mode for interactively evaluating Emacs Lisp expressions.
482 Uses the interface provided by `comint-mode' (which see).
484 * \\<ielm-map>\\[ielm-send-input] evaluates the sexp following the prompt. There must be at most
485 one top level sexp per prompt.
487 * \\[ielm-return] inserts a newline and indents, or evaluates a
488 complete expression (but see variable `ielm-dynamic-return').
489 Inputs longer than one line are moved to the line following the
490 prompt (but see variable `ielm-dynamic-multiline-inputs').
492 * \\[ielm-return-for-effect] works like `ielm-return', except
493 that it doesn't print the result of evaluating the input. This
494 functionality is useful when forms would generate voluminous
497 * \\[completion-at-point] completes Lisp symbols (or filenames, within strings),
498 or indents the line if there is nothing to complete.
500 The current working buffer may be changed (with a call to `set-buffer',
501 or with \\[ielm-change-working-buffer]), and its value is preserved between successive
502 evaluations. In this way, expressions may be evaluated in a different
503 buffer than the *ielm* buffer. By default, its name is shown on the
504 mode line; you can always display it with \\[ielm-print-working-buffer], or the buffer itself
505 with \\[ielm-display-working-buffer].
507 During evaluations, the values of the variables `*', `**', and `***'
508 are the results of the previous, second previous and third previous
509 evaluations respectively. If the working buffer is another IELM
510 buffer, then the values in the working buffer are used. The variables
511 `*1', `*2' and `*3', yield the process buffer values.
513 If, at the start of evaluation, `standard-output' is t (the
514 default), `standard-output' is set to a special function that
515 causes output to be directed to the ielm buffer.
516 `standard-output' is restored after evaluation unless explicitly
517 set to a different value during evaluation. You can use (princ
518 VALUE) or (pp VALUE) to write to the ielm buffer.
520 Expressions evaluated by IELM are not subject to `debug-on-quit' or
523 The behavior of IELM may be customized with the following variables:
524 * To stop beeping on error, set `ielm-noisy' to nil.
525 * If you don't like the prompt, you can change it by setting `ielm-prompt'.
526 * If you do not like that the prompt is (by default) read-only, set
527 `ielm-prompt-read-only' to nil.
528 * Set `ielm-dynamic-return' to nil for bindings like `lisp-interaction-mode'.
529 * Entry to this mode runs `comint-mode-hook' and `ielm-mode-hook'
532 Customized bindings may be defined in `ielm-map', which currently contains:
534 :syntax-table emacs-lisp-mode-syntax-table
536 (setq comint-prompt-regexp
(concat "^" (regexp-quote ielm-prompt
)))
537 (set (make-local-variable 'paragraph-separate
) "\\'")
538 (set (make-local-variable 'paragraph-start
) comint-prompt-regexp
)
539 (setq comint-input-sender
'ielm-input-sender
)
540 (setq comint-process-echoes nil
)
541 (set (make-local-variable 'completion-at-point-functions
)
542 '(comint-replace-by-expanded-history
543 ielm-complete-filename elisp-completion-at-point
))
544 (add-function :before-until
(local 'eldoc-documentation-function
)
545 #'elisp-eldoc-documentation-function
)
546 (set (make-local-variable 'ielm-prompt-internal
) ielm-prompt
)
547 (set (make-local-variable 'comint-prompt-read-only
) ielm-prompt-read-only
)
548 (setq comint-get-old-input
'ielm-get-old-input
)
549 (set (make-local-variable 'comint-completion-addsuffix
) '("/" .
""))
550 (setq mode-line-process
'(":%s on " (:eval
(buffer-name ielm-working-buffer
))))
551 ;; Useful for `hs-minor-mode'.
552 (setq-local comment-start
";")
553 (setq-local comment-use-syntax t
)
555 (set (make-local-variable 'indent-line-function
) 'ielm-indent-line
)
556 (set (make-local-variable 'ielm-working-buffer
) (current-buffer))
557 (set (make-local-variable 'fill-paragraph-function
) 'lisp-fill-paragraph
)
560 (set (make-local-variable '*) nil
)
561 (set (make-local-variable '**) nil
)
562 (set (make-local-variable '***) nil
)
563 (set (make-local-variable 'ielm-match-data
) nil
)
566 (set (make-local-variable 'font-lock-defaults
)
567 '(ielm-font-lock-keywords nil nil
((?
: .
"w") (?- .
"w") (?
* .
"w"))))
569 ;; A dummy process to keep comint happy. It will never get any input
570 (unless (comint-check-proc (current-buffer))
571 ;; Was cat, but on non-Unix platforms that might not exist, so
572 ;; use hexl instead, which is part of the Emacs distribution.
574 (start-process "ielm" (current-buffer) "hexl")
575 (file-error (start-process "ielm" (current-buffer) "cat")))
576 (set-process-query-on-exit-flag (ielm-process) nil
)
577 (goto-char (point-max))
579 ;; Lisp output can include raw characters that confuse comint's
580 ;; carriage control code.
581 (set (make-local-variable 'comint-inhibit-carriage-motion
) t
)
583 ;; Add a silly header
585 (ielm-set-pm (point-max))
586 (unless comint-use-prompt-regexp
587 (let ((inhibit-read-only t
))
589 (point-min) (point-max)
590 '(rear-nonsticky t field output inhibit-line-move-field-capture t
))))
591 (comint-output-filter (ielm-process) ielm-prompt-internal
)
592 (set-marker comint-last-input-start
(ielm-pm))
593 (set-process-filter (get-buffer-process (current-buffer)) 'comint-output-filter
)))
595 (defun ielm-get-old-input nil
596 ;; Return the previous input surrounding point
599 (unless (looking-at-p comint-prompt-regexp
)
600 (re-search-backward comint-prompt-regexp
))
602 (buffer-substring (point) (progn (forward-sexp 1) (point)))))
608 "Interactively evaluate Emacs Lisp expressions.
609 Switches to the buffer `*ielm*', or creates it if it does not exist.
610 See `inferior-emacs-lisp-mode' for details."
613 (unless (comint-check-proc "*ielm*")
614 (with-current-buffer (get-buffer-create "*ielm*")
615 (unless (zerop (buffer-size)) (setq old-point
(point)))
616 (inferior-emacs-lisp-mode)))
617 (pop-to-buffer-same-window "*ielm*")
618 (when old-point
(push-mark old-point
))))
622 ;;; ielm.el ends here