1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
9 ;; 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-error-function
)
106 (defvar lisp-indent-defun-method
'(4 &lambda
&body
))
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 (if (save-excursion (goto-char (elt state
1))
139 (looking-at "([Ll][Oo][Oo][Pp]"))
140 (common-lisp-loop-part-indentation indent-point state
)
141 (common-lisp-indent-function-1 indent-point state
)))
144 (defun common-lisp-indent-function-1 (indent-point state
)
145 (let ((normal-indent (current-column)))
146 ;; Walk up list levels until we see something
147 ;; which does special things with subforms.
149 ;; Path describes the position of point in terms of
150 ;; list-structure with respect to containing lists.
151 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
153 ;; set non-nil when somebody works out the indentation to use
155 ;; If non-nil, this is an indentation to use
156 ;; if nothing else specifies it more firmly.
158 (last-point indent-point
)
159 ;; the position of the open-paren of the innermost containing list
160 (containing-form-start (elt state
1))
161 ;; the column of the above
163 ;; Move to start of innermost containing list
164 (goto-char containing-form-start
)
165 (setq sexp-column
(current-column))
167 ;; Look over successively less-deep containing forms
168 (while (and (not calculated
)
169 (< depth lisp-indent-maximum-backtracking
))
170 (let ((containing-sexp (point)))
172 (parse-partial-sexp (point) indent-point
1 t
)
173 ;; Move to the car of the relevant containing form
174 (let (tem function method tentative-defun
)
175 (if (not (looking-at "\\sw\\|\\s_"))
176 ;; This form doesn't seem to start with a symbol
177 (setq function nil method nil
)
180 (setq function
(downcase (buffer-substring-no-properties
183 (setq tem
(intern-soft function
)
184 method
(get tem
'common-lisp-indent-function
))
185 (cond ((and (null method
)
186 (string-match ":[^:]+" function
))
187 ;; The pleblisp package feature
188 (setq function
(substring function
189 (1+ (match-beginning 0)))
190 method
(get (intern-soft function
)
191 'common-lisp-indent-function
)))
193 ;; backwards compatibility
194 (setq method
(get tem
'lisp-indent-function
)))))
196 ;; How far into the containing form is the current form?
197 (if (< (point) indent-point
)
198 (while (condition-case ()
201 (if (>= (point) indent-point
)
203 (parse-partial-sexp (point)
208 (setq path
(cons n path
)))
210 ;; backwards compatibility.
211 (cond ((null function
))
213 (when (null (cdr path
))
214 ;; (package prefix was stripped off above)
215 (cond ((string-match "\\`def"
217 (setq tentative-defun
t))
221 (regexp-opt '("with" "without" "do"))
224 (setq method
'(&lambda
&body
))))))
225 ;; backwards compatibility. Bletch.
227 (setq method lisp-indent-defun-method
)))
229 (cond ((and (or (eq (char-after (1- containing-sexp
)) ?
\')
230 (and (not lisp-backquote-indentation
)
231 (eq (char-after (1- containing-sexp
)) ?\
`)))
232 (not (eq (char-after (- containing-sexp
2)) ?\
#)))
233 ;; No indentation for "'(...)" elements
234 (setq calculated
(1+ sexp-column
)))
235 ((or (eq (char-after (1- containing-sexp
)) ?\
,)
236 (and (eq (char-after (1- containing-sexp
)) ?\
@)
237 (eq (char-after (- containing-sexp
2)) ?\
,)))
238 ;; ",(...)" or ",@(...)"
239 (setq calculated normal-indent
))
240 ((eq (char-after (1- containing-sexp
)) ?\
#)
242 (setq calculated
(1+ sexp-column
)))
244 ;; If this looks like a call to a `def...' form,
245 ;; think about indenting it as one, but do it
246 ;; tentatively for cases like
249 ;; Set both normal-indent and tentative-calculated.
250 ;; The latter ensures this value gets used
251 ;; if there are no relevant containing constructs.
252 ;; The former ensures this value gets used
253 ;; if there is a relevant containing construct
254 ;; but we are nested within the structure levels
255 ;; that it specifies indentation for.
257 (setq tentative-calculated
258 (common-lisp-indent-call-method
259 function lisp-indent-defun-method
260 path state indent-point
261 sexp-column normal-indent
)
262 normal-indent tentative-calculated
)))
264 ;; convenient top-level hack.
265 ;; (also compatible with lisp-indent-function)
266 ;; The number specifies how many `distinguished'
267 ;; forms there are before the body starts
268 ;; Equivalent to (4 4 ... &body)
269 (setq calculated
(cond ((cdr path
)
271 ((<= (car path
) method
)
272 ;; `distinguished' form
273 (list (+ sexp-column
4)
274 containing-form-start
))
275 ((= (car path
) (1+ method
))
277 (+ sexp-column lisp-body-indent
))
283 (common-lisp-indent-call-method
284 function method path state indent-point
285 sexp-column normal-indent
)))))
286 (goto-char containing-sexp
)
287 (setq last-point containing-sexp
)
290 (progn (backward-up-list 1)
291 (setq depth
(1+ depth
)))
292 (error (setq depth lisp-indent-maximum-backtracking
))))))
293 (or calculated tentative-calculated
))))
296 (defun common-lisp-indent-call-method (function method path state indent-point
297 sexp-column normal-indent
)
298 (let ((lisp-indent-error-function function
))
301 path state indent-point
302 sexp-column normal-indent
)
303 (lisp-indent-259 method path state indent-point
304 sexp-column normal-indent
))))
306 (defun lisp-indent-report-bad-format (m)
307 (error "%s has a badly-formed %s property: %s"
308 ;; Love those free variable references!!
309 lisp-indent-error-function
'common-lisp-indent-function m
))
311 ;; Blame the crufty control structure on dynamic scoping
313 (defun lisp-indent-259 (method path state indent-point
314 sexp-column normal-indent
)
317 (containing-form-start (elt state
1))
319 ;; Isn't tail-recursion wonderful?
321 ;; This while loop is for destructuring.
322 ;; p is set to (cdr p) each iteration.
323 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
328 ;; This while loop is for advancing along a method
329 ;; until the relevant (possibly &rest/&body) pattern
331 ;; n is set to (1- n) and method to (cdr method)
333 (setq tem
(car method
))
335 (or (eq tem
'nil
) ;default indentation
336 (eq tem
'&lambda
) ;lambda list
337 (and (eq tem
'&body
) (null (cdr method
)))
340 (null (cddr method
)))
341 (integerp tem
) ;explicit indentation specified
342 (and (consp tem
) ;destructuring
343 (eq (car tem
) '&whole
)
344 (or (symbolp (cadr tem
))
345 (integerp (cadr tem
))))
346 (and (symbolp tem
) ;a function to call to do the work.
348 (lisp-indent-report-bad-format method
))
350 (cond ((and tail
(not (consp tem
)))
351 ;; indent tail of &rest in same way as first elt of rest
352 (throw 'exit normal-indent
))
354 ;; &body means (&rest <lisp-body-indent>)
356 (if (and (= n
0) ;first body form
357 (null p
)) ;not in subforms
362 ;; this pattern holds for all remaining forms
365 method
(cdr method
)))
367 ;; try next element of pattern
371 ;; Too few elements in pattern.
372 (throw 'exit normal-indent
)))
374 (throw 'exit
(if (consp normal-indent
)
376 (list normal-indent containing-form-start
))))
380 (list (+ sexp-column
4) containing-form-start
))
386 (if (null p
) ;not in subforms
387 (list (+ sexp-column tem
) containing-form-start
)
389 ((symbolp tem
) ;a function to call
391 (funcall tem path state indent-point
392 sexp-column normal-indent
)))
394 ;; must be a destructing frob
397 (setq method
(cddr tem
)
399 (setq tem
(cadr tem
))
405 containing-form-start
))
407 (list (+ sexp-column tem
)
408 containing-form-start
))
410 (funcall tem path state indent-point
411 sexp-column normal-indent
))))))))))))
413 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
414 (if (not (null (cdr path
)))
417 (goto-char indent-point
)
419 (skip-chars-forward " \t")
420 (list (cond ((looking-at "\\sw\\|\\s_")
422 (+ sexp-column lisp-tag-indentation
))
423 ((integerp lisp-tag-body-indentation
)
424 (+ sexp-column lisp-tag-body-indentation
))
425 ((eq lisp-tag-body-indentation
't
)
427 (progn (backward-sexp 1) (current-column))
428 (error (1+ sexp-column
))))
429 (t (+ sexp-column lisp-body-indent
)))
430 ; (cond ((integerp lisp-tag-body-indentation)
431 ; (+ sexp-column lisp-tag-body-indentation))
432 ; ((eq lisp-tag-body-indentation 't)
435 ; (+ sexp-column lisp-body-indent)))
439 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
440 (if (>= (car path
) 3)
441 (let ((lisp-tag-body-indentation lisp-body-indent
))
442 (funcall (function lisp-indent-tagbody
)
443 path state indent-point sexp-column normal-indent
))
444 (funcall (function lisp-indent-259
)
446 ;; the following causes weird indentation
449 (&whole nil
&rest
1))
450 path state indent-point sexp-column normal-indent
)))
453 (defun lisp-indent-defmethod (path state indent-point sexp-column
455 "Indentation function defmethod."
456 (lisp-indent-259 (if (and (>= (car path
) 3)
458 (save-excursion (goto-char (elt state
1))
462 (looking-at ":\\|\\sw+")))
463 '(4 4 (&whole
4 &rest
4) &body
)
464 (get 'defun
'common-lisp-indent-function
))
465 path state indent-point sexp-column normal-indent
))
468 (defun lisp-indent-function-lambda-hack (path state indent-point
469 sexp-column normal-indent
)
470 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
471 (if (or (cdr path
) ; wtf?
473 ;; line up under previous body form
475 ;; line up under function rather than under lambda in order to
476 ;; conserve horizontal space. (Which is what #' is for.)
481 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
482 (+ lisp-body-indent -
1 (current-column))
483 (+ sexp-column lisp-body-indent
)))
484 (error (+ sexp-column lisp-body-indent
)))))
489 (case (4 &rest
(&whole
2 &rest
1)))
490 (ccase . case
) (ecase . case
)
491 (typecase . case
) (etypecase . case
) (ctypecase . case
)
493 (cond (&rest
(&whole
2 &rest
1)))
495 (defclass (6 4 (&whole
2 &rest
1) (&whole
2 &rest
1)))
496 (defconstant . defvar
)
497 (defcustom (4 2 2 2))
498 (defparameter . defvar
)
499 (defconst . defcustom
)
500 (define-condition . defclass
)
501 (define-modify-macro (4 &lambda
&body
))
502 (defsetf (4 &lambda
4 &body
))
503 (defun (4 &lambda
&body
))
504 (define-setf-method . defun
)
505 (define-setf-expander . defun
)
506 (defmacro . defun
) (defsubst . defun
) (deftype . defun
)
507 (defmethod lisp-indent-defmethod)
509 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
510 &rest
(&whole
2 &rest
1)))
512 ((&whole
6 &rest
1) 4 &body
))
515 (dolist ((&whole
4 2 1) &body
))
518 (flet ((&whole
4 &rest
(&whole
1 &lambda
&body
)) &body
))
521 (generic-flet . flet
) (generic-labels . flet
)
522 (handler-case (4 &rest
(&whole
2 &lambda
&body
)))
523 (restart-case . handler-case
)
526 ;; single-else style (then and else equally indented)
528 (lambda (&lambda
&rest lisp-indent-function-lambda-hack
))
529 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
531 (compiler-let . let
) ;barf
532 (handler-bind . let
) (restart-bind . let
)
534 ;(loop lisp-indent-loop)
535 (:method
(&lambda
&body
)) ; in `defgeneric'
536 (multiple-value-bind ((&whole
6 &rest
1) 4 &body
))
537 (multiple-value-call (4 &body
))
538 (multiple-value-prog1 1)
539 (multiple-value-setq (4 2))
540 (multiple-value-setf . multiple-value-setq
)
541 (pprint-logical-block (4 2))
542 (print-unreadable-object ((&whole
4 1 &rest
1) &body
))
543 ;; Combines the worst features of BLOCK, LET and TAGBODY
544 (prog (&lambda
&rest lisp-indent-tagbody
))
551 (return-from (nil &body
))
552 (symbol-macrolet . let
)
553 (tagbody lisp-indent-tagbody
)
556 (unwind-protect (5 &body
))
558 (with-accessors . multiple-value-bind
)
559 (with-condition-restarts . multiple-value-bind
)
560 (with-output-to-string (4 2))
561 (with-slots . multiple-value-bind
)
562 (with-standard-io-syntax (2)))))
564 (put (car el
) 'common-lisp-indent-function
565 (if (symbolp (cdr el
))
566 (get (cdr el
) 'common-lisp-indent-function
)
581 ; (flet ((foo (bar baz zap)
599 ;(put 'while 'common-lisp-indent-function 1)
600 ;(put 'defwrapper'common-lisp-indent-function ...)
601 ;(put 'def 'common-lisp-indent-function ...)
602 ;(put 'defflavor 'common-lisp-indent-function ...)
603 ;(put 'defsubst 'common-lisp-indent-function ...)
605 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
606 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
607 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
608 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
609 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
610 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
611 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
613 ;; arch-tag: 7914d50f-92ec-4476-93fc-0f043a380e03
614 ;;; cl-indent.el ends here