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