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