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