[lice @ add all function symbols emacs implements in C]
[lice.git] / lisp-indent.lisp
blob5b636d6b8eb420cd936f1d718045e67a11e1750e
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 (with-match-data
130 (let ((normal-indent (current-column)))
131 ;; Walk up list levels until we see something
132 ;; which does special things with subforms.
133 (let ((depth 0)
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)'
137 (path ())
138 ;; set non-nil when somebody works out the indentation to use
139 calculated
140 ;; If non-nil, this is an indentation to use
141 ;; if nothing else specifies it more firmly.
142 tentative-calculated
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
147 sexp-column)
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)))
157 (forward-char 1)
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)
165 (progn
166 (setq tem (point))
167 (forward-sexp 1)
168 (setq function (string-upcase (buffer-substring-no-properties
169 tem (point))))
170 (message "name is ~a" function)
171 (goto-char tem)
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)))
181 ((and (null method))
182 ;; backwards compatibility
183 (setq method (get tem 'lisp-indent-function))))))
184 (let ((n 0))
185 ;; How far into the containing form is the current form?
186 (if (< (point) indent-point)
187 (while (handler-case
188 (progn
189 (forward-sexp 1)
190 (if (>= (point) indent-point)
192 (progn
193 ;;(message "second one!")
194 (parse-partial-sexp (point)
195 indent-point :target-depth 1 :stop-before t)
196 (setq n (1+ n))
197 t)))
198 (error () nil))))
199 (setq path (cons n path)))
201 ;; backwards compatibility.
202 (cond ((null function))
203 ((null method)
204 (when (null (cdr path))
205 ;; (package prefix was stripped off above)
206 (cond ((string-match "\\`def"
207 function)
208 (setq tentative-defun t))
209 ((string-match
210 ;; eval-when-compile ;;(regexp-opt '("with" "without" "do"))
211 "\\`\\(with\\|without\\|do\\)-"
212 function)
213 (setq method '(&lambda &body))))))
214 ;; backwards compatibility. Bletch.
215 ((eq method 'defun)
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)) #\#)
230 ;; "#(...)"
231 (setq calculated (1+ sexp-column)))
232 ((null method)
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
236 ;; (flet ((defunp ()
237 ;; nil)))
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.
245 (if tentative-defun
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)))
252 ((integerp method)
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)
259 normal-indent)
260 ((<= (car path) method)
261 ;; `distinguished' form
262 (list (+ sexp-column 4)
263 containing-form-start))
264 ((= (car path) (1+ method))
265 ;; first body form.
266 (+ sexp-column lisp-body-indent))
268 ;; other body form
269 normal-indent))))
271 (setq calculated
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)
277 (unless calculated
278 (handler-case
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))
288 (if (symbolp method)
289 (funcall method
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
301 ;; -- not on me!
302 (defun lisp-indent-259 (method path state indent-point
303 sexp-column normal-indent)
304 (catch 'exit
305 (let ((p path)
306 (containing-form-start (parse-state-prev-level-start state))
307 n tem tail)
308 ;; Isn't tail-recursion wonderful?
309 (while p
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))
313 (setq n (1- (car p))
314 p (cdr p)
315 tail nil)
316 (while n
317 ;; This while loop is for advancing along a method
318 ;; until the relevant (possibly &rest/&body) pattern
319 ;; is reached.
320 ;; n is set to (1- n) and method to (cdr method)
321 ;; each iteration.
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)))
327 (and (eq tem '&rest)
328 (consp (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.
336 (null (cdr method)))
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))
342 ((eq tem '&body)
343 ;; &body means (&rest <lisp-body-indent>)
344 (throw 'exit
345 (if (and (= n 0) ;first body form
346 (null p)) ;not in subforms
347 (+ sexp-column
348 lisp-body-indent)
349 normal-indent)))
350 ((eq tem '&rest)
351 ;; this pattern holds for all remaining forms
352 (setq tail (> n 0)
354 method (cdr method)))
355 ((> n 0)
356 ;; try next element of pattern
357 (setq n (1- n)
358 method (cdr method))
359 (if (< n 0)
360 ;; Too few elements in pattern.
361 (throw 'exit normal-indent)))
362 ((eq tem 'nil)
363 (throw 'exit (list normal-indent containing-form-start)))
364 ((eq tem '&lambda)
365 (throw 'exit
366 (cond ((null p)
367 (list (+ sexp-column 4) containing-form-start))
368 ((null (cdr p))
369 (+ sexp-column 1))
370 (t normal-indent))))
371 ((integerp tem)
372 (throw 'exit
373 (if (null p) ;not in subforms
374 (list (+ sexp-column tem) containing-form-start)
375 normal-indent)))
376 ((symbolp tem) ;a function to call
377 (throw 'exit
378 (funcall tem path state indent-point
379 sexp-column normal-indent)))
381 ;; must be a destructing frob
382 (if (not (null p))
383 ;; descend
384 (setq method (cddr tem)
385 n nil)
386 (progn
387 (setq tem (cadr tem))
388 (throw 'exit
389 (cond (tail
390 normal-indent)
391 ((eq tem 'nil)
392 (list normal-indent
393 containing-form-start))
394 ((integerp tem)
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)))
403 normal-indent
404 (save-excursion
405 (goto-char indent-point)
406 (beginning-of-line)
407 (skip-whitespace-forward)
408 (list (cond ((looking-at "\\w|\\w") ;; used to be symbol constituent \\s_
409 ;; a tagbody tag
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)
414 (handler-case
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)
421 ; normal-indent)
422 ; (t
423 ; (+ sexp-column lisp-body-indent)))
424 (parse-state-prev-level-start state)
425 ))))
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)
433 '((&whole nil &rest
434 ;; the following causes weird indentation
435 ;;(&whole 1 1 2 nil)
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
442 normal-indent)
443 "Indentation function defmethod."
444 (lisp-indent-259 (if (and (>= (car path) 3)
445 (null (cdr path))
446 (save-excursion (goto-char (parse-state-prev-level-start state))
447 (forward-char 1)
448 (forward-sexp 3)
449 (backward-sexp)
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?
461 (> (car path) 3))
462 ;; line up under previous body form
463 normal-indent
464 ;; line up under function rather than under lambda in order to
465 ;; conserve horizontal space. (Which is what #' is for.)
466 (handler-case
467 (save-excursion
468 (backward-up-list 2)
469 (forward-char 1)
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)))))
477 (let ((l '((block 1)
478 (case (4 &rest (&whole 2 &rest 1)))
479 (ccase . case) (ecase . case)
480 (typecase . case) (etypecase . case) (ctypecase . case)
481 (catch 1)
482 (cond (&rest (&whole 2 &rest 1)))
483 (defvar (4 2 2))
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)
497 (defpackage (4 2))
498 (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
499 &rest (&whole 2 &rest 1)))
500 (destructuring-bind
501 ((&whole 6 &rest 1) 4 &body))
502 (do lisp-indent-do)
503 (do* . do)
504 (dolist ((&whole 4 2 1) &body))
505 (dotimes . dolist)
506 (eval-when 1)
507 (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
508 (labels . flet)
509 (macrolet . flet)
510 (generic-flet . flet) (generic-labels . flet)
511 (handler-case (4 &rest (&whole 2 &lambda &body)))
512 (restart-case . handler-case)
513 ;; `else-body' style
514 (if (nil nil &body))
515 ;; single-else style (then and else equally indented)
516 (if (&rest nil))
517 (lambda (&lambda &rest lisp-indent-function-lambda-hack))
518 (let ((&whole 4 &rest (&whole 1 1 2)) &body))
519 (let* . let)
520 (compiler-let . let) ;barf
521 (handler-bind . let) (restart-bind . let)
522 (locally 1)
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))
534 (prog* . prog)
535 (prog1 1)
536 (prog2 2)
537 (progn 0)
538 (progv (4 4 &body))
539 (return 0)
540 (return-from (nil &body))
541 (symbol-macrolet . let)
542 (tagbody lisp-indent-tagbody)
543 (throw 1)
544 (unless 1)
545 (unwind-protect (5 &body))
546 (when 1)
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)))))
552 (dolist (el l)
553 (setf (get (car el) 'common-lisp-indent-function)
554 (if (symbolp (cdr el))
555 (get (cdr el) 'common-lisp-indent-function)
556 (car (cdr el))))))
559 ;(defun foo (x)
560 ; (tagbody
561 ; foo
562 ; (bar)
563 ; baz
564 ; (when (losing)
565 ; (with-big-loser
566 ; (yow)
567 ; ((lambda ()
568 ; foo)
569 ; big)))
570 ; (flet ((foo (bar baz zap)
571 ; (zip))
572 ; (zot ()
573 ; quux))
574 ; (do ()
575 ; ((lose)
576 ; (foo 1))
577 ; (quux)
578 ; foo
579 ; (lose))
580 ; (cond ((x)
581 ; (win 1 2
582 ; (foo)))
583 ; (t
584 ; (lose
585 ; 3))))))
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