1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987 Free Software Foundation, Inc.
4 ;; Written by Richard Mlynarik July 1987
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 1, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; &key (like &body)??
29 ;; &rest 1 in lambda-lists doesn't work
30 ;; -- really want (foo bar
34 ;; Need something better than &rest for such cases
37 ;;; Hairy lisp indentation.
39 (defvar lisp-indent-maximum-backtracking 3
40 "*Maximum depth to backtrack out from a sublist for structured indentation.
41 If this variable is 0, no backtracking will occur and forms such as flet
42 may not be correctly indented.")
44 (defvar lisp-tag-indentation 1
45 "*Indentation of tags relative to containing list.
46 This variable is used by the function `lisp-indent-tagbody'.")
48 (defvar lisp-tag-body-indentation 3
49 "*Indentation of non-tagged lines relative to containing list.
50 This variable is used by the function `lisp-indent-tagbody' to indent normal
51 lines (lines without tags).
52 The indentation is relative to the indentation of the parenthesis enclosing
53 the special form. If the value is t, the body of tags will be indented
54 as a block at the same indentation as the first s-expression following
55 the tag. In this case, any forms before the first tag are indented
56 by `lisp-body-indent'.")
60 (defun common-lisp-indent-function (indent-point state)
61 (let ((normal-indent (current-column)))
62 ;; Walk up list levels until we see something
63 ;; which does special things with subforms.
65 ;; Path describes the position of point in terms of
66 ;; list-structure with respect to contining lists.
67 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
69 ;; set non-nil when somebody works out the indentation to use
71 (last-point indent-point)
72 ;; the position of the open-paren of the innermost containing list
73 (containing-form-start (elt state 1))
74 ;; the column of the above
76 ;; Move to start of innermost containing list
77 (goto-char containing-form-start)
78 (setq sexp-column (current-column))
79 ;; Look over successively less-deep containing forms
80 (while (and (not calculated)
81 (< depth lisp-indent-maximum-backtracking))
82 (let ((containing-sexp (point)))
84 (parse-partial-sexp (point) indent-point 1 t)
85 ;; Move to the car of the relevant containing form
86 (let (tem function method)
87 (if (not (looking-at "\\sw\\|\\s_"))
88 ;; This form doesn't seem to start with a symbol
89 (setq function nil method nil)
92 (setq function (downcase (buffer-substring tem (point))))
94 (setq tem (intern-soft function)
95 method (get tem 'common-lisp-indent-function))
96 (cond ((and (null method)
97 (string-match ":[^:]+" function))
98 ;; The pleblisp package feature
99 (setq function (substring function
100 (1+ (match-beginning 0)))
101 method (get (intern-soft function)
102 'common-lisp-indent-function)))
104 ;; backwards compatibility
105 (setq method (get tem 'lisp-indent-function)))))
107 ;; How far into the containing form is the current form?
108 (if (< (point) indent-point)
109 (while (condition-case ()
112 (if (>= (point) indent-point)
114 (parse-partial-sexp (point)
119 (setq path (cons n path)))
121 ;; backwards compatibility.
122 (cond ((null function))
124 (if (null (cdr path))
125 ;; (package prefix was stripped off above)
126 (setq method (cond ((string-match "\\`def"
128 '(4 (&whole 4 &rest 1) &body))
129 ((string-match "\\`\\(with\\|do\\)-"
132 ;; backwards compatibility. Bletch.
134 (setq method '(4 (&whole 4 &rest 1) &body))))
136 (cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`))
137 (not (eql (char-after (- containing-sexp 2)) ?\#)))
138 ;; No indentation for "'(...)" elements
139 (setq calculated (1+ sexp-column)))
140 ((or (eql (char-after (1- containing-sexp)) ?\,)
141 (and (eql (char-after (1- containing-sexp)) ?\@)
142 (eql (char-after (- containing-sexp 2)) ?\,)))
143 ;; ",(...)" or ",@(...)"
144 (setq calculated normal-indent))
145 ((eql (char-after (1- containing-sexp)) ?\#)
147 (setq calculated (1+ sexp-column)))
150 ;; convenient top-level hack.
151 ;; (also compatible with lisp-indent-function)
152 ;; The number specifies how many `distinguished'
153 ;; forms there are before the body starts
154 ;; Equivalent to (4 4 ... &body)
155 (setq calculated (cond ((cdr path)
157 ((<= (car path) method)
158 ;; `distinguished' form
159 (list (+ sexp-column 4)
160 containing-form-start))
161 ((= (car path) (1+ method))
163 (+ sexp-column lisp-body-indent))
168 (setq calculated (funcall method
169 path state indent-point
170 sexp-column normal-indent)))
172 (setq calculated (lisp-indent-259
173 method path state indent-point
174 sexp-column normal-indent)))))
175 (goto-char containing-sexp)
176 (setq last-point containing-sexp)
179 (progn (backward-up-list 1)
180 (setq depth (1+ depth)))
181 (error (setq depth lisp-indent-maximum-backtracking))))))
185 (defun lisp-indent-report-bad-format (m)
186 (error "%s has a badly-formed %s property: %s"
187 ;; Love those free variable references!!
188 function 'common-lisp-indent-function m))
190 ;; Blame the crufty control structure on dynamic scoping
192 (defun lisp-indent-259 (method path state indent-point
193 sexp-column normal-indent)
196 (containing-form-start (elt state 1))
198 ;; Isn't tail-recursion wonderful?
200 ;; This while loop is for destructuring.
201 ;; p is set to (cdr p) each iteration.
202 (if (not (consp method)) (lisp-indent-report-bad-format method))
207 ;; This while loop is for advancing along a method
208 ;; until the relevant (possibly &rest/&body) pattern
210 ;; n is set to (1- n) and method to (cdr method)
212 (setq tem (car method))
214 (or (eq tem 'nil) ;default indentation
215 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
216 (and (eq tem '&body) (null (cdr method)))
218 (consp (cdr method)) (null (cdr (cdr method))))
219 (integerp tem) ;explicit indentation specified
220 (and (consp tem) ;destructuring
221 (eq (car tem) '&whole)
222 (or (symbolp (car (cdr tem)))
223 (integerp (car (cdr tem)))))
224 (and (symbolp tem) ;a function to call to do the work.
226 (lisp-indent-report-bad-format method))
228 (cond ((and tail (not (consp tem)))
229 ;; indent tail of &rest in same way as first elt of rest
230 (throw 'exit normal-indent))
232 ;; &body means (&rest <lisp-body-indent>)
234 (if (and (= n 0) ;first body form
235 (null p)) ;not in subforms
240 ;; this pattern holds for all remaining forms
243 method (cdr method)))
245 ;; try next element of pattern
249 ;; Too few elements in pattern.
250 (throw 'exit normal-indent)))
252 (throw 'exit (list normal-indent containing-form-start)))
254 ; ;; abbrev for (&whole 4 &rest 1)
257 ; (list (+ sexp-column 4) containing-form-start))
260 ; (t normal-indent))))
263 (if (null p) ;not in subforms
264 (list (+ sexp-column tem) containing-form-start)
266 ((symbolp tem) ;a function to call
268 (funcall tem path state indent-point
269 sexp-column normal-indent)))
271 ;; must be a destructing frob
274 (setq method (cdr (cdr tem))
276 (setq tem (car (cdr tem)))
282 containing-form-start))
284 (list (+ sexp-column tem)
285 containing-form-start))
287 (funcall tem path state indent-point
288 sexp-column normal-indent))))))))))))
290 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
291 (if (not (null (cdr path)))
294 (goto-char indent-point)
296 (skip-chars-forward " \t")
297 (list (cond ((looking-at "\\sw\\|\\s_")
299 (+ sexp-column lisp-tag-indentation))
300 ((integerp lisp-tag-body-indentation)
301 (+ sexp-column lisp-tag-body-indentation))
302 ((eq lisp-tag-body-indentation 't)
304 (progn (backward-sexp 1) (current-column))
305 (error (1+ sexp-column))))
306 (t (+ sexp-column lisp-body-indent)))
307 ; (cond ((integerp lisp-tag-body-indentation)
308 ; (+ sexp-column lisp-tag-body-indentation))
309 ; ((eq lisp-tag-body-indentation 't)
312 ; (+ sexp-column lisp-body-indent)))
316 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
317 (if (>= (car path) 3)
318 (let ((lisp-tag-body-indentation lisp-body-indent))
319 (funcall (function lisp-indent-tagbody)
320 path state indent-point sexp-column normal-indent))
321 (funcall (function lisp-indent-259)
323 ;; the following causes wierd indentation
326 (&whole nil &rest 1))
327 path state indent-point sexp-column normal-indent)))
329 (defun lisp-indent-function-lambda-hack (path state indent-point
330 sexp-column normal-indent)
331 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
332 (if (or (cdr path) ; wtf?
334 ;; line up under previous body form
336 ;; line up under function rather than under lambda in order to
337 ;; conserve horizontal space. (Which is what #' is for.)
342 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
343 (+ lisp-body-indent -1 (current-column))
344 (+ sexp-column lisp-body-indent)))
345 (error (+ sexp-column lisp-body-indent)))))
350 (case (4 &rest (&whole 2 &rest 1)))
351 (ccase . case) (ecase . case)
352 (typecase . case) (etypecase . case) (ctypecase . case)
354 (cond (&rest (&whole 2 &rest 1)))
357 (defconstant . defvar) (defparameter . defvar)
361 (4 (&whole 4 &rest 1) &body))
362 (defsetf (4 (&whole 4 &rest 1) 4 &body))
363 (defun (4 (&whole 4 &rest 1) &body))
364 (defmacro . defun) (deftype . defun)
365 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
366 &rest (&whole 2 &rest 1)))
368 ((&whole 6 &rest 1) 4 &body))
371 (dolist ((&whole 4 2 1) &body))
374 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
380 ;; single-else style (then and else equally indented)
382 ;(lambda ((&whole 4 &rest 1) &body))
383 (lambda ((&whole 4 &rest 1)
384 &rest lisp-indent-function-lambda-hack))
385 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
387 (compiler-let . let) ;barf
391 ((&whole 6 &rest 1) 4 &body))
394 (multiple-value-list 1)
395 (multiple-value-prog1 1)
398 ;; Combines the worst features of BLOCK, LET and TAGBODY
399 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
406 (return-from (nil &body))
407 (tagbody lisp-indent-tagbody)
414 (put (car (car l)) 'common-lisp-indent-function
415 (if (symbolp (cdr (car l)))
416 (get (cdr (car l)) 'common-lisp-indent-function)
417 (car (cdr (car l)))))
432 ; (flet ((foo (bar baz zap)
450 ;(put 'while 'common-lisp-indent-function 1)
451 ;(put 'defwrapper'common-lisp-indent-function ...)
452 ;(put 'def 'common-lisp-indent-function ...)
453 ;(put 'defflavor 'common-lisp-indent-function ...)
454 ;(put 'defsubst 'common-lisp-indent-function ...)
456 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
457 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
458 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
459 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
460 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
464 ;(setq lisp-indent-function 'common-lisp-indent-function)
466 ;; To disable this stuff, (setq lisp-indent-function 'lisp-indent-function)
468 ;;; cl-indent.el ends here