Shrink EIEIO object header. Move generics to eieio-generic.el.
[emacs.git] / lisp / emacs-lisp / cl-indent.el
blob1bcfb6df2cf920a0f0559eec3df954b4e36029bd
1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987, 2000-2015 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)
142 (defvar lisp-indent-defun-method '(4 &lambda &body)
143 "Defun-like indentation method.
144 This applies when the value of the `common-lisp-indent-function' property
145 is set to `defun'.")
148 (defun extended-loop-p (loop-start)
149 "True if an extended loop form starts at LOOP-START."
150 (condition-case ()
151 (save-excursion
152 (goto-char loop-start)
153 (forward-char 1)
154 (forward-sexp 2)
155 (backward-sexp 1)
156 (looking-at "\\sw"))
157 (error t)))
159 (defun lisp-indent-find-method (symbol &optional no-compat)
160 "Find the lisp indentation function for SYMBOL.
161 If NO-COMPAT is non-nil, do not retrieve indenters intended for
162 the standard lisp indent package."
163 (or (and (derived-mode-p 'emacs-lisp-mode)
164 (get symbol 'common-lisp-indent-function-for-elisp))
165 (get symbol 'common-lisp-indent-function)
166 (and (not no-compat)
167 (get symbol 'lisp-indent-function))))
169 (defun common-lisp-loop-part-indentation (indent-point state)
170 "Compute the indentation of loop form constituents."
171 (let* ((loop-indentation (save-excursion
172 (goto-char (elt state 1))
173 (current-column))))
174 (goto-char indent-point)
175 (beginning-of-line)
176 (list
177 (cond ((not (extended-loop-p (elt state 1)))
178 (+ loop-indentation lisp-simple-loop-indentation))
179 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
180 (+ loop-indentation lisp-loop-keyword-indentation))
182 (+ loop-indentation lisp-loop-forms-indentation)))
183 ;; Tell the caller that the next line needs recomputation, even
184 ;; though it doesn't start a sexp.
185 loop-indentation)))
188 ;; Cf (info "(elisp)Specification List")
189 ;;;###autoload
190 (defun common-lisp-indent-function (indent-point state)
191 "Function to indent the arguments of a Lisp function call.
192 This is suitable for use as the value of the variable
193 `lisp-indent-function'. INDENT-POINT is the point at which the
194 indentation function is called, and STATE is the
195 `parse-partial-sexp' state at that position. Browse the
196 `lisp-indent' customize group for options affecting the behavior
197 of this function.
199 If the indentation point is in a call to a Lisp function, that
200 function's `common-lisp-indent-function' property specifies how
201 this function should indent it. Possible values for this
202 property are:
204 * defun, meaning indent according to `lisp-indent-defun-method';
205 i.e., like (4 &lambda &body), as explained below.
207 * any other symbol, meaning a function to call. The function should
208 take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT.
209 PATH is a list of integers describing the position of point in terms of
210 list-structure with respect to the containing lists. For example, in
211 ((a b c (d foo) f) g), foo has a path of (0 3 1). In other words,
212 to reach foo take the 0th element of the outermost list, then
213 the 3rd element of the next list, and finally the 1st element.
214 STATE and INDENT-POINT are as in the arguments to
215 `common-lisp-indent-function'. SEXP-COLUMN is the column of
216 the open parenthesis of the innermost containing list.
217 NORMAL-INDENT is the column the indentation point was
218 originally in. This function should behave like `lisp-indent-259'.
220 * an integer N, meaning indent the first N arguments like
221 function arguments, and any further arguments like a body.
222 This is equivalent to (4 4 ... &body).
224 * a list. The list element in position M specifies how to indent the Mth
225 function argument. If there are fewer elements than function arguments,
226 the last list element applies to all remaining arguments. The accepted
227 list elements are:
229 * nil, meaning the default indentation.
231 * an integer, specifying an explicit indentation.
233 * &lambda. Indent the argument (which may be a list) by 4.
235 * &rest. When used, this must be the penultimate element. The
236 element after this one applies to all remaining arguments.
238 * &body. This is equivalent to &rest lisp-body-indent, i.e., indent
239 all remaining elements by `lisp-body-indent'.
241 * &whole. This must be followed by nil, an integer, or a
242 function symbol. This indentation is applied to the
243 associated argument, and as a base indent for all remaining
244 arguments. For example, an integer P means indent this
245 argument by P, and all remaining arguments by P, plus the
246 value specified by their associated list element.
248 * a symbol. A function to call, with the 6 arguments specified above.
250 * a list, with elements as described above. This applies when the
251 associated function argument is itself a list. Each element of the list
252 specifies how to indent the associated argument.
254 For example, the function `case' has an indent property
255 \(4 &rest (&whole 2 &rest 1)), meaning:
256 * indent the first argument by 4.
257 * arguments after the first should be lists, and there may be any number
258 of them. The first list element has an offset of 2, all the rest
259 have an offset of 2+1=3.
261 If the current mode is actually `emacs-lisp-mode', look for a
262 `common-lisp-indent-function-for-elisp' property before looking
263 at `common-lisp-indent-function' and, if set, use its value
264 instead."
265 ;; FIXME: why do we need to special-case loop?
266 (if (save-excursion (goto-char (elt state 1))
267 (looking-at (if (derived-mode-p 'emacs-lisp-mode)
268 "(\\(cl-\\)?[Ll][Oo][Oo][Pp]"
269 "([Ll][Oo][Oo][Pp]")))
270 (common-lisp-loop-part-indentation indent-point state)
271 (common-lisp-indent-function-1 indent-point state)))
274 (defun common-lisp-indent-function-1 (indent-point state)
275 (let ((normal-indent (current-column)))
276 ;; Walk up list levels until we see something
277 ;; which does special things with subforms.
278 (let ((depth 0)
279 ;; Path describes the position of point in terms of
280 ;; list-structure with respect to containing lists.
281 ;; `foo' has a path of (0 3 1) in `((a b c (d foo) f) g)'.
282 (path ())
283 ;; set non-nil when somebody works out the indentation to use
284 calculated
285 ;; If non-nil, this is an indentation to use
286 ;; if nothing else specifies it more firmly.
287 tentative-calculated
288 (last-point indent-point)
289 ;; the position of the open-paren of the innermost containing list
290 (containing-form-start (elt state 1))
291 ;; the column of the above
292 sexp-column)
293 ;; Move to start of innermost containing list
294 (goto-char containing-form-start)
295 (setq sexp-column (current-column))
297 ;; Look over successively less-deep containing forms
298 (while (and (not calculated)
299 (< depth lisp-indent-maximum-backtracking))
300 (let ((containing-sexp (point)))
301 (forward-char 1)
302 (parse-partial-sexp (point) indent-point 1 t)
303 ;; Move to the car of the relevant containing form
304 (let (tem function method tentative-defun)
305 (if (not (looking-at "\\sw\\|\\s_"))
306 ;; This form doesn't seem to start with a symbol
307 (setq function nil method nil)
308 (setq tem (point))
309 (forward-sexp 1)
310 (setq function (downcase (buffer-substring-no-properties
311 tem (point))))
312 (goto-char tem)
313 ;; Elisp generally provides CL functionality with a CL
314 ;; prefix, so if we have a special indenter for the
315 ;; unprefixed version, prefer it over whatever's defined
316 ;; for the cl- version. Users can override this
317 ;; heuristic by defining a
318 ;; common-lisp-indent-function-for-elisp property on the
319 ;; cl- version.
320 (when (and (derived-mode-p 'emacs-lisp-mode)
321 (not (lisp-indent-find-method
322 (intern-soft function) t))
323 (string-match "\\`cl-" function)
324 (setf tem (intern-soft
325 (substring function (match-end 0))))
326 (lisp-indent-find-method tem t))
327 (setf function (symbol-name tem)))
328 (setq tem (intern-soft function)
329 method (lisp-indent-find-method tem))
330 ;; The pleblisp package feature
331 (when (and (null tem)
332 (string-match ":[^:]+" function))
333 (setq function (substring function (1+ (match-beginning 0)))
334 tem (intern-soft function)
335 method (lisp-indent-find-method tem))))
336 (let ((n 0))
337 ;; How far into the containing form is the current form?
338 (if (< (point) indent-point)
339 (while (condition-case ()
340 (progn
341 (forward-sexp 1)
342 (if (>= (point) indent-point)
344 (parse-partial-sexp (point)
345 indent-point 1 t)
346 (setq n (1+ n))
348 (error nil))))
349 (setq path (cons n path)))
351 ;; backwards compatibility.
352 (cond ((null function))
353 ((null method)
354 (when (null (cdr path))
355 ;; (package prefix was stripped off above)
356 (cond ((string-match "\\`def"
357 function)
358 (setq tentative-defun t))
359 ((string-match
360 (eval-when-compile
361 (concat "\\`\\("
362 (regexp-opt '("with" "without" "do"))
363 "\\)-"))
364 function)
365 (setq method '(&lambda &body))))))
366 ;; backwards compatibility. Bletch.
367 ((eq method 'defun)
368 (setq method lisp-indent-defun-method)))
370 (cond ((and (or (eq (char-after (1- containing-sexp)) ?\')
371 (and (not lisp-backquote-indentation)
372 (eq (char-after (1- containing-sexp)) ?\`)))
373 (not (eq (char-after (- containing-sexp 2)) ?\#)))
374 ;; No indentation for "'(...)" elements
375 (setq calculated (1+ sexp-column)))
376 ((or (eq (char-after (1- containing-sexp)) ?\,)
377 (and (eq (char-after (1- containing-sexp)) ?\@)
378 (eq (char-after (- containing-sexp 2)) ?\,)))
379 ;; ",(...)" or ",@(...)"
380 (setq calculated normal-indent))
381 ((eq (char-after (1- containing-sexp)) ?\#)
382 ;; "#(...)"
383 (setq calculated (1+ sexp-column)))
384 ((null method)
385 ;; If this looks like a call to a `def...' form,
386 ;; think about indenting it as one, but do it
387 ;; tentatively for cases like
388 ;; (flet ((defunp ()
389 ;; nil)))
390 ;; Set both normal-indent and tentative-calculated.
391 ;; The latter ensures this value gets used
392 ;; if there are no relevant containing constructs.
393 ;; The former ensures this value gets used
394 ;; if there is a relevant containing construct
395 ;; but we are nested within the structure levels
396 ;; that it specifies indentation for.
397 (if tentative-defun
398 (setq tentative-calculated
399 (common-lisp-indent-call-method
400 function lisp-indent-defun-method
401 path state indent-point
402 sexp-column normal-indent)
403 normal-indent tentative-calculated)))
404 ((integerp method)
405 ;; convenient top-level hack.
406 ;; (also compatible with lisp-indent-function)
407 ;; The number specifies how many `distinguished'
408 ;; forms there are before the body starts
409 ;; Equivalent to (4 4 ... &body)
410 (setq calculated (cond ((cdr path)
411 normal-indent)
412 ((<= (car path) method)
413 ;; `distinguished' form
414 (list (+ sexp-column 4)
415 containing-form-start))
416 ((= (car path) (1+ method))
417 ;; first body form.
418 (+ sexp-column lisp-body-indent))
420 ;; other body form
421 normal-indent))))
423 (setq calculated
424 (common-lisp-indent-call-method
425 function method path state indent-point
426 sexp-column normal-indent)))))
427 (goto-char containing-sexp)
428 (setq last-point containing-sexp)
429 (unless calculated
430 (condition-case ()
431 (progn (backward-up-list 1)
432 (setq depth (1+ depth)))
433 (error (setq depth lisp-indent-maximum-backtracking))))))
434 (or calculated tentative-calculated))))
437 (defun common-lisp-indent-call-method (function method path state indent-point
438 sexp-column normal-indent)
439 (let ((lisp-indent-error-function function))
440 (if (symbolp method)
441 (funcall method
442 path state indent-point
443 sexp-column normal-indent)
444 (lisp-indent-259 method path state indent-point
445 sexp-column normal-indent))))
447 ;; Dynamically bound in common-lisp-indent-call-method.
448 (defvar lisp-indent-error-function)
450 (defun lisp-indent-report-bad-format (m)
451 (error "%s has a badly-formed %s property: %s"
452 ;; Love those free variable references!!
453 lisp-indent-error-function 'common-lisp-indent-function m))
456 ;; Lambda-list indentation is now done in LISP-INDENT-LAMBDA-LIST.
457 ;; See also `lisp-lambda-list-keyword-alignment',
458 ;; `lisp-lambda-list-keyword-parameter-alignment' and
459 ;; `lisp-lambda-list-keyword-parameter-indentation' -- dvl
461 (defvar lisp-indent-lambda-list-keywords-regexp
462 "&\\(\
463 optional\\|rest\\|key\\|allow-other-keys\\|aux\\|whole\\|body\\|environment\
464 \\)\\([ \t]\\|$\\)"
465 "Regular expression matching lambda-list keywords.")
467 (defun lisp-indent-lambda-list
468 (indent-point sexp-column containing-form-start)
469 (let (limit)
470 (cond ((save-excursion
471 (goto-char indent-point)
472 (beginning-of-line)
473 (skip-chars-forward " \t")
474 (setq limit (point))
475 (looking-at lisp-indent-lambda-list-keywords-regexp))
476 ;; We're facing a lambda-list keyword.
477 (if lisp-lambda-list-keyword-alignment
478 ;; Align to the first keyword if any, or to the beginning of
479 ;; the lambda-list.
480 (save-excursion
481 (goto-char containing-form-start)
482 (save-match-data
483 (if (re-search-forward
484 lisp-indent-lambda-list-keywords-regexp
485 limit t)
486 (progn
487 (goto-char (match-beginning 0))
488 (current-column))
489 (1+ sexp-column))))
490 ;; Align to the beginning of the lambda-list.
491 (1+ sexp-column)))
493 ;; Otherwise, align to the first argument of the last lambda-list
494 ;; keyword, the keyword itself, or the beginning of the
495 ;; lambda-list.
496 (save-excursion
497 (goto-char indent-point)
498 (forward-line -1)
499 (end-of-line)
500 (save-match-data
501 (if (re-search-backward lisp-indent-lambda-list-keywords-regexp
502 containing-form-start t)
503 (let* ((keyword-posn
504 (progn
505 (goto-char (match-beginning 0))
506 (current-column)))
507 (indented-keyword-posn
508 (+ keyword-posn
509 lisp-lambda-list-keyword-parameter-indentation)))
510 (goto-char (match-end 0))
511 (skip-chars-forward " \t")
512 (if (eolp)
513 indented-keyword-posn
514 (if lisp-lambda-list-keyword-parameter-alignment
515 (current-column)
516 indented-keyword-posn)))
517 (1+ sexp-column))))))))
519 ;; Blame the crufty control structure on dynamic scoping
520 ;; -- not on me!
521 (defun lisp-indent-259
522 (method path state indent-point sexp-column normal-indent)
523 (catch 'exit
524 (let ((p path)
525 (containing-form-start (elt state 1))
526 n tem tail)
527 ;; Isn't tail-recursion wonderful?
528 (while p
529 ;; This while loop is for destructuring.
530 ;; p is set to (cdr p) each iteration.
531 (if (not (consp method)) (lisp-indent-report-bad-format method))
532 (setq n (1- (car p))
533 p (cdr p)
534 tail nil)
535 (while n
536 ;; This while loop is for advancing along a method
537 ;; until the relevant (possibly &rest/&body) pattern
538 ;; is reached.
539 ;; n is set to (1- n) and method to (cdr method)
540 ;; each iteration.
541 (setq tem (car method))
543 (or (eq tem 'nil) ;default indentation
544 (eq tem '&lambda) ;lambda list
545 (and (eq tem '&body) (null (cdr method)))
546 (and (eq tem '&rest)
547 (consp (cdr method))
548 (null (cddr method)))
549 (integerp tem) ;explicit indentation specified
550 (and (consp tem) ;destructuring
551 (eq (car tem) '&whole)
552 (or (symbolp (cadr tem))
553 (integerp (cadr tem))))
554 (and (symbolp tem) ;a function to call to do the work.
555 (null (cdr method)))
556 (lisp-indent-report-bad-format method))
558 (cond ((and tail (not (consp tem)))
559 ;; indent tail of &rest in same way as first elt of rest
560 (throw 'exit normal-indent))
561 ((eq tem '&body)
562 ;; &body means (&rest <lisp-body-indent>)
563 (throw 'exit
564 (if (and (= n 0) ;first body form
565 (null p)) ;not in subforms
566 (+ sexp-column
567 lisp-body-indent)
568 normal-indent)))
569 ((eq tem '&rest)
570 ;; this pattern holds for all remaining forms
571 (setq tail (> n 0)
573 method (cdr method)))
574 ((> n 0)
575 ;; try next element of pattern
576 (setq n (1- n)
577 method (cdr method))
578 (if (< n 0)
579 ;; Too few elements in pattern.
580 (throw 'exit normal-indent)))
581 ((eq tem 'nil)
582 (throw 'exit (if (consp normal-indent)
583 normal-indent
584 (list normal-indent containing-form-start))))
585 ((eq tem '&lambda)
586 (throw 'exit
587 (cond ((null p)
588 (list (+ sexp-column 4) containing-form-start))
589 ((null (cdr p))
590 ;; Indentation within a lambda-list. -- dvl
591 (list (lisp-indent-lambda-list
592 indent-point
593 sexp-column
594 containing-form-start)
595 containing-form-start))
597 normal-indent))))
598 ((integerp tem)
599 (throw 'exit
600 (if (null p) ;not in subforms
601 (list (+ sexp-column tem) containing-form-start)
602 normal-indent)))
603 ((symbolp tem) ;a function to call
604 (throw 'exit
605 (funcall tem path state indent-point
606 sexp-column normal-indent)))
608 ;; must be a destructing frob
609 (if (not (null p))
610 ;; descend
611 (setq method (cddr tem)
612 n nil)
613 (setq tem (cadr tem))
614 (throw 'exit
615 (cond (tail
616 normal-indent)
617 ((eq tem 'nil)
618 (list normal-indent
619 containing-form-start))
620 ((integerp tem)
621 (list (+ sexp-column tem)
622 containing-form-start))
624 (funcall tem path state indent-point
625 sexp-column normal-indent))))))))))))
627 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
628 (if (not (null (cdr path)))
629 normal-indent
630 (save-excursion
631 (goto-char indent-point)
632 (beginning-of-line)
633 (skip-chars-forward " \t")
634 (list (cond ((looking-at "\\sw\\|\\s_")
635 ;; a tagbody tag
636 (+ sexp-column lisp-tag-indentation))
637 ((integerp lisp-tag-body-indentation)
638 (+ sexp-column lisp-tag-body-indentation))
639 ((eq lisp-tag-body-indentation 't)
640 (condition-case ()
641 (progn (backward-sexp 1) (current-column))
642 (error (1+ sexp-column))))
643 (t (+ sexp-column lisp-body-indent)))
644 ; (cond ((integerp lisp-tag-body-indentation)
645 ; (+ sexp-column lisp-tag-body-indentation))
646 ; ((eq lisp-tag-body-indentation 't)
647 ; normal-indent)
648 ; (t
649 ; (+ sexp-column lisp-body-indent)))
650 (elt state 1)
651 ))))
653 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
654 (if (>= (car path) 3)
655 (let ((lisp-tag-body-indentation lisp-body-indent))
656 (funcall (function lisp-indent-tagbody)
657 path state indent-point sexp-column normal-indent))
658 (funcall (function lisp-indent-259)
659 '((&whole nil &rest
660 ;; the following causes weird indentation
661 ;;(&whole 1 1 2 nil)
663 (&whole nil &rest 1))
664 path state indent-point sexp-column normal-indent)))
667 ;; LISP-INDENT-DEFMETHOD now supports the presence of more than one method
668 ;; qualifier and indents the method's lambda list properly. -- dvl
669 (defun lisp-indent-defmethod
670 (path state indent-point sexp-column normal-indent)
671 (lisp-indent-259
672 (let ((nqual 0))
673 (if (and (>= (car path) 3)
674 (save-excursion
675 (beginning-of-defun)
676 (forward-char 1)
677 (forward-sexp 2)
678 (skip-chars-forward " \t\n")
679 (while (looking-at "\\sw\\|\\s_")
680 (incf nqual)
681 (forward-sexp)
682 (skip-chars-forward " \t\n"))
683 (> nqual 0)))
684 (append '(4) (make-list nqual 4) '(&lambda &body))
685 (get 'defun 'common-lisp-indent-function)))
686 path state indent-point sexp-column normal-indent))
689 (defun lisp-indent-function-lambda-hack (path state indent-point
690 sexp-column normal-indent)
691 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
692 (if (or (cdr path) ; wtf?
693 (> (car path) 3))
694 ;; line up under previous body form
695 normal-indent
696 ;; line up under function rather than under lambda in order to
697 ;; conserve horizontal space. (Which is what #' is for.)
698 (condition-case ()
699 (save-excursion
700 (backward-up-list 2)
701 (forward-char 1)
702 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
703 (+ lisp-body-indent -1 (current-column))
704 (+ sexp-column lisp-body-indent)))
705 (error (+ sexp-column lisp-body-indent)))))
709 (let ((l '((block 1)
710 (case (4 &rest (&whole 2 &rest 1)))
711 (ccase . case)
712 (ecase . case)
713 (typecase . case)
714 (etypecase . case)
715 (ctypecase . case)
716 (catch 1)
717 (cond (&rest (&whole 2 &rest 1)))
718 (defvar (4 2 2))
719 (defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1)))
720 (defconstant . defvar)
721 (defcustom (4 2 2 2))
722 (defparameter . defvar)
723 (defconst . defcustom)
724 (define-condition . defclass)
725 (define-modify-macro (4 &lambda &body))
726 (defsetf (4 &lambda 4 &body))
727 (defun (4 &lambda &body))
728 (defgeneric (4 &lambda &body))
729 (define-setf-method . defun)
730 (define-setf-expander . defun)
731 (defmacro . defun)
732 (defsubst . defun)
733 (deftype . defun)
734 (defmethod lisp-indent-defmethod)
735 (defpackage (4 2))
736 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
737 &rest (&whole 2 &rest 1)))
738 (destructuring-bind
739 ((&whole 6 &rest 1) 4 &body))
740 (do lisp-indent-do)
741 (do* . do)
742 (dolist ((&whole 4 2 1) &body))
743 (dotimes . dolist)
744 (eval-when 1)
745 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
746 (labels . flet)
747 (macrolet . flet)
748 (generic-flet . flet)
749 (generic-labels . flet)
750 (handler-case (4 &rest (&whole 2 &lambda &body)))
751 (restart-case . handler-case)
752 ;; `else-body' style
753 (if (nil nil &body))
754 ;; single-else style (then and else equally indented)
755 (if (&rest nil))
756 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
757 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
758 (let* . let)
759 (compiler-let . let) ;barf
760 (handler-bind . let)
761 (restart-bind . let)
762 (locally 1)
763 ;(loop lisp-indent-loop)
764 (:method (&lambda &body)) ; in `defgeneric'
765 (multiple-value-bind ((&whole 6 &rest 1) 4 &body))
766 (multiple-value-call (4 &body))
767 (multiple-value-prog1 1)
768 (multiple-value-setq (4 2))
769 (multiple-value-setf . multiple-value-setq)
770 (pprint-logical-block (4 2))
771 (print-unreadable-object ((&whole 4 1 &rest 1) &body))
772 ;; Combines the worst features of BLOCK, LET and TAGBODY
773 (prog (&lambda &rest lisp-indent-tagbody))
774 (prog* . prog)
775 (prog1 1)
776 (prog2 2)
777 (progn 0)
778 (progv (4 4 &body))
779 (return 0)
780 (return-from (nil &body))
781 (symbol-macrolet . let)
782 (tagbody lisp-indent-tagbody)
783 (throw 1)
784 (unless 1)
785 (unwind-protect (5 &body))
786 (when 1)
787 (with-accessors . multiple-value-bind)
788 (with-condition-restarts . multiple-value-bind)
789 (with-compilation-unit (&lambda &body))
790 (with-output-to-string (4 2))
791 (with-slots . multiple-value-bind)
792 (with-standard-io-syntax (2)))))
793 (dolist (el l)
794 (put (car el) 'common-lisp-indent-function
795 (if (symbolp (cdr el))
796 (get (cdr el) 'common-lisp-indent-function)
797 (car (cdr el))))))
799 ;; In elisp, the else part of `if' is in an implicit progn, so indent
800 ;; it more.
801 (put 'if 'common-lisp-indent-function-for-elisp 2)
804 ;(defun foo (x)
805 ; (tagbody
806 ; foo
807 ; (bar)
808 ; baz
809 ; (when (losing)
810 ; (with-big-loser
811 ; (yow)
812 ; ((lambda ()
813 ; foo)
814 ; big)))
815 ; (flet ((foo (bar baz zap)
816 ; (zip))
817 ; (zot ()
818 ; quux))
819 ; (do ()
820 ; ((lose)
821 ; (foo 1))
822 ; (quux)
823 ; foo
824 ; (lose))
825 ; (cond ((x)
826 ; (win 1 2
827 ; (foo)))
828 ; (t
829 ; (lose
830 ; 3))))))
833 ;(put 'while 'common-lisp-indent-function 1)
834 ;(put 'defwrapper'common-lisp-indent-function ...)
835 ;(put 'def 'common-lisp-indent-function ...)
836 ;(put 'defflavor 'common-lisp-indent-function ...)
837 ;(put 'defsubst 'common-lisp-indent-function ...)
839 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
840 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
841 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
842 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
843 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
844 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
845 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
847 (provide 'cl-indent)
849 ;;; cl-indent.el ends here