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