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