Merge from origin/emacs-25
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob4f3af2a7d7f6985cd2ab0c801205cc8a1f324643
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1999-2016 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 ?\x8a0 " " 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)
168 (defvar lisp-doc-string-elt-property 'doc-string-elt
169 "The symbol property that holds the docstring position info.")
171 (defconst lisp-prettify-symbols-alist '(("lambda" . ?λ))
172 "Alist of symbol/\"pretty\" characters to be displayed.")
174 ;;;; Font-lock support.
176 (defun lisp--match-hidden-arg (limit)
177 (let ((res nil))
178 (while
179 (let ((ppss (parse-partial-sexp (line-beginning-position)
180 (line-end-position)
181 -1)))
182 (skip-syntax-forward " )")
183 (if (or (>= (car ppss) 0)
184 (looking-at ";\\|$"))
185 (progn
186 (forward-line 1)
187 (< (point) limit))
188 (looking-at ".*") ;Set the match-data.
189 (forward-line 1)
190 (setq res (point))
191 nil)))
192 res))
194 (defun lisp--el-non-funcall-position-p (pos)
195 "Heuristically determine whether POS is an evaluated position."
196 (save-match-data
197 (save-excursion
198 (ignore-errors
199 (goto-char pos)
200 (or (eql (char-before) ?\')
201 (let* ((ppss (syntax-ppss))
202 (paren-posns (nth 9 ppss))
203 (parent
204 (when paren-posns
205 (goto-char (car (last paren-posns))) ;(up-list -1)
206 (cond
207 ((ignore-errors
208 (and (eql (char-after) ?\()
209 (when (cdr paren-posns)
210 (goto-char (car (last paren-posns 2)))
211 (looking-at "(\\_<let\\*?\\_>"))))
212 (goto-char (match-end 0))
213 'let)
214 ((looking-at
215 (rx "("
216 (group-n 1 (+ (or (syntax w) (syntax _))))
217 symbol-end))
218 (prog1 (intern-soft (match-string-no-properties 1))
219 (goto-char (match-end 1))))))))
220 (or (eq parent 'declare)
221 (and (eq parent 'let)
222 (progn
223 (forward-sexp 1)
224 (< pos (point))))
225 (and (eq parent 'condition-case)
226 (progn
227 (forward-sexp 2)
228 (< (point) pos))))))))))
230 (defun lisp--el-match-keyword (limit)
231 ;; FIXME: Move to elisp-mode.el.
232 (catch 'found
233 (while (re-search-forward
234 (eval-when-compile
235 (concat "(\\(" lisp-mode-symbol-regexp "\\)\\_>"))
236 limit t)
237 (let ((sym (intern-soft (match-string 1))))
238 (when (or (special-form-p sym)
239 (and (macrop sym)
240 (not (get sym 'no-font-lock-keyword))
241 (not (lisp--el-non-funcall-position-p
242 (match-beginning 0)))))
243 (throw 'found t))))))
245 (defmacro let-when-compile (bindings &rest body)
246 "Like `let*', but allow for compile time optimization.
247 Use BINDINGS as in regular `let*', but in BODY each usage should
248 be wrapped in `eval-when-compile'.
249 This will generate compile-time constants from BINDINGS."
250 (declare (indent 1) (debug let))
251 (letrec ((loop
252 (lambda (bindings)
253 (if (null bindings)
254 (macroexpand-all (macroexp-progn body)
255 macroexpand-all-environment)
256 (let ((binding (pop bindings)))
257 (cl-progv (list (car binding))
258 (list (eval (nth 1 binding) t))
259 (funcall loop bindings)))))))
260 (funcall loop bindings)))
262 (let-when-compile
263 ((lisp-fdefs '("defmacro" "defun"))
264 (lisp-vdefs '("defvar"))
265 (lisp-kw '("cond" "if" "while" "let" "let*" "progn" "prog1"
266 "prog2" "lambda" "unwind-protect" "condition-case"
267 "when" "unless" "with-output-to-string"
268 "ignore-errors" "dotimes" "dolist" "declare"))
269 (lisp-errs '("warn" "error" "signal"))
270 ;; Elisp constructs. Now they are update dynamically
271 ;; from obarray but they are also used for setting up
272 ;; the keywords for Common Lisp.
273 (el-fdefs '("defsubst" "cl-defsubst" "define-inline"
274 "define-advice" "defadvice" "defalias"
275 "define-derived-mode" "define-minor-mode"
276 "define-generic-mode" "define-global-minor-mode"
277 "define-globalized-minor-mode" "define-skeleton"
278 "define-widget" "ert-deftest"))
279 (el-vdefs '("defconst" "defcustom" "defvaralias" "defvar-local"
280 "defface"))
281 (el-tdefs '("defgroup" "deftheme"))
282 (el-errs '("user-error"))
283 ;; Common-Lisp constructs supported by EIEIO. FIXME: namespace.
284 (eieio-fdefs '("defgeneric" "defmethod"))
285 (eieio-tdefs '("defclass"))
286 ;; Common-Lisp constructs supported by cl-lib.
287 (cl-lib-fdefs '("defmacro" "defsubst" "defun" "defmethod" "defgeneric"))
288 (cl-lib-tdefs '("defstruct" "deftype"))
289 (cl-lib-errs '("assert" "check-type"))
290 ;; Common-Lisp constructs not supported by cl-lib.
291 (cl-fdefs '("defsetf" "define-method-combination"
292 "define-condition" "define-setf-expander"
293 ;; "define-function"??
294 "define-compiler-macro" "define-modify-macro"))
295 (cl-vdefs '("define-symbol-macro" "defconstant" "defparameter"))
296 (cl-tdefs '("defpackage" "defstruct" "deftype"))
297 (cl-kw '("block" "break" "case" "ccase" "compiler-let" "ctypecase"
298 "declaim" "destructuring-bind" "do" "do*"
299 "ecase" "etypecase" "eval-when" "flet" "flet*"
300 "go" "handler-case" "handler-bind" "in-package" ;; "inline"
301 "labels" "letf" "locally" "loop"
302 "macrolet" "multiple-value-bind" "multiple-value-prog1"
303 "proclaim" "prog" "prog*" "progv"
304 "restart-case" "restart-bind" "return" "return-from"
305 "symbol-macrolet" "tagbody" "the" "typecase"
306 "with-accessors" "with-compilation-unit"
307 "with-condition-restarts" "with-hash-table-iterator"
308 "with-input-from-string" "with-open-file"
309 "with-open-stream" "with-package-iterator"
310 "with-simple-restart" "with-slots" "with-standard-io-syntax"))
311 (cl-errs '("abort" "cerror")))
312 (let ((vdefs (eval-when-compile
313 (append lisp-vdefs el-vdefs cl-vdefs)))
314 (tdefs (eval-when-compile
315 (append el-tdefs eieio-tdefs cl-tdefs cl-lib-tdefs
316 (mapcar (lambda (s) (concat "cl-" s)) cl-lib-tdefs))))
317 ;; Elisp and Common Lisp definers.
318 (el-defs-re (eval-when-compile
319 (regexp-opt (append lisp-fdefs lisp-vdefs
320 el-fdefs el-vdefs el-tdefs
321 (mapcar (lambda (s) (concat "cl-" s))
322 (append cl-lib-fdefs cl-lib-tdefs))
323 eieio-fdefs eieio-tdefs)
324 t)))
325 (cl-defs-re (eval-when-compile
326 (regexp-opt (append lisp-fdefs lisp-vdefs
327 cl-lib-fdefs cl-lib-tdefs
328 eieio-fdefs eieio-tdefs
329 cl-fdefs cl-vdefs cl-tdefs)
330 t)))
331 ;; Common Lisp keywords (Elisp keywords are handled dynamically).
332 (cl-kws-re (eval-when-compile
333 (regexp-opt (append lisp-kw cl-kw) t)))
334 ;; Elisp and Common Lisp "errors".
335 (el-errs-re (eval-when-compile
336 (regexp-opt (append (mapcar (lambda (s) (concat "cl-" s))
337 cl-lib-errs)
338 lisp-errs el-errs)
339 t)))
340 (cl-errs-re (eval-when-compile
341 (regexp-opt (append lisp-errs cl-lib-errs cl-errs) t))))
342 (dolist (v vdefs)
343 (put (intern v) 'lisp-define-type 'var))
344 (dolist (v tdefs)
345 (put (intern v) 'lisp-define-type 'type))
347 (define-obsolete-variable-alias 'lisp-font-lock-keywords-1
348 'lisp-el-font-lock-keywords-1 "24.4")
349 (defconst lisp-el-font-lock-keywords-1
350 `( ;; Definitions.
351 (,(concat "(" el-defs-re "\\_>"
352 ;; Any whitespace and defined object.
353 "[ \t']*"
354 "\\(([ \t']*\\)?" ;; An opening paren.
355 "\\(\\(setf\\)[ \t]+" lisp-mode-symbol-regexp
356 "\\|" lisp-mode-symbol-regexp "\\)?")
357 (1 font-lock-keyword-face)
358 (3 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type)))
359 (cond ((eq type 'var) font-lock-variable-name-face)
360 ((eq type 'type) font-lock-type-face)
361 ;; If match-string 2 is non-nil, we encountered a
362 ;; form like (defalias (intern (concat s "-p"))),
363 ;; unless match-string 4 is also there. Then its a
364 ;; defmethod with (setf foo) as name.
365 ((or (not (match-string 2)) ;; Normal defun.
366 (and (match-string 2) ;; Setf method.
367 (match-string 4)))
368 font-lock-function-name-face)))
369 nil t))
370 ;; Emacs Lisp autoload cookies. Supports the slightly different
371 ;; forms used by mh-e, calendar, etc.
372 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend))
373 "Subdued level highlighting for Emacs Lisp mode.")
375 (defconst lisp-cl-font-lock-keywords-1
376 `( ;; Definitions.
377 (,(concat "(" cl-defs-re "\\_>"
378 ;; Any whitespace and defined object.
379 "[ \t']*"
380 "\\(([ \t']*\\)?" ;; An opening paren.
381 "\\(\\(setf\\)[ \t]+" lisp-mode-symbol-regexp
382 "\\|" lisp-mode-symbol-regexp "\\)?")
383 (1 font-lock-keyword-face)
384 (3 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type)))
385 (cond ((eq type 'var) font-lock-variable-name-face)
386 ((eq type 'type) font-lock-type-face)
387 ((or (not (match-string 2)) ;; Normal defun.
388 (and (match-string 2) ;; Setf function.
389 (match-string 4)))
390 font-lock-function-name-face)))
391 nil t)))
392 "Subdued level highlighting for Lisp modes.")
394 (define-obsolete-variable-alias 'lisp-font-lock-keywords-2
395 'lisp-el-font-lock-keywords-2 "24.4")
396 (defconst lisp-el-font-lock-keywords-2
397 (append
398 lisp-el-font-lock-keywords-1
399 `( ;; Regexp negated char group.
400 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)
401 ;; Erroneous structures.
402 (,(concat "(" el-errs-re "\\_>")
403 (1 font-lock-warning-face))
404 ;; Control structures. Common Lisp forms.
405 (lisp--el-match-keyword . 1)
406 ;; Exit/Feature symbols as constants.
407 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\_>"
408 "[ \t']*\\(" lisp-mode-symbol-regexp "\\)?")
409 (1 font-lock-keyword-face)
410 (2 font-lock-constant-face nil t))
411 ;; Words inside \\[] tend to be for `substitute-command-keys'.
412 (,(concat "\\\\\\\\\\[\\(" lisp-mode-symbol-regexp "\\)\\]")
413 (1 font-lock-constant-face prepend))
414 ;; Words inside ‘’ and `' tend to be symbol names.
415 (,(concat "[`‘]\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)"
416 lisp-mode-symbol-regexp "\\)['’]")
417 (1 font-lock-constant-face prepend))
418 ;; Constant values.
419 (,(concat "\\_<:" lisp-mode-symbol-regexp "\\_>")
420 (0 font-lock-builtin-face))
421 ;; ELisp and CLisp `&' keywords as types.
422 (,(concat "\\_<\\&" lisp-mode-symbol-regexp "\\_>")
423 . font-lock-type-face)
424 ;; ELisp regexp grouping constructs
425 (,(lambda (bound)
426 (catch 'found
427 ;; The following loop is needed to continue searching after matches
428 ;; that do not occur in strings. The associated regexp matches one
429 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
430 ;; avoid highlighting, for example, `\\(' in `\\\\('.
431 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
432 (unless (match-beginning 2)
433 (let ((face (get-text-property (1- (point)) 'face)))
434 (when (or (and (listp face)
435 (memq 'font-lock-string-face face))
436 (eq 'font-lock-string-face face))
437 (throw 'found t)))))))
438 (1 'font-lock-regexp-grouping-backslash prepend)
439 (3 'font-lock-regexp-grouping-construct prepend))
440 ;; This is too general -- rms.
441 ;; A user complained that he has functions whose names start with `do'
442 ;; and that they get the wrong color.
443 ;; ;; CL `with-' and `do-' constructs
444 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
445 (lisp--match-hidden-arg
446 (0 '(face font-lock-warning-face
447 help-echo "Hidden behind deeper element; move to another line?")))
449 "Gaudy level highlighting for Emacs Lisp mode.")
451 (defconst lisp-cl-font-lock-keywords-2
452 (append
453 lisp-cl-font-lock-keywords-1
454 `( ;; Regexp negated char group.
455 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)
456 ;; Control structures. Common Lisp forms.
457 (,(concat "(" cl-kws-re "\\_>") . 1)
458 ;; Exit/Feature symbols as constants.
459 (,(concat "(\\(catch\\|throw\\|provide\\|require\\)\\_>"
460 "[ \t']*\\(" lisp-mode-symbol-regexp "\\)?")
461 (1 font-lock-keyword-face)
462 (2 font-lock-constant-face nil t))
463 ;; Erroneous structures.
464 (,(concat "(" cl-errs-re "\\_>")
465 (1 font-lock-warning-face))
466 ;; Words inside ‘’ and `' tend to be symbol names.
467 (,(concat "[`‘]\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)"
468 lisp-mode-symbol-regexp "\\)['’]")
469 (1 font-lock-constant-face prepend))
470 ;; Constant values.
471 (,(concat "\\_<:" lisp-mode-symbol-regexp "\\_>")
472 (0 font-lock-builtin-face))
473 ;; ELisp and CLisp `&' keywords as types.
474 (,(concat "\\_<\\&" lisp-mode-symbol-regexp "\\_>")
475 . font-lock-type-face)
476 ;; This is too general -- rms.
477 ;; A user complained that he has functions whose names start with `do'
478 ;; and that they get the wrong color.
479 ;; ;; CL `with-' and `do-' constructs
480 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
481 (lisp--match-hidden-arg
482 (0 '(face font-lock-warning-face
483 help-echo "Hidden behind deeper element; move to another line?")))
485 "Gaudy level highlighting for Lisp modes.")))
487 (define-obsolete-variable-alias 'lisp-font-lock-keywords
488 'lisp-el-font-lock-keywords "24.4")
489 (defvar lisp-el-font-lock-keywords lisp-el-font-lock-keywords-1
490 "Default expressions to highlight in Emacs Lisp mode.")
491 (defvar lisp-cl-font-lock-keywords lisp-cl-font-lock-keywords-1
492 "Default expressions to highlight in Lisp modes.")
494 (defun lisp-string-in-doc-position-p (listbeg startpos)
495 "Return true if a doc string may occur at STARTPOS inside a list.
496 LISTBEG is the position of the start of the innermost list
497 containing STARTPOS."
498 (let* ((firstsym (and listbeg
499 (save-excursion
500 (goto-char listbeg)
501 (and (looking-at
502 (eval-when-compile
503 (concat "([ \t\n]*\\("
504 lisp-mode-symbol-regexp "\\)")))
505 (match-string 1)))))
506 (docelt (and firstsym
507 (function-get (intern-soft firstsym)
508 lisp-doc-string-elt-property))))
509 (and docelt
510 ;; It's a string in a form that can have a docstring.
511 ;; Check whether it's in docstring position.
512 (save-excursion
513 (when (functionp docelt)
514 (goto-char (match-end 1))
515 (setq docelt (funcall docelt)))
516 (goto-char listbeg)
517 (forward-char 1)
518 (condition-case nil
519 (while (and (> docelt 0) (< (point) startpos)
520 (progn (forward-sexp 1) t))
521 (setq docelt (1- docelt)))
522 (error nil))
523 (and (zerop docelt) (<= (point) startpos)
524 (progn (forward-comment (point-max)) t)
525 (= (point) startpos))))))
527 (defun lisp-string-after-doc-keyword-p (listbeg startpos)
528 "Return true if `:documentation' symbol ends at STARTPOS inside a list.
529 LISTBEG is the position of the start of the innermost list
530 containing STARTPOS."
531 (and listbeg ; We are inside a Lisp form.
532 (save-excursion
533 (goto-char startpos)
534 (ignore-errors
535 (progn (backward-sexp 1)
536 (looking-at ":documentation\\_>"))))))
538 (defun lisp-font-lock-syntactic-face-function (state)
539 "Return syntactic face function for the position represented by STATE.
540 STATE is a `parse-partial-sexp' state, and the returned function is the
541 Lisp font lock syntactic face function."
542 (if (nth 3 state)
543 ;; This might be a (doc)string or a |...| symbol.
544 (let ((startpos (nth 8 state)))
545 (if (eq (char-after startpos) ?|)
546 ;; This is not a string, but a |...| symbol.
548 (let ((listbeg (nth 1 state)))
549 (if (or (lisp-string-in-doc-position-p listbeg startpos)
550 (lisp-string-after-doc-keyword-p listbeg startpos))
551 font-lock-doc-face
552 font-lock-string-face))))
553 font-lock-comment-face))
555 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
556 elisp)
557 "Common initialization routine for lisp modes.
558 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
559 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
560 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
561 font-lock keywords will not be case sensitive."
562 (when lisp-syntax
563 (set-syntax-table lisp-mode-syntax-table))
564 (setq-local paragraph-ignore-fill-prefix t)
565 (setq-local fill-paragraph-function 'lisp-fill-paragraph)
566 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
567 ;; a single docstring. Let's fix it here.
568 (setq-local adaptive-fill-function
569 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
570 ;; Adaptive fill mode gets in the way of auto-fill,
571 ;; and should make no difference for explicit fill
572 ;; because lisp-fill-paragraph should do the job.
573 ;; I believe that newcomment's auto-fill code properly deals with it -stef
574 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
575 (setq-local indent-line-function 'lisp-indent-line)
576 (setq-local outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
577 (setq-local outline-level 'lisp-outline-level)
578 (setq-local add-log-current-defun-function #'lisp-current-defun-name)
579 (setq-local comment-start ";")
580 (setq-local comment-start-skip ";+ *")
581 (setq-local comment-add 1) ;default to `;;' in comment-region
582 (setq-local comment-column 40)
583 (setq-local comment-use-syntax t)
584 (setq-local imenu-generic-expression lisp-imenu-generic-expression)
585 (setq-local multibyte-syntax-as-symbol t)
586 ;; (setq-local syntax-begin-function 'beginning-of-defun) ;;Bug#16247.
587 (setq font-lock-defaults
588 `(,(if elisp '(lisp-el-font-lock-keywords
589 lisp-el-font-lock-keywords-1
590 lisp-el-font-lock-keywords-2)
591 '(lisp-cl-font-lock-keywords
592 lisp-cl-font-lock-keywords-1
593 lisp-cl-font-lock-keywords-2))
594 nil ,keywords-case-insensitive nil nil
595 (font-lock-mark-block-function . mark-defun)
596 (font-lock-extra-managed-props help-echo)
597 (font-lock-syntactic-face-function
598 . lisp-font-lock-syntactic-face-function)))
599 (setq-local prettify-symbols-alist lisp-prettify-symbols-alist)
600 (setq-local electric-pair-skip-whitespace 'chomp)
601 (setq-local electric-pair-open-newline-between-pairs nil))
603 (defun lisp-outline-level ()
604 "Lisp mode `outline-level' function."
605 (let ((len (- (match-end 0) (match-beginning 0))))
606 (if (looking-at "(\\|;;;###autoload")
607 1000
608 len)))
610 (defun lisp-current-defun-name ()
611 "Return the name of the defun at point, or nil."
612 (save-excursion
613 (let ((location (point)))
614 ;; If we are now precisely at the beginning of a defun, make sure
615 ;; beginning-of-defun finds that one rather than the previous one.
616 (or (eobp) (forward-char 1))
617 (beginning-of-defun)
618 ;; Make sure we are really inside the defun found, not after it.
619 (when (and (looking-at "\\s(")
620 (progn (end-of-defun)
621 (< location (point)))
622 (progn (forward-sexp -1)
623 (>= location (point))))
624 (if (looking-at "\\s(")
625 (forward-char 1))
626 ;; Skip the defining construct name, typically "defun" or
627 ;; "defvar".
628 (forward-sexp 1)
629 ;; The second element is usually a symbol being defined. If it
630 ;; is not, use the first symbol in it.
631 (skip-chars-forward " \t\n'(")
632 (buffer-substring-no-properties (point)
633 (progn (forward-sexp 1)
634 (point)))))))
636 (defvar lisp-mode-shared-map
637 (let ((map (make-sparse-keymap)))
638 (set-keymap-parent map prog-mode-map)
639 (define-key map "\e\C-q" 'indent-sexp)
640 (define-key map "\177" 'backward-delete-char-untabify)
641 ;; This gets in the way when viewing a Lisp file in view-mode. As
642 ;; long as [backspace] is mapped into DEL via the
643 ;; function-key-map, this should remain disabled!!
644 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
645 map)
646 "Keymap for commands shared by all sorts of Lisp modes.")
648 (defcustom lisp-mode-hook nil
649 "Hook run when entering Lisp mode."
650 :options '(imenu-add-menubar-index)
651 :type 'hook
652 :group 'lisp)
654 (defcustom lisp-interaction-mode-hook nil
655 "Hook run when entering Lisp Interaction mode."
656 :options '(eldoc-mode)
657 :type 'hook
658 :group 'lisp)
660 ;;; Generic Lisp mode.
662 (defvar lisp-mode-map
663 (let ((map (make-sparse-keymap))
664 (menu-map (make-sparse-keymap "Lisp")))
665 (set-keymap-parent map lisp-mode-shared-map)
666 (define-key map "\e\C-x" 'lisp-eval-defun)
667 (define-key map "\C-c\C-z" 'run-lisp)
668 (bindings--define-key map [menu-bar lisp] (cons "Lisp" menu-map))
669 (bindings--define-key menu-map [run-lisp]
670 '(menu-item "Run inferior Lisp" run-lisp
671 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
672 (bindings--define-key menu-map [ev-def]
673 '(menu-item "Eval defun" lisp-eval-defun
674 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
675 (bindings--define-key menu-map [ind-sexp]
676 '(menu-item "Indent sexp" indent-sexp
677 :help "Indent each line of the list starting just after point"))
678 map)
679 "Keymap for ordinary Lisp mode.
680 All commands in `lisp-mode-shared-map' are inherited by this map.")
682 (define-derived-mode lisp-mode prog-mode "Lisp"
683 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
684 Commands:
685 Delete converts tabs to spaces as it moves back.
686 Blank lines separate paragraphs. Semicolons start comments.
688 \\{lisp-mode-map}
689 Note that `run-lisp' may be used either to start an inferior Lisp job
690 or to switch back to an existing one."
691 (lisp-mode-variables nil t)
692 (setq-local find-tag-default-function 'lisp-find-tag-default)
693 (setq-local comment-start-skip
694 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
695 (setq imenu-case-fold-search t))
697 (defun lisp-find-tag-default ()
698 (let ((default (find-tag-default)))
699 (when (stringp default)
700 (if (string-match ":+" default)
701 (substring default (match-end 0))
702 default))))
704 ;; Used in old LispM code.
705 (defalias 'common-lisp-mode 'lisp-mode)
707 ;; This will do unless inf-lisp.el is loaded.
708 (defun lisp-eval-defun (&optional _and-go)
709 "Send the current defun to the Lisp process made by \\[run-lisp]."
710 (interactive)
711 (error "Process lisp does not exist"))
713 ;; May still be used by some external Lisp-mode variant.
714 (define-obsolete-function-alias 'lisp-comment-indent
715 'comment-indent-default "22.1")
716 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
718 (defcustom lisp-indent-offset nil
719 "If non-nil, indent second line of expressions that many more columns."
720 :group 'lisp
721 :type '(choice (const nil) integer))
722 (put 'lisp-indent-offset 'safe-local-variable
723 (lambda (x) (or (null x) (integerp x))))
725 (defcustom lisp-indent-function 'lisp-indent-function
726 "A function to be called by `calculate-lisp-indent'.
727 It indents the arguments of a Lisp function call. This function
728 should accept two arguments: the indent-point, and the
729 `parse-partial-sexp' state at that position. One option for this
730 function is `common-lisp-indent-function'."
731 :type 'function
732 :group 'lisp)
734 (defun lisp-indent-line (&optional _whole-exp)
735 "Indent current line as Lisp code.
736 With argument, indent any additional lines of the same expression
737 rigidly along with this one."
738 (interactive "P")
739 (let ((indent (calculate-lisp-indent)) shift-amt
740 (pos (- (point-max) (point)))
741 (beg (progn (beginning-of-line) (point))))
742 (skip-chars-forward " \t")
743 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
744 ;; Don't alter indentation of a ;;; comment line
745 ;; or a line that starts in a string.
746 ;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
747 (goto-char (- (point-max) pos))
748 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
749 ;; Single-semicolon comment lines should be indented
750 ;; as comment lines, not as code.
751 (progn (indent-for-comment) (forward-char -1))
752 (if (listp indent) (setq indent (car indent)))
753 (setq shift-amt (- indent (current-column)))
754 (if (zerop shift-amt)
756 (delete-region beg (point))
757 (indent-to indent)))
758 ;; If initial point was within line's indentation,
759 ;; position after the indentation. Else stay at same point in text.
760 (if (> (- (point-max) pos) (point))
761 (goto-char (- (point-max) pos))))))
763 (defvar calculate-lisp-indent-last-sexp)
765 (defun calculate-lisp-indent (&optional parse-start)
766 "Return appropriate indentation for current line as Lisp code.
767 In usual case returns an integer: the column to indent to.
768 If the value is nil, that means don't change the indentation
769 because the line starts inside a string.
771 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
772 This means that following lines at the same level of indentation
773 should not necessarily be indented the same as this line.
774 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
775 is the buffer position of the start of the containing expression."
776 (save-excursion
777 (beginning-of-line)
778 (let ((indent-point (point))
779 state
780 ;; setting this to a number inhibits calling hook
781 (desired-indent nil)
782 (retry t)
783 calculate-lisp-indent-last-sexp containing-sexp)
784 (if parse-start
785 (goto-char parse-start)
786 (beginning-of-defun))
787 ;; Find outermost containing sexp
788 (while (< (point) indent-point)
789 (setq state (parse-partial-sexp (point) indent-point 0)))
790 ;; Find innermost containing sexp
791 (while (and retry
792 state
793 (> (elt state 0) 0))
794 (setq retry nil)
795 (setq calculate-lisp-indent-last-sexp (elt state 2))
796 (setq containing-sexp (elt state 1))
797 ;; Position following last unclosed open.
798 (goto-char (1+ containing-sexp))
799 ;; Is there a complete sexp since then?
800 (if (and calculate-lisp-indent-last-sexp
801 (> calculate-lisp-indent-last-sexp (point)))
802 ;; Yes, but is there a containing sexp after that?
803 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
804 indent-point 0)))
805 (if (setq retry (car (cdr peek))) (setq state peek)))))
806 (if retry
808 ;; Innermost containing sexp found
809 (goto-char (1+ containing-sexp))
810 (if (not calculate-lisp-indent-last-sexp)
811 ;; indent-point immediately follows open paren.
812 ;; Don't call hook.
813 (setq desired-indent (current-column))
814 ;; Find the start of first element of containing sexp.
815 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
816 (cond ((looking-at "\\s(")
817 ;; First element of containing sexp is a list.
818 ;; Indent under that list.
820 ((> (save-excursion (forward-line 1) (point))
821 calculate-lisp-indent-last-sexp)
822 ;; This is the first line to start within the containing sexp.
823 ;; It's almost certainly a function call.
824 (if (= (point) calculate-lisp-indent-last-sexp)
825 ;; Containing sexp has nothing before this line
826 ;; except the first element. Indent under that element.
828 ;; Skip the first element, find start of second (the first
829 ;; argument of the function call) and indent under.
830 (progn (forward-sexp 1)
831 (parse-partial-sexp (point)
832 calculate-lisp-indent-last-sexp
833 0 t)))
834 (backward-prefix-chars))
836 ;; Indent beneath first sexp on same line as
837 ;; `calculate-lisp-indent-last-sexp'. Again, it's
838 ;; almost certainly a function call.
839 (goto-char calculate-lisp-indent-last-sexp)
840 (beginning-of-line)
841 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
842 0 t)
843 (backward-prefix-chars)))))
844 ;; Point is at the point to indent under unless we are inside a string.
845 ;; Call indentation hook except when overridden by lisp-indent-offset
846 ;; or if the desired indentation has already been computed.
847 (let ((normal-indent (current-column)))
848 (cond ((elt state 3)
849 ;; Inside a string, don't change indentation.
850 nil)
851 ((and (integerp lisp-indent-offset) containing-sexp)
852 ;; Indent by constant offset
853 (goto-char containing-sexp)
854 (+ (current-column) lisp-indent-offset))
855 ;; in this case calculate-lisp-indent-last-sexp is not nil
856 (calculate-lisp-indent-last-sexp
858 ;; try to align the parameters of a known function
859 (and lisp-indent-function
860 (not retry)
861 (funcall lisp-indent-function indent-point state))
862 ;; If the function has no special alignment
863 ;; or it does not apply to this argument,
864 ;; try to align a constant-symbol under the last
865 ;; preceding constant symbol, if there is such one of
866 ;; the last 2 preceding symbols, in the previous
867 ;; uncommented line.
868 (and (save-excursion
869 (goto-char indent-point)
870 (skip-chars-forward " \t")
871 (looking-at ":"))
872 ;; The last sexp may not be at the indentation
873 ;; where it begins, so find that one, instead.
874 (save-excursion
875 (goto-char calculate-lisp-indent-last-sexp)
876 ;; Handle prefix characters and whitespace
877 ;; following an open paren. (Bug#1012)
878 (backward-prefix-chars)
879 (while (not (or (looking-back "^[ \t]*\\|([ \t]+"
880 (line-beginning-position))
881 (and containing-sexp
882 (>= (1+ containing-sexp) (point)))))
883 (forward-sexp -1)
884 (backward-prefix-chars))
885 (setq calculate-lisp-indent-last-sexp (point)))
886 (> calculate-lisp-indent-last-sexp
887 (save-excursion
888 (goto-char (1+ containing-sexp))
889 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
890 (point)))
891 (let ((parse-sexp-ignore-comments t)
892 indent)
893 (goto-char calculate-lisp-indent-last-sexp)
894 (or (and (looking-at ":")
895 (setq indent (current-column)))
896 (and (< (line-beginning-position)
897 (prog2 (backward-sexp) (point)))
898 (looking-at ":")
899 (setq indent (current-column))))
900 indent))
901 ;; another symbols or constants not preceded by a constant
902 ;; as defined above.
903 normal-indent))
904 ;; in this case calculate-lisp-indent-last-sexp is nil
905 (desired-indent)
907 normal-indent))))))
909 (defun lisp-indent-function (indent-point state)
910 "This function is the normal value of the variable `lisp-indent-function'.
911 The function `calculate-lisp-indent' calls this to determine
912 if the arguments of a Lisp function call should be indented specially.
914 INDENT-POINT is the position at which the line being indented begins.
915 Point is located at the point to indent under (for default indentation);
916 STATE is the `parse-partial-sexp' state for that position.
918 If the current line is in a call to a Lisp function that has a non-nil
919 property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
920 it specifies how to indent. The property value can be:
922 * `defun', meaning indent `defun'-style
923 (this is also the case if there is no property and the function
924 has a name that begins with \"def\", and three or more arguments);
926 * an integer N, meaning indent the first N arguments specially
927 (like ordinary function arguments), and then indent any further
928 arguments like a body;
930 * a function to call that returns the indentation (or nil).
931 `lisp-indent-function' calls this function with the same two arguments
932 that it itself received.
934 This function returns either the indentation to use, or nil if the
935 Lisp function does not specify a special indentation."
936 (let ((normal-indent (current-column)))
937 (goto-char (1+ (elt state 1)))
938 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
939 (if (and (elt state 2)
940 (not (looking-at "\\sw\\|\\s_")))
941 ;; car of form doesn't seem to be a symbol
942 (progn
943 (if (not (> (save-excursion (forward-line 1) (point))
944 calculate-lisp-indent-last-sexp))
945 (progn (goto-char calculate-lisp-indent-last-sexp)
946 (beginning-of-line)
947 (parse-partial-sexp (point)
948 calculate-lisp-indent-last-sexp 0 t)))
949 ;; Indent under the list or under the first sexp on the same
950 ;; line as calculate-lisp-indent-last-sexp. Note that first
951 ;; thing on that line has to be complete sexp since we are
952 ;; inside the innermost containing sexp.
953 (backward-prefix-chars)
954 (current-column))
955 (let ((function (buffer-substring (point)
956 (progn (forward-sexp 1) (point))))
957 method)
958 (setq method (or (function-get (intern-soft function)
959 'lisp-indent-function)
960 (get (intern-soft function) 'lisp-indent-hook)))
961 (cond ((or (eq method 'defun)
962 (and (null method)
963 (> (length function) 3)
964 (string-match "\\`def" function)))
965 (lisp-indent-defform state indent-point))
966 ((integerp method)
967 (lisp-indent-specform method state
968 indent-point normal-indent))
969 (method
970 (funcall method indent-point state)))))))
972 (defcustom lisp-body-indent 2
973 "Number of columns to indent the second line of a `(def...)' form."
974 :group 'lisp
975 :type 'integer)
976 (put 'lisp-body-indent 'safe-local-variable 'integerp)
978 (defun lisp-indent-specform (count state indent-point normal-indent)
979 (let ((containing-form-start (elt state 1))
980 (i count)
981 body-indent containing-form-column)
982 ;; Move to the start of containing form, calculate indentation
983 ;; to use for non-distinguished forms (> count), and move past the
984 ;; function symbol. lisp-indent-function guarantees that there is at
985 ;; least one word or symbol character following open paren of containing
986 ;; form.
987 (goto-char containing-form-start)
988 (setq containing-form-column (current-column))
989 (setq body-indent (+ lisp-body-indent containing-form-column))
990 (forward-char 1)
991 (forward-sexp 1)
992 ;; Now find the start of the last form.
993 (parse-partial-sexp (point) indent-point 1 t)
994 (while (and (< (point) indent-point)
995 (condition-case ()
996 (progn
997 (setq count (1- count))
998 (forward-sexp 1)
999 (parse-partial-sexp (point) indent-point 1 t))
1000 (error nil))))
1001 ;; Point is sitting on first character of last (or count) sexp.
1002 (if (> count 0)
1003 ;; A distinguished form. If it is the first or second form use double
1004 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1005 ;; to 2 (the default), this just happens to work the same with if as
1006 ;; the older code, but it makes unwind-protect, condition-case,
1007 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1008 ;; less hacked, behavior can be obtained by replacing below with
1009 ;; (list normal-indent containing-form-start).
1010 (if (<= (- i count) 1)
1011 (list (+ containing-form-column (* 2 lisp-body-indent))
1012 containing-form-start)
1013 (list normal-indent containing-form-start))
1014 ;; A non-distinguished form. Use body-indent if there are no
1015 ;; distinguished forms and this is the first undistinguished form,
1016 ;; or if this is the first undistinguished form and the preceding
1017 ;; distinguished form has indentation at least as great as body-indent.
1018 (if (or (and (= i 0) (= count 0))
1019 (and (= count 0) (<= body-indent normal-indent)))
1020 body-indent
1021 normal-indent))))
1023 (defun lisp-indent-defform (state _indent-point)
1024 (goto-char (car (cdr state)))
1025 (forward-line 1)
1026 (if (> (point) (car (cdr (cdr state))))
1027 (progn
1028 (goto-char (car (cdr state)))
1029 (+ lisp-body-indent (current-column)))))
1032 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1033 ;; like defun if the first form is placed on the next line, otherwise
1034 ;; it is indented like any other form (i.e. forms line up under first).
1036 (put 'autoload 'lisp-indent-function 'defun) ;Elisp
1037 (put 'progn 'lisp-indent-function 0)
1038 (put 'prog1 'lisp-indent-function 1)
1039 (put 'prog2 'lisp-indent-function 2)
1040 (put 'save-excursion 'lisp-indent-function 0) ;Elisp
1041 (put 'save-restriction 'lisp-indent-function 0) ;Elisp
1042 (put 'save-current-buffer 'lisp-indent-function 0) ;Elisp
1043 (put 'let 'lisp-indent-function 1)
1044 (put 'let* 'lisp-indent-function 1)
1045 (put 'while 'lisp-indent-function 1)
1046 (put 'if 'lisp-indent-function 2)
1047 (put 'catch 'lisp-indent-function 1)
1048 (put 'condition-case 'lisp-indent-function 2)
1049 (put 'handler-case 'lisp-indent-function 1) ;CL
1050 (put 'handler-bind 'lisp-indent-function 1) ;CL
1051 (put 'unwind-protect 'lisp-indent-function 1)
1052 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1054 (defun indent-sexp (&optional endpos)
1055 "Indent each line of the list starting just after point.
1056 If optional arg ENDPOS is given, indent each line, stopping when
1057 ENDPOS is encountered."
1058 (interactive)
1059 (let ((indent-stack (list nil))
1060 (next-depth 0)
1061 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1062 ;; so that calculate-lisp-indent will find the beginning of
1063 ;; the defun we are in.
1064 ;; If ENDPOS is nil, it is safe not to scan before point
1065 ;; since every line we indent is more deeply nested than point is.
1066 (starting-point (if endpos nil (point)))
1067 (last-point (point))
1068 last-depth bol outer-loop-done inner-loop-done state this-indent)
1069 (or endpos
1070 ;; Get error now if we don't have a complete sexp after point.
1071 (save-excursion (forward-sexp 1)))
1072 (save-excursion
1073 (setq outer-loop-done nil)
1074 (while (if endpos (< (point) endpos)
1075 (not outer-loop-done))
1076 (setq last-depth next-depth
1077 inner-loop-done nil)
1078 ;; Parse this line so we can learn the state
1079 ;; to indent the next line.
1080 ;; This inner loop goes through only once
1081 ;; unless a line ends inside a string.
1082 (while (and (not inner-loop-done)
1083 (not (setq outer-loop-done (eobp))))
1084 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1085 nil nil state))
1086 (setq next-depth (car state))
1087 ;; If the line contains a comment other than the sort
1088 ;; that is indented like code,
1089 ;; indent it now with indent-for-comment.
1090 ;; Comments indented like code are right already.
1091 ;; In any case clear the in-comment flag in the state
1092 ;; because parse-partial-sexp never sees the newlines.
1093 (if (car (nthcdr 4 state))
1094 (progn (indent-for-comment)
1095 (end-of-line)
1096 (setcar (nthcdr 4 state) nil)))
1097 ;; If this line ends inside a string,
1098 ;; go straight to next line, remaining within the inner loop,
1099 ;; and turn off the \-flag.
1100 (if (car (nthcdr 3 state))
1101 (progn
1102 (forward-line 1)
1103 (setcar (nthcdr 5 state) nil))
1104 (setq inner-loop-done t)))
1105 (and endpos
1106 (<= next-depth 0)
1107 (progn
1108 (setq indent-stack (nconc indent-stack
1109 (make-list (- next-depth) nil))
1110 last-depth (- last-depth next-depth)
1111 next-depth 0)))
1112 (forward-line 1)
1113 ;; Decide whether to exit.
1114 (if endpos
1115 ;; If we have already reached the specified end,
1116 ;; give up and do not reindent this line.
1117 (if (<= endpos (point))
1118 (setq outer-loop-done t))
1119 ;; If no specified end, we are done if we have finished one sexp.
1120 (if (<= next-depth 0)
1121 (setq outer-loop-done t)))
1122 (unless outer-loop-done
1123 (while (> last-depth next-depth)
1124 (setq indent-stack (cdr indent-stack)
1125 last-depth (1- last-depth)))
1126 (while (< last-depth next-depth)
1127 (setq indent-stack (cons nil indent-stack)
1128 last-depth (1+ last-depth)))
1129 ;; Now indent the next line according
1130 ;; to what we learned from parsing the previous one.
1131 (setq bol (point))
1132 (skip-chars-forward " \t")
1133 ;; But not if the line is blank, or just a comment
1134 ;; (except for double-semi comments; indent them as usual).
1135 (if (or (eobp) (looking-at "\\s<\\|\n"))
1137 (if (and (car indent-stack)
1138 (>= (car indent-stack) 0))
1139 (setq this-indent (car indent-stack))
1140 (let ((val (calculate-lisp-indent
1141 (if (car indent-stack) (- (car indent-stack))
1142 starting-point))))
1143 (if (null val)
1144 (setq this-indent val)
1145 (if (integerp val)
1146 (setcar indent-stack
1147 (setq this-indent val))
1148 (setcar indent-stack (- (car (cdr val))))
1149 (setq this-indent (car val))))))
1150 (if (and this-indent (/= (current-column) this-indent))
1151 (progn (delete-region bol (point))
1152 (indent-to this-indent)))))
1153 (or outer-loop-done
1154 (setq outer-loop-done (= (point) last-point))
1155 (setq last-point (point)))))))
1157 (defun indent-pp-sexp (&optional arg)
1158 "Indent each line of the list starting just after point, or prettyprint it.
1159 A prefix argument specifies pretty-printing."
1160 (interactive "P")
1161 (if arg
1162 (save-excursion
1163 (save-restriction
1164 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1165 (pp-buffer)
1166 (goto-char (point-max))
1167 (if (eq (char-before) ?\n)
1168 (delete-char -1)))))
1169 (indent-sexp))
1171 ;;;; Lisp paragraph filling commands.
1173 (defcustom emacs-lisp-docstring-fill-column 65
1174 "Value of `fill-column' to use when filling a docstring.
1175 Any non-integer value means do not use a different value of
1176 `fill-column' when filling docstrings."
1177 :type '(choice (integer)
1178 (const :tag "Use the current `fill-column'" t))
1179 :group 'lisp)
1180 (put 'emacs-lisp-docstring-fill-column 'safe-local-variable
1181 (lambda (x) (or (eq x t) (integerp x))))
1183 (defun lisp-fill-paragraph (&optional justify)
1184 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1185 If any of the current line is a comment, fill the comment or the
1186 paragraph of it that point is in, preserving the comment's indentation
1187 and initial semicolons."
1188 (interactive "P")
1189 (or (fill-comment-paragraph justify)
1190 ;; Since fill-comment-paragraph returned nil, that means we're not in
1191 ;; a comment: Point is on a program line; we are interested
1192 ;; particularly in docstring lines.
1194 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1195 ;; are buffer-local, but we avoid changing them so that they can be set
1196 ;; to make `forward-paragraph' and friends do something the user wants.
1198 ;; `paragraph-start': The `(' in the character alternative and the
1199 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1200 ;; sexps and backquoted sexps that follow a docstring from being filled
1201 ;; with the docstring. This setting has the consequence of inhibiting
1202 ;; filling many program lines that are not docstrings, which is sensible,
1203 ;; because the user probably asked to fill program lines by accident, or
1204 ;; expecting indentation (perhaps we should try to do indenting in that
1205 ;; case). The `;' and `:' stop the paragraph being filled at following
1206 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1207 ;; escaped to keep font-locking, filling, & paren matching in the source
1208 ;; file happy.
1210 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1211 ;; a docstring and identifies it as a paragraph separator, so that it
1212 ;; won't be filled. (Since the first line of documentation stands alone
1213 ;; in some contexts, filling should not alter the contents the author has
1214 ;; chosen.) Only the first line of a docstring begins with whitespace
1215 ;; and a quotation mark and ends with a period or (rarely) a comma.
1217 ;; The `fill-column' is temporarily bound to
1218 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1219 (let ((paragraph-start
1220 (concat paragraph-start
1221 (format "\\|\\s-*\\([(;%s\"]\\|`(\\|#'(\\)"
1222 ;; If we're inside a string (like the doc
1223 ;; string), don't consider a colon to be
1224 ;; a paragraph-start character.
1225 (if (nth 3 (syntax-ppss))
1227 ":"))))
1228 (paragraph-separate
1229 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1230 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1231 (derived-mode-p 'emacs-lisp-mode))
1232 emacs-lisp-docstring-fill-column
1233 fill-column)))
1234 (fill-paragraph justify))
1235 ;; Never return nil.
1238 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1239 "Indent all lines of code, starting in the region, sideways by ARG columns.
1240 Does not affect lines starting inside comments or strings, assuming that
1241 the start of the region is not inside them.
1243 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1244 The last is a regexp which, if matched at the beginning of a line,
1245 means don't indent that line."
1246 (interactive "r\np")
1247 (let (state)
1248 (save-excursion
1249 (goto-char end)
1250 (setq end (point-marker))
1251 (goto-char start)
1252 (or (bolp)
1253 (setq state (parse-partial-sexp (point)
1254 (progn
1255 (forward-line 1) (point))
1256 nil nil state)))
1257 (while (< (point) end)
1258 (or (car (nthcdr 3 state))
1259 (and nochange-regexp
1260 (looking-at nochange-regexp))
1261 ;; If line does not start in string, indent it
1262 (let ((indent (current-indentation)))
1263 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1264 (or (eolp)
1265 (indent-to (max 0 (+ indent arg)) 0))))
1266 (setq state (parse-partial-sexp (point)
1267 (progn
1268 (forward-line 1) (point))
1269 nil nil state))))))
1271 (provide 'lisp-mode)
1273 ;;; lisp-mode.el ends here