1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
8 ;; Keywords: lisp, tools
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/>.
28 ;; This package supplies a single entry point, common-lisp-indent-function,
29 ;; which performs indentation in the preferred style for Common Lisp code.
32 ;; (setq lisp-indent-function 'common-lisp-indent-function)
39 ;; &key (like &body)??
41 ;; &rest 1 in lambda-lists doesn't work
42 ;; -- really want (foo bar
46 ;; Need something better than &rest for such cases
50 (defgroup lisp-indent nil
51 "Indentation in Lisp."
55 (defcustom lisp-indent-maximum-backtracking
3
56 "Maximum depth to backtrack out from a sublist for structured indentation.
57 If this variable is 0, no backtracking will occur and forms such as `flet'
58 may not be correctly indented."
62 (defcustom lisp-tag-indentation
1
63 "Indentation of tags relative to containing list.
64 This variable is used by the function `lisp-indent-tagbody'."
68 (defcustom lisp-tag-body-indentation
3
69 "Indentation of non-tagged lines relative to containing list.
70 This variable is used by the function `lisp-indent-tagbody' to indent normal
71 lines (lines without tags).
72 The indentation is relative to the indentation of the parenthesis enclosing
73 the special form. If the value is t, the body of tags will be indented
74 as a block at the same indentation as the first s-expression following
75 the tag. In this case, any forms before the first tag are indented
76 by `lisp-body-indent'."
80 (defcustom lisp-backquote-indentation t
81 "Whether or not to indent backquoted lists as code.
82 If nil, indent backquoted lists as data, i.e., like quoted lists."
87 (defcustom lisp-loop-keyword-indentation
3
88 "Indentation of loop keywords in extended loop forms."
93 (defcustom lisp-loop-forms-indentation
5
94 "Indentation of forms in extended loop forms."
99 (defcustom lisp-simple-loop-indentation
3
100 "Indentation of forms in simple loop forms."
105 (defvar lisp-indent-defun-method
'(4 &lambda
&body
)
106 "Indentation for function with `common-lisp-indent-function' property `defun'.")
109 (defun extended-loop-p (loop-start)
110 "True if an extended loop form starts at LOOP-START."
113 (goto-char loop-start
)
121 (defun common-lisp-loop-part-indentation (indent-point state
)
122 "Compute the indentation of loop form constituents."
123 (let* ((loop-indentation (save-excursion
124 (goto-char (elt state
1))
126 (goto-char indent-point
)
128 (cond ((not (extended-loop-p (elt state
1)))
129 (+ loop-indentation lisp-simple-loop-indentation
))
130 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
131 (+ loop-indentation lisp-loop-keyword-indentation
))
133 (+ loop-indentation lisp-loop-forms-indentation
)))))
137 (defun common-lisp-indent-function (indent-point state
)
138 "Function to indent the arguments of a Lisp function call.
139 This is suitable for use as the value of the variable
140 `lisp-indent-function'. INDENT-POINT is the point at which the
141 indentation function is called, and STATE is the
142 `parse-partial-sexp' state at that position. Browse the
143 `lisp-indent' customize group for options affecting the behavior
146 If the indentation point is in a call to a Lisp function, that
147 function's common-lisp-indent-function property specifies how
148 this function should indent it. Possible values for this
151 * defun, meaning indent according to `lisp-indent-defun-method';
152 i.e., like (4 &lambda &body), as explained below.
154 * any other symbol, meaning a function to call. The function should
155 take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT.
156 PATH is a list of integers describing the position of point in terms of
157 list-structure with respect to the containing lists. For example, in
158 ((a b c (d foo) f) g), foo has a path of (0 3 1). In other words,
159 to reach foo take the 0th element of the outermost list, then
160 the 3rd element of the next list, and finally the 1st element.
161 STATE and INDENT-POINT are as in the arguments to
162 `common-lisp-indent-function'. SEXP-COLUMN is the column of
163 the open parenthesis of the innermost containing list.
164 NORMAL-INDENT is the column the indentation point was
165 originally in. This function should behave like `lisp-indent-259'.
167 * an integer N, meaning indent the first N arguments like
168 function arguments, and any further arguments like a body.
169 This is equivalent to (4 4 ... &body).
171 * a list. The list element in position M specifies how to indent the Mth
172 function argument. If there are fewer elements than function arguments,
173 the last list element applies to all remaining arguments. The accepted
176 * nil, meaning the default indentation.
178 * an integer, specifying an explicit indentation.
180 * &lambda. Indent the argument (which may be a list) by 4.
182 * &rest. When used, this must be the penultimate element. The
183 element after this one applies to all remaining arguments.
185 * &body. This is equivalent to &rest lisp-body-indent, i.e., indent
186 all remaining elements by `lisp-body-indent'.
188 * &whole. This must be followed by nil, an integer, or a
189 function symbol. This indentation is applied to the
190 associated argument, and as a base indent for all remaining
191 arguments. For example, an integer P means indent this
192 argument by P, and all remaining arguments by P, plus the
193 value specified by their associated list element.
195 * a symbol. A function to call, with the 6 arguments specified above.
197 * a list, with elements as described above. This applies when the
198 associated function argument is itself a list. Each element of the list
199 specifies how to indent the associated argument.
201 For example, the function `case' has an indent property
202 \(4 &rest (&whole 2 &rest 1)), meaning:
203 * indent the first argument by 4.
204 * arguments after the first should be lists, and there may be any number
205 of them. The first list element has an offset of 2, all the rest
206 have an offset of 2+1=3."
207 (if (save-excursion (goto-char (elt state
1))
208 (looking-at "([Ll][Oo][Oo][Pp]"))
209 (common-lisp-loop-part-indentation indent-point state
)
210 (common-lisp-indent-function-1 indent-point state
)))
213 (defun common-lisp-indent-function-1 (indent-point state
)
214 (let ((normal-indent (current-column)))
215 ;; Walk up list levels until we see something
216 ;; which does special things with subforms.
218 ;; Path describes the position of point in terms of
219 ;; list-structure with respect to containing lists.
220 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
221 ;; (Surely (0 3 1)?).
223 ;; set non-nil when somebody works out the indentation to use
225 ;; If non-nil, this is an indentation to use
226 ;; if nothing else specifies it more firmly.
228 (last-point indent-point
)
229 ;; the position of the open-paren of the innermost containing list
230 (containing-form-start (elt state
1))
231 ;; the column of the above
233 ;; Move to start of innermost containing list
234 (goto-char containing-form-start
)
235 (setq sexp-column
(current-column))
237 ;; Look over successively less-deep containing forms
238 (while (and (not calculated
)
239 (< depth lisp-indent-maximum-backtracking
))
240 (let ((containing-sexp (point)))
242 (parse-partial-sexp (point) indent-point
1 t
)
243 ;; Move to the car of the relevant containing form
244 (let (tem function method tentative-defun
)
245 (if (not (looking-at "\\sw\\|\\s_"))
246 ;; This form doesn't seem to start with a symbol
247 (setq function nil method nil
)
250 (setq function
(downcase (buffer-substring-no-properties
253 (setq tem
(intern-soft function
)
254 method
(get tem
'common-lisp-indent-function
))
255 (cond ((and (null method
)
256 (string-match ":[^:]+" function
))
257 ;; The pleblisp package feature
258 (setq function
(substring function
259 (1+ (match-beginning 0)))
260 method
(get (intern-soft function
)
261 'common-lisp-indent-function
)))
263 ;; backwards compatibility
264 (setq method
(get tem
'lisp-indent-function
)))))
266 ;; How far into the containing form is the current form?
267 (if (< (point) indent-point
)
268 (while (condition-case ()
271 (if (>= (point) indent-point
)
273 (parse-partial-sexp (point)
278 (setq path
(cons n path
)))
280 ;; backwards compatibility.
281 (cond ((null function
))
283 (when (null (cdr path
))
284 ;; (package prefix was stripped off above)
285 (cond ((string-match "\\`def"
287 (setq tentative-defun
t))
291 (regexp-opt '("with" "without" "do"))
294 (setq method
'(&lambda
&body
))))))
295 ;; backwards compatibility. Bletch.
297 (setq method lisp-indent-defun-method
)))
299 (cond ((and (or (eq (char-after (1- containing-sexp
)) ?
\')
300 (and (not lisp-backquote-indentation
)
301 (eq (char-after (1- containing-sexp
)) ?\
`)))
302 (not (eq (char-after (- containing-sexp
2)) ?\
#)))
303 ;; No indentation for "'(...)" elements
304 (setq calculated
(1+ sexp-column
)))
305 ((or (eq (char-after (1- containing-sexp
)) ?\
,)
306 (and (eq (char-after (1- containing-sexp
)) ?\
@)
307 (eq (char-after (- containing-sexp
2)) ?\
,)))
308 ;; ",(...)" or ",@(...)"
309 (setq calculated normal-indent
))
310 ((eq (char-after (1- containing-sexp
)) ?\
#)
312 (setq calculated
(1+ sexp-column
)))
314 ;; If this looks like a call to a `def...' form,
315 ;; think about indenting it as one, but do it
316 ;; tentatively for cases like
319 ;; Set both normal-indent and tentative-calculated.
320 ;; The latter ensures this value gets used
321 ;; if there are no relevant containing constructs.
322 ;; The former ensures this value gets used
323 ;; if there is a relevant containing construct
324 ;; but we are nested within the structure levels
325 ;; that it specifies indentation for.
327 (setq tentative-calculated
328 (common-lisp-indent-call-method
329 function lisp-indent-defun-method
330 path state indent-point
331 sexp-column normal-indent
)
332 normal-indent tentative-calculated
)))
334 ;; convenient top-level hack.
335 ;; (also compatible with lisp-indent-function)
336 ;; The number specifies how many `distinguished'
337 ;; forms there are before the body starts
338 ;; Equivalent to (4 4 ... &body)
339 (setq calculated
(cond ((cdr path
)
341 ((<= (car path
) method
)
342 ;; `distinguished' form
343 (list (+ sexp-column
4)
344 containing-form-start
))
345 ((= (car path
) (1+ method
))
347 (+ sexp-column lisp-body-indent
))
353 (common-lisp-indent-call-method
354 function method path state indent-point
355 sexp-column normal-indent
)))))
356 (goto-char containing-sexp
)
357 (setq last-point containing-sexp
)
360 (progn (backward-up-list 1)
361 (setq depth
(1+ depth
)))
362 (error (setq depth lisp-indent-maximum-backtracking
))))))
363 (or calculated tentative-calculated
))))
366 (defun common-lisp-indent-call-method (function method path state indent-point
367 sexp-column normal-indent
)
368 (let ((lisp-indent-error-function function
))
371 path state indent-point
372 sexp-column normal-indent
)
373 (lisp-indent-259 method path state indent-point
374 sexp-column normal-indent
))))
376 ;; Dynamically bound in common-lisp-indent-call-method.
377 (defvar lisp-indent-error-function
)
379 (defun lisp-indent-report-bad-format (m)
380 (error "%s has a badly-formed %s property: %s"
381 ;; Love those free variable references!!
382 lisp-indent-error-function
'common-lisp-indent-function m
))
384 ;; Blame the crufty control structure on dynamic scoping
386 (defun lisp-indent-259 (method path state indent-point
387 sexp-column normal-indent
)
390 (containing-form-start (elt state
1))
392 ;; Isn't tail-recursion wonderful?
394 ;; This while loop is for destructuring.
395 ;; p is set to (cdr p) each iteration.
396 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
401 ;; This while loop is for advancing along a method
402 ;; until the relevant (possibly &rest/&body) pattern
404 ;; n is set to (1- n) and method to (cdr method)
406 (setq tem
(car method
))
408 (or (eq tem
'nil
) ;default indentation
409 (eq tem
'&lambda
) ;lambda list
410 (and (eq tem
'&body
) (null (cdr method
)))
413 (null (cddr method
)))
414 (integerp tem
) ;explicit indentation specified
415 (and (consp tem
) ;destructuring
416 (eq (car tem
) '&whole
)
417 (or (symbolp (cadr tem
))
418 (integerp (cadr tem
))))
419 (and (symbolp tem
) ;a function to call to do the work.
421 (lisp-indent-report-bad-format method
))
423 (cond ((and tail
(not (consp tem
)))
424 ;; indent tail of &rest in same way as first elt of rest
425 (throw 'exit normal-indent
))
427 ;; &body means (&rest <lisp-body-indent>)
429 (if (and (= n
0) ;first body form
430 (null p
)) ;not in subforms
435 ;; this pattern holds for all remaining forms
438 method
(cdr method
)))
440 ;; try next element of pattern
444 ;; Too few elements in pattern.
445 (throw 'exit normal-indent
)))
447 (throw 'exit
(if (consp normal-indent
)
449 (list normal-indent containing-form-start
))))
453 (list (+ sexp-column
4) containing-form-start
))
459 (if (null p
) ;not in subforms
460 (list (+ sexp-column tem
) containing-form-start
)
462 ((symbolp tem
) ;a function to call
464 (funcall tem path state indent-point
465 sexp-column normal-indent
)))
467 ;; must be a destructing frob
470 (setq method
(cddr tem
)
472 (setq tem
(cadr tem
))
478 containing-form-start
))
480 (list (+ sexp-column tem
)
481 containing-form-start
))
483 (funcall tem path state indent-point
484 sexp-column normal-indent
))))))))))))
486 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
487 (if (not (null (cdr path
)))
490 (goto-char indent-point
)
492 (skip-chars-forward " \t")
493 (list (cond ((looking-at "\\sw\\|\\s_")
495 (+ sexp-column lisp-tag-indentation
))
496 ((integerp lisp-tag-body-indentation
)
497 (+ sexp-column lisp-tag-body-indentation
))
498 ((eq lisp-tag-body-indentation
't
)
500 (progn (backward-sexp 1) (current-column))
501 (error (1+ sexp-column
))))
502 (t (+ sexp-column lisp-body-indent
)))
503 ; (cond ((integerp lisp-tag-body-indentation)
504 ; (+ sexp-column lisp-tag-body-indentation))
505 ; ((eq lisp-tag-body-indentation 't)
508 ; (+ sexp-column lisp-body-indent)))
512 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
513 (if (>= (car path
) 3)
514 (let ((lisp-tag-body-indentation lisp-body-indent
))
515 (funcall (function lisp-indent-tagbody
)
516 path state indent-point sexp-column normal-indent
))
517 (funcall (function lisp-indent-259
)
519 ;; the following causes weird indentation
522 (&whole nil
&rest
1))
523 path state indent-point sexp-column normal-indent
)))
526 (defun lisp-indent-defmethod (path state indent-point sexp-column
528 "Indentation function defmethod."
529 (lisp-indent-259 (if (and (>= (car path
) 3)
531 (save-excursion (goto-char (elt state
1))
535 (looking-at ":\\|\\sw+")))
536 '(4 4 (&whole
4 &rest
4) &body
)
537 (get 'defun
'common-lisp-indent-function
))
538 path state indent-point sexp-column normal-indent
))
541 (defun lisp-indent-function-lambda-hack (path state indent-point
542 sexp-column normal-indent
)
543 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
544 (if (or (cdr path
) ; wtf?
546 ;; line up under previous body form
548 ;; line up under function rather than under lambda in order to
549 ;; conserve horizontal space. (Which is what #' is for.)
554 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
555 (+ lisp-body-indent -
1 (current-column))
556 (+ sexp-column lisp-body-indent
)))
557 (error (+ sexp-column lisp-body-indent
)))))
562 (case (4 &rest
(&whole
2 &rest
1)))
569 (cond (&rest
(&whole
2 &rest
1)))
571 (defclass (6 4 (&whole
2 &rest
1) (&whole
2 &rest
1)))
572 (defconstant . defvar
)
573 (defcustom (4 2 2 2))
574 (defparameter . defvar
)
575 (defconst . defcustom
)
576 (define-condition . defclass
)
577 (define-modify-macro (4 &lambda
&body
))
578 (defsetf (4 &lambda
4 &body
))
579 (defun (4 &lambda
&body
))
580 (define-setf-method . defun
)
581 (define-setf-expander . defun
)
585 (defmethod lisp-indent-defmethod)
587 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
588 &rest
(&whole
2 &rest
1)))
590 ((&whole
6 &rest
1) 4 &body
))
593 (dolist ((&whole
4 2 1) &body
))
596 (flet ((&whole
4 &rest
(&whole
1 &lambda
&body
)) &body
))
599 (generic-flet . flet
)
600 (generic-labels . flet
)
601 (handler-case (4 &rest
(&whole
2 &lambda
&body
)))
602 (restart-case . handler-case
)
605 ;; single-else style (then and else equally indented)
607 (lambda (&lambda
&rest lisp-indent-function-lambda-hack
))
608 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
610 (compiler-let . let
) ;barf
614 ;(loop lisp-indent-loop)
615 (:method
(&lambda
&body
)) ; in `defgeneric'
616 (multiple-value-bind ((&whole
6 &rest
1) 4 &body
))
617 (multiple-value-call (4 &body
))
618 (multiple-value-prog1 1)
619 (multiple-value-setq (4 2))
620 (multiple-value-setf . multiple-value-setq
)
621 (pprint-logical-block (4 2))
622 (print-unreadable-object ((&whole
4 1 &rest
1) &body
))
623 ;; Combines the worst features of BLOCK, LET and TAGBODY
624 (prog (&lambda
&rest lisp-indent-tagbody
))
631 (return-from (nil &body
))
632 (symbol-macrolet . let
)
633 (tagbody lisp-indent-tagbody
)
636 (unwind-protect (5 &body
))
638 (with-accessors . multiple-value-bind
)
639 (with-condition-restarts . multiple-value-bind
)
640 (with-output-to-string (4 2))
641 (with-slots . multiple-value-bind
)
642 (with-standard-io-syntax (2)))))
644 (put (car el
) 'common-lisp-indent-function
645 (if (symbolp (cdr el
))
646 (get (cdr el
) 'common-lisp-indent-function
)
661 ; (flet ((foo (bar baz zap)
679 ;(put 'while 'common-lisp-indent-function 1)
680 ;(put 'defwrapper'common-lisp-indent-function ...)
681 ;(put 'def 'common-lisp-indent-function ...)
682 ;(put 'defflavor 'common-lisp-indent-function ...)
683 ;(put 'defsubst 'common-lisp-indent-function ...)
685 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
686 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
687 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
688 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
689 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
690 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
691 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
693 ;;; cl-indent.el ends here