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
)
129 (let ((normal-indent (current-column)))
130 ;; Walk up list levels until we see something
131 ;; which does special things with subforms.
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)'
137 ;; set non-nil when somebody works out the indentation to use
139 ;; If non-nil, this is an indentation to use
140 ;; if nothing else specifies it more firmly.
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
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)))
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
)
167 (setq function
(string-upcase (buffer-substring-no-properties
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
)))
180 ;; backwards compatibility
181 (setq method
(get tem
'lisp-indent-function
))))))
183 ;; How far into the containing form is the current form?
184 (if (< (point) indent-point
)
188 (if (>= (point) indent-point
)
191 ;;(message "second one!")
192 (parse-partial-sexp (point)
193 indent-point
:target-depth
1 :stop-before t
)
197 (setq path
(cons n path
)))
199 ;; backwards compatibility.
200 (cond ((null function
))
202 (when (null (cdr path
))
203 ;; (package prefix was stripped off above)
204 (cond ((string-match "\\`def"
206 (setq tentative-defun
t))
208 ;; eval-when-compile ;;(regexp-opt '("with" "without" "do"))
209 "\\`\\(with\\|without\\|do\\)-"
211 (setq method
'(&lambda
&body
))))))
212 ;; backwards compatibility. Bletch.
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
)) #\
#)
229 (setq calculated
(1+ sexp-column
)))
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
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.
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
)))
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
)
258 ((<= (car path
) method
)
259 ;; `distinguished' form
260 (list (+ sexp-column
4)
261 containing-form-start
))
262 ((= (car path
) (1+ method
))
264 (+ sexp-column lisp-body-indent
))
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
)
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
))
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
300 (defun lisp-indent-259 (method path state indent-point
301 sexp-column normal-indent
)
304 (containing-form-start (parse-state-prev-level-start state
))
306 ;; Isn't tail-recursion wonderful?
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
))
315 ;; This while loop is for advancing along a method
316 ;; until the relevant (possibly &rest/&body) pattern
318 ;; n is set to (1- n) and method to (cdr method)
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
)))
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.
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
))
341 ;; &body means (&rest <lisp-body-indent>)
343 (if (and (= n
0) ;first body form
344 (null p
)) ;not in subforms
349 ;; this pattern holds for all remaining forms
352 method
(cdr method
)))
354 ;; try next element of pattern
358 ;; Too few elements in pattern.
359 (throw 'exit normal-indent
)))
361 (throw 'exit
(list normal-indent containing-form-start
)))
365 (list (+ sexp-column
4) containing-form-start
))
371 (if (null p
) ;not in subforms
372 (list (+ sexp-column tem
) containing-form-start
)
374 ((symbolp tem
) ;a function to call
376 (funcall tem path state indent-point
377 sexp-column normal-indent
)))
379 ;; must be a destructing frob
382 (setq method
(cddr tem
)
385 (setq tem
(cadr tem
))
391 containing-form-start
))
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
)))
403 (goto-char indent-point
)
405 (skip-whitespace-forward)
406 (list (cond ((looking-at "\\w|\\w") ;; used to be symbol constituent \\s_
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
)
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)
421 ; (+ sexp-column lisp-body-indent)))
422 (parse-state-prev-level-start state
)
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
)
432 ;; the following causes weird indentation
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
441 "Indentation function defmethod."
442 (lisp-indent-259 (if (and (>= (car path
) 3)
444 (save-excursion (goto-char (parse-state-prev-level-start state
))
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?
459 ;; line up under previous body form
461 ;; line up under function rather than under lambda in order to
462 ;; conserve horizontal space. (Which is what #' is for.)
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
)))))
475 (case (4 &rest
(&whole
2 &rest
1)))
476 (ccase . case
) (ecase . case
)
477 (typecase . case
) (etypecase . case
) (ctypecase . case
)
479 (cond (&rest
(&whole
2 &rest
1)))
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)
495 (defstruct ((&whole
4 &rest
(&whole
2 &rest
1))
496 &rest
(&whole
2 &rest
1)))
498 ((&whole
6 &rest
1) 4 &body
))
501 (dolist ((&whole
4 2 1) &body
))
504 (flet ((&whole
4 &rest
(&whole
1 &lambda
&body
)) &body
))
507 (generic-flet . flet
) (generic-labels . flet
)
508 (handler-case (4 &rest
(&whole
2 &lambda
&body
)))
509 (restart-case . handler-case
)
512 ;; single-else style (then and else equally indented)
514 (lambda (&lambda
&rest lisp-indent-function-lambda-hack
))
515 (let ((&whole
4 &rest
(&whole
1 1 2)) &body
))
517 (compiler-let . let
) ;barf
518 (handler-bind . let
) (restart-bind . let
)
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
))
537 (return-from (nil &body
))
538 (symbol-macrolet . let
)
539 (tagbody lisp-indent-tagbody
)
542 (unwind-protect (5 &body
))
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)))))
550 (setf (get (car el
) 'common-lisp-indent-function
)
551 (if (symbolp (cdr el
))
552 (get (cdr el
) 'common-lisp-indent-function
)
567 ; (flet ((foo (bar baz zap)
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