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