1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynark <mly@eddie.mit.edu>
8 ;; Keywords: lisp, tools
10 ;; This file is part of GNU Emacs.
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)
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.
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; This package supplies a single entry point, common-lisp-indent-function,
29 ;; which performs indentation in the preferred style for Common Lisp code.
32 ;; (setq lisp-indent-function 'common-lisp-indent-function)
39 ;; &key (like &body)??
41 ;; &rest 1 in lambda-lists doesn't work
42 ;; -- really want (foo bar
46 ;; Need something better than &rest for such cases
50 (defvar lisp-indent-maximum-backtracking 3
51 "*Maximum depth to backtrack out from a sublist for structured indentation.
52 If this variable is 0, no backtracking will occur and forms such as flet
53 may not be correctly indented.")
55 (defvar lisp-tag-indentation 1
56 "*Indentation of tags relative to containing list.
57 This variable is used by the function `lisp-indent-tagbody'.")
59 (defvar lisp-tag-body-indentation 3
60 "*Indentation of non-tagged lines relative to containing list.
61 This variable is used by the function `lisp-indent-tagbody' to indent normal
62 lines (lines without tags).
63 The indentation is relative to the indentation of the parenthesis enclosing
64 the special form. If the value is t, the body of tags will be indented
65 as a block at the same indentation as the first s-expression following
66 the tag. In this case, any forms before the first tag are indented
67 by `lisp-body-indent'.")
71 (defun common-lisp-indent-function (indent-point state)
72 (let ((normal-indent (current-column)))
73 ;; Walk up list levels until we see something
74 ;; which does special things with subforms.
76 ;; Path describes the position of point in terms of
77 ;; list-structure with respect to contining lists.
78 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
80 ;; set non-nil when somebody works out the indentation to use
82 (last-point indent-point)
83 ;; the position of the open-paren of the innermost containing list
84 (containing-form-start (elt state 1))
85 ;; the column of the above
87 ;; Move to start of innermost containing list
88 (goto-char containing-form-start)
89 (setq sexp-column (current-column))
90 ;; Look over successively less-deep containing forms
91 (while (and (not calculated)
92 (< depth lisp-indent-maximum-backtracking))
93 (let ((containing-sexp (point)))
95 (parse-partial-sexp (point) indent-point 1 t)
96 ;; Move to the car of the relevant containing form
97 (let (tem function method)
98 (if (not (looking-at "\\sw\\|\\s_"))
99 ;; This form doesn't seem to start with a symbol
100 (setq function nil method nil)
103 (setq function (downcase (buffer-substring tem (point))))
105 (setq tem (intern-soft function)
106 method (get tem 'common-lisp-indent-function))
107 (cond ((and (null method)
108 (string-match ":[^:]+" function))
109 ;; The pleblisp package feature
110 (setq function (substring function
111 (1+ (match-beginning 0)))
112 method (get (intern-soft function)
113 'common-lisp-indent-function)))
115 ;; backwards compatibility
116 (setq method (get tem 'lisp-indent-function)))))
118 ;; How far into the containing form is the current form?
119 (if (< (point) indent-point)
120 (while (condition-case ()
123 (if (>= (point) indent-point)
125 (parse-partial-sexp (point)
130 (setq path (cons n path)))
132 ;; backwards compatibility.
133 (cond ((null function))
135 (if (null (cdr path))
136 ;; (package prefix was stripped off above)
137 (setq method (cond ((string-match "\\`def"
139 '(4 (&whole 4 &rest 1) &body))
140 ((string-match "\\`\\(with\\|do\\)-"
143 ;; backwards compatibility. Bletch.
145 (setq method '(4 (&whole 4 &rest 1) &body))))
147 (cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`))
148 (not (eql (char-after (- containing-sexp 2)) ?\#)))
149 ;; No indentation for "'(...)" elements
150 (setq calculated (1+ sexp-column)))
151 ((or (eql (char-after (1- containing-sexp)) ?\,)
152 (and (eql (char-after (1- containing-sexp)) ?\@)
153 (eql (char-after (- containing-sexp 2)) ?\,)))
154 ;; ",(...)" or ",@(...)"
155 (setq calculated normal-indent))
156 ((eql (char-after (1- containing-sexp)) ?\#)
158 (setq calculated (1+ sexp-column)))
161 ;; convenient top-level hack.
162 ;; (also compatible with lisp-indent-function)
163 ;; The number specifies how many `distinguished'
164 ;; forms there are before the body starts
165 ;; Equivalent to (4 4 ... &body)
166 (setq calculated (cond ((cdr path)
168 ((<= (car path) method)
169 ;; `distinguished' form
170 (list (+ sexp-column 4)
171 containing-form-start))
172 ((= (car path) (1+ method))
174 (+ sexp-column lisp-body-indent))
179 (setq calculated (funcall method
180 path state indent-point
181 sexp-column normal-indent)))
183 (setq calculated (lisp-indent-259
184 method path state indent-point
185 sexp-column normal-indent)))))
186 (goto-char containing-sexp)
187 (setq last-point containing-sexp)
190 (progn (backward-up-list 1)
191 (setq depth (1+ depth)))
192 (error (setq depth lisp-indent-maximum-backtracking))))))
196 (defun lisp-indent-report-bad-format (m)
197 (error "%s has a badly-formed %s property: %s"
198 ;; Love those free variable references!!
199 function 'common-lisp-indent-function m))
201 ;; Blame the crufty control structure on dynamic scoping
203 (defun lisp-indent-259 (method path state indent-point
204 sexp-column normal-indent)
207 (containing-form-start (elt state 1))
209 ;; Isn't tail-recursion wonderful?
211 ;; This while loop is for destructuring.
212 ;; p is set to (cdr p) each iteration.
213 (if (not (consp method)) (lisp-indent-report-bad-format method))
218 ;; This while loop is for advancing along a method
219 ;; until the relevant (possibly &rest/&body) pattern
221 ;; n is set to (1- n) and method to (cdr method)
223 (setq tem (car method))
225 (or (eq tem 'nil) ;default indentation
226 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
227 (and (eq tem '&body) (null (cdr method)))
229 (consp (cdr method)) (null (cdr (cdr method))))
230 (integerp tem) ;explicit indentation specified
231 (and (consp tem) ;destructuring
232 (eq (car tem) '&whole)
233 (or (symbolp (car (cdr tem)))
234 (integerp (car (cdr tem)))))
235 (and (symbolp tem) ;a function to call to do the work.
237 (lisp-indent-report-bad-format method))
239 (cond ((and tail (not (consp tem)))
240 ;; indent tail of &rest in same way as first elt of rest
241 (throw 'exit normal-indent))
243 ;; &body means (&rest <lisp-body-indent>)
245 (if (and (= n 0) ;first body form
246 (null p)) ;not in subforms
251 ;; this pattern holds for all remaining forms
254 method (cdr method)))
256 ;; try next element of pattern
260 ;; Too few elements in pattern.
261 (throw 'exit normal-indent)))
263 (throw 'exit (list normal-indent containing-form-start)))
265 ; ;; abbrev for (&whole 4 &rest 1)
268 ; (list (+ sexp-column 4) containing-form-start))
271 ; (t normal-indent))))
274 (if (null p) ;not in subforms
275 (list (+ sexp-column tem) containing-form-start)
277 ((symbolp tem) ;a function to call
279 (funcall tem path state indent-point
280 sexp-column normal-indent)))
282 ;; must be a destructing frob
285 (setq method (cdr (cdr tem))
287 (setq tem (car (cdr tem)))
293 containing-form-start))
295 (list (+ sexp-column tem)
296 containing-form-start))
298 (funcall tem path state indent-point
299 sexp-column normal-indent))))))))))))
301 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
302 (if (not (null (cdr path)))
305 (goto-char indent-point)
307 (skip-chars-forward " \t")
308 (list (cond ((looking-at "\\sw\\|\\s_")
310 (+ sexp-column lisp-tag-indentation))
311 ((integerp lisp-tag-body-indentation)
312 (+ sexp-column lisp-tag-body-indentation))
313 ((eq lisp-tag-body-indentation 't)
315 (progn (backward-sexp 1) (current-column))
316 (error (1+ sexp-column))))
317 (t (+ sexp-column lisp-body-indent)))
318 ; (cond ((integerp lisp-tag-body-indentation)
319 ; (+ sexp-column lisp-tag-body-indentation))
320 ; ((eq lisp-tag-body-indentation 't)
323 ; (+ sexp-column lisp-body-indent)))
327 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
328 (if (>= (car path) 3)
329 (let ((lisp-tag-body-indentation lisp-body-indent))
330 (funcall (function lisp-indent-tagbody)
331 path state indent-point sexp-column normal-indent))
332 (funcall (function lisp-indent-259)
334 ;; the following causes wierd indentation
337 (&whole nil &rest 1))
338 path state indent-point sexp-column normal-indent)))
340 (defun lisp-indent-function-lambda-hack (path state indent-point
341 sexp-column normal-indent)
342 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
343 (if (or (cdr path) ; wtf?
345 ;; line up under previous body form
347 ;; line up under function rather than under lambda in order to
348 ;; conserve horizontal space. (Which is what #' is for.)
353 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
354 (+ lisp-body-indent -1 (current-column))
355 (+ sexp-column lisp-body-indent)))
356 (error (+ sexp-column lisp-body-indent)))))
361 (case (4 &rest (&whole 2 &rest 1)))
362 (ccase . case) (ecase . case)
363 (typecase . case) (etypecase . case) (ctypecase . case)
365 (cond (&rest (&whole 2 &rest 1)))
368 (defconstant . defvar) (defparameter . defvar)
372 (4 (&whole 4 &rest 1) &body))
373 (defsetf (4 (&whole 4 &rest 1) 4 &body))
374 (defun (4 (&whole 4 &rest 1) &body))
375 (defmacro . defun) (deftype . defun)
376 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
377 &rest (&whole 2 &rest 1)))
379 ((&whole 6 &rest 1) 4 &body))
382 (dolist ((&whole 4 2 1) &body))
385 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
391 ;; single-else style (then and else equally indented)
393 ;(lambda ((&whole 4 &rest 1) &body))
394 (lambda ((&whole 4 &rest 1)
395 &rest lisp-indent-function-lambda-hack))
396 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
398 (compiler-let . let) ;barf
402 ((&whole 6 &rest 1) 4 &body))
405 (multiple-value-list 1)
406 (multiple-value-prog1 1)
409 ;; Combines the worst features of BLOCK, LET and TAGBODY
410 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
417 (return-from (nil &body))
418 (tagbody lisp-indent-tagbody)
425 (put (car (car l)) 'common-lisp-indent-function
426 (if (symbolp (cdr (car l)))
427 (get (cdr (car l)) 'common-lisp-indent-function)
428 (car (cdr (car l)))))
443 ; (flet ((foo (bar baz zap)
461 ;(put 'while 'common-lisp-indent-function 1)
462 ;(put 'defwrapper'common-lisp-indent-function ...)
463 ;(put 'def 'common-lisp-indent-function ...)
464 ;(put 'defflavor 'common-lisp-indent-function ...)
465 ;(put 'defsubst 'common-lisp-indent-function ...)
467 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
468 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
469 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
470 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
471 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
473 ;;; cl-indent.el ends here