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