(lisp-font-lock-keywords-1): Fontify `defconstant' and `defparameter'.
[bpt/emacs.git] / lisp / emacs-lisp / cl-indent.el
CommitLineData
c0274f38
ER
1;;; cl-indent.el --- enhanced lisp-indent mode
2
9750e079 3;; Copyright (C) 1987 Free Software Foundation, Inc.
e41b2db1 4
a7acbbe4 5;; Author: Richard Mlynarik <mly@eddie.mit.edu>
e41b2db1 6;; Created: July 1987
e5167999 7;; Maintainer: FSF
fd7fa35a 8;; Keywords: lisp, tools
e5167999 9
745bc783
JB
10;; This file is part of GNU Emacs.
11
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
e5167999 14;; the Free Software Foundation; either version 2, or (at your option)
745bc783
JB
15;; any later version.
16
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.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
745bc783 26
e5167999
ER
27;;; Commentary:
28
e41b2db1
ER
29;; This package supplies a single entry point, common-lisp-indent-function,
30;; which performs indentation in the preferred style for Common Lisp code.
31;; To enable it:
32;;
33;; (setq lisp-indent-function 'common-lisp-indent-function)
34
745bc783
JB
35;;>> TODO
36;; :foo
37;; bar
38;; :baz
39;; zap
40;; &key (like &body)??
41
42;; &rest 1 in lambda-lists doesn't work
43;; -- really want (foo bar
44;; baz)
45;; not (foo bar
46;; baz)
47;; Need something better than &rest for such cases
48
e5167999 49;;; Code:
745bc783 50
fcad5199
RS
51(defgroup lisp-indent nil
52 "Indentation in Lisp"
53 :group 'lisp)
54
55
56(defcustom lisp-indent-maximum-backtracking 3
745bc783
JB
57 "*Maximum depth to backtrack out from a sublist for structured indentation.
58If this variable is 0, no backtracking will occur and forms such as flet
fcad5199
RS
59may not be correctly indented."
60 :type 'integer
61 :group 'lisp-indent)
745bc783 62
fcad5199 63(defcustom lisp-tag-indentation 1
745bc783 64 "*Indentation of tags relative to containing list.
fcad5199
RS
65This variable is used by the function `lisp-indent-tagbody'."
66 :type 'integer
67 :group 'lisp-indent)
745bc783 68
fcad5199 69(defcustom lisp-tag-body-indentation 3
745bc783
JB
70 "*Indentation of non-tagged lines relative to containing list.
71This variable is used by the function `lisp-indent-tagbody' to indent normal
72lines (lines without tags).
73The indentation is relative to the indentation of the parenthesis enclosing
74the special form. If the value is t, the body of tags will be indented
75as a block at the same indentation as the first s-expression following
76the tag. In this case, any forms before the first tag are indented
fcad5199
RS
77by `lisp-body-indent'."
78 :type 'integer
79 :group 'lisp-indent)
745bc783
JB
80
81\f
bf92b5a4
RS
82(defvar lisp-indent-error-function)
83
745bc783
JB
84;;;###autoload
85(defun common-lisp-indent-function (indent-point state)
86 (let ((normal-indent (current-column)))
87 ;; Walk up list levels until we see something
88 ;; which does special things with subforms.
89 (let ((depth 0)
90 ;; Path describes the position of point in terms of
eb8c3be9 91 ;; list-structure with respect to containing lists.
745bc783
JB
92 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
93 (path ())
94 ;; set non-nil when somebody works out the indentation to use
95 calculated
96 (last-point indent-point)
97 ;; the position of the open-paren of the innermost containing list
98 (containing-form-start (elt state 1))
99 ;; the column of the above
100 sexp-column)
101 ;; Move to start of innermost containing list
102 (goto-char containing-form-start)
103 (setq sexp-column (current-column))
104 ;; Look over successively less-deep containing forms
105 (while (and (not calculated)
106 (< depth lisp-indent-maximum-backtracking))
107 (let ((containing-sexp (point)))
108 (forward-char 1)
109 (parse-partial-sexp (point) indent-point 1 t)
110 ;; Move to the car of the relevant containing form
111 (let (tem function method)
112 (if (not (looking-at "\\sw\\|\\s_"))
113 ;; This form doesn't seem to start with a symbol
114 (setq function nil method nil)
115 (setq tem (point))
116 (forward-sexp 1)
117 (setq function (downcase (buffer-substring tem (point))))
118 (goto-char tem)
119 (setq tem (intern-soft function)
120 method (get tem 'common-lisp-indent-function))
121 (cond ((and (null method)
122 (string-match ":[^:]+" function))
123 ;; The pleblisp package feature
124 (setq function (substring function
125 (1+ (match-beginning 0)))
126 method (get (intern-soft function)
127 'common-lisp-indent-function)))
128 ((and (null method))
129 ;; backwards compatibility
130 (setq method (get tem 'lisp-indent-function)))))
131 (let ((n 0))
132 ;; How far into the containing form is the current form?
133 (if (< (point) indent-point)
134 (while (condition-case ()
135 (progn
136 (forward-sexp 1)
137 (if (>= (point) indent-point)
138 nil
139 (parse-partial-sexp (point)
140 indent-point 1 t)
141 (setq n (1+ n))
142 t))
143 (error nil))))
144 (setq path (cons n path)))
145
146 ;; backwards compatibility.
147 (cond ((null function))
148 ((null method)
149 (if (null (cdr path))
150 ;; (package prefix was stripped off above)
151 (setq method (cond ((string-match "\\`def"
152 function)
153 '(4 (&whole 4 &rest 1) &body))
154 ((string-match "\\`\\(with\\|do\\)-"
155 function)
156 '(4 &body))))))
157 ;; backwards compatibility. Bletch.
158 ((eq method 'defun)
159 (setq method '(4 (&whole 4 &rest 1) &body))))
160
161 (cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`))
162 (not (eql (char-after (- containing-sexp 2)) ?\#)))
163 ;; No indentation for "'(...)" elements
164 (setq calculated (1+ sexp-column)))
165 ((or (eql (char-after (1- containing-sexp)) ?\,)
166 (and (eql (char-after (1- containing-sexp)) ?\@)
167 (eql (char-after (- containing-sexp 2)) ?\,)))
168 ;; ",(...)" or ",@(...)"
169 (setq calculated normal-indent))
170 ((eql (char-after (1- containing-sexp)) ?\#)
171 ;; "#(...)"
172 (setq calculated (1+ sexp-column)))
173 ((null method))
174 ((integerp method)
175 ;; convenient top-level hack.
176 ;; (also compatible with lisp-indent-function)
177 ;; The number specifies how many `distinguished'
178 ;; forms there are before the body starts
179 ;; Equivalent to (4 4 ... &body)
180 (setq calculated (cond ((cdr path)
181 normal-indent)
182 ((<= (car path) method)
183 ;; `distinguished' form
184 (list (+ sexp-column 4)
185 containing-form-start))
186 ((= (car path) (1+ method))
187 ;; first body form.
188 (+ sexp-column lisp-body-indent))
189 (t
190 ;; other body form
191 normal-indent))))
192 ((symbolp method)
bf92b5a4
RS
193 (let ((lisp-indent-error-function function))
194 (setq calculated (funcall method
195 path state indent-point
196 sexp-column normal-indent))))
745bc783 197 (t
bf92b5a4
RS
198 (let ((lisp-indent-error-function function))
199 (setq calculated (lisp-indent-259
200 method path state indent-point
201 sexp-column normal-indent))))))
745bc783
JB
202 (goto-char containing-sexp)
203 (setq last-point containing-sexp)
204 (if (not calculated)
205 (condition-case ()
206 (progn (backward-up-list 1)
207 (setq depth (1+ depth)))
208 (error (setq depth lisp-indent-maximum-backtracking))))))
209 calculated)))
210
211
212(defun lisp-indent-report-bad-format (m)
213 (error "%s has a badly-formed %s property: %s"
214 ;; Love those free variable references!!
bf92b5a4 215 lisp-indent-error-function 'common-lisp-indent-function m))
745bc783
JB
216
217;; Blame the crufty control structure on dynamic scoping
218;; -- not on me!
219(defun lisp-indent-259 (method path state indent-point
220 sexp-column normal-indent)
221 (catch 'exit
222 (let ((p path)
223 (containing-form-start (elt state 1))
224 n tem tail)
225 ;; Isn't tail-recursion wonderful?
226 (while p
227 ;; This while loop is for destructuring.
228 ;; p is set to (cdr p) each iteration.
229 (if (not (consp method)) (lisp-indent-report-bad-format method))
230 (setq n (1- (car p))
231 p (cdr p)
232 tail nil)
233 (while n
234 ;; This while loop is for advancing along a method
235 ;; until the relevant (possibly &rest/&body) pattern
236 ;; is reached.
237 ;; n is set to (1- n) and method to (cdr method)
238 ;; each iteration.
239 (setq tem (car method))
240
241 (or (eq tem 'nil) ;default indentation
242; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
243 (and (eq tem '&body) (null (cdr method)))
244 (and (eq tem '&rest)
245 (consp (cdr method)) (null (cdr (cdr method))))
246 (integerp tem) ;explicit indentation specified
247 (and (consp tem) ;destructuring
248 (eq (car tem) '&whole)
249 (or (symbolp (car (cdr tem)))
250 (integerp (car (cdr tem)))))
251 (and (symbolp tem) ;a function to call to do the work.
252 (null (cdr method)))
253 (lisp-indent-report-bad-format method))
254
255 (cond ((and tail (not (consp tem)))
256 ;; indent tail of &rest in same way as first elt of rest
257 (throw 'exit normal-indent))
258 ((eq tem '&body)
259 ;; &body means (&rest <lisp-body-indent>)
260 (throw 'exit
261 (if (and (= n 0) ;first body form
262 (null p)) ;not in subforms
263 (+ sexp-column
264 lisp-body-indent)
265 normal-indent)))
266 ((eq tem '&rest)
267 ;; this pattern holds for all remaining forms
268 (setq tail (> n 0)
269 n 0
270 method (cdr method)))
271 ((> n 0)
272 ;; try next element of pattern
273 (setq n (1- n)
274 method (cdr method))
275 (if (< n 0)
276 ;; Too few elements in pattern.
277 (throw 'exit normal-indent)))
278 ((eq tem 'nil)
279 (throw 'exit (list normal-indent containing-form-start)))
280; ((eq tem '&lambda)
281; ;; abbrev for (&whole 4 &rest 1)
282; (throw 'exit
283; (cond ((null p)
284; (list (+ sexp-column 4) containing-form-start))
285; ((null (cdr p))
286; (+ sexp-column 1))
287; (t normal-indent))))
288 ((integerp tem)
289 (throw 'exit
290 (if (null p) ;not in subforms
291 (list (+ sexp-column tem) containing-form-start)
292 normal-indent)))
293 ((symbolp tem) ;a function to call
294 (throw 'exit
295 (funcall tem path state indent-point
296 sexp-column normal-indent)))
297 (t
298 ;; must be a destructing frob
299 (if (not (null p))
300 ;; descend
301 (setq method (cdr (cdr tem))
302 n nil)
303 (setq tem (car (cdr tem)))
304 (throw 'exit
305 (cond (tail
306 normal-indent)
307 ((eq tem 'nil)
308 (list normal-indent
309 containing-form-start))
310 ((integerp tem)
311 (list (+ sexp-column tem)
312 containing-form-start))
313 (t
314 (funcall tem path state indent-point
315 sexp-column normal-indent))))))))))))
316\f
317(defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
318 (if (not (null (cdr path)))
319 normal-indent
320 (save-excursion
321 (goto-char indent-point)
322 (beginning-of-line)
323 (skip-chars-forward " \t")
324 (list (cond ((looking-at "\\sw\\|\\s_")
325 ;; a tagbody tag
326 (+ sexp-column lisp-tag-indentation))
327 ((integerp lisp-tag-body-indentation)
328 (+ sexp-column lisp-tag-body-indentation))
329 ((eq lisp-tag-body-indentation 't)
330 (condition-case ()
331 (progn (backward-sexp 1) (current-column))
332 (error (1+ sexp-column))))
333 (t (+ sexp-column lisp-body-indent)))
334; (cond ((integerp lisp-tag-body-indentation)
335; (+ sexp-column lisp-tag-body-indentation))
336; ((eq lisp-tag-body-indentation 't)
337; normal-indent)
338; (t
339; (+ sexp-column lisp-body-indent)))
340 (elt state 1)
341 ))))
342
343(defun lisp-indent-do (path state indent-point sexp-column normal-indent)
344 (if (>= (car path) 3)
345 (let ((lisp-tag-body-indentation lisp-body-indent))
346 (funcall (function lisp-indent-tagbody)
347 path state indent-point sexp-column normal-indent))
348 (funcall (function lisp-indent-259)
349 '((&whole nil &rest
eb8c3be9 350 ;; the following causes weird indentation
745bc783
JB
351 ;;(&whole 1 1 2 nil)
352 )
353 (&whole nil &rest 1))
354 path state indent-point sexp-column normal-indent)))
355
356(defun lisp-indent-function-lambda-hack (path state indent-point
357 sexp-column normal-indent)
358 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
359 (if (or (cdr path) ; wtf?
360 (> (car path) 3))
361 ;; line up under previous body form
362 normal-indent
363 ;; line up under function rather than under lambda in order to
364 ;; conserve horizontal space. (Which is what #' is for.)
365 (condition-case ()
366 (save-excursion
367 (backward-up-list 2)
368 (forward-char 1)
369 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
370 (+ lisp-body-indent -1 (current-column))
371 (+ sexp-column lisp-body-indent)))
372 (error (+ sexp-column lisp-body-indent)))))
373
374\f
375(let ((l '((block 1)
376 (catch 1)
a63f3864
KH
377 (case (4 &rest (&whole 2 &rest 1)))
378 (ccase . case) (ecase . case)
379 (typecase . case) (etypecase . case) (ctypecase . case)
380 (catch 1)
381 (cond (&rest (&whole 2 &rest 1)))
382 (block 1)
383 (defvar (4 2 2))
7bd1de91
RS
384 (defconstant . defvar)
385 (defparameter . defvar)
a63f3864 386 (define-modify-macro
745bc783 387 (4 &body))
a63f3864
KH
388 (define-setf-method
389 (4 (&whole 4 &rest 1) &body))
390 (defsetf (4 (&whole 4 &rest 1) 4 &body))
391 (defun (4 (&whole 4 &rest 1) &body))
392 (defmacro . defun) (deftype . defun)
8acc018f 393 (defpackage (4 2))
a63f3864
KH
394 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
395 &rest (&whole 2 &rest 1)))
396 (destructuring-bind
397 ((&whole 6 &rest 1) 4 &body))
398 (do lisp-indent-do)
399 (do* . do)
400 (dolist ((&whole 4 2 1) &body))
401 (dotimes . dolist)
402 (eval-when 1)
403 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
404 &body))
405 (labels . flet)
406 (macrolet . flet)
09a4d958
RS
407 (handler-case (4 &rest (&whole 2 (&whole 4 &rest 1) &body)))
408 (restart-case . handler-case)
a63f3864
KH
409 ;; `else-body' style
410 (if (nil nil &body))
411 ;; single-else style (then and else equally indented)
412 (if (&rest nil))
7bd1de91 413 ;; (lambda ((&whole 4 &rest 1) &body))
a63f3864
KH
414 (lambda ((&whole 4 &rest 1)
415 &rest lisp-indent-function-lambda-hack))
416 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
417 (let* . let)
418 (compiler-let . let) ;barf
09a4d958 419 (handler-bind . let) (restart-bind . let)
a63f3864
KH
420 (locally 1)
421 ;(loop ...)
7bd1de91
RS
422 (multiple-value-bind
423 ((&whole 6 &rest 1) 4 &body))
424 (multiple-value-call
425 (4 &body))
a63f3864 426 (multiple-value-prog1 1)
7bd1de91
RS
427 (multiple-value-setq
428 (4 2))
a63f3864
KH
429 (multiple-value-setf . multiple-value-setq)
430 ;; Combines the worst features of BLOCK, LET and TAGBODY
431 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
432 (prog* . prog)
433 (prog1 1)
434 (prog2 2)
435 (progn 0)
436 (progv (4 4 &body))
437 (return 0)
438 (return-from (nil &body))
439 (tagbody lisp-indent-tagbody)
440 (throw 1)
441 (unless 1)
442 (unwind-protect (5 &body))
f0d527fc
RS
443 (when 1)
444 (with-standard-io-syntax (2)))))
745bc783
JB
445 (while l
446 (put (car (car l)) 'common-lisp-indent-function
a63f3864
KH
447 (if (symbolp (cdr (car l)))
448 (get (cdr (car l)) 'common-lisp-indent-function)
449 (car (cdr (car l)))))
745bc783
JB
450 (setq l (cdr l))))
451
452\f
453;(defun foo (x)
454; (tagbody
455; foo
456; (bar)
457; baz
458; (when (losing)
459; (with-big-loser
460; (yow)
461; ((lambda ()
462; foo)
463; big)))
464; (flet ((foo (bar baz zap)
465; (zip))
466; (zot ()
467; quux))
468; (do ()
469; ((lose)
470; (foo 1))
471; (quux)
472; foo
473; (lose))
474; (cond ((x)
475; (win 1 2
476; (foo)))
477; (t
478; (lose
479; 3))))))
480
481
482;(put 'while 'common-lisp-indent-function 1)
483;(put 'defwrapper'common-lisp-indent-function ...)
484;(put 'def 'common-lisp-indent-function ...)
485;(put 'defflavor 'common-lisp-indent-function ...)
486;(put 'defsubst 'common-lisp-indent-function ...)
487
488;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
489;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
490;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
491;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
492;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
493
c0274f38 494;;; cl-indent.el ends here