1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987, 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
8 ;; Keywords: lisp, tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
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
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-error-function
)
107 (defvar lisp-indent-defun-method
'(4 &lambda
&body
))
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 (if (save-excursion (goto-char (elt state
1))
140 (looking-at "([Ll][Oo][Oo][Pp]"))
141 (common-lisp-loop-part-indentation indent-point state
)
142 (common-lisp-indent-function-1 indent-point state
)))
145 (defun common-lisp-indent-function-1 (indent-point state
)
146 (let ((normal-indent (current-column)))
147 ;; Walk up list levels until we see something
148 ;; which does special things with subforms.
150 ;; Path describes the position of point in terms of
151 ;; list-structure with respect to containing lists.
152 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
154 ;; set non-nil when somebody works out the indentation to use
156 ;; If non-nil, this is an indentation to use
157 ;; if nothing else specifies it more firmly.
159 (last-point indent-point
)
160 ;; the position of the open-paren of the innermost containing list
161 (containing-form-start (elt state
1))
162 ;; the column of the above
164 ;; Move to start of innermost containing list
165 (goto-char containing-form-start
)
166 (setq sexp-column
(current-column))
168 ;; Look over successively less-deep containing forms
169 (while (and (not calculated
)
170 (< depth lisp-indent-maximum-backtracking
))
171 (let ((containing-sexp (point)))
173 (parse-partial-sexp (point) indent-point
1 t
)
174 ;; Move to the car of the relevant containing form
175 (let (tem function method tentative-defun
)
176 (if (not (looking-at "\\sw\\|\\s_"))
177 ;; This form doesn't seem to start with a symbol
178 (setq function nil method nil
)
181 (setq function
(downcase (buffer-substring-no-properties
184 (setq tem
(intern-soft function
)
185 method
(get tem
'common-lisp-indent-function
))
186 (cond ((and (null method
)
187 (string-match ":[^:]+" function
))
188 ;; The pleblisp package feature
189 (setq function
(substring function
190 (1+ (match-beginning 0)))
191 method
(get (intern-soft function
)
192 'common-lisp-indent-function
)))
194 ;; backwards compatibility
195 (setq method
(get tem
'lisp-indent-function
)))))
197 ;; How far into the containing form is the current form?
198 (if (< (point) indent-point
)
199 (while (condition-case ()
202 (if (>= (point) indent-point
)
204 (parse-partial-sexp (point)
209 (setq path
(cons n path
)))
211 ;; backwards compatibility.
212 (cond ((null function
))
214 (when (null (cdr path
))
215 ;; (package prefix was stripped off above)
216 (cond ((string-match "\\`def"
218 (setq tentative-defun
t))
219 ((string-match "\\`\\(with\\|do\\)-"
221 (setq method
'(&lambda
&body
))))))
222 ;; backwards compatibility. Bletch.
224 (setq method lisp-indent-defun-method
)))
226 (cond ((and (or (eq (char-after (1- containing-sexp
)) ?
\')
227 (and (not lisp-backquote-indentation
)
228 (eq (char-after (1- containing-sexp
)) ?\
`)))
229 (not (eq (char-after (- containing-sexp
2)) ?\
#)))
230 ;; No indentation for "'(...)" elements
231 (setq calculated
(1+ sexp-column
)))
232 ((or (eq (char-after (1- containing-sexp
)) ?\
,)
233 (and (eq (char-after (1- containing-sexp
)) ?\
@)
234 (eq (char-after (- containing-sexp
2)) ?\
,)))
235 ;; ",(...)" or ",@(...)"
236 (setq calculated normal-indent
))
237 ((eq (char-after (1- containing-sexp
)) ?\
#)
239 (setq calculated
(1+ sexp-column
)))
241 ;; If this looks like a call to a `def...' form,
242 ;; think about indenting it as one, but do it
243 ;; tentatively for cases like
246 ;; Set both normal-indent and tentative-calculated.
247 ;; The latter ensures this value gets used
248 ;; if there are no relevant containing constructs.
249 ;; The former ensures this value gets used
250 ;; if there is a relevant containing construct
251 ;; but we are nested within the structure levels
252 ;; that it specifies indentation for.
254 (setq tentative-calculated
255 (common-lisp-indent-call-method
256 function lisp-indent-defun-method
257 path state indent-point
258 sexp-column normal-indent
)
259 normal-indent tentative-calculated
)))
261 ;; convenient top-level hack.
262 ;; (also compatible with lisp-indent-function)
263 ;; The number specifies how many `distinguished'
264 ;; forms there are before the body starts
265 ;; Equivalent to (4 4 ... &body)
266 (setq calculated
(cond ((cdr path
)
268 ((<= (car path
) method
)
269 ;; `distinguished' form
270 (list (+ sexp-column
4)
271 containing-form-start
))
272 ((= (car path
) (1+ method
))
274 (+ sexp-column lisp-body-indent
))
280 (common-lisp-indent-call-method
281 function method path state indent-point
282 sexp-column normal-indent
)))))
283 (goto-char containing-sexp
)
284 (setq last-point containing-sexp
)
287 (progn (backward-up-list 1)
288 (setq depth
(1+ depth
)))
289 (error (setq depth lisp-indent-maximum-backtracking
))))))
290 (or calculated tentative-calculated
))))
293 (defun common-lisp-indent-call-method (function method path state indent-point
294 sexp-column normal-indent
)
295 (let ((lisp-indent-error-function function
))
298 path state indent-point
299 sexp-column normal-indent
)
300 (lisp-indent-259 method path state indent-point
301 sexp-column normal-indent
))))
303 (defun lisp-indent-report-bad-format (m)
304 (error "%s has a badly-formed %s property: %s"
305 ;; Love those free variable references!!
306 lisp-indent-error-function
'common-lisp-indent-function m
))
308 ;; Blame the crufty control structure on dynamic scoping
310 (defun lisp-indent-259 (method path state indent-point
311 sexp-column normal-indent
)
314 (containing-form-start (elt state
1))
316 ;; Isn't tail-recursion wonderful?
318 ;; This while loop is for destructuring.
319 ;; p is set to (cdr p) each iteration.
320 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
325 ;; This while loop is for advancing along a method
326 ;; until the relevant (possibly &rest/&body) pattern
328 ;; n is set to (1- n) and method to (cdr method)
330 (setq tem
(car method
))
332 (or (eq tem
'nil
) ;default indentation
333 (eq tem
'&lambda
) ;lambda list
334 (and (eq tem
'&body
) (null (cdr method
)))
337 (null (cddr method
)))
338 (integerp tem
) ;explicit indentation specified
339 (and (consp tem
) ;destructuring
340 (eq (car tem
) '&whole
)
341 (or (symbolp (cadr tem
))
342 (integerp (cadr tem
))))
343 (and (symbolp tem
) ;a function to call to do the work.
345 (lisp-indent-report-bad-format method
))
347 (cond ((and tail
(not (consp tem
)))
348 ;; indent tail of &rest in same way as first elt of rest
349 (throw 'exit normal-indent
))
351 ;; &body means (&rest <lisp-body-indent>)
353 (if (and (= n
0) ;first body form
354 (null p
)) ;not in subforms
359 ;; this pattern holds for all remaining forms
362 method
(cdr method
)))
364 ;; try next element of pattern
368 ;; Too few elements in pattern.
369 (throw 'exit normal-indent
)))
371 (throw 'exit
(list normal-indent containing-form-start
)))
375 (list (+ sexp-column
4) containing-form-start
))
381 (if (null p
) ;not in subforms
382 (list (+ sexp-column tem
) containing-form-start
)
384 ((symbolp tem
) ;a function to call
386 (funcall tem path state indent-point
387 sexp-column normal-indent
)))
389 ;; must be a destructing frob
392 (setq method
(cddr tem
)
394 (setq tem
(cadr tem
))
400 containing-form-start
))
402 (list (+ sexp-column tem
)
403 containing-form-start
))
405 (funcall tem path state indent-point
406 sexp-column normal-indent
))))))))))))
408 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
409 (if (not (null (cdr path
)))
412 (goto-char indent-point
)
414 (skip-chars-forward " \t")
415 (list (cond ((looking-at "\\sw\\|\\s_")
417 (+ sexp-column lisp-tag-indentation
))
418 ((integerp lisp-tag-body-indentation
)
419 (+ sexp-column lisp-tag-body-indentation
))
420 ((eq lisp-tag-body-indentation
't
)
422 (progn (backward-sexp 1) (current-column))
423 (error (1+ sexp-column
))))
424 (t (+ sexp-column lisp-body-indent
)))
425 ; (cond ((integerp lisp-tag-body-indentation)
426 ; (+ sexp-column lisp-tag-body-indentation))
427 ; ((eq lisp-tag-body-indentation 't)
430 ; (+ sexp-column lisp-body-indent)))
434 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
435 (if (>= (car path
) 3)
436 (let ((lisp-tag-body-indentation lisp-body-indent
))
437 (funcall (function lisp-indent-tagbody
)
438 path state indent-point sexp-column normal-indent
))
439 (funcall (function lisp-indent-259
)
441 ;; the following causes weird indentation
444 (&whole nil
&rest
1))
445 path state indent-point sexp-column normal-indent
)))
448 (defun lisp-indent-defmethod (path state indent-point sexp-column
450 "Indentation function defmethod."
451 (lisp-indent-259 (if (and (>= (car path
) 3)
453 (save-excursion (goto-char (elt state
1))
458 '(4 4 (&whole
4 &rest
4) &body
)
459 (get 'defun
'common-lisp-indent-function
))
460 path state indent-point sexp-column normal-indent
))
463 (defun lisp-indent-function-lambda-hack (path state indent-point
464 sexp-column normal-indent
)
465 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
466 (if (or (cdr path
) ; wtf?
468 ;; line up under previous body form
470 ;; line up under function rather than under lambda in order to
471 ;; conserve horizontal space. (Which is what #' is for.)
476 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
477 (+ lisp-body-indent -
1 (current-column))
478 (+ sexp-column lisp-body-indent
)))
479 (error (+ sexp-column lisp-body-indent
)))))
484 (case (4 &rest
(&whole
2 &rest
1)))
485 (ccase . case
) (ecase . case
)
486 (typecase . case
) (etypecase . case
) (ctypecase . case
)
488 (cond (&rest
(&whole
2 &rest
1)))
490 (defclass (6 4 (&whole
2 &rest
1) (&whole
2 &rest
1)))
491 (defconstant . defvar
)
492 (defcustom (4 2 2 2))
493 (defparameter . defvar
)
494 (defconst . defcustom
)
495 (define-condition . defclass
)
496 (define-modify-macro (4 &lambda
&body
))
497 (defsetf (4 &lambda
4 &body
))
498 (defun (4 &lambda
&body
))
499 (define-setf-method . defun
)
500 (define-setf-expander . defun
)
501 (defmacro . defun
) (defsubst . defun
) (deftype . defun
)
502 (defmethod lisp-indent-defmethod)
504 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
505 &rest
(&whole
2 &rest
1)))
507 ((&whole
6 &rest
1) 4 &body
))
510 (dolist ((&whole
4 2 1) &body
))
513 (flet ((&whole
4 &rest
(&whole
1 &lambda
&body
)) &body
))
516 (generic-flet . flet
) (generic-labels . flet
)
517 (handler-case (4 &rest
(&whole
2 &lambda
&body
)))
518 (restart-case . handler-case
)
521 ;; single-else style (then and else equally indented)
523 (lambda (&lambda
&rest lisp-indent-function-lambda-hack
))
524 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
526 (compiler-let . let
) ;barf
527 (handler-bind . let
) (restart-bind . let
)
529 ;(loop lisp-indent-loop)
530 (:method
(&lambda
&body
)) ; in `defgeneric'
531 (multiple-value-bind ((&whole
6 &rest
1) 4 &body
))
532 (multiple-value-call (4 &body
))
533 (multiple-value-prog1 1)
534 (multiple-value-setq (4 2))
535 (multiple-value-setf . multiple-value-setq
)
536 (pprint-logical-block (4 2))
537 (print-unreadable-object ((&whole
4 1 &rest
1) &body
))
538 ;; Combines the worst features of BLOCK, LET and TAGBODY
539 (prog (&lambda
&rest lisp-indent-tagbody
))
546 (return-from (nil &body
))
547 (symbol-macrolet . multiple-value-bind
)
548 (tagbody lisp-indent-tagbody
)
551 (unwind-protect (5 &body
))
553 (with-accessors . multiple-value-bind
)
554 (with-condition-restarts . multiple-value-bind
)
555 (with-output-to-string (4 2))
556 (with-slots . multiple-value-bind
)
557 (with-standard-io-syntax (2)))))
559 (put (car el
) 'common-lisp-indent-function
560 (if (symbolp (cdr el
))
561 (get (cdr el
) 'common-lisp-indent-function
)
576 ; (flet ((foo (bar baz zap)
594 ;(put 'while 'common-lisp-indent-function 1)
595 ;(put 'defwrapper'common-lisp-indent-function ...)
596 ;(put 'def 'common-lisp-indent-function ...)
597 ;(put 'defflavor 'common-lisp-indent-function ...)
598 ;(put 'defsubst 'common-lisp-indent-function ...)
600 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
601 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
602 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
603 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
604 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
605 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
606 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
608 ;;; cl-indent.el ends here