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