ielm.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. ;;; ielm.el --- interaction mode for Emacs Lisp -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1994, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: David Smith <maa036@lancaster.ac.uk>
  4. ;; Maintainer: emacs-devel@gnu.org
  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 init 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 '(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" 'ielm-tab)
  135. (define-key map "\C-m" 'ielm-return)
  136. (define-key map "\e\C-m" 'ielm-return-for-effect)
  137. (define-key map "\C-j" 'ielm-send-input)
  138. (define-key map "\e\C-x" 'eval-defun) ; for consistency with
  139. (define-key map "\e\t" 'completion-at-point) ; lisp-interaction-mode
  140. ;; These bindings are from `lisp-mode-shared-map' -- can you inherit
  141. ;; from more than one keymap??
  142. (define-key map "\e\C-q" 'indent-sexp)
  143. (define-key map "\177" 'backward-delete-char-untabify)
  144. ;; Some convenience bindings for setting the working buffer
  145. (define-key map "\C-c\C-b" 'ielm-change-working-buffer)
  146. (define-key map "\C-c\C-f" 'ielm-display-working-buffer)
  147. (define-key map "\C-c\C-v" 'ielm-print-working-buffer)
  148. map)
  149. "Keymap for IELM mode.")
  150. (defvaralias 'inferior-emacs-lisp-mode-map 'ielm-map)
  151. (easy-menu-define ielm-menu ielm-map
  152. "IELM mode menu."
  153. '("IELM"
  154. ["Change Working Buffer" ielm-change-working-buffer t]
  155. ["Display Working Buffer" ielm-display-working-buffer t]
  156. ["Print Working Buffer" ielm-print-working-buffer t]))
  157. (defvar ielm-font-lock-keywords
  158. '(("\\(^\\*\\*\\*[^*]+\\*\\*\\*\\)\\(.*$\\)"
  159. (1 font-lock-comment-face)
  160. (2 font-lock-constant-face)))
  161. "Additional expressions to highlight in IELM buffers.")
  162. ;;; Completion stuff
  163. (defun ielm-tab ()
  164. "Indent or complete."
  165. (interactive)
  166. (if (or (eq (preceding-char) ?\n)
  167. (eq (char-syntax (preceding-char)) ?\s))
  168. (ielm-indent-line)
  169. (completion-at-point)))
  170. (defun ielm-complete-filename nil
  171. "Dynamically complete filename before point, if in a string."
  172. (when (nth 3 (parse-partial-sexp comint-last-input-start (point)))
  173. (comint-filename-completion)))
  174. (defun ielm-indent-line nil
  175. "Indent the current line as Lisp code if it is not a prompt line."
  176. (when (save-excursion (comint-bol t) (bolp))
  177. (lisp-indent-line)))
  178. ;;; Working buffer manipulation
  179. (defun ielm-print-working-buffer nil
  180. "Print the current IELM working buffer's name in the echo area."
  181. (interactive)
  182. (message "The current working buffer is: %s" (buffer-name ielm-working-buffer)))
  183. (defun ielm-display-working-buffer nil
  184. "Display the current IELM working buffer.
  185. Don't forget that selecting that buffer will change its value of `point'
  186. to its value of `window-point'!"
  187. (interactive)
  188. (display-buffer ielm-working-buffer)
  189. (ielm-print-working-buffer))
  190. (defun ielm-change-working-buffer (buf)
  191. "Change the current IELM working buffer to BUF.
  192. This is the buffer in which all sexps entered at the IELM prompt are
  193. evaluated. You can achieve the same effect with a call to
  194. `set-buffer' at the IELM prompt."
  195. (interactive "bSet working buffer to: ")
  196. (let ((buffer (get-buffer buf)))
  197. (if (and buffer (buffer-live-p buffer))
  198. (setq ielm-working-buffer buffer)
  199. (error "No such buffer: %S" buf)))
  200. (ielm-print-working-buffer))
  201. ;;; Other bindings
  202. (defun ielm-return (&optional for-effect)
  203. "Newline and indent, or evaluate the sexp before the prompt.
  204. Complete sexps are evaluated; for incomplete sexps inserts a newline
  205. and indents. If however `ielm-dynamic-return' is nil, this always
  206. simply inserts a newline."
  207. (interactive)
  208. (if ielm-dynamic-return
  209. (let ((state
  210. (save-excursion
  211. (end-of-line)
  212. (parse-partial-sexp (ielm-pm)
  213. (point)))))
  214. (if (and (< (car state) 1) (not (nth 3 state)))
  215. (ielm-send-input for-effect)
  216. (when (and ielm-dynamic-multiline-inputs
  217. (save-excursion
  218. (beginning-of-line)
  219. (looking-at-p comint-prompt-regexp)))
  220. (save-excursion
  221. (goto-char (ielm-pm))
  222. (newline 1)))
  223. (newline-and-indent)))
  224. (newline)))
  225. (defun ielm-return-for-effect ()
  226. "Like `ielm-return', but do not print the result."
  227. (interactive)
  228. (ielm-return t))
  229. (defvar ielm-input)
  230. (defun ielm-input-sender (_proc input)
  231. ;; Just sets the variable ielm-input, which is in the scope of
  232. ;; `ielm-send-input's call.
  233. (setq ielm-input input))
  234. (defun ielm-send-input (&optional for-effect)
  235. "Evaluate the Emacs Lisp expression after the prompt."
  236. (interactive)
  237. (let (ielm-input) ; set by ielm-input-sender
  238. (comint-send-input) ; update history, markers etc.
  239. (ielm-eval-input ielm-input for-effect)))
  240. ;;; Utility functions
  241. (defun ielm-is-whitespace-or-comment (string)
  242. "Return non-nil if STRING is all whitespace or a comment."
  243. (or (string= string "")
  244. (string-match-p "\\`[ \t\n]*\\(?:;.*\\)*\\'" string)))
  245. ;;; Evaluation
  246. (defun ielm-standard-output-impl (process)
  247. "Return a function to use for `standard-output' while in ielm eval.
  248. The returned function takes one character as input. Passing nil
  249. to this function instead of a character flushes the output
  250. buffer. Passing t appends a terminating newline if the buffer is
  251. nonempty, then flushes the buffer."
  252. ;; Use an intermediate output buffer because doing redisplay for
  253. ;; each character we output is too expensive. Set up a flush timer
  254. ;; so that users don't have to wait for whole lines to appear before
  255. ;; seeing output.
  256. (let* ((output-buffer nil)
  257. (flush-timer nil)
  258. (flush-buffer
  259. (lambda ()
  260. (comint-output-filter
  261. process
  262. (apply #'string (nreverse output-buffer)))
  263. (redisplay)
  264. (setf output-buffer nil)
  265. (when flush-timer
  266. (cancel-timer flush-timer)
  267. (setf flush-timer nil)))))
  268. (lambda (char)
  269. (let (flush-now)
  270. (cond ((and (eq char t) output-buffer)
  271. (push ?\n output-buffer)
  272. (setf flush-now t))
  273. ((characterp char)
  274. (push char output-buffer)))
  275. (if flush-now
  276. (funcall flush-buffer)
  277. (unless flush-timer
  278. (setf flush-timer (run-with-timer 0.1 nil flush-buffer))))))))
  279. (defun ielm-eval-input (input-string &optional for-effect)
  280. "Evaluate the Lisp expression INPUT-STRING, and pretty-print the result."
  281. ;; This is the function that actually `sends' the input to the
  282. ;; `inferior Lisp process'. All comint-send-input does is works out
  283. ;; what that input is. What this function does is evaluates that
  284. ;; input and produces `output' which gets inserted into the buffer,
  285. ;; along with a new prompt. A better way of doing this might have
  286. ;; been to actually send the output to the `cat' process, and write
  287. ;; this as in output filter that converted sexps in the output
  288. ;; stream to their evaluated value. But that would have involved
  289. ;; more process coordination than I was happy to deal with.
  290. (let ((string input-string) ; input expression, as a string
  291. form ; form to evaluate
  292. pos ; End posn of parse in string
  293. result ; Result, or error message
  294. error-type ; string, nil if no error
  295. (output "") ; result to display
  296. (wbuf ielm-working-buffer) ; current buffer after evaluation
  297. (pmark (ielm-pm)))
  298. (unless (ielm-is-whitespace-or-comment string)
  299. (condition-case err
  300. (let ((rout (read-from-string string)))
  301. (setq form (car rout)
  302. pos (cdr rout)))
  303. (error (setq result (error-message-string err))
  304. (setq error-type "Read error")))
  305. (unless error-type
  306. ;; Make sure working buffer has not been killed
  307. (if (not (buffer-name ielm-working-buffer))
  308. (setq result "Working buffer has been killed"
  309. error-type "IELM Error"
  310. wbuf (current-buffer))
  311. (if (ielm-is-whitespace-or-comment (substring string pos))
  312. ;; To correctly handle the ielm-local variables *,
  313. ;; ** and ***, we need a temporary buffer to be
  314. ;; current at entry to the inner of the next two let
  315. ;; forms. We need another temporary buffer to exit
  316. ;; that same let. To avoid problems, neither of
  317. ;; these buffers should be alive during the
  318. ;; evaluation of form.
  319. (let* ((*1 *)
  320. (*2 **)
  321. (*3 ***)
  322. (active-process (ielm-process))
  323. (old-standard-output standard-output)
  324. new-standard-output
  325. ielm-temp-buffer)
  326. (set-match-data ielm-match-data)
  327. (save-excursion
  328. (with-temp-buffer
  329. (condition-case err
  330. (unwind-protect
  331. ;; The next let form creates default
  332. ;; bindings for *, ** and ***. But
  333. ;; these default bindings are
  334. ;; identical to the ielm-local
  335. ;; bindings. Hence, during the
  336. ;; evaluation of form, the
  337. ;; ielm-local values are going to be
  338. ;; used in all buffers except for
  339. ;; other ielm buffers, which override
  340. ;; them. Normally, the variables *1,
  341. ;; *2 and *3 also have default
  342. ;; bindings, which are not overridden.
  343. (let ((* *1)
  344. (** *2)
  345. (*** *3))
  346. (when (eq standard-output t)
  347. (setf new-standard-output
  348. (ielm-standard-output-impl
  349. active-process))
  350. (setf standard-output new-standard-output))
  351. (kill-buffer (current-buffer))
  352. (set-buffer wbuf)
  353. (setq result
  354. (eval form lexical-binding))
  355. (setq wbuf (current-buffer))
  356. (setq
  357. ielm-temp-buffer
  358. (generate-new-buffer " *ielm-temp*"))
  359. (set-buffer ielm-temp-buffer))
  360. (when ielm-temp-buffer
  361. (kill-buffer ielm-temp-buffer))
  362. (when (eq new-standard-output standard-output)
  363. (ignore-errors
  364. (funcall standard-output t))
  365. (setf standard-output old-standard-output)))
  366. (error (setq result (error-message-string err))
  367. (setq error-type "Eval error"))
  368. (quit (setq result "Quit during evaluation")
  369. (setq error-type "Eval error")))))
  370. (setq ielm-match-data (match-data)))
  371. (setq error-type "IELM error")
  372. (setq result "More than one sexp in input"))))
  373. ;; If the eval changed the current buffer, mention it here
  374. (unless (eq wbuf ielm-working-buffer)
  375. (message "current buffer is now: %s" wbuf)
  376. (setq ielm-working-buffer wbuf))
  377. (goto-char pmark)
  378. (unless error-type
  379. (condition-case nil
  380. ;; Self-referential objects cause loops in the printer, so
  381. ;; trap quits here. May as well do errors, too
  382. (unless for-effect
  383. (setq output (concat output (pp-to-string result)
  384. (let ((str (eval-expression-print-format result)))
  385. (if str (propertize str 'font-lock-face 'shadow))))))
  386. (error (setq error-type "IELM Error")
  387. (setq result "Error during pretty-printing (bug in pp)"))
  388. (quit (setq error-type "IELM Error")
  389. (setq result "Quit during pretty-printing"))))
  390. (if error-type
  391. (progn
  392. (when ielm-noisy (ding))
  393. (setq output (concat output "*** " error-type " *** "))
  394. (setq output (concat output result)))
  395. ;; There was no error, so shift the *** values
  396. (setq *** **)
  397. (setq ** *)
  398. (setq * result))
  399. (when (or (not for-effect) (not (equal output "")))
  400. (setq output (concat output "\n"))))
  401. (setq output (concat output ielm-prompt-internal))
  402. (comint-output-filter (ielm-process) output)))
  403. ;;; Process and marker utilities
  404. (defun ielm-process nil
  405. ;; Return the current buffer's process.
  406. (get-buffer-process (current-buffer)))
  407. (defun ielm-pm nil
  408. ;; Return the process mark of the current buffer.
  409. (process-mark (get-buffer-process (current-buffer))))
  410. (defun ielm-set-pm (pos)
  411. ;; Set the process mark in the current buffer to POS.
  412. (set-marker (process-mark (get-buffer-process (current-buffer))) pos))
  413. ;;; Major mode
  414. (define-derived-mode inferior-emacs-lisp-mode comint-mode "IELM"
  415. "Major mode for interactively evaluating Emacs Lisp expressions.
  416. Uses the interface provided by `comint-mode' (which see).
  417. * \\<ielm-map>\\[ielm-send-input] evaluates the sexp following the prompt. There must be at most
  418. one top level sexp per prompt.
  419. * \\[ielm-return] inserts a newline and indents, or evaluates a
  420. complete expression (but see variable `ielm-dynamic-return').
  421. Inputs longer than one line are moved to the line following the
  422. prompt (but see variable `ielm-dynamic-multiline-inputs').
  423. * \\[ielm-return-for-effect] works like `ielm-return', except
  424. that it doesn't print the result of evaluating the input. This
  425. functionality is useful when forms would generate voluminous
  426. output.
  427. * \\[completion-at-point] completes Lisp symbols (or filenames, within strings),
  428. or indents the line if there is nothing to complete.
  429. The current working buffer may be changed (with a call to `set-buffer',
  430. or with \\[ielm-change-working-buffer]), and its value is preserved between successive
  431. evaluations. In this way, expressions may be evaluated in a different
  432. buffer than the *ielm* buffer. By default, its name is shown on the
  433. mode line; you can always display it with \\[ielm-print-working-buffer], or the buffer itself
  434. with \\[ielm-display-working-buffer].
  435. During evaluations, the values of the variables `*', `**', and `***'
  436. are the results of the previous, second previous and third previous
  437. evaluations respectively. If the working buffer is another IELM
  438. buffer, then the values in the working buffer are used. The variables
  439. `*1', `*2' and `*3', yield the process buffer values.
  440. If, at the start of evaluation, `standard-output' is t (the
  441. default), `standard-output' is set to a special function that
  442. causes output to be directed to the ielm buffer.
  443. `standard-output' is restored after evaluation unless explicitly
  444. set to a different value during evaluation. You can use (princ
  445. VALUE) or (pp VALUE) to write to the ielm buffer.
  446. Expressions evaluated by IELM are not subject to `debug-on-quit' or
  447. `debug-on-error'.
  448. The behavior of IELM may be customized with the following variables:
  449. * To stop beeping on error, set `ielm-noisy' to nil.
  450. * If you don't like the prompt, you can change it by setting `ielm-prompt'.
  451. * If you do not like that the prompt is (by default) read-only, set
  452. `ielm-prompt-read-only' to nil.
  453. * Set `ielm-dynamic-return' to nil for bindings like `lisp-interaction-mode'.
  454. * Entry to this mode runs `comint-mode-hook' and `ielm-mode-hook'
  455. (in that order).
  456. Customized bindings may be defined in `ielm-map', which currently contains:
  457. \\{ielm-map}"
  458. :syntax-table emacs-lisp-mode-syntax-table
  459. (setq comint-prompt-regexp (concat "^" (regexp-quote ielm-prompt)))
  460. (set (make-local-variable 'paragraph-separate) "\\'")
  461. (set (make-local-variable 'paragraph-start) comint-prompt-regexp)
  462. (setq comint-input-sender 'ielm-input-sender)
  463. (setq comint-process-echoes nil)
  464. (set (make-local-variable 'completion-at-point-functions)
  465. '(comint-replace-by-expanded-history
  466. ielm-complete-filename elisp-completion-at-point))
  467. (add-function :before-until (local 'eldoc-documentation-function)
  468. #'elisp-eldoc-documentation-function)
  469. (set (make-local-variable 'ielm-prompt-internal) ielm-prompt)
  470. (set (make-local-variable 'comint-prompt-read-only) ielm-prompt-read-only)
  471. (setq comint-get-old-input 'ielm-get-old-input)
  472. (set (make-local-variable 'comint-completion-addsuffix) '("/" . ""))
  473. (setq mode-line-process '(":%s on " (:eval (buffer-name ielm-working-buffer))))
  474. ;; Useful for `hs-minor-mode'.
  475. (setq-local comment-start ";")
  476. (setq-local comment-use-syntax t)
  477. (set (make-local-variable 'indent-line-function) 'ielm-indent-line)
  478. (set (make-local-variable 'ielm-working-buffer) (current-buffer))
  479. (set (make-local-variable 'fill-paragraph-function) 'lisp-fill-paragraph)
  480. ;; Value holders
  481. (set (make-local-variable '*) nil)
  482. (set (make-local-variable '**) nil)
  483. (set (make-local-variable '***) nil)
  484. (set (make-local-variable 'ielm-match-data) nil)
  485. ;; font-lock support
  486. (set (make-local-variable 'font-lock-defaults)
  487. '(ielm-font-lock-keywords nil nil ((?: . "w") (?- . "w") (?* . "w"))))
  488. ;; A dummy process to keep comint happy. It will never get any input
  489. (unless (comint-check-proc (current-buffer))
  490. ;; Was cat, but on non-Unix platforms that might not exist, so
  491. ;; use hexl instead, which is part of the Emacs distribution.
  492. (condition-case nil
  493. (start-process "ielm" (current-buffer) "hexl")
  494. (file-error (start-process "ielm" (current-buffer) "cat")))
  495. (set-process-query-on-exit-flag (ielm-process) nil)
  496. (goto-char (point-max))
  497. ;; Lisp output can include raw characters that confuse comint's
  498. ;; carriage control code.
  499. (set (make-local-variable 'comint-inhibit-carriage-motion) t)
  500. ;; Add a silly header
  501. (insert ielm-header)
  502. (ielm-set-pm (point-max))
  503. (unless comint-use-prompt-regexp
  504. (let ((inhibit-read-only t))
  505. (add-text-properties
  506. (point-min) (point-max)
  507. '(rear-nonsticky t field output inhibit-line-move-field-capture t))))
  508. (comint-output-filter (ielm-process) ielm-prompt-internal)
  509. (set-marker comint-last-input-start (ielm-pm))
  510. (set-process-filter (get-buffer-process (current-buffer)) 'comint-output-filter)))
  511. (defun ielm-get-old-input nil
  512. ;; Return the previous input surrounding point
  513. (save-excursion
  514. (beginning-of-line)
  515. (unless (looking-at-p comint-prompt-regexp)
  516. (re-search-backward comint-prompt-regexp))
  517. (comint-skip-prompt)
  518. (buffer-substring (point) (progn (forward-sexp 1) (point)))))
  519. ;;; User command
  520. ;;;###autoload
  521. (defun ielm nil
  522. "Interactively evaluate Emacs Lisp expressions.
  523. Switches to the buffer `*ielm*', or creates it if it does not exist.
  524. See `inferior-emacs-lisp-mode' for details."
  525. (interactive)
  526. (let (old-point)
  527. (unless (comint-check-proc "*ielm*")
  528. (with-current-buffer (get-buffer-create "*ielm*")
  529. (unless (zerop (buffer-size)) (setq old-point (point)))
  530. (inferior-emacs-lisp-mode)))
  531. (pop-to-buffer-same-window "*ielm*")
  532. (when old-point (push-mark old-point))))
  533. (provide 'ielm)
  534. ;;; ielm.el ends here