Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / ielm.el
CommitLineData
813f532d 1;;; ielm.el --- interaction mode for Emacs Lisp
b578f267 2
c90f2757 3;; Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005,
5df4f04c 4;; 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
813f532d
RS
5
6;; Author: David Smith <maa036@lancaster.ac.uk>
4228277d 7;; Maintainer: FSF
813f532d
RS
8;; Created: 25 Feb 1994
9;; Keywords: lisp
10
11;; This file is part of GNU Emacs.
12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
813f532d 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
813f532d
RS
17
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.
22
23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
813f532d
RS
25
26;;; Commentary:
27
e848854a 28;; Provides a nice interface to evaluating Emacs Lisp expressions.
813f532d
RS
29;; Input is handled by the comint package, and output is passed
30;; through the pretty-printer.
31
813f532d
RS
32;; To start: M-x ielm. Type C-h m in the *ielm* buffer for more info.
33
813f532d
RS
34;;; Code:
35
36(require 'comint)
37(require 'pp)
38
39;;; User variables
40
94114394
RS
41(defgroup ielm nil
42 "Interaction mode for Emacs Lisp."
43 :group 'lisp)
44
45
46(defcustom ielm-noisy t
61c11870 47 "If non-nil, IELM will beep on error."
94114394
RS
48 :type 'boolean
49 :group 'ielm)
813f532d 50
759f960a
LT
51(defcustom ielm-prompt-read-only t
52 "If non-nil, the IELM prompt is read only.
7655d378 53The read only region includes the newline before the prompt.
759f960a 54Setting this variable does not affect existing IELM runs.
016b8f71 55This works by setting the buffer-local value of `comint-prompt-read-only'.
7655d378
LT
56Setting that value directly affects new prompts in the current buffer.
57
58If this option is enabled, then the safe way to temporarily
61c11870 59override the read-only-ness of IELM prompts is to call
7655d378
LT
60`comint-kill-whole-line' or `comint-kill-region' with no
61narrowing in effect. This way you will be certain that none of
62the remaining prompts will be accidentally messed up. You may
63wish to put something like the following in your `.emacs' file:
64
65\(add-hook 'ielm-mode-hook
66 '(lambda ()
1ab3c04f 67 (define-key ielm-map \"\\C-w\" 'comint-kill-region)
7655d378
LT
68 (define-key ielm-map [C-S-backspace]
69 'comint-kill-whole-line)))
70
71If 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
61c11870 74buffers, including IELM buffers. If you sometimes use IELM on
7655d378
LT
75text-only terminals or with `emacs -nw', you might wish to use
76another binding for `comint-kill-whole-line'."
759f960a
LT
77 :type 'boolean
78 :group 'ielm
bf247b6e 79 :version "22.1")
759f960a 80
40cf8f25 81(defcustom ielm-prompt "ELISP> "
759f960a 82 "Prompt used in IELM.
016b8f71 83Setting this variable does not affect existing IELM runs.
ee9cd72d
LT
84
85Interrupting the IELM process with \\<ielm-map>\\[comint-interrupt-subjob],
86and then restarting it using \\[ielm], makes the then current
016b8f71 87default value affect _new_ prompts. Unless the new prompt
ee9cd72d
LT
88differs only in text properties from the old one, IELM will no
89longer recognize the old prompts. However, executing \\[ielm]
90does not update the prompt of an *ielm* buffer with a running process.
9eca7c03 91For IELM buffers that are not called `*ielm*', you can execute
ee9cd72d
LT
92\\[inferior-emacs-lisp-mode] in that IELM buffer to update the value,
93for new prompts. This works even if the buffer has a running process."
40cf8f25 94 :type 'string
759f960a 95 :group 'ielm)
813f532d 96
016b8f71
LT
97(defvar ielm-prompt-internal "ELISP> "
98 "Stored value of `ielm-prompt' in the current IELM buffer.
99This is an internal variable used by IELM. Its purpose is to
100prevent a running IELM process from being messed up when the user
101customizes `ielm-prompt'.")
102
94114394 103(defcustom ielm-dynamic-return t
61c11870 104 "Controls whether \\<ielm-map>\\[ielm-return] has intelligent behavior in IELM.
e848854a 105If non-nil, \\[ielm-return] evaluates input for complete sexps, or inserts a newline
94114394
RS
106and indents for incomplete sexps. If nil, always inserts newlines."
107 :type 'boolean
108 :group 'ielm)
e848854a 109
94114394 110(defcustom ielm-dynamic-multiline-inputs t
61c11870 111 "Force multiline inputs to start from column zero?
e848854a
RS
112If non-nil, after entering the first line of an incomplete sexp, a newline
113will be inserted after the prompt, moving the input to the next line.
114This gives more frame width for large indented sexps, and allows functions
94114394
RS
115such as `edebug-defun' to work with such inputs."
116 :type 'boolean
117 :group 'ielm)
813f532d 118
94114394 119(defcustom ielm-mode-hook nil
61c11870 120 "Hooks to be run when IELM (`inferior-emacs-lisp-mode') is started."
10fea268 121 :options '(turn-on-eldoc-mode)
94114394
RS
122 :type 'hook
123 :group 'ielm)
61c11870 124(defvaralias 'inferior-emacs-lisp-mode-hook 'ielm-mode-hook)
813f532d 125
d38e7d5f
RS
126(defvar * nil
127 "Most recent value evaluated in IELM.")
128
129(defvar ** nil
130 "Second-most-recent value evaluated in IELM.")
131
132(defvar *** nil
133 "Third-most-recent value evaluated in IELM.")
690ec649 134
565f89ec
SM
135(defvar ielm-match-data nil
136 "Match data saved at the end of last command.")
137
1322516b 138(defvar *1 nil
5d9655bb
RS
139 "During IELM evaluation, most recent value evaluated in IELM.
140Normally identical to `*'. However, if the working buffer is an IELM
141buffer, distinct from the process buffer, then `*' gives the value in
71296446 142the working buffer, `*1' the value in the process buffer.
5d9655bb
RS
143The intended value is only accessible during IELM evaluation.")
144
145(defvar *2 nil
146 "During IELM evaluation, second-most-recent value evaluated in IELM.
147Normally identical to `**'. However, if the working buffer is an IELM
148buffer, distinct from the process buffer, then `**' gives the value in
149the working buffer, `*2' the value in the process buffer.
150The intended value is only accessible during IELM evaluation.")
151
152(defvar *3 nil
153 "During IELM evaluation, third-most-recent value evaluated in IELM.
154Normally identical to `***'. However, if the working buffer is an IELM
155buffer, distinct from the process buffer, then `***' gives the value in
156the working buffer, `*3' the value in the process buffer.
157The intended value is only accessible during IELM evaluation.")
158
813f532d
RS
159;;; System variables
160
161(defvar ielm-working-buffer nil
e848854a
RS
162 "Buffer in which IELM sexps will be evaluated.
163This variable is buffer-local.")
813f532d 164
690ec649 165(defvar ielm-header
0a13146e 166 "*** Welcome to IELM *** Type (describe-mode) for help.\n"
e848854a 167 "Message to display when IELM is started.")
813f532d 168
61c11870
JB
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
51ef56c4 175 (define-key map "\e\t" 'completion-at-point) ; lisp-interaction-mode
61c11870
JB
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)
813f532d 187
9d42eea3 188(defvar ielm-font-lock-keywords
016b8f71 189 '(("\\(^\\*\\*\\*[^*]+\\*\\*\\*\\)\\(.*$\\)"
883212ce
SM
190 (1 font-lock-comment-face)
191 (2 font-lock-constant-face)))
61c11870 192 "Additional expressions to highlight in IELM buffers.")
690ec649 193
813f532d
RS
194;;; Completion stuff
195
196(defun ielm-tab nil
e5927b52 197 "Possibly indent the current line as Lisp code."
813f532d 198 (interactive)
61c11870
JB
199 (when (or (eq (preceding-char) ?\n)
200 (eq (char-syntax (preceding-char)) ?\s))
201 (ielm-indent-line)
202 t))
690ec649 203
813f532d 204(defun ielm-complete-symbol nil
e5927b52 205 "Complete the Lisp symbol before point."
e848854a
RS
206 ;; A wrapper for lisp-complete symbol that returns non-nil if
207 ;; completion has occurred
813f532d 208 (let* ((btick (buffer-modified-tick))
e58df0dc 209 (cbuffer (get-buffer "*Completions*"))
813f532d
RS
210 (ctick (and cbuffer (buffer-modified-tick cbuffer))))
211 (lisp-complete-symbol)
212 ;; completion has occurred if:
690ec649 213 (or
813f532d 214 ;; the buffer has been modified
690ec649 215 (not (= btick (buffer-modified-tick)))
a7acbbe4 216 ;; a completions buffer has been modified or created
813f532d
RS
217 (if cbuffer
218 (not (= ctick (buffer-modified-tick cbuffer)))
e58df0dc 219 (get-buffer "*Completions*")))))
813f532d
RS
220
221(defun ielm-complete-filename nil
e848854a 222 "Dynamically complete filename before point, if in a string."
61c11870
JB
223 (when (nth 3 (parse-partial-sexp comint-last-input-start (point)))
224 (comint-dynamic-complete-filename)))
690ec649 225
813f532d 226(defun ielm-indent-line nil
e848854a 227 "Indent the current line as Lisp code if it is not a prompt line."
3250010d 228 (when (save-excursion (comint-bol) (bolp))
813f532d
RS
229 (lisp-indent-line)))
230
e848854a
RS
231;;; Working buffer manipulation
232
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)))
237
238(defun ielm-display-working-buffer nil
239 "Display the current IELM working buffer.
240Don't forget that selecting that buffer will change its value of `point'
241to its value of `window-point'!"
242 (interactive)
243 (display-buffer ielm-working-buffer)
244 (ielm-print-working-buffer))
245
246(defun ielm-change-working-buffer (buf)
247 "Change the current IELM working buffer to BUF.
248This is the buffer in which all sexps entered at the IELM prompt are
249evaluated. You can achieve the same effect with a call to
250`set-buffer' at the IELM prompt."
251 (interactive "bSet working buffer to: ")
61c11870
JB
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)))
e848854a
RS
256 (ielm-print-working-buffer))
257
813f532d
RS
258;;; Other bindings
259
260(defun ielm-return nil
e848854a
RS
261 "Newline and indent, or evaluate the sexp before the prompt.
262Complete sexps are evaluated; for incomplete sexps inserts a newline
263and indents. If however `ielm-dynamic-return' is nil, this always
264simply inserts a newline."
813f532d 265 (interactive)
690ec649
SS
266 (if ielm-dynamic-return
267 (let ((state
813f532d
RS
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)
61c11870
JB
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)))
813f532d
RS
281 (newline-and-indent)))
282 (newline)))
283
94aba130
RS
284(defvar ielm-input)
285
813f532d 286(defun ielm-input-sender (proc input)
690ec649 287 ;; Just sets the variable ielm-input, which is in the scope of
e848854a 288 ;; `ielm-send-input's call.
813f532d
RS
289 (setq ielm-input input))
290
291(defun ielm-send-input nil
e848854a 292 "Evaluate the Emacs Lisp expression after the prompt."
813f532d 293 (interactive)
016b8f71 294 (let (ielm-input) ; set by ielm-input-sender
813f532d
RS
295 (comint-send-input) ; update history, markers etc.
296 (ielm-eval-input ielm-input)))
297
298;;; Utility functions
299
6ad18d45 300(defun ielm-is-whitespace-or-comment (string)
2ef180f7 301 "Return non-nil if STRING is all whitespace or a comment."
61c11870
JB
302 (or (string= string "")
303 (string-match-p "\\`[ \t\n]*\\(?:;.*\\)*\\'" string)))
813f532d 304
813f532d
RS
305;;; Evaluation
306
e848854a
RS
307(defun ielm-eval-input (ielm-string)
308 "Evaluate the Lisp expression IELM-STRING, and pretty-print the result."
813f532d 309 ;; This is the function that actually `sends' the input to the
e848854a 310 ;; `inferior Lisp process'. All comint-send-input does is works out
813f532d
RS
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.
e848854a
RS
318 ;;
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
61c11870 325 (ielm-output "") ; result to display
e848854a
RS
326 (ielm-wbuf ielm-working-buffer) ; current buffer after evaluation
327 (ielm-pmark (ielm-pm)))
61c11870
JB
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"))))
390
391 ;; If the eval changed the current buffer, mention it here
a535b26e 392 (unless (eq ielm-wbuf ielm-working-buffer)
61c11870
JB
393 (message "current buffer is now: %s" ielm-wbuf)
394 (setq ielm-working-buffer ielm-wbuf))
395
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")))
016b8f71 416 (setq ielm-output (concat ielm-output ielm-prompt-internal))
e848854a 417 (comint-output-filter (ielm-process) ielm-output)))
813f532d
RS
418
419;;; Process and marker utilities
420
421(defun ielm-process nil
e848854a 422 ;; Return the current buffer's process.
813f532d
RS
423 (get-buffer-process (current-buffer)))
424
425(defun ielm-pm nil
e848854a 426 ;; Return the process mark of the current buffer.
813f532d
RS
427 (process-mark (get-buffer-process (current-buffer))))
428
429(defun ielm-set-pm (pos)
e848854a 430 ;; Set the process mark in the current buffer to POS.
813f532d
RS
431 (set-marker (process-mark (get-buffer-process (current-buffer))) pos))
432
433;;; Major mode
434
61c11870 435(define-derived-mode inferior-emacs-lisp-mode comint-mode "IELM"
e848854a
RS
436 "Major mode for interactively evaluating Emacs Lisp expressions.
437Uses the interface provided by `comint-mode' (which see).
438
66f06793
SM
439* \\<ielm-map>\\[ielm-send-input] evaluates the sexp following the prompt. There must be at most
440 one top level sexp per prompt.
813f532d 441
e848854a
RS
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').
446
690ec649 447* \\[comint-dynamic-complete] completes Lisp symbols (or filenames, within strings),
e848854a 448 or indents the line if there is nothing to complete.
813f532d 449
61c11870
JB
450The current working buffer may be changed (with a call to `set-buffer',
451or with \\[ielm-change-working-buffer]), and its value is preserved between successive
452evaluations. In this way, expressions may be evaluated in a different
453buffer than the *ielm* buffer. By default, its name is shown on the
454mode line; you can always display it with \\[ielm-print-working-buffer], or the buffer itself
455with \\[ielm-display-working-buffer].
813f532d 456
5d9655bb
RS
457During evaluations, the values of the variables `*', `**', and `***'
458are the results of the previous, second previous and third previous
459evaluations respectively. If the working buffer is another IELM
460buffer, then the values in the working buffer are used. The variables
461`*1', `*2' and `*3', yield the process buffer values.
462
e848854a
RS
463Expressions evaluated by IELM are not subject to `debug-on-quit' or
464`debug-on-error'.
813f532d 465
291cfc0c 466The behavior of IELM may be customized with the following variables:
0382cf8f 467* To stop beeping on error, set `ielm-noisy' to nil.
813f532d 468* If you don't like the prompt, you can change it by setting `ielm-prompt'.
759f960a
LT
469* If you do not like that the prompt is (by default) read-only, set
470 `ielm-prompt-read-only' to nil.
0382cf8f 471* Set `ielm-dynamic-return' to nil for bindings like `lisp-interaction-mode'.
813f532d
RS
472* Entry to this mode runs `comint-mode-hook' and `ielm-mode-hook'
473 (in that order).
474
0382cf8f 475Customized bindings may be defined in `ielm-map', which currently contains:
813f532d 476\\{ielm-map}"
61c11870
JB
477 :syntax-table emacs-lisp-mode-syntax-table
478
813f532d 479 (setq comint-prompt-regexp (concat "^" (regexp-quote ielm-prompt)))
85ab9f4e 480 (set (make-local-variable 'paragraph-separate) "\\'")
61c11870 481 (set (make-local-variable 'paragraph-start) comint-prompt-regexp)
813f532d
RS
482 (setq comint-input-sender 'ielm-input-sender)
483 (setq comint-process-echoes nil)
61c11870
JB
484 (set (make-local-variable 'comint-dynamic-complete-functions)
485 '(ielm-tab comint-replace-by-expanded-history
486 ielm-complete-filename ielm-complete-symbol))
016b8f71
LT
487 (set (make-local-variable 'ielm-prompt-internal) ielm-prompt)
488 (set (make-local-variable 'comint-prompt-read-only) ielm-prompt-read-only)
e848854a 489 (setq comint-get-old-input 'ielm-get-old-input)
61c11870 490 (set (make-local-variable 'comint-completion-addsuffix) '("/" . ""))
76364803 491 (setq mode-line-process '(":%s on " (:eval (buffer-name ielm-working-buffer))))
813f532d 492
61c11870
JB
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)
51ef56c4
SM
496 (add-hook 'completion-at-point-functions
497 'lisp-completion-at-point nil 'local)
813f532d 498
e848854a 499 ;; Value holders
61c11870
JB
500 (set (make-local-variable '*) nil)
501 (set (make-local-variable '**) nil)
502 (set (make-local-variable '***) nil)
565f89ec 503 (set (make-local-variable 'ielm-match-data) nil)
813f532d 504
9d42eea3 505 ;; font-lock support
61c11870
JB
506 (set (make-local-variable 'font-lock-defaults)
507 '(ielm-font-lock-keywords nil nil ((?: . "w") (?- . "w") (?* . "w"))))
690ec649 508
813f532d 509 ;; A dummy process to keep comint happy. It will never get any input
20b13009 510 (unless (comint-check-proc (current-buffer))
33a65176
JR
511 ;; Was cat, but on non-Unix platforms that might not exist, so
512 ;; use hexl instead, which is part of the Emacs distribution.
66f06793
SM
513 (condition-case nil
514 (start-process "ielm" (current-buffer) "hexl")
515 (file-error (start-process "ielm" (current-buffer) "cat")))
e5927b52 516 (set-process-query-on-exit-flag (ielm-process) nil)
813f532d 517 (goto-char (point-max))
71296446 518
20b13009
MB
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)
522
813f532d
RS
523 ;; Add a silly header
524 (insert ielm-header)
525 (ielm-set-pm (point-max))
85ab9f4e 526 (unless comint-use-prompt-regexp
76364803
JB
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))))
016b8f71 531 (comint-output-filter (ielm-process) ielm-prompt-internal)
813f532d 532 (set-marker comint-last-input-start (ielm-pm))
61c11870 533 (set-process-filter (get-buffer-process (current-buffer)) 'comint-output-filter)))
813f532d 534
e848854a
RS
535(defun ielm-get-old-input nil
536 ;; Return the previous input surrounding point
537 (save-excursion
538 (beginning-of-line)
61c11870 539 (unless (looking-at-p comint-prompt-regexp)
e848854a
RS
540 (re-search-backward comint-prompt-regexp))
541 (comint-skip-prompt)
542 (buffer-substring (point) (progn (forward-sexp 1) (point)))))
543
813f532d
RS
544;;; User command
545
ca0a881a 546;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*ielm*"))
9e983c78 547
e848854a 548;;;###autoload
813f532d 549(defun ielm nil
e848854a 550 "Interactively evaluate Emacs Lisp expressions.
9e983c78 551Switches to the buffer `*ielm*', or creates it if it does not exist."
813f532d 552 (interactive)
ee9cd72d
LT
553 (let (old-point)
554 (unless (comint-check-proc "*ielm*")
555 (with-current-buffer (get-buffer-create "*ielm*")
016b8f71 556 (unless (zerop (buffer-size)) (setq old-point (point)))
ee9cd72d
LT
557 (inferior-emacs-lisp-mode)))
558 (pop-to-buffer "*ielm*")
559 (when old-point (push-mark old-point))))
813f532d 560
896546cd
RS
561(provide 'ielm)
562
cbee283d 563;; arch-tag: ef60e4c0-9c4f-4bdb-8402-271313329790
e848854a 564;;; ielm.el ends here