ielm.el 25 KB

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