ielm.el 21 KB

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