2
|
1 ;;; cl-indent.el --- enhanced lisp-indent mode
|
|
2
|
|
3 ;; Copyright (C) 1987 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
|
|
6 ;; Created: July 1987
|
|
7 ;; Maintainer: FSF
|
|
8 ;; Keywords: lisp, tools
|
0
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
25 ;; 02111-1307, USA.
|
|
26
|
|
27 ;;; Synched up with: FSF 19.34
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
|
31 ;; This package supplies a single entry point, common-lisp-indent-function,
|
|
32 ;; which performs indentation in the preferred style for Common Lisp code.
|
|
33 ;; To enable it:
|
|
34 ;;
|
|
35 ;; (setq lisp-indent-function 'common-lisp-indent-function)
|
0
|
36
|
|
37 ;;>> TODO
|
|
38 ;; :foo
|
|
39 ;; bar
|
|
40 ;; :baz
|
|
41 ;; zap
|
|
42 ;; &key (like &body)??
|
|
43
|
|
44 ;; &rest 1 in lambda-lists doesn't work
|
|
45 ;; -- really want (foo bar
|
|
46 ;; baz)
|
|
47 ;; not (foo bar
|
|
48 ;; baz)
|
|
49 ;; Need something better than &rest for such cases
|
|
50
|
2
|
51 ;;; Code:
|
0
|
52
|
120
|
53 (defgroup lisp-indent nil
|
|
54 "Indentation in Lisp"
|
|
55 :group 'lisp)
|
|
56
|
|
57
|
|
58 (defcustom lisp-indent-maximum-backtracking 3
|
0
|
59 "*Maximum depth to backtrack out from a sublist for structured indentation.
|
2
|
60 If this variable is 0, no backtracking will occur and forms such as flet
|
120
|
61 may not be correctly indented."
|
|
62 :type 'integer
|
|
63 :group 'lisp-indent)
|
0
|
64
|
120
|
65 (defcustom lisp-tag-indentation 1
|
0
|
66 "*Indentation of tags relative to containing list.
|
120
|
67 This variable is used by the function `lisp-indent-tagbody'."
|
|
68 :type 'integer
|
|
69 :group 'lisp-indent)
|
0
|
70
|
120
|
71 (defcustom lisp-tag-body-indentation 3
|
0
|
72 "*Indentation of non-tagged lines relative to containing list.
|
2
|
73 This variable is used by the function `lisp-indent-tagbody' to indent normal
|
0
|
74 lines (lines without tags).
|
|
75 The indentation is relative to the indentation of the parenthesis enclosing
|
2
|
76 the special form. If the value is t, the body of tags will be indented
|
0
|
77 as a block at the same indentation as the first s-expression following
|
|
78 the tag. In this case, any forms before the first tag are indented
|
120
|
79 by `lisp-body-indent'."
|
|
80 :type 'integer
|
|
81 :group 'lisp-indent)
|
0
|
82
|
|
83
|
|
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
|
2
|
91 ;; list-structure with respect to containing lists.
|
0
|
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)) '(?\' ?\`))
|
2
|
162 (not (eql (char-after (- containing-sexp 2)) ?\#)))
|
0
|
163 ;; No indentation for "'(...)" elements
|
|
164 (setq calculated (1+ sexp-column)))
|
2
|
165 ((or (eql (char-after (1- containing-sexp)) ?\,)
|
|
166 (and (eql (char-after (1- containing-sexp)) ?\@)
|
|
167 (eql (char-after (- containing-sexp 2)) ?\,)))
|
0
|
168 ;; ",(...)" or ",@(...)"
|
|
169 (setq calculated normal-indent))
|
2
|
170 ((eql (char-after (1- containing-sexp)) ?\#)
|
0
|
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)
|
|
193 (setq calculated (funcall method
|
|
194 path state indent-point
|
|
195 sexp-column normal-indent)))
|
|
196 (t
|
|
197 (setq calculated (lisp-indent-259
|
|
198 method path state indent-point
|
|
199 sexp-column normal-indent)))))
|
|
200 (goto-char containing-sexp)
|
|
201 (setq last-point containing-sexp)
|
|
202 (if (not calculated)
|
|
203 (condition-case ()
|
|
204 (progn (backward-up-list 1)
|
|
205 (setq depth (1+ depth)))
|
|
206 (error (setq depth lisp-indent-maximum-backtracking))))))
|
|
207 calculated)))
|
|
208
|
|
209
|
|
210 (defun lisp-indent-report-bad-format (m)
|
|
211 (error "%s has a badly-formed %s property: %s"
|
2
|
212 ;; Love those free variable references!!
|
0
|
213 function 'common-lisp-indent-function m))
|
|
214
|
|
215 ;; Blame the crufty control structure on dynamic scoping
|
|
216 ;; -- not on me!
|
|
217 (defun lisp-indent-259 (method path state indent-point
|
|
218 sexp-column normal-indent)
|
|
219 (catch 'exit
|
|
220 (let ((p path)
|
|
221 (containing-form-start (elt state 1))
|
|
222 n tem tail)
|
|
223 ;; Isn't tail-recursion wonderful?
|
|
224 (while p
|
|
225 ;; This while loop is for destructuring.
|
|
226 ;; p is set to (cdr p) each iteration.
|
|
227 (if (not (consp method)) (lisp-indent-report-bad-format method))
|
|
228 (setq n (1- (car p))
|
|
229 p (cdr p)
|
|
230 tail nil)
|
|
231 (while n
|
|
232 ;; This while loop is for advancing along a method
|
|
233 ;; until the relevant (possibly &rest/&body) pattern
|
|
234 ;; is reached.
|
|
235 ;; n is set to (1- n) and method to (cdr method)
|
|
236 ;; each iteration.
|
|
237 (setq tem (car method))
|
|
238
|
|
239 (or (eq tem 'nil) ;default indentation
|
|
240 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
|
|
241 (and (eq tem '&body) (null (cdr method)))
|
|
242 (and (eq tem '&rest)
|
|
243 (consp (cdr method)) (null (cdr (cdr method))))
|
|
244 (integerp tem) ;explicit indentation specified
|
|
245 (and (consp tem) ;destructuring
|
|
246 (eq (car tem) '&whole)
|
|
247 (or (symbolp (car (cdr tem)))
|
|
248 (integerp (car (cdr tem)))))
|
|
249 (and (symbolp tem) ;a function to call to do the work.
|
|
250 (null (cdr method)))
|
|
251 (lisp-indent-report-bad-format method))
|
|
252
|
|
253 (cond ((and tail (not (consp tem)))
|
|
254 ;; indent tail of &rest in same way as first elt of rest
|
|
255 (throw 'exit normal-indent))
|
|
256 ((eq tem '&body)
|
|
257 ;; &body means (&rest <lisp-body-indent>)
|
|
258 (throw 'exit
|
|
259 (if (and (= n 0) ;first body form
|
|
260 (null p)) ;not in subforms
|
|
261 (+ sexp-column
|
|
262 lisp-body-indent)
|
|
263 normal-indent)))
|
|
264 ((eq tem '&rest)
|
|
265 ;; this pattern holds for all remaining forms
|
|
266 (setq tail (> n 0)
|
|
267 n 0
|
|
268 method (cdr method)))
|
|
269 ((> n 0)
|
|
270 ;; try next element of pattern
|
|
271 (setq n (1- n)
|
|
272 method (cdr method))
|
|
273 (if (< n 0)
|
|
274 ;; Too few elements in pattern.
|
|
275 (throw 'exit normal-indent)))
|
|
276 ((eq tem 'nil)
|
|
277 (throw 'exit (list normal-indent containing-form-start)))
|
|
278 ; ((eq tem '&lambda)
|
|
279 ; ;; abbrev for (&whole 4 &rest 1)
|
|
280 ; (throw 'exit
|
|
281 ; (cond ((null p)
|
|
282 ; (list (+ sexp-column 4) containing-form-start))
|
|
283 ; ((null (cdr p))
|
|
284 ; (+ sexp-column 1))
|
|
285 ; (t normal-indent))))
|
|
286 ((integerp tem)
|
|
287 (throw 'exit
|
|
288 (if (null p) ;not in subforms
|
|
289 (list (+ sexp-column tem) containing-form-start)
|
|
290 normal-indent)))
|
|
291 ((symbolp tem) ;a function to call
|
|
292 (throw 'exit
|
|
293 (funcall tem path state indent-point
|
|
294 sexp-column normal-indent)))
|
|
295 (t
|
|
296 ;; must be a destructing frob
|
|
297 (if (not (null p))
|
|
298 ;; descend
|
|
299 (setq method (cdr (cdr tem))
|
|
300 n nil)
|
|
301 (setq tem (car (cdr tem)))
|
|
302 (throw 'exit
|
|
303 (cond (tail
|
|
304 normal-indent)
|
|
305 ((eq tem 'nil)
|
|
306 (list normal-indent
|
|
307 containing-form-start))
|
|
308 ((integerp tem)
|
|
309 (list (+ sexp-column tem)
|
|
310 containing-form-start))
|
|
311 (t
|
|
312 (funcall tem path state indent-point
|
|
313 sexp-column normal-indent))))))))))))
|
|
314
|
|
315 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
|
|
316 (if (not (null (cdr path)))
|
|
317 normal-indent
|
|
318 (save-excursion
|
|
319 (goto-char indent-point)
|
|
320 (beginning-of-line)
|
|
321 (skip-chars-forward " \t")
|
|
322 (list (cond ((looking-at "\\sw\\|\\s_")
|
|
323 ;; a tagbody tag
|
|
324 (+ sexp-column lisp-tag-indentation))
|
|
325 ((integerp lisp-tag-body-indentation)
|
|
326 (+ sexp-column lisp-tag-body-indentation))
|
|
327 ((eq lisp-tag-body-indentation 't)
|
|
328 (condition-case ()
|
|
329 (progn (backward-sexp 1) (current-column))
|
|
330 (error (1+ sexp-column))))
|
|
331 (t (+ sexp-column lisp-body-indent)))
|
|
332 ; (cond ((integerp lisp-tag-body-indentation)
|
|
333 ; (+ sexp-column lisp-tag-body-indentation))
|
|
334 ; ((eq lisp-tag-body-indentation 't)
|
|
335 ; normal-indent)
|
|
336 ; (t
|
|
337 ; (+ sexp-column lisp-body-indent)))
|
|
338 (elt state 1)
|
|
339 ))))
|
|
340
|
|
341 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
|
|
342 (if (>= (car path) 3)
|
|
343 (let ((lisp-tag-body-indentation lisp-body-indent))
|
|
344 (funcall (function lisp-indent-tagbody)
|
|
345 path state indent-point sexp-column normal-indent))
|
|
346 (funcall (function lisp-indent-259)
|
|
347 '((&whole nil &rest
|
2
|
348 ;; the following causes weird indentation
|
0
|
349 ;;(&whole 1 1 2 nil)
|
|
350 )
|
|
351 (&whole nil &rest 1))
|
|
352 path state indent-point sexp-column normal-indent)))
|
|
353
|
|
354 (defun lisp-indent-function-lambda-hack (path state indent-point
|
|
355 sexp-column normal-indent)
|
|
356 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
|
|
357 (if (or (cdr path) ; wtf?
|
|
358 (> (car path) 3))
|
|
359 ;; line up under previous body form
|
|
360 normal-indent
|
|
361 ;; line up under function rather than under lambda in order to
|
|
362 ;; conserve horizontal space. (Which is what #' is for.)
|
|
363 (condition-case ()
|
|
364 (save-excursion
|
|
365 (backward-up-list 2)
|
|
366 (forward-char 1)
|
|
367 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
|
|
368 (+ lisp-body-indent -1 (current-column))
|
|
369 (+ sexp-column lisp-body-indent)))
|
|
370 (error (+ sexp-column lisp-body-indent)))))
|
|
371
|
2
|
372 ;; XEmacs change
|
0
|
373 (defun lisp-indent-defmethod (path state indent-point
|
|
374 sexp-column normal-indent)
|
|
375 ;; Look for a method combination specifier...
|
|
376 (let* ((combined (if (and (>= (car path) 3)
|
|
377 (null (cdr path)))
|
|
378 (save-excursion
|
|
379 (goto-char (car (cdr state)))
|
|
380 (forward-char)
|
|
381 (forward-sexp)
|
|
382 (forward-sexp)
|
|
383 (forward-sexp)
|
|
384 (backward-sexp)
|
|
385 (if (looking-at ":")
|
|
386 t
|
|
387 nil))
|
|
388 nil))
|
|
389 (method (if combined
|
|
390 '(4 4 (&whole 4 &rest 1) &body)
|
|
391 '(4 (&whole 4 &rest 1) &body))))
|
|
392 (funcall (function lisp-indent-259)
|
|
393 method
|
|
394 path state indent-point sexp-column normal-indent)))
|
2
|
395
|
0
|
396
|
|
397 (let ((l '((block 1)
|
|
398 (catch 1)
|
|
399 (case (4 &rest (&whole 2 &rest 1)))
|
|
400 (ccase . case) (ecase . case)
|
|
401 (typecase . case) (etypecase . case) (ctypecase . case)
|
|
402 (catch 1)
|
|
403 (cond (&rest (&whole 2 &rest 1)))
|
|
404 (block 1)
|
|
405 (defvar (4 2 2))
|
|
406 (defconstant . defvar) (defparameter . defvar)
|
|
407 (define-modify-macro
|
|
408 (4 &body))
|
|
409 (define-setf-method
|
|
410 (4 (&whole 4 &rest 1) &body))
|
|
411 (defsetf (4 (&whole 4 &rest 1) 4 &body))
|
|
412 (defun (4 (&whole 4 &rest 1) &body))
|
|
413 (defmacro . defun) (deftype . defun)
|
2
|
414 ;; XEmacs change
|
0
|
415 (defmethod lisp-indent-defmethod)
|
|
416 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
|
|
417 &rest (&whole 2 &rest 1)))
|
|
418 (destructuring-bind
|
|
419 ((&whole 6 &rest 1) 4 &body))
|
|
420 (do lisp-indent-do)
|
|
421 (do* . do)
|
|
422 (dolist ((&whole 4 2 1) &body))
|
|
423 (dotimes . dolist)
|
|
424 (eval-when 1)
|
|
425 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
|
|
426 &body))
|
|
427 (labels . flet)
|
|
428 (macrolet . flet)
|
|
429 ;; `else-body' style
|
|
430 (if (nil nil &body))
|
|
431 ;; single-else style (then and else equally indented)
|
|
432 (if (&rest nil))
|
|
433 ;(lambda ((&whole 4 &rest 1) &body))
|
|
434 (lambda ((&whole 4 &rest 1)
|
|
435 &rest lisp-indent-function-lambda-hack))
|
|
436 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
|
|
437 (let* . let)
|
|
438 (compiler-let . let) ;barf
|
|
439 (locally 1)
|
|
440 ;(loop ...)
|
|
441 (multiple-value-bind
|
|
442 ((&whole 6 &rest 1) 4 &body))
|
|
443 (multiple-value-call
|
|
444 (4 &body))
|
|
445 (multiple-value-list 1)
|
|
446 (multiple-value-prog1 1)
|
|
447 (multiple-value-setq
|
|
448 (4 2))
|
|
449 ;; Combines the worst features of BLOCK, LET and TAGBODY
|
|
450 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
|
|
451 (prog* . prog)
|
|
452 (prog1 1)
|
|
453 (prog2 2)
|
|
454 (progn 0)
|
|
455 (progv (4 4 &body))
|
|
456 (return 0)
|
|
457 (return-from (nil &body))
|
|
458 (tagbody lisp-indent-tagbody)
|
|
459 (throw 1)
|
|
460 (unless 1)
|
|
461 (unwind-protect
|
|
462 (5 &body))
|
|
463 (when 1))))
|
|
464 (while l
|
|
465 (put (car (car l)) 'common-lisp-indent-function
|
|
466 (if (symbolp (cdr (car l)))
|
|
467 (get (cdr (car l)) 'common-lisp-indent-function)
|
|
468 (car (cdr (car l)))))
|
|
469 (setq l (cdr l))))
|
|
470
|
|
471
|
|
472 ;(defun foo (x)
|
|
473 ; (tagbody
|
|
474 ; foo
|
|
475 ; (bar)
|
|
476 ; baz
|
|
477 ; (when (losing)
|
|
478 ; (with-big-loser
|
|
479 ; (yow)
|
|
480 ; ((lambda ()
|
|
481 ; foo)
|
|
482 ; big)))
|
|
483 ; (flet ((foo (bar baz zap)
|
|
484 ; (zip))
|
|
485 ; (zot ()
|
|
486 ; quux))
|
|
487 ; (do ()
|
|
488 ; ((lose)
|
|
489 ; (foo 1))
|
|
490 ; (quux)
|
|
491 ; foo
|
|
492 ; (lose))
|
|
493 ; (cond ((x)
|
|
494 ; (win 1 2
|
|
495 ; (foo)))
|
|
496 ; (t
|
|
497 ; (lose
|
|
498 ; 3))))))
|
|
499
|
|
500
|
|
501 ;(put 'while 'common-lisp-indent-function 1)
|
|
502 ;(put 'defwrapper'common-lisp-indent-function ...)
|
|
503 ;(put 'def 'common-lisp-indent-function ...)
|
|
504 ;(put 'defflavor 'common-lisp-indent-function ...)
|
|
505 ;(put 'defsubst 'common-lisp-indent-function ...)
|
|
506
|
|
507 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
|
|
508 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
|
|
509 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
|
|
510 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
|
|
511 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
|
|
512
|
2
|
513 ;;; cl-indent.el ends here
|