Sync to HEAD
[bpt/emacs.git] / lisp / emacs-lisp / cl-indent.el
1 ;;; cl-indent.el --- enhanced lisp-indent mode
2
3 ;; Copyright (C) 1987, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
6 ;; Created: July 1987
7 ;; Maintainer: FSF
8 ;; Keywords: lisp, tools
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; This package supplies a single entry point, common-lisp-indent-function,
30 ;; which performs indentation in the preferred style for Common Lisp code.
31 ;; To enable it:
32 ;;
33 ;; (setq lisp-indent-function 'common-lisp-indent-function)
34
35 ;;>> TODO
36 ;; :foo
37 ;; bar
38 ;; :baz
39 ;; zap
40 ;; &key (like &body)??
41
42 ;; &rest 1 in lambda-lists doesn't work
43 ;; -- really want (foo bar
44 ;; baz)
45 ;; not (foo bar
46 ;; baz)
47 ;; Need something better than &rest for such cases
48
49 ;;; Code:
50
51 (defgroup lisp-indent nil
52 "Indentation in Lisp"
53 :group 'lisp)
54
55
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."
60 :type 'integer
61 :group 'lisp-indent)
62
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'."
66 :type 'integer
67 :group 'lisp-indent)
68
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'."
78 :type 'integer
79 :group 'lisp-indent)
80
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."
84 :type 'boolean
85 :group 'lisp-indent)
86
87
88 (defcustom lisp-loop-keyword-indentation 3
89 "*Indentation of loop keywords in extended loop forms."
90 :type 'integer
91 :group 'lisp-indent)
92
93
94 (defcustom lisp-loop-forms-indentation 5
95 "*Indentation of forms in extended loop forms."
96 :type 'integer
97 :group 'lisp-indent)
98
99
100 (defcustom lisp-simple-loop-indentation 3
101 "*Indentation of forms in simple loop forms."
102 :type 'integer
103 :group 'lisp-indent)
104
105 \f
106 (defvar lisp-indent-error-function)
107 (defvar lisp-indent-defun-method '(4 &lambda &body))
108
109
110 (defun extended-loop-p (loop-start)
111 "True if an extended loop form starts at LOOP-START."
112 (condition-case ()
113 (save-excursion
114 (goto-char loop-start)
115 (forward-char 1)
116 (forward-sexp 2)
117 (backward-sexp 1)
118 (looking-at "\\sw"))
119 (error t)))
120
121
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))
126 (current-column))))
127 (goto-char indent-point)
128 (beginning-of-line)
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))
133 (t
134 (+ loop-indentation lisp-loop-forms-indentation)))))
135
136
137 ;;;###autoload
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)))
143
144
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.
149 (let ((depth 0)
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)'
153 (path ())
154 ;; set non-nil when somebody works out the indentation to use
155 calculated
156 ;; If non-nil, this is an indentation to use
157 ;; if nothing else specifies it more firmly.
158 tentative-calculated
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
163 sexp-column)
164 ;; Move to start of innermost containing list
165 (goto-char containing-form-start)
166 (setq sexp-column (current-column))
167
168 ;; Look over successively less-deep containing forms
169 (while (and (not calculated)
170 (< depth lisp-indent-maximum-backtracking))
171 (let ((containing-sexp (point)))
172 (forward-char 1)
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)
179 (setq tem (point))
180 (forward-sexp 1)
181 (setq function (downcase (buffer-substring-no-properties
182 tem (point))))
183 (goto-char tem)
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)))
193 ((and (null method))
194 ;; backwards compatibility
195 (setq method (get tem 'lisp-indent-function)))))
196 (let ((n 0))
197 ;; How far into the containing form is the current form?
198 (if (< (point) indent-point)
199 (while (condition-case ()
200 (progn
201 (forward-sexp 1)
202 (if (>= (point) indent-point)
203 nil
204 (parse-partial-sexp (point)
205 indent-point 1 t)
206 (setq n (1+ n))
207 t))
208 (error nil))))
209 (setq path (cons n path)))
210
211 ;; backwards compatibility.
212 (cond ((null function))
213 ((null method)
214 (when (null (cdr path))
215 ;; (package prefix was stripped off above)
216 (cond ((string-match "\\`def"
217 function)
218 (setq tentative-defun t))
219 ((string-match "\\`\\(with\\|do\\)-"
220 function)
221 (setq method '(&lambda &body))))))
222 ;; backwards compatibility. Bletch.
223 ((eq method 'defun)
224 (setq method lisp-indent-defun-method)))
225
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)) ?\#)
238 ;; "#(...)"
239 (setq calculated (1+ sexp-column)))
240 ((null method)
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
244 ;; (flet ((defunp ()
245 ;; nil)))
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.
253 (if tentative-defun
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)))
260 ((integerp method)
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)
267 normal-indent)
268 ((<= (car path) method)
269 ;; `distinguished' form
270 (list (+ sexp-column 4)
271 containing-form-start))
272 ((= (car path) (1+ method))
273 ;; first body form.
274 (+ sexp-column lisp-body-indent))
275 (t
276 ;; other body form
277 normal-indent))))
278 (t
279 (setq calculated
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)
285 (unless calculated
286 (condition-case ()
287 (progn (backward-up-list 1)
288 (setq depth (1+ depth)))
289 (error (setq depth lisp-indent-maximum-backtracking))))))
290 (or calculated tentative-calculated))))
291
292
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))
296 (if (symbolp method)
297 (funcall method
298 path state indent-point
299 sexp-column normal-indent)
300 (lisp-indent-259 method path state indent-point
301 sexp-column normal-indent))))
302
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))
307
308 ;; Blame the crufty control structure on dynamic scoping
309 ;; -- not on me!
310 (defun lisp-indent-259 (method path state indent-point
311 sexp-column normal-indent)
312 (catch 'exit
313 (let ((p path)
314 (containing-form-start (elt state 1))
315 n tem tail)
316 ;; Isn't tail-recursion wonderful?
317 (while p
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))
321 (setq n (1- (car p))
322 p (cdr p)
323 tail nil)
324 (while n
325 ;; This while loop is for advancing along a method
326 ;; until the relevant (possibly &rest/&body) pattern
327 ;; is reached.
328 ;; n is set to (1- n) and method to (cdr method)
329 ;; each iteration.
330 (setq tem (car method))
331
332 (or (eq tem 'nil) ;default indentation
333 (eq tem '&lambda) ;lambda list
334 (and (eq tem '&body) (null (cdr method)))
335 (and (eq tem '&rest)
336 (consp (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.
344 (null (cdr method)))
345 (lisp-indent-report-bad-format method))
346
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))
350 ((eq tem '&body)
351 ;; &body means (&rest <lisp-body-indent>)
352 (throw 'exit
353 (if (and (= n 0) ;first body form
354 (null p)) ;not in subforms
355 (+ sexp-column
356 lisp-body-indent)
357 normal-indent)))
358 ((eq tem '&rest)
359 ;; this pattern holds for all remaining forms
360 (setq tail (> n 0)
361 n 0
362 method (cdr method)))
363 ((> n 0)
364 ;; try next element of pattern
365 (setq n (1- n)
366 method (cdr method))
367 (if (< n 0)
368 ;; Too few elements in pattern.
369 (throw 'exit normal-indent)))
370 ((eq tem 'nil)
371 (throw 'exit (list normal-indent containing-form-start)))
372 ((eq tem '&lambda)
373 (throw 'exit
374 (cond ((null p)
375 (list (+ sexp-column 4) containing-form-start))
376 ((null (cdr p))
377 (+ sexp-column 1))
378 (t normal-indent))))
379 ((integerp tem)
380 (throw 'exit
381 (if (null p) ;not in subforms
382 (list (+ sexp-column tem) containing-form-start)
383 normal-indent)))
384 ((symbolp tem) ;a function to call
385 (throw 'exit
386 (funcall tem path state indent-point
387 sexp-column normal-indent)))
388 (t
389 ;; must be a destructing frob
390 (if (not (null p))
391 ;; descend
392 (setq method (cddr tem)
393 n nil)
394 (setq tem (cadr tem))
395 (throw 'exit
396 (cond (tail
397 normal-indent)
398 ((eq tem 'nil)
399 (list normal-indent
400 containing-form-start))
401 ((integerp tem)
402 (list (+ sexp-column tem)
403 containing-form-start))
404 (t
405 (funcall tem path state indent-point
406 sexp-column normal-indent))))))))))))
407 \f
408 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
409 (if (not (null (cdr path)))
410 normal-indent
411 (save-excursion
412 (goto-char indent-point)
413 (beginning-of-line)
414 (skip-chars-forward " \t")
415 (list (cond ((looking-at "\\sw\\|\\s_")
416 ;; a tagbody tag
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)
421 (condition-case ()
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)
428 ; normal-indent)
429 ; (t
430 ; (+ sexp-column lisp-body-indent)))
431 (elt state 1)
432 ))))
433
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)
440 '((&whole nil &rest
441 ;; the following causes weird indentation
442 ;;(&whole 1 1 2 nil)
443 )
444 (&whole nil &rest 1))
445 path state indent-point sexp-column normal-indent)))
446
447
448 (defun lisp-indent-defmethod (path state indent-point sexp-column
449 normal-indent)
450 "Indentation function defmethod."
451 (lisp-indent-259 (if (and (>= (car path) 3)
452 (null (cdr path))
453 (save-excursion (goto-char (elt state 1))
454 (forward-char 1)
455 (forward-sexp 3)
456 (backward-sexp)
457 (looking-at ":")))
458 '(4 4 (&whole 4 &rest 4) &body)
459 (get 'defun 'common-lisp-indent-function))
460 path state indent-point sexp-column normal-indent))
461
462
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?
467 (> (car path) 3))
468 ;; line up under previous body form
469 normal-indent
470 ;; line up under function rather than under lambda in order to
471 ;; conserve horizontal space. (Which is what #' is for.)
472 (condition-case ()
473 (save-excursion
474 (backward-up-list 2)
475 (forward-char 1)
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)))))
480
481
482 \f
483 (let ((l '((block 1)
484 (case (4 &rest (&whole 2 &rest 1)))
485 (ccase . case) (ecase . case)
486 (typecase . case) (etypecase . case) (ctypecase . case)
487 (catch 1)
488 (cond (&rest (&whole 2 &rest 1)))
489 (defvar (4 2 2))
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)
503 (defpackage (4 2))
504 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
505 &rest (&whole 2 &rest 1)))
506 (destructuring-bind
507 ((&whole 6 &rest 1) 4 &body))
508 (do lisp-indent-do)
509 (do* . do)
510 (dolist ((&whole 4 2 1) &body))
511 (dotimes . dolist)
512 (eval-when 1)
513 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
514 (labels . flet)
515 (macrolet . flet)
516 (generic-flet . flet) (generic-labels . flet)
517 (handler-case (4 &rest (&whole 2 &lambda &body)))
518 (restart-case . handler-case)
519 ;; `else-body' style
520 (if (nil nil &body))
521 ;; single-else style (then and else equally indented)
522 (if (&rest nil))
523 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
524 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
525 (let* . let)
526 (compiler-let . let) ;barf
527 (handler-bind . let) (restart-bind . let)
528 (locally 1)
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))
540 (prog* . prog)
541 (prog1 1)
542 (prog2 2)
543 (progn 0)
544 (progv (4 4 &body))
545 (return 0)
546 (return-from (nil &body))
547 (symbol-macrolet . let)
548 (tagbody lisp-indent-tagbody)
549 (throw 1)
550 (unless 1)
551 (unwind-protect (5 &body))
552 (when 1)
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)))))
558 (dolist (el l)
559 (put (car el) 'common-lisp-indent-function
560 (if (symbolp (cdr el))
561 (get (cdr el) 'common-lisp-indent-function)
562 (car (cdr el))))))
563
564 \f
565 ;(defun foo (x)
566 ; (tagbody
567 ; foo
568 ; (bar)
569 ; baz
570 ; (when (losing)
571 ; (with-big-loser
572 ; (yow)
573 ; ((lambda ()
574 ; foo)
575 ; big)))
576 ; (flet ((foo (bar baz zap)
577 ; (zip))
578 ; (zot ()
579 ; quux))
580 ; (do ()
581 ; ((lose)
582 ; (foo 1))
583 ; (quux)
584 ; foo
585 ; (lose))
586 ; (cond ((x)
587 ; (win 1 2
588 ; (foo)))
589 ; (t
590 ; (lose
591 ; 3))))))
592
593
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 ...)
599
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)
607
608 ;;; arch-tag: 7914d50f-92ec-4476-93fc-0f043a380e03
609 ;;; cl-indent.el ends here