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