Mercurial > emacs
annotate lisp/emacs-lisp/cl-indent.el @ 762:852a2f5838da
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Mon, 13 Jul 1992 18:39:10 +0000 |
parents | 8a533acedb77 |
children | 4f28bd14272c |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
1 ;;; cl-indent.el --- enhanced lisp-indent mode |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
2 |
257 | 3 ;; Copyright (C) 1987 Free Software Foundation, Inc. |
4 ;; Written by Richard Mlynarik July 1987 | |
5 | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 1, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 ;;>> TODO | |
23 ;; :foo | |
24 ;; bar | |
25 ;; :baz | |
26 ;; zap | |
27 ;; &key (like &body)?? | |
28 | |
29 ;; &rest 1 in lambda-lists doesn't work | |
30 ;; -- really want (foo bar | |
31 ;; baz) | |
32 ;; not (foo bar | |
33 ;; baz) | |
34 ;; Need something better than &rest for such cases | |
35 | |
36 | |
37 ;;; Hairy lisp indentation. | |
38 | |
39 (defvar lisp-indent-maximum-backtracking 3 | |
40 "*Maximum depth to backtrack out from a sublist for structured indentation. | |
41 If this variable is 0, no backtracking will occur and forms such as flet | |
42 may not be correctly indented.") | |
43 | |
44 (defvar lisp-tag-indentation 1 | |
45 "*Indentation of tags relative to containing list. | |
46 This variable is used by the function `lisp-indent-tagbody'.") | |
47 | |
48 (defvar lisp-tag-body-indentation 3 | |
49 "*Indentation of non-tagged lines relative to containing list. | |
50 This variable is used by the function `lisp-indent-tagbody' to indent normal | |
51 lines (lines without tags). | |
52 The indentation is relative to the indentation of the parenthesis enclosing | |
53 the special form. If the value is t, the body of tags will be indented | |
54 as a block at the same indentation as the first s-expression following | |
55 the tag. In this case, any forms before the first tag are indented | |
56 by `lisp-body-indent'.") | |
57 | |
58 | |
59 ;;;###autoload | |
60 (defun common-lisp-indent-function (indent-point state) | |
61 (let ((normal-indent (current-column))) | |
62 ;; Walk up list levels until we see something | |
63 ;; which does special things with subforms. | |
64 (let ((depth 0) | |
65 ;; Path describes the position of point in terms of | |
66 ;; list-structure with respect to contining lists. | |
67 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)' | |
68 (path ()) | |
69 ;; set non-nil when somebody works out the indentation to use | |
70 calculated | |
71 (last-point indent-point) | |
72 ;; the position of the open-paren of the innermost containing list | |
73 (containing-form-start (elt state 1)) | |
74 ;; the column of the above | |
75 sexp-column) | |
76 ;; Move to start of innermost containing list | |
77 (goto-char containing-form-start) | |
78 (setq sexp-column (current-column)) | |
79 ;; Look over successively less-deep containing forms | |
80 (while (and (not calculated) | |
81 (< depth lisp-indent-maximum-backtracking)) | |
82 (let ((containing-sexp (point))) | |
83 (forward-char 1) | |
84 (parse-partial-sexp (point) indent-point 1 t) | |
85 ;; Move to the car of the relevant containing form | |
86 (let (tem function method) | |
87 (if (not (looking-at "\\sw\\|\\s_")) | |
88 ;; This form doesn't seem to start with a symbol | |
89 (setq function nil method nil) | |
90 (setq tem (point)) | |
91 (forward-sexp 1) | |
92 (setq function (downcase (buffer-substring tem (point)))) | |
93 (goto-char tem) | |
94 (setq tem (intern-soft function) | |
95 method (get tem 'common-lisp-indent-function)) | |
96 (cond ((and (null method) | |
97 (string-match ":[^:]+" function)) | |
98 ;; The pleblisp package feature | |
99 (setq function (substring function | |
100 (1+ (match-beginning 0))) | |
101 method (get (intern-soft function) | |
102 'common-lisp-indent-function))) | |
103 ((and (null method)) | |
104 ;; backwards compatibility | |
105 (setq method (get tem 'lisp-indent-function))))) | |
106 (let ((n 0)) | |
107 ;; How far into the containing form is the current form? | |
108 (if (< (point) indent-point) | |
109 (while (condition-case () | |
110 (progn | |
111 (forward-sexp 1) | |
112 (if (>= (point) indent-point) | |
113 nil | |
114 (parse-partial-sexp (point) | |
115 indent-point 1 t) | |
116 (setq n (1+ n)) | |
117 t)) | |
118 (error nil)))) | |
119 (setq path (cons n path))) | |
120 | |
121 ;; backwards compatibility. | |
122 (cond ((null function)) | |
123 ((null method) | |
124 (if (null (cdr path)) | |
125 ;; (package prefix was stripped off above) | |
126 (setq method (cond ((string-match "\\`def" | |
127 function) | |
128 '(4 (&whole 4 &rest 1) &body)) | |
129 ((string-match "\\`\\(with\\|do\\)-" | |
130 function) | |
131 '(4 &body)))))) | |
132 ;; backwards compatibility. Bletch. | |
133 ((eq method 'defun) | |
134 (setq method '(4 (&whole 4 &rest 1) &body)))) | |
135 | |
136 (cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`)) | |
137 (not (eql (char-after (- containing-sexp 2)) ?\#))) | |
138 ;; No indentation for "'(...)" elements | |
139 (setq calculated (1+ sexp-column))) | |
140 ((or (eql (char-after (1- containing-sexp)) ?\,) | |
141 (and (eql (char-after (1- containing-sexp)) ?\@) | |
142 (eql (char-after (- containing-sexp 2)) ?\,))) | |
143 ;; ",(...)" or ",@(...)" | |
144 (setq calculated normal-indent)) | |
145 ((eql (char-after (1- containing-sexp)) ?\#) | |
146 ;; "#(...)" | |
147 (setq calculated (1+ sexp-column))) | |
148 ((null method)) | |
149 ((integerp method) | |
150 ;; convenient top-level hack. | |
151 ;; (also compatible with lisp-indent-function) | |
152 ;; The number specifies how many `distinguished' | |
153 ;; forms there are before the body starts | |
154 ;; Equivalent to (4 4 ... &body) | |
155 (setq calculated (cond ((cdr path) | |
156 normal-indent) | |
157 ((<= (car path) method) | |
158 ;; `distinguished' form | |
159 (list (+ sexp-column 4) | |
160 containing-form-start)) | |
161 ((= (car path) (1+ method)) | |
162 ;; first body form. | |
163 (+ sexp-column lisp-body-indent)) | |
164 (t | |
165 ;; other body form | |
166 normal-indent)))) | |
167 ((symbolp method) | |
168 (setq calculated (funcall method | |
169 path state indent-point | |
170 sexp-column normal-indent))) | |
171 (t | |
172 (setq calculated (lisp-indent-259 | |
173 method path state indent-point | |
174 sexp-column normal-indent))))) | |
175 (goto-char containing-sexp) | |
176 (setq last-point containing-sexp) | |
177 (if (not calculated) | |
178 (condition-case () | |
179 (progn (backward-up-list 1) | |
180 (setq depth (1+ depth))) | |
181 (error (setq depth lisp-indent-maximum-backtracking)))))) | |
182 calculated))) | |
183 | |
184 | |
185 (defun lisp-indent-report-bad-format (m) | |
186 (error "%s has a badly-formed %s property: %s" | |
187 ;; Love those free variable references!! | |
188 function 'common-lisp-indent-function m)) | |
189 | |
190 ;; Blame the crufty control structure on dynamic scoping | |
191 ;; -- not on me! | |
192 (defun lisp-indent-259 (method path state indent-point | |
193 sexp-column normal-indent) | |
194 (catch 'exit | |
195 (let ((p path) | |
196 (containing-form-start (elt state 1)) | |
197 n tem tail) | |
198 ;; Isn't tail-recursion wonderful? | |
199 (while p | |
200 ;; This while loop is for destructuring. | |
201 ;; p is set to (cdr p) each iteration. | |
202 (if (not (consp method)) (lisp-indent-report-bad-format method)) | |
203 (setq n (1- (car p)) | |
204 p (cdr p) | |
205 tail nil) | |
206 (while n | |
207 ;; This while loop is for advancing along a method | |
208 ;; until the relevant (possibly &rest/&body) pattern | |
209 ;; is reached. | |
210 ;; n is set to (1- n) and method to (cdr method) | |
211 ;; each iteration. | |
212 (setq tem (car method)) | |
213 | |
214 (or (eq tem 'nil) ;default indentation | |
215 ; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1)) | |
216 (and (eq tem '&body) (null (cdr method))) | |
217 (and (eq tem '&rest) | |
218 (consp (cdr method)) (null (cdr (cdr method)))) | |
219 (integerp tem) ;explicit indentation specified | |
220 (and (consp tem) ;destructuring | |
221 (eq (car tem) '&whole) | |
222 (or (symbolp (car (cdr tem))) | |
223 (integerp (car (cdr tem))))) | |
224 (and (symbolp tem) ;a function to call to do the work. | |
225 (null (cdr method))) | |
226 (lisp-indent-report-bad-format method)) | |
227 | |
228 (cond ((and tail (not (consp tem))) | |
229 ;; indent tail of &rest in same way as first elt of rest | |
230 (throw 'exit normal-indent)) | |
231 ((eq tem '&body) | |
232 ;; &body means (&rest <lisp-body-indent>) | |
233 (throw 'exit | |
234 (if (and (= n 0) ;first body form | |
235 (null p)) ;not in subforms | |
236 (+ sexp-column | |
237 lisp-body-indent) | |
238 normal-indent))) | |
239 ((eq tem '&rest) | |
240 ;; this pattern holds for all remaining forms | |
241 (setq tail (> n 0) | |
242 n 0 | |
243 method (cdr method))) | |
244 ((> n 0) | |
245 ;; try next element of pattern | |
246 (setq n (1- n) | |
247 method (cdr method)) | |
248 (if (< n 0) | |
249 ;; Too few elements in pattern. | |
250 (throw 'exit normal-indent))) | |
251 ((eq tem 'nil) | |
252 (throw 'exit (list normal-indent containing-form-start))) | |
253 ; ((eq tem '&lambda) | |
254 ; ;; abbrev for (&whole 4 &rest 1) | |
255 ; (throw 'exit | |
256 ; (cond ((null p) | |
257 ; (list (+ sexp-column 4) containing-form-start)) | |
258 ; ((null (cdr p)) | |
259 ; (+ sexp-column 1)) | |
260 ; (t normal-indent)))) | |
261 ((integerp tem) | |
262 (throw 'exit | |
263 (if (null p) ;not in subforms | |
264 (list (+ sexp-column tem) containing-form-start) | |
265 normal-indent))) | |
266 ((symbolp tem) ;a function to call | |
267 (throw 'exit | |
268 (funcall tem path state indent-point | |
269 sexp-column normal-indent))) | |
270 (t | |
271 ;; must be a destructing frob | |
272 (if (not (null p)) | |
273 ;; descend | |
274 (setq method (cdr (cdr tem)) | |
275 n nil) | |
276 (setq tem (car (cdr tem))) | |
277 (throw 'exit | |
278 (cond (tail | |
279 normal-indent) | |
280 ((eq tem 'nil) | |
281 (list normal-indent | |
282 containing-form-start)) | |
283 ((integerp tem) | |
284 (list (+ sexp-column tem) | |
285 containing-form-start)) | |
286 (t | |
287 (funcall tem path state indent-point | |
288 sexp-column normal-indent)))))))))))) | |
289 | |
290 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent) | |
291 (if (not (null (cdr path))) | |
292 normal-indent | |
293 (save-excursion | |
294 (goto-char indent-point) | |
295 (beginning-of-line) | |
296 (skip-chars-forward " \t") | |
297 (list (cond ((looking-at "\\sw\\|\\s_") | |
298 ;; a tagbody tag | |
299 (+ sexp-column lisp-tag-indentation)) | |
300 ((integerp lisp-tag-body-indentation) | |
301 (+ sexp-column lisp-tag-body-indentation)) | |
302 ((eq lisp-tag-body-indentation 't) | |
303 (condition-case () | |
304 (progn (backward-sexp 1) (current-column)) | |
305 (error (1+ sexp-column)))) | |
306 (t (+ sexp-column lisp-body-indent))) | |
307 ; (cond ((integerp lisp-tag-body-indentation) | |
308 ; (+ sexp-column lisp-tag-body-indentation)) | |
309 ; ((eq lisp-tag-body-indentation 't) | |
310 ; normal-indent) | |
311 ; (t | |
312 ; (+ sexp-column lisp-body-indent))) | |
313 (elt state 1) | |
314 )))) | |
315 | |
316 (defun lisp-indent-do (path state indent-point sexp-column normal-indent) | |
317 (if (>= (car path) 3) | |
318 (let ((lisp-tag-body-indentation lisp-body-indent)) | |
319 (funcall (function lisp-indent-tagbody) | |
320 path state indent-point sexp-column normal-indent)) | |
321 (funcall (function lisp-indent-259) | |
322 '((&whole nil &rest | |
323 ;; the following causes wierd indentation | |
324 ;;(&whole 1 1 2 nil) | |
325 ) | |
326 (&whole nil &rest 1)) | |
327 path state indent-point sexp-column normal-indent))) | |
328 | |
329 (defun lisp-indent-function-lambda-hack (path state indent-point | |
330 sexp-column normal-indent) | |
331 ;; indent (function (lambda () <newline> <body-forms>)) kludgily. | |
332 (if (or (cdr path) ; wtf? | |
333 (> (car path) 3)) | |
334 ;; line up under previous body form | |
335 normal-indent | |
336 ;; line up under function rather than under lambda in order to | |
337 ;; conserve horizontal space. (Which is what #' is for.) | |
338 (condition-case () | |
339 (save-excursion | |
340 (backward-up-list 2) | |
341 (forward-char 1) | |
342 (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)") | |
343 (+ lisp-body-indent -1 (current-column)) | |
344 (+ sexp-column lisp-body-indent))) | |
345 (error (+ sexp-column lisp-body-indent))))) | |
346 | |
347 | |
348 (let ((l '((block 1) | |
349 (catch 1) | |
350 (case (4 &rest (&whole 2 &rest 1))) | |
351 (ccase . case) (ecase . case) | |
352 (typecase . case) (etypecase . case) (ctypecase . case) | |
353 (catch 1) | |
354 (cond (&rest (&whole 2 &rest 1))) | |
355 (block 1) | |
356 (defvar (4 2 2)) | |
357 (defconstant . defvar) (defparameter . defvar) | |
358 (define-modify-macro | |
359 (4 &body)) | |
360 (define-setf-method | |
361 (4 (&whole 4 &rest 1) &body)) | |
362 (defsetf (4 (&whole 4 &rest 1) 4 &body)) | |
363 (defun (4 (&whole 4 &rest 1) &body)) | |
364 (defmacro . defun) (deftype . defun) | |
365 (defstruct ((&whole 4 &rest (&whole 2 &rest 1)) | |
366 &rest (&whole 2 &rest 1))) | |
367 (destructuring-bind | |
368 ((&whole 6 &rest 1) 4 &body)) | |
369 (do lisp-indent-do) | |
370 (do* . do) | |
371 (dolist ((&whole 4 2 1) &body)) | |
372 (dotimes . dolist) | |
373 (eval-when 1) | |
374 (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body)) | |
375 &body)) | |
376 (labels . flet) | |
377 (macrolet . flet) | |
378 ;; `else-body' style | |
379 (if (nil nil &body)) | |
380 ;; single-else style (then and else equally indented) | |
381 (if (&rest nil)) | |
382 ;(lambda ((&whole 4 &rest 1) &body)) | |
383 (lambda ((&whole 4 &rest 1) | |
384 &rest lisp-indent-function-lambda-hack)) | |
385 (let ((&whole 4 &rest (&whole 1 1 2)) &body)) | |
386 (let* . let) | |
387 (compiler-let . let) ;barf | |
388 (locally 1) | |
389 ;(loop ...) | |
390 (multiple-value-bind | |
391 ((&whole 6 &rest 1) 4 &body)) | |
392 (multiple-value-call | |
393 (4 &body)) | |
394 (multiple-value-list 1) | |
395 (multiple-value-prog1 1) | |
396 (multiple-value-setq | |
397 (4 2)) | |
398 ;; Combines the worst features of BLOCK, LET and TAGBODY | |
399 (prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody)) | |
400 (prog* . prog) | |
401 (prog1 1) | |
402 (prog2 2) | |
403 (progn 0) | |
404 (progv (4 4 &body)) | |
405 (return 0) | |
406 (return-from (nil &body)) | |
407 (tagbody lisp-indent-tagbody) | |
408 (throw 1) | |
409 (unless 1) | |
410 (unwind-protect | |
411 (5 &body)) | |
412 (when 1)))) | |
413 (while l | |
414 (put (car (car l)) 'common-lisp-indent-function | |
415 (if (symbolp (cdr (car l))) | |
416 (get (cdr (car l)) 'common-lisp-indent-function) | |
417 (car (cdr (car l))))) | |
418 (setq l (cdr l)))) | |
419 | |
420 | |
421 ;(defun foo (x) | |
422 ; (tagbody | |
423 ; foo | |
424 ; (bar) | |
425 ; baz | |
426 ; (when (losing) | |
427 ; (with-big-loser | |
428 ; (yow) | |
429 ; ((lambda () | |
430 ; foo) | |
431 ; big))) | |
432 ; (flet ((foo (bar baz zap) | |
433 ; (zip)) | |
434 ; (zot () | |
435 ; quux)) | |
436 ; (do () | |
437 ; ((lose) | |
438 ; (foo 1)) | |
439 ; (quux) | |
440 ; foo | |
441 ; (lose)) | |
442 ; (cond ((x) | |
443 ; (win 1 2 | |
444 ; (foo))) | |
445 ; (t | |
446 ; (lose | |
447 ; 3)))))) | |
448 | |
449 | |
450 ;(put 'while 'common-lisp-indent-function 1) | |
451 ;(put 'defwrapper'common-lisp-indent-function ...) | |
452 ;(put 'def 'common-lisp-indent-function ...) | |
453 ;(put 'defflavor 'common-lisp-indent-function ...) | |
454 ;(put 'defsubst 'common-lisp-indent-function ...) | |
455 | |
456 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body))) | |
457 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1))))) | |
458 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body))) | |
459 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body))) | |
460 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body))))) | |
461 | |
462 | |
463 ;;;; Turn it on. | |
464 ;(setq lisp-indent-function 'common-lisp-indent-function) | |
465 | |
466 ;; To disable this stuff, (setq lisp-indent-function 'lisp-indent-function) | |
467 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
468 ;;; cl-indent.el ends here |