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