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