Merge branch 'master' into comment-cache
[emacs.git] / lisp / emacs-lisp / cl-indent.el
blob33ecf3f454202adde48cc61d06cb88714703faa0
1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987, 2000-2017 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 <http://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))
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 (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 (last-point indent-point)
319 ;; the position of the open-paren of the innermost containing list
320 (containing-form-start (elt state 1))
321 ;; the column of the above
322 sexp-column)
323 ;; Move to start of innermost containing list
324 (goto-char containing-form-start)
325 (setq sexp-column (current-column))
327 ;; Look over successively less-deep containing forms
328 (while (and (not calculated)
329 (< depth lisp-indent-maximum-backtracking))
330 (let ((containing-sexp (point)))
331 (forward-char 1)
332 (parse-partial-sexp (point) indent-point 1 t)
333 ;; Move to the car of the relevant containing form
334 (let (tem function method tentative-defun)
335 (if (not (looking-at "\\sw\\|\\s_"))
336 ;; This form doesn't seem to start with a symbol
337 (setq function nil method nil)
338 (setq tem (point))
339 (forward-sexp 1)
340 (setq function (downcase (buffer-substring-no-properties
341 tem (point))))
342 (goto-char tem)
343 ;; Elisp generally provides CL functionality with a CL
344 ;; prefix, so if we have a special indenter for the
345 ;; unprefixed version, prefer it over whatever's defined
346 ;; for the cl- version. Users can override this
347 ;; heuristic by defining a
348 ;; common-lisp-indent-function-for-elisp property on the
349 ;; cl- version.
350 (when (and (derived-mode-p 'emacs-lisp-mode)
351 (not (lisp-indent-find-method
352 (intern-soft function) t))
353 (string-match "\\`cl-" function)
354 (setf tem (intern-soft
355 (substring function (match-end 0))))
356 (lisp-indent-find-method tem t))
357 (setf function (symbol-name tem)))
358 (setq tem (intern-soft function)
359 method (lisp-indent-find-method tem))
360 ;; The pleblisp package feature
361 (when (and (null tem)
362 (string-match ":[^:]+" function))
363 (setq function (substring function (1+ (match-beginning 0)))
364 tem (intern-soft function)
365 method (lisp-indent-find-method tem))))
366 (let ((n 0))
367 ;; How far into the containing form is the current form?
368 (if (< (point) indent-point)
369 (while (condition-case ()
370 (progn
371 (forward-sexp 1)
372 (if (>= (point) indent-point)
374 (parse-partial-sexp (point)
375 indent-point 1 t)
376 (setq n (1+ n))
378 (error nil))))
379 (setq path (cons n path)))
381 ;; backwards compatibility.
382 (cond ((null function))
383 ((null method)
384 (when (null (cdr path))
385 ;; (package prefix was stripped off above)
386 (cond ((string-match "\\`def"
387 function)
388 (setq tentative-defun t))
389 ((string-match
390 (eval-when-compile
391 (concat "\\`\\("
392 (regexp-opt '("with" "without" "do"))
393 "\\)-"))
394 function)
395 (setq method '(&lambda &body))))))
396 ;; backwards compatibility. Bletch.
397 ((eq method 'defun)
398 (setq method lisp-indent-defun-method)))
400 (cond ((and (or (eq (char-after (1- containing-sexp)) ?\')
401 (and (not lisp-backquote-indentation)
402 (eq (char-after (1- containing-sexp)) ?\`)))
403 (not (eq (char-after (- containing-sexp 2)) ?\#)))
404 ;; No indentation for "'(...)" elements
405 (setq calculated (1+ sexp-column)))
406 ((when
407 (or (eq (char-after (1- containing-sexp)) ?\,)
408 (and (eq (char-after (1- containing-sexp)) ?\@)
409 (eq (char-after (- containing-sexp 2)) ?\,)))
410 ;; ",(...)" or ",@(...)"
411 (when (eq lisp-indent-backquote-substitution-mode
412 'corrected)
413 (incf sexp-column -1)
414 (when (eq (char-after (1- containing-sexp)) ?\@)
415 (incf sexp-column -1)))
416 (cond (lisp-indent-backquote-substitution-mode
417 (setf tentative-calculated normal-indent)
418 (setq depth lisp-indent-maximum-backtracking)
419 nil)
420 (t (setq calculated normal-indent)))))
421 ((eq (char-after (1- containing-sexp)) ?\#)
422 ;; "#(...)"
423 (setq calculated (1+ sexp-column)))
424 ((null method)
425 ;; If this looks like a call to a `def...' form,
426 ;; think about indenting it as one, but do it
427 ;; tentatively for cases like
428 ;; (flet ((defunp ()
429 ;; nil)))
430 ;; Set both normal-indent and tentative-calculated.
431 ;; The latter ensures this value gets used
432 ;; if there are no relevant containing constructs.
433 ;; The former ensures this value gets used
434 ;; if there is a relevant containing construct
435 ;; but we are nested within the structure levels
436 ;; that it specifies indentation for.
437 (if tentative-defun
438 (setq tentative-calculated
439 (common-lisp-indent-call-method
440 function lisp-indent-defun-method
441 path state indent-point
442 sexp-column normal-indent)
443 normal-indent tentative-calculated)))
444 ((integerp method)
445 ;; convenient top-level hack.
446 ;; (also compatible with lisp-indent-function)
447 ;; The number specifies how many `distinguished'
448 ;; forms there are before the body starts
449 ;; Equivalent to (4 4 ... &body)
450 (setq calculated (cond ((cdr path)
451 normal-indent)
452 ((<= (car path) method)
453 ;; `distinguished' form
454 (list (+ sexp-column 4)
455 containing-form-start))
456 ((= (car path) (1+ method))
457 ;; first body form.
458 (+ sexp-column lisp-body-indent))
460 ;; other body form
461 normal-indent))))
463 (setq calculated
464 (common-lisp-indent-call-method
465 function method path state indent-point
466 sexp-column normal-indent)))))
467 (goto-char containing-sexp)
468 (setq last-point containing-sexp)
469 (unless calculated
470 (condition-case ()
471 (progn (backward-up-list 1)
472 (setq depth (1+ depth)))
473 (error (setq depth lisp-indent-maximum-backtracking))))))
474 (or calculated tentative-calculated))))
477 (defun common-lisp-indent-call-method (function method path state indent-point
478 sexp-column normal-indent)
479 (let ((lisp-indent-error-function function))
480 (if (symbolp method)
481 (funcall method
482 path state indent-point
483 sexp-column normal-indent)
484 (lisp-indent-259 method path state indent-point
485 sexp-column normal-indent))))
487 ;; Dynamically bound in common-lisp-indent-call-method.
488 (defvar lisp-indent-error-function)
490 (defun lisp-indent-report-bad-format (m)
491 (error "%s has a badly-formed %s property: %s"
492 ;; Love those free variable references!!
493 lisp-indent-error-function 'common-lisp-indent-function m))
496 ;; Lambda-list indentation is now done in LISP-INDENT-LAMBDA-LIST.
497 ;; See also `lisp-lambda-list-keyword-alignment',
498 ;; `lisp-lambda-list-keyword-parameter-alignment' and
499 ;; `lisp-lambda-list-keyword-parameter-indentation' -- dvl
501 (defvar lisp-indent-lambda-list-keywords-regexp
502 "&\\(\
503 optional\\|rest\\|key\\|allow-other-keys\\|aux\\|whole\\|body\\|environment\
504 \\)\\([ \t]\\|$\\)"
505 "Regular expression matching lambda-list keywords.")
507 (defun lisp-indent-lambda-list
508 (indent-point sexp-column containing-form-start)
509 (let (limit)
510 (cond ((save-excursion
511 (goto-char indent-point)
512 (beginning-of-line)
513 (skip-chars-forward " \t")
514 (setq limit (point))
515 (looking-at lisp-indent-lambda-list-keywords-regexp))
516 ;; We're facing a lambda-list keyword.
517 (if lisp-lambda-list-keyword-alignment
518 ;; Align to the first keyword if any, or to the beginning of
519 ;; the lambda-list.
520 (save-excursion
521 (goto-char containing-form-start)
522 (save-match-data
523 (if (re-search-forward
524 lisp-indent-lambda-list-keywords-regexp
525 limit t)
526 (progn
527 (goto-char (match-beginning 0))
528 (current-column))
529 (1+ sexp-column))))
530 ;; Align to the beginning of the lambda-list.
531 (1+ sexp-column)))
533 ;; Otherwise, align to the first argument of the last lambda-list
534 ;; keyword, the keyword itself, or the beginning of the
535 ;; lambda-list.
536 (save-excursion
537 (goto-char indent-point)
538 (forward-line -1)
539 (end-of-line)
540 (save-match-data
541 (if (re-search-backward lisp-indent-lambda-list-keywords-regexp
542 containing-form-start t)
543 (let* ((keyword-posn
544 (progn
545 (goto-char (match-beginning 0))
546 (current-column)))
547 (indented-keyword-posn
548 (+ keyword-posn
549 lisp-lambda-list-keyword-parameter-indentation)))
550 (goto-char (match-end 0))
551 (skip-chars-forward " \t")
552 (if (eolp)
553 indented-keyword-posn
554 (if lisp-lambda-list-keyword-parameter-alignment
555 (current-column)
556 indented-keyword-posn)))
557 (1+ sexp-column))))))))
559 ;; Blame the crufty control structure on dynamic scoping
560 ;; -- not on me!
561 (defun lisp-indent-259
562 (method path state indent-point sexp-column normal-indent)
563 (catch 'exit
564 (let ((p path)
565 (containing-form-start (elt state 1))
566 n tem tail)
567 ;; Isn't tail-recursion wonderful?
568 (while p
569 ;; This while loop is for destructuring.
570 ;; p is set to (cdr p) each iteration.
571 (if (not (consp method)) (lisp-indent-report-bad-format method))
572 (setq n (1- (car p))
573 p (cdr p)
574 tail nil)
575 (while n
576 ;; This while loop is for advancing along a method
577 ;; until the relevant (possibly &rest/&body) pattern
578 ;; is reached.
579 ;; n is set to (1- n) and method to (cdr method)
580 ;; each iteration.
581 (setq tem (car method))
583 (or (eq tem 'nil) ;default indentation
584 (eq tem '&lambda) ;lambda list
585 (and (eq tem '&body) (null (cdr method)))
586 (and (eq tem '&rest)
587 (consp (cdr method))
588 (null (cddr method)))
589 (integerp tem) ;explicit indentation specified
590 (and (consp tem) ;destructuring
591 (eq (car tem) '&whole)
592 (or (symbolp (cadr tem))
593 (integerp (cadr tem))))
594 (and (symbolp tem) ;a function to call to do the work.
595 (null (cdr method)))
596 (lisp-indent-report-bad-format method))
598 (cond ((and tail (not (consp tem)))
599 ;; indent tail of &rest in same way as first elt of rest
600 (throw 'exit normal-indent))
601 ((eq tem '&body)
602 ;; &body means (&rest <lisp-body-indent>)
603 (throw 'exit
604 (if (and (= n 0) ;first body form
605 (null p)) ;not in subforms
606 (+ sexp-column
607 lisp-body-indent)
608 normal-indent)))
609 ((eq tem '&rest)
610 ;; this pattern holds for all remaining forms
611 (setq tail (> n 0)
613 method (cdr method)))
614 ((> n 0)
615 ;; try next element of pattern
616 (setq n (1- n)
617 method (cdr method))
618 (if (< n 0)
619 ;; Too few elements in pattern.
620 (throw 'exit normal-indent)))
621 ((eq tem 'nil)
622 (throw 'exit (if (consp normal-indent)
623 normal-indent
624 (list normal-indent containing-form-start))))
625 ((eq tem '&lambda)
626 (throw 'exit
627 (cond ((null p)
628 (list (+ sexp-column 4) containing-form-start))
629 ((null (cdr p))
630 ;; Indentation within a lambda-list. -- dvl
631 (list (lisp-indent-lambda-list
632 indent-point
633 sexp-column
634 containing-form-start)
635 containing-form-start))
637 normal-indent))))
638 ((integerp tem)
639 (throw 'exit
640 (if (null p) ;not in subforms
641 (list (+ sexp-column tem) containing-form-start)
642 normal-indent)))
643 ((symbolp tem) ;a function to call
644 (throw 'exit
645 (funcall tem path state indent-point
646 sexp-column normal-indent)))
648 ;; must be a destructing frob
649 (if (not (null p))
650 ;; descend
651 (setq method (cddr tem)
652 n nil)
653 (setq tem (cadr tem))
654 (throw 'exit
655 (cond (tail
656 normal-indent)
657 ((eq tem 'nil)
658 (list normal-indent
659 containing-form-start))
660 ((integerp tem)
661 (list (+ sexp-column tem)
662 containing-form-start))
664 (funcall tem path state indent-point
665 sexp-column normal-indent))))))))))))
667 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
668 (if (not (null (cdr path)))
669 normal-indent
670 (save-excursion
671 (goto-char indent-point)
672 (beginning-of-line)
673 (skip-chars-forward " \t")
674 (list (cond ((looking-at "\\sw\\|\\s_")
675 ;; a tagbody tag
676 (+ sexp-column lisp-tag-indentation))
677 ((integerp lisp-tag-body-indentation)
678 (+ sexp-column lisp-tag-body-indentation))
679 ((eq lisp-tag-body-indentation 't)
680 (condition-case ()
681 (progn (backward-sexp 1) (current-column))
682 (error (1+ sexp-column))))
683 (t (+ sexp-column lisp-body-indent)))
684 ; (cond ((integerp lisp-tag-body-indentation)
685 ; (+ sexp-column lisp-tag-body-indentation))
686 ; ((eq lisp-tag-body-indentation 't)
687 ; normal-indent)
688 ; (t
689 ; (+ sexp-column lisp-body-indent)))
690 (elt state 1)
691 ))))
693 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
694 (if (>= (car path) 3)
695 (let ((lisp-tag-body-indentation lisp-body-indent))
696 (funcall (function lisp-indent-tagbody)
697 path state indent-point sexp-column normal-indent))
698 (funcall (function lisp-indent-259)
699 '((&whole nil &rest
700 ;; the following causes weird indentation
701 ;;(&whole 1 1 2 nil)
703 (&whole nil &rest 1))
704 path state indent-point sexp-column normal-indent)))
707 ;; LISP-INDENT-DEFMETHOD now supports the presence of more than one method
708 ;; qualifier and indents the method's lambda list properly. -- dvl
709 (defun lisp-indent-defmethod
710 (path state indent-point sexp-column normal-indent)
711 (lisp-indent-259
712 (let ((nqual 0))
713 (if (and (>= (car path) 3)
714 (save-excursion
715 (beginning-of-defun)
716 (forward-char 1)
717 (forward-sexp 2)
718 (skip-chars-forward " \t\n")
719 (while (looking-at "\\sw\\|\\s_")
720 (incf nqual)
721 (forward-sexp)
722 (skip-chars-forward " \t\n"))
723 (> nqual 0)))
724 (append '(4) (make-list nqual 4) '(&lambda &body))
725 (get 'defun 'common-lisp-indent-function)))
726 path state indent-point sexp-column normal-indent))
729 (defun lisp-indent-function-lambda-hack (path state indent-point
730 sexp-column normal-indent)
731 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
732 (if (or (cdr path) ; wtf?
733 (> (car path) 3))
734 ;; line up under previous body form
735 normal-indent
736 ;; line up under function rather than under lambda in order to
737 ;; conserve horizontal space. (Which is what #' is for.)
738 (condition-case ()
739 (save-excursion
740 (backward-up-list 2)
741 (forward-char 1)
742 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
743 (+ lisp-body-indent -1 (current-column))
744 (+ sexp-column lisp-body-indent)))
745 (error (+ sexp-column lisp-body-indent)))))
749 (let ((l '((block 1)
750 (case (4 &rest (&whole 2 &rest 1)))
751 (ccase . case)
752 (ecase . case)
753 (typecase . case)
754 (etypecase . case)
755 (ctypecase . case)
756 (catch 1)
757 (cond (&rest (&whole 2 &rest 1)))
758 (defvar (4 2 2))
759 (defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1)))
760 (defconstant . defvar)
761 (defcustom (4 2 2 2))
762 (defparameter . defvar)
763 (defconst . defcustom)
764 (define-condition . defclass)
765 (define-modify-macro (4 &lambda &body))
766 (defsetf (4 &lambda 4 &body))
767 (defun (4 &lambda &body))
768 (defgeneric (4 &lambda &body))
769 (define-setf-method . defun)
770 (define-setf-expander . defun)
771 (defmacro . defun)
772 (defsubst . defun)
773 (deftype . defun)
774 (defmethod lisp-indent-defmethod)
775 (defpackage (4 2))
776 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
777 &rest (&whole 2 &rest 1)))
778 (destructuring-bind
779 ((&whole 6 &rest 1) 4 &body))
780 (do lisp-indent-do)
781 (do* . do)
782 (dolist ((&whole 4 2 1) &body))
783 (dotimes . dolist)
784 (eval-when 1)
785 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
786 (labels . flet)
787 (macrolet . flet)
788 (generic-flet . flet)
789 (generic-labels . flet)
790 (handler-case (4 &rest (&whole 2 &lambda &body)))
791 (restart-case . handler-case)
792 ;; `else-body' style
793 (if (nil nil &body))
794 ;; single-else style (then and else equally indented)
795 (if (&rest nil))
796 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
797 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
798 (let* . let)
799 (compiler-let . let) ;barf
800 (handler-bind . let)
801 (restart-bind . let)
802 (locally 1)
803 ;(loop lisp-indent-loop)
804 (:method (&lambda &body)) ; in `defgeneric'
805 (multiple-value-bind ((&whole 6 &rest 1) 4 &body))
806 (multiple-value-call (4 &body))
807 (multiple-value-prog1 1)
808 (multiple-value-setq (4 2))
809 (multiple-value-setf . multiple-value-setq)
810 (pprint-logical-block (4 2))
811 (print-unreadable-object ((&whole 4 1 &rest 1) &body))
812 ;; Combines the worst features of BLOCK, LET and TAGBODY
813 (prog (&lambda &rest lisp-indent-tagbody))
814 (prog* . prog)
815 (prog1 1)
816 (prog2 2)
817 (progn 0)
818 (progv (4 4 &body))
819 (return 0)
820 (return-from (nil &body))
821 (symbol-macrolet . let)
822 (tagbody lisp-indent-tagbody)
823 (throw 1)
824 (unless 1)
825 (unwind-protect (5 &body))
826 (when 1)
827 (with-accessors . multiple-value-bind)
828 (with-condition-restarts . multiple-value-bind)
829 (with-compilation-unit (&lambda &body))
830 (with-output-to-string (4 2))
831 (with-slots . multiple-value-bind)
832 (with-standard-io-syntax (2)))))
833 (dolist (el l)
834 (put (car el) 'common-lisp-indent-function
835 (if (symbolp (cdr el))
836 (get (cdr el) 'common-lisp-indent-function)
837 (car (cdr el))))))
839 ;; In elisp, the else part of `if' is in an implicit progn, so indent
840 ;; it more.
841 (put 'if 'common-lisp-indent-function-for-elisp 2)
842 (put 'with-output-to-string 'common-lisp-indent-function-for-elisp 0)
845 ;(defun foo (x)
846 ; (tagbody
847 ; foo
848 ; (bar)
849 ; baz
850 ; (when (losing)
851 ; (with-big-loser
852 ; (yow)
853 ; ((lambda ()
854 ; foo)
855 ; big)))
856 ; (flet ((foo (bar baz zap)
857 ; (zip))
858 ; (zot ()
859 ; quux))
860 ; (do ()
861 ; ((lose)
862 ; (foo 1))
863 ; (quux)
864 ; foo
865 ; (lose))
866 ; (cond ((x)
867 ; (win 1 2
868 ; (foo)))
869 ; (t
870 ; (lose
871 ; 3))))))
874 ;(put 'while 'common-lisp-indent-function 1)
875 ;(put 'defwrapper'common-lisp-indent-function ...)
876 ;(put 'def 'common-lisp-indent-function ...)
877 ;(put 'defflavor 'common-lisp-indent-function ...)
878 ;(put 'defsubst 'common-lisp-indent-function ...)
880 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
881 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
882 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
883 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
884 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
885 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
886 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
888 (provide 'cl-indent)
890 ;;; cl-indent.el ends here