Switch to recommended form of GPLv3 permissions notice.
[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, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
7 ;; Created: July 1987
8 ;; Maintainer: FSF
9 ;; Keywords: lisp, tools
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
28 ;; This package supplies a single entry point, common-lisp-indent-function,
29 ;; which performs indentation in the preferred style for Common Lisp code.
30 ;; To enable it:
31 ;;
32 ;; (setq lisp-indent-function 'common-lisp-indent-function)
33
34 ;;>> TODO
35 ;; :foo
36 ;; bar
37 ;; :baz
38 ;; zap
39 ;; &key (like &body)??
40
41 ;; &rest 1 in lambda-lists doesn't work
42 ;; -- really want (foo bar
43 ;; baz)
44 ;; not (foo bar
45 ;; baz)
46 ;; Need something better than &rest for such cases
47
48 ;;; Code:
49
50 (defgroup lisp-indent nil
51 "Indentation in Lisp."
52 :group 'lisp)
53
54
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."
59 :type 'integer
60 :group 'lisp-indent)
61
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'."
65 :type 'integer
66 :group 'lisp-indent)
67
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'."
77 :type 'integer
78 :group 'lisp-indent)
79
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."
83 :type 'boolean
84 :group 'lisp-indent)
85
86
87 (defcustom lisp-loop-keyword-indentation 3
88 "*Indentation of loop keywords in extended loop forms."
89 :type 'integer
90 :group 'lisp-indent)
91
92
93 (defcustom lisp-loop-forms-indentation 5
94 "*Indentation of forms in extended loop forms."
95 :type 'integer
96 :group 'lisp-indent)
97
98
99 (defcustom lisp-simple-loop-indentation 3
100 "*Indentation of forms in simple loop forms."
101 :type 'integer
102 :group 'lisp-indent)
103
104 \f
105 (defvar lisp-indent-error-function)
106 (defvar lisp-indent-defun-method '(4 &lambda &body))
107
108
109 (defun extended-loop-p (loop-start)
110 "True if an extended loop form starts at LOOP-START."
111 (condition-case ()
112 (save-excursion
113 (goto-char loop-start)
114 (forward-char 1)
115 (forward-sexp 2)
116 (backward-sexp 1)
117 (looking-at "\\sw"))
118 (error t)))
119
120
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))
125 (current-column))))
126 (goto-char indent-point)
127 (beginning-of-line)
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))
132 (t
133 (+ loop-indentation lisp-loop-forms-indentation)))))
134
135
136 ;;;###autoload
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)))
142
143
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.
148 (let ((depth 0)
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)'
152 (path ())
153 ;; set non-nil when somebody works out the indentation to use
154 calculated
155 ;; If non-nil, this is an indentation to use
156 ;; if nothing else specifies it more firmly.
157 tentative-calculated
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
162 sexp-column)
163 ;; Move to start of innermost containing list
164 (goto-char containing-form-start)
165 (setq sexp-column (current-column))
166
167 ;; Look over successively less-deep containing forms
168 (while (and (not calculated)
169 (< depth lisp-indent-maximum-backtracking))
170 (let ((containing-sexp (point)))
171 (forward-char 1)
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)
178 (setq tem (point))
179 (forward-sexp 1)
180 (setq function (downcase (buffer-substring-no-properties
181 tem (point))))
182 (goto-char tem)
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)))
192 ((and (null method))
193 ;; backwards compatibility
194 (setq method (get tem 'lisp-indent-function)))))
195 (let ((n 0))
196 ;; How far into the containing form is the current form?
197 (if (< (point) indent-point)
198 (while (condition-case ()
199 (progn
200 (forward-sexp 1)
201 (if (>= (point) indent-point)
202 nil
203 (parse-partial-sexp (point)
204 indent-point 1 t)
205 (setq n (1+ n))
206 t))
207 (error nil))))
208 (setq path (cons n path)))
209
210 ;; backwards compatibility.
211 (cond ((null function))
212 ((null method)
213 (when (null (cdr path))
214 ;; (package prefix was stripped off above)
215 (cond ((string-match "\\`def"
216 function)
217 (setq tentative-defun t))
218 ((string-match
219 (eval-when-compile
220 (concat "\\`\\("
221 (regexp-opt '("with" "without" "do"))
222 "\\)-"))
223 function)
224 (setq method '(&lambda &body))))))
225 ;; backwards compatibility. Bletch.
226 ((eq method 'defun)
227 (setq method lisp-indent-defun-method)))
228
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)) ?\#)
241 ;; "#(...)"
242 (setq calculated (1+ sexp-column)))
243 ((null method)
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
247 ;; (flet ((defunp ()
248 ;; nil)))
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.
256 (if tentative-defun
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)))
263 ((integerp method)
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)
270 normal-indent)
271 ((<= (car path) method)
272 ;; `distinguished' form
273 (list (+ sexp-column 4)
274 containing-form-start))
275 ((= (car path) (1+ method))
276 ;; first body form.
277 (+ sexp-column lisp-body-indent))
278 (t
279 ;; other body form
280 normal-indent))))
281 (t
282 (setq calculated
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)
288 (unless calculated
289 (condition-case ()
290 (progn (backward-up-list 1)
291 (setq depth (1+ depth)))
292 (error (setq depth lisp-indent-maximum-backtracking))))))
293 (or calculated tentative-calculated))))
294
295
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))
299 (if (symbolp method)
300 (funcall method
301 path state indent-point
302 sexp-column normal-indent)
303 (lisp-indent-259 method path state indent-point
304 sexp-column normal-indent))))
305
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))
310
311 ;; Blame the crufty control structure on dynamic scoping
312 ;; -- not on me!
313 (defun lisp-indent-259 (method path state indent-point
314 sexp-column normal-indent)
315 (catch 'exit
316 (let ((p path)
317 (containing-form-start (elt state 1))
318 n tem tail)
319 ;; Isn't tail-recursion wonderful?
320 (while p
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))
324 (setq n (1- (car p))
325 p (cdr p)
326 tail nil)
327 (while n
328 ;; This while loop is for advancing along a method
329 ;; until the relevant (possibly &rest/&body) pattern
330 ;; is reached.
331 ;; n is set to (1- n) and method to (cdr method)
332 ;; each iteration.
333 (setq tem (car method))
334
335 (or (eq tem 'nil) ;default indentation
336 (eq tem '&lambda) ;lambda list
337 (and (eq tem '&body) (null (cdr method)))
338 (and (eq tem '&rest)
339 (consp (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.
347 (null (cdr method)))
348 (lisp-indent-report-bad-format method))
349
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))
353 ((eq tem '&body)
354 ;; &body means (&rest <lisp-body-indent>)
355 (throw 'exit
356 (if (and (= n 0) ;first body form
357 (null p)) ;not in subforms
358 (+ sexp-column
359 lisp-body-indent)
360 normal-indent)))
361 ((eq tem '&rest)
362 ;; this pattern holds for all remaining forms
363 (setq tail (> n 0)
364 n 0
365 method (cdr method)))
366 ((> n 0)
367 ;; try next element of pattern
368 (setq n (1- n)
369 method (cdr method))
370 (if (< n 0)
371 ;; Too few elements in pattern.
372 (throw 'exit normal-indent)))
373 ((eq tem 'nil)
374 (throw 'exit (if (consp normal-indent)
375 normal-indent
376 (list normal-indent containing-form-start))))
377 ((eq tem '&lambda)
378 (throw 'exit
379 (cond ((null p)
380 (list (+ sexp-column 4) containing-form-start))
381 ((null (cdr p))
382 (+ sexp-column 1))
383 (t normal-indent))))
384 ((integerp tem)
385 (throw 'exit
386 (if (null p) ;not in subforms
387 (list (+ sexp-column tem) containing-form-start)
388 normal-indent)))
389 ((symbolp tem) ;a function to call
390 (throw 'exit
391 (funcall tem path state indent-point
392 sexp-column normal-indent)))
393 (t
394 ;; must be a destructing frob
395 (if (not (null p))
396 ;; descend
397 (setq method (cddr tem)
398 n nil)
399 (setq tem (cadr tem))
400 (throw 'exit
401 (cond (tail
402 normal-indent)
403 ((eq tem 'nil)
404 (list normal-indent
405 containing-form-start))
406 ((integerp tem)
407 (list (+ sexp-column tem)
408 containing-form-start))
409 (t
410 (funcall tem path state indent-point
411 sexp-column normal-indent))))))))))))
412 \f
413 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
414 (if (not (null (cdr path)))
415 normal-indent
416 (save-excursion
417 (goto-char indent-point)
418 (beginning-of-line)
419 (skip-chars-forward " \t")
420 (list (cond ((looking-at "\\sw\\|\\s_")
421 ;; a tagbody tag
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)
426 (condition-case ()
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)
433 ; normal-indent)
434 ; (t
435 ; (+ sexp-column lisp-body-indent)))
436 (elt state 1)
437 ))))
438
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)
445 '((&whole nil &rest
446 ;; the following causes weird indentation
447 ;;(&whole 1 1 2 nil)
448 )
449 (&whole nil &rest 1))
450 path state indent-point sexp-column normal-indent)))
451
452
453 (defun lisp-indent-defmethod (path state indent-point sexp-column
454 normal-indent)
455 "Indentation function defmethod."
456 (lisp-indent-259 (if (and (>= (car path) 3)
457 (null (cdr path))
458 (save-excursion (goto-char (elt state 1))
459 (forward-char 1)
460 (forward-sexp 3)
461 (backward-sexp)
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))
466
467
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?
472 (> (car path) 3))
473 ;; line up under previous body form
474 normal-indent
475 ;; line up under function rather than under lambda in order to
476 ;; conserve horizontal space. (Which is what #' is for.)
477 (condition-case ()
478 (save-excursion
479 (backward-up-list 2)
480 (forward-char 1)
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)))))
485
486
487 \f
488 (let ((l '((block 1)
489 (case (4 &rest (&whole 2 &rest 1)))
490 (ccase . case) (ecase . case)
491 (typecase . case) (etypecase . case) (ctypecase . case)
492 (catch 1)
493 (cond (&rest (&whole 2 &rest 1)))
494 (defvar (4 2 2))
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)
508 (defpackage (4 2))
509 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
510 &rest (&whole 2 &rest 1)))
511 (destructuring-bind
512 ((&whole 6 &rest 1) 4 &body))
513 (do lisp-indent-do)
514 (do* . do)
515 (dolist ((&whole 4 2 1) &body))
516 (dotimes . dolist)
517 (eval-when 1)
518 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
519 (labels . flet)
520 (macrolet . flet)
521 (generic-flet . flet) (generic-labels . flet)
522 (handler-case (4 &rest (&whole 2 &lambda &body)))
523 (restart-case . handler-case)
524 ;; `else-body' style
525 (if (nil nil &body))
526 ;; single-else style (then and else equally indented)
527 (if (&rest nil))
528 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
529 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
530 (let* . let)
531 (compiler-let . let) ;barf
532 (handler-bind . let) (restart-bind . let)
533 (locally 1)
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))
545 (prog* . prog)
546 (prog1 1)
547 (prog2 2)
548 (progn 0)
549 (progv (4 4 &body))
550 (return 0)
551 (return-from (nil &body))
552 (symbol-macrolet . let)
553 (tagbody lisp-indent-tagbody)
554 (throw 1)
555 (unless 1)
556 (unwind-protect (5 &body))
557 (when 1)
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)))))
563 (dolist (el l)
564 (put (car el) 'common-lisp-indent-function
565 (if (symbolp (cdr el))
566 (get (cdr el) 'common-lisp-indent-function)
567 (car (cdr el))))))
568
569 \f
570 ;(defun foo (x)
571 ; (tagbody
572 ; foo
573 ; (bar)
574 ; baz
575 ; (when (losing)
576 ; (with-big-loser
577 ; (yow)
578 ; ((lambda ()
579 ; foo)
580 ; big)))
581 ; (flet ((foo (bar baz zap)
582 ; (zip))
583 ; (zot ()
584 ; quux))
585 ; (do ()
586 ; ((lose)
587 ; (foo 1))
588 ; (quux)
589 ; foo
590 ; (lose))
591 ; (cond ((x)
592 ; (win 1 2
593 ; (foo)))
594 ; (t
595 ; (lose
596 ; 3))))))
597
598
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 ...)
604
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)
612
613 ;; arch-tag: 7914d50f-92ec-4476-93fc-0f043a380e03
614 ;;; cl-indent.el ends here