1 ;;; cl-indent.el --- enhanced lisp-indent mode
3 ;; Copyright (C) 1987 Free Software Foundation, Inc.
4 ;; Written by Richard Mlynarik July 1987
5 ;; Author: Richard Mlynark <mly@eddie.mit.edu>
7 ;; 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.
33 ;; &key (like &body)??
35 ;; &rest 1 in lambda-lists doesn't work
36 ;; -- really want (foo bar
40 ;; Need something better than &rest for such cases
44 ;;; Hairy lisp indentation.
46 (defvar lisp-indent-maximum-backtracking 3
47 "*Maximum depth to backtrack out from a sublist for structured indentation.
48 If this variable is 0, no backtracking will occur and forms such as flet
49 may not be correctly indented.")
51 (defvar lisp-tag-indentation 1
52 "*Indentation of tags relative to containing list.
53 This variable is used by the function `lisp-indent-tagbody'.")
55 (defvar lisp-tag-body-indentation 3
56 "*Indentation of non-tagged lines relative to containing list.
57 This variable is used by the function `lisp-indent-tagbody' to indent normal
58 lines (lines without tags).
59 The indentation is relative to the indentation of the parenthesis enclosing
60 the special form. If the value is t, the body of tags will be indented
61 as a block at the same indentation as the first s-expression following
62 the tag. In this case, any forms before the first tag are indented
63 by `lisp-body-indent'.")
67 (defun common-lisp-indent-function (indent-point state)
68 (let ((normal-indent (current-column)))
69 ;; Walk up list levels until we see something
70 ;; which does special things with subforms.
72 ;; Path describes the position of point in terms of
73 ;; list-structure with respect to contining lists.
74 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
76 ;; set non-nil when somebody works out the indentation to use
78 (last-point indent-point)
79 ;; the position of the open-paren of the innermost containing list
80 (containing-form-start (elt state 1))
81 ;; the column of the above
83 ;; Move to start of innermost containing list
84 (goto-char containing-form-start)
85 (setq sexp-column (current-column))
86 ;; Look over successively less-deep containing forms
87 (while (and (not calculated)
88 (< depth lisp-indent-maximum-backtracking))
89 (let ((containing-sexp (point)))
91 (parse-partial-sexp (point) indent-point 1 t)
92 ;; Move to the car of the relevant containing form
93 (let (tem function method)
94 (if (not (looking-at "\\sw\\|\\s_"))
95 ;; This form doesn't seem to start with a symbol
96 (setq function nil method nil)
99 (setq function (downcase (buffer-substring tem (point))))
101 (setq tem (intern-soft function)
102 method (get tem 'common-lisp-indent-function))
103 (cond ((and (null method)
104 (string-match ":[^:]+" function))
105 ;; The pleblisp package feature
106 (setq function (substring function
107 (1+ (match-beginning 0)))
108 method (get (intern-soft function)
109 'common-lisp-indent-function)))
111 ;; backwards compatibility
112 (setq method (get tem 'lisp-indent-function)))))
114 ;; How far into the containing form is the current form?
115 (if (< (point) indent-point)
116 (while (condition-case ()
119 (if (>= (point) indent-point)
121 (parse-partial-sexp (point)
126 (setq path (cons n path)))
128 ;; backwards compatibility.
129 (cond ((null function))
131 (if (null (cdr path))
132 ;; (package prefix was stripped off above)
133 (setq method (cond ((string-match "\\`def"
135 '(4 (&whole 4 &rest 1) &body))
136 ((string-match "\\`\\(with\\|do\\)-"
139 ;; backwards compatibility. Bletch.
141 (setq method '(4 (&whole 4 &rest 1) &body))))
143 (cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`))
144 (not (eql (char-after (- containing-sexp 2)) ?\#)))
145 ;; No indentation for "'(...)" elements
146 (setq calculated (1+ sexp-column)))
147 ((or (eql (char-after (1- containing-sexp)) ?\,)
148 (and (eql (char-after (1- containing-sexp)) ?\@)
149 (eql (char-after (- containing-sexp 2)) ?\,)))
150 ;; ",(...)" or ",@(...)"
151 (setq calculated normal-indent))
152 ((eql (char-after (1- containing-sexp)) ?\#)
154 (setq calculated (1+ sexp-column)))
157 ;; convenient top-level hack.
158 ;; (also compatible with lisp-indent-function)
159 ;; The number specifies how many `distinguished'
160 ;; forms there are before the body starts
161 ;; Equivalent to (4 4 ... &body)
162 (setq calculated (cond ((cdr path)
164 ((<= (car path) method)
165 ;; `distinguished' form
166 (list (+ sexp-column 4)
167 containing-form-start))
168 ((= (car path) (1+ method))
170 (+ sexp-column lisp-body-indent))
175 (setq calculated (funcall method
176 path state indent-point
177 sexp-column normal-indent)))
179 (setq calculated (lisp-indent-259
180 method path state indent-point
181 sexp-column normal-indent)))))
182 (goto-char containing-sexp)
183 (setq last-point containing-sexp)
186 (progn (backward-up-list 1)
187 (setq depth (1+ depth)))
188 (error (setq depth lisp-indent-maximum-backtracking))))))
192 (defun lisp-indent-report-bad-format (m)
193 (error "%s has a badly-formed %s property: %s"
194 ;; Love those free variable references!!
195 function 'common-lisp-indent-function m))
197 ;; Blame the crufty control structure on dynamic scoping
199 (defun lisp-indent-259 (method path state indent-point
200 sexp-column normal-indent)
203 (containing-form-start (elt state 1))
205 ;; Isn't tail-recursion wonderful?
207 ;; This while loop is for destructuring.
208 ;; p is set to (cdr p) each iteration.
209 (if (not (consp method)) (lisp-indent-report-bad-format method))
214 ;; This while loop is for advancing along a method
215 ;; until the relevant (possibly &rest/&body) pattern
217 ;; n is set to (1- n) and method to (cdr method)
219 (setq tem (car method))
221 (or (eq tem 'nil) ;default indentation
222 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
223 (and (eq tem '&body) (null (cdr method)))
225 (consp (cdr method)) (null (cdr (cdr method))))
226 (integerp tem) ;explicit indentation specified
227 (and (consp tem) ;destructuring
228 (eq (car tem) '&whole)
229 (or (symbolp (car (cdr tem)))
230 (integerp (car (cdr tem)))))
231 (and (symbolp tem) ;a function to call to do the work.
233 (lisp-indent-report-bad-format method))
235 (cond ((and tail (not (consp tem)))
236 ;; indent tail of &rest in same way as first elt of rest
237 (throw 'exit normal-indent))
239 ;; &body means (&rest <lisp-body-indent>)
241 (if (and (= n 0) ;first body form
242 (null p)) ;not in subforms
247 ;; this pattern holds for all remaining forms
250 method (cdr method)))
252 ;; try next element of pattern
256 ;; Too few elements in pattern.
257 (throw 'exit normal-indent)))
259 (throw 'exit (list normal-indent containing-form-start)))
261 ; ;; abbrev for (&whole 4 &rest 1)
264 ; (list (+ sexp-column 4) containing-form-start))
267 ; (t normal-indent))))
270 (if (null p) ;not in subforms
271 (list (+ sexp-column tem) containing-form-start)
273 ((symbolp tem) ;a function to call
275 (funcall tem path state indent-point
276 sexp-column normal-indent)))
278 ;; must be a destructing frob
281 (setq method (cdr (cdr tem))
283 (setq tem (car (cdr tem)))
289 containing-form-start))
291 (list (+ sexp-column tem)
292 containing-form-start))
294 (funcall tem path state indent-point
295 sexp-column normal-indent))))))))))))
297 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
298 (if (not (null (cdr path)))
301 (goto-char indent-point)
303 (skip-chars-forward " \t")
304 (list (cond ((looking-at "\\sw\\|\\s_")
306 (+ sexp-column lisp-tag-indentation))
307 ((integerp lisp-tag-body-indentation)
308 (+ sexp-column lisp-tag-body-indentation))
309 ((eq lisp-tag-body-indentation 't)
311 (progn (backward-sexp 1) (current-column))
312 (error (1+ sexp-column))))
313 (t (+ sexp-column lisp-body-indent)))
314 ; (cond ((integerp lisp-tag-body-indentation)
315 ; (+ sexp-column lisp-tag-body-indentation))
316 ; ((eq lisp-tag-body-indentation 't)
319 ; (+ sexp-column lisp-body-indent)))
323 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
324 (if (>= (car path) 3)
325 (let ((lisp-tag-body-indentation lisp-body-indent))
326 (funcall (function lisp-indent-tagbody)
327 path state indent-point sexp-column normal-indent))
328 (funcall (function lisp-indent-259)
330 ;; the following causes wierd indentation
333 (&whole nil &rest 1))
334 path state indent-point sexp-column normal-indent)))
336 (defun lisp-indent-function-lambda-hack (path state indent-point
337 sexp-column normal-indent)
338 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
339 (if (or (cdr path) ; wtf?
341 ;; line up under previous body form
343 ;; line up under function rather than under lambda in order to
344 ;; conserve horizontal space. (Which is what #' is for.)
349 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
350 (+ lisp-body-indent -1 (current-column))
351 (+ sexp-column lisp-body-indent)))
352 (error (+ sexp-column lisp-body-indent)))))
357 (case (4 &rest (&whole 2 &rest 1)))
358 (ccase . case) (ecase . case)
359 (typecase . case) (etypecase . case) (ctypecase . case)
361 (cond (&rest (&whole 2 &rest 1)))
364 (defconstant . defvar) (defparameter . defvar)
368 (4 (&whole 4 &rest 1) &body))
369 (defsetf (4 (&whole 4 &rest 1) 4 &body))
370 (defun (4 (&whole 4 &rest 1) &body))
371 (defmacro . defun) (deftype . defun)
372 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
373 &rest (&whole 2 &rest 1)))
375 ((&whole 6 &rest 1) 4 &body))
378 (dolist ((&whole 4 2 1) &body))
381 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
387 ;; single-else style (then and else equally indented)
389 ;(lambda ((&whole 4 &rest 1) &body))
390 (lambda ((&whole 4 &rest 1)
391 &rest lisp-indent-function-lambda-hack))
392 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
394 (compiler-let . let) ;barf
398 ((&whole 6 &rest 1) 4 &body))
401 (multiple-value-list 1)
402 (multiple-value-prog1 1)
405 ;; Combines the worst features of BLOCK, LET and TAGBODY
406 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
413 (return-from (nil &body))
414 (tagbody lisp-indent-tagbody)
421 (put (car (car l)) 'common-lisp-indent-function
422 (if (symbolp (cdr (car l)))
423 (get (cdr (car l)) 'common-lisp-indent-function)
424 (car (cdr (car l)))))
439 ; (flet ((foo (bar baz zap)
457 ;(put 'while 'common-lisp-indent-function 1)
458 ;(put 'defwrapper'common-lisp-indent-function ...)
459 ;(put 'def 'common-lisp-indent-function ...)
460 ;(put 'defflavor 'common-lisp-indent-function ...)
461 ;(put 'defsubst 'common-lisp-indent-function ...)
463 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
464 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
465 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
466 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
467 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
471 ;(setq lisp-indent-function 'common-lisp-indent-function)
473 ;; To disable this stuff, (setq lisp-indent-function 'lisp-indent-function)
475 ;;; cl-indent.el ends here