[lice @ simulate interrupt key when waiting for input]
[lice.git] / lisp-indent.lisp
blobba251d8e5c7e82b30f7bbed6071bef3b2cd2da12
1 ;;; lisp-indent.lisp --- enhanced lisp-indent mode
3 ;; to implement: backward-up-list concat downcase intern-soft match-beginning parse-partial-sexp regexp-opt skip-chars-forward substring
6 ;;; Commentary:
8 ;; This package supplies a single entry point, common-lisp-indent-function,
9 ;; which performs indentation in the preferred style for Common Lisp code.
10 ;; To enable it:
12 ;; (setq lisp-indent-function 'common-lisp-indent-function)
14 ;;>> TODO
15 ;; :foo
16 ;; bar
17 ;; :baz
18 ;; zap
19 ;; &key (like &body)??
21 ;; &rest 1 in lambda-lists doesn't work
22 ;; -- really want (foo bar
23 ;; baz)
24 ;; not (foo bar
25 ;; baz)
26 ;; Need something better than &rest for such cases
28 ;;; Code:
30 (in-package "LICE")
32 ;; (defgroup lisp-indent nil
33 ;; "Indentation in Lisp."
34 ;; :group 'lisp)
37 (defcustom lisp-indent-maximum-backtracking 3
38 "*Maximum depth to backtrack out from a sublist for structured indentation.
39 If this variable is 0, no backtracking will occur and forms such as `flet'
40 may not be correctly indented."
41 :type 'integer
42 :group 'lisp-indent)
44 (defcustom lisp-tag-indentation 1
45 "*Indentation of tags relative to containing list.
46 This variable is used by the function `lisp-indent-tagbody'."
47 :type 'integer
48 :group 'lisp-indent)
50 (defcustom lisp-tag-body-indentation 3
51 "*Indentation of non-tagged lines relative to containing list.
52 This variable is used by the function `lisp-indent-tagbody' to indent normal
53 lines (lines without tags).
54 The indentation is relative to the indentation of the parenthesis enclosing
55 the special form. If the value is t, the body of tags will be indented
56 as a block at the same indentation as the first s-expression following
57 the tag. In this case, any forms before the first tag are indented
58 by `lisp-body-indent'."
59 :type 'integer
60 :group 'lisp-indent)
62 (defcustom lisp-backquote-indentation t
63 "*Whether or not to indent backquoted lists as code.
64 If nil, indent backquoted lists as data, i.e., like quoted lists."
65 :type 'boolean
66 :group 'lisp-indent)
69 (defcustom lisp-loop-keyword-indentation 3
70 "*Indentation of loop keywords in extended loop forms."
71 :type 'integer
72 :group 'lisp-indent)
75 (defcustom lisp-loop-forms-indentation 5
76 "*Indentation of forms in extended loop forms."
77 :type 'integer
78 :group 'lisp-indent)
81 (defcustom lisp-simple-loop-indentation 3
82 "*Indentation of forms in simple loop forms."
83 :type 'integer
84 :group 'lisp-indent)
87 (defvar lisp-indent-error-function)
88 (defvar lisp-indent-defun-method '(4 &lambda &body))
90 (defvar lisp-body-indent 2
91 "Number of columns to indent the second line of a `(def...)' form.")
93 (defun extended-loop-p (loop-start)
94 "True if an extended loop form starts at LOOP-START."
95 (handler-case
96 (save-excursion
97 (goto-char loop-start)
98 (forward-char 1)
99 (forward-sexp 2)
100 (backward-sexp 1)
101 (looking-at "\\w"))
102 (error () t)))
105 (defun common-lisp-loop-part-indentation (indent-point state)
106 "Compute the indentation of loop form constituents."
107 (let* ((loop-indentation (save-excursion
108 (goto-char (parse-state-prev-level-start state))
109 (current-column))))
110 (goto-char indent-point)
111 (beginning-of-line)
112 (cond ((not (extended-loop-p (parse-state-prev-level-start state)))
113 (+ loop-indentation lisp-simple-loop-indentation))
114 ((looking-at "^\\s*(:?\\w+|;)")
115 (+ loop-indentation lisp-loop-keyword-indentation))
117 (+ loop-indentation lisp-loop-forms-indentation)))))
120 ;;;###autoload
121 (defun common-lisp-indent-function (indent-point state)
122 (if (save-excursion (goto-char (parse-state-prev-level-start state))
123 (looking-at "\\([Ll][Oo][Oo][Pp]"))
124 (common-lisp-loop-part-indentation indent-point state)
125 (common-lisp-indent-function-1 indent-point state)))
128 (defun common-lisp-indent-function-1 (indent-point state)
129 (let ((normal-indent (current-column)))
130 ;; Walk up list levels until we see something
131 ;; which does special things with subforms.
132 (let ((depth 0)
133 ;; Path describes the position of point in terms of
134 ;; list-structure with respect to containing lists.
135 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
136 (path ())
137 ;; set non-nil when somebody works out the indentation to use
138 calculated
139 ;; If non-nil, this is an indentation to use
140 ;; if nothing else specifies it more firmly.
141 tentative-calculated
142 (last-point indent-point)
143 ;; the position of the open-paren of the innermost containing list
144 (containing-form-start (parse-state-prev-level-start state))
145 ;; the column of the above
146 sexp-column)
147 (message "hey ~d" containing-form-start)
148 ;; Move to start of innermost containing list
149 (goto-char containing-form-start)
150 (setq sexp-column (current-column))
152 ;; Look over successively less-deep containing forms
153 (while (and (not calculated)
154 (< depth lisp-indent-maximum-backtracking))
155 (let ((containing-sexp (point)))
156 (forward-char 1)
157 ;;(message "first one!")
158 (parse-partial-sexp (point) indent-point :target-depth 1 :stop-before t)
159 ;; Move to the car of the relevant containing form
160 (let (tem function method tentative-defun)
161 (if (not (looking-at "\\w|\\s"))
162 ;; This form doesn't seem to start with a symbol
163 (setq function nil method nil)
164 (progn
165 (setq tem (point))
166 (forward-sexp 1)
167 (setq function (string-upcase (buffer-substring-no-properties
168 tem (point))))
169 (goto-char tem)
170 (setq tem (intern-soft function)
171 method (get tem 'common-lisp-indent-function))
172 (cond ((and (null method)
173 (string-match ":[^:]+" function))
174 ;; The pleblisp package feature
175 (setq function (subseq function
176 (1+ (match-beginning 0)))
177 method (get (intern-soft function)
178 'common-lisp-indent-function)))
179 ((and (null method))
180 ;; backwards compatibility
181 (setq method (get tem 'lisp-indent-function))))))
182 (let ((n 0))
183 ;; How far into the containing form is the current form?
184 (if (< (point) indent-point)
185 (while (handler-case
186 (progn
187 (forward-sexp 1)
188 (if (>= (point) indent-point)
190 (progn
191 ;;(message "second one!")
192 (parse-partial-sexp (point)
193 indent-point :target-depth 1 :stop-before t)
194 (setq n (1+ n))
195 t)))
196 (error () nil))))
197 (setq path (cons n path)))
199 ;; backwards compatibility.
200 (cond ((null function))
201 ((null method)
202 (when (null (cdr path))
203 ;; (package prefix was stripped off above)
204 (cond ((string-match "\\`def"
205 function)
206 (setq tentative-defun t))
207 ((string-match
208 ;; eval-when-compile ;;(regexp-opt '("with" "without" "do"))
209 "\\`\\(with\\|without\\|do\\)-"
210 function)
211 (setq method '(&lambda &body))))))
212 ;; backwards compatibility. Bletch.
213 ((eq method 'defun)
214 (setq method lisp-indent-defun-method)))
216 (cond ((and (or (eq (char-after (1- containing-sexp)) #\')
217 (and (not lisp-backquote-indentation)
218 (eq (char-after (1- containing-sexp)) #\`)))
219 (not (eq (char-after (- containing-sexp 2)) #\#)))
220 ;; No indentation for "'(...)" elements
221 (setq calculated (1+ sexp-column)))
222 ((or (eq (char-after (1- containing-sexp)) #\,)
223 (and (eq (char-after (1- containing-sexp)) #\@)
224 (eq (char-after (- containing-sexp 2)) #\,)))
225 ;; ",(...)" or ",@(...)"
226 (setq calculated normal-indent))
227 ((eq (char-after (1- containing-sexp)) #\#)
228 ;; "#(...)"
229 (setq calculated (1+ sexp-column)))
230 ((null method)
231 ;; If this looks like a call to a `def...' form,
232 ;; think about indenting it as one, but do it
233 ;; tentatively for cases like
234 ;; (flet ((defunp ()
235 ;; nil)))
236 ;; Set both normal-indent and tentative-calculated.
237 ;; The latter ensures this value gets used
238 ;; if there are no relevant containing constructs.
239 ;; The former ensures this value gets used
240 ;; if there is a relevant containing construct
241 ;; but we are nested within the structure levels
242 ;; that it specifies indentation for.
243 (if tentative-defun
244 (setq tentative-calculated
245 (common-lisp-indent-call-method
246 function lisp-indent-defun-method
247 path state indent-point
248 sexp-column normal-indent)
249 normal-indent tentative-calculated)))
250 ((integerp method)
251 ;; convenient top-level hack.
252 ;; (also compatible with lisp-indent-function)
253 ;; The number specifies how many `distinguished'
254 ;; forms there are before the body starts
255 ;; Equivalent to (4 4 ... &body)
256 (setq calculated (cond ((cdr path)
257 normal-indent)
258 ((<= (car path) method)
259 ;; `distinguished' form
260 (list (+ sexp-column 4)
261 containing-form-start))
262 ((= (car path) (1+ method))
263 ;; first body form.
264 (+ sexp-column lisp-body-indent))
266 ;; other body form
267 normal-indent))))
269 (setq calculated
270 (common-lisp-indent-call-method
271 function method path state indent-point
272 sexp-column normal-indent)))))
273 (goto-char containing-sexp)
274 (setq last-point containing-sexp)
275 (unless calculated
276 (handler-case
277 (progn (backward-up-list 1)
278 (setq depth (1+ depth)))
279 (error () (setq depth lisp-indent-maximum-backtracking))))))
280 (or calculated tentative-calculated))))
283 (defun common-lisp-indent-call-method (function method path state indent-point
284 sexp-column normal-indent)
285 (let ((lisp-indent-error-function function))
286 (if (symbolp method)
287 (funcall method
288 path state indent-point
289 sexp-column normal-indent)
290 (lisp-indent-259 method path state indent-point
291 sexp-column normal-indent))))
293 (defun lisp-indent-report-bad-format (m)
294 (error "~s has a badly-formed ~s property: ~s"
295 ;; Love those free variable references!!
296 lisp-indent-error-function 'common-lisp-indent-function m))
298 ;; Blame the crufty control structure on dynamic scoping
299 ;; -- not on me!
300 (defun lisp-indent-259 (method path state indent-point
301 sexp-column normal-indent)
302 (catch 'exit
303 (let ((p path)
304 (containing-form-start (parse-state-prev-level-start state))
305 n tem tail)
306 ;; Isn't tail-recursion wonderful?
307 (while p
308 ;; This while loop is for destructuring.
309 ;; p is set to (cdr p) each iteration.
310 (if (not (consp method)) (lisp-indent-report-bad-format method))
311 (setq n (1- (car p))
312 p (cdr p)
313 tail nil)
314 (while n
315 ;; This while loop is for advancing along a method
316 ;; until the relevant (possibly &rest/&body) pattern
317 ;; is reached.
318 ;; n is set to (1- n) and method to (cdr method)
319 ;; each iteration.
320 (setq tem (car method))
322 (or (eq tem 'nil) ;default indentation
323 (eq tem '&lambda) ;lambda list
324 (and (eq tem '&body) (null (cdr method)))
325 (and (eq tem '&rest)
326 (consp (cdr method))
327 (null (cddr method)))
328 (integerp tem) ;explicit indentation specified
329 (and (consp tem) ;destructuring
330 (eq (car tem) '&whole)
331 (or (symbolp (cadr tem))
332 (integerp (cadr tem))))
333 (and (symbolp tem) ;a function to call to do the work.
334 (null (cdr method)))
335 (lisp-indent-report-bad-format method))
337 (cond ((and tail (not (consp tem)))
338 ;; indent tail of &rest in same way as first elt of rest
339 (throw 'exit normal-indent))
340 ((eq tem '&body)
341 ;; &body means (&rest <lisp-body-indent>)
342 (throw 'exit
343 (if (and (= n 0) ;first body form
344 (null p)) ;not in subforms
345 (+ sexp-column
346 lisp-body-indent)
347 normal-indent)))
348 ((eq tem '&rest)
349 ;; this pattern holds for all remaining forms
350 (setq tail (> n 0)
352 method (cdr method)))
353 ((> n 0)
354 ;; try next element of pattern
355 (setq n (1- n)
356 method (cdr method))
357 (if (< n 0)
358 ;; Too few elements in pattern.
359 (throw 'exit normal-indent)))
360 ((eq tem 'nil)
361 (throw 'exit (list normal-indent containing-form-start)))
362 ((eq tem '&lambda)
363 (throw 'exit
364 (cond ((null p)
365 (list (+ sexp-column 4) containing-form-start))
366 ((null (cdr p))
367 (+ sexp-column 1))
368 (t normal-indent))))
369 ((integerp tem)
370 (throw 'exit
371 (if (null p) ;not in subforms
372 (list (+ sexp-column tem) containing-form-start)
373 normal-indent)))
374 ((symbolp tem) ;a function to call
375 (throw 'exit
376 (funcall tem path state indent-point
377 sexp-column normal-indent)))
379 ;; must be a destructing frob
380 (if (not (null p))
381 ;; descend
382 (setq method (cddr tem)
383 n nil)
384 (progn
385 (setq tem (cadr tem))
386 (throw 'exit
387 (cond (tail
388 normal-indent)
389 ((eq tem 'nil)
390 (list normal-indent
391 containing-form-start))
392 ((integerp tem)
393 (list (+ sexp-column tem)
394 containing-form-start))
396 (funcall tem path state indent-point
397 sexp-column normal-indent)))))))))))))
399 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
400 (if (not (null (cdr path)))
401 normal-indent
402 (save-excursion
403 (goto-char indent-point)
404 (beginning-of-line)
405 (skip-whitespace-forward)
406 (list (cond ((looking-at "\\w|\\w") ;; used to be symbol constituent \\s_
407 ;; a tagbody tag
408 (+ sexp-column lisp-tag-indentation))
409 ((integerp lisp-tag-body-indentation)
410 (+ sexp-column lisp-tag-body-indentation))
411 ((eq lisp-tag-body-indentation 't)
412 (handler-case
413 (progn (backward-sexp 1) (current-column))
414 (error () (1+ sexp-column))))
415 (t (+ sexp-column lisp-body-indent)))
416 ; (cond ((integerp lisp-tag-body-indentation)
417 ; (+ sexp-column lisp-tag-body-indentation))
418 ; ((eq lisp-tag-body-indentation 't)
419 ; normal-indent)
420 ; (t
421 ; (+ sexp-column lisp-body-indent)))
422 (parse-state-prev-level-start state)
423 ))))
425 (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
426 (if (>= (car path) 3)
427 (let ((lisp-tag-body-indentation lisp-body-indent))
428 (funcall (function lisp-indent-tagbody)
429 path state indent-point sexp-column normal-indent))
430 (funcall (function lisp-indent-259)
431 '((&whole nil &rest
432 ;; the following causes weird indentation
433 ;;(&whole 1 1 2 nil)
435 (&whole nil &rest 1))
436 path state indent-point sexp-column normal-indent)))
439 (defun lisp-indent-defmethod (path state indent-point sexp-column
440 normal-indent)
441 "Indentation function defmethod."
442 (lisp-indent-259 (if (and (>= (car path) 3)
443 (null (cdr path))
444 (save-excursion (goto-char (parse-state-prev-level-start state))
445 (forward-char 1)
446 (forward-sexp 3)
447 (backward-sexp)
448 (looking-at ":|\\w+")))
449 '(4 4 (&whole 4 &rest 4) &body)
450 (get 'defun 'common-lisp-indent-function))
451 path state indent-point sexp-column normal-indent))
454 (defun lisp-indent-function-lambda-hack (path state indent-point
455 sexp-column normal-indent)
456 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
457 (if (or (cdr path) ; wtf?
458 (> (car path) 3))
459 ;; line up under previous body form
460 normal-indent
461 ;; line up under function rather than under lambda in order to
462 ;; conserve horizontal space. (Which is what #' is for.)
463 (handler-case
464 (save-excursion
465 (backward-up-list 2)
466 (forward-char 1)
467 (if (looking-at "(lisp:+)?function(\\W|\\W)") ; XXX: used to be symbol constituent \\S_
468 (+ lisp-body-indent -1 (current-column))
469 (+ sexp-column lisp-body-indent)))
470 (error () (+ sexp-column lisp-body-indent)))))
474 (let ((l '((block 1)
475 (case (4 &rest (&whole 2 &rest 1)))
476 (ccase . case) (ecase . case)
477 (typecase . case) (etypecase . case) (ctypecase . case)
478 (catch 1)
479 (cond (&rest (&whole 2 &rest 1)))
480 (defvar (4 2 2))
481 (defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1)))
482 (defconstant . defvar)
483 (defcustom (4 2 2 2))
484 (defparameter . defvar)
485 (defconst . defcustom)
486 (define-condition . defclass)
487 (define-modify-macro (4 &lambda &body))
488 (defsetf (4 &lambda 4 &body))
489 (defun (4 &lambda &body))
490 (define-setf-method . defun)
491 (define-setf-expander . defun)
492 (defmacro . defun) (defsubst . defun) (deftype . defun)
493 (defmethod lisp-indent-defmethod)
494 (defpackage (4 2))
495 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
496 &rest (&whole 2 &rest 1)))
497 (destructuring-bind
498 ((&whole 6 &rest 1) 4 &body))
499 (do lisp-indent-do)
500 (do* . do)
501 (dolist ((&whole 4 2 1) &body))
502 (dotimes . dolist)
503 (eval-when 1)
504 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
505 (labels . flet)
506 (macrolet . flet)
507 (generic-flet . flet) (generic-labels . flet)
508 (handler-case (4 &rest (&whole 2 &lambda &body)))
509 (restart-case . handler-case)
510 ;; `else-body' style
511 (if (nil nil &body))
512 ;; single-else style (then and else equally indented)
513 (if (&rest nil))
514 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
515 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
516 (let* . let)
517 (compiler-let . let) ;barf
518 (handler-bind . let) (restart-bind . let)
519 (locally 1)
520 ;(loop lisp-indent-loop)
521 (:method (&lambda &body)) ; in `defgeneric'
522 (multiple-value-bind ((&whole 6 &rest 1) 4 &body))
523 (multiple-value-call (4 &body))
524 (multiple-value-prog1 1)
525 (multiple-value-setq (4 2))
526 (multiple-value-setf . multiple-value-setq)
527 (pprint-logical-block (4 2))
528 (print-unreadable-object ((&whole 4 1 &rest 1) &body))
529 ;; Combines the worst features of BLOCK, LET and TAGBODY
530 (prog (&lambda &rest lisp-indent-tagbody))
531 (prog* . prog)
532 (prog1 1)
533 (prog2 2)
534 (progn 0)
535 (progv (4 4 &body))
536 (return 0)
537 (return-from (nil &body))
538 (symbol-macrolet . let)
539 (tagbody lisp-indent-tagbody)
540 (throw 1)
541 (unless 1)
542 (unwind-protect (5 &body))
543 (when 1)
544 (with-accessors . multiple-value-bind)
545 (with-condition-restarts . multiple-value-bind)
546 (with-output-to-string (4 2))
547 (with-slots . multiple-value-bind)
548 (with-standard-io-syntax (2)))))
549 (dolist (el l)
550 (setf (get (car el) 'common-lisp-indent-function)
551 (if (symbolp (cdr el))
552 (get (cdr el) 'common-lisp-indent-function)
553 (car (cdr el))))))
556 ;(defun foo (x)
557 ; (tagbody
558 ; foo
559 ; (bar)
560 ; baz
561 ; (when (losing)
562 ; (with-big-loser
563 ; (yow)
564 ; ((lambda ()
565 ; foo)
566 ; big)))
567 ; (flet ((foo (bar baz zap)
568 ; (zip))
569 ; (zot ()
570 ; quux))
571 ; (do ()
572 ; ((lose)
573 ; (foo 1))
574 ; (quux)
575 ; foo
576 ; (lose))
577 ; (cond ((x)
578 ; (win 1 2
579 ; (foo)))
580 ; (t
581 ; (lose
582 ; 3))))))
585 ;(put 'while 'common-lisp-indent-function 1)
586 ;(put 'defwrapper'common-lisp-indent-function ...)
587 ;(put 'def 'common-lisp-indent-function ...)
588 ;(put 'defflavor 'common-lisp-indent-function ...)
589 ;(put 'defsubst 'common-lisp-indent-function ...)
591 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
592 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
593 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
594 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
595 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
596 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
597 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
599 ;;; arch-tag: 7914d50f-92ec-4476-93fc-0f043a380e03
600 ;;; cl-indent.el ends here