1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
9 ;; Keywords: lisp, tools
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This package supplies a single entry point, common-lisp-indent-function,
30 ;; which performs indentation in the preferred style for Common Lisp code.
33 ;; (setq lisp-indent-function 'common-lisp-indent-function)
40 ;; &key (like &body)??
42 ;; &rest 1 in lambda-lists doesn't work
43 ;; -- really want (foo bar
47 ;; Need something better than &rest for such cases
51 (defgroup lisp-indent nil
52 "Indentation in Lisp."
56 (defcustom lisp-indent-maximum-backtracking
3
57 "Maximum depth to backtrack out from a sublist for structured indentation.
58 If this variable is 0, no backtracking will occur and forms such as `flet'
59 may not be correctly indented."
63 (defcustom lisp-tag-indentation
1
64 "Indentation of tags relative to containing list.
65 This variable is used by the function `lisp-indent-tagbody'."
69 (defcustom lisp-tag-body-indentation
3
70 "Indentation of non-tagged lines relative to containing list.
71 This variable is used by the function `lisp-indent-tagbody' to indent normal
72 lines (lines without tags).
73 The indentation is relative to the indentation of the parenthesis enclosing
74 the special form. If the value is t, the body of tags will be indented
75 as a block at the same indentation as the first s-expression following
76 the tag. In this case, any forms before the first tag are indented
77 by `lisp-body-indent'."
81 (defcustom lisp-backquote-indentation t
82 "Whether or not to indent backquoted lists as code.
83 If nil, indent backquoted lists as data, i.e., like quoted lists."
88 (defcustom lisp-loop-keyword-indentation
3
89 "Indentation of loop keywords in extended loop forms."
94 (defcustom lisp-loop-forms-indentation
5
95 "Indentation of forms in extended loop forms."
100 (defcustom lisp-simple-loop-indentation
3
101 "Indentation of forms in simple loop forms."
106 (defvar lisp-indent-defun-method
'(4 &lambda
&body
)
107 "Indentation for function with `common-lisp-indent-function' property `defun'.")
110 (defun extended-loop-p (loop-start)
111 "True if an extended loop form starts at LOOP-START."
114 (goto-char loop-start
)
122 (defun common-lisp-loop-part-indentation (indent-point state
)
123 "Compute the indentation of loop form constituents."
124 (let* ((loop-indentation (save-excursion
125 (goto-char (elt state
1))
127 (goto-char indent-point
)
129 (cond ((not (extended-loop-p (elt state
1)))
130 (+ loop-indentation lisp-simple-loop-indentation
))
131 ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
132 (+ loop-indentation lisp-loop-keyword-indentation
))
134 (+ loop-indentation lisp-loop-forms-indentation
)))))
138 (defun common-lisp-indent-function (indent-point state
)
139 "Function to indent the arguments of a Lisp function call.
140 This is suitable for use as the value of the variable
141 `lisp-indent-function'. INDENT-POINT is the point at which the
142 indentation function is called, and STATE is the
143 `parse-partial-sexp' state at that position. Browse the
144 `lisp-indent' customize group for options affecting the behavior
147 If the indentation point is in a call to a Lisp function, that
148 function's common-lisp-indent-function property specifies how
149 this function should indent it. Possible values for this
152 * defun, meaning indent according to `lisp-indent-defun-method';
153 i.e., like (4 &lambda &body), as explained below.
155 * any other symbol, meaning a function to call. The function should
156 take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT.
157 PATH is a list of integers describing the position of point in terms of
158 list-structure with respect to the containing lists. For example, in
159 ((a b c (d foo) f) g), foo has a path of (0 3 1). In other words,
160 to reach foo take the 0th element of the outermost list, then
161 the 3rd element of the next list, and finally the 1st element.
162 STATE and INDENT-POINT are as in the arguments to
163 `common-lisp-indent-function'. SEXP-COLUMN is the column of
164 the open parenthesis of the innermost containing list.
165 NORMAL-INDENT is the column the indentation point was
166 originally in. This function should behave like `lisp-indent-259'.
168 * an integer N, meaning indent the first N arguments like
169 function arguments, and any further arguments like a body.
170 This is equivalent to (4 4 ... &body).
172 * a list. The list element in position M specifies how to indent the Mth
173 function argument. If there are fewer elements than function arguments,
174 the last list element applies to all remaining arguments. The accepted
177 * nil, meaning the default indentation.
179 * an integer, specifying an explicit indentation.
181 * &lambda. Indent the argument (which may be a list) by 4.
183 * &rest. When used, this must be the penultimate element. The
184 element after this one applies to all remaining arguments.
186 * &body. This is equivalent to &rest lisp-body-indent, i.e., indent
187 all remaining elements by `lisp-body-indent'.
189 * &whole. This must be followed by nil, an integer, or a
190 function symbol. This indentation is applied to the
191 associated argument, and as a base indent for all remaining
192 arguments. For example, an integer P means indent this
193 argument by P, and all remaining arguments by P, plus the
194 value specified by their associated list element.
196 * a symbol. A function to call, with the 6 arguments specified above.
198 * a list, with elements as described above. This applies when the
199 associated function argument is itself a list. Each element of the list
200 specifies how to indent the associated argument.
202 For example, the function `case' has an indent property
203 \(4 &rest (&whole 2 &rest 1)), meaning:
204 * indent the first argument by 4.
205 * arguments after the first should be lists, and there may be any number
206 of them. The first list element has an offset of 2, all the rest
207 have an offset of 2+1=3."
208 (if (save-excursion (goto-char (elt state
1))
209 (looking-at "([Ll][Oo][Oo][Pp]"))
210 (common-lisp-loop-part-indentation indent-point state
)
211 (common-lisp-indent-function-1 indent-point state
)))
214 (defun common-lisp-indent-function-1 (indent-point state
)
215 (let ((normal-indent (current-column)))
216 ;; Walk up list levels until we see something
217 ;; which does special things with subforms.
219 ;; Path describes the position of point in terms of
220 ;; list-structure with respect to containing lists.
221 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
222 ;; (Surely (0 3 1)?).
224 ;; set non-nil when somebody works out the indentation to use
226 ;; If non-nil, this is an indentation to use
227 ;; if nothing else specifies it more firmly.
229 (last-point indent-point
)
230 ;; the position of the open-paren of the innermost containing list
231 (containing-form-start (elt state
1))
232 ;; the column of the above
234 ;; Move to start of innermost containing list
235 (goto-char containing-form-start
)
236 (setq sexp-column
(current-column))
238 ;; Look over successively less-deep containing forms
239 (while (and (not calculated
)
240 (< depth lisp-indent-maximum-backtracking
))
241 (let ((containing-sexp (point)))
243 (parse-partial-sexp (point) indent-point
1 t
)
244 ;; Move to the car of the relevant containing form
245 (let (tem function method tentative-defun
)
246 (if (not (looking-at "\\sw\\|\\s_"))
247 ;; This form doesn't seem to start with a symbol
248 (setq function nil method nil
)
251 (setq function
(downcase (buffer-substring-no-properties
254 (setq tem
(intern-soft function
)
255 method
(get tem
'common-lisp-indent-function
))
256 (cond ((and (null method
)
257 (string-match ":[^:]+" function
))
258 ;; The pleblisp package feature
259 (setq function
(substring function
260 (1+ (match-beginning 0)))
261 method
(get (intern-soft function
)
262 'common-lisp-indent-function
)))
264 ;; backwards compatibility
265 (setq method
(get tem
'lisp-indent-function
)))))
267 ;; How far into the containing form is the current form?
268 (if (< (point) indent-point
)
269 (while (condition-case ()
272 (if (>= (point) indent-point
)
274 (parse-partial-sexp (point)
279 (setq path
(cons n path
)))
281 ;; backwards compatibility.
282 (cond ((null function
))
284 (when (null (cdr path
))
285 ;; (package prefix was stripped off above)
286 (cond ((string-match "\\`def"
288 (setq tentative-defun
t))
292 (regexp-opt '("with" "without" "do"))
295 (setq method
'(&lambda
&body
))))))
296 ;; backwards compatibility. Bletch.
298 (setq method lisp-indent-defun-method
)))
300 (cond ((and (or (eq (char-after (1- containing-sexp
)) ?
\')
301 (and (not lisp-backquote-indentation
)
302 (eq (char-after (1- containing-sexp
)) ?\
`)))
303 (not (eq (char-after (- containing-sexp
2)) ?\
#)))
304 ;; No indentation for "'(...)" elements
305 (setq calculated
(1+ sexp-column
)))
306 ((or (eq (char-after (1- containing-sexp
)) ?\
,)
307 (and (eq (char-after (1- containing-sexp
)) ?\
@)
308 (eq (char-after (- containing-sexp
2)) ?\
,)))
309 ;; ",(...)" or ",@(...)"
310 (setq calculated normal-indent
))
311 ((eq (char-after (1- containing-sexp
)) ?\
#)
313 (setq calculated
(1+ sexp-column
)))
315 ;; If this looks like a call to a `def...' form,
316 ;; think about indenting it as one, but do it
317 ;; tentatively for cases like
320 ;; Set both normal-indent and tentative-calculated.
321 ;; The latter ensures this value gets used
322 ;; if there are no relevant containing constructs.
323 ;; The former ensures this value gets used
324 ;; if there is a relevant containing construct
325 ;; but we are nested within the structure levels
326 ;; that it specifies indentation for.
328 (setq tentative-calculated
329 (common-lisp-indent-call-method
330 function lisp-indent-defun-method
331 path state indent-point
332 sexp-column normal-indent
)
333 normal-indent tentative-calculated
)))
335 ;; convenient top-level hack.
336 ;; (also compatible with lisp-indent-function)
337 ;; The number specifies how many `distinguished'
338 ;; forms there are before the body starts
339 ;; Equivalent to (4 4 ... &body)
340 (setq calculated
(cond ((cdr path
)
342 ((<= (car path
) method
)
343 ;; `distinguished' form
344 (list (+ sexp-column
4)
345 containing-form-start
))
346 ((= (car path
) (1+ method
))
348 (+ sexp-column lisp-body-indent
))
354 (common-lisp-indent-call-method
355 function method path state indent-point
356 sexp-column normal-indent
)))))
357 (goto-char containing-sexp
)
358 (setq last-point containing-sexp
)
361 (progn (backward-up-list 1)
362 (setq depth
(1+ depth
)))
363 (error (setq depth lisp-indent-maximum-backtracking
))))))
364 (or calculated tentative-calculated
))))
367 (defun common-lisp-indent-call-method (function method path state indent-point
368 sexp-column normal-indent
)
369 (let ((lisp-indent-error-function function
))
372 path state indent-point
373 sexp-column normal-indent
)
374 (lisp-indent-259 method path state indent-point
375 sexp-column normal-indent
))))
377 ;; Dynamically bound in common-lisp-indent-call-method.
378 (defvar lisp-indent-error-function
)
380 (defun lisp-indent-report-bad-format (m)
381 (error "%s has a badly-formed %s property: %s"
382 ;; Love those free variable references!!
383 lisp-indent-error-function
'common-lisp-indent-function m
))
385 ;; Blame the crufty control structure on dynamic scoping
387 (defun lisp-indent-259 (method path state indent-point
388 sexp-column normal-indent
)
391 (containing-form-start (elt state
1))
393 ;; Isn't tail-recursion wonderful?
395 ;; This while loop is for destructuring.
396 ;; p is set to (cdr p) each iteration.
397 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
402 ;; This while loop is for advancing along a method
403 ;; until the relevant (possibly &rest/&body) pattern
405 ;; n is set to (1- n) and method to (cdr method)
407 (setq tem
(car method
))
409 (or (eq tem
'nil
) ;default indentation
410 (eq tem
'&lambda
) ;lambda list
411 (and (eq tem
'&body
) (null (cdr method
)))
414 (null (cddr method
)))
415 (integerp tem
) ;explicit indentation specified
416 (and (consp tem
) ;destructuring
417 (eq (car tem
) '&whole
)
418 (or (symbolp (cadr tem
))
419 (integerp (cadr tem
))))
420 (and (symbolp tem
) ;a function to call to do the work.
422 (lisp-indent-report-bad-format method
))
424 (cond ((and tail
(not (consp tem
)))
425 ;; indent tail of &rest in same way as first elt of rest
426 (throw 'exit normal-indent
))
428 ;; &body means (&rest <lisp-body-indent>)
430 (if (and (= n
0) ;first body form
431 (null p
)) ;not in subforms
436 ;; this pattern holds for all remaining forms
439 method
(cdr method
)))
441 ;; try next element of pattern
445 ;; Too few elements in pattern.
446 (throw 'exit normal-indent
)))
448 (throw 'exit
(if (consp normal-indent
)
450 (list normal-indent containing-form-start
))))
454 (list (+ sexp-column
4) containing-form-start
))
460 (if (null p
) ;not in subforms
461 (list (+ sexp-column tem
) containing-form-start
)
463 ((symbolp tem
) ;a function to call
465 (funcall tem path state indent-point
466 sexp-column normal-indent
)))
468 ;; must be a destructing frob
471 (setq method
(cddr tem
)
473 (setq tem
(cadr tem
))
479 containing-form-start
))
481 (list (+ sexp-column tem
)
482 containing-form-start
))
484 (funcall tem path state indent-point
485 sexp-column normal-indent
))))))))))))
487 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
488 (if (not (null (cdr path
)))
491 (goto-char indent-point
)
493 (skip-chars-forward " \t")
494 (list (cond ((looking-at "\\sw\\|\\s_")
496 (+ sexp-column lisp-tag-indentation
))
497 ((integerp lisp-tag-body-indentation
)
498 (+ sexp-column lisp-tag-body-indentation
))
499 ((eq lisp-tag-body-indentation
't
)
501 (progn (backward-sexp 1) (current-column))
502 (error (1+ sexp-column
))))
503 (t (+ sexp-column lisp-body-indent
)))
504 ; (cond ((integerp lisp-tag-body-indentation)
505 ; (+ sexp-column lisp-tag-body-indentation))
506 ; ((eq lisp-tag-body-indentation 't)
509 ; (+ sexp-column lisp-body-indent)))
513 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
514 (if (>= (car path
) 3)
515 (let ((lisp-tag-body-indentation lisp-body-indent
))
516 (funcall (function lisp-indent-tagbody
)
517 path state indent-point sexp-column normal-indent
))
518 (funcall (function lisp-indent-259
)
520 ;; the following causes weird indentation
523 (&whole nil
&rest
1))
524 path state indent-point sexp-column normal-indent
)))
527 (defun lisp-indent-defmethod (path state indent-point sexp-column
529 "Indentation function defmethod."
530 (lisp-indent-259 (if (and (>= (car path
) 3)
532 (save-excursion (goto-char (elt state
1))
536 (looking-at ":\\|\\sw+")))
537 '(4 4 (&whole
4 &rest
4) &body
)
538 (get 'defun
'common-lisp-indent-function
))
539 path state indent-point sexp-column normal-indent
))
542 (defun lisp-indent-function-lambda-hack (path state indent-point
543 sexp-column normal-indent
)
544 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
545 (if (or (cdr path
) ; wtf?
547 ;; line up under previous body form
549 ;; line up under function rather than under lambda in order to
550 ;; conserve horizontal space. (Which is what #' is for.)
555 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
556 (+ lisp-body-indent -
1 (current-column))
557 (+ sexp-column lisp-body-indent
)))
558 (error (+ sexp-column lisp-body-indent
)))))
563 (case (4 &rest
(&whole
2 &rest
1)))
570 (cond (&rest
(&whole
2 &rest
1)))
572 (defclass (6 4 (&whole
2 &rest
1) (&whole
2 &rest
1)))
573 (defconstant . defvar
)
574 (defcustom (4 2 2 2))
575 (defparameter . defvar
)
576 (defconst . defcustom
)
577 (define-condition . defclass
)
578 (define-modify-macro (4 &lambda
&body
))
579 (defsetf (4 &lambda
4 &body
))
580 (defun (4 &lambda
&body
))
581 (define-setf-method . defun
)
582 (define-setf-expander . defun
)
586 (defmethod lisp-indent-defmethod)
588 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
589 &rest
(&whole
2 &rest
1)))
591 ((&whole
6 &rest
1) 4 &body
))
594 (dolist ((&whole
4 2 1) &body
))
597 (flet ((&whole
4 &rest
(&whole
1 &lambda
&body
)) &body
))
600 (generic-flet . flet
)
601 (generic-labels . flet
)
602 (handler-case (4 &rest
(&whole
2 &lambda
&body
)))
603 (restart-case . handler-case
)
606 ;; single-else style (then and else equally indented)
608 (lambda (&lambda
&rest lisp-indent-function-lambda-hack
))
609 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
611 (compiler-let . let
) ;barf
615 ;(loop lisp-indent-loop)
616 (:method
(&lambda
&body
)) ; in `defgeneric'
617 (multiple-value-bind ((&whole
6 &rest
1) 4 &body
))
618 (multiple-value-call (4 &body
))
619 (multiple-value-prog1 1)
620 (multiple-value-setq (4 2))
621 (multiple-value-setf . multiple-value-setq
)
622 (pprint-logical-block (4 2))
623 (print-unreadable-object ((&whole
4 1 &rest
1) &body
))
624 ;; Combines the worst features of BLOCK, LET and TAGBODY
625 (prog (&lambda
&rest lisp-indent-tagbody
))
632 (return-from (nil &body
))
633 (symbol-macrolet . let
)
634 (tagbody lisp-indent-tagbody
)
637 (unwind-protect (5 &body
))
639 (with-accessors . multiple-value-bind
)
640 (with-condition-restarts . multiple-value-bind
)
641 (with-output-to-string (4 2))
642 (with-slots . multiple-value-bind
)
643 (with-standard-io-syntax (2)))))
645 (put (car el
) 'common-lisp-indent-function
646 (if (symbolp (cdr el
))
647 (get (cdr el
) 'common-lisp-indent-function
)
662 ; (flet ((foo (bar baz zap)
680 ;(put 'while 'common-lisp-indent-function 1)
681 ;(put 'defwrapper'common-lisp-indent-function ...)
682 ;(put 'def 'common-lisp-indent-function ...)
683 ;(put 'defflavor 'common-lisp-indent-function ...)
684 ;(put 'defsubst 'common-lisp-indent-function ...)
686 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
687 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
688 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
689 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
690 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
691 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
692 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
694 ;; arch-tag: 7914d50f-92ec-4476-93fc-0f043a380e03
695 ;;; cl-indent.el ends here