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