;; Lisp mode, and its idiosyncratic commands.
;; Copyright (C) 1987 Free Software Foundation, Inc.
;; Written by Richard Mlynarik July 1987
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 1, or (at your option)
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;; &rest 1 in lambda-lists doesn't work
;; -- really want (foo bar
;; Need something better than &rest for such cases
;;; Hairy lisp indentation.
(defvar lisp-indent-maximum-backtracking 3
"*Maximum depth to backtrack out from a sublist for structured indentation.
If this variable is 0, no backtracking will occur and forms such as flet
may not be correctly indented.")
(defvar lisp-tag-indentation 1
"*Indentation of tags relative to containing list.
This variable is used by the function lisp-indent-tagbody.")
(defvar lisp-tag-body-indentation 3
"*Indentation of non-tagged lines relative to containing list.
This variable is used by the function lisp-indent-tagbody to indent normal
lines (lines without tags).
The indentation is relative to the indentation of the parenthesis enclosing
he special form. If the value is t, the body of tags will be indented
as a block at the same indentation as the first s-expression following
the tag. In this case, any forms before the first tag are indented
(defun common-lisp-indent-hook (indent-point state)
(let ((normal-indent (current-column)))
;; Walk up list levels until we see something
;; which does special things with subforms.
;; Path describes the position of point in terms of
;; list-structure with respect to contining lists.
;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
;; set non-nil when somebody works out the indentation to use
(last-point indent-point)
;; the position of the open-paren of the innermost containing list
(containing-form-start (elt state 1))
;; the column of the above
;; Move to start of innermost containing list
(goto-char containing-form-start)
(setq sexp-column (current-column))
;; Look over successively less-deep containing forms
(while (and (not calculated)
(< depth lisp-indent-maximum-backtracking))
(let ((containing-sexp (point)))
(parse-partial-sexp (point) indent-point 1 t)
;; Move to the car of the relevant containing form
(let (tem function method)
(if (not (looking-at "\\sw\\|\\s_"))
;; This form doesn't seem to start with a symbol
(setq function nil method nil)
(setq function (downcase (buffer-substring tem (point))))
(setq tem (intern-soft function)
method (get tem 'common-lisp-indent-hook))
(cond ((and (null method)
(string-match ":[^:]+" function))
;; The pleblisp package feature
(setq function (substring function
(1+ (match-beginning 0)))
method (get (intern-soft function)
'common-lisp-indent-hook)))
;; backwards compatibility
(setq method (get tem 'lisp-indent-hook)))))
;; How far into the containing form is the current form?
(if (< (point) indent-point)
(while (condition-case ()
(if (>= (point) indent-point)
(parse-partial-sexp (point)
(setq path (cons n path)))
;; backwards compatibility.
;; (package prefix was stripped off above)
(setq method (cond ((string-match "\\`def"
'(4 (&whole 4 &rest 1) &body))
((string-match "\\`\\(with\\|do\\)-"
;; backwards compatibility. Bletch.
(setq method '(4 (&whole 4 &rest 1) &body))))
(cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`))
(not (eql (char-after (- containing-sexp 2)) ?\#)))
;; No indentation for "'(...)" elements
(setq calculated (1+ sexp-column)))
((eql (char-after (1- containing-sexp)) ?\#)
(setq calculated (1+ sexp-column)))
;; convenient top-level hack.
;; (also compatible with lisp-indent-hook)
;; The number specifies how many `distinguished'
;; forms there are before the body starts
;; Equivalent to (4 4 ... &body)
(setq calculated (cond ((cdr path)
((= (car path) (1+ method))
(+ sexp-column lisp-body-indent))
(setq calculated (funcall method
sexp-column normal-indent)))
(setq calculated (lisp-indent-259
method path state indent-point
sexp-column normal-indent)))))
(goto-char containing-sexp)
(setq last-point containing-sexp)
(progn (backward-up-list 1)
(error (setq depth lisp-indent-maximum-backtracking))))))
(defun lisp-indent-report-bad-format (m)
(error "%s has a badly-formed %s property: %s"
;; Love them free variable references!!
function 'common-lisp-indent-hook m))
;; Blame the crufty control structure on dynamic scoping
(defun lisp-indent-259 (method path state indent-point
sexp-column normal-indent)
(containing-form-start (elt state 1))
;; Isn't tail-recursion wonderful?
;; This while loop is for destructuring.
;; p is set to (cdr p) each iteration.
(if (not (consp method)) (lisp-indent-report-bad-format method))
;; This while loop is for advancing along a method
;; until the relevant (possibly &rest/&body) pattern
;; n is set to (1- n) and method to (cdr method)
; (message "trying %s for %s %s" method p function) (sit-for 1)
(or (eq tem 'nil) ;default indentation
; (eq tem '&lambda) ;abbrev for (&whole 4 (&rest 1))
(and (eq tem '&body) (null (cdr method)))
(consp (cdr method)) (null (cdr (cdr method))))
(integerp tem) ;explicit indentation specified
(and (consp tem) ;destructuring
(or (symbolp (car (cdr tem)))
(integerp (car (cdr tem)))))
(and (symbolp tem) ;a function to call to do the work.
(lisp-indent-report-bad-format method))
(cond ((and tail (not (consp tem)))
;; indent tail of &rest in same way as first elt of rest
(throw 'exit normal-indent))
;; &body means (&rest <lisp-body-indent>)
(if (and (= n 0) ;first body form
(null p)) ;not in subforms
;; this pattern holds for all remaining forms
;; try next element of pattern
;; Too few elements in pattern.
(throw 'exit normal-indent)))
(throw 'exit (list normal-indent containing-form-start)))
; ;; abbrev for (&whole 4 &rest 1)
; (list (+ sexp-column 4) containing-form-start))
(if (null p) ;not in subforms
(list (+ sexp-column tem) containing-form-start)
((symbolp tem) ;a function to call
(funcall tem path state indent-point
sexp-column normal-indent)))
;; must be a destructing frob
(setq method (cdr (cdr tem))
(setq tem (car (cdr tem)))
(list (+ sexp-column tem)
(funcall tem path state indent-point
sexp-column normal-indent))))))))))))
(defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
(if (not (null (cdr path)))
(skip-chars-forward " \t")
(list (cond ((looking-at "\\sw\\|\\s_")
(+ sexp-column lisp-tag-indentation))
((integerp lisp-tag-body-indentation)
(+ sexp-column lisp-tag-body-indentation))
((eq lisp-tag-body-indentation 't)
(progn (backward-sexp 1) (current-column))
(error (1+ sexp-column))))
(t (+ sexp-column lisp-body-indent)))
; (cond ((integerp lisp-tag-body-indentation)
; (+ sexp-column lisp-tag-body-indentation))
; ((eq lisp-tag-body-indentation 't)
; (+ sexp-column lisp-body-indent)))
(defun lisp-indent-do (path state indent-point sexp-column normal-indent)
(let ((lisp-tag-body-indentation lisp-body-indent))
(funcall (function lisp-indent-tagbody)
path state indent-point sexp-column normal-indent))
(funcall (function lisp-indent-259)
;; the following causes wierd indentation
path state indent-point sexp-column normal-indent)))
(defun lisp-indent-function-lambda-hack (path state indent-point
sexp-column normal-indent)
;; indent (function (lambda () <newline> <body-forms>)) kludgily.
(if (or (cdr path) ; wtf?
;; line up under previous body form
;; line up under function rather than under lambda in order to
;; conserve horizontal space. (Which is what #' is for.)
(if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
(+ lisp-body-indent -1 (current-column))
(+ sexp-column lisp-body-indent)))
(error (+ sexp-column lisp-body-indent)))))
(case (4 &rest (&whole 2 &rest 1)))
(ccase . case) (ecase . case)
(typecase . case) (etypecase . case) (ctypecase . case)
(cond (&rest (&whole 2 &rest 1)))
(defconstant . defvar) (defparameter . defvar)
(4 (&whole 4 &rest 1) &body))
(defsetf (4 (&whole 4 &rest 1) 4 &body))
(defun (4 (&whole 4 &rest 1) &body))
(defmacro . defun) (deftype . defun)
(defstruct ((&whole 4 &rest (&whole 2 &rest 1))
&rest (&whole 2 &rest 1)))
((&whole 6 &rest 1) 4 &body))
(dolist ((&whole 4 2 1) &body))
(flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
;; single-else style (then and else equally indented)
;(lambda ((&whole 4 &rest 1) &body))
(lambda ((&whole 4 &rest 1)
&rest lisp-indent-function-lambda-hack))
(let ((&whole 4 &rest (&whole 1 1 2)) &body))
((&whole 6 &rest 1) 4 &body))
;; Combines the worst features of BLOCK, LET and TAGBODY
(prog ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
(return-from (nil &body))
(tagbody lisp-indent-tagbody)
(put (car (car l)) 'common-lisp-indent-hook
(if (symbolp (cdr (car l)))
(get (cdr (car l)) 'common-lisp-indent-hook)
; (flet ((foo (bar baz zap)
;(put 'while 'common-lisp-indent-hook 1)
;(put 'defwrapper'common-lisp-indent-hook ...)
;(put 'def 'common-lisp-indent-hook ...)
;(put 'defflavor 'common-lisp-indent-hook ...)
;(put 'defsubst 'common-lisp-indent-hook ...)
;;(put 'define-restart-name 'common-lisp-indent-hook '1)
;(put 'with-restart 'common-lisp-indent-hook '((1 4 ((* 1))) (2 &body)))
;(put 'restart-case 'common-lisp-indent-hook '((1 4) (* 2 ((0 1) (* 1)))))
;(put 'define-condition 'common-lisp-indent-hook '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
;(put 'with-condition-handler 'common-lisp-indent-hook '((1 4 ((* 1))) (2 &body)))
;(put 'condition-case 'common-lisp-indent-hook '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
;(setq lisp-indent-hook 'common-lisp-indent-hook)
;; To disable this stuff, (setq lisp-indent-hook 'lisp-indent-hook)