*** empty log message ***
[bpt/emacs.git] / lisp / emacs-lisp / cl-indent.el
CommitLineData
c0274f38
ER
1;;; cl-indent.el --- enhanced lisp-indent mode
2
e5167999
ER
3;; Author: Richard Mlynark <mly@eddie.mit.edu>
4;; Maintainer: FSF
5;; Last-Modified: 09 May 1991
fd7fa35a 6;; Keywords: lisp, tools
e5167999 7
745bc783
JB
8;; Copyright (C) 1987 Free Software Foundation, Inc.
9;; Written by Richard Mlynarik July 1987
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
e5167999 15;; the Free Software Foundation; either version 2, or (at your option)
745bc783
JB
16;; 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; see the file COPYING. If not, write to
25;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
e5167999
ER
27;;; Commentary:
28
745bc783
JB
29;;>> TODO
30;; :foo
31;; bar
32;; :baz
33;; zap
34;; &key (like &body)??
35
36;; &rest 1 in lambda-lists doesn't work
37;; -- really want (foo bar
38;; baz)
39;; not (foo bar
40;; baz)
41;; Need something better than &rest for such cases
42
e5167999 43;;; Code:
745bc783
JB
44
45;;; Hairy lisp indentation.
46
47(defvar lisp-indent-maximum-backtracking 3
48 "*Maximum depth to backtrack out from a sublist for structured indentation.
49If this variable is 0, no backtracking will occur and forms such as flet
50may not be correctly indented.")
51
52(defvar lisp-tag-indentation 1
53 "*Indentation of tags relative to containing list.
54This variable is used by the function `lisp-indent-tagbody'.")
55
56(defvar lisp-tag-body-indentation 3
57 "*Indentation of non-tagged lines relative to containing list.
58This variable is used by the function `lisp-indent-tagbody' to indent normal
59lines (lines without tags).
60The indentation is relative to the indentation of the parenthesis enclosing
61the special form. If the value is t, the body of tags will be indented
62as a block at the same indentation as the first s-expression following
63the tag. In this case, any forms before the first tag are indented
64by `lisp-body-indent'.")
65
66\f
67;;;###autoload
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.
72 (let ((depth 0)
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)'
76 (path ())
77 ;; set non-nil when somebody works out the indentation to use
78 calculated
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
83 sexp-column)
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)))
91 (forward-char 1)
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)
98 (setq tem (point))
99 (forward-sexp 1)
100 (setq function (downcase (buffer-substring tem (point))))
101 (goto-char tem)
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)))
111 ((and (null method))
112 ;; backwards compatibility
113 (setq method (get tem 'lisp-indent-function)))))
114 (let ((n 0))
115 ;; How far into the containing form is the current form?
116 (if (< (point) indent-point)
117 (while (condition-case ()
118 (progn
119 (forward-sexp 1)
120 (if (>= (point) indent-point)
121 nil
122 (parse-partial-sexp (point)
123 indent-point 1 t)
124 (setq n (1+ n))
125 t))
126 (error nil))))
127 (setq path (cons n path)))
128
129 ;; backwards compatibility.
130 (cond ((null function))
131 ((null method)
132 (if (null (cdr path))
133 ;; (package prefix was stripped off above)
134 (setq method (cond ((string-match "\\`def"
135 function)
136 '(4 (&whole 4 &rest 1) &body))
137 ((string-match "\\`\\(with\\|do\\)-"
138 function)
139 '(4 &body))))))
140 ;; backwards compatibility. Bletch.
141 ((eq method 'defun)
142 (setq method '(4 (&whole 4 &rest 1) &body))))
143
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)) ?\#)
154 ;; "#(...)"
155 (setq calculated (1+ sexp-column)))
156 ((null method))
157 ((integerp method)
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)
164 normal-indent)
165 ((<= (car path) method)
166 ;; `distinguished' form
167 (list (+ sexp-column 4)
168 containing-form-start))
169 ((= (car path) (1+ method))
170 ;; first body form.
171 (+ sexp-column lisp-body-indent))
172 (t
173 ;; other body form
174 normal-indent))))
175 ((symbolp method)
176 (setq calculated (funcall method
177 path state indent-point
178 sexp-column normal-indent)))
179 (t
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)
185 (if (not calculated)
186 (condition-case ()
187 (progn (backward-up-list 1)
188 (setq depth (1+ depth)))
189 (error (setq depth lisp-indent-maximum-backtracking))))))
190 calculated)))
191
192
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))
197
198;; Blame the crufty control structure on dynamic scoping
199;; -- not on me!
200(defun lisp-indent-259 (method path state indent-point
201 sexp-column normal-indent)
202 (catch 'exit
203 (let ((p path)
204 (containing-form-start (elt state 1))
205 n tem tail)
206 ;; Isn't tail-recursion wonderful?
207 (while p
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))
211 (setq n (1- (car p))
212 p (cdr p)
213 tail nil)
214 (while n
215 ;; This while loop is for advancing along a method
216 ;; until the relevant (possibly &rest/&body) pattern
217 ;; is reached.
218 ;; n is set to (1- n) and method to (cdr method)
219 ;; each iteration.
220 (setq tem (car method))
221
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)))
225 (and (eq tem '&rest)
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.
233 (null (cdr method)))
234 (lisp-indent-report-bad-format method))
235
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))
239 ((eq tem '&body)
240 ;; &body means (&rest <lisp-body-indent>)
241 (throw 'exit
242 (if (and (= n 0) ;first body form
243 (null p)) ;not in subforms
244 (+ sexp-column
245 lisp-body-indent)
246 normal-indent)))
247 ((eq tem '&rest)
248 ;; this pattern holds for all remaining forms
249 (setq tail (> n 0)
250 n 0
251 method (cdr method)))
252 ((> n 0)
253 ;; try next element of pattern
254 (setq n (1- n)
255 method (cdr method))
256 (if (< n 0)
257 ;; Too few elements in pattern.
258 (throw 'exit normal-indent)))
259 ((eq tem 'nil)
260 (throw 'exit (list normal-indent containing-form-start)))
261; ((eq tem '&lambda)
262; ;; abbrev for (&whole 4 &rest 1)
263; (throw 'exit
264; (cond ((null p)
265; (list (+ sexp-column 4) containing-form-start))
266; ((null (cdr p))
267; (+ sexp-column 1))
268; (t normal-indent))))
269 ((integerp tem)
270 (throw 'exit
271 (if (null p) ;not in subforms
272 (list (+ sexp-column tem) containing-form-start)
273 normal-indent)))
274 ((symbolp tem) ;a function to call
275 (throw 'exit
276 (funcall tem path state indent-point
277 sexp-column normal-indent)))
278 (t
279 ;; must be a destructing frob
280 (if (not (null p))
281 ;; descend
282 (setq method (cdr (cdr tem))
283 n nil)
284 (setq tem (car (cdr tem)))
285 (throw 'exit
286 (cond (tail
287 normal-indent)
288 ((eq tem 'nil)
289 (list normal-indent
290 containing-form-start))
291 ((integerp tem)
292 (list (+ sexp-column tem)
293 containing-form-start))
294 (t
295 (funcall tem path state indent-point
296 sexp-column normal-indent))))))))))))
297\f
298(defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
299 (if (not (null (cdr path)))
300 normal-indent
301 (save-excursion
302 (goto-char indent-point)
303 (beginning-of-line)
304 (skip-chars-forward " \t")
305 (list (cond ((looking-at "\\sw\\|\\s_")
306 ;; a tagbody tag
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)
311 (condition-case ()
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)
318; normal-indent)
319; (t
320; (+ sexp-column lisp-body-indent)))
321 (elt state 1)
322 ))))
323
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)
330 '((&whole nil &rest
331 ;; the following causes wierd indentation
332 ;;(&whole 1 1 2 nil)
333 )
334 (&whole nil &rest 1))
335 path state indent-point sexp-column normal-indent)))
336
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?
341 (> (car path) 3))
342 ;; line up under previous body form
343 normal-indent
344 ;; line up under function rather than under lambda in order to
345 ;; conserve horizontal space. (Which is what #' is for.)
346 (condition-case ()
347 (save-excursion
348 (backward-up-list 2)
349 (forward-char 1)
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)))))
354
355\f
356(let ((l '((block 1)
357 (catch 1)
358 (case (4 &rest (&whole 2 &rest 1)))
359 (ccase . case) (ecase . case)
360 (typecase . case) (etypecase . case) (ctypecase . case)
361 (catch 1)
362 (cond (&rest (&whole 2 &rest 1)))
363 (block 1)
364 (defvar (4 2 2))
365 (defconstant . defvar) (defparameter . defvar)
366 (define-modify-macro
367 (4 &body))
368 (define-setf-method
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)))
375 (destructuring-bind
376 ((&whole 6 &rest 1) 4 &body))
377 (do lisp-indent-do)
378 (do* . do)
379 (dolist ((&whole 4 2 1) &body))
380 (dotimes . dolist)
381 (eval-when 1)
382 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
383 &body))
384 (labels . flet)
385 (macrolet . flet)
386 ;; `else-body' style
387 (if (nil nil &body))
388 ;; single-else style (then and else equally indented)
389 (if (&rest nil))
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))
394 (let* . let)
395 (compiler-let . let) ;barf
396 (locally 1)
397 ;(loop ...)
398 (multiple-value-bind
399 ((&whole 6 &rest 1) 4 &body))
400 (multiple-value-call
401 (4 &body))
402 (multiple-value-list 1)
403 (multiple-value-prog1 1)
404 (multiple-value-setq
405 (4 2))
406 ;; Combines the worst features of BLOCK, LET and TAGBODY
407 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
408 (prog* . prog)
409 (prog1 1)
410 (prog2 2)
411 (progn 0)
412 (progv (4 4 &body))
413 (return 0)
414 (return-from (nil &body))
415 (tagbody lisp-indent-tagbody)
416 (throw 1)
417 (unless 1)
418 (unwind-protect
419 (5 &body))
420 (when 1))))
421 (while l
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)))))
426 (setq l (cdr l))))
427
428\f
429;(defun foo (x)
430; (tagbody
431; foo
432; (bar)
433; baz
434; (when (losing)
435; (with-big-loser
436; (yow)
437; ((lambda ()
438; foo)
439; big)))
440; (flet ((foo (bar baz zap)
441; (zip))
442; (zot ()
443; quux))
444; (do ()
445; ((lose)
446; (foo 1))
447; (quux)
448; foo
449; (lose))
450; (cond ((x)
451; (win 1 2
452; (foo)))
453; (t
454; (lose
455; 3))))))
456
457
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 ...)
463
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)))))
469
470\f
471;;;; Turn it on.
472;(setq lisp-indent-function 'common-lisp-indent-function)
473
474;; To disable this stuff, (setq lisp-indent-function 'lisp-indent-function)
475
c0274f38 476;;; cl-indent.el ends here