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