1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc.
6 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
27 ;; This mode is documented in the Emacs manual.
31 (defvar font-lock-comment-face
)
32 (defvar font-lock-doc-face
)
33 (defvar font-lock-keywords-case-fold-search
)
34 (defvar font-lock-string-face
)
36 (defvar lisp-mode-abbrev-table nil
)
37 (define-abbrev-table 'lisp-mode-abbrev-table
()
38 "Abbrev table for Lisp mode.")
40 (defvar emacs-lisp-mode-abbrev-table nil
)
41 (define-abbrev-table 'emacs-lisp-mode-abbrev-table
()
42 "Abbrev table for Emacs Lisp mode.
43 It has `lisp-mode-abbrev-table' as its parent."
44 :parents
(list lisp-mode-abbrev-table
))
46 (defvar emacs-lisp-mode-syntax-table
47 (let ((table (make-syntax-table))
50 (modify-syntax-entry i
"_ " table
)
54 (modify-syntax-entry i
"_ " table
)
58 (modify-syntax-entry i
"_ " table
)
62 (modify-syntax-entry i
"_ " table
)
64 (modify-syntax-entry ?\s
" " table
)
65 ;; Non-break space acts as whitespace.
66 (modify-syntax-entry ?
\x8a0 " " table
)
67 (modify-syntax-entry ?
\t " " table
)
68 (modify-syntax-entry ?
\f " " table
)
69 (modify-syntax-entry ?
\n "> " table
)
70 ;; This is probably obsolete since nowadays such features use overlays.
71 ;; ;; Give CR the same syntax as newline, for selective-display.
72 ;; (modify-syntax-entry ?\^m "> " table)
73 (modify-syntax-entry ?\
; "< " table)
74 (modify-syntax-entry ?
` "' " table
)
75 (modify-syntax-entry ?
' "' " table
)
76 (modify-syntax-entry ?
, "' " table
)
77 (modify-syntax-entry ?
@ "' " table
)
78 ;; Used to be singlequote; changed for flonums.
79 (modify-syntax-entry ?.
"_ " table
)
80 (modify-syntax-entry ?
# "' " table
)
81 (modify-syntax-entry ?
\" "\" " table
)
82 (modify-syntax-entry ?
\\ "\\ " table
)
83 (modify-syntax-entry ?\
( "() " table
)
84 (modify-syntax-entry ?\
) ")( " table
)
85 (modify-syntax-entry ?\
[ "(] " table
)
86 (modify-syntax-entry ?\
] ")[ " table
)
88 "Syntax table used in `emacs-lisp-mode'.")
90 (defvar lisp-mode-syntax-table
91 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
92 (modify-syntax-entry ?\
[ "_ " table
)
93 (modify-syntax-entry ?\
] "_ " table
)
94 (modify-syntax-entry ?
# "' 14" table
)
95 (modify-syntax-entry ?|
"\" 23bn" table
)
97 "Syntax table used in `lisp-mode'.")
99 (defvar lisp-imenu-generic-expression
102 (purecopy (concat "^\\s-*("
105 '("defun" "defun*" "defsubst" "defmacro"
106 "defadvice" "define-skeleton"
107 "define-minor-mode" "define-global-minor-mode"
108 "define-globalized-minor-mode"
109 "define-derived-mode" "define-generic-mode"
110 "define-compiler-macro" "define-modify-macro"
111 "defsetf" "define-setf-expander"
112 "define-method-combination"
113 "defgeneric" "defmethod"
114 "cl-defun" "cl-defsubst" "cl-defmacro"
115 "cl-define-compiler-macro") t
))
116 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
118 (list (purecopy "Variables")
119 (purecopy (concat "^\\s-*("
122 '("defconst" "defconstant" "defcustom"
123 "defparameter" "define-symbol-macro") t
))
124 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
126 ;; For `defvar', we ignore (defvar FOO) constructs.
127 (list (purecopy "Variables")
128 (purecopy (concat "^\\s-*(defvar\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"
129 "[[:space:]\n]+[^)]"))
131 (list (purecopy "Types")
132 (purecopy (concat "^\\s-*("
135 '("defgroup" "deftheme" "deftype" "defstruct"
136 "defclass" "define-condition" "define-widget"
137 "defface" "defpackage" "cl-deftype"
139 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
142 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
144 ;; This was originally in autoload.el and is still used there.
145 (put 'autoload
'doc-string-elt
3)
146 (put 'defmethod
'doc-string-elt
3)
147 (put 'defvar
'doc-string-elt
3)
148 (put 'defconst
'doc-string-elt
3)
149 (put 'defalias
'doc-string-elt
3)
150 (put 'defvaralias
'doc-string-elt
3)
151 (put 'define-category
'doc-string-elt
2)
153 (defvar lisp-doc-string-elt-property
'doc-string-elt
154 "The symbol property that holds the docstring position info.")
157 ;;;; Font-lock support.
161 ,el-defs-re
,cl-defs-re
162 ,el-kws-re
,cl-kws-re
163 ,el-errs-re
,cl-errs-re
)
165 (let ((lisp-fdefs '("defmacro" "defsubst" "defun"))
166 (lisp-vdefs '("defvar"))
167 (lisp-kw '("cond" "if" "while" "let" "let*" "progn" "prog1"
168 "prog2" "lambda" "unwind-protect" "condition-case"
169 "when" "unless" "with-output-to-string"
170 "ignore-errors" "dotimes" "dolist" "declare"))
171 (lisp-errs '("warn" "error" "signal"))
172 ;; Elisp constructs. FIXME: update dynamically from obarray.
173 (el-fdefs '("defadvice" "defalias"
174 "define-derived-mode" "define-minor-mode"
175 "define-generic-mode" "define-global-minor-mode"
176 "define-globalized-minor-mode" "define-skeleton"
178 (el-vdefs '("defconst" "defcustom" "defvaralias" "defvar-local"
180 (el-tdefs '("defgroup" "deftheme"))
181 (el-kw '("while-no-input" "letrec" "pcase" "pcase-let"
182 "pcase-let*" "save-restriction" "save-excursion"
183 "save-selected-window"
184 ;; "eval-after-load" "eval-next-after-load"
185 "save-window-excursion" "save-current-buffer"
186 "save-match-data" "combine-after-change-calls"
187 "condition-case-unless-debug" "track-mouse"
188 "eval-and-compile" "eval-when-compile" "with-case-table"
189 "with-category-table" "with-coding-priority"
190 "with-current-buffer" "with-demoted-errors"
191 "with-electric-help" "with-eval-after-load"
192 "with-local-quit" "with-no-warnings"
193 "with-output-to-temp-buffer" "with-selected-window"
194 "with-selected-frame" "with-silent-modifications"
195 "with-syntax-table" "with-temp-buffer" "with-temp-file"
196 "with-temp-message" "with-timeout"
197 "with-timeout-handler"))
198 (el-errs '("user-error"))
199 ;; Common-Lisp constructs supported by EIEIO. FIXME: namespace.
200 (eieio-fdefs '("defgeneric" "defmethod"))
201 (eieio-tdefs '("defclass"))
202 (eieio-kw '("with-slots"))
203 ;; Common-Lisp constructs supported by cl-lib.
204 (cl-lib-fdefs '("defmacro" "defsubst" "defun"))
205 (cl-lib-tdefs '("defstruct" "deftype"))
206 (cl-lib-kw '("progv" "eval-when" "case" "ecase" "typecase"
207 "etypecase" "ccase" "ctypecase" "loop" "do" "do*"
208 "the" "locally" "proclaim" "declaim" "letf" "go"
209 ;; "lexical-let" "lexical-let*"
210 "symbol-macrolet" "flet" "destructuring-bind"
211 "labels" "macrolet" "tagbody" "multiple-value-bind"
212 "block" "return" "return-from"))
213 (cl-lib-errs '("assert" "check-type"))
214 ;; Common-Lisp constructs not supported by cl-lib.
215 (cl-fdefs '("defsetf" "define-method-combination"
216 "define-condition" "define-setf-expander"
217 ;; "define-function"??
218 "define-compiler-macro" "define-modify-macro"))
219 (cl-vdefs '("define-symbol-macro" "defconstant" "defparameter"))
220 (cl-tdefs '("defpackage" "defstruct" "deftype"))
221 (cl-kw '("prog" "prog*" "handler-case" "handler-bind"
222 "in-package" "restart-case" ;; "inline"
223 "restart-bind" "break" "multiple-value-prog1"
224 "compiler-let" "with-accessors" "with-compilation-unit"
225 "with-condition-restarts" "with-hash-table-iterator"
226 "with-input-from-string" "with-open-file"
227 "with-open-stream" "with-package-iterator"
228 "with-simple-restart" "with-standard-io-syntax"))
229 (cl-errs '("abort" "cerror")))
231 (list (append lisp-vdefs el-vdefs cl-vdefs
)
232 (append el-tdefs eieio-tdefs cl-tdefs cl-lib-tdefs
233 (mapcar (lambda (s) (concat "cl-" s
)) cl-lib-tdefs
))
235 ;; Elisp and Common Lisp definers.
236 (regexp-opt (append lisp-fdefs lisp-vdefs
237 el-fdefs el-vdefs el-tdefs
238 (mapcar (lambda (s) (concat "cl-" s
))
239 (append cl-lib-fdefs cl-lib-tdefs
))
240 eieio-fdefs eieio-tdefs
)
242 (regexp-opt (append lisp-fdefs lisp-vdefs
243 cl-lib-fdefs cl-lib-tdefs
244 eieio-fdefs eieio-tdefs
245 cl-fdefs cl-vdefs cl-tdefs
)
248 ;; Elisp and Common Lisp keywords.
250 lisp-kw el-kw eieio-kw
251 (cons "go" (mapcar (lambda (s) (concat "cl-" s
))
252 (remove "go" cl-lib-kw
))))
254 (regexp-opt (append lisp-kw el-kw eieio-kw
255 (cons "go" (mapcar (lambda (s) (concat "cl-" s
))
256 (remove "go" cl-kw
))))
259 ;; Elisp and Common Lisp "errors".
260 (regexp-opt (append (mapcar (lambda (s) (concat "cl-" s
))
264 (regexp-opt (append lisp-errs cl-lib-errs cl-errs
) t
))))))
267 (put (intern v
) 'lisp-define-type
'var
))
269 (put (intern v
) 'lisp-define-type
'type
))
271 (define-obsolete-variable-alias 'lisp-font-lock-keywords-1
272 'lisp-el-font-lock-keywords-1
"24.4")
273 (defconst lisp-el-font-lock-keywords-1
275 (,(concat "(" el-defs-re
"\\_>"
276 ;; Any whitespace and defined object.
278 "\\(\\(?:\\sw\\|\\s_\\)+\\)?")
279 (1 font-lock-keyword-face
)
280 (2 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type
)))
281 (cond ((eq type
'var
) font-lock-variable-name-face
)
282 ((eq type
'type
) font-lock-type-face
)
283 (t font-lock-function-name-face
)))
285 ;; Emacs Lisp autoload cookies. Supports the slightly different
286 ;; forms used by mh-e, calendar, etc.
287 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend
))
288 "Subdued level highlighting for Emacs Lisp mode.")
290 (defconst lisp-cl-font-lock-keywords-1
292 (,(concat "(" cl-defs-re
"\\_>"
293 ;; Any whitespace and defined object.
295 "\\(setf[ \t]+\\(?:\\sw\\|\\s_\\)+\\|\\(?:\\sw\\|\\s_\\)+\\)?")
296 (1 font-lock-keyword-face
)
297 (2 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type
)))
298 (cond ((eq type
'var
) font-lock-variable-name-face
)
299 ((eq type
'type
) font-lock-type-face
)
300 (t font-lock-function-name-face
)))
302 "Subdued level highlighting for Lisp modes.")
304 (define-obsolete-variable-alias 'lisp-font-lock-keywords-2
305 'lisp-el-font-lock-keywords-2
"24.4")
306 (defconst lisp-el-font-lock-keywords-2
308 lisp-el-font-lock-keywords-1
309 `( ;; Regexp negated char group.
310 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend
)
311 ;; Control structures. Common Lisp forms.
312 (,(concat "(" el-kws-re
"\\_>") .
1)
313 ;; Exit/Feature symbols as constants.
314 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\_>"
315 "[ \t']*\\(\\(?:\\sw\\|\\s_\\)+\\)?")
316 (1 font-lock-keyword-face
)
317 (2 font-lock-constant-face nil t
))
318 ;; Erroneous structures.
319 (,(concat "(" el-errs-re
"\\_>")
320 (1 font-lock-warning-face
))
321 ;; Words inside \\[] tend to be for `substitute-command-keys'.
322 ("\\\\\\\\\\[\\(\\(?:\\sw\\|\\s_\\)+\\)\\]"
323 (1 font-lock-constant-face prepend
))
324 ;; Words inside `' tend to be symbol names.
325 ("`\\(\\(?:\\sw\\|\\s_\\)\\(?:\\sw\\|\\s_\\)+\\)'"
326 (1 font-lock-constant-face prepend
))
328 ("\\_<:\\(?:\\sw\\|\\s_\\)+\\_>" 0 font-lock-builtin-face
)
329 ;; ELisp and CLisp `&' keywords as types.
330 ("\\_<\\&\\(?:\\sw\\|\\s_\\)+\\_>" . font-lock-type-face
)
331 ;; ELisp regexp grouping constructs
334 ;; The following loop is needed to continue searching after matches
335 ;; that do not occur in strings. The associated regexp matches one
336 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
337 ;; avoid highlighting, for example, `\\(' in `\\\\('.
338 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t
)
339 (unless (match-beginning 2)
340 (let ((face (get-text-property (1- (point)) 'face
)))
341 (when (or (and (listp face
)
342 (memq 'font-lock-string-face face
))
343 (eq 'font-lock-string-face face
))
344 (throw 'found t
)))))))
345 (1 'font-lock-regexp-grouping-backslash prepend
)
346 (3 'font-lock-regexp-grouping-construct prepend
))
347 ;; This is too general -- rms.
348 ;; A user complained that he has functions whose names start with `do'
349 ;; and that they get the wrong color.
350 ;; ;; CL `with-' and `do-' constructs
351 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
353 "Gaudy level highlighting for Emacs Lisp mode.")
355 (defconst lisp-cl-font-lock-keywords-2
357 lisp-cl-font-lock-keywords-1
358 `( ;; Regexp negated char group.
359 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend
)
360 ;; Control structures. Common Lisp forms.
361 (,(concat "(" cl-kws-re
"\\_>") .
1)
362 ;; Exit/Feature symbols as constants.
363 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\_>"
364 "[ \t']*\\(\\(?:\\sw\\|\\s_\\)+\\)?")
365 (1 font-lock-keyword-face
)
366 (2 font-lock-constant-face nil t
))
367 ;; Erroneous structures.
368 (,(concat "(" cl-errs-re
"\\_>")
369 (1 font-lock-warning-face
))
370 ;; Words inside `' tend to be symbol names.
371 ("`\\(\\(?:\\sw\\|\\s_\\)\\(?:\\sw\\|\\s_\\)+\\)'"
372 (1 font-lock-constant-face prepend
))
374 ("\\_<:\\(?:\\sw\\|\\s_\\)+\\_>" 0 font-lock-builtin-face
)
375 ;; ELisp and CLisp `&' keywords as types.
376 ("\\_<\\&\\(?:\\sw\\|\\s_\\)+\\_>" . font-lock-type-face
)
377 ;; This is too general -- rms.
378 ;; A user complained that he has functions whose names start with `do'
379 ;; and that they get the wrong color.
380 ;; ;; CL `with-' and `do-' constructs
381 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
383 "Gaudy level highlighting for Lisp modes."))
385 (define-obsolete-variable-alias 'lisp-font-lock-keywords
386 'lisp-el-font-lock-keywords
"24.4")
387 (defvar lisp-el-font-lock-keywords lisp-el-font-lock-keywords-1
388 "Default expressions to highlight in Emacs Lisp mode.")
389 (defvar lisp-cl-font-lock-keywords lisp-cl-font-lock-keywords-1
390 "Default expressions to highlight in Lisp modes.")
392 (defun lisp-font-lock-syntactic-face-function (state)
394 ;; This might be a (doc)string or a |...| symbol.
395 (let ((startpos (nth 8 state
)))
396 (if (eq (char-after startpos
) ?|
)
397 ;; This is not a string, but a |...| symbol.
399 (let* ((listbeg (nth 1 state
))
400 (firstsym (and listbeg
403 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
405 (docelt (and firstsym
406 (function-get (intern-soft firstsym
)
407 lisp-doc-string-elt-property
))))
409 ;; It's a string in a form that can have a docstring.
410 ;; Check whether it's in docstring position.
412 (when (functionp docelt
)
413 (goto-char (match-end 1))
414 (setq docelt
(funcall docelt
)))
418 (while (and (> docelt
0) (< (point) startpos
)
419 (progn (forward-sexp 1) t
))
420 (setq docelt
(1- docelt
)))
422 (and (zerop docelt
) (<= (point) startpos
)
423 (progn (forward-comment (point-max)) t
)
424 (= (point) (nth 8 state
)))))
426 font-lock-string-face
))))
427 font-lock-comment-face
))
429 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
431 "Common initialization routine for lisp modes.
432 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
433 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
434 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
435 font-lock keywords will not be case sensitive."
437 (set-syntax-table lisp-mode-syntax-table
))
438 (setq-local paragraph-ignore-fill-prefix t
)
439 (setq-local fill-paragraph-function
'lisp-fill-paragraph
)
440 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
441 ;; a single docstring. Let's fix it here.
442 (setq-local adaptive-fill-function
443 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
444 ;; Adaptive fill mode gets in the way of auto-fill,
445 ;; and should make no difference for explicit fill
446 ;; because lisp-fill-paragraph should do the job.
447 ;; I believe that newcomment's auto-fill code properly deals with it -stef
448 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
449 (setq-local indent-line-function
'lisp-indent-line
)
450 (setq-local outline-regexp
";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
451 (setq-local outline-level
'lisp-outline-level
)
452 (setq-local add-log-current-defun-function
#'lisp-current-defun-name
)
453 (setq-local comment-start
";")
454 ;; Look within the line for a ; following an even number of backslashes
455 ;; after either a non-backslash or the line beginning.
456 (setq-local comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
457 ;; Font lock mode uses this only when it KNOWS a comment is starting.
458 (setq-local font-lock-comment-start-skip
";+ *")
459 (setq-local comment-add
1) ;default to `;;' in comment-region
460 (setq-local comment-column
40)
461 ;; Don't get confused by `;' in doc strings when paragraph-filling.
462 (setq-local comment-use-global-state t
)
463 (setq-local imenu-generic-expression lisp-imenu-generic-expression
)
464 (setq-local multibyte-syntax-as-symbol t
)
465 (setq-local syntax-begin-function
'beginning-of-defun
)
466 (setq font-lock-defaults
467 `(,(if elisp
'(lisp-el-font-lock-keywords
468 lisp-el-font-lock-keywords-1
469 lisp-el-font-lock-keywords-2
)
470 '(lisp-cl-font-lock-keywords
471 lisp-cl-font-lock-keywords-1
472 lisp-cl-font-lock-keywords-2
))
473 nil
,keywords-case-insensitive nil nil
474 (font-lock-mark-block-function . mark-defun
)
475 (font-lock-syntactic-face-function
476 . lisp-font-lock-syntactic-face-function
)))
477 (setq-local prettify-symbols-alist lisp--prettify-symbols-alist
))
479 (defun lisp-outline-level ()
480 "Lisp mode `outline-level' function."
481 (let ((len (- (match-end 0) (match-beginning 0))))
482 (if (looking-at "(\\|;;;###autoload")
486 (defun lisp-current-defun-name ()
487 "Return the name of the defun at point, or nil."
489 (let ((location (point)))
490 ;; If we are now precisely at the beginning of a defun, make sure
491 ;; beginning-of-defun finds that one rather than the previous one.
492 (or (eobp) (forward-char 1))
494 ;; Make sure we are really inside the defun found, not after it.
495 (when (and (looking-at "\\s(")
496 (progn (end-of-defun)
497 (< location
(point)))
498 (progn (forward-sexp -
1)
499 (>= location
(point))))
500 (if (looking-at "\\s(")
502 ;; Skip the defining construct name, typically "defun" or
505 ;; The second element is usually a symbol being defined. If it
506 ;; is not, use the first symbol in it.
507 (skip-chars-forward " \t\n'(")
508 (buffer-substring-no-properties (point)
509 (progn (forward-sexp 1)
512 (defvar lisp-mode-shared-map
513 (let ((map (make-sparse-keymap)))
514 (set-keymap-parent map prog-mode-map
)
515 (define-key map
"\e\C-q" 'indent-sexp
)
516 (define-key map
"\177" 'backward-delete-char-untabify
)
517 ;; This gets in the way when viewing a Lisp file in view-mode. As
518 ;; long as [backspace] is mapped into DEL via the
519 ;; function-key-map, this should remain disabled!!
520 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
522 "Keymap for commands shared by all sorts of Lisp modes.")
524 (defvar emacs-lisp-mode-map
525 (let ((map (make-sparse-keymap "Emacs-Lisp"))
526 (menu-map (make-sparse-keymap "Emacs-Lisp"))
527 (lint-map (make-sparse-keymap))
528 (prof-map (make-sparse-keymap))
529 (tracing-map (make-sparse-keymap)))
530 (set-keymap-parent map lisp-mode-shared-map
)
531 (define-key map
"\e\t" 'completion-at-point
)
532 (define-key map
"\e\C-x" 'eval-defun
)
533 (define-key map
"\e\C-q" 'indent-pp-sexp
)
534 (bindings--define-key map
[menu-bar emacs-lisp
]
535 (cons "Emacs-Lisp" menu-map
))
536 (bindings--define-key menu-map
[eldoc]
537 '(menu-item "Auto-Display Documentation Strings" eldoc-mode
538 :button (:toggle . (bound-and-true-p eldoc-mode))
539 :help "Display the documentation string for the item under cursor"))
540 (bindings--define-key menu-map [checkdoc]
541 '(menu-item "Check Documentation Strings" checkdoc
542 :help "Check documentation strings for style requirements"))
543 (bindings--define-key menu-map [re-builder]
544 '(menu-item "Construct Regexp" re-builder
545 :help "Construct a regexp interactively"))
546 (bindings--define-key menu-map [tracing] (cons "Tracing" tracing-map))
547 (bindings--define-key tracing-map [tr-a]
548 '(menu-item "Untrace All" untrace-all
549 :help "Untrace all currently traced functions"))
550 (bindings--define-key tracing-map [tr-uf]
551 '(menu-item "Untrace Function..." untrace-function
552 :help "Untrace function, and possibly activate all remaining advice"))
553 (bindings--define-key tracing-map [tr-sep] menu-bar-separator)
554 (bindings--define-key tracing-map [tr-q]
555 '(menu-item "Trace Function Quietly..." trace-function-background
556 :help "Trace the function with trace output going quietly to a buffer"))
557 (bindings--define-key tracing-map [tr-f]
558 '(menu-item "Trace Function..." trace-function
559 :help "Trace the function given as an argument"))
560 (bindings--define-key menu-map [profiling] (cons "Profiling" prof-map))
561 (bindings--define-key prof-map [prof-restall]
562 '(menu-item "Remove Instrumentation for All Functions" elp-restore-all
563 :help "Restore the original definitions of all functions being profiled"))
564 (bindings--define-key prof-map [prof-restfunc]
565 '(menu-item "Remove Instrumentation for Function..." elp-restore-function
566 :help "Restore an instrumented function to its original definition"))
568 (bindings--define-key prof-map [sep-rem] menu-bar-separator)
569 (bindings--define-key prof-map [prof-resall]
570 '(menu-item "Reset Counters for All Functions" elp-reset-all
571 :help "Reset the profiling information for all functions being profiled"))
572 (bindings--define-key prof-map [prof-resfunc]
573 '(menu-item "Reset Counters for Function..." elp-reset-function
574 :help "Reset the profiling information for a function"))
575 (bindings--define-key prof-map [prof-res]
576 '(menu-item "Show Profiling Results" elp-results
577 :help "Display current profiling results"))
578 (bindings--define-key prof-map [prof-pack]
579 '(menu-item "Instrument Package..." elp-instrument-package
580 :help "Instrument for profiling all function that start with a prefix"))
581 (bindings--define-key prof-map [prof-func]
582 '(menu-item "Instrument Function..." elp-instrument-function
583 :help "Instrument a function for profiling"))
584 ;; Maybe this should be in a separate submenu from the ELP stuff?
585 (bindings--define-key prof-map [sep-natprof] menu-bar-separator)
586 (bindings--define-key prof-map [prof-natprof-stop]
587 '(menu-item "Stop Native Profiler" profiler-stop
588 :help "Stop recording profiling information"
589 :enable (and (featurep 'profiler)
590 (profiler-running-p))))
591 (bindings--define-key prof-map [prof-natprof-report]
592 '(menu-item "Show Profiler Report" profiler-report
593 :help "Show the current profiler report"
594 :enable (and (featurep 'profiler)
595 (profiler-running-p))))
596 (bindings--define-key prof-map [prof-natprof-start]
597 '(menu-item "Start Native Profiler..." profiler-start
598 :help "Start recording profiling information"))
600 (bindings--define-key menu-map [lint] (cons "Linting" lint-map))
601 (bindings--define-key lint-map [lint-di]
602 '(menu-item "Lint Directory..." elint-directory
603 :help "Lint a directory"))
604 (bindings--define-key lint-map [lint-f]
605 '(menu-item "Lint File..." elint-file
606 :help "Lint a file"))
607 (bindings--define-key lint-map [lint-b]
608 '(menu-item "Lint Buffer" elint-current-buffer
609 :help "Lint the current buffer"))
610 (bindings--define-key lint-map [lint-d]
611 '(menu-item "Lint Defun" elint-defun
612 :help "Lint the function at point"))
613 (bindings--define-key menu-map [edebug-defun]
614 '(menu-item "Instrument Function for Debugging" edebug-defun
615 :help "Evaluate the top level form point is in, stepping through with Edebug"
617 (bindings--define-key menu-map [separator-byte] menu-bar-separator)
618 (bindings--define-key menu-map [disas]
619 '(menu-item "Disassemble Byte Compiled Object..." disassemble
620 :help "Print disassembled code for OBJECT in a buffer"))
621 (bindings--define-key menu-map [byte-recompile]
622 '(menu-item "Byte-recompile Directory..." byte-recompile-directory
623 :help "Recompile every `.el' file in DIRECTORY that needs recompilation"))
624 (bindings--define-key menu-map [emacs-byte-compile-and-load]
625 '(menu-item "Byte-compile and Load" emacs-lisp-byte-compile-and-load
626 :help "Byte-compile the current file (if it has changed), then load compiled code"))
627 (bindings--define-key menu-map [byte-compile]
628 '(menu-item "Byte-compile This File" emacs-lisp-byte-compile
629 :help "Byte compile the file containing the current buffer"))
630 (bindings--define-key menu-map [separator-eval] menu-bar-separator)
631 (bindings--define-key menu-map [ielm]
632 '(menu-item "Interactive Expression Evaluation" ielm
633 :help "Interactively evaluate Emacs Lisp expressions"))
634 (bindings--define-key menu-map [eval-buffer]
635 '(menu-item "Evaluate Buffer" eval-buffer
636 :help "Execute the current buffer as Lisp code"))
637 (bindings--define-key menu-map [eval-region]
638 '(menu-item "Evaluate Region" eval-region
639 :help "Execute the region as Lisp code"
640 :enable mark-active))
641 (bindings--define-key menu-map [eval-sexp]
642 '(menu-item "Evaluate Last S-expression" eval-last-sexp
643 :help "Evaluate sexp before point; print value in echo area"))
644 (bindings--define-key menu-map [separator-format] menu-bar-separator)
645 (bindings--define-key menu-map [comment-region]
646 '(menu-item "Comment Out Region" comment-region
647 :help "Comment or uncomment each line in the region"
648 :enable mark-active))
649 (bindings--define-key menu-map [indent-region]
650 '(menu-item "Indent Region" indent-region
651 :help "Indent each nonblank line in the region"
652 :enable mark-active))
653 (bindings--define-key menu-map [indent-line]
654 '(menu-item "Indent Line" lisp-indent-line))
656 "Keymap for Emacs Lisp mode.
657 All commands in `lisp-mode-shared-map' are inherited by this map.")
659 (defun emacs-lisp-byte-compile ()
660 "Byte compile the file containing the current buffer."
663 (byte-compile-file buffer-file-name)
664 (error "The buffer must be saved in a file first")))
666 (defun emacs-lisp-byte-compile-and-load ()
667 "Byte-compile the current file (if it has changed), then load compiled code."
670 (error "The buffer must be saved in a file first"))
672 ;; Recompile if file or buffer has changed since last compilation.
673 (if (and (buffer-modified-p)
674 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
676 (byte-recompile-file buffer-file-name nil 0 t))
678 (defcustom emacs-lisp-mode-hook nil
679 "Hook run when entering Emacs Lisp mode."
680 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
684 (defcustom lisp-mode-hook nil
685 "Hook run when entering Lisp mode."
686 :options '(imenu-add-menubar-index)
690 (defcustom lisp-interaction-mode-hook nil
691 "Hook run when entering Lisp Interaction mode."
692 :options '(turn-on-eldoc-mode)
696 (defconst lisp--prettify-symbols-alist
699 (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp"
700 "Major mode for editing Lisp code to run in Emacs.
702 Delete converts tabs to spaces as it moves back.
703 Blank lines separate paragraphs. Semicolons start comments.
705 \\{emacs-lisp-mode-map}
706 Entry to this mode calls the value of `emacs-lisp-mode-hook'
707 if that value is non-nil."
709 (lisp-mode-variables nil nil 'elisp)
710 (setq imenu-case-fold-search nil)
711 (add-hook 'completion-at-point-functions
712 'lisp-completion-at-point nil 'local))
714 ;;; Emacs Lisp Byte-Code mode
717 (defconst emacs-list-byte-code-comment-re
718 (concat "\\(#\\)@\\([0-9]+\\) "
719 ;; Make sure it's a docstring and not a lazy-loaded byte-code.
720 "\\(?:[^(]\\|([^\"]\\)")))
722 (defun emacs-lisp-byte-code-comment (end &optional _point)
723 "Try to syntactically mark the #@NNN ....^_ docstrings in byte-code files."
724 (let ((ppss (syntax-ppss)))
725 (when (and (nth 4 ppss)
726 (eq (char-after (nth 8 ppss)) ?#))
727 (let* ((n (save-excursion
728 (goto-char (nth 8 ppss))
729 (when (looking-at emacs-list-byte-code-comment-re)
730 (string-to-number (match-string 2)))))
731 ;; `maxdiff' tries to make sure the loop below terminates.
734 (let* ((bchar (match-end 2))
735 (b (position-bytes bchar)))
737 (while (let ((diff (- (position-bytes (point)) b n)))
739 (when (> diff maxdiff) (setq diff maxdiff))
740 (forward-char (- diff))
741 (setq maxdiff (if (> diff 0) diff
742 (max (1- maxdiff) 1)))
745 (put-text-property (1- (point)) (point)
747 (string-to-syntax "> b"))
748 (goto-char end)))))))
750 (defun emacs-lisp-byte-code-syntax-propertize (start end)
751 (emacs-lisp-byte-code-comment end (point))
753 (syntax-propertize-rules
754 (emacs-list-byte-code-comment-re
755 (1 (prog1 "< b" (emacs-lisp-byte-code-comment end (point))))))
758 (add-to-list 'auto-mode-alist '("\\.elc\\'" . emacs-lisp-byte-code-mode))
759 (define-derived-mode emacs-lisp-byte-code-mode emacs-lisp-mode
761 "Major mode for *.elc files."
762 ;; TODO: Add way to disassemble byte-code under point.
763 (setq-local open-paren-in-column-0-is-defun-start nil)
764 (setq-local syntax-propertize-function
765 #'emacs-lisp-byte-code-syntax-propertize))
767 ;;; Generic Lisp mode.
769 (defvar lisp-mode-map
770 (let ((map (make-sparse-keymap))
771 (menu-map (make-sparse-keymap "Lisp")))
772 (set-keymap-parent map lisp-mode-shared-map)
773 (define-key map "\e\C-x" 'lisp-eval-defun)
774 (define-key map "\C-c\C-z" 'run-lisp)
775 (bindings--define-key map [menu-bar lisp] (cons "Lisp" menu-map))
776 (bindings--define-key menu-map [run-lisp]
777 '(menu-item "Run inferior Lisp" run-lisp
778 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
779 (bindings--define-key menu-map [ev-def]
780 '(menu-item "Eval defun" lisp-eval-defun
781 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
782 (bindings--define-key menu-map [ind-sexp]
783 '(menu-item "Indent sexp" indent-sexp
784 :help "Indent each line of the list starting just after point"))
786 "Keymap for ordinary Lisp mode.
787 All commands in `lisp-mode-shared-map' are inherited by this map.")
789 (define-derived-mode lisp-mode prog-mode "Lisp"
790 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
792 Delete converts tabs to spaces as it moves back.
793 Blank lines separate paragraphs. Semicolons start comments.
796 Note that `run-lisp' may be used either to start an inferior Lisp job
797 or to switch back to an existing one.
799 Entry to this mode calls the value of `lisp-mode-hook'
800 if that value is non-nil."
801 (lisp-mode-variables nil t)
802 (setq-local find-tag-default-function 'lisp-find-tag-default)
803 (setq-local comment-start-skip
804 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
805 (setq imenu-case-fold-search t))
807 (defun lisp-find-tag-default ()
808 (let ((default (find-tag-default)))
809 (when (stringp default)
810 (if (string-match ":+" default)
811 (substring default (match-end 0))
814 ;; Used in old LispM code.
815 (defalias 'common-lisp-mode 'lisp-mode)
817 ;; This will do unless inf-lisp.el is loaded.
818 (defun lisp-eval-defun (&optional _and-go)
819 "Send the current defun to the Lisp process made by \\[run-lisp]."
821 (error "Process lisp does not exist"))
823 (defvar lisp-interaction-mode-map
824 (let ((map (make-sparse-keymap))
825 (menu-map (make-sparse-keymap "Lisp-Interaction")))
826 (set-keymap-parent map lisp-mode-shared-map)
827 (define-key map "\e\C-x" 'eval-defun)
828 (define-key map "\e\C-q" 'indent-pp-sexp)
829 (define-key map "\e\t" 'completion-at-point)
830 (define-key map "\n" 'eval-print-last-sexp)
831 (bindings--define-key map [menu-bar lisp-interaction]
832 (cons "Lisp-Interaction" menu-map))
833 (bindings--define-key menu-map [eval-defun]
834 '(menu-item "Evaluate Defun" eval-defun
835 :help "Evaluate the top-level form containing point, or after point"))
836 (bindings--define-key menu-map [eval-print-last-sexp]
837 '(menu-item "Evaluate and Print" eval-print-last-sexp
838 :help "Evaluate sexp before point; print value into current buffer"))
839 (bindings--define-key menu-map [edebug-defun-lisp-interaction]
840 '(menu-item "Instrument Function for Debugging" edebug-defun
841 :help "Evaluate the top level form point is in, stepping through with Edebug"
843 (bindings--define-key menu-map [indent-pp-sexp]
844 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
845 :help "Indent each line of the list starting just after point, or prettyprint it"))
846 (bindings--define-key menu-map [complete-symbol]
847 '(menu-item "Complete Lisp Symbol" completion-at-point
848 :help "Perform completion on Lisp symbol preceding point"))
850 "Keymap for Lisp Interaction mode.
851 All commands in `lisp-mode-shared-map' are inherited by this map.")
853 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
854 "Major mode for typing and evaluating Lisp forms.
855 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
856 before point, and prints its value into the buffer, advancing point.
857 Note that printing is controlled by `eval-expression-print-length'
858 and `eval-expression-print-level'.
861 Delete converts tabs to spaces as it moves back.
862 Paragraphs are separated only by blank lines.
863 Semicolons start comments.
865 \\{lisp-interaction-mode-map}
866 Entry to this mode calls the value of `lisp-interaction-mode-hook'
867 if that value is non-nil."
870 (defun eval-print-last-sexp ()
871 "Evaluate sexp before point; print value into current buffer.
873 If `eval-expression-debug-on-error' is non-nil, which is the default,
874 this command arranges for all errors to enter the debugger.
876 Note that printing the result is controlled by the variables
877 `eval-expression-print-length' and `eval-expression-print-level',
880 (let ((standard-output (current-buffer)))
886 (defun last-sexp-setup-props (beg end value alt1 alt2)
887 "Set up text properties for the output of `eval-last-sexp-1'.
888 BEG and END are the start and end of the output in current-buffer.
889 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
890 alternative printed representations that can be displayed."
891 (let ((map (make-sparse-keymap)))
892 (define-key map "\C-m" 'last-sexp-toggle-display)
893 (define-key map [down-mouse-2] 'mouse-set-point)
894 (define-key map [mouse-2] 'last-sexp-toggle-display)
897 `(printed-value (,value ,alt1 ,alt2)
900 help-echo "RET, mouse-2: toggle abbreviated display"
901 rear-nonsticky (mouse-face keymap help-echo
905 (defun last-sexp-toggle-display (&optional _arg)
906 "Toggle between abbreviated and unabbreviated printed representations."
910 (let ((value (get-text-property (point) 'printed-value)))
912 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
915 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
916 (standard-output (current-buffer))
918 (delete-region beg end)
919 (insert (nth 1 value))
921 (setq point (1- (point))))
922 (last-sexp-setup-props beg (point)
926 (goto-char (min (point-max) point)))))))
928 (defun prin1-char (char)
929 "Return a string representing CHAR as a character rather than as an integer.
930 If CHAR is not a character, return nil."
933 (let ((c (event-basic-type char))
934 (mods (event-modifiers char))
936 ;; Prevent ?A from turning into ?\S-a.
937 (if (and (memq 'shift mods)
938 (zerop (logand char ?\S-\^@))
939 (not (let ((case-fold-search nil))
940 (char-equal c (upcase c)))))
941 (setq c (upcase c) mods nil))
942 ;; What string are we considering using?
949 (cond ((eq modif 'super) "\\s-")
950 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
953 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
958 ;; Verify the string reads a CHAR, not to some other character.
959 ;; If it doesn't, return nil instead.
961 (= (car (read-from-string string)) char)
965 (defun preceding-sexp ()
966 "Return sexp before the point."
967 (let ((opoint (point))
971 (with-syntax-table emacs-lisp-mode-syntax-table
972 ;; If this sexp appears to be enclosed in `...'
973 ;; then ignore the surrounding quotes.
975 (or (eq (following-char) ?\')
976 (eq (preceding-char) ?\')))
978 ;; If we were after `?\e' (or similar case),
979 ;; use the whole thing, not just the `e'.
980 (when (eq (preceding-char) ?\\)
982 (when (eq (preceding-char) ??)
985 ;; Skip over hash table read syntax.
986 (and (> (point) (1+ (point-min)))
987 (looking-back "#s" (- (point) 2))
991 (when (eq (preceding-char) ?=)
994 (skip-chars-backward "0-9#=")
995 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
1000 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
1001 ;; `variable' so that the value is returned, not the
1003 (if (and ignore-quotes
1004 (eq (following-char) ?`))
1006 (narrow-to-region (point-min) opoint)
1007 (setq expr (read (current-buffer)))
1008 ;; If it's an (interactive ...) form, it's more
1009 ;; useful to show how an interactive call would
1012 (eq (car expr) 'interactive)
1014 (list 'call-interactively
1023 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
1024 "Evaluate sexp before point; print value in the echo area.
1025 With argument, print output into current buffer."
1026 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
1027 ;; Setup the lexical environment if lexical-binding is enabled.
1028 (eval-last-sexp-print-value
1029 (eval (eval-sexp-add-defvars (preceding-sexp)) lexical-binding))))
1032 (defun eval-last-sexp-print-value (value)
1033 (let ((unabbreviated (let ((print-length nil) (print-level nil))
1034 (prin1-to-string value)))
1035 (print-length eval-expression-print-length)
1036 (print-level eval-expression-print-level)
1041 (let ((str (eval-expression-print-format value)))
1042 (if str (princ str)))
1044 (when (and (bufferp standard-output)
1045 (or (not (null print-length))
1046 (not (null print-level)))
1047 (not (string= unabbreviated
1048 (buffer-substring-no-properties beg end))))
1049 (last-sexp-setup-props beg end value
1051 (buffer-substring-no-properties beg end))
1055 (defvar eval-last-sexp-fake-value (make-symbol "t"))
1057 (defun eval-sexp-add-defvars (exp &optional pos)
1058 "Prepend EXP with all the `defvar's that precede it in the buffer.
1059 POS specifies the starting position where EXP was found and defaults to point."
1060 (setq exp (macroexpand-all exp)) ;Eager macro-expansion.
1061 (if (not lexical-binding)
1064 (unless pos (setq pos (point)))
1066 (goto-char (point-min))
1067 (while (re-search-forward
1068 "(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
1070 (let ((var (intern (match-string 1))))
1071 (and (not (special-variable-p var))
1073 (zerop (car (syntax-ppss (match-beginning 0)))))
1075 `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
1077 (defun eval-last-sexp (eval-last-sexp-arg-internal)
1078 "Evaluate sexp before point; print value in the echo area.
1079 Interactively, with prefix argument, print output into current buffer.
1080 Truncates long output according to the value of the variables
1081 `eval-expression-print-length' and `eval-expression-print-level'.
1083 If `eval-expression-debug-on-error' is non-nil, which is the default,
1084 this command arranges for all errors to enter the debugger."
1086 (if (null eval-expression-debug-on-error)
1087 (eval-last-sexp-1 eval-last-sexp-arg-internal)
1089 (let ((debug-on-error eval-last-sexp-fake-value))
1090 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
1092 (unless (eq (cdr value) eval-last-sexp-fake-value)
1093 (setq debug-on-error (cdr value)))
1096 (defun eval-defun-1 (form)
1097 "Treat some expressions specially.
1098 Reset the `defvar' and `defcustom' variables to the initial value.
1099 \(For `defcustom', use the :set function if there is one.)
1100 Reinitialize the face according to the `defface' specification."
1101 ;; The code in edebug-defun should be consistent with this, but not
1102 ;; the same, since this gets a macroexpanded form.
1103 (cond ((not (listp form))
1105 ((and (eq (car form) 'defvar)
1106 (cdr-safe (cdr-safe form))
1107 (boundp (cadr form)))
1108 ;; Force variable to be re-set.
1109 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
1110 (setq-default ,(nth 1 form) ,(nth 2 form))))
1111 ;; `defcustom' is now macroexpanded to
1112 ;; `custom-declare-variable' with a quoted value arg.
1113 ((and (eq (car form) 'custom-declare-variable)
1114 (default-boundp (eval (nth 1 form) lexical-binding)))
1115 ;; Force variable to be bound, using :set function if specified.
1116 (let ((setfunc (memq :set form)))
1118 (setq setfunc (car-safe (cdr-safe setfunc)))
1119 (or (functionp setfunc) (setq setfunc nil)))
1120 (funcall (or setfunc 'set-default)
1121 (eval (nth 1 form) lexical-binding)
1122 ;; The second arg is an expression that evaluates to
1123 ;; an expression. The second evaluation is the one
1124 ;; normally performed not by normal execution but by
1125 ;; custom-initialize-set (for example), which does not
1126 ;; use lexical-binding.
1127 (eval (eval (nth 2 form) lexical-binding))))
1129 ;; `defface' is macroexpanded to `custom-declare-face'.
1130 ((eq (car form) 'custom-declare-face)
1132 (let ((face-symbol (eval (nth 1 form) lexical-binding)))
1133 (setq face-new-frame-defaults
1134 (assq-delete-all face-symbol face-new-frame-defaults))
1135 (put face-symbol 'face-defface-spec nil)
1136 (put face-symbol 'face-override-spec nil))
1138 ((eq (car form) 'progn)
1139 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
1142 (defun eval-defun-2 ()
1143 "Evaluate defun that point is in or before.
1144 The value is displayed in the echo area.
1145 If the current defun is actually a call to `defvar',
1146 then reset the variable using the initial value expression
1147 even if the variable already has some other value.
1148 \(Normally `defvar' does not change the variable's value
1149 if it already has a value.\)
1151 Return the result of evaluation."
1152 ;; FIXME: the print-length/level bindings should only be applied while
1153 ;; printing, not while evaluating.
1154 (let ((debug-on-error eval-expression-debug-on-error)
1155 (print-length eval-expression-print-length)
1156 (print-level eval-expression-print-level))
1158 ;; Arrange for eval-region to "read" the (possibly) altered form.
1159 ;; eval-region handles recording which file defines a function or
1161 (let ((standard-output t)
1163 ;; Read the form from the buffer, and record where it ends.
1166 (beginning-of-defun)
1168 (setq form (read (current-buffer)))
1170 ;; Alter the form if necessary.
1171 (let ((form (eval-sexp-add-defvars
1172 (eval-defun-1 (macroexpand form)))))
1173 (eval-region beg end standard-output
1175 ;; Skipping to the end of the specified region
1176 ;; will make eval-region return.
1179 ;; The result of evaluation has been put onto VALUES. So return it.
1182 (defun eval-defun (edebug-it)
1183 "Evaluate the top-level form containing point, or after point.
1185 If the current defun is actually a call to `defvar' or `defcustom',
1186 evaluating it this way resets the variable using its initial value
1187 expression (using the defcustom's :set function if there is one), even
1188 if the variable already has some other value. \(Normally `defvar' and
1189 `defcustom' do not alter the value if there already is one.) In an
1190 analogous way, evaluating a `defface' overrides any customizations of
1191 the face, so that it becomes defined exactly as the `defface' expression
1194 If `eval-expression-debug-on-error' is non-nil, which is the default,
1195 this command arranges for all errors to enter the debugger.
1197 With a prefix argument, instrument the code for Edebug.
1199 If acting on a `defun' for FUNCTION, and the function was
1200 instrumented, `Edebug: FUNCTION' is printed in the echo area. If not
1201 instrumented, just FUNCTION is printed.
1203 If not acting on a `defun', the result of evaluation is displayed in
1204 the echo area. This display is controlled by the variables
1205 `eval-expression-print-length' and `eval-expression-print-level',
1210 (eval-defun (not edebug-all-defs)))
1212 (if (null eval-expression-debug-on-error)
1214 (let ((old-value (make-symbol "t")) new-value value)
1215 (let ((debug-on-error old-value))
1216 (setq value (eval-defun-2))
1217 (setq new-value debug-on-error))
1218 (unless (eq old-value new-value)
1219 (setq debug-on-error new-value))
1222 ;; May still be used by some external Lisp-mode variant.
1223 (define-obsolete-function-alias 'lisp-comment-indent
1224 'comment-indent-default "22.1")
1225 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
1227 (defcustom lisp-indent-offset nil
1228 "If non-nil, indent second line of expressions that many more columns."
1230 :type '(choice (const nil) integer))
1231 (put 'lisp-indent-offset 'safe-local-variable
1232 (lambda (x) (or (null x) (integerp x))))
1234 (defcustom lisp-indent-function 'lisp-indent-function
1235 "A function to be called by `calculate-lisp-indent'.
1236 It indents the arguments of a Lisp function call. This function
1237 should accept two arguments: the indent-point, and the
1238 `parse-partial-sexp' state at that position. One option for this
1239 function is `common-lisp-indent-function'."
1243 (defun lisp-indent-line (&optional _whole-exp)
1244 "Indent current line as Lisp code.
1245 With argument, indent any additional lines of the same expression
1246 rigidly along with this one."
1248 (let ((indent (calculate-lisp-indent)) shift-amt
1249 (pos (- (point-max) (point)))
1250 (beg (progn (beginning-of-line) (point))))
1251 (skip-chars-forward " \t")
1252 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
1253 ;; Don't alter indentation of a ;;; comment line
1254 ;; or a line that starts in a string.
1255 ;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
1256 (goto-char (- (point-max) pos))
1257 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
1258 ;; Single-semicolon comment lines should be indented
1259 ;; as comment lines, not as code.
1260 (progn (indent-for-comment) (forward-char -1))
1261 (if (listp indent) (setq indent (car indent)))
1262 (setq shift-amt (- indent (current-column)))
1263 (if (zerop shift-amt)
1265 (delete-region beg (point))
1266 (indent-to indent)))
1267 ;; If initial point was within line's indentation,
1268 ;; position after the indentation. Else stay at same point in text.
1269 (if (> (- (point-max) pos) (point))
1270 (goto-char (- (point-max) pos))))))
1272 (defvar calculate-lisp-indent-last-sexp)
1274 (defun calculate-lisp-indent (&optional parse-start)
1275 "Return appropriate indentation for current line as Lisp code.
1276 In usual case returns an integer: the column to indent to.
1277 If the value is nil, that means don't change the indentation
1278 because the line starts inside a string.
1280 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
1281 This means that following lines at the same level of indentation
1282 should not necessarily be indented the same as this line.
1283 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
1284 is the buffer position of the start of the containing expression."
1287 (let ((indent-point (point))
1289 ;; setting this to a number inhibits calling hook
1290 (desired-indent nil)
1292 calculate-lisp-indent-last-sexp containing-sexp)
1294 (goto-char parse-start)
1295 (beginning-of-defun))
1296 ;; Find outermost containing sexp
1297 (while (< (point) indent-point)
1298 (setq state (parse-partial-sexp (point) indent-point 0)))
1299 ;; Find innermost containing sexp
1302 (> (elt state 0) 0))
1304 (setq calculate-lisp-indent-last-sexp (elt state 2))
1305 (setq containing-sexp (elt state 1))
1306 ;; Position following last unclosed open.
1307 (goto-char (1+ containing-sexp))
1308 ;; Is there a complete sexp since then?
1309 (if (and calculate-lisp-indent-last-sexp
1310 (> calculate-lisp-indent-last-sexp (point)))
1311 ;; Yes, but is there a containing sexp after that?
1312 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
1314 (if (setq retry (car (cdr peek))) (setq state peek)))))
1317 ;; Innermost containing sexp found
1318 (goto-char (1+ containing-sexp))
1319 (if (not calculate-lisp-indent-last-sexp)
1320 ;; indent-point immediately follows open paren.
1322 (setq desired-indent (current-column))
1323 ;; Find the start of first element of containing sexp.
1324 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1325 (cond ((looking-at "\\s(")
1326 ;; First element of containing sexp is a list.
1327 ;; Indent under that list.
1329 ((> (save-excursion (forward-line 1) (point))
1330 calculate-lisp-indent-last-sexp)
1331 ;; This is the first line to start within the containing sexp.
1332 ;; It's almost certainly a function call.
1333 (if (= (point) calculate-lisp-indent-last-sexp)
1334 ;; Containing sexp has nothing before this line
1335 ;; except the first element. Indent under that element.
1337 ;; Skip the first element, find start of second (the first
1338 ;; argument of the function call) and indent under.
1339 (progn (forward-sexp 1)
1340 (parse-partial-sexp (point)
1341 calculate-lisp-indent-last-sexp
1343 (backward-prefix-chars))
1345 ;; Indent beneath first sexp on same line as
1346 ;; `calculate-lisp-indent-last-sexp'. Again, it's
1347 ;; almost certainly a function call.
1348 (goto-char calculate-lisp-indent-last-sexp)
1350 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
1352 (backward-prefix-chars)))))
1353 ;; Point is at the point to indent under unless we are inside a string.
1354 ;; Call indentation hook except when overridden by lisp-indent-offset
1355 ;; or if the desired indentation has already been computed.
1356 (let ((normal-indent (current-column)))
1357 (cond ((elt state 3)
1358 ;; Inside a string, don't change indentation.
1360 ((and (integerp lisp-indent-offset) containing-sexp)
1361 ;; Indent by constant offset
1362 (goto-char containing-sexp)
1363 (+ (current-column) lisp-indent-offset))
1364 ;; in this case calculate-lisp-indent-last-sexp is not nil
1365 (calculate-lisp-indent-last-sexp
1367 ;; try to align the parameters of a known function
1368 (and lisp-indent-function
1370 (funcall lisp-indent-function indent-point state))
1371 ;; If the function has no special alignment
1372 ;; or it does not apply to this argument,
1373 ;; try to align a constant-symbol under the last
1374 ;; preceding constant symbol, if there is such one of
1375 ;; the last 2 preceding symbols, in the previous
1376 ;; uncommented line.
1377 (and (save-excursion
1378 (goto-char indent-point)
1379 (skip-chars-forward " \t")
1381 ;; The last sexp may not be at the indentation
1382 ;; where it begins, so find that one, instead.
1384 (goto-char calculate-lisp-indent-last-sexp)
1385 ;; Handle prefix characters and whitespace
1386 ;; following an open paren. (Bug#1012)
1387 (backward-prefix-chars)
1388 (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
1389 (or (not containing-sexp)
1390 (< (1+ containing-sexp) (point))))
1392 (backward-prefix-chars))
1393 (setq calculate-lisp-indent-last-sexp (point)))
1394 (> calculate-lisp-indent-last-sexp
1396 (goto-char (1+ containing-sexp))
1397 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1399 (let ((parse-sexp-ignore-comments t)
1401 (goto-char calculate-lisp-indent-last-sexp)
1402 (or (and (looking-at ":")
1403 (setq indent (current-column)))
1404 (and (< (line-beginning-position)
1405 (prog2 (backward-sexp) (point)))
1407 (setq indent (current-column))))
1409 ;; another symbols or constants not preceded by a constant
1410 ;; as defined above.
1412 ;; in this case calculate-lisp-indent-last-sexp is nil
1417 (defun lisp-indent-function (indent-point state)
1418 "This function is the normal value of the variable `lisp-indent-function'.
1419 The function `calculate-lisp-indent' calls this to determine
1420 if the arguments of a Lisp function call should be indented specially.
1422 INDENT-POINT is the position at which the line being indented begins.
1423 Point is located at the point to indent under (for default indentation);
1424 STATE is the `parse-partial-sexp' state for that position.
1426 If the current line is in a call to a Lisp function that has a non-nil
1427 property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
1428 it specifies how to indent. The property value can be:
1430 * `defun', meaning indent `defun'-style
1431 \(this is also the case if there is no property and the function
1432 has a name that begins with \"def\", and three or more arguments);
1434 * an integer N, meaning indent the first N arguments specially
1435 (like ordinary function arguments), and then indent any further
1436 arguments like a body;
1438 * a function to call that returns the indentation (or nil).
1439 `lisp-indent-function' calls this function with the same two arguments
1440 that it itself received.
1442 This function returns either the indentation to use, or nil if the
1443 Lisp function does not specify a special indentation."
1444 (let ((normal-indent (current-column)))
1445 (goto-char (1+ (elt state 1)))
1446 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1447 (if (and (elt state 2)
1448 (not (looking-at "\\sw\\|\\s_")))
1449 ;; car of form doesn't seem to be a symbol
1451 (if (not (> (save-excursion (forward-line 1) (point))
1452 calculate-lisp-indent-last-sexp))
1453 (progn (goto-char calculate-lisp-indent-last-sexp)
1455 (parse-partial-sexp (point)
1456 calculate-lisp-indent-last-sexp 0 t)))
1457 ;; Indent under the list or under the first sexp on the same
1458 ;; line as calculate-lisp-indent-last-sexp. Note that first
1459 ;; thing on that line has to be complete sexp since we are
1460 ;; inside the innermost containing sexp.
1461 (backward-prefix-chars)
1463 (let ((function (buffer-substring (point)
1464 (progn (forward-sexp 1) (point))))
1466 (setq method (or (function-get (intern-soft function)
1467 'lisp-indent-function)
1468 (get (intern-soft function) 'lisp-indent-hook)))
1469 (cond ((or (eq method 'defun)
1471 (> (length function) 3)
1472 (string-match "\\`def" function)))
1473 (lisp-indent-defform state indent-point))
1475 (lisp-indent-specform method state
1476 indent-point normal-indent))
1478 (funcall method indent-point state)))))))
1480 (defcustom lisp-body-indent 2
1481 "Number of columns to indent the second line of a `(def...)' form."
1484 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1486 (defun lisp-indent-specform (count state indent-point normal-indent)
1487 (let ((containing-form-start (elt state 1))
1489 body-indent containing-form-column)
1490 ;; Move to the start of containing form, calculate indentation
1491 ;; to use for non-distinguished forms (> count), and move past the
1492 ;; function symbol. lisp-indent-function guarantees that there is at
1493 ;; least one word or symbol character following open paren of containing
1495 (goto-char containing-form-start)
1496 (setq containing-form-column (current-column))
1497 (setq body-indent (+ lisp-body-indent containing-form-column))
1500 ;; Now find the start of the last form.
1501 (parse-partial-sexp (point) indent-point 1 t)
1502 (while (and (< (point) indent-point)
1505 (setq count (1- count))
1507 (parse-partial-sexp (point) indent-point 1 t))
1509 ;; Point is sitting on first character of last (or count) sexp.
1511 ;; A distinguished form. If it is the first or second form use double
1512 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1513 ;; to 2 (the default), this just happens to work the same with if as
1514 ;; the older code, but it makes unwind-protect, condition-case,
1515 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1516 ;; less hacked, behavior can be obtained by replacing below with
1517 ;; (list normal-indent containing-form-start).
1518 (if (<= (- i count) 1)
1519 (list (+ containing-form-column (* 2 lisp-body-indent))
1520 containing-form-start)
1521 (list normal-indent containing-form-start))
1522 ;; A non-distinguished form. Use body-indent if there are no
1523 ;; distinguished forms and this is the first undistinguished form,
1524 ;; or if this is the first undistinguished form and the preceding
1525 ;; distinguished form has indentation at least as great as body-indent.
1526 (if (or (and (= i 0) (= count 0))
1527 (and (= count 0) (<= body-indent normal-indent)))
1531 (defun lisp-indent-defform (state _indent-point)
1532 (goto-char (car (cdr state)))
1534 (if (> (point) (car (cdr (cdr state))))
1536 (goto-char (car (cdr state)))
1537 (+ lisp-body-indent (current-column)))))
1540 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1541 ;; like defun if the first form is placed on the next line, otherwise
1542 ;; it is indented like any other form (i.e. forms line up under first).
1544 (put 'autoload 'lisp-indent-function 'defun)
1545 (put 'progn 'lisp-indent-function 0)
1546 (put 'prog1 'lisp-indent-function 1)
1547 (put 'prog2 'lisp-indent-function 2)
1548 (put 'save-excursion 'lisp-indent-function 0)
1549 (put 'save-restriction 'lisp-indent-function 0)
1550 (put 'save-current-buffer 'lisp-indent-function 0)
1551 (put 'let 'lisp-indent-function 1)
1552 (put 'let* 'lisp-indent-function 1)
1553 (put 'while 'lisp-indent-function 1)
1554 (put 'if 'lisp-indent-function 2)
1555 (put 'catch 'lisp-indent-function 1)
1556 (put 'condition-case 'lisp-indent-function 2)
1557 (put 'unwind-protect 'lisp-indent-function 1)
1558 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1560 (defun indent-sexp (&optional endpos)
1561 "Indent each line of the list starting just after point.
1562 If optional arg ENDPOS is given, indent each line, stopping when
1563 ENDPOS is encountered."
1565 (let ((indent-stack (list nil))
1567 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1568 ;; so that calculate-lisp-indent will find the beginning of
1569 ;; the defun we are in.
1570 ;; If ENDPOS is nil, it is safe not to scan before point
1571 ;; since every line we indent is more deeply nested than point is.
1572 (starting-point (if endpos nil (point)))
1573 (last-point (point))
1574 last-depth bol outer-loop-done inner-loop-done state this-indent)
1576 ;; Get error now if we don't have a complete sexp after point.
1577 (save-excursion (forward-sexp 1)))
1579 (setq outer-loop-done nil)
1580 (while (if endpos (< (point) endpos)
1581 (not outer-loop-done))
1582 (setq last-depth next-depth
1583 inner-loop-done nil)
1584 ;; Parse this line so we can learn the state
1585 ;; to indent the next line.
1586 ;; This inner loop goes through only once
1587 ;; unless a line ends inside a string.
1588 (while (and (not inner-loop-done)
1589 (not (setq outer-loop-done (eobp))))
1590 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1592 (setq next-depth (car state))
1593 ;; If the line contains a comment other than the sort
1594 ;; that is indented like code,
1595 ;; indent it now with indent-for-comment.
1596 ;; Comments indented like code are right already.
1597 ;; In any case clear the in-comment flag in the state
1598 ;; because parse-partial-sexp never sees the newlines.
1599 (if (car (nthcdr 4 state))
1600 (progn (indent-for-comment)
1602 (setcar (nthcdr 4 state) nil)))
1603 ;; If this line ends inside a string,
1604 ;; go straight to next line, remaining within the inner loop,
1605 ;; and turn off the \-flag.
1606 (if (car (nthcdr 3 state))
1609 (setcar (nthcdr 5 state) nil))
1610 (setq inner-loop-done t)))
1614 (setq indent-stack (nconc indent-stack
1615 (make-list (- next-depth) nil))
1616 last-depth (- last-depth next-depth)
1619 ;; Decide whether to exit.
1621 ;; If we have already reached the specified end,
1622 ;; give up and do not reindent this line.
1623 (if (<= endpos (point))
1624 (setq outer-loop-done t))
1625 ;; If no specified end, we are done if we have finished one sexp.
1626 (if (<= next-depth 0)
1627 (setq outer-loop-done t)))
1628 (unless outer-loop-done
1629 (while (> last-depth next-depth)
1630 (setq indent-stack (cdr indent-stack)
1631 last-depth (1- last-depth)))
1632 (while (< last-depth next-depth)
1633 (setq indent-stack (cons nil indent-stack)
1634 last-depth (1+ last-depth)))
1635 ;; Now indent the next line according
1636 ;; to what we learned from parsing the previous one.
1638 (skip-chars-forward " \t")
1639 ;; But not if the line is blank, or just a comment
1640 ;; (except for double-semi comments; indent them as usual).
1641 (if (or (eobp) (looking-at "\\s<\\|\n"))
1643 (if (and (car indent-stack)
1644 (>= (car indent-stack) 0))
1645 (setq this-indent (car indent-stack))
1646 (let ((val (calculate-lisp-indent
1647 (if (car indent-stack) (- (car indent-stack))
1650 (setq this-indent val)
1652 (setcar indent-stack
1653 (setq this-indent val))
1654 (setcar indent-stack (- (car (cdr val))))
1655 (setq this-indent (car val))))))
1656 (if (and this-indent (/= (current-column) this-indent))
1657 (progn (delete-region bol (point))
1658 (indent-to this-indent)))))
1660 (setq outer-loop-done (= (point) last-point))
1661 (setq last-point (point)))))))
1663 (defun indent-pp-sexp (&optional arg)
1664 "Indent each line of the list starting just after point, or prettyprint it.
1665 A prefix argument specifies pretty-printing."
1670 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1672 (goto-char (point-max))
1673 (if (eq (char-before) ?\n)
1674 (delete-char -1)))))
1677 ;;;; Lisp paragraph filling commands.
1679 (defcustom emacs-lisp-docstring-fill-column 65
1680 "Value of `fill-column' to use when filling a docstring.
1681 Any non-integer value means do not use a different value of
1682 `fill-column' when filling docstrings."
1683 :type '(choice (integer)
1684 (const :tag "Use the current `fill-column'" t))
1686 (put 'emacs-lisp-docstring-fill-column 'safe-local-variable
1687 (lambda (x) (or (eq x t) (integerp x))))
1689 (defun lisp-fill-paragraph (&optional justify)
1690 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1691 If any of the current line is a comment, fill the comment or the
1692 paragraph of it that point is in, preserving the comment's indentation
1693 and initial semicolons."
1695 (or (fill-comment-paragraph justify)
1696 ;; Since fill-comment-paragraph returned nil, that means we're not in
1697 ;; a comment: Point is on a program line; we are interested
1698 ;; particularly in docstring lines.
1700 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1701 ;; are buffer-local, but we avoid changing them so that they can be set
1702 ;; to make `forward-paragraph' and friends do something the user wants.
1704 ;; `paragraph-start': The `(' in the character alternative and the
1705 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1706 ;; sexps and backquoted sexps that follow a docstring from being filled
1707 ;; with the docstring. This setting has the consequence of inhibiting
1708 ;; filling many program lines that are not docstrings, which is sensible,
1709 ;; because the user probably asked to fill program lines by accident, or
1710 ;; expecting indentation (perhaps we should try to do indenting in that
1711 ;; case). The `;' and `:' stop the paragraph being filled at following
1712 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1713 ;; escaped to keep font-locking, filling, & paren matching in the source
1716 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1717 ;; a docstring and identifies it as a paragraph separator, so that it
1718 ;; won't be filled. (Since the first line of documentation stands alone
1719 ;; in some contexts, filling should not alter the contents the author has
1720 ;; chosen.) Only the first line of a docstring begins with whitespace
1721 ;; and a quotation mark and ends with a period or (rarely) a comma.
1723 ;; The `fill-column' is temporarily bound to
1724 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1725 (let ((paragraph-start (concat paragraph-start
1726 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1728 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1729 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1730 (derived-mode-p 'emacs-lisp-mode))
1731 emacs-lisp-docstring-fill-column
1733 (fill-paragraph justify))
1734 ;; Never return nil.
1737 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1738 "Indent all lines of code, starting in the region, sideways by ARG columns.
1739 Does not affect lines starting inside comments or strings, assuming that
1740 the start of the region is not inside them.
1742 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1743 The last is a regexp which, if matched at the beginning of a line,
1744 means don't indent that line."
1745 (interactive "r\np")
1749 (setq end (point-marker))
1752 (setq state (parse-partial-sexp (point)
1754 (forward-line 1) (point))
1756 (while (< (point) end)
1757 (or (car (nthcdr 3 state))
1758 (and nochange-regexp
1759 (looking-at nochange-regexp))
1760 ;; If line does not start in string, indent it
1761 (let ((indent (current-indentation)))
1762 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1764 (indent-to (max 0 (+ indent arg)) 0))))
1765 (setq state (parse-partial-sexp (point)
1767 (forward-line 1) (point))
1770 (provide 'lisp-mode)
1772 ;;; lisp-mode.el ends here