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
8 ;; This package supplies a single entry point, common-lisp-indent-function,
9 ;; which performs indentation in the preferred style for Common Lisp code.
12 ;; (setq lisp-indent-function 'common-lisp-indent-function)
19 ;; &key (like &body)??
21 ;; &rest 1 in lambda-lists doesn't work
22 ;; -- really want (foo bar
26 ;; Need something better than &rest for such cases
32 ;; (defgroup lisp-indent nil
33 ;; "Indentation in 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."
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'."
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'."
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."
69 (defcustom lisp-loop-keyword-indentation
3
70 "*Indentation of loop keywords in extended loop forms."
75 (defcustom lisp-loop-forms-indentation
5
76 "*Indentation of forms in extended loop forms."
81 (defcustom lisp-simple-loop-indentation
3
82 "*Indentation of forms in simple loop forms."
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."
97 (goto-char loop-start
)
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
))
110 (goto-char indent-point
)
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
)))))
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
)
130 (let ((normal-indent (current-column)))
131 ;; Walk up list levels until we see something
132 ;; which does special things with subforms.
134 ;; Path describes the position of point in terms of
135 ;; list-structure with respect to containing lists.
136 ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
138 ;; set non-nil when somebody works out the indentation to use
140 ;; If non-nil, this is an indentation to use
141 ;; if nothing else specifies it more firmly.
143 (last-point indent-point
)
144 ;; the position of the open-paren of the innermost containing list
145 (containing-form-start (parse-state-prev-level-start state
))
146 ;; the column of the above
148 (message "hey ~d" containing-form-start
)
149 ;; Move to start of innermost containing list
150 (goto-char containing-form-start
)
151 (setq sexp-column
(current-column))
153 ;; Look over successively less-deep containing forms
154 (while (and (not calculated
)
155 (< depth lisp-indent-maximum-backtracking
))
156 (let ((containing-sexp (point)))
158 ;;(message "first one!")
159 (parse-partial-sexp (point) indent-point
:target-depth
1 :stop-before t
)
160 ;; Move to the car of the relevant containing form
161 (let (tem function method tentative-defun
)
162 (if (not (looking-at "\\w|\\s"))
163 ;; This form doesn't seem to start with a symbol
164 (setq function nil method nil
)
168 (setq function
(string-upcase (buffer-substring-no-properties
170 (message "name is ~a" function
)
172 (setq tem
(intern-soft function
)
173 method
(get tem
'common-lisp-indent-function
))
174 (cond ((and (null method
)
175 (string-match ":[^:]+" function
))
176 ;; The pleblisp package feature
177 (setq function
(subseq function
178 (1+ (match-beginning 0)))
179 method
(get (intern-soft function
)
180 'common-lisp-indent-function
)))
182 ;; backwards compatibility
183 (setq method
(get tem
'lisp-indent-function
))))))
185 ;; How far into the containing form is the current form?
186 (if (< (point) indent-point
)
190 (if (>= (point) indent-point
)
193 ;;(message "second one!")
194 (parse-partial-sexp (point)
195 indent-point
:target-depth
1 :stop-before t
)
199 (setq path
(cons n path
)))
201 ;; backwards compatibility.
202 (cond ((null function
))
204 (when (null (cdr path
))
205 ;; (package prefix was stripped off above)
206 (cond ((string-match "\\`def"
208 (setq tentative-defun
t))
210 ;; eval-when-compile ;;(regexp-opt '("with" "without" "do"))
211 "\\`\\(with\\|without\\|do\\)-"
213 (setq method
'(&lambda
&body
))))))
214 ;; backwards compatibility. Bletch.
216 (setq method lisp-indent-defun-method
)))
218 (cond ((and (or (eq (char-after (1- containing-sexp
)) #\')
219 (and (not lisp-backquote-indentation
)
220 (eq (char-after (1- containing-sexp
)) #\
`)))
221 (not (eq (char-after (- containing-sexp
2)) #\
#)))
222 ;; No indentation for "'(...)" elements
223 (setq calculated
(1+ sexp-column
)))
224 ((or (eq (char-after (1- containing-sexp
)) #\
,)
225 (and (eq (char-after (1- containing-sexp
)) #\
@)
226 (eq (char-after (- containing-sexp
2)) #\
,)))
227 ;; ",(...)" or ",@(...)"
228 (setq calculated normal-indent
))
229 ((eq (char-after (1- containing-sexp
)) #\
#)
231 (setq calculated
(1+ sexp-column
)))
233 ;; If this looks like a call to a `def...' form,
234 ;; think about indenting it as one, but do it
235 ;; tentatively for cases like
238 ;; Set both normal-indent and tentative-calculated.
239 ;; The latter ensures this value gets used
240 ;; if there are no relevant containing constructs.
241 ;; The former ensures this value gets used
242 ;; if there is a relevant containing construct
243 ;; but we are nested within the structure levels
244 ;; that it specifies indentation for.
246 (setq tentative-calculated
247 (common-lisp-indent-call-method
248 function lisp-indent-defun-method
249 path state indent-point
250 sexp-column normal-indent
)
251 normal-indent tentative-calculated
)))
253 ;; convenient top-level hack.
254 ;; (also compatible with lisp-indent-function)
255 ;; The number specifies how many `distinguished'
256 ;; forms there are before the body starts
257 ;; Equivalent to (4 4 ... &body)
258 (setq calculated
(cond ((cdr path
)
260 ((<= (car path
) method
)
261 ;; `distinguished' form
262 (list (+ sexp-column
4)
263 containing-form-start
))
264 ((= (car path
) (1+ method
))
266 (+ sexp-column lisp-body-indent
))
272 (common-lisp-indent-call-method
273 function method path state indent-point
274 sexp-column normal-indent
)))))
275 (goto-char containing-sexp
)
276 (setq last-point containing-sexp
)
279 (progn (backward-up-list 1)
280 (setq depth
(1+ depth
)))
281 (error () (setq depth lisp-indent-maximum-backtracking
))))))
282 (or calculated tentative-calculated
)))))
285 (defun common-lisp-indent-call-method (function method path state indent-point
286 sexp-column normal-indent
)
287 (let ((lisp-indent-error-function function
))
290 path state indent-point
291 sexp-column normal-indent
)
292 (lisp-indent-259 method path state indent-point
293 sexp-column normal-indent
))))
295 (defun lisp-indent-report-bad-format (m)
296 (error "~s has a badly-formed ~s property: ~s"
297 ;; Love those free variable references!!
298 lisp-indent-error-function
'common-lisp-indent-function m
))
300 ;; Blame the crufty control structure on dynamic scoping
302 (defun lisp-indent-259 (method path state indent-point
303 sexp-column normal-indent
)
306 (containing-form-start (parse-state-prev-level-start state
))
308 ;; Isn't tail-recursion wonderful?
310 ;; This while loop is for destructuring.
311 ;; p is set to (cdr p) each iteration.
312 (if (not (consp method
)) (lisp-indent-report-bad-format method
))
317 ;; This while loop is for advancing along a method
318 ;; until the relevant (possibly &rest/&body) pattern
320 ;; n is set to (1- n) and method to (cdr method)
322 (setq tem
(car method
))
324 (or (eq tem
'nil
) ;default indentation
325 (eq tem
'&lambda
) ;lambda list
326 (and (eq tem
'&body
) (null (cdr method
)))
329 (null (cddr method
)))
330 (integerp tem
) ;explicit indentation specified
331 (and (consp tem
) ;destructuring
332 (eq (car tem
) '&whole
)
333 (or (symbolp (cadr tem
))
334 (integerp (cadr tem
))))
335 (and (symbolp tem
) ;a function to call to do the work.
337 (lisp-indent-report-bad-format method
))
339 (cond ((and tail
(not (consp tem
)))
340 ;; indent tail of &rest in same way as first elt of rest
341 (throw 'exit normal-indent
))
343 ;; &body means (&rest <lisp-body-indent>)
345 (if (and (= n
0) ;first body form
346 (null p
)) ;not in subforms
351 ;; this pattern holds for all remaining forms
354 method
(cdr method
)))
356 ;; try next element of pattern
360 ;; Too few elements in pattern.
361 (throw 'exit normal-indent
)))
363 (throw 'exit
(list normal-indent containing-form-start
)))
367 (list (+ sexp-column
4) containing-form-start
))
373 (if (null p
) ;not in subforms
374 (list (+ sexp-column tem
) containing-form-start
)
376 ((symbolp tem
) ;a function to call
378 (funcall tem path state indent-point
379 sexp-column normal-indent
)))
381 ;; must be a destructing frob
384 (setq method
(cddr tem
)
387 (setq tem
(cadr tem
))
393 containing-form-start
))
395 (list (+ sexp-column tem
)
396 containing-form-start
))
398 (funcall tem path state indent-point
399 sexp-column normal-indent
)))))))))))))
401 (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent
)
402 (if (not (null (cdr path
)))
405 (goto-char indent-point
)
407 (skip-whitespace-forward)
408 (list (cond ((looking-at "\\w|\\w") ;; used to be symbol constituent \\s_
410 (+ sexp-column lisp-tag-indentation
))
411 ((integerp lisp-tag-body-indentation
)
412 (+ sexp-column lisp-tag-body-indentation
))
413 ((eq lisp-tag-body-indentation
't
)
415 (progn (backward-sexp 1) (current-column))
416 (error () (1+ sexp-column
))))
417 (t (+ sexp-column lisp-body-indent
)))
418 ; (cond ((integerp lisp-tag-body-indentation)
419 ; (+ sexp-column lisp-tag-body-indentation))
420 ; ((eq lisp-tag-body-indentation 't)
423 ; (+ sexp-column lisp-body-indent)))
424 (parse-state-prev-level-start state
)
427 (defun lisp-indent-do (path state indent-point sexp-column normal-indent
)
428 (if (>= (car path
) 3)
429 (let ((lisp-tag-body-indentation lisp-body-indent
))
430 (funcall (function lisp-indent-tagbody
)
431 path state indent-point sexp-column normal-indent
))
432 (funcall (function lisp-indent-259
)
434 ;; the following causes weird indentation
437 (&whole nil
&rest
1))
438 path state indent-point sexp-column normal-indent
)))
441 (defun lisp-indent-defmethod (path state indent-point sexp-column
443 "Indentation function defmethod."
444 (lisp-indent-259 (if (and (>= (car path
) 3)
446 (save-excursion (goto-char (parse-state-prev-level-start state
))
450 (looking-at ":|\\w+")))
451 '(4 4 (&whole
4 &rest
4) &body
)
452 (get 'defun
'common-lisp-indent-function
))
453 path state indent-point sexp-column normal-indent
))
456 (defun lisp-indent-function-lambda-hack (path state indent-point
457 sexp-column normal-indent
)
458 (declare (ignore state indent-point
))
459 ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
460 (if (or (cdr path
) ; wtf?
462 ;; line up under previous body form
464 ;; line up under function rather than under lambda in order to
465 ;; conserve horizontal space. (Which is what #' is for.)
470 (if (looking-at "(lisp:+)?function(\\W|\\W)") ; XXX: used to be symbol constituent \\S_
471 (+ lisp-body-indent -
1 (current-column))
472 (+ sexp-column lisp-body-indent
)))
473 (error () (+ sexp-column lisp-body-indent
)))))
478 (case (4 &rest
(&whole
2 &rest
1)))
479 (ccase . case
) (ecase . case
)
480 (typecase . case
) (etypecase . case
) (ctypecase . case
)
482 (cond (&rest
(&whole
2 &rest
1)))
484 (defclass (6 4 (&whole
2 &rest
1) (&whole
2 &rest
1)))
485 (defconstant . defvar
)
486 (defcustom (4 2 2 2))
487 (defparameter . defvar
)
488 (defconst . defcustom
)
489 (define-condition . defclass
)
490 (define-modify-macro (4 &lambda
&body
))
491 (defsetf (4 &lambda
4 &body
))
492 (defun (4 &lambda
&body
))
493 (define-setf-method . defun
)
494 (define-setf-expander . defun
)
495 (defmacro . defun
) (defsubst . defun
) (deftype . defun
)
496 (defmethod lisp-indent-defmethod)
498 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
499 &rest
(&whole
2 &rest
1)))
501 ((&whole
6 &rest
1) 4 &body
))
504 (dolist ((&whole
4 2 1) &body
))
507 (flet ((&whole
4 &rest
(&whole
1 &lambda
&body
)) &body
))
510 (generic-flet . flet
) (generic-labels . flet
)
511 (handler-case (4 &rest
(&whole
2 &lambda
&body
)))
512 (restart-case . handler-case
)
515 ;; single-else style (then and else equally indented)
517 (lambda (&lambda
&rest lisp-indent-function-lambda-hack
))
518 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
520 (compiler-let . let
) ;barf
521 (handler-bind . let
) (restart-bind . let
)
523 ;(loop lisp-indent-loop)
524 (:method
(&lambda
&body
)) ; in `defgeneric'
525 (multiple-value-bind ((&whole
6 &rest
1) 4 &body
))
526 (multiple-value-call (4 &body
))
527 (multiple-value-prog1 1)
528 (multiple-value-setq (4 2))
529 (multiple-value-setf . multiple-value-setq
)
530 (pprint-logical-block (4 2))
531 (print-unreadable-object ((&whole
4 1 &rest
1) &body
))
532 ;; Combines the worst features of BLOCK, LET and TAGBODY
533 (prog (&lambda
&rest lisp-indent-tagbody
))
540 (return-from (nil &body
))
541 (symbol-macrolet . let
)
542 (tagbody lisp-indent-tagbody
)
545 (unwind-protect (5 &body
))
547 (with-accessors . multiple-value-bind
)
548 (with-condition-restarts . multiple-value-bind
)
549 (with-output-to-string (4 2))
550 (with-slots . multiple-value-bind
)
551 (with-standard-io-syntax (2)))))
553 (setf (get (car el
) 'common-lisp-indent-function
)
554 (if (symbolp (cdr el
))
555 (get (cdr el
) 'common-lisp-indent-function
)
570 ; (flet ((foo (bar baz zap)
588 ;(put 'while 'common-lisp-indent-function 1)
589 ;(put 'defwrapper'common-lisp-indent-function ...)
590 ;(put 'def 'common-lisp-indent-function ...)
591 ;(put 'defflavor 'common-lisp-indent-function ...)
592 ;(put 'defsubst 'common-lisp-indent-function ...)
594 ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
595 ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
596 ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
597 ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
598 ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
599 ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
600 ;(put 'defgeneric 'common-lisp-indent-function 'defun)
602 ;;; arch-tag: 7914d50f-92ec-4476-93fc-0f043a380e03
603 ;;; cl-indent.el ends here