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