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