1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Author: Richard Mlynark <mly@eddie.mit.edu>
5 ;; Last-Modified: 09 May 1991
6 ;; Keywords: lisp, tools
8 ;; Copyright (C) 1987 Free Software Foundation, Inc.
9 ;; Written by Richard Mlynarik July 1987
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 2, or (at your option)
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; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
34 ;; &key (like &body)??
36 ;; &rest 1 in lambda-lists doesn't work
37 ;; -- really want (foo bar
41 ;; Need something better than &rest for such cases
45 ;;; Hairy lisp indentation.
47 (defvar lisp-indent-maximum-backtracking
3
48 "*Maximum depth to backtrack out from a sublist for structured indentation.
49 If this variable is 0, no backtracking will occur and forms such as flet
50 may not be correctly indented.")
52 (defvar lisp-tag-indentation
1
53 "*Indentation of tags relative to containing list.
54 This variable is used by the function `lisp-indent-tagbody'.")
56 (defvar lisp-tag-body-indentation
3
57 "*Indentation of non-tagged lines relative to containing list.
58 This variable is used by the function `lisp-indent-tagbody' to indent normal
59 lines (lines without tags).
60 The indentation is relative to the indentation of the parenthesis enclosing
61 the special form. If the value is t, the body of tags will be indented
62 as a block at the same indentation as the first s-expression following
63 the tag. In this case, any forms before the first tag are indented
64 by `lisp-body-indent'.")
68 (defun common-lisp-indent-function (indent-point state
)
69 (let ((normal-indent (current-column)))
70 ;; Walk up list levels until we see something
71 ;; which does special things with subforms.
73 ;; Path describes the position of point in terms of
74 ;; list-structure with respect to contining lists.
75 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
77 ;; set non-nil when somebody works out the indentation to use
79 (last-point indent-point
)
80 ;; the position of the open-paren of the innermost containing list
81 (containing-form-start (elt state
1))
82 ;; the column of the above
84 ;; Move to start of innermost containing list
85 (goto-char containing-form-start
)
86 (setq sexp-column
(current-column))
87 ;; Look over successively less-deep containing forms
88 (while (and (not calculated
)
89 (< depth lisp-indent-maximum-backtracking
))
90 (let ((containing-sexp (point)))
92 (parse-partial-sexp (point) indent-point
1 t
)
93 ;; Move to the car of the relevant containing form
94 (let (tem function method
)
95 (if (not (looking-at "\\sw\\|\\s_"))
96 ;; This form doesn't seem to start with a symbol
97 (setq function nil method nil
)
100 (setq function
(downcase (buffer-substring tem
(point))))
102 (setq tem
(intern-soft function
)
103 method
(get tem
'common-lisp-indent-function
))
104 (cond ((and (null method
)
105 (string-match ":[^:]+" function
))
106 ;; The pleblisp package feature
107 (setq function
(substring function
108 (1+ (match-beginning 0)))
109 method
(get (intern-soft function
)
110 'common-lisp-indent-function
)))
112 ;; backwards compatibility
113 (setq method
(get tem
'lisp-indent-function
)))))
115 ;; How far into the containing form is the current form?
116 (if (< (point) indent-point
)
117 (while (condition-case ()
120 (if (>= (point) indent-point
)
122 (parse-partial-sexp (point)
127 (setq path
(cons n path
)))
129 ;; backwards compatibility.
130 (cond ((null function
))
132 (if (null (cdr path
))
133 ;; (package prefix was stripped off above)
134 (setq method
(cond ((string-match "\\`def"
136 '(4 (&whole
4 &rest
1) &body
))
137 ((string-match "\\`\\(with\\|do\\)-"
140 ;; backwards compatibility. Bletch.
142 (setq method
'(4 (&whole
4 &rest
1) &body
))))
144 (cond ((and (memq (char-after (1- containing-sexp
)) '(?
\' ?\
`))
145 (not (eql (char-after (- containing-sexp
2)) ?\
#)))
146 ;; No indentation for "'(...)" elements
147 (setq calculated
(1+ sexp-column
)))
148 ((or (eql (char-after (1- containing-sexp
)) ?\
,)
149 (and (eql (char-after (1- containing-sexp
)) ?\
@)
150 (eql (char-after (- containing-sexp
2)) ?\
,)))
151 ;; ",(...)" or ",@(...)"
152 (setq calculated normal-indent
))
153 ((eql (char-after (1- containing-sexp
)) ?\
#)
155 (setq calculated
(1+ sexp-column
)))
158 ;; convenient top-level hack.
159 ;; (also compatible with lisp-indent-function)
160 ;; The number specifies how many `distinguished'
161 ;; forms there are before the body starts
162 ;; Equivalent to (4 4 ... &body)
163 (setq calculated
(cond ((cdr path
)
165 ((<= (car path
) method
)
166 ;; `distinguished' form
167 (list (+ sexp-column
4)
168 containing-form-start
))
169 ((= (car path
) (1+ method
))
171 (+ sexp-column lisp-body-indent
))
176 (setq calculated
(funcall method
177 path state indent-point
178 sexp-column normal-indent
)))
180 (setq calculated
(lisp-indent-259
181 method path state indent-point
182 sexp-column normal-indent
)))))
183 (goto-char containing-sexp
)
184 (setq last-point containing-sexp
)
187 (progn (backward-up-list 1)
188 (setq depth
(1+ depth
)))
189 (error (setq depth lisp-indent-maximum-backtracking
))))))
193 (defun lisp-indent-report-bad-format (m)
194 (error "%s has a badly-formed %s property: %s"
195 ;; Love those free variable references!!
196 function
'common-lisp-indent-function m
))
198 ;; Blame the crufty control structure on dynamic scoping
200 (defun lisp-indent-259 (method path state indent-point
201 sexp-column normal-indent
)
204 (containing-form-start (elt state
1))
206 ;; Isn't tail-recursion wonderful?
208 ;; This while loop is for destructuring.
209 ;; p is set to (cdr p) each iteration.
210 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
215 ;; This while loop is for advancing along a method
216 ;; until the relevant (possibly &rest/&body) pattern
218 ;; n is set to (1- n) and method to (cdr method)
220 (setq tem
(car method
))
222 (or (eq tem
'nil
) ;default indentation
223 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
224 (and (eq tem
'&body
) (null (cdr method
)))
226 (consp (cdr method
)) (null (cdr (cdr method
))))
227 (integerp tem
) ;explicit indentation specified
228 (and (consp tem
) ;destructuring
229 (eq (car tem
) '&whole
)
230 (or (symbolp (car (cdr tem
)))
231 (integerp (car (cdr tem
)))))
232 (and (symbolp tem
) ;a function to call to do the work.
234 (lisp-indent-report-bad-format method
))
236 (cond ((and tail
(not (consp tem
)))
237 ;; indent tail of &rest in same way as first elt of rest
238 (throw 'exit normal-indent
))
240 ;; &body means (&rest <lisp-body-indent>)
242 (if (and (= n
0) ;first body form
243 (null p
)) ;not in subforms
248 ;; this pattern holds for all remaining forms
251 method
(cdr method
)))
253 ;; try next element of pattern
257 ;; Too few elements in pattern.
258 (throw 'exit normal-indent
)))
260 (throw 'exit
(list normal-indent containing-form-start
)))
262 ; ;; abbrev for (&whole 4 &rest 1)
265 ; (list (+ sexp-column 4) containing-form-start))
268 ; (t normal-indent))))
271 (if (null p
) ;not in subforms
272 (list (+ sexp-column tem
) containing-form-start
)
274 ((symbolp tem
) ;a function to call
276 (funcall tem path state indent-point
277 sexp-column normal-indent
)))
279 ;; must be a destructing frob
282 (setq method
(cdr (cdr tem
))
284 (setq tem
(car (cdr tem
)))
290 containing-form-start
))
292 (list (+ sexp-column tem
)
293 containing-form-start
))
295 (funcall tem path state indent-point
296 sexp-column normal-indent
))))))))))))
298 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
299 (if (not (null (cdr path
)))
302 (goto-char indent-point
)
304 (skip-chars-forward " \t")
305 (list (cond ((looking-at "\\sw\\|\\s_")
307 (+ sexp-column lisp-tag-indentation
))
308 ((integerp lisp-tag-body-indentation
)
309 (+ sexp-column lisp-tag-body-indentation
))
310 ((eq lisp-tag-body-indentation
't
)
312 (progn (backward-sexp 1) (current-column))
313 (error (1+ sexp-column
))))
314 (t (+ sexp-column lisp-body-indent
)))
315 ; (cond ((integerp lisp-tag-body-indentation)
316 ; (+ sexp-column lisp-tag-body-indentation))
317 ; ((eq lisp-tag-body-indentation 't)
320 ; (+ sexp-column lisp-body-indent)))
324 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
325 (if (>= (car path
) 3)
326 (let ((lisp-tag-body-indentation lisp-body-indent
))
327 (funcall (function lisp-indent-tagbody
)
328 path state indent-point sexp-column normal-indent
))
329 (funcall (function lisp-indent-259
)
331 ;; the following causes wierd indentation
334 (&whole nil
&rest
1))
335 path state indent-point sexp-column normal-indent
)))
337 (defun lisp-indent-function-lambda-hack (path state indent-point
338 sexp-column normal-indent
)
339 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
340 (if (or (cdr path
) ; wtf?
342 ;; line up under previous body form
344 ;; line up under function rather than under lambda in order to
345 ;; conserve horizontal space. (Which is what #' is for.)
350 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
351 (+ lisp-body-indent -
1 (current-column))
352 (+ sexp-column lisp-body-indent
)))
353 (error (+ sexp-column lisp-body-indent
)))))
358 (case (4 &rest
(&whole
2 &rest
1)))
359 (ccase . case
) (ecase . case
)
360 (typecase . case
) (etypecase . case
) (ctypecase . case
)
362 (cond (&rest
(&whole
2 &rest
1)))
365 (defconstant . defvar
) (defparameter . defvar
)
369 (4 (&whole
4 &rest
1) &body
))
370 (defsetf (4 (&whole
4 &rest
1) 4 &body
))
371 (defun (4 (&whole
4 &rest
1) &body
))
372 (defmacro . defun
) (deftype . defun
)
373 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
374 &rest
(&whole
2 &rest
1)))
376 ((&whole
6 &rest
1) 4 &body
))
379 (dolist ((&whole
4 2 1) &body
))
382 (flet ((&whole
4 &rest
(&whole
1 (&whole
4 &rest
1) &body
))
388 ;; single-else style (then and else equally indented)
390 ;(lambda ((&whole 4 &rest 1) &body))
391 (lambda ((&whole
4 &rest
1)
392 &rest lisp-indent-function-lambda-hack
))
393 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
395 (compiler-let . let
) ;barf
399 ((&whole
6 &rest
1) 4 &body
))
402 (multiple-value-list 1)
403 (multiple-value-prog1 1)
406 ;; Combines the worst features of BLOCK, LET and TAGBODY
407 (prog ((&whole
4 &rest
1) &rest lisp-indent-tagbody
))
414 (return-from (nil &body
))
415 (tagbody lisp-indent-tagbody
)
422 (put (car (car l
)) 'common-lisp-indent-function
423 (if (symbolp (cdr (car l
)))
424 (get (cdr (car l
)) 'common-lisp-indent-function
)
425 (car (cdr (car l
)))))
440 ; (flet ((foo (bar baz zap)
458 ;(put 'while 'common-lisp-indent-function 1)
459 ;(put 'defwrapper'common-lisp-indent-function ...)
460 ;(put 'def 'common-lisp-indent-function ...)
461 ;(put 'defflavor 'common-lisp-indent-function ...)
462 ;(put 'defsubst 'common-lisp-indent-function ...)
464 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
465 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
466 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
467 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
468 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
472 ;(setq lisp-indent-function 'common-lisp-indent-function)
474 ;; To disable this stuff, (setq lisp-indent-function 'lisp-indent-function)
476 ;;; cl-indent.el ends here