Document reserved keys
[emacs.git] / lisp / emacs-lisp / cl-indent.el
blob73b91725cc8334594b59ebfff6654767446499e2
1 ;;; cl-indent.el --- Enhanced lisp-indent mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 1987, 2000-2018 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
6 ;; Created: July 1987
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: lisp, tools
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package supplies a single entry point, common-lisp-indent-function,
29 ;; which performs indentation in the preferred style for Common Lisp code.
30 ;; It is also a suitable function for indenting Emacs lisp code.
32 ;; To enable it:
34 ;; (setq lisp-indent-function 'common-lisp-indent-function)
36 ;;; Code:
38 (eval-when-compile (require 'cl-lib))
40 (defgroup lisp-indent nil
41 "Indentation in Lisp."
42 :group 'lisp)
45 (defcustom lisp-indent-maximum-backtracking 3
46 "Maximum depth to backtrack out from a sublist for structured indentation.
47 If this variable is 0, no backtracking will occur and forms such as `flet'
48 may not be correctly indented."
49 :type 'integer
50 :group 'lisp-indent)
52 (defcustom lisp-tag-indentation 1
53 "Indentation of tags relative to containing list.
54 This variable is used by the function `lisp-indent-tagbody'."
55 :type 'integer
56 :group 'lisp-indent)
58 (defcustom lisp-tag-body-indentation 3
59 "Indentation of non-tagged lines relative to containing list.
60 This variable is used by the function `lisp-indent-tagbody' to indent normal
61 lines (lines without tags).
62 The indentation is relative to the indentation of the parenthesis enclosing
63 the special form. If the value is t, the body of tags will be indented
64 as a block at the same indentation as the first s-expression following
65 the tag. In this case, any forms before the first tag are indented
66 by `lisp-body-indent'."
67 :type 'integer
68 :group 'lisp-indent)
70 (defcustom lisp-backquote-indentation t
71 "Whether or not to indent backquoted lists as code.
72 If nil, indent backquoted lists as data, i.e., like quoted lists."
73 :type 'boolean
74 :group 'lisp-indent)
77 (defcustom lisp-loop-keyword-indentation 3
78 "Indentation of loop keywords in extended loop forms."
79 :type 'integer
80 :group 'lisp-indent)
83 (defcustom lisp-loop-forms-indentation 5
84 "Indentation of forms in extended loop forms."
85 :type 'integer
86 :group 'lisp-indent)
89 (defcustom lisp-simple-loop-indentation 3
90 "Indentation of forms in simple loop forms."
91 :type 'integer
92 :group 'lisp-indent)
94 (defcustom lisp-lambda-list-keyword-alignment nil
95 "Whether to vertically align lambda-list keywords together.
96 If nil (the default), keyworded lambda-list parts are aligned
97 with the initial mandatory arguments, like this:
99 \(defun foo (arg1 arg2 &rest rest
100 &key key1 key2)
101 #|...|#)
103 If non-nil, alignment is done with the first keyword
104 \(or falls back to the previous case), as in:
106 \(defun foo (arg1 arg2 &rest rest
107 &key key1 key2)
108 #|...|#)"
109 :version "24.1"
110 :type 'boolean
111 :group 'lisp-indent)
113 (defcustom lisp-lambda-list-keyword-parameter-indentation 2
114 "Indentation of lambda list keyword parameters.
115 See `lisp-lambda-list-keyword-parameter-alignment'
116 for more information."
117 :version "24.1"
118 :type 'integer
119 :group 'lisp-indent)
121 (defcustom lisp-lambda-list-keyword-parameter-alignment nil
122 "Whether to vertically align lambda-list keyword parameters together.
123 If nil (the default), the parameters are aligned
124 with their corresponding keyword, plus the value of
125 `lisp-lambda-list-keyword-parameter-indentation', like this:
127 \(defun foo (arg1 arg2 &key key1 key2
128 key3 key4)
129 #|...|#)
131 If non-nil, alignment is done with the first parameter
132 \(or falls back to the previous case), as in:
134 \(defun foo (arg1 arg2 &key key1 key2
135 key3 key4)
136 #|...|#)"
137 :version "24.1"
138 :type 'boolean
139 :group 'lisp-indent)
141 (defcustom lisp-indent-backquote-substitution-mode t
142 "How to indent substitutions in backquotes.
143 If t, the default, indent substituted forms normally.
144 If nil, do not apply special indentation rule to substituted
145 forms. If `corrected', subtract the `,' or `,@' from the form
146 column, indenting as if this character sequence were not present.
147 In any case, do not backtrack beyond a backquote substitution.
149 Until Emacs 25.1, the nil behavior was hard-wired."
150 :version "25.1"
151 :type '(choice (const corrected) (const nil) (const t))
152 :group 'lisp-indent)
155 (defvar lisp-indent-defun-method '(4 &lambda &body)
156 "Defun-like indentation method.
157 This applies when the value of the `common-lisp-indent-function' property
158 is set to `defun'.")
161 (defun lisp-extended-loop-p (loop-start)
162 "True if an extended loop form starts at LOOP-START."
163 (condition-case ()
164 (save-excursion
165 (goto-char loop-start)
166 (forward-char 1)
167 (forward-sexp 2)
168 (backward-sexp 1)
169 (looking-at "\\(:\\|\\sw\\)"))
170 (error t)))
172 (defun lisp-indent-find-method (symbol &optional no-compat)
173 "Find the lisp indentation function for SYMBOL.
174 If NO-COMPAT is non-nil, do not retrieve indenters intended for
175 the standard lisp indent package."
176 (or (and (derived-mode-p 'emacs-lisp-mode)
177 (get symbol 'common-lisp-indent-function-for-elisp))
178 (get symbol 'common-lisp-indent-function)
179 (and (not no-compat)
180 (get symbol 'lisp-indent-function))))
182 (defun common-lisp-loop-part-indentation (indent-point state)
183 "Compute the indentation of loop form constituents."
184 (let* ((loop-indentation (save-excursion
185 (goto-char (elt state 1))
186 (current-column))))
187 (when (and (eq lisp-indent-backquote-substitution-mode 'corrected))
188 (save-excursion
189 (goto-char (elt state 1))
190 (cl-incf loop-indentation
191 (cond ((eq (char-before) ?,) -1)
192 ((and (eq (char-before) ?@)
193 (progn (backward-char)
194 (eq (char-before) ?,)))
196 (t 0)))))
198 (goto-char indent-point)
199 (beginning-of-line)
200 (list
201 (cond ((not (lisp-extended-loop-p (elt state 1)))
202 (+ loop-indentation lisp-simple-loop-indentation))
203 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
204 (+ loop-indentation lisp-loop-keyword-indentation))
206 (+ loop-indentation lisp-loop-forms-indentation)))
207 ;; Tell the caller that the next line needs recomputation, even
208 ;; though it doesn't start a sexp.
209 loop-indentation)))
212 ;; Cf (info "(elisp)Specification List")
213 ;;;###autoload
214 (defun common-lisp-indent-function (indent-point state)
215 "Function to indent the arguments of a Lisp function call.
216 This is suitable for use as the value of the variable
217 `lisp-indent-function'. INDENT-POINT is the point at which the
218 indentation function is called, and STATE is the
219 `parse-partial-sexp' state at that position. Browse the
220 `lisp-indent' customize group for options affecting the behavior
221 of this function.
223 If the indentation point is in a call to a Lisp function, that
224 function's `common-lisp-indent-function' property specifies how
225 this function should indent it. Possible values for this
226 property are:
228 * defun, meaning indent according to `lisp-indent-defun-method';
229 i.e., like (4 &lambda &body), as explained below.
231 * any other symbol, meaning a function to call. The function should
232 take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT.
233 PATH is a list of integers describing the position of point in terms of
234 list-structure with respect to the containing lists. For example, in
235 ((a b c (d foo) f) g), foo has a path of (0 3 1). In other words,
236 to reach foo take the 0th element of the outermost list, then
237 the 3rd element of the next list, and finally the 1st element.
238 STATE and INDENT-POINT are as in the arguments to
239 `common-lisp-indent-function'. SEXP-COLUMN is the column of
240 the open parenthesis of the innermost containing list.
241 NORMAL-INDENT is the column the indentation point was
242 originally in. This function should behave like `lisp-indent-259'.
244 * an integer N, meaning indent the first N arguments like
245 function arguments, and any further arguments like a body.
246 This is equivalent to (4 4 ... &body).
248 * a list. The list element in position M specifies how to indent the Mth
249 function argument. If there are fewer elements than function arguments,
250 the last list element applies to all remaining arguments. The accepted
251 list elements are:
253 * nil, meaning the default indentation.
255 * an integer, specifying an explicit indentation.
257 * &lambda. Indent the argument (which may be a list) by 4.
259 * &rest. When used, this must be the penultimate element. The
260 element after this one applies to all remaining arguments.
262 * &body. This is equivalent to &rest lisp-body-indent, i.e., indent
263 all remaining elements by `lisp-body-indent'.
265 * &whole. This must be followed by nil, an integer, or a
266 function symbol. This indentation is applied to the
267 associated argument, and as a base indent for all remaining
268 arguments. For example, an integer P means indent this
269 argument by P, and all remaining arguments by P, plus the
270 value specified by their associated list element.
272 * a symbol. A function to call, with the 6 arguments specified above.
274 * a list, with elements as described above. This applies when the
275 associated function argument is itself a list. Each element of the list
276 specifies how to indent the associated argument.
278 For example, the function `case' has an indent property
279 \(4 &rest (&whole 2 &rest 1)), meaning:
280 * indent the first argument by 4.
281 * arguments after the first should be lists, and there may be any number
282 of them. The first list element has an offset of 2, all the rest
283 have an offset of 2+1=3.
285 If the current mode is actually `emacs-lisp-mode', look for a
286 `common-lisp-indent-function-for-elisp' property before looking
287 at `common-lisp-indent-function' and, if set, use its value
288 instead."
289 ;; FIXME: why do we need to special-case loop?
290 (if (save-excursion (goto-char (elt state 1))
291 (and (looking-at (if (derived-mode-p 'emacs-lisp-mode)
292 "(\\(cl-\\)?loop"
293 "([Ll][Oo][Oo][Pp]"))
294 (or lisp-indent-backquote-substitution-mode
295 (not
296 (or (and (eq (char-before) ?@)
297 (progn (backward-char)
298 (eq (char-before) ?,)))
299 (eq (char-before) ?,))))))
300 (common-lisp-loop-part-indentation indent-point state)
301 (common-lisp-indent-function-1 indent-point state)))
304 (defun common-lisp-indent-function-1 (indent-point state)
305 (let ((normal-indent (current-column)))
306 ;; Walk up list levels until we see something
307 ;; which does special things with subforms.
308 (let ((depth 0)
309 ;; Path describes the position of point in terms of
310 ;; list-structure with respect to containing lists.
311 ;; `foo' has a path of (0 3 1) in `((a b c (d foo) f) g)'.
312 (path ())
313 ;; set non-nil when somebody works out the indentation to use
314 calculated
315 ;; If non-nil, this is an indentation to use
316 ;; if nothing else specifies it more firmly.
317 tentative-calculated
318 ;; the position of the open-paren of the innermost containing list
319 (containing-form-start (elt state 1))
320 ;; the column of the above
321 sexp-column)
322 ;; Move to start of innermost containing list
323 (goto-char containing-form-start)
324 (setq sexp-column (current-column))
326 ;; Look over successively less-deep containing forms
327 (while (and (not calculated)
328 (< depth lisp-indent-maximum-backtracking))
329 (let ((containing-sexp (point)))
330 (forward-char 1)
331 (parse-partial-sexp (point) indent-point 1 t)
332 ;; Move to the car of the relevant containing form
333 (let (tem function method tentative-defun)
334 (if (not (looking-at "\\sw\\|\\s_"))
335 ;; This form doesn't seem to start with a symbol
336 (setq function nil method nil)
337 (setq tem (point))
338 (forward-sexp 1)
339 (setq function (downcase (buffer-substring-no-properties
340 tem (point))))
341 (goto-char tem)
342 ;; Elisp generally provides CL functionality with a CL
343 ;; prefix, so if we have a special indenter for the
344 ;; unprefixed version, prefer it over whatever's defined
345 ;; for the cl- version. Users can override this
346 ;; heuristic by defining a
347 ;; common-lisp-indent-function-for-elisp property on the
348 ;; cl- version.
349 (when (and (derived-mode-p 'emacs-lisp-mode)
350 (not (lisp-indent-find-method
351 (intern-soft function) t))
352 (string-match "\\`cl-" function)
353 (setf tem (intern-soft
354 (substring function (match-end 0))))
355 (lisp-indent-find-method tem t))
356 (setf function (symbol-name tem)))
357 (setq tem (intern-soft function)
358 method (lisp-indent-find-method tem))
359 ;; The pleblisp package feature
360 (when (and (null tem)
361 (string-match ":[^:]+" function))
362 (setq function (substring function (1+ (match-beginning 0)))
363 tem (intern-soft function)
364 method (lisp-indent-find-method tem))))
365 (let ((n 0))
366 ;; How far into the containing form is the current form?
367 (if (< (point) indent-point)
368 (while (condition-case ()
369 (progn
370 (forward-sexp 1)
371 (if (>= (point) indent-point)
373 (parse-partial-sexp (point)
374 indent-point 1 t)
375 (setq n (1+ n))
377 (error nil))))
378 (setq path (cons n path)))
380 ;; backwards compatibility.
381 (cond ((null function))
382 ((null method)
383 (when (null (cdr path))
384 ;; (package prefix was stripped off above)
385 (cond ((string-match "\\`def"
386 function)
387 (setq tentative-defun t))
388 ((string-match
389 (eval-when-compile
390 (concat "\\`\\("
391 (regexp-opt '("with" "without" "do"))
392 "\\)-"))
393 function)
394 (setq method '(&lambda &body))))))
395 ;; backwards compatibility. Bletch.
396 ((eq method 'defun)
397 (setq method lisp-indent-defun-method)))
399 (cond ((and (or (eq (char-after (1- containing-sexp)) ?\')
400 (and (not lisp-backquote-indentation)
401 (eq (char-after (1- containing-sexp)) ?\`)))
402 (not (eq (char-after (- containing-sexp 2)) ?\#)))
403 ;; No indentation for "'(...)" elements
404 (setq calculated (1+ sexp-column)))
405 ((when
406 (or (eq (char-after (1- containing-sexp)) ?\,)
407 (and (eq (char-after (1- containing-sexp)) ?\@)
408 (eq (char-after (- containing-sexp 2)) ?\,)))
409 ;; ",(...)" or ",@(...)"
410 (when (eq lisp-indent-backquote-substitution-mode
411 'corrected)
412 (cl-incf sexp-column -1)
413 (when (eq (char-after (1- containing-sexp)) ?\@)
414 (cl-incf sexp-column -1)))
415 (cond (lisp-indent-backquote-substitution-mode
416 (setf tentative-calculated normal-indent)
417 (setq depth lisp-indent-maximum-backtracking)
418 nil)
419 (t (setq calculated normal-indent)))))
420 ((eq (char-after (1- containing-sexp)) ?\#)
421 ;; "#(...)"
422 (setq calculated (1+ sexp-column)))
423 ((null method)
424 ;; If this looks like a call to a `def...' form,
425 ;; think about indenting it as one, but do it
426 ;; tentatively for cases like
427 ;; (flet ((defunp ()
428 ;; nil)))
429 ;; Set both normal-indent and tentative-calculated.
430 ;; The latter ensures this value gets used
431 ;; if there are no relevant containing constructs.
432 ;; The former ensures this value gets used
433 ;; if there is a relevant containing construct
434 ;; but we are nested within the structure levels
435 ;; that it specifies indentation for.
436 (if tentative-defun
437 (setq tentative-calculated
438 (common-lisp-indent-call-method
439 function lisp-indent-defun-method
440 path state indent-point
441 sexp-column normal-indent)
442 normal-indent tentative-calculated)))
443 ((integerp method)
444 ;; convenient top-level hack.
445 ;; (also compatible with lisp-indent-function)
446 ;; The number specifies how many `distinguished'
447 ;; forms there are before the body starts
448 ;; Equivalent to (4 4 ... &body)
449 (setq calculated (cond ((cdr path)
450 normal-indent)
451 ((<= (car path) method)
452 ;; `distinguished' form
453 (list (+ sexp-column 4)
454 containing-form-start))
455 ((= (car path) (1+ method))
456 ;; first body form.
457 (+ sexp-column lisp-body-indent))
459 ;; other body form
460 normal-indent))))
462 (setq calculated
463 (common-lisp-indent-call-method
464 function method path state indent-point
465 sexp-column normal-indent)))))
466 (goto-char containing-sexp)
467 (unless calculated
468 (condition-case ()
469 (progn (backward-up-list 1)
470 (setq depth (1+ depth)))
471 (error (setq depth lisp-indent-maximum-backtracking))))))
472 (or calculated tentative-calculated))))
475 ;; Dynamically bound in common-lisp-indent-call-method.
476 (defvar lisp-indent-error-function)
478 (defun common-lisp-indent-call-method (function method path state indent-point
479 sexp-column normal-indent)
480 (let ((lisp-indent-error-function function))
481 (if (symbolp method)
482 (funcall method
483 path state indent-point
484 sexp-column normal-indent)
485 (lisp-indent-259 method path state indent-point
486 sexp-column normal-indent))))
488 (defun lisp-indent-report-bad-format (m)
489 (error "%s has a badly-formed %s property: %s"
490 ;; Love those free variable references!!
491 lisp-indent-error-function 'common-lisp-indent-function m))
494 ;; Lambda-list indentation is now done in LISP-INDENT-LAMBDA-LIST.
495 ;; See also `lisp-lambda-list-keyword-alignment',
496 ;; `lisp-lambda-list-keyword-parameter-alignment' and
497 ;; `lisp-lambda-list-keyword-parameter-indentation' -- dvl
499 (defvar lisp-indent-lambda-list-keywords-regexp
500 "&\\(\
501 optional\\|rest\\|key\\|allow-other-keys\\|aux\\|whole\\|body\\|environment\
502 \\)\\([ \t]\\|$\\)"
503 "Regular expression matching lambda-list keywords.")
505 (defun lisp-indent-lambda-list
506 (indent-point sexp-column containing-form-start)
507 (let (limit)
508 (cond ((save-excursion
509 (goto-char indent-point)
510 (beginning-of-line)
511 (skip-chars-forward " \t")
512 (setq limit (point))
513 (looking-at lisp-indent-lambda-list-keywords-regexp))
514 ;; We're facing a lambda-list keyword.
515 (if lisp-lambda-list-keyword-alignment
516 ;; Align to the first keyword if any, or to the beginning of
517 ;; the lambda-list.
518 (save-excursion
519 (goto-char containing-form-start)
520 (save-match-data
521 (if (re-search-forward
522 lisp-indent-lambda-list-keywords-regexp
523 limit t)
524 (progn
525 (goto-char (match-beginning 0))
526 (current-column))
527 (1+ sexp-column))))
528 ;; Align to the beginning of the lambda-list.
529 (1+ sexp-column)))
531 ;; Otherwise, align to the first argument of the last lambda-list
532 ;; keyword, the keyword itself, or the beginning of the
533 ;; lambda-list.
534 (save-excursion
535 (goto-char indent-point)
536 (forward-line -1)
537 (end-of-line)
538 (save-match-data
539 (if (re-search-backward lisp-indent-lambda-list-keywords-regexp
540 containing-form-start t)
541 (let* ((keyword-posn
542 (progn
543 (goto-char (match-beginning 0))
544 (current-column)))
545 (indented-keyword-posn
546 (+ keyword-posn
547 lisp-lambda-list-keyword-parameter-indentation)))
548 (goto-char (match-end 0))
549 (skip-chars-forward " \t")
550 (if (eolp)
551 indented-keyword-posn
552 (if lisp-lambda-list-keyword-parameter-alignment
553 (current-column)
554 indented-keyword-posn)))
555 (1+ sexp-column))))))))
557 ;; Blame the crufty control structure on dynamic scoping
558 ;; -- not on me!
559 (defun lisp-indent-259
560 (method path state indent-point sexp-column normal-indent)
561 (catch 'exit
562 (let ((p path)
563 (containing-form-start (elt state 1))
564 n tem tail)
565 ;; Isn't tail-recursion wonderful?
566 (while p
567 ;; This while loop is for destructuring.
568 ;; p is set to (cdr p) each iteration.
569 (if (not (consp method)) (lisp-indent-report-bad-format method))
570 (setq n (1- (car p))
571 p (cdr p)
572 tail nil)
573 (while n
574 ;; This while loop is for advancing along a method
575 ;; until the relevant (possibly &rest/&body) pattern
576 ;; is reached.
577 ;; n is set to (1- n) and method to (cdr method)
578 ;; each iteration.
579 (setq tem (car method))
581 (or (eq tem 'nil) ;default indentation
582 (eq tem '&lambda) ;lambda list
583 (and (eq tem '&body) (null (cdr method)))
584 (and (eq tem '&rest)
585 (consp (cdr method))
586 (null (cddr method)))
587 (integerp tem) ;explicit indentation specified
588 (and (consp tem) ;destructuring
589 (eq (car tem) '&whole)
590 (or (symbolp (cadr tem))
591 (integerp (cadr tem))))
592 (and (symbolp tem) ;a function to call to do the work.
593 (null (cdr method)))
594 (lisp-indent-report-bad-format method))
596 (cond ((and tail (not (consp tem)))
597 ;; indent tail of &rest in same way as first elt of rest
598 (throw 'exit normal-indent))
599 ((eq tem '&body)
600 ;; &body means (&rest <lisp-body-indent>)
601 (throw 'exit
602 (if (and (= n 0) ;first body form
603 (null p)) ;not in subforms
604 (+ sexp-column
605 lisp-body-indent)
606 normal-indent)))
607 ((eq tem '&rest)
608 ;; this pattern holds for all remaining forms
609 (setq tail (> n 0)
611 method (cdr method)))
612 ((> n 0)
613 ;; try next element of pattern
614 (setq n (1- n)
615 method (cdr method))
616 (if (< n 0)
617 ;; Too few elements in pattern.
618 (throw 'exit normal-indent)))
619 ((eq tem 'nil)
620 (throw 'exit (if (consp normal-indent)
621 normal-indent
622 (list normal-indent containing-form-start))))
623 ((eq tem '&lambda)
624 (throw 'exit
625 (cond ((null p)
626 (list (+ sexp-column 4) containing-form-start))
627 ((null (cdr p))
628 ;; Indentation within a lambda-list. -- dvl
629 (list (lisp-indent-lambda-list
630 indent-point
631 sexp-column
632 containing-form-start)
633 containing-form-start))
635 normal-indent))))
636 ((integerp tem)
637 (throw 'exit
638 (if (null p) ;not in subforms
639 (list (+ sexp-column tem) containing-form-start)
640 normal-indent)))
641 ((symbolp tem) ;a function to call
642 (throw 'exit
643 (funcall tem path state indent-point
644 sexp-column normal-indent)))
646 ;; must be a destructing frob
647 (if (not (null p))
648 ;; descend
649 (setq method (cddr tem)
650 n nil)
651 (setq tem (cadr tem))
652 (throw 'exit
653 (cond (tail
654 normal-indent)
655 ((eq tem 'nil)
656 (list normal-indent
657 containing-form-start))
658 ((integerp tem)
659 (list (+ sexp-column tem)
660 containing-form-start))
662 (funcall tem path state indent-point
663 sexp-column normal-indent))))))))))))
665 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
666 (if (not (null (cdr path)))
667 normal-indent
668 (save-excursion
669 (goto-char indent-point)
670 (beginning-of-line)
671 (skip-chars-forward " \t")
672 (list (cond ((looking-at "\\sw\\|\\s_")
673 ;; a tagbody tag
674 (+ sexp-column lisp-tag-indentation))
675 ((integerp lisp-tag-body-indentation)
676 (+ sexp-column lisp-tag-body-indentation))
677 ((eq lisp-tag-body-indentation 't)
678 (condition-case ()
679 (progn (backward-sexp 1) (current-column))
680 (error (1+ sexp-column))))
681 (t (+ sexp-column lisp-body-indent)))
682 ; (cond ((integerp lisp-tag-body-indentation)
683 ; (+ sexp-column lisp-tag-body-indentation))
684 ; ((eq lisp-tag-body-indentation 't)
685 ; normal-indent)
686 ; (t
687 ; (+ sexp-column lisp-body-indent)))
688 (elt state 1)
689 ))))
691 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
692 (if (>= (car path) 3)
693 (let ((lisp-tag-body-indentation lisp-body-indent))
694 (funcall (function lisp-indent-tagbody)
695 path state indent-point sexp-column normal-indent))
696 (funcall (function lisp-indent-259)
697 '((&whole nil &rest
698 ;; the following causes weird indentation
699 ;;(&whole 1 1 2 nil)
701 (&whole nil &rest 1))
702 path state indent-point sexp-column normal-indent)))
705 ;; LISP-INDENT-DEFMETHOD now supports the presence of more than one method
706 ;; qualifier and indents the method's lambda list properly. -- dvl
707 (defun lisp-indent-defmethod
708 (path state indent-point sexp-column normal-indent)
709 (lisp-indent-259
710 (let ((nqual 0))
711 (if (and (>= (car path) 3)
712 (save-excursion
713 (beginning-of-defun)
714 (forward-char 1)
715 (forward-sexp 2)
716 (skip-chars-forward " \t\n")
717 (while (looking-at "\\sw\\|\\s_")
718 (cl-incf nqual)
719 (forward-sexp)
720 (skip-chars-forward " \t\n"))
721 (> nqual 0)))
722 (append '(4) (make-list nqual 4) '(&lambda &body))
723 (get 'defun 'common-lisp-indent-function)))
724 path state indent-point sexp-column normal-indent))
727 (defun lisp-indent-function-lambda-hack (path _state _indent-point
728 sexp-column normal-indent)
729 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
730 (if (or (cdr path) ; wtf?
731 (> (car path) 3))
732 ;; line up under previous body form
733 normal-indent
734 ;; line up under function rather than under lambda in order to
735 ;; conserve horizontal space. (Which is what #' is for.)
736 (condition-case ()
737 (save-excursion
738 (backward-up-list 2)
739 (forward-char 1)
740 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
741 (+ lisp-body-indent -1 (current-column))
742 (+ sexp-column lisp-body-indent)))
743 (error (+ sexp-column lisp-body-indent)))))
747 (let ((l '((block 1)
748 (case (4 &rest (&whole 2 &rest 1)))
749 (ccase . case)
750 (ecase . case)
751 (typecase . case)
752 (etypecase . case)
753 (ctypecase . case)
754 (catch 1)
755 (cond (&rest (&whole 2 &rest 1)))
756 (defvar (4 2 2))
757 (defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1)))
758 (defconstant . defvar)
759 (defcustom (4 2 2 2))
760 (defparameter . defvar)
761 (defconst . defcustom)
762 (define-condition . defclass)
763 (define-modify-macro (4 &lambda &body))
764 (defsetf (4 &lambda 4 &body))
765 (defun (4 &lambda &body))
766 (defgeneric (4 &lambda &body))
767 (define-setf-method . defun)
768 (define-setf-expander . defun)
769 (defmacro . defun)
770 (defsubst . defun)
771 (deftype . defun)
772 (defmethod lisp-indent-defmethod)
773 (defpackage (4 2))
774 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
775 &rest (&whole 2 &rest 1)))
776 (destructuring-bind
777 ((&whole 6 &rest 1) 4 &body))
778 (do lisp-indent-do)
779 (do* . do)
780 (dolist ((&whole 4 2 1) &body))
781 (dotimes . dolist)
782 (eval-when 1)
783 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
784 (labels . flet)
785 (macrolet . flet)
786 (generic-flet . flet)
787 (generic-labels . flet)
788 (handler-case (4 &rest (&whole 2 &lambda &body)))
789 (restart-case . handler-case)
790 ;; `else-body' style
791 (if (nil nil &body))
792 ;; single-else style (then and else equally indented)
793 (if (&rest nil))
794 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
795 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
796 (let* . let)
797 (compiler-let . let) ;barf
798 (handler-bind . let)
799 (restart-bind . let)
800 (locally 1)
801 ;(loop lisp-indent-loop)
802 (:method (&lambda &body)) ; in `defgeneric'
803 (multiple-value-bind ((&whole 6 &rest 1) 4 &body))
804 (multiple-value-call (4 &body))
805 (multiple-value-prog1 1)
806 (multiple-value-setq (4 2))
807 (multiple-value-setf . multiple-value-setq)
808 (pprint-logical-block (4 2))
809 (print-unreadable-object ((&whole 4 1 &rest 1) &body))
810 ;; Combines the worst features of BLOCK, LET and TAGBODY
811 (prog (&lambda &rest lisp-indent-tagbody))
812 (prog* . prog)
813 (prog1 1)
814 (prog2 2)
815 (progn 0)
816 (progv (4 4 &body))
817 (return 0)
818 (return-from (nil &body))
819 (symbol-macrolet . let)
820 (tagbody lisp-indent-tagbody)
821 (throw 1)
822 (unless 1)
823 (unwind-protect (5 &body))
824 (when 1)
825 (with-accessors . multiple-value-bind)
826 (with-condition-restarts . multiple-value-bind)
827 (with-compilation-unit (&lambda &body))
828 (with-output-to-string (4 2))
829 (with-slots . multiple-value-bind)
830 (with-standard-io-syntax (2)))))
831 (dolist (el l)
832 (put (car el) 'common-lisp-indent-function
833 (if (symbolp (cdr el))
834 (get (cdr el) 'common-lisp-indent-function)
835 (car (cdr el))))))
837 ;; In elisp, the else part of `if' is in an implicit progn, so indent
838 ;; it more.
839 (put 'if 'common-lisp-indent-function-for-elisp 2)
840 (put 'with-output-to-string 'common-lisp-indent-function-for-elisp 0)
843 ;(defun foo (x)
844 ; (tagbody
845 ; foo
846 ; (bar)
847 ; baz
848 ; (when (losing)
849 ; (with-big-loser
850 ; (yow)
851 ; ((lambda ()
852 ; foo)
853 ; big)))
854 ; (flet ((foo (bar baz zap)
855 ; (zip))
856 ; (zot ()
857 ; quux))
858 ; (do ()
859 ; ((lose)
860 ; (foo 1))
861 ; (quux)
862 ; foo
863 ; (lose))
864 ; (cond ((x)
865 ; (win 1 2
866 ; (foo)))
867 ; (t
868 ; (lose
869 ; 3))))))
872 ;(put 'while 'common-lisp-indent-function 1)
873 ;(put 'defwrapper'common-lisp-indent-function ...)
874 ;(put 'def 'common-lisp-indent-function ...)
875 ;(put 'defflavor 'common-lisp-indent-function ...)
876 ;(put 'defsubst 'common-lisp-indent-function ...)
878 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
879 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
880 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
881 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
882 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
883 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
884 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
886 (provide 'cl-indent)
888 ;;; cl-indent.el ends here