1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This package supplies a single entry point, common-lisp-indent-function,
30 ;; which performs indentation in the preferred style for Common Lisp code.
33 ;; (setq lisp-indent-function 'common-lisp-indent-function)
40 ;; &key (like &body)??
42 ;; &rest 1 in lambda-lists doesn't work
43 ;; -- really want (foo bar
47 ;; Need something better than &rest for such cases
51 (defgroup lisp-indent nil
56 (defcustom lisp-indent-maximum-backtracking 3
57 "*Maximum depth to backtrack out from a sublist for structured indentation.
58 If this variable is 0, no backtracking will occur and forms such as flet
59 may not be correctly indented."
63 (defcustom lisp-tag-indentation 1
64 "*Indentation of tags relative to containing list.
65 This variable is used by the function `lisp-indent-tagbody'."
69 (defcustom lisp-tag-body-indentation 3
70 "*Indentation of non-tagged lines relative to containing list.
71 This variable is used by the function `lisp-indent-tagbody' to indent normal
72 lines (lines without tags).
73 The indentation is relative to the indentation of the parenthesis enclosing
74 the special form. If the value is t, the body of tags will be indented
75 as a block at the same indentation as the first s-expression following
76 the tag. In this case, any forms before the first tag are indented
77 by `lisp-body-indent'."
82 (defvar lisp-indent-error-function)
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.
90 ;; Path describes the position of point in terms of
91 ;; list-structure with respect to containing lists.
92 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
94 ;; set non-nil when somebody works out the indentation to use
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
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)))
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)
117 (setq function (downcase (buffer-substring tem (point))))
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)))
129 ;; backwards compatibility
130 (setq method (get tem 'lisp-indent-function)))))
132 ;; How far into the containing form is the current form?
133 (if (< (point) indent-point)
134 (while (condition-case ()
137 (if (>= (point) indent-point)
139 (parse-partial-sexp (point)
144 (setq path (cons n path)))
146 ;; backwards compatibility.
147 (cond ((null function))
149 (if (null (cdr path))
150 ;; (package prefix was stripped off above)
151 (setq method (cond ((string-match "\\`def"
153 '(4 (&whole 4 &rest 1) &body))
154 ((string-match "\\`\\(with\\|do\\)-"
157 ;; backwards compatibility. Bletch.
159 (setq method '(4 (&whole 4 &rest 1) &body))))
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)) ?\#)
172 (setq calculated (1+ sexp-column)))
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)
182 ((<= (car path) method)
183 ;; `distinguished' form
184 (list (+ sexp-column 4)
185 containing-form-start))
186 ((= (car path) (1+ method))
188 (+ sexp-column lisp-body-indent))
193 (let ((lisp-indent-error-function function))
194 (setq calculated (funcall method
195 path state indent-point
196 sexp-column normal-indent))))
198 (let ((lisp-indent-error-function function))
199 (setq calculated (lisp-indent-259
200 method path state indent-point
201 sexp-column normal-indent))))))
202 (goto-char containing-sexp)
203 (setq last-point containing-sexp)
206 (progn (backward-up-list 1)
207 (setq depth (1+ depth)))
208 (error (setq depth lisp-indent-maximum-backtracking))))))
212 (defun lisp-indent-report-bad-format (m)
213 (error "%s has a badly-formed %s property: %s"
214 ;; Love those free variable references!!
215 lisp-indent-error-function 'common-lisp-indent-function m))
217 ;; Blame the crufty control structure on dynamic scoping
219 (defun lisp-indent-259 (method path state indent-point
220 sexp-column normal-indent)
223 (containing-form-start (elt state 1))
225 ;; Isn't tail-recursion wonderful?
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))
234 ;; This while loop is for advancing along a method
235 ;; until the relevant (possibly &rest/&body) pattern
237 ;; n is set to (1- n) and method to (cdr method)
239 (setq tem (car method))
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)))
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.
253 (lisp-indent-report-bad-format method))
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))
259 ;; &body means (&rest <lisp-body-indent>)
261 (if (and (= n 0) ;first body form
262 (null p)) ;not in subforms
267 ;; this pattern holds for all remaining forms
270 method (cdr method)))
272 ;; try next element of pattern
276 ;; Too few elements in pattern.
277 (throw 'exit normal-indent)))
279 (throw 'exit (list normal-indent containing-form-start)))
281 ; ;; abbrev for (&whole 4 &rest 1)
284 ; (list (+ sexp-column 4) containing-form-start))
287 ; (t normal-indent))))
290 (if (null p) ;not in subforms
291 (list (+ sexp-column tem) containing-form-start)
293 ((symbolp tem) ;a function to call
295 (funcall tem path state indent-point
296 sexp-column normal-indent)))
298 ;; must be a destructing frob
301 (setq method (cdr (cdr tem))
303 (setq tem (car (cdr tem)))
309 containing-form-start))
311 (list (+ sexp-column tem)
312 containing-form-start))
314 (funcall tem path state indent-point
315 sexp-column normal-indent))))))))))))
317 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
318 (if (not (null (cdr path)))
321 (goto-char indent-point)
323 (skip-chars-forward " \t")
324 (list (cond ((looking-at "\\sw\\|\\s_")
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)
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)
339 ; (+ sexp-column lisp-body-indent)))
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)
350 ;; the following causes weird indentation
353 (&whole nil &rest 1))
354 path state indent-point sexp-column normal-indent)))
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?
361 ;; line up under previous body form
363 ;; line up under function rather than under lambda in order to
364 ;; conserve horizontal space. (Which is what #' is for.)
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)))))
377 (case (4 &rest (&whole 2 &rest 1)))
378 (ccase . case) (ecase . case)
379 (typecase . case) (etypecase . case) (ctypecase . case)
381 (cond (&rest (&whole 2 &rest 1)))
384 (defconstant . defvar)
385 (defparameter . defvar)
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)
394 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
395 &rest (&whole 2 &rest 1)))
397 ((&whole 6 &rest 1) 4 &body))
400 (dolist ((&whole 4 2 1) &body))
403 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
409 ;; single-else style (then and else equally indented)
411 ;; (lambda ((&whole 4 &rest 1) &body))
412 (lambda ((&whole 4 &rest 1)
413 &rest lisp-indent-function-lambda-hack))
414 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
416 (compiler-let . let) ;barf
420 ((&whole 6 &rest 1) 4 &body))
423 (multiple-value-prog1 1)
426 (multiple-value-setf . multiple-value-setq)
427 ;; Combines the worst features of BLOCK, LET and TAGBODY
428 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
435 (return-from (nil &body))
436 (tagbody lisp-indent-tagbody)
439 (unwind-protect (5 &body))
441 (with-standard-io-syntax (2)))))
443 (put (car (car l)) 'common-lisp-indent-function
444 (if (symbolp (cdr (car l)))
445 (get (cdr (car l)) 'common-lisp-indent-function)
446 (car (cdr (car l)))))
461 ; (flet ((foo (bar baz zap)
479 ;(put 'while 'common-lisp-indent-function 1)
480 ;(put 'defwrapper'common-lisp-indent-function ...)
481 ;(put 'def 'common-lisp-indent-function ...)
482 ;(put 'defflavor 'common-lisp-indent-function ...)
483 ;(put 'defsubst 'common-lisp-indent-function ...)
485 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
486 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
487 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
488 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
489 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
491 ;;; cl-indent.el ends here