1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987 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 (defvar lisp-indent-maximum-backtracking
3
52 "*Maximum depth to backtrack out from a sublist for structured indentation.
53 If this variable is 0, no backtracking will occur and forms such as flet
54 may not be correctly indented.")
56 (defvar lisp-tag-indentation
1
57 "*Indentation of tags relative to containing list.
58 This variable is used by the function `lisp-indent-tagbody'.")
60 (defvar lisp-tag-body-indentation
3
61 "*Indentation of non-tagged lines relative to containing list.
62 This variable is used by the function `lisp-indent-tagbody' to indent normal
63 lines (lines without tags).
64 The indentation is relative to the indentation of the parenthesis enclosing
65 the special form. If the value is t, the body of tags will be indented
66 as a block at the same indentation as the first s-expression following
67 the tag. In this case, any forms before the first tag are indented
68 by `lisp-body-indent'.")
72 (defun common-lisp-indent-function (indent-point state
)
73 (let ((normal-indent (current-column)))
74 ;; Walk up list levels until we see something
75 ;; which does special things with subforms.
77 ;; Path describes the position of point in terms of
78 ;; list-structure with respect to containing lists.
79 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
81 ;; set non-nil when somebody works out the indentation to use
83 (last-point indent-point
)
84 ;; the position of the open-paren of the innermost containing list
85 (containing-form-start (elt state
1))
86 ;; the column of the above
88 ;; Move to start of innermost containing list
89 (goto-char containing-form-start
)
90 (setq sexp-column
(current-column))
91 ;; Look over successively less-deep containing forms
92 (while (and (not calculated
)
93 (< depth lisp-indent-maximum-backtracking
))
94 (let ((containing-sexp (point)))
96 (parse-partial-sexp (point) indent-point
1 t
)
97 ;; Move to the car of the relevant containing form
98 (let (tem function method
)
99 (if (not (looking-at "\\sw\\|\\s_"))
100 ;; This form doesn't seem to start with a symbol
101 (setq function nil method nil
)
104 (setq function
(downcase (buffer-substring tem
(point))))
106 (setq tem
(intern-soft function
)
107 method
(get tem
'common-lisp-indent-function
))
108 (cond ((and (null method
)
109 (string-match ":[^:]+" function
))
110 ;; The pleblisp package feature
111 (setq function
(substring function
112 (1+ (match-beginning 0)))
113 method
(get (intern-soft function
)
114 'common-lisp-indent-function
)))
116 ;; backwards compatibility
117 (setq method
(get tem
'lisp-indent-function
)))))
119 ;; How far into the containing form is the current form?
120 (if (< (point) indent-point
)
121 (while (condition-case ()
124 (if (>= (point) indent-point
)
126 (parse-partial-sexp (point)
131 (setq path
(cons n path
)))
133 ;; backwards compatibility.
134 (cond ((null function
))
136 (if (null (cdr path
))
137 ;; (package prefix was stripped off above)
138 (setq method
(cond ((string-match "\\`def"
140 '(4 (&whole
4 &rest
1) &body
))
141 ((string-match "\\`\\(with\\|do\\)-"
144 ;; backwards compatibility. Bletch.
146 (setq method
'(4 (&whole
4 &rest
1) &body
))))
148 (cond ((and (memq (char-after (1- containing-sexp
)) '(?
\' ?\
`))
149 (not (eql (char-after (- containing-sexp
2)) ?\
#)))
150 ;; No indentation for "'(...)" elements
151 (setq calculated
(1+ sexp-column
)))
152 ((or (eql (char-after (1- containing-sexp
)) ?\
,)
153 (and (eql (char-after (1- containing-sexp
)) ?\
@)
154 (eql (char-after (- containing-sexp
2)) ?\
,)))
155 ;; ",(...)" or ",@(...)"
156 (setq calculated normal-indent
))
157 ((eql (char-after (1- containing-sexp
)) ?\
#)
159 (setq calculated
(1+ sexp-column
)))
162 ;; convenient top-level hack.
163 ;; (also compatible with lisp-indent-function)
164 ;; The number specifies how many `distinguished'
165 ;; forms there are before the body starts
166 ;; Equivalent to (4 4 ... &body)
167 (setq calculated
(cond ((cdr path
)
169 ((<= (car path
) method
)
170 ;; `distinguished' form
171 (list (+ sexp-column
4)
172 containing-form-start
))
173 ((= (car path
) (1+ method
))
175 (+ sexp-column lisp-body-indent
))
180 (setq calculated
(funcall method
181 path state indent-point
182 sexp-column normal-indent
)))
184 (setq calculated
(lisp-indent-259
185 method path state indent-point
186 sexp-column normal-indent
)))))
187 (goto-char containing-sexp
)
188 (setq last-point containing-sexp
)
191 (progn (backward-up-list 1)
192 (setq depth
(1+ depth
)))
193 (error (setq depth lisp-indent-maximum-backtracking
))))))
197 (defun lisp-indent-report-bad-format (m)
198 (error "%s has a badly-formed %s property: %s"
199 ;; Love those free variable references!!
200 function
'common-lisp-indent-function m
))
202 ;; Blame the crufty control structure on dynamic scoping
204 (defun lisp-indent-259 (method path state indent-point
205 sexp-column normal-indent
)
208 (containing-form-start (elt state
1))
210 ;; Isn't tail-recursion wonderful?
212 ;; This while loop is for destructuring.
213 ;; p is set to (cdr p) each iteration.
214 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
219 ;; This while loop is for advancing along a method
220 ;; until the relevant (possibly &rest/&body) pattern
222 ;; n is set to (1- n) and method to (cdr method)
224 (setq tem
(car method
))
226 (or (eq tem
'nil
) ;default indentation
227 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
228 (and (eq tem
'&body
) (null (cdr method
)))
230 (consp (cdr method
)) (null (cdr (cdr method
))))
231 (integerp tem
) ;explicit indentation specified
232 (and (consp tem
) ;destructuring
233 (eq (car tem
) '&whole
)
234 (or (symbolp (car (cdr tem
)))
235 (integerp (car (cdr tem
)))))
236 (and (symbolp tem
) ;a function to call to do the work.
238 (lisp-indent-report-bad-format method
))
240 (cond ((and tail
(not (consp tem
)))
241 ;; indent tail of &rest in same way as first elt of rest
242 (throw 'exit normal-indent
))
244 ;; &body means (&rest <lisp-body-indent>)
246 (if (and (= n
0) ;first body form
247 (null p
)) ;not in subforms
252 ;; this pattern holds for all remaining forms
255 method
(cdr method
)))
257 ;; try next element of pattern
261 ;; Too few elements in pattern.
262 (throw 'exit normal-indent
)))
264 (throw 'exit
(list normal-indent containing-form-start
)))
266 ; ;; abbrev for (&whole 4 &rest 1)
269 ; (list (+ sexp-column 4) containing-form-start))
272 ; (t normal-indent))))
275 (if (null p
) ;not in subforms
276 (list (+ sexp-column tem
) containing-form-start
)
278 ((symbolp tem
) ;a function to call
280 (funcall tem path state indent-point
281 sexp-column normal-indent
)))
283 ;; must be a destructing frob
286 (setq method
(cdr (cdr tem
))
288 (setq tem
(car (cdr tem
)))
294 containing-form-start
))
296 (list (+ sexp-column tem
)
297 containing-form-start
))
299 (funcall tem path state indent-point
300 sexp-column normal-indent
))))))))))))
302 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
303 (if (not (null (cdr path
)))
306 (goto-char indent-point
)
308 (skip-chars-forward " \t")
309 (list (cond ((looking-at "\\sw\\|\\s_")
311 (+ sexp-column lisp-tag-indentation
))
312 ((integerp lisp-tag-body-indentation
)
313 (+ sexp-column lisp-tag-body-indentation
))
314 ((eq lisp-tag-body-indentation
't
)
316 (progn (backward-sexp 1) (current-column))
317 (error (1+ sexp-column
))))
318 (t (+ sexp-column lisp-body-indent
)))
319 ; (cond ((integerp lisp-tag-body-indentation)
320 ; (+ sexp-column lisp-tag-body-indentation))
321 ; ((eq lisp-tag-body-indentation 't)
324 ; (+ sexp-column lisp-body-indent)))
328 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
329 (if (>= (car path
) 3)
330 (let ((lisp-tag-body-indentation lisp-body-indent
))
331 (funcall (function lisp-indent-tagbody
)
332 path state indent-point sexp-column normal-indent
))
333 (funcall (function lisp-indent-259
)
335 ;; the following causes weird indentation
338 (&whole nil
&rest
1))
339 path state indent-point sexp-column normal-indent
)))
341 (defun lisp-indent-function-lambda-hack (path state indent-point
342 sexp-column normal-indent
)
343 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
344 (if (or (cdr path
) ; wtf?
346 ;; line up under previous body form
348 ;; line up under function rather than under lambda in order to
349 ;; conserve horizontal space. (Which is what #' is for.)
354 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
355 (+ lisp-body-indent -
1 (current-column))
356 (+ sexp-column lisp-body-indent
)))
357 (error (+ sexp-column lisp-body-indent
)))))
362 (case (4 &rest
(&whole
2 &rest
1)))
363 (ccase . case
) (ecase . case
)
364 (typecase . case
) (etypecase . case
) (ctypecase . case
)
366 (cond (&rest
(&whole
2 &rest
1)))
369 (defconstant . defvar
) (defparameter . defvar
)
373 (4 (&whole
4 &rest
1) &body
))
374 (defsetf (4 (&whole
4 &rest
1) 4 &body
))
375 (defun (4 (&whole
4 &rest
1) &body
))
376 (defmacro . defun
) (deftype . defun
)
377 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
378 &rest
(&whole
2 &rest
1)))
380 ((&whole
6 &rest
1) 4 &body
))
383 (dolist ((&whole
4 2 1) &body
))
386 (flet ((&whole
4 &rest
(&whole
1 (&whole
4 &rest
1) &body
))
392 ;; single-else style (then and else equally indented)
394 ;(lambda ((&whole 4 &rest 1) &body))
395 (lambda ((&whole
4 &rest
1)
396 &rest lisp-indent-function-lambda-hack
))
397 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
399 (compiler-let . let
) ;barf
403 ((&whole
6 &rest
1) 4 &body
))
406 (multiple-value-list 1)
407 (multiple-value-prog1 1)
410 ;; Combines the worst features of BLOCK, LET and TAGBODY
411 (prog ((&whole
4 &rest
1) &rest lisp-indent-tagbody
))
418 (return-from (nil &body
))
419 (tagbody lisp-indent-tagbody
)
426 (put (car (car l
)) 'common-lisp-indent-function
427 (if (symbolp (cdr (car l
)))
428 (get (cdr (car l
)) 'common-lisp-indent-function
)
429 (car (cdr (car l
)))))
444 ; (flet ((foo (bar baz zap)
462 ;(put 'while 'common-lisp-indent-function 1)
463 ;(put 'defwrapper'common-lisp-indent-function ...)
464 ;(put 'def 'common-lisp-indent-function ...)
465 ;(put 'defflavor 'common-lisp-indent-function ...)
466 ;(put 'defsubst 'common-lisp-indent-function ...)
468 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
469 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
470 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
471 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
472 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
474 ;;; cl-indent.el ends here