Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; cl-indent.el --- enhanced lisp-indent mode |
2 | ||
ba318903 | 3 | ;; Copyright (C) 1987, 2000-2014 Free Software Foundation, Inc. |
e41b2db1 | 4 | |
a7acbbe4 | 5 | ;; Author: Richard Mlynarik <mly@eddie.mit.edu> |
e41b2db1 | 6 | ;; Created: July 1987 |
34dc21db | 7 | ;; Maintainer: emacs-devel@gnu.org |
fd7fa35a | 8 | ;; Keywords: lisp, tools |
bd78fa1d | 9 | ;; Package: emacs |
e5167999 | 10 | |
745bc783 JB |
11 | ;; This file is part of GNU Emacs. |
12 | ||
d6cba7ae | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
745bc783 | 14 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
745bc783 JB |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
745bc783 | 25 | |
e5167999 ER |
26 | ;;; Commentary: |
27 | ||
e41b2db1 ER |
28 | ;; This package supplies a single entry point, common-lisp-indent-function, |
29 | ;; which performs indentation in the preferred style for Common Lisp code. | |
30 | ;; To enable it: | |
31 | ;; | |
32 | ;; (setq lisp-indent-function 'common-lisp-indent-function) | |
33 | ||
e5167999 | 34 | ;;; Code: |
745bc783 | 35 | |
8497a297 DV |
36 | (eval-when-compile (require 'cl)) |
37 | ||
fcad5199 | 38 | (defgroup lisp-indent nil |
ec0421f3 | 39 | "Indentation in Lisp." |
fcad5199 RS |
40 | :group 'lisp) |
41 | ||
42 | ||
43 | (defcustom lisp-indent-maximum-backtracking 3 | |
c66cd0ff | 44 | "Maximum depth to backtrack out from a sublist for structured indentation. |
ec0421f3 | 45 | If this variable is 0, no backtracking will occur and forms such as `flet' |
fcad5199 RS |
46 | may not be correctly indented." |
47 | :type 'integer | |
48 | :group 'lisp-indent) | |
745bc783 | 49 | |
fcad5199 | 50 | (defcustom lisp-tag-indentation 1 |
c66cd0ff | 51 | "Indentation of tags relative to containing list. |
fcad5199 RS |
52 | This variable is used by the function `lisp-indent-tagbody'." |
53 | :type 'integer | |
54 | :group 'lisp-indent) | |
745bc783 | 55 | |
fcad5199 | 56 | (defcustom lisp-tag-body-indentation 3 |
c66cd0ff | 57 | "Indentation of non-tagged lines relative to containing list. |
745bc783 JB |
58 | This variable is used by the function `lisp-indent-tagbody' to indent normal |
59 | lines (lines without tags). | |
60 | The indentation is relative to the indentation of the parenthesis enclosing | |
61 | the special form. If the value is t, the body of tags will be indented | |
62 | as a block at the same indentation as the first s-expression following | |
63 | the tag. In this case, any forms before the first tag are indented | |
fcad5199 RS |
64 | by `lisp-body-indent'." |
65 | :type 'integer | |
66 | :group 'lisp-indent) | |
745bc783 | 67 | |
59e0f579 | 68 | (defcustom lisp-backquote-indentation t |
c66cd0ff | 69 | "Whether or not to indent backquoted lists as code. |
59e0f579 GM |
70 | If nil, indent backquoted lists as data, i.e., like quoted lists." |
71 | :type 'boolean | |
72 | :group 'lisp-indent) | |
73 | ||
74 | ||
75 | (defcustom lisp-loop-keyword-indentation 3 | |
c66cd0ff | 76 | "Indentation of loop keywords in extended loop forms." |
59e0f579 GM |
77 | :type 'integer |
78 | :group 'lisp-indent) | |
79 | ||
80 | ||
81 | (defcustom lisp-loop-forms-indentation 5 | |
c66cd0ff | 82 | "Indentation of forms in extended loop forms." |
59e0f579 GM |
83 | :type 'integer |
84 | :group 'lisp-indent) | |
85 | ||
86 | ||
87 | (defcustom lisp-simple-loop-indentation 3 | |
c66cd0ff | 88 | "Indentation of forms in simple loop forms." |
59e0f579 GM |
89 | :type 'integer |
90 | :group 'lisp-indent) | |
91 | ||
8497a297 DV |
92 | (defcustom lisp-lambda-list-keyword-alignment nil |
93 | "Whether to vertically align lambda-list keywords together. | |
94 | If nil (the default), keyworded lambda-list parts are aligned | |
95 | with the initial mandatory arguments, like this: | |
96 | ||
97 | \(defun foo (arg1 arg2 &rest rest | |
98 | &key key1 key2) | |
99 | #|...|#) | |
100 | ||
101 | If non-nil, alignment is done with the first keyword | |
102 | \(or falls back to the previous case), as in: | |
103 | ||
104 | \(defun foo (arg1 arg2 &rest rest | |
105 | &key key1 key2) | |
106 | #|...|#)" | |
2bed3f04 | 107 | :version "24.1" |
8497a297 DV |
108 | :type 'boolean |
109 | :group 'lisp-indent) | |
110 | ||
111 | (defcustom lisp-lambda-list-keyword-parameter-indentation 2 | |
112 | "Indentation of lambda list keyword parameters. | |
113 | See `lisp-lambda-list-keyword-parameter-alignment' | |
114 | for more information." | |
2bed3f04 | 115 | :version "24.1" |
8497a297 DV |
116 | :type 'integer |
117 | :group 'lisp-indent) | |
118 | ||
119 | (defcustom lisp-lambda-list-keyword-parameter-alignment nil | |
120 | "Whether to vertically align lambda-list keyword parameters together. | |
121 | If nil (the default), the parameters are aligned | |
122 | with their corresponding keyword, plus the value of | |
123 | `lisp-lambda-list-keyword-parameter-indentation', like this: | |
124 | ||
125 | \(defun foo (arg1 arg2 &key key1 key2 | |
126 | key3 key4) | |
127 | #|...|#) | |
128 | ||
129 | If non-nil, alignment is done with the first parameter | |
130 | \(or falls back to the previous case), as in: | |
131 | ||
132 | \(defun foo (arg1 arg2 &key key1 key2 | |
133 | key3 key4) | |
134 | #|...|#)" | |
2bed3f04 | 135 | :version "24.1" |
8497a297 DV |
136 | :type 'boolean |
137 | :group 'lisp-indent) | |
138 | ||
745bc783 | 139 | \f |
0ae8ebe8 | 140 | (defvar lisp-indent-defun-method '(4 &lambda &body) |
8497a297 DV |
141 | "Defun-like indentation method. |
142 | This applies when the value of the `common-lisp-indent-function' property | |
143 | is set to `defun'.") | |
bf92b5a4 | 144 | |
59e0f579 GM |
145 | |
146 | (defun extended-loop-p (loop-start) | |
cb0fd101 | 147 | "True if an extended loop form starts at LOOP-START." |
59e0f579 GM |
148 | (condition-case () |
149 | (save-excursion | |
150 | (goto-char loop-start) | |
151 | (forward-char 1) | |
152 | (forward-sexp 2) | |
153 | (backward-sexp 1) | |
154 | (looking-at "\\sw")) | |
155 | (error t))) | |
156 | ||
157 | ||
158 | (defun common-lisp-loop-part-indentation (indent-point state) | |
159 | "Compute the indentation of loop form constituents." | |
160 | (let* ((loop-indentation (save-excursion | |
161 | (goto-char (elt state 1)) | |
162 | (current-column)))) | |
163 | (goto-char indent-point) | |
164 | (beginning-of-line) | |
9c34a344 LMI |
165 | (list |
166 | (cond ((not (extended-loop-p (elt state 1))) | |
167 | (+ loop-indentation lisp-simple-loop-indentation)) | |
168 | ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)") | |
169 | (+ loop-indentation lisp-loop-keyword-indentation)) | |
170 | (t | |
171 | (+ loop-indentation lisp-loop-forms-indentation))) | |
172 | ;; Tell the caller that the next line needs recomputation, even | |
173 | ;; though it doesn't start a sexp. | |
174 | loop-indentation))) | |
a1506d29 | 175 | |
59e0f579 | 176 | |
b7556719 | 177 | ;; Cf (info "(elisp)Specification List") |
745bc783 JB |
178 | ;;;###autoload |
179 | (defun common-lisp-indent-function (indent-point state) | |
0ae8ebe8 GM |
180 | "Function to indent the arguments of a Lisp function call. |
181 | This is suitable for use as the value of the variable | |
182 | `lisp-indent-function'. INDENT-POINT is the point at which the | |
183 | indentation function is called, and STATE is the | |
184 | `parse-partial-sexp' state at that position. Browse the | |
185 | `lisp-indent' customize group for options affecting the behavior | |
186 | of this function. | |
187 | ||
188 | If the indentation point is in a call to a Lisp function, that | |
8497a297 | 189 | function's `common-lisp-indent-function' property specifies how |
0ae8ebe8 GM |
190 | this function should indent it. Possible values for this |
191 | property are: | |
192 | ||
193 | * defun, meaning indent according to `lisp-indent-defun-method'; | |
194 | i.e., like (4 &lambda &body), as explained below. | |
195 | ||
196 | * any other symbol, meaning a function to call. The function should | |
197 | take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT. | |
198 | PATH is a list of integers describing the position of point in terms of | |
199 | list-structure with respect to the containing lists. For example, in | |
200 | ((a b c (d foo) f) g), foo has a path of (0 3 1). In other words, | |
201 | to reach foo take the 0th element of the outermost list, then | |
202 | the 3rd element of the next list, and finally the 1st element. | |
203 | STATE and INDENT-POINT are as in the arguments to | |
204 | `common-lisp-indent-function'. SEXP-COLUMN is the column of | |
205 | the open parenthesis of the innermost containing list. | |
206 | NORMAL-INDENT is the column the indentation point was | |
207 | originally in. This function should behave like `lisp-indent-259'. | |
208 | ||
209 | * an integer N, meaning indent the first N arguments like | |
210 | function arguments, and any further arguments like a body. | |
211 | This is equivalent to (4 4 ... &body). | |
212 | ||
213 | * a list. The list element in position M specifies how to indent the Mth | |
214 | function argument. If there are fewer elements than function arguments, | |
215 | the last list element applies to all remaining arguments. The accepted | |
216 | list elements are: | |
217 | ||
218 | * nil, meaning the default indentation. | |
219 | ||
220 | * an integer, specifying an explicit indentation. | |
221 | ||
222 | * &lambda. Indent the argument (which may be a list) by 4. | |
223 | ||
224 | * &rest. When used, this must be the penultimate element. The | |
225 | element after this one applies to all remaining arguments. | |
226 | ||
227 | * &body. This is equivalent to &rest lisp-body-indent, i.e., indent | |
228 | all remaining elements by `lisp-body-indent'. | |
229 | ||
230 | * &whole. This must be followed by nil, an integer, or a | |
231 | function symbol. This indentation is applied to the | |
232 | associated argument, and as a base indent for all remaining | |
233 | arguments. For example, an integer P means indent this | |
234 | argument by P, and all remaining arguments by P, plus the | |
235 | value specified by their associated list element. | |
236 | ||
237 | * a symbol. A function to call, with the 6 arguments specified above. | |
238 | ||
239 | * a list, with elements as described above. This applies when the | |
240 | associated function argument is itself a list. Each element of the list | |
241 | specifies how to indent the associated argument. | |
242 | ||
243 | For example, the function `case' has an indent property | |
244 | \(4 &rest (&whole 2 &rest 1)), meaning: | |
245 | * indent the first argument by 4. | |
246 | * arguments after the first should be lists, and there may be any number | |
247 | of them. The first list element has an offset of 2, all the rest | |
248 | have an offset of 2+1=3." | |
59e0f579 GM |
249 | (if (save-excursion (goto-char (elt state 1)) |
250 | (looking-at "([Ll][Oo][Oo][Pp]")) | |
251 | (common-lisp-loop-part-indentation indent-point state) | |
252 | (common-lisp-indent-function-1 indent-point state))) | |
a1506d29 JB |
253 | |
254 | ||
59e0f579 | 255 | (defun common-lisp-indent-function-1 (indent-point state) |
745bc783 JB |
256 | (let ((normal-indent (current-column))) |
257 | ;; Walk up list levels until we see something | |
258 | ;; which does special things with subforms. | |
259 | (let ((depth 0) | |
260 | ;; Path describes the position of point in terms of | |
eb8c3be9 | 261 | ;; list-structure with respect to containing lists. |
8497a297 | 262 | ;; `foo' has a path of (0 3 1) in `((a b c (d foo) f) g)'. |
745bc783 JB |
263 | (path ()) |
264 | ;; set non-nil when somebody works out the indentation to use | |
265 | calculated | |
f620e5e2 RS |
266 | ;; If non-nil, this is an indentation to use |
267 | ;; if nothing else specifies it more firmly. | |
268 | tentative-calculated | |
269 | (last-point indent-point) | |
745bc783 JB |
270 | ;; the position of the open-paren of the innermost containing list |
271 | (containing-form-start (elt state 1)) | |
272 | ;; the column of the above | |
273 | sexp-column) | |
274 | ;; Move to start of innermost containing list | |
275 | (goto-char containing-form-start) | |
276 | (setq sexp-column (current-column)) | |
59e0f579 | 277 | |
745bc783 JB |
278 | ;; Look over successively less-deep containing forms |
279 | (while (and (not calculated) | |
280 | (< depth lisp-indent-maximum-backtracking)) | |
281 | (let ((containing-sexp (point))) | |
282 | (forward-char 1) | |
283 | (parse-partial-sexp (point) indent-point 1 t) | |
284 | ;; Move to the car of the relevant containing form | |
f620e5e2 | 285 | (let (tem function method tentative-defun) |
745bc783 JB |
286 | (if (not (looking-at "\\sw\\|\\s_")) |
287 | ;; This form doesn't seem to start with a symbol | |
288 | (setq function nil method nil) | |
289 | (setq tem (point)) | |
290 | (forward-sexp 1) | |
bbf1ae49 RS |
291 | (setq function (downcase (buffer-substring-no-properties |
292 | tem (point)))) | |
745bc783 JB |
293 | (goto-char tem) |
294 | (setq tem (intern-soft function) | |
295 | method (get tem 'common-lisp-indent-function)) | |
296 | (cond ((and (null method) | |
297 | (string-match ":[^:]+" function)) | |
298 | ;; The pleblisp package feature | |
299 | (setq function (substring function | |
300 | (1+ (match-beginning 0))) | |
301 | method (get (intern-soft function) | |
302 | 'common-lisp-indent-function))) | |
303 | ((and (null method)) | |
304 | ;; backwards compatibility | |
305 | (setq method (get tem 'lisp-indent-function))))) | |
306 | (let ((n 0)) | |
307 | ;; How far into the containing form is the current form? | |
308 | (if (< (point) indent-point) | |
309 | (while (condition-case () | |
310 | (progn | |
311 | (forward-sexp 1) | |
312 | (if (>= (point) indent-point) | |
313 | nil | |
314 | (parse-partial-sexp (point) | |
315 | indent-point 1 t) | |
316 | (setq n (1+ n)) | |
317 | t)) | |
318 | (error nil)))) | |
319 | (setq path (cons n path))) | |
320 | ||
321 | ;; backwards compatibility. | |
322 | (cond ((null function)) | |
323 | ((null method) | |
bbf1ae49 | 324 | (when (null (cdr path)) |
f620e5e2 RS |
325 | ;; (package prefix was stripped off above) |
326 | (cond ((string-match "\\`def" | |
327 | function) | |
328 | (setq tentative-defun t)) | |
410019e5 SS |
329 | ((string-match |
330 | (eval-when-compile | |
331 | (concat "\\`\\(" | |
332 | (regexp-opt '("with" "without" "do")) | |
333 | "\\)-")) | |
334 | function) | |
f620e5e2 | 335 | (setq method '(&lambda &body)))))) |
745bc783 JB |
336 | ;; backwards compatibility. Bletch. |
337 | ((eq method 'defun) | |
bbf1ae49 | 338 | (setq method lisp-indent-defun-method))) |
745bc783 | 339 | |
59e0f579 GM |
340 | (cond ((and (or (eq (char-after (1- containing-sexp)) ?\') |
341 | (and (not lisp-backquote-indentation) | |
342 | (eq (char-after (1- containing-sexp)) ?\`))) | |
893d4ef0 | 343 | (not (eq (char-after (- containing-sexp 2)) ?\#))) |
745bc783 JB |
344 | ;; No indentation for "'(...)" elements |
345 | (setq calculated (1+ sexp-column))) | |
5d80cc9c SS |
346 | ((or (eq (char-after (1- containing-sexp)) ?\,) |
347 | (and (eq (char-after (1- containing-sexp)) ?\@) | |
348 | (eq (char-after (- containing-sexp 2)) ?\,))) | |
349 | ;; ",(...)" or ",@(...)" | |
350 | (setq calculated normal-indent)) | |
893d4ef0 | 351 | ((eq (char-after (1- containing-sexp)) ?\#) |
745bc783 JB |
352 | ;; "#(...)" |
353 | (setq calculated (1+ sexp-column))) | |
f620e5e2 RS |
354 | ((null method) |
355 | ;; If this looks like a call to a `def...' form, | |
356 | ;; think about indenting it as one, but do it | |
357 | ;; tentatively for cases like | |
358 | ;; (flet ((defunp () | |
359 | ;; nil))) | |
360 | ;; Set both normal-indent and tentative-calculated. | |
361 | ;; The latter ensures this value gets used | |
362 | ;; if there are no relevant containing constructs. | |
363 | ;; The former ensures this value gets used | |
364 | ;; if there is a relevant containing construct | |
365 | ;; but we are nested within the structure levels | |
366 | ;; that it specifies indentation for. | |
367 | (if tentative-defun | |
368 | (setq tentative-calculated | |
369 | (common-lisp-indent-call-method | |
370 | function lisp-indent-defun-method | |
371 | path state indent-point | |
372 | sexp-column normal-indent) | |
373 | normal-indent tentative-calculated))) | |
745bc783 JB |
374 | ((integerp method) |
375 | ;; convenient top-level hack. | |
376 | ;; (also compatible with lisp-indent-function) | |
377 | ;; The number specifies how many `distinguished' | |
378 | ;; forms there are before the body starts | |
379 | ;; Equivalent to (4 4 ... &body) | |
380 | (setq calculated (cond ((cdr path) | |
381 | normal-indent) | |
382 | ((<= (car path) method) | |
383 | ;; `distinguished' form | |
384 | (list (+ sexp-column 4) | |
385 | containing-form-start)) | |
386 | ((= (car path) (1+ method)) | |
387 | ;; first body form. | |
388 | (+ sexp-column lisp-body-indent)) | |
389 | (t | |
390 | ;; other body form | |
391 | normal-indent)))) | |
f620e5e2 RS |
392 | (t |
393 | (setq calculated | |
394 | (common-lisp-indent-call-method | |
395 | function method path state indent-point | |
396 | sexp-column normal-indent))))) | |
745bc783 JB |
397 | (goto-char containing-sexp) |
398 | (setq last-point containing-sexp) | |
bbf1ae49 | 399 | (unless calculated |
f620e5e2 RS |
400 | (condition-case () |
401 | (progn (backward-up-list 1) | |
402 | (setq depth (1+ depth))) | |
403 | (error (setq depth lisp-indent-maximum-backtracking)))))) | |
404 | (or calculated tentative-calculated)))) | |
405 | ||
406 | ||
407 | (defun common-lisp-indent-call-method (function method path state indent-point | |
408 | sexp-column normal-indent) | |
409 | (let ((lisp-indent-error-function function)) | |
410 | (if (symbolp method) | |
411 | (funcall method | |
412 | path state indent-point | |
413 | sexp-column normal-indent) | |
414 | (lisp-indent-259 method path state indent-point | |
415 | sexp-column normal-indent)))) | |
745bc783 | 416 | |
c66cd0ff GM |
417 | ;; Dynamically bound in common-lisp-indent-call-method. |
418 | (defvar lisp-indent-error-function) | |
419 | ||
745bc783 JB |
420 | (defun lisp-indent-report-bad-format (m) |
421 | (error "%s has a badly-formed %s property: %s" | |
422 | ;; Love those free variable references!! | |
bf92b5a4 | 423 | lisp-indent-error-function 'common-lisp-indent-function m)) |
745bc783 | 424 | |
8497a297 DV |
425 | |
426 | ;; Lambda-list indentation is now done in LISP-INDENT-LAMBDA-LIST. | |
427 | ;; See also `lisp-lambda-list-keyword-alignment', | |
428 | ;; `lisp-lambda-list-keyword-parameter-alignment' and | |
429 | ;; `lisp-lambda-list-keyword-parameter-indentation' -- dvl | |
430 | ||
431 | (defvar lisp-indent-lambda-list-keywords-regexp | |
432 | "&\\(\ | |
433 | optional\\|rest\\|key\\|allow-other-keys\\|aux\\|whole\\|body\\|environment\ | |
434 | \\)\\([ \t]\\|$\\)" | |
435 | "Regular expression matching lambda-list keywords.") | |
436 | ||
437 | (defun lisp-indent-lambda-list | |
438 | (indent-point sexp-column containing-form-start) | |
439 | (let (limit) | |
440 | (cond ((save-excursion | |
441 | (goto-char indent-point) | |
442 | (beginning-of-line) | |
443 | (skip-chars-forward " \t") | |
444 | (setq limit (point)) | |
445 | (looking-at lisp-indent-lambda-list-keywords-regexp)) | |
446 | ;; We're facing a lambda-list keyword. | |
447 | (if lisp-lambda-list-keyword-alignment | |
448 | ;; Align to the first keyword if any, or to the beginning of | |
449 | ;; the lambda-list. | |
450 | (save-excursion | |
451 | (goto-char containing-form-start) | |
452 | (save-match-data | |
453 | (if (re-search-forward | |
454 | lisp-indent-lambda-list-keywords-regexp | |
455 | limit t) | |
456 | (progn | |
457 | (goto-char (match-beginning 0)) | |
458 | (current-column)) | |
459 | (1+ sexp-column)))) | |
460 | ;; Align to the beginning of the lambda-list. | |
461 | (1+ sexp-column))) | |
462 | (t | |
463 | ;; Otherwise, align to the first argument of the last lambda-list | |
464 | ;; keyword, the keyword itself, or the beginning of the | |
465 | ;; lambda-list. | |
466 | (save-excursion | |
467 | (goto-char indent-point) | |
468 | (forward-line -1) | |
469 | (end-of-line) | |
470 | (save-match-data | |
471 | (if (re-search-backward lisp-indent-lambda-list-keywords-regexp | |
472 | containing-form-start t) | |
473 | (let* ((keyword-posn | |
474 | (progn | |
475 | (goto-char (match-beginning 0)) | |
476 | (current-column))) | |
477 | (indented-keyword-posn | |
478 | (+ keyword-posn | |
479 | lisp-lambda-list-keyword-parameter-indentation))) | |
480 | (goto-char (match-end 0)) | |
481 | (skip-chars-forward " \t") | |
482 | (if (eolp) | |
483 | indented-keyword-posn | |
484 | (if lisp-lambda-list-keyword-parameter-alignment | |
485 | (current-column) | |
486 | indented-keyword-posn))) | |
487 | (1+ sexp-column)))))))) | |
488 | ||
745bc783 JB |
489 | ;; Blame the crufty control structure on dynamic scoping |
490 | ;; -- not on me! | |
8497a297 DV |
491 | (defun lisp-indent-259 |
492 | (method path state indent-point sexp-column normal-indent) | |
745bc783 JB |
493 | (catch 'exit |
494 | (let ((p path) | |
495 | (containing-form-start (elt state 1)) | |
496 | n tem tail) | |
497 | ;; Isn't tail-recursion wonderful? | |
498 | (while p | |
499 | ;; This while loop is for destructuring. | |
500 | ;; p is set to (cdr p) each iteration. | |
501 | (if (not (consp method)) (lisp-indent-report-bad-format method)) | |
502 | (setq n (1- (car p)) | |
503 | p (cdr p) | |
504 | tail nil) | |
505 | (while n | |
506 | ;; This while loop is for advancing along a method | |
507 | ;; until the relevant (possibly &rest/&body) pattern | |
508 | ;; is reached. | |
509 | ;; n is set to (1- n) and method to (cdr method) | |
510 | ;; each iteration. | |
511 | (setq tem (car method)) | |
512 | ||
513 | (or (eq tem 'nil) ;default indentation | |
5d80cc9c | 514 | (eq tem '&lambda) ;lambda list |
745bc783 JB |
515 | (and (eq tem '&body) (null (cdr method))) |
516 | (and (eq tem '&rest) | |
5d80cc9c SS |
517 | (consp (cdr method)) |
518 | (null (cddr method))) | |
745bc783 JB |
519 | (integerp tem) ;explicit indentation specified |
520 | (and (consp tem) ;destructuring | |
521 | (eq (car tem) '&whole) | |
5d80cc9c SS |
522 | (or (symbolp (cadr tem)) |
523 | (integerp (cadr tem)))) | |
745bc783 JB |
524 | (and (symbolp tem) ;a function to call to do the work. |
525 | (null (cdr method))) | |
526 | (lisp-indent-report-bad-format method)) | |
527 | ||
528 | (cond ((and tail (not (consp tem))) | |
529 | ;; indent tail of &rest in same way as first elt of rest | |
530 | (throw 'exit normal-indent)) | |
531 | ((eq tem '&body) | |
532 | ;; &body means (&rest <lisp-body-indent>) | |
533 | (throw 'exit | |
534 | (if (and (= n 0) ;first body form | |
535 | (null p)) ;not in subforms | |
536 | (+ sexp-column | |
537 | lisp-body-indent) | |
538 | normal-indent))) | |
539 | ((eq tem '&rest) | |
540 | ;; this pattern holds for all remaining forms | |
541 | (setq tail (> n 0) | |
542 | n 0 | |
543 | method (cdr method))) | |
544 | ((> n 0) | |
545 | ;; try next element of pattern | |
546 | (setq n (1- n) | |
547 | method (cdr method)) | |
548 | (if (< n 0) | |
549 | ;; Too few elements in pattern. | |
550 | (throw 'exit normal-indent))) | |
551 | ((eq tem 'nil) | |
6655e16d CY |
552 | (throw 'exit (if (consp normal-indent) |
553 | normal-indent | |
554 | (list normal-indent containing-form-start)))) | |
bec9dc7b CY |
555 | ((eq tem '&lambda) |
556 | (throw 'exit | |
557 | (cond ((null p) | |
558 | (list (+ sexp-column 4) containing-form-start)) | |
559 | ((null (cdr p)) | |
8497a297 DV |
560 | ;; Indentation within a lambda-list. -- dvl |
561 | (list (lisp-indent-lambda-list | |
562 | indent-point | |
563 | sexp-column | |
564 | containing-form-start) | |
565 | containing-form-start)) | |
566 | (t | |
567 | normal-indent)))) | |
745bc783 JB |
568 | ((integerp tem) |
569 | (throw 'exit | |
570 | (if (null p) ;not in subforms | |
571 | (list (+ sexp-column tem) containing-form-start) | |
572 | normal-indent))) | |
573 | ((symbolp tem) ;a function to call | |
574 | (throw 'exit | |
575 | (funcall tem path state indent-point | |
576 | sexp-column normal-indent))) | |
577 | (t | |
578 | ;; must be a destructing frob | |
579 | (if (not (null p)) | |
580 | ;; descend | |
bbf1ae49 | 581 | (setq method (cddr tem) |
745bc783 | 582 | n nil) |
bbf1ae49 | 583 | (setq tem (cadr tem)) |
745bc783 JB |
584 | (throw 'exit |
585 | (cond (tail | |
586 | normal-indent) | |
587 | ((eq tem 'nil) | |
588 | (list normal-indent | |
589 | containing-form-start)) | |
590 | ((integerp tem) | |
591 | (list (+ sexp-column tem) | |
592 | containing-form-start)) | |
593 | (t | |
594 | (funcall tem path state indent-point | |
595 | sexp-column normal-indent)))))))))))) | |
596 | \f | |
597 | (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent) | |
598 | (if (not (null (cdr path))) | |
599 | normal-indent | |
600 | (save-excursion | |
601 | (goto-char indent-point) | |
602 | (beginning-of-line) | |
603 | (skip-chars-forward " \t") | |
604 | (list (cond ((looking-at "\\sw\\|\\s_") | |
605 | ;; a tagbody tag | |
606 | (+ sexp-column lisp-tag-indentation)) | |
607 | ((integerp lisp-tag-body-indentation) | |
608 | (+ sexp-column lisp-tag-body-indentation)) | |
609 | ((eq lisp-tag-body-indentation 't) | |
610 | (condition-case () | |
611 | (progn (backward-sexp 1) (current-column)) | |
612 | (error (1+ sexp-column)))) | |
613 | (t (+ sexp-column lisp-body-indent))) | |
614 | ; (cond ((integerp lisp-tag-body-indentation) | |
615 | ; (+ sexp-column lisp-tag-body-indentation)) | |
616 | ; ((eq lisp-tag-body-indentation 't) | |
617 | ; normal-indent) | |
618 | ; (t | |
619 | ; (+ sexp-column lisp-body-indent))) | |
620 | (elt state 1) | |
621 | )))) | |
622 | ||
623 | (defun lisp-indent-do (path state indent-point sexp-column normal-indent) | |
624 | (if (>= (car path) 3) | |
625 | (let ((lisp-tag-body-indentation lisp-body-indent)) | |
626 | (funcall (function lisp-indent-tagbody) | |
5d80cc9c | 627 | path state indent-point sexp-column normal-indent)) |
745bc783 | 628 | (funcall (function lisp-indent-259) |
5d80cc9c SS |
629 | '((&whole nil &rest |
630 | ;; the following causes weird indentation | |
631 | ;;(&whole 1 1 2 nil) | |
632 | ) | |
633 | (&whole nil &rest 1)) | |
634 | path state indent-point sexp-column normal-indent))) | |
745bc783 | 635 | |
59e0f579 | 636 | |
8497a297 DV |
637 | ;; LISP-INDENT-DEFMETHOD now supports the presence of more than one method |
638 | ;; qualifier and indents the method's lambda list properly. -- dvl | |
639 | (defun lisp-indent-defmethod | |
640 | (path state indent-point sexp-column normal-indent) | |
641 | (lisp-indent-259 | |
642 | (let ((nqual 0)) | |
643 | (if (and (>= (car path) 3) | |
644 | (save-excursion | |
645 | (beginning-of-defun) | |
646 | (forward-char 1) | |
647 | (forward-sexp 2) | |
648 | (skip-chars-forward " \t\n") | |
649 | (while (looking-at "\\sw\\|\\s_") | |
650 | (incf nqual) | |
651 | (forward-sexp) | |
652 | (skip-chars-forward " \t\n")) | |
653 | (> nqual 0))) | |
654 | (append '(4) (make-list nqual 4) '(&lambda &body)) | |
655 | (get 'defun 'common-lisp-indent-function))) | |
656 | path state indent-point sexp-column normal-indent)) | |
ec69d5ec GM |
657 | |
658 | ||
745bc783 JB |
659 | (defun lisp-indent-function-lambda-hack (path state indent-point |
660 | sexp-column normal-indent) | |
661 | ;; indent (function (lambda () <newline> <body-forms>)) kludgily. | |
662 | (if (or (cdr path) ; wtf? | |
663 | (> (car path) 3)) | |
664 | ;; line up under previous body form | |
665 | normal-indent | |
666 | ;; line up under function rather than under lambda in order to | |
667 | ;; conserve horizontal space. (Which is what #' is for.) | |
668 | (condition-case () | |
669 | (save-excursion | |
670 | (backward-up-list 2) | |
671 | (forward-char 1) | |
672 | (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)") | |
673 | (+ lisp-body-indent -1 (current-column)) | |
674 | (+ sexp-column lisp-body-indent))) | |
675 | (error (+ sexp-column lisp-body-indent))))) | |
676 | ||
59e0f579 | 677 | |
745bc783 JB |
678 | \f |
679 | (let ((l '((block 1) | |
5d80cc9c | 680 | (case (4 &rest (&whole 2 &rest 1))) |
c66cd0ff GM |
681 | (ccase . case) |
682 | (ecase . case) | |
683 | (typecase . case) | |
684 | (etypecase . case) | |
685 | (ctypecase . case) | |
5d80cc9c SS |
686 | (catch 1) |
687 | (cond (&rest (&whole 2 &rest 1))) | |
688 | (defvar (4 2 2)) | |
5e9e032a | 689 | (defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1))) |
5d80cc9c | 690 | (defconstant . defvar) |
2ac6f2c8 | 691 | (defcustom (4 2 2 2)) |
5d80cc9c | 692 | (defparameter . defvar) |
5e9e032a SS |
693 | (defconst . defcustom) |
694 | (define-condition . defclass) | |
b6f053c6 | 695 | (define-modify-macro (4 &lambda &body)) |
5d80cc9c SS |
696 | (defsetf (4 &lambda 4 &body)) |
697 | (defun (4 &lambda &body)) | |
8497a297 | 698 | (defgeneric (4 &lambda &body)) |
5d80cc9c SS |
699 | (define-setf-method . defun) |
700 | (define-setf-expander . defun) | |
c66cd0ff GM |
701 | (defmacro . defun) |
702 | (defsubst . defun) | |
703 | (deftype . defun) | |
ec69d5ec | 704 | (defmethod lisp-indent-defmethod) |
5d80cc9c SS |
705 | (defpackage (4 2)) |
706 | (defstruct ((&whole 4 &rest (&whole 2 &rest 1)) | |
707 | &rest (&whole 2 &rest 1))) | |
708 | (destructuring-bind | |
709 | ((&whole 6 &rest 1) 4 &body)) | |
710 | (do lisp-indent-do) | |
711 | (do* . do) | |
712 | (dolist ((&whole 4 2 1) &body)) | |
713 | (dotimes . dolist) | |
714 | (eval-when 1) | |
715 | (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body)) | |
716 | (labels . flet) | |
717 | (macrolet . flet) | |
c66cd0ff GM |
718 | (generic-flet . flet) |
719 | (generic-labels . flet) | |
bbf1ae49 | 720 | (handler-case (4 &rest (&whole 2 &lambda &body))) |
09a4d958 | 721 | (restart-case . handler-case) |
5d80cc9c SS |
722 | ;; `else-body' style |
723 | (if (nil nil &body)) | |
724 | ;; single-else style (then and else equally indented) | |
725 | (if (&rest nil)) | |
726 | (lambda (&lambda &rest lisp-indent-function-lambda-hack)) | |
727 | (let ((&whole 4 &rest (&whole 1 1 2)) &body)) | |
728 | (let* . let) | |
729 | (compiler-let . let) ;barf | |
c66cd0ff GM |
730 | (handler-bind . let) |
731 | (restart-bind . let) | |
5d80cc9c | 732 | (locally 1) |
59e0f579 | 733 | ;(loop lisp-indent-loop) |
2180ea97 | 734 | (:method (&lambda &body)) ; in `defgeneric' |
5e9e032a SS |
735 | (multiple-value-bind ((&whole 6 &rest 1) 4 &body)) |
736 | (multiple-value-call (4 &body)) | |
5d80cc9c SS |
737 | (multiple-value-prog1 1) |
738 | (multiple-value-setq (4 2)) | |
739 | (multiple-value-setf . multiple-value-setq) | |
7dd21017 | 740 | (pprint-logical-block (4 2)) |
5d80cc9c SS |
741 | (print-unreadable-object ((&whole 4 1 &rest 1) &body)) |
742 | ;; Combines the worst features of BLOCK, LET and TAGBODY | |
743 | (prog (&lambda &rest lisp-indent-tagbody)) | |
744 | (prog* . prog) | |
745 | (prog1 1) | |
746 | (prog2 2) | |
747 | (progn 0) | |
748 | (progv (4 4 &body)) | |
749 | (return 0) | |
750 | (return-from (nil &body)) | |
10b97bf7 | 751 | (symbol-macrolet . let) |
5d80cc9c SS |
752 | (tagbody lisp-indent-tagbody) |
753 | (throw 1) | |
754 | (unless 1) | |
755 | (unwind-protect (5 &body)) | |
f0d527fc | 756 | (when 1) |
e7c8c428 SS |
757 | (with-accessors . multiple-value-bind) |
758 | (with-condition-restarts . multiple-value-bind) | |
ef9dd188 | 759 | (with-compilation-unit (&lambda &body)) |
44c705d4 | 760 | (with-output-to-string (4 2)) |
5e9e032a | 761 | (with-slots . multiple-value-bind) |
f0d527fc | 762 | (with-standard-io-syntax (2))))) |
e7c8c428 SS |
763 | (dolist (el l) |
764 | (put (car el) 'common-lisp-indent-function | |
765 | (if (symbolp (cdr el)) | |
766 | (get (cdr el) 'common-lisp-indent-function) | |
767 | (car (cdr el)))))) | |
745bc783 JB |
768 | |
769 | \f | |
770 | ;(defun foo (x) | |
771 | ; (tagbody | |
772 | ; foo | |
773 | ; (bar) | |
774 | ; baz | |
775 | ; (when (losing) | |
776 | ; (with-big-loser | |
777 | ; (yow) | |
778 | ; ((lambda () | |
779 | ; foo) | |
780 | ; big))) | |
781 | ; (flet ((foo (bar baz zap) | |
782 | ; (zip)) | |
783 | ; (zot () | |
784 | ; quux)) | |
785 | ; (do () | |
786 | ; ((lose) | |
787 | ; (foo 1)) | |
788 | ; (quux) | |
789 | ; foo | |
790 | ; (lose)) | |
791 | ; (cond ((x) | |
792 | ; (win 1 2 | |
793 | ; (foo))) | |
794 | ; (t | |
795 | ; (lose | |
796 | ; 3)))))) | |
5d80cc9c | 797 | |
745bc783 JB |
798 | |
799 | ;(put 'while 'common-lisp-indent-function 1) | |
800 | ;(put 'defwrapper'common-lisp-indent-function ...) | |
801 | ;(put 'def 'common-lisp-indent-function ...) | |
802 | ;(put 'defflavor 'common-lisp-indent-function ...) | |
803 | ;(put 'defsubst 'common-lisp-indent-function ...) | |
804 | ||
805 | ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body))) | |
806 | ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1))))) | |
968880c4 | 807 | ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body))) |
745bc783 JB |
808 | ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body))) |
809 | ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body))))) | |
968880c4 DL |
810 | ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1))) |
811 | ;(put 'defgeneric 'common-lisp-indent-function 'defun) | |
745bc783 | 812 | |
36f84c37 GM |
813 | (provide 'cl-indent) |
814 | ||
c0274f38 | 815 | ;;; cl-indent.el ends here |