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