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