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