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