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