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