Stylistic changes in tramp-cache.el
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blobfa25a0c39751b6d75c0639afdd2bfdb5ceeb1c1f
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1999-2017 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: lisp, languages
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
27 ;; This mode is documented in the Emacs manual.
29 ;;; Code:
31 (eval-when-compile (require 'cl-lib))
33 (defvar font-lock-comment-face)
34 (defvar font-lock-doc-face)
35 (defvar font-lock-keywords-case-fold-search)
36 (defvar font-lock-string-face)
38 (define-abbrev-table 'lisp-mode-abbrev-table ()
39 "Abbrev table for Lisp mode.")
41 (defvar lisp--mode-syntax-table
42 (let ((table (make-syntax-table))
43 (i 0))
44 (while (< i ?0)
45 (modify-syntax-entry i "_ " table)
46 (setq i (1+ i)))
47 (setq i (1+ ?9))
48 (while (< i ?A)
49 (modify-syntax-entry i "_ " table)
50 (setq i (1+ i)))
51 (setq i (1+ ?Z))
52 (while (< i ?a)
53 (modify-syntax-entry i "_ " table)
54 (setq i (1+ i)))
55 (setq i (1+ ?z))
56 (while (< i 128)
57 (modify-syntax-entry i "_ " table)
58 (setq i (1+ i)))
59 (modify-syntax-entry ?\s " " table)
60 ;; Non-break space acts as whitespace.
61 (modify-syntax-entry ?\xa0 " " table)
62 (modify-syntax-entry ?\t " " table)
63 (modify-syntax-entry ?\f " " table)
64 (modify-syntax-entry ?\n "> " table)
65 ;; This is probably obsolete since nowadays such features use overlays.
66 ;; ;; Give CR the same syntax as newline, for selective-display.
67 ;; (modify-syntax-entry ?\^m "> " table)
68 (modify-syntax-entry ?\; "< " table)
69 (modify-syntax-entry ?` "' " table)
70 (modify-syntax-entry ?' "' " table)
71 (modify-syntax-entry ?, "' " table)
72 (modify-syntax-entry ?@ "_ p" table)
73 ;; Used to be singlequote; changed for flonums.
74 (modify-syntax-entry ?. "_ " table)
75 (modify-syntax-entry ?# "' " table)
76 (modify-syntax-entry ?\" "\" " table)
77 (modify-syntax-entry ?\\ "\\ " table)
78 (modify-syntax-entry ?\( "() " table)
79 (modify-syntax-entry ?\) ")( " table)
80 table)
81 "Parent syntax table used in Lisp modes.")
83 (defvar lisp-mode-syntax-table
84 (let ((table (make-syntax-table lisp--mode-syntax-table)))
85 (modify-syntax-entry ?\[ "_ " table)
86 (modify-syntax-entry ?\] "_ " table)
87 (modify-syntax-entry ?# "' 14" table)
88 (modify-syntax-entry ?| "\" 23bn" table)
89 table)
90 "Syntax table used in `lisp-mode'.")
92 (eval-and-compile
93 (defconst lisp-mode-symbol-regexp "\\(?:\\sw\\|\\s_\\|\\\\.\\)+"))
95 (defvar lisp-imenu-generic-expression
96 (list
97 (list nil
98 (purecopy (concat "^\\s-*("
99 (eval-when-compile
100 (regexp-opt
101 '("defun" "defmacro"
102 ;; Elisp.
103 "defun*" "defsubst" "define-inline"
104 "define-advice" "defadvice" "define-skeleton"
105 "define-compilation-mode" "define-minor-mode"
106 "define-global-minor-mode"
107 "define-globalized-minor-mode"
108 "define-derived-mode" "define-generic-mode"
109 "ert-deftest"
110 "cl-defun" "cl-defsubst" "cl-defmacro"
111 "cl-define-compiler-macro" "cl-defgeneric"
112 "cl-defmethod"
113 ;; CL.
114 "define-compiler-macro" "define-modify-macro"
115 "defsetf" "define-setf-expander"
116 "define-method-combination"
117 ;; CLOS and EIEIO
118 "defgeneric" "defmethod")
120 "\\s-+\\(" lisp-mode-symbol-regexp "\\)"))
122 (list (purecopy "Variables")
123 (purecopy (concat "^\\s-*("
124 (eval-when-compile
125 (regexp-opt
126 '(;; Elisp
127 "defconst" "defcustom"
128 ;; CL
129 "defconstant"
130 "defparameter" "define-symbol-macro")
132 "\\s-+\\(" lisp-mode-symbol-regexp "\\)"))
134 ;; For `defvar'/`defvar-local', we ignore (defvar FOO) constructs.
135 (list (purecopy "Variables")
136 (purecopy (concat "^\\s-*(defvar\\(?:-local\\)?\\s-+\\("
137 lisp-mode-symbol-regexp "\\)"
138 "[[:space:]\n]+[^)]"))
140 (list (purecopy "Types")
141 (purecopy (concat "^\\s-*("
142 (eval-when-compile
143 (regexp-opt
144 '(;; Elisp
145 "defgroup" "deftheme"
146 "define-widget" "define-error"
147 "defface" "cl-deftype" "cl-defstruct"
148 ;; CL
149 "deftype" "defstruct"
150 "define-condition" "defpackage"
151 ;; CLOS and EIEIO
152 "defclass")
154 "\\s-+'?\\(" lisp-mode-symbol-regexp "\\)"))
157 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
159 ;; This was originally in autoload.el and is still used there.
160 (put 'autoload 'doc-string-elt 3)
161 (put 'defmethod 'doc-string-elt 3)
162 (put 'defvar 'doc-string-elt 3)
163 (put 'defconst 'doc-string-elt 3)
164 (put 'defalias 'doc-string-elt 3)
165 (put 'defvaralias 'doc-string-elt 3)
166 (put 'define-category 'doc-string-elt 2)
167 ;; CL
168 (put 'defconstant 'doc-string-elt 3)
169 (put 'defparameter 'doc-string-elt 3)
171 (defvar lisp-doc-string-elt-property 'doc-string-elt
172 "The symbol property that holds the docstring position info.")
174 (defconst lisp-prettify-symbols-alist '(("lambda" . ?λ))
175 "Alist of symbol/\"pretty\" characters to be displayed.")
177 ;;;; Font-lock support.
179 (defun lisp--match-hidden-arg (limit)
180 (let ((res nil))
181 (while
182 (let ((ppss (parse-partial-sexp (line-beginning-position)
183 (line-end-position)
184 -1)))
185 (skip-syntax-forward " )")
186 (if (or (>= (car ppss) 0)
187 (looking-at ";\\|$"))
188 (progn
189 (forward-line 1)
190 (< (point) limit))
191 (looking-at ".*") ;Set the match-data.
192 (forward-line 1)
193 (setq res (point))
194 nil)))
195 res))
197 (defun lisp--el-non-funcall-position-p (pos)
198 "Heuristically determine whether POS is an evaluated position."
199 (save-match-data
200 (save-excursion
201 (ignore-errors
202 (goto-char pos)
203 (or (eql (char-before) ?\')
204 (let* ((ppss (syntax-ppss))
205 (paren-posns (nth 9 ppss))
206 (parent
207 (when paren-posns
208 (goto-char (car (last paren-posns))) ;(up-list -1)
209 (cond
210 ((ignore-errors
211 (and (eql (char-after) ?\()
212 (when (cdr paren-posns)
213 (goto-char (car (last paren-posns 2)))
214 (looking-at "(\\_<let\\*?\\_>"))))
215 (goto-char (match-end 0))
216 'let)
217 ((looking-at
218 (rx "("
219 (group-n 1 (+ (or (syntax w) (syntax _))))
220 symbol-end))
221 (prog1 (intern-soft (match-string-no-properties 1))
222 (goto-char (match-end 1))))))))
223 (or (eq parent 'declare)
224 (and (eq parent 'let)
225 (progn
226 (forward-sexp 1)
227 (< pos (point))))
228 (and (eq parent 'condition-case)
229 (progn
230 (forward-sexp 2)
231 (< (point) pos))))))))))
233 (defun lisp--el-match-keyword (limit)
234 ;; FIXME: Move to elisp-mode.el.
235 (catch 'found
236 (while (re-search-forward
237 (eval-when-compile
238 (concat "(\\(" lisp-mode-symbol-regexp "\\)\\_>"))
239 limit t)
240 (let ((sym (intern-soft (match-string 1))))
241 (when (or (special-form-p sym)
242 (and (macrop sym)
243 (not (get sym 'no-font-lock-keyword))
244 (not (lisp--el-non-funcall-position-p
245 (match-beginning 0)))))
246 (throw 'found t))))))
248 (defmacro let-when-compile (bindings &rest body)
249 "Like `let*', but allow for compile time optimization.
250 Use BINDINGS as in regular `let*', but in BODY each usage should
251 be wrapped in `eval-when-compile'.
252 This will generate compile-time constants from BINDINGS."
253 (declare (indent 1) (debug let))
254 (letrec ((loop
255 (lambda (bindings)
256 (if (null bindings)
257 (macroexpand-all (macroexp-progn body)
258 macroexpand-all-environment)
259 (let ((binding (pop bindings)))
260 (cl-progv (list (car binding))
261 (list (eval (nth 1 binding) t))
262 (funcall loop bindings)))))))
263 (funcall loop bindings)))
265 (defun elisp--font-lock-backslash ()
266 (let* ((beg0 (match-beginning 0))
267 (end0 (match-end 0))
268 (ppss (save-excursion (syntax-ppss beg0))))
269 (and (nth 3 ppss) ;Inside a string.
270 (not (nth 5 ppss)) ;The \ is not itself \-escaped.
271 ;; Don't highlight the \( introduced because of
272 ;; `open-paren-in-column-0-is-defun-start'.
273 (not (eq ?\n (char-before beg0)))
274 (equal (ignore-errors
275 (car (read-from-string
276 (format "\"%s\""
277 (buffer-substring-no-properties
278 beg0 end0)))))
279 (buffer-substring-no-properties (1+ beg0) end0))
280 `(face ,font-lock-warning-face
281 help-echo "This \\ has no effect"))))
283 (let-when-compile
284 ((lisp-fdefs '("defmacro" "defun"))
285 (lisp-vdefs '("defvar"))
286 (lisp-kw '("cond" "if" "while" "let" "let*" "progn" "prog1"
287 "prog2" "lambda" "unwind-protect" "condition-case"
288 "when" "unless" "with-output-to-string"
289 "ignore-errors" "dotimes" "dolist" "declare"))
290 (lisp-errs '("warn" "error" "signal"))
291 ;; Elisp constructs. Now they are update dynamically
292 ;; from obarray but they are also used for setting up
293 ;; the keywords for Common Lisp.
294 (el-fdefs '("defsubst" "cl-defsubst" "define-inline"
295 "define-advice" "defadvice" "defalias"
296 "define-derived-mode" "define-minor-mode"
297 "define-generic-mode" "define-global-minor-mode"
298 "define-globalized-minor-mode" "define-skeleton"
299 "define-widget" "ert-deftest"))
300 (el-vdefs '("defconst" "defcustom" "defvaralias" "defvar-local"
301 "defface"))
302 (el-tdefs '("defgroup" "deftheme"))
303 (el-errs '("user-error"))
304 ;; Common-Lisp constructs supported by EIEIO. FIXME: namespace.
305 (eieio-fdefs '("defgeneric" "defmethod"))
306 (eieio-tdefs '("defclass"))
307 ;; Common-Lisp constructs supported by cl-lib.
308 (cl-lib-fdefs '("defmacro" "defsubst" "defun" "defmethod" "defgeneric"))
309 (cl-lib-tdefs '("defstruct" "deftype"))
310 (cl-lib-errs '("assert" "check-type"))
311 ;; Common-Lisp constructs not supported by cl-lib.
312 (cl-fdefs '("defsetf" "define-method-combination"
313 "define-condition" "define-setf-expander"
314 ;; "define-function"??
315 "define-compiler-macro" "define-modify-macro"))
316 (cl-vdefs '("define-symbol-macro" "defconstant" "defparameter"))
317 (cl-tdefs '("defpackage" "defstruct" "deftype"))
318 (cl-kw '("block" "break" "case" "ccase" "compiler-let" "ctypecase"
319 "declaim" "destructuring-bind" "do" "do*"
320 "ecase" "etypecase" "eval-when" "flet" "flet*"
321 "go" "handler-case" "handler-bind" "in-package" ;; "inline"
322 "labels" "letf" "locally" "loop"
323 "macrolet" "multiple-value-bind" "multiple-value-prog1"
324 "proclaim" "prog" "prog*" "progv"
325 "restart-case" "restart-bind" "return" "return-from"
326 "symbol-macrolet" "tagbody" "the" "typecase"
327 "with-accessors" "with-compilation-unit"
328 "with-condition-restarts" "with-hash-table-iterator"
329 "with-input-from-string" "with-open-file"
330 "with-open-stream" "with-package-iterator"
331 "with-simple-restart" "with-slots" "with-standard-io-syntax"))
332 (cl-errs '("abort" "cerror")))
333 (let ((vdefs (eval-when-compile
334 (append lisp-vdefs el-vdefs cl-vdefs)))
335 (tdefs (eval-when-compile
336 (append el-tdefs eieio-tdefs cl-tdefs cl-lib-tdefs
337 (mapcar (lambda (s) (concat "cl-" s)) cl-lib-tdefs))))
338 ;; Elisp and Common Lisp definers.
339 (el-defs-re (eval-when-compile
340 (regexp-opt (append lisp-fdefs lisp-vdefs
341 el-fdefs el-vdefs el-tdefs
342 (mapcar (lambda (s) (concat "cl-" s))
343 (append cl-lib-fdefs cl-lib-tdefs))
344 eieio-fdefs eieio-tdefs)
345 t)))
346 (cl-defs-re (eval-when-compile
347 (regexp-opt (append lisp-fdefs lisp-vdefs
348 cl-lib-fdefs cl-lib-tdefs
349 eieio-fdefs eieio-tdefs
350 cl-fdefs cl-vdefs cl-tdefs)
351 t)))
352 ;; Common Lisp keywords (Elisp keywords are handled dynamically).
353 (cl-kws-re (eval-when-compile
354 (regexp-opt (append lisp-kw cl-kw) t)))
355 ;; Elisp and Common Lisp "errors".
356 (el-errs-re (eval-when-compile
357 (regexp-opt (append (mapcar (lambda (s) (concat "cl-" s))
358 cl-lib-errs)
359 lisp-errs el-errs)
360 t)))
361 (cl-errs-re (eval-when-compile
362 (regexp-opt (append lisp-errs cl-lib-errs cl-errs) t))))
363 (dolist (v vdefs)
364 (put (intern v) 'lisp-define-type 'var))
365 (dolist (v tdefs)
366 (put (intern v) 'lisp-define-type 'type))
368 (define-obsolete-variable-alias 'lisp-font-lock-keywords-1
369 'lisp-el-font-lock-keywords-1 "24.4")
370 (defconst lisp-el-font-lock-keywords-1
371 `( ;; Definitions.
372 (,(concat "(" el-defs-re "\\_>"
373 ;; Any whitespace and defined object.
374 "[ \t']*"
375 "\\(([ \t']*\\)?" ;; An opening paren.
376 "\\(\\(setf\\)[ \t]+" lisp-mode-symbol-regexp
377 "\\|" lisp-mode-symbol-regexp "\\)?")
378 (1 font-lock-keyword-face)
379 (3 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type)))
380 (cond ((eq type 'var) font-lock-variable-name-face)
381 ((eq type 'type) font-lock-type-face)
382 ;; If match-string 2 is non-nil, we encountered a
383 ;; form like (defalias (intern (concat s "-p"))),
384 ;; unless match-string 4 is also there. Then its a
385 ;; defmethod with (setf foo) as name.
386 ((or (not (match-string 2)) ;; Normal defun.
387 (and (match-string 2) ;; Setf method.
388 (match-string 4)))
389 font-lock-function-name-face)))
390 nil t))
391 ;; Emacs Lisp autoload cookies. Supports the slightly different
392 ;; forms used by mh-e, calendar, etc.
393 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend))
394 "Subdued level highlighting for Emacs Lisp mode.")
396 (defconst lisp-cl-font-lock-keywords-1
397 `( ;; Definitions.
398 (,(concat "(" cl-defs-re "\\_>"
399 ;; Any whitespace and defined object.
400 "[ \t']*"
401 "\\(([ \t']*\\)?" ;; An opening paren.
402 "\\(\\(setf\\)[ \t]+" lisp-mode-symbol-regexp
403 "\\|" lisp-mode-symbol-regexp "\\)?")
404 (1 font-lock-keyword-face)
405 (3 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type)))
406 (cond ((eq type 'var) font-lock-variable-name-face)
407 ((eq type 'type) font-lock-type-face)
408 ((or (not (match-string 2)) ;; Normal defun.
409 (and (match-string 2) ;; Setf function.
410 (match-string 4)))
411 font-lock-function-name-face)))
412 nil t)))
413 "Subdued level highlighting for Lisp modes.")
415 (define-obsolete-variable-alias 'lisp-font-lock-keywords-2
416 'lisp-el-font-lock-keywords-2 "24.4")
417 (defconst lisp-el-font-lock-keywords-2
418 (append
419 lisp-el-font-lock-keywords-1
420 `( ;; Regexp negated char group.
421 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)
422 ;; Erroneous structures.
423 (,(concat "(" el-errs-re "\\_>")
424 (1 font-lock-warning-face))
425 ;; Control structures. Common Lisp forms.
426 (lisp--el-match-keyword . 1)
427 ;; Exit/Feature symbols as constants.
428 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\_>"
429 "[ \t']*\\(" lisp-mode-symbol-regexp "\\)?")
430 (1 font-lock-keyword-face)
431 (2 font-lock-constant-face nil t))
432 ;; Words inside \\[] tend to be for `substitute-command-keys'.
433 (,(concat "\\\\\\\\\\[\\(" lisp-mode-symbol-regexp "\\)\\]")
434 (1 font-lock-constant-face prepend))
435 ;; Ineffective backslashes (typically in need of doubling).
436 ("\\(\\\\\\)\\([^\"\\]\\)"
437 (1 (elisp--font-lock-backslash) prepend))
438 ;; Words inside ‘’ and `' tend to be symbol names.
439 (,(concat "[`‘]\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)"
440 lisp-mode-symbol-regexp "\\)['’]")
441 (1 font-lock-constant-face prepend))
442 ;; Constant values.
443 (,(concat "\\_<:" lisp-mode-symbol-regexp "\\_>")
444 (0 font-lock-builtin-face))
445 ;; ELisp and CLisp `&' keywords as types.
446 (,(concat "\\_<\\&" lisp-mode-symbol-regexp "\\_>")
447 . font-lock-type-face)
448 ;; ELisp regexp grouping constructs
449 (,(lambda (bound)
450 (catch 'found
451 ;; The following loop is needed to continue searching after matches
452 ;; that do not occur in strings. The associated regexp matches one
453 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
454 ;; avoid highlighting, for example, `\\(' in `\\\\('.
455 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
456 (unless (match-beginning 2)
457 (let ((face (get-text-property (1- (point)) 'face)))
458 (when (or (and (listp face)
459 (memq 'font-lock-string-face face))
460 (eq 'font-lock-string-face face))
461 (throw 'found t)))))))
462 (1 'font-lock-regexp-grouping-backslash prepend)
463 (3 'font-lock-regexp-grouping-construct prepend))
464 ;; This is too general -- rms.
465 ;; A user complained that he has functions whose names start with `do'
466 ;; and that they get the wrong color.
467 ;; ;; CL `with-' and `do-' constructs
468 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
469 (lisp--match-hidden-arg
470 (0 '(face font-lock-warning-face
471 help-echo "Hidden behind deeper element; move to another line?")))
473 "Gaudy level highlighting for Emacs Lisp mode.")
475 (defconst lisp-cl-font-lock-keywords-2
476 (append
477 lisp-cl-font-lock-keywords-1
478 `( ;; Regexp negated char group.
479 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)
480 ;; Control structures. Common Lisp forms.
481 (,(concat "(" cl-kws-re "\\_>") . 1)
482 ;; Exit/Feature symbols as constants.
483 (,(concat "(\\(catch\\|throw\\|provide\\|require\\)\\_>"
484 "[ \t']*\\(" lisp-mode-symbol-regexp "\\)?")
485 (1 font-lock-keyword-face)
486 (2 font-lock-constant-face nil t))
487 ;; Erroneous structures.
488 (,(concat "(" cl-errs-re "\\_>")
489 (1 font-lock-warning-face))
490 ;; Words inside ‘’ and `' tend to be symbol names.
491 (,(concat "[`‘]\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)"
492 lisp-mode-symbol-regexp "\\)['’]")
493 (1 font-lock-constant-face prepend))
494 ;; Constant values.
495 (,(concat "\\_<:" lisp-mode-symbol-regexp "\\_>")
496 (0 font-lock-builtin-face))
497 ;; ELisp and CLisp `&' keywords as types.
498 (,(concat "\\_<\\&" lisp-mode-symbol-regexp "\\_>")
499 . font-lock-type-face)
500 ;; This is too general -- rms.
501 ;; A user complained that he has functions whose names start with `do'
502 ;; and that they get the wrong color.
503 ;; ;; CL `with-' and `do-' constructs
504 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
505 (lisp--match-hidden-arg
506 (0 '(face font-lock-warning-face
507 help-echo "Hidden behind deeper element; move to another line?")))
509 "Gaudy level highlighting for Lisp modes.")))
511 (define-obsolete-variable-alias 'lisp-font-lock-keywords
512 'lisp-el-font-lock-keywords "24.4")
513 (defvar lisp-el-font-lock-keywords lisp-el-font-lock-keywords-1
514 "Default expressions to highlight in Emacs Lisp mode.")
515 (defvar lisp-cl-font-lock-keywords lisp-cl-font-lock-keywords-1
516 "Default expressions to highlight in Lisp modes.")
518 (defun lisp-string-in-doc-position-p (listbeg startpos)
519 "Return true if a doc string may occur at STARTPOS inside a list.
520 LISTBEG is the position of the start of the innermost list
521 containing STARTPOS."
522 (let* ((firstsym (and listbeg
523 (save-excursion
524 (goto-char listbeg)
525 (and (looking-at
526 (eval-when-compile
527 (concat "([ \t\n]*\\("
528 lisp-mode-symbol-regexp "\\)")))
529 (match-string 1)))))
530 (docelt (and firstsym
531 (function-get (intern-soft firstsym)
532 lisp-doc-string-elt-property))))
533 (and docelt
534 ;; It's a string in a form that can have a docstring.
535 ;; Check whether it's in docstring position.
536 (save-excursion
537 (when (functionp docelt)
538 (goto-char (match-end 1))
539 (setq docelt (funcall docelt)))
540 (goto-char listbeg)
541 (forward-char 1)
542 (condition-case nil
543 (while (and (> docelt 0) (< (point) startpos)
544 (progn (forward-sexp 1) t))
545 (setq docelt (1- docelt)))
546 (error nil))
547 (and (zerop docelt) (<= (point) startpos)
548 (progn (forward-comment (point-max)) t)
549 (= (point) startpos))))))
551 (defun lisp-string-after-doc-keyword-p (listbeg startpos)
552 "Return true if `:documentation' symbol ends at STARTPOS inside a list.
553 LISTBEG is the position of the start of the innermost list
554 containing STARTPOS."
555 (and listbeg ; We are inside a Lisp form.
556 (save-excursion
557 (goto-char startpos)
558 (ignore-errors
559 (progn (backward-sexp 1)
560 (looking-at ":documentation\\_>"))))))
562 (defun lisp-font-lock-syntactic-face-function (state)
563 "Return syntactic face function for the position represented by STATE.
564 STATE is a `parse-partial-sexp' state, and the returned function is the
565 Lisp font lock syntactic face function."
566 (if (nth 3 state)
567 ;; This might be a (doc)string or a |...| symbol.
568 (let ((startpos (nth 8 state)))
569 (if (eq (char-after startpos) ?|)
570 ;; This is not a string, but a |...| symbol.
572 (let ((listbeg (nth 1 state)))
573 (if (or (lisp-string-in-doc-position-p listbeg startpos)
574 (lisp-string-after-doc-keyword-p listbeg startpos))
575 font-lock-doc-face
576 font-lock-string-face))))
577 font-lock-comment-face))
579 (defun lisp-adaptive-fill ()
580 "Return fill prefix found at point.
581 Value for `adaptive-fill-function'."
582 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
583 ;; a single docstring. Let's fix it here.
584 (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") ""))
586 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
587 elisp)
588 "Common initialization routine for lisp modes.
589 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
590 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
591 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
592 font-lock keywords will not be case sensitive."
593 (when lisp-syntax
594 (set-syntax-table lisp-mode-syntax-table))
595 (setq-local paragraph-ignore-fill-prefix t)
596 (setq-local fill-paragraph-function 'lisp-fill-paragraph)
597 (setq-local adaptive-fill-function #'lisp-adaptive-fill)
598 ;; Adaptive fill mode gets in the way of auto-fill,
599 ;; and should make no difference for explicit fill
600 ;; because lisp-fill-paragraph should do the job.
601 ;; I believe that newcomment's auto-fill code properly deals with it -stef
602 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
603 (setq-local indent-line-function 'lisp-indent-line)
604 (setq-local indent-region-function 'lisp-indent-region)
605 (setq-local comment-indent-function #'lisp-comment-indent)
606 (setq-local outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
607 (setq-local outline-level 'lisp-outline-level)
608 (setq-local add-log-current-defun-function #'lisp-current-defun-name)
609 (setq-local comment-start ";")
610 (setq-local comment-start-skip ";+ *")
611 (setq-local comment-add 1) ;default to `;;' in comment-region
612 (setq-local comment-column 40)
613 (setq-local comment-use-syntax t)
614 (setq-local imenu-generic-expression lisp-imenu-generic-expression)
615 (setq-local multibyte-syntax-as-symbol t)
616 ;; (setq-local syntax-begin-function 'beginning-of-defun) ;;Bug#16247.
617 (setq font-lock-defaults
618 `(,(if elisp '(lisp-el-font-lock-keywords
619 lisp-el-font-lock-keywords-1
620 lisp-el-font-lock-keywords-2)
621 '(lisp-cl-font-lock-keywords
622 lisp-cl-font-lock-keywords-1
623 lisp-cl-font-lock-keywords-2))
624 nil ,keywords-case-insensitive nil nil
625 (font-lock-mark-block-function . mark-defun)
626 (font-lock-extra-managed-props help-echo)
627 (font-lock-syntactic-face-function
628 . lisp-font-lock-syntactic-face-function)))
629 (setq-local prettify-symbols-alist lisp-prettify-symbols-alist)
630 (setq-local electric-pair-skip-whitespace 'chomp)
631 (setq-local electric-pair-open-newline-between-pairs nil))
633 (defun lisp-outline-level ()
634 "Lisp mode `outline-level' function."
635 (let ((len (- (match-end 0) (match-beginning 0))))
636 (if (looking-at "(\\|;;;###autoload")
637 1000
638 len)))
640 (defun lisp-current-defun-name ()
641 "Return the name of the defun at point, or nil."
642 (save-excursion
643 (let ((location (point)))
644 ;; If we are now precisely at the beginning of a defun, make sure
645 ;; beginning-of-defun finds that one rather than the previous one.
646 (or (eobp) (forward-char 1))
647 (beginning-of-defun)
648 ;; Make sure we are really inside the defun found, not after it.
649 (when (and (looking-at "\\s(")
650 (progn (end-of-defun)
651 (< location (point)))
652 (progn (forward-sexp -1)
653 (>= location (point))))
654 (if (looking-at "\\s(")
655 (forward-char 1))
656 ;; Skip the defining construct name, typically "defun" or
657 ;; "defvar".
658 (forward-sexp 1)
659 ;; The second element is usually a symbol being defined. If it
660 ;; is not, use the first symbol in it.
661 (skip-chars-forward " \t\n'(")
662 (buffer-substring-no-properties (point)
663 (progn (forward-sexp 1)
664 (point)))))))
666 (defvar lisp-mode-shared-map
667 (let ((map (make-sparse-keymap)))
668 (set-keymap-parent map prog-mode-map)
669 (define-key map "\e\C-q" 'indent-sexp)
670 (define-key map "\177" 'backward-delete-char-untabify)
671 ;; This gets in the way when viewing a Lisp file in view-mode. As
672 ;; long as [backspace] is mapped into DEL via the
673 ;; function-key-map, this should remain disabled!!
674 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
675 map)
676 "Keymap for commands shared by all sorts of Lisp modes.")
678 (defcustom lisp-mode-hook nil
679 "Hook run when entering Lisp mode."
680 :options '(imenu-add-menubar-index)
681 :type 'hook
682 :group 'lisp)
684 (defcustom lisp-interaction-mode-hook nil
685 "Hook run when entering Lisp Interaction mode."
686 :options '(eldoc-mode)
687 :type 'hook
688 :group 'lisp)
690 ;;; Generic Lisp mode.
692 (defvar lisp-mode-map
693 (let ((map (make-sparse-keymap))
694 (menu-map (make-sparse-keymap "Lisp")))
695 (set-keymap-parent map lisp-mode-shared-map)
696 (define-key map "\e\C-x" 'lisp-eval-defun)
697 (define-key map "\C-c\C-z" 'run-lisp)
698 (bindings--define-key map [menu-bar lisp] (cons "Lisp" menu-map))
699 (bindings--define-key menu-map [run-lisp]
700 '(menu-item "Run inferior Lisp" run-lisp
701 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
702 (bindings--define-key menu-map [ev-def]
703 '(menu-item "Eval defun" lisp-eval-defun
704 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
705 (bindings--define-key menu-map [ind-sexp]
706 '(menu-item "Indent sexp" indent-sexp
707 :help "Indent each line of the list starting just after point"))
708 map)
709 "Keymap for ordinary Lisp mode.
710 All commands in `lisp-mode-shared-map' are inherited by this map.")
712 (define-derived-mode lisp-mode prog-mode "Lisp"
713 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
714 Commands:
715 Delete converts tabs to spaces as it moves back.
716 Blank lines separate paragraphs. Semicolons start comments.
718 \\{lisp-mode-map}
719 Note that `run-lisp' may be used either to start an inferior Lisp job
720 or to switch back to an existing one."
721 (lisp-mode-variables nil t)
722 (setq-local find-tag-default-function 'lisp-find-tag-default)
723 (setq-local comment-start-skip
724 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
725 (setq imenu-case-fold-search t))
727 (defun lisp-find-tag-default ()
728 (let ((default (find-tag-default)))
729 (when (stringp default)
730 (if (string-match ":+" default)
731 (substring default (match-end 0))
732 default))))
734 ;; Used in old LispM code.
735 (defalias 'common-lisp-mode 'lisp-mode)
737 (autoload 'lisp-eval-defun "inf-lisp" nil t)
739 (defun lisp-comment-indent ()
740 "Like `comment-indent-default', but don't put space after open paren."
741 (or (when (looking-at "\\s<\\s<")
742 (let ((pt (point)))
743 (skip-syntax-backward " ")
744 (if (eq (preceding-char) ?\()
745 (cons (current-column) (current-column))
746 (goto-char pt)
747 nil)))
748 (comment-indent-default)))
750 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
752 (defcustom lisp-indent-offset nil
753 "If non-nil, indent second line of expressions that many more columns."
754 :group 'lisp
755 :type '(choice (const nil) integer))
756 (put 'lisp-indent-offset 'safe-local-variable
757 (lambda (x) (or (null x) (integerp x))))
759 (defcustom lisp-indent-function 'lisp-indent-function
760 "A function to be called by `calculate-lisp-indent'.
761 It indents the arguments of a Lisp function call. This function
762 should accept two arguments: the indent-point, and the
763 `parse-partial-sexp' state at that position. One option for this
764 function is `common-lisp-indent-function'."
765 :type 'function
766 :group 'lisp)
768 (defun lisp-ppss (&optional pos)
769 "Return Parse-Partial-Sexp State at POS, defaulting to point.
770 Like `syntax-ppss' but includes the character address of the last
771 complete sexp in the innermost containing list at position
772 2 (counting from 0). This is important for lisp indentation."
773 (unless pos (setq pos (point)))
774 (let ((pss (syntax-ppss pos)))
775 (if (nth 9 pss)
776 (let ((sexp-start (car (last (nth 9 pss)))))
777 (parse-partial-sexp sexp-start pos nil nil (syntax-ppss sexp-start)))
778 pss)))
780 (cl-defstruct (lisp-indent-state
781 (:constructor nil)
782 (:constructor lisp-indent-initial-state
783 (&aux (ppss (lisp-ppss))
784 (ppss-point (point))
785 (stack (make-list (1+ (car ppss)) nil)))))
786 stack ;; Cached indentation, per depth.
787 ppss
788 ppss-point)
790 (defun lisp-indent-calc-next (state)
791 "Move to next line and return calculated indent for it.
792 STATE is updated by side effect, the first state should be
793 created by `lisp-indent-initial-state'. This function may move
794 by more than one line to cross a string literal."
795 (pcase-let* (((cl-struct lisp-indent-state
796 (stack indent-stack) ppss ppss-point)
797 state)
798 (indent-depth (car ppss)) ; Corresponding to indent-stack.
799 (depth indent-depth))
800 ;; Parse this line so we can learn the state to indent the
801 ;; next line.
802 (while (let ((last-sexp (nth 2 ppss)))
803 (setq ppss (parse-partial-sexp
804 ppss-point (progn (end-of-line) (point))
805 nil nil ppss))
806 ;; Preserve last sexp of state (position 2) for
807 ;; `calculate-lisp-indent', if we're at the same depth.
808 (if (and (not (nth 2 ppss)) (= depth (car ppss)))
809 (setf (nth 2 ppss) last-sexp)
810 (setq last-sexp (nth 2 ppss)))
811 (setq depth (car ppss))
812 ;; Skip over newlines within strings.
813 (nth 3 ppss))
814 (let ((string-start (nth 8 ppss)))
815 (setq ppss (parse-partial-sexp (point) (point-max)
816 nil nil ppss 'syntax-table))
817 (setf (nth 2 ppss) string-start) ; Finished a complete string.
818 (setq depth (car ppss)))
819 (setq ppss-point (point)))
820 (setq ppss-point (point))
821 (let* ((depth-delta (- depth indent-depth)))
822 (cond ((< depth-delta 0)
823 (setq indent-stack (nthcdr (- depth-delta) indent-stack)))
824 ((> depth-delta 0)
825 (setq indent-stack (nconc (make-list depth-delta nil)
826 indent-stack)))))
827 (prog1
828 (let (indent)
829 (cond ((= (forward-line 1) 1) nil)
830 ((car indent-stack))
831 ((integerp (setq indent (calculate-lisp-indent ppss)))
832 (setf (car indent-stack) indent))
833 ((consp indent) ; (COLUMN CONTAINING-SEXP-START)
834 (car indent))
835 ;; This only happens if we're in a string.
836 (t (error "This shouldn't happen"))))
837 (setf (lisp-indent-state-stack state) indent-stack)
838 (setf (lisp-indent-state-ppss-point state) ppss-point)
839 (setf (lisp-indent-state-ppss state) ppss))))
841 (defun lisp-indent-region (start end)
842 "Indent region as Lisp code, efficiently."
843 (save-excursion
844 (setq end (copy-marker end))
845 (goto-char start)
846 (beginning-of-line)
847 ;; The default `indent-region-line-by-line' doesn't hold a running
848 ;; parse state, which forces each indent call to reparse from the
849 ;; beginning. That has O(n^2) complexity.
850 (let* ((parse-state (lisp-indent-initial-state))
851 (pr (unless (minibufferp)
852 (make-progress-reporter "Indenting region..." (point) end))))
853 (let ((ppss (lisp-indent-state-ppss parse-state)))
854 (unless (or (and (bolp) (eolp)) (nth 3 ppss))
855 (lisp-indent-line (calculate-lisp-indent ppss))))
856 (let ((indent nil))
857 (while (progn (setq indent (lisp-indent-calc-next parse-state))
858 (< (point) end))
859 (unless (or (and (bolp) (eolp)) (not indent))
860 (lisp-indent-line indent))
861 (and pr (progress-reporter-update pr (point)))))
862 (and pr (progress-reporter-done pr))
863 (move-marker end nil))))
865 (defun lisp-indent-line (&optional indent)
866 "Indent current line as Lisp code."
867 (interactive)
868 (let ((pos (- (point-max) (point)))
869 (indent (progn (beginning-of-line)
870 (or indent (calculate-lisp-indent (lisp-ppss))))))
871 (skip-chars-forward " \t")
872 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
873 ;; Don't alter indentation of a ;;; comment line
874 ;; or a line that starts in a string.
875 ;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
876 (goto-char (- (point-max) pos))
877 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
878 ;; Single-semicolon comment lines should be indented
879 ;; as comment lines, not as code.
880 (progn (indent-for-comment) (forward-char -1))
881 (if (listp indent) (setq indent (car indent)))
882 (indent-line-to indent))
883 ;; If initial point was within line's indentation,
884 ;; position after the indentation. Else stay at same point in text.
885 (if (> (- (point-max) pos) (point))
886 (goto-char (- (point-max) pos))))))
888 (defvar calculate-lisp-indent-last-sexp)
890 (defun calculate-lisp-indent (&optional parse-start)
891 "Return appropriate indentation for current line as Lisp code.
892 In usual case returns an integer: the column to indent to.
893 If the value is nil, that means don't change the indentation
894 because the line starts inside a string.
896 PARSE-START may be a buffer position to start parsing from, or a
897 parse state as returned by calling `parse-partial-sexp' up to the
898 beginning of the current line.
900 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
901 This means that following lines at the same level of indentation
902 should not necessarily be indented the same as this line.
903 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
904 is the buffer position of the start of the containing expression."
905 (save-excursion
906 (beginning-of-line)
907 (let ((indent-point (point))
908 state
909 ;; setting this to a number inhibits calling hook
910 (desired-indent nil)
911 (retry t)
912 calculate-lisp-indent-last-sexp containing-sexp)
913 (cond ((or (markerp parse-start) (integerp parse-start))
914 (goto-char parse-start))
915 ((null parse-start) (beginning-of-defun))
916 (t (setq state parse-start)))
917 (unless state
918 ;; Find outermost containing sexp
919 (while (< (point) indent-point)
920 (setq state (parse-partial-sexp (point) indent-point 0))))
921 ;; Find innermost containing sexp
922 (while (and retry
923 state
924 (> (elt state 0) 0))
925 (setq retry nil)
926 (setq calculate-lisp-indent-last-sexp (elt state 2))
927 (setq containing-sexp (elt state 1))
928 ;; Position following last unclosed open.
929 (goto-char (1+ containing-sexp))
930 ;; Is there a complete sexp since then?
931 (if (and calculate-lisp-indent-last-sexp
932 (> calculate-lisp-indent-last-sexp (point)))
933 ;; Yes, but is there a containing sexp after that?
934 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
935 indent-point 0)))
936 (if (setq retry (car (cdr peek))) (setq state peek)))))
937 (if retry
939 ;; Innermost containing sexp found
940 (goto-char (1+ containing-sexp))
941 (if (not calculate-lisp-indent-last-sexp)
942 ;; indent-point immediately follows open paren.
943 ;; Don't call hook.
944 (setq desired-indent (current-column))
945 ;; Find the start of first element of containing sexp.
946 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
947 (cond ((looking-at "\\s(")
948 ;; First element of containing sexp is a list.
949 ;; Indent under that list.
951 ((> (save-excursion (forward-line 1) (point))
952 calculate-lisp-indent-last-sexp)
953 ;; This is the first line to start within the containing sexp.
954 ;; It's almost certainly a function call.
955 (if (= (point) calculate-lisp-indent-last-sexp)
956 ;; Containing sexp has nothing before this line
957 ;; except the first element. Indent under that element.
959 ;; Skip the first element, find start of second (the first
960 ;; argument of the function call) and indent under.
961 (progn (forward-sexp 1)
962 (parse-partial-sexp (point)
963 calculate-lisp-indent-last-sexp
964 0 t)))
965 (backward-prefix-chars))
967 ;; Indent beneath first sexp on same line as
968 ;; `calculate-lisp-indent-last-sexp'. Again, it's
969 ;; almost certainly a function call.
970 (goto-char calculate-lisp-indent-last-sexp)
971 (beginning-of-line)
972 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
973 0 t)
974 (backward-prefix-chars)))))
975 ;; Point is at the point to indent under unless we are inside a string.
976 ;; Call indentation hook except when overridden by lisp-indent-offset
977 ;; or if the desired indentation has already been computed.
978 (let ((normal-indent (current-column)))
979 (cond ((elt state 3)
980 ;; Inside a string, don't change indentation.
981 nil)
982 ((and (integerp lisp-indent-offset) containing-sexp)
983 ;; Indent by constant offset
984 (goto-char containing-sexp)
985 (+ (current-column) lisp-indent-offset))
986 ;; in this case calculate-lisp-indent-last-sexp is not nil
987 (calculate-lisp-indent-last-sexp
989 ;; try to align the parameters of a known function
990 (and lisp-indent-function
991 (not retry)
992 (funcall lisp-indent-function indent-point state))
993 ;; If the function has no special alignment
994 ;; or it does not apply to this argument,
995 ;; try to align a constant-symbol under the last
996 ;; preceding constant symbol, if there is such one of
997 ;; the last 2 preceding symbols, in the previous
998 ;; uncommented line.
999 (and (save-excursion
1000 (goto-char indent-point)
1001 (skip-chars-forward " \t")
1002 (looking-at ":"))
1003 ;; The last sexp may not be at the indentation
1004 ;; where it begins, so find that one, instead.
1005 (save-excursion
1006 (goto-char calculate-lisp-indent-last-sexp)
1007 ;; Handle prefix characters and whitespace
1008 ;; following an open paren. (Bug#1012)
1009 (backward-prefix-chars)
1010 (while (not (or (looking-back "^[ \t]*\\|([ \t]+"
1011 (line-beginning-position))
1012 (and containing-sexp
1013 (>= (1+ containing-sexp) (point)))))
1014 (forward-sexp -1)
1015 (backward-prefix-chars))
1016 (setq calculate-lisp-indent-last-sexp (point)))
1017 (> calculate-lisp-indent-last-sexp
1018 (save-excursion
1019 (goto-char (1+ containing-sexp))
1020 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1021 (point)))
1022 (let ((parse-sexp-ignore-comments t)
1023 indent)
1024 (goto-char calculate-lisp-indent-last-sexp)
1025 (or (and (looking-at ":")
1026 (setq indent (current-column)))
1027 (and (< (line-beginning-position)
1028 (prog2 (backward-sexp) (point)))
1029 (looking-at ":")
1030 (setq indent (current-column))))
1031 indent))
1032 ;; another symbols or constants not preceded by a constant
1033 ;; as defined above.
1034 normal-indent))
1035 ;; in this case calculate-lisp-indent-last-sexp is nil
1036 (desired-indent)
1038 normal-indent))))))
1040 (defun lisp-indent-function (indent-point state)
1041 "This function is the normal value of the variable `lisp-indent-function'.
1042 The function `calculate-lisp-indent' calls this to determine
1043 if the arguments of a Lisp function call should be indented specially.
1045 INDENT-POINT is the position at which the line being indented begins.
1046 Point is located at the point to indent under (for default indentation);
1047 STATE is the `parse-partial-sexp' state for that position.
1049 If the current line is in a call to a Lisp function that has a non-nil
1050 property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
1051 it specifies how to indent. The property value can be:
1053 * `defun', meaning indent `defun'-style
1054 (this is also the case if there is no property and the function
1055 has a name that begins with \"def\", and three or more arguments);
1057 * an integer N, meaning indent the first N arguments specially
1058 (like ordinary function arguments), and then indent any further
1059 arguments like a body;
1061 * a function to call that returns the indentation (or nil).
1062 `lisp-indent-function' calls this function with the same two arguments
1063 that it itself received.
1065 This function returns either the indentation to use, or nil if the
1066 Lisp function does not specify a special indentation."
1067 (let ((normal-indent (current-column)))
1068 (goto-char (1+ (elt state 1)))
1069 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1070 (if (and (elt state 2)
1071 (not (looking-at "\\sw\\|\\s_")))
1072 ;; car of form doesn't seem to be a symbol
1073 (progn
1074 (if (not (> (save-excursion (forward-line 1) (point))
1075 calculate-lisp-indent-last-sexp))
1076 (progn (goto-char calculate-lisp-indent-last-sexp)
1077 (beginning-of-line)
1078 (parse-partial-sexp (point)
1079 calculate-lisp-indent-last-sexp 0 t)))
1080 ;; Indent under the list or under the first sexp on the same
1081 ;; line as calculate-lisp-indent-last-sexp. Note that first
1082 ;; thing on that line has to be complete sexp since we are
1083 ;; inside the innermost containing sexp.
1084 (backward-prefix-chars)
1085 (current-column))
1086 (let ((function (buffer-substring (point)
1087 (progn (forward-sexp 1) (point))))
1088 method)
1089 (setq method (or (function-get (intern-soft function)
1090 'lisp-indent-function)
1091 (get (intern-soft function) 'lisp-indent-hook)))
1092 (cond ((or (eq method 'defun)
1093 (and (null method)
1094 (> (length function) 3)
1095 (string-match "\\`def" function)))
1096 (lisp-indent-defform state indent-point))
1097 ((integerp method)
1098 (lisp-indent-specform method state
1099 indent-point normal-indent))
1100 (method
1101 (funcall method indent-point state)))))))
1103 (defcustom lisp-body-indent 2
1104 "Number of columns to indent the second line of a `(def...)' form."
1105 :group 'lisp
1106 :type 'integer)
1107 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1109 (defun lisp-indent-specform (count state indent-point normal-indent)
1110 (let ((containing-form-start (elt state 1))
1111 (i count)
1112 body-indent containing-form-column)
1113 ;; Move to the start of containing form, calculate indentation
1114 ;; to use for non-distinguished forms (> count), and move past the
1115 ;; function symbol. lisp-indent-function guarantees that there is at
1116 ;; least one word or symbol character following open paren of containing
1117 ;; form.
1118 (goto-char containing-form-start)
1119 (setq containing-form-column (current-column))
1120 (setq body-indent (+ lisp-body-indent containing-form-column))
1121 (forward-char 1)
1122 (forward-sexp 1)
1123 ;; Now find the start of the last form.
1124 (parse-partial-sexp (point) indent-point 1 t)
1125 (while (and (< (point) indent-point)
1126 (condition-case ()
1127 (progn
1128 (setq count (1- count))
1129 (forward-sexp 1)
1130 (parse-partial-sexp (point) indent-point 1 t))
1131 (error nil))))
1132 ;; Point is sitting on first character of last (or count) sexp.
1133 (if (> count 0)
1134 ;; A distinguished form. If it is the first or second form use double
1135 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1136 ;; to 2 (the default), this just happens to work the same with if as
1137 ;; the older code, but it makes unwind-protect, condition-case,
1138 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1139 ;; less hacked, behavior can be obtained by replacing below with
1140 ;; (list normal-indent containing-form-start).
1141 (if (<= (- i count) 1)
1142 (list (+ containing-form-column (* 2 lisp-body-indent))
1143 containing-form-start)
1144 (list normal-indent containing-form-start))
1145 ;; A non-distinguished form. Use body-indent if there are no
1146 ;; distinguished forms and this is the first undistinguished form,
1147 ;; or if this is the first undistinguished form and the preceding
1148 ;; distinguished form has indentation at least as great as body-indent.
1149 (if (or (and (= i 0) (= count 0))
1150 (and (= count 0) (<= body-indent normal-indent)))
1151 body-indent
1152 normal-indent))))
1154 (defun lisp-indent-defform (state _indent-point)
1155 (goto-char (car (cdr state)))
1156 (forward-line 1)
1157 (if (> (point) (car (cdr (cdr state))))
1158 (progn
1159 (goto-char (car (cdr state)))
1160 (+ lisp-body-indent (current-column)))))
1163 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1164 ;; like defun if the first form is placed on the next line, otherwise
1165 ;; it is indented like any other form (i.e. forms line up under first).
1167 (put 'autoload 'lisp-indent-function 'defun) ;Elisp
1168 (put 'progn 'lisp-indent-function 0)
1169 (put 'prog1 'lisp-indent-function 1)
1170 (put 'prog2 'lisp-indent-function 2)
1171 (put 'save-excursion 'lisp-indent-function 0) ;Elisp
1172 (put 'save-restriction 'lisp-indent-function 0) ;Elisp
1173 (put 'save-current-buffer 'lisp-indent-function 0) ;Elisp
1174 (put 'let 'lisp-indent-function 1)
1175 (put 'let* 'lisp-indent-function 1)
1176 (put 'while 'lisp-indent-function 1)
1177 (put 'if 'lisp-indent-function 2)
1178 (put 'catch 'lisp-indent-function 1)
1179 (put 'condition-case 'lisp-indent-function 2)
1180 (put 'handler-case 'lisp-indent-function 1) ;CL
1181 (put 'handler-bind 'lisp-indent-function 1) ;CL
1182 (put 'unwind-protect 'lisp-indent-function 1)
1183 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1185 (defun indent-sexp (&optional endpos)
1186 "Indent each line of the list starting just after point.
1187 If optional arg ENDPOS is given, indent each line, stopping when
1188 ENDPOS is encountered."
1189 (interactive)
1190 (let* ((parse-state (lisp-indent-initial-state)))
1191 ;; We need a marker because we modify the buffer
1192 ;; text preceding endpos.
1193 (setq endpos (copy-marker
1194 (if endpos endpos
1195 ;; Get error now if we don't have a complete sexp
1196 ;; after point.
1197 (save-excursion (forward-sexp 1) (point)))))
1198 (save-excursion
1199 (while (let ((indent (lisp-indent-calc-next parse-state))
1200 (ppss (lisp-indent-state-ppss parse-state)))
1201 ;; If the line contains a comment indent it now with
1202 ;; `indent-for-comment'.
1203 (when (and (nth 4 ppss) (<= (nth 8 ppss) endpos))
1204 (save-excursion
1205 (goto-char (lisp-indent-state-ppss-point parse-state))
1206 (indent-for-comment)
1207 (setf (lisp-indent-state-ppss-point parse-state)
1208 (line-end-position))))
1209 (when (< (point) endpos)
1210 ;; Indent the next line, unless it's blank, or just a
1211 ;; comment (we will `indent-for-comment' the latter).
1212 (skip-chars-forward " \t")
1213 (unless (or (eolp) (not indent)
1214 (eq (char-syntax (char-after)) ?<))
1215 (indent-line-to indent))
1216 t))))
1217 (move-marker endpos nil)))
1219 (defun indent-pp-sexp (&optional arg)
1220 "Indent each line of the list starting just after point, or prettyprint it.
1221 A prefix argument specifies pretty-printing."
1222 (interactive "P")
1223 (if arg
1224 (save-excursion
1225 (save-restriction
1226 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1227 (pp-buffer)
1228 (goto-char (point-max))
1229 (if (eq (char-before) ?\n)
1230 (delete-char -1)))))
1231 (indent-sexp))
1233 ;;;; Lisp paragraph filling commands.
1235 (defcustom emacs-lisp-docstring-fill-column 65
1236 "Value of `fill-column' to use when filling a docstring.
1237 Any non-integer value means do not use a different value of
1238 `fill-column' when filling docstrings."
1239 :type '(choice (integer)
1240 (const :tag "Use the current `fill-column'" t))
1241 :group 'lisp)
1242 (put 'emacs-lisp-docstring-fill-column 'safe-local-variable
1243 (lambda (x) (or (eq x t) (integerp x))))
1245 (defun lisp-fill-paragraph (&optional justify)
1246 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1247 If any of the current line is a comment, fill the comment or the
1248 paragraph of it that point is in, preserving the comment's indentation
1249 and initial semicolons."
1250 (interactive "P")
1251 (or (fill-comment-paragraph justify)
1252 ;; Since fill-comment-paragraph returned nil, that means we're not in
1253 ;; a comment: Point is on a program line; we are interested
1254 ;; particularly in docstring lines.
1256 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1257 ;; are buffer-local, but we avoid changing them so that they can be set
1258 ;; to make `forward-paragraph' and friends do something the user wants.
1260 ;; `paragraph-start': The `(' in the character alternative and the
1261 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1262 ;; sexps and backquoted sexps that follow a docstring from being filled
1263 ;; with the docstring. This setting has the consequence of inhibiting
1264 ;; filling many program lines that are not docstrings, which is sensible,
1265 ;; because the user probably asked to fill program lines by accident, or
1266 ;; expecting indentation (perhaps we should try to do indenting in that
1267 ;; case). The `;' and `:' stop the paragraph being filled at following
1268 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1269 ;; escaped to keep font-locking, filling, & paren matching in the source
1270 ;; file happy.
1272 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1273 ;; a docstring and identifies it as a paragraph separator, so that it
1274 ;; won't be filled. (Since the first line of documentation stands alone
1275 ;; in some contexts, filling should not alter the contents the author has
1276 ;; chosen.) Only the first line of a docstring begins with whitespace
1277 ;; and a quotation mark and ends with a period or (rarely) a comma.
1279 ;; The `fill-column' is temporarily bound to
1280 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1281 (let ((paragraph-start
1282 (concat paragraph-start
1283 (format "\\|\\s-*\\([(;%s\"]\\|`(\\|#'(\\)"
1284 ;; If we're inside a string (like the doc
1285 ;; string), don't consider a colon to be
1286 ;; a paragraph-start character.
1287 (if (nth 3 (syntax-ppss))
1289 ":"))))
1290 (paragraph-separate
1291 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1292 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1293 (derived-mode-p 'emacs-lisp-mode))
1294 emacs-lisp-docstring-fill-column
1295 fill-column)))
1296 (fill-paragraph justify))
1297 ;; Never return nil.
1300 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1301 "Indent all lines of code, starting in the region, sideways by ARG columns.
1302 Does not affect lines starting inside comments or strings, assuming that
1303 the start of the region is not inside them.
1305 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1306 The last is a regexp which, if matched at the beginning of a line,
1307 means don't indent that line."
1308 (interactive "r\np")
1309 (let (state)
1310 (save-excursion
1311 (goto-char end)
1312 (setq end (point-marker))
1313 (goto-char start)
1314 (or (bolp)
1315 (setq state (parse-partial-sexp (point)
1316 (progn
1317 (forward-line 1) (point))
1318 nil nil state)))
1319 (while (< (point) end)
1320 (or (car (nthcdr 3 state))
1321 (and nochange-regexp
1322 (looking-at nochange-regexp))
1323 ;; If line does not start in string, indent it
1324 (let ((indent (current-indentation)))
1325 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1326 (or (eolp)
1327 (indent-to (max 0 (+ indent arg)) 0))))
1328 (setq state (parse-partial-sexp (point)
1329 (progn
1330 (forward-line 1) (point))
1331 nil nil state))))))
1333 (provide 'lisp-mode)
1335 ;;; lisp-mode.el ends here