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