1992-05-30 23:54:21 +00:00
|
|
|
|
;;; cl-indent.el --- enhanced lisp-indent mode
|
|
|
|
|
|
2011-01-24 20:08:28 -08:00
|
|
|
|
;; Copyright (C) 1987, 2000-2011 Free Software Foundation, Inc.
|
1993-03-22 03:27:18 +00:00
|
|
|
|
|
1996-01-05 22:21:28 +00:00
|
|
|
|
;; Author: Richard Mlynarik <mly@eddie.mit.edu>
|
1993-03-22 03:27:18 +00:00
|
|
|
|
;; Created: July 1987
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;; Maintainer: FSF
|
1992-07-17 08:15:29 +00:00
|
|
|
|
;; Keywords: lisp, tools
|
2010-08-29 12:17:13 -04:00
|
|
|
|
;; Package: emacs
|
1992-07-16 21:47:34 +00:00
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
|
|
|
|
;; 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
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
1993-03-22 03:27:18 +00:00
|
|
|
|
;; This package supplies a single entry point, common-lisp-indent-function,
|
|
|
|
|
;; which performs indentation in the preferred style for Common Lisp code.
|
|
|
|
|
;; To enable it:
|
|
|
|
|
;;
|
|
|
|
|
;; (setq lisp-indent-function 'common-lisp-indent-function)
|
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;;>> TODO
|
|
|
|
|
;; :foo
|
|
|
|
|
;; bar
|
|
|
|
|
;; :baz
|
|
|
|
|
;; zap
|
|
|
|
|
;; &key (like &body)??
|
|
|
|
|
|
|
|
|
|
;; &rest 1 in lambda-lists doesn't work
|
|
|
|
|
;; -- really want (foo bar
|
|
|
|
|
;; baz)
|
|
|
|
|
;; not (foo bar
|
|
|
|
|
;; baz)
|
|
|
|
|
;; Need something better than &rest for such cases
|
|
|
|
|
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;;; Code:
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
1997-04-12 04:15:03 +00:00
|
|
|
|
(defgroup lisp-indent nil
|
2005-07-04 00:39:30 +00:00
|
|
|
|
"Indentation in Lisp."
|
1997-04-12 04:15:03 +00:00
|
|
|
|
:group 'lisp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defcustom lisp-indent-maximum-backtracking 3
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Maximum depth to backtrack out from a sublist for structured indentation.
|
2005-07-04 00:39:30 +00:00
|
|
|
|
If this variable is 0, no backtracking will occur and forms such as `flet'
|
1997-04-12 04:15:03 +00:00
|
|
|
|
may not be correctly indented."
|
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'lisp-indent)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
1997-04-12 04:15:03 +00:00
|
|
|
|
(defcustom lisp-tag-indentation 1
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Indentation of tags relative to containing list.
|
1997-04-12 04:15:03 +00:00
|
|
|
|
This variable is used by the function `lisp-indent-tagbody'."
|
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'lisp-indent)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
1997-04-12 04:15:03 +00:00
|
|
|
|
(defcustom lisp-tag-body-indentation 3
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Indentation of non-tagged lines relative to containing list.
|
1991-05-09 21:50:45 +00:00
|
|
|
|
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
|
|
|
|
|
the 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
|
1997-04-12 04:15:03 +00:00
|
|
|
|
by `lisp-body-indent'."
|
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'lisp-indent)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
(defcustom lisp-backquote-indentation t
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Whether or not to indent backquoted lists as code.
|
2002-03-12 16:27:15 +00:00
|
|
|
|
If nil, indent backquoted lists as data, i.e., like quoted lists."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'lisp-indent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defcustom lisp-loop-keyword-indentation 3
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Indentation of loop keywords in extended loop forms."
|
2002-03-12 16:27:15 +00:00
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'lisp-indent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defcustom lisp-loop-forms-indentation 5
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Indentation of forms in extended loop forms."
|
2002-03-12 16:27:15 +00:00
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'lisp-indent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defcustom lisp-simple-loop-indentation 3
|
2009-06-22 06:27:00 +00:00
|
|
|
|
"Indentation of forms in simple loop forms."
|
2002-03-12 16:27:15 +00:00
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'lisp-indent)
|
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
2009-06-21 01:16:55 +00:00
|
|
|
|
(defvar lisp-indent-defun-method '(4 &lambda &body)
|
|
|
|
|
"Indentation for function with `common-lisp-indent-function' property `defun'.")
|
1998-05-24 16:58:32 +00:00
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
|
|
|
|
|
(defun extended-loop-p (loop-start)
|
2002-09-07 06:45:43 +00:00
|
|
|
|
"True if an extended loop form starts at LOOP-START."
|
2002-03-12 16:27:15 +00:00
|
|
|
|
(condition-case ()
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char loop-start)
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(forward-sexp 2)
|
|
|
|
|
(backward-sexp 1)
|
|
|
|
|
(looking-at "\\sw"))
|
|
|
|
|
(error t)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun common-lisp-loop-part-indentation (indent-point state)
|
|
|
|
|
"Compute the indentation of loop form constituents."
|
|
|
|
|
(let* ((loop-indentation (save-excursion
|
|
|
|
|
(goto-char (elt state 1))
|
|
|
|
|
(current-column))))
|
|
|
|
|
(goto-char indent-point)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(cond ((not (extended-loop-p (elt state 1)))
|
2002-03-15 08:57:55 +00:00
|
|
|
|
(+ loop-indentation lisp-simple-loop-indentation))
|
2002-03-12 16:27:15 +00:00
|
|
|
|
((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
|
|
|
|
|
(+ loop-indentation lisp-loop-keyword-indentation))
|
|
|
|
|
(t
|
|
|
|
|
(+ loop-indentation lisp-loop-forms-indentation)))))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun common-lisp-indent-function (indent-point state)
|
2009-06-21 01:16:55 +00:00
|
|
|
|
"Function to indent the arguments of a Lisp function call.
|
|
|
|
|
This is suitable for use as the value of the variable
|
|
|
|
|
`lisp-indent-function'. INDENT-POINT is the point at which the
|
|
|
|
|
indentation function is called, and STATE is the
|
|
|
|
|
`parse-partial-sexp' state at that position. Browse the
|
|
|
|
|
`lisp-indent' customize group for options affecting the behavior
|
|
|
|
|
of this function.
|
|
|
|
|
|
|
|
|
|
If the indentation point is in a call to a Lisp function, that
|
|
|
|
|
function's common-lisp-indent-function property specifies how
|
|
|
|
|
this function should indent it. Possible values for this
|
|
|
|
|
property are:
|
|
|
|
|
|
|
|
|
|
* defun, meaning indent according to `lisp-indent-defun-method';
|
|
|
|
|
i.e., like (4 &lambda &body), as explained below.
|
|
|
|
|
|
|
|
|
|
* any other symbol, meaning a function to call. The function should
|
|
|
|
|
take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT.
|
|
|
|
|
PATH is a list of integers describing the position of point in terms of
|
|
|
|
|
list-structure with respect to the containing lists. For example, in
|
|
|
|
|
((a b c (d foo) f) g), foo has a path of (0 3 1). In other words,
|
|
|
|
|
to reach foo take the 0th element of the outermost list, then
|
|
|
|
|
the 3rd element of the next list, and finally the 1st element.
|
|
|
|
|
STATE and INDENT-POINT are as in the arguments to
|
|
|
|
|
`common-lisp-indent-function'. SEXP-COLUMN is the column of
|
|
|
|
|
the open parenthesis of the innermost containing list.
|
|
|
|
|
NORMAL-INDENT is the column the indentation point was
|
|
|
|
|
originally in. This function should behave like `lisp-indent-259'.
|
|
|
|
|
|
|
|
|
|
* an integer N, meaning indent the first N arguments like
|
|
|
|
|
function arguments, and any further arguments like a body.
|
|
|
|
|
This is equivalent to (4 4 ... &body).
|
|
|
|
|
|
|
|
|
|
* a list. The list element in position M specifies how to indent the Mth
|
|
|
|
|
function argument. If there are fewer elements than function arguments,
|
|
|
|
|
the last list element applies to all remaining arguments. The accepted
|
|
|
|
|
list elements are:
|
|
|
|
|
|
|
|
|
|
* nil, meaning the default indentation.
|
|
|
|
|
|
|
|
|
|
* an integer, specifying an explicit indentation.
|
|
|
|
|
|
|
|
|
|
* &lambda. Indent the argument (which may be a list) by 4.
|
|
|
|
|
|
|
|
|
|
* &rest. When used, this must be the penultimate element. The
|
|
|
|
|
element after this one applies to all remaining arguments.
|
|
|
|
|
|
|
|
|
|
* &body. This is equivalent to &rest lisp-body-indent, i.e., indent
|
|
|
|
|
all remaining elements by `lisp-body-indent'.
|
|
|
|
|
|
|
|
|
|
* &whole. This must be followed by nil, an integer, or a
|
|
|
|
|
function symbol. This indentation is applied to the
|
|
|
|
|
associated argument, and as a base indent for all remaining
|
|
|
|
|
arguments. For example, an integer P means indent this
|
|
|
|
|
argument by P, and all remaining arguments by P, plus the
|
|
|
|
|
value specified by their associated list element.
|
|
|
|
|
|
|
|
|
|
* a symbol. A function to call, with the 6 arguments specified above.
|
|
|
|
|
|
|
|
|
|
* a list, with elements as described above. This applies when the
|
|
|
|
|
associated function argument is itself a list. Each element of the list
|
|
|
|
|
specifies how to indent the associated argument.
|
|
|
|
|
|
|
|
|
|
For example, the function `case' has an indent property
|
|
|
|
|
\(4 &rest (&whole 2 &rest 1)), meaning:
|
|
|
|
|
* indent the first argument by 4.
|
|
|
|
|
* arguments after the first should be lists, and there may be any number
|
|
|
|
|
of them. The first list element has an offset of 2, all the rest
|
|
|
|
|
have an offset of 2+1=3."
|
2002-03-12 16:27:15 +00:00
|
|
|
|
(if (save-excursion (goto-char (elt state 1))
|
|
|
|
|
(looking-at "([Ll][Oo][Oo][Pp]"))
|
|
|
|
|
(common-lisp-loop-part-indentation indent-point state)
|
|
|
|
|
(common-lisp-indent-function-1 indent-point state)))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
|
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
(defun common-lisp-indent-function-1 (indent-point state)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(let ((normal-indent (current-column)))
|
|
|
|
|
;; Walk up list levels until we see something
|
|
|
|
|
;; which does special things with subforms.
|
|
|
|
|
(let ((depth 0)
|
|
|
|
|
;; Path describes the position of point in terms of
|
1993-06-09 11:59:12 +00:00
|
|
|
|
;; list-structure with respect to containing lists.
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
|
2009-06-21 01:16:55 +00:00
|
|
|
|
;; (Surely (0 3 1)?).
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(path ())
|
|
|
|
|
;; set non-nil when somebody works out the indentation to use
|
|
|
|
|
calculated
|
2002-05-29 16:40:34 +00:00
|
|
|
|
;; If non-nil, this is an indentation to use
|
|
|
|
|
;; if nothing else specifies it more firmly.
|
|
|
|
|
tentative-calculated
|
|
|
|
|
(last-point indent-point)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; the position of the open-paren of the innermost containing list
|
|
|
|
|
(containing-form-start (elt state 1))
|
|
|
|
|
;; the column of the above
|
|
|
|
|
sexp-column)
|
|
|
|
|
;; Move to start of innermost containing list
|
|
|
|
|
(goto-char containing-form-start)
|
|
|
|
|
(setq sexp-column (current-column))
|
2002-03-12 16:27:15 +00:00
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; Look over successively less-deep containing forms
|
|
|
|
|
(while (and (not calculated)
|
|
|
|
|
(< depth lisp-indent-maximum-backtracking))
|
|
|
|
|
(let ((containing-sexp (point)))
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(parse-partial-sexp (point) indent-point 1 t)
|
|
|
|
|
;; Move to the car of the relevant containing form
|
2002-05-29 16:40:34 +00:00
|
|
|
|
(let (tem function method tentative-defun)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(if (not (looking-at "\\sw\\|\\s_"))
|
|
|
|
|
;; This form doesn't seem to start with a symbol
|
|
|
|
|
(setq function nil method nil)
|
|
|
|
|
(setq tem (point))
|
|
|
|
|
(forward-sexp 1)
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(setq function (downcase (buffer-substring-no-properties
|
|
|
|
|
tem (point))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(goto-char tem)
|
|
|
|
|
(setq tem (intern-soft function)
|
|
|
|
|
method (get tem 'common-lisp-indent-function))
|
|
|
|
|
(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-function)))
|
|
|
|
|
((and (null method))
|
|
|
|
|
;; backwards compatibility
|
|
|
|
|
(setq method (get tem 'lisp-indent-function)))))
|
|
|
|
|
(let ((n 0))
|
|
|
|
|
;; How far into the containing form is the current form?
|
|
|
|
|
(if (< (point) indent-point)
|
|
|
|
|
(while (condition-case ()
|
|
|
|
|
(progn
|
|
|
|
|
(forward-sexp 1)
|
|
|
|
|
(if (>= (point) indent-point)
|
|
|
|
|
nil
|
|
|
|
|
(parse-partial-sexp (point)
|
|
|
|
|
indent-point 1 t)
|
|
|
|
|
(setq n (1+ n))
|
|
|
|
|
t))
|
|
|
|
|
(error nil))))
|
|
|
|
|
(setq path (cons n path)))
|
|
|
|
|
|
|
|
|
|
;; backwards compatibility.
|
|
|
|
|
(cond ((null function))
|
|
|
|
|
((null method)
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(when (null (cdr path))
|
2002-05-29 16:40:34 +00:00
|
|
|
|
;; (package prefix was stripped off above)
|
|
|
|
|
(cond ((string-match "\\`def"
|
|
|
|
|
function)
|
|
|
|
|
(setq tentative-defun t))
|
2004-05-17 17:00:00 +00:00
|
|
|
|
((string-match
|
|
|
|
|
(eval-when-compile
|
|
|
|
|
(concat "\\`\\("
|
|
|
|
|
(regexp-opt '("with" "without" "do"))
|
|
|
|
|
"\\)-"))
|
|
|
|
|
function)
|
2002-05-29 16:40:34 +00:00
|
|
|
|
(setq method '(&lambda &body))))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; backwards compatibility. Bletch.
|
|
|
|
|
((eq method 'defun)
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(setq method lisp-indent-defun-method)))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
(cond ((and (or (eq (char-after (1- containing-sexp)) ?\')
|
|
|
|
|
(and (not lisp-backquote-indentation)
|
|
|
|
|
(eq (char-after (1- containing-sexp)) ?\`)))
|
1999-10-16 12:00:02 +00:00
|
|
|
|
(not (eq (char-after (- containing-sexp 2)) ?\#)))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; No indentation for "'(...)" elements
|
|
|
|
|
(setq calculated (1+ sexp-column)))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
((or (eq (char-after (1- containing-sexp)) ?\,)
|
|
|
|
|
(and (eq (char-after (1- containing-sexp)) ?\@)
|
|
|
|
|
(eq (char-after (- containing-sexp 2)) ?\,)))
|
|
|
|
|
;; ",(...)" or ",@(...)"
|
|
|
|
|
(setq calculated normal-indent))
|
1999-10-16 12:00:02 +00:00
|
|
|
|
((eq (char-after (1- containing-sexp)) ?\#)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;; "#(...)"
|
|
|
|
|
(setq calculated (1+ sexp-column)))
|
2002-05-29 16:40:34 +00:00
|
|
|
|
((null method)
|
|
|
|
|
;; If this looks like a call to a `def...' form,
|
|
|
|
|
;; think about indenting it as one, but do it
|
|
|
|
|
;; tentatively for cases like
|
|
|
|
|
;; (flet ((defunp ()
|
|
|
|
|
;; nil)))
|
|
|
|
|
;; Set both normal-indent and tentative-calculated.
|
|
|
|
|
;; The latter ensures this value gets used
|
|
|
|
|
;; if there are no relevant containing constructs.
|
|
|
|
|
;; The former ensures this value gets used
|
|
|
|
|
;; if there is a relevant containing construct
|
|
|
|
|
;; but we are nested within the structure levels
|
|
|
|
|
;; that it specifies indentation for.
|
|
|
|
|
(if tentative-defun
|
|
|
|
|
(setq tentative-calculated
|
|
|
|
|
(common-lisp-indent-call-method
|
|
|
|
|
function lisp-indent-defun-method
|
|
|
|
|
path state indent-point
|
|
|
|
|
sexp-column normal-indent)
|
|
|
|
|
normal-indent tentative-calculated)))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
((integerp method)
|
|
|
|
|
;; convenient top-level hack.
|
|
|
|
|
;; (also compatible with lisp-indent-function)
|
|
|
|
|
;; The number specifies how many `distinguished'
|
|
|
|
|
;; forms there are before the body starts
|
|
|
|
|
;; Equivalent to (4 4 ... &body)
|
|
|
|
|
(setq calculated (cond ((cdr path)
|
|
|
|
|
normal-indent)
|
|
|
|
|
((<= (car path) method)
|
|
|
|
|
;; `distinguished' form
|
|
|
|
|
(list (+ sexp-column 4)
|
|
|
|
|
containing-form-start))
|
|
|
|
|
((= (car path) (1+ method))
|
|
|
|
|
;; first body form.
|
|
|
|
|
(+ sexp-column lisp-body-indent))
|
|
|
|
|
(t
|
|
|
|
|
;; other body form
|
|
|
|
|
normal-indent))))
|
2002-05-29 16:40:34 +00:00
|
|
|
|
(t
|
|
|
|
|
(setq calculated
|
|
|
|
|
(common-lisp-indent-call-method
|
|
|
|
|
function method path state indent-point
|
|
|
|
|
sexp-column normal-indent)))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(goto-char containing-sexp)
|
|
|
|
|
(setq last-point containing-sexp)
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(unless calculated
|
2002-05-29 16:40:34 +00:00
|
|
|
|
(condition-case ()
|
|
|
|
|
(progn (backward-up-list 1)
|
|
|
|
|
(setq depth (1+ depth)))
|
|
|
|
|
(error (setq depth lisp-indent-maximum-backtracking))))))
|
|
|
|
|
(or calculated tentative-calculated))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun common-lisp-indent-call-method (function method path state indent-point
|
|
|
|
|
sexp-column normal-indent)
|
|
|
|
|
(let ((lisp-indent-error-function function))
|
|
|
|
|
(if (symbolp method)
|
|
|
|
|
(funcall method
|
|
|
|
|
path state indent-point
|
|
|
|
|
sexp-column normal-indent)
|
|
|
|
|
(lisp-indent-259 method path state indent-point
|
|
|
|
|
sexp-column normal-indent))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
2009-06-22 06:27:00 +00:00
|
|
|
|
;; Dynamically bound in common-lisp-indent-call-method.
|
|
|
|
|
(defvar lisp-indent-error-function)
|
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(defun lisp-indent-report-bad-format (m)
|
|
|
|
|
(error "%s has a badly-formed %s property: %s"
|
|
|
|
|
;; Love those free variable references!!
|
1998-05-24 16:58:32 +00:00
|
|
|
|
lisp-indent-error-function 'common-lisp-indent-function m))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
|
|
|
|
;; Blame the crufty control structure on dynamic scoping
|
|
|
|
|
;; -- not on me!
|
|
|
|
|
(defun lisp-indent-259 (method path state indent-point
|
|
|
|
|
sexp-column normal-indent)
|
|
|
|
|
(catch 'exit
|
|
|
|
|
(let ((p path)
|
|
|
|
|
(containing-form-start (elt state 1))
|
|
|
|
|
n tem tail)
|
|
|
|
|
;; Isn't tail-recursion wonderful?
|
|
|
|
|
(while p
|
|
|
|
|
;; This while loop is for destructuring.
|
|
|
|
|
;; p is set to (cdr p) each iteration.
|
|
|
|
|
(if (not (consp method)) (lisp-indent-report-bad-format method))
|
|
|
|
|
(setq n (1- (car p))
|
|
|
|
|
p (cdr p)
|
|
|
|
|
tail nil)
|
|
|
|
|
(while n
|
|
|
|
|
;; This while loop is for advancing along a method
|
|
|
|
|
;; until the relevant (possibly &rest/&body) pattern
|
|
|
|
|
;; is reached.
|
|
|
|
|
;; n is set to (1- n) and method to (cdr method)
|
|
|
|
|
;; each iteration.
|
|
|
|
|
(setq tem (car method))
|
|
|
|
|
|
|
|
|
|
(or (eq tem 'nil) ;default indentation
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(eq tem '&lambda) ;lambda list
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(and (eq tem '&body) (null (cdr method)))
|
|
|
|
|
(and (eq tem '&rest)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(consp (cdr method))
|
|
|
|
|
(null (cddr method)))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(integerp tem) ;explicit indentation specified
|
|
|
|
|
(and (consp tem) ;destructuring
|
|
|
|
|
(eq (car tem) '&whole)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(or (symbolp (cadr tem))
|
|
|
|
|
(integerp (cadr tem))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(and (symbolp tem) ;a function to call to do the work.
|
|
|
|
|
(null (cdr method)))
|
|
|
|
|
(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))
|
|
|
|
|
((eq tem '&body)
|
|
|
|
|
;; &body means (&rest <lisp-body-indent>)
|
|
|
|
|
(throw 'exit
|
|
|
|
|
(if (and (= n 0) ;first body form
|
|
|
|
|
(null p)) ;not in subforms
|
|
|
|
|
(+ sexp-column
|
|
|
|
|
lisp-body-indent)
|
|
|
|
|
normal-indent)))
|
|
|
|
|
((eq tem '&rest)
|
|
|
|
|
;; this pattern holds for all remaining forms
|
|
|
|
|
(setq tail (> n 0)
|
|
|
|
|
n 0
|
|
|
|
|
method (cdr method)))
|
|
|
|
|
((> n 0)
|
|
|
|
|
;; try next element of pattern
|
|
|
|
|
(setq n (1- n)
|
|
|
|
|
method (cdr method))
|
|
|
|
|
(if (< n 0)
|
|
|
|
|
;; Too few elements in pattern.
|
|
|
|
|
(throw 'exit normal-indent)))
|
|
|
|
|
((eq tem 'nil)
|
2006-10-27 00:52:06 +00:00
|
|
|
|
(throw 'exit (if (consp normal-indent)
|
|
|
|
|
normal-indent
|
|
|
|
|
(list normal-indent containing-form-start))))
|
2006-10-26 20:39:53 +00:00
|
|
|
|
((eq tem '&lambda)
|
|
|
|
|
(throw 'exit
|
|
|
|
|
(cond ((null p)
|
|
|
|
|
(list (+ sexp-column 4) containing-form-start))
|
|
|
|
|
((null (cdr p))
|
|
|
|
|
(+ sexp-column 1))
|
|
|
|
|
(t normal-indent))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
((integerp tem)
|
|
|
|
|
(throw 'exit
|
|
|
|
|
(if (null p) ;not in subforms
|
|
|
|
|
(list (+ sexp-column tem) containing-form-start)
|
|
|
|
|
normal-indent)))
|
|
|
|
|
((symbolp tem) ;a function to call
|
|
|
|
|
(throw 'exit
|
|
|
|
|
(funcall tem path state indent-point
|
|
|
|
|
sexp-column normal-indent)))
|
|
|
|
|
(t
|
|
|
|
|
;; must be a destructing frob
|
|
|
|
|
(if (not (null p))
|
|
|
|
|
;; descend
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(setq method (cddr tem)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
n nil)
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(setq tem (cadr tem))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(throw 'exit
|
|
|
|
|
(cond (tail
|
|
|
|
|
normal-indent)
|
|
|
|
|
((eq tem 'nil)
|
|
|
|
|
(list normal-indent
|
|
|
|
|
containing-form-start))
|
|
|
|
|
((integerp tem)
|
|
|
|
|
(list (+ sexp-column tem)
|
|
|
|
|
containing-form-start))
|
|
|
|
|
(t
|
|
|
|
|
(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)))
|
|
|
|
|
normal-indent
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char indent-point)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(list (cond ((looking-at "\\sw\\|\\s_")
|
|
|
|
|
;; a tagbody tag
|
|
|
|
|
(+ sexp-column lisp-tag-indentation))
|
|
|
|
|
((integerp lisp-tag-body-indentation)
|
|
|
|
|
(+ sexp-column lisp-tag-body-indentation))
|
|
|
|
|
((eq lisp-tag-body-indentation 't)
|
|
|
|
|
(condition-case ()
|
|
|
|
|
(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)
|
|
|
|
|
; normal-indent)
|
|
|
|
|
; (t
|
|
|
|
|
; (+ sexp-column lisp-body-indent)))
|
|
|
|
|
(elt state 1)
|
|
|
|
|
))))
|
|
|
|
|
|
|
|
|
|
(defun lisp-indent-do (path state indent-point sexp-column normal-indent)
|
|
|
|
|
(if (>= (car path) 3)
|
|
|
|
|
(let ((lisp-tag-body-indentation lisp-body-indent))
|
|
|
|
|
(funcall (function lisp-indent-tagbody)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
path state indent-point sexp-column normal-indent))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(funcall (function lisp-indent-259)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
'((&whole nil &rest
|
|
|
|
|
;; the following causes weird indentation
|
|
|
|
|
;;(&whole 1 1 2 nil)
|
|
|
|
|
)
|
|
|
|
|
(&whole nil &rest 1))
|
|
|
|
|
path state indent-point sexp-column normal-indent)))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
|
2001-11-12 19:58:33 +00:00
|
|
|
|
(defun lisp-indent-defmethod (path state indent-point sexp-column
|
2001-08-15 11:57:03 +00:00
|
|
|
|
normal-indent)
|
|
|
|
|
"Indentation function defmethod."
|
2002-03-12 16:27:15 +00:00
|
|
|
|
(lisp-indent-259 (if (and (>= (car path) 3)
|
|
|
|
|
(null (cdr path))
|
2001-11-19 06:24:05 +00:00
|
|
|
|
(save-excursion (goto-char (elt state 1))
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(forward-sexp 3)
|
|
|
|
|
(backward-sexp)
|
2004-08-21 12:24:13 +00:00
|
|
|
|
(looking-at ":\\|\\sw+")))
|
2001-08-15 11:57:03 +00:00
|
|
|
|
'(4 4 (&whole 4 &rest 4) &body)
|
|
|
|
|
(get 'defun 'common-lisp-indent-function))
|
|
|
|
|
path state indent-point sexp-column normal-indent))
|
|
|
|
|
|
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
(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?
|
|
|
|
|
(> (car path) 3))
|
|
|
|
|
;; line up under previous body form
|
|
|
|
|
normal-indent
|
|
|
|
|
;; line up under function rather than under lambda in order to
|
|
|
|
|
;; conserve horizontal space. (Which is what #' is for.)
|
|
|
|
|
(condition-case ()
|
|
|
|
|
(save-excursion
|
|
|
|
|
(backward-up-list 2)
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
|
|
|
|
|
(+ lisp-body-indent -1 (current-column))
|
|
|
|
|
(+ sexp-column lisp-body-indent)))
|
|
|
|
|
(error (+ sexp-column lisp-body-indent)))))
|
|
|
|
|
|
2002-03-12 16:27:15 +00:00
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
|
|
|
|
(let ((l '((block 1)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(case (4 &rest (&whole 2 &rest 1)))
|
2009-06-22 06:27:00 +00:00
|
|
|
|
(ccase . case)
|
|
|
|
|
(ecase . case)
|
|
|
|
|
(typecase . case)
|
|
|
|
|
(etypecase . case)
|
|
|
|
|
(ctypecase . case)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(catch 1)
|
|
|
|
|
(cond (&rest (&whole 2 &rest 1)))
|
|
|
|
|
(defvar (4 2 2))
|
2000-08-07 16:51:57 +00:00
|
|
|
|
(defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1)))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(defconstant . defvar)
|
2000-01-06 23:46:13 +00:00
|
|
|
|
(defcustom (4 2 2 2))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(defparameter . defvar)
|
2000-08-07 16:51:57 +00:00
|
|
|
|
(defconst . defcustom)
|
|
|
|
|
(define-condition . defclass)
|
2001-12-22 00:03:08 +00:00
|
|
|
|
(define-modify-macro (4 &lambda &body))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(defsetf (4 &lambda 4 &body))
|
|
|
|
|
(defun (4 &lambda &body))
|
|
|
|
|
(define-setf-method . defun)
|
|
|
|
|
(define-setf-expander . defun)
|
2009-06-22 06:27:00 +00:00
|
|
|
|
(defmacro . defun)
|
|
|
|
|
(defsubst . defun)
|
|
|
|
|
(deftype . defun)
|
2001-08-15 11:57:03 +00:00
|
|
|
|
(defmethod lisp-indent-defmethod)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(defpackage (4 2))
|
|
|
|
|
(defstruct ((&whole 4 &rest (&whole 2 &rest 1))
|
|
|
|
|
&rest (&whole 2 &rest 1)))
|
|
|
|
|
(destructuring-bind
|
|
|
|
|
((&whole 6 &rest 1) 4 &body))
|
|
|
|
|
(do lisp-indent-do)
|
|
|
|
|
(do* . do)
|
|
|
|
|
(dolist ((&whole 4 2 1) &body))
|
|
|
|
|
(dotimes . dolist)
|
|
|
|
|
(eval-when 1)
|
|
|
|
|
(flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
|
|
|
|
|
(labels . flet)
|
|
|
|
|
(macrolet . flet)
|
2009-06-22 06:27:00 +00:00
|
|
|
|
(generic-flet . flet)
|
|
|
|
|
(generic-labels . flet)
|
1998-07-31 03:21:07 +00:00
|
|
|
|
(handler-case (4 &rest (&whole 2 &lambda &body)))
|
1998-07-14 08:20:17 +00:00
|
|
|
|
(restart-case . handler-case)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
;; `else-body' style
|
|
|
|
|
(if (nil nil &body))
|
|
|
|
|
;; single-else style (then and else equally indented)
|
|
|
|
|
(if (&rest nil))
|
|
|
|
|
(lambda (&lambda &rest lisp-indent-function-lambda-hack))
|
|
|
|
|
(let ((&whole 4 &rest (&whole 1 1 2)) &body))
|
|
|
|
|
(let* . let)
|
|
|
|
|
(compiler-let . let) ;barf
|
2009-06-22 06:27:00 +00:00
|
|
|
|
(handler-bind . let)
|
|
|
|
|
(restart-bind . let)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(locally 1)
|
2002-03-12 16:27:15 +00:00
|
|
|
|
;(loop lisp-indent-loop)
|
2001-05-16 21:13:21 +00:00
|
|
|
|
(:method (&lambda &body)) ; in `defgeneric'
|
2000-08-07 16:51:57 +00:00
|
|
|
|
(multiple-value-bind ((&whole 6 &rest 1) 4 &body))
|
|
|
|
|
(multiple-value-call (4 &body))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(multiple-value-prog1 1)
|
|
|
|
|
(multiple-value-setq (4 2))
|
|
|
|
|
(multiple-value-setf . multiple-value-setq)
|
2000-02-25 15:46:24 +00:00
|
|
|
|
(pprint-logical-block (4 2))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(print-unreadable-object ((&whole 4 1 &rest 1) &body))
|
|
|
|
|
;; Combines the worst features of BLOCK, LET and TAGBODY
|
|
|
|
|
(prog (&lambda &rest lisp-indent-tagbody))
|
|
|
|
|
(prog* . prog)
|
|
|
|
|
(prog1 1)
|
|
|
|
|
(prog2 2)
|
|
|
|
|
(progn 0)
|
|
|
|
|
(progv (4 4 &body))
|
|
|
|
|
(return 0)
|
|
|
|
|
(return-from (nil &body))
|
2003-04-25 02:00:17 +00:00
|
|
|
|
(symbol-macrolet . let)
|
2000-06-20 15:01:59 +00:00
|
|
|
|
(tagbody lisp-indent-tagbody)
|
|
|
|
|
(throw 1)
|
|
|
|
|
(unless 1)
|
|
|
|
|
(unwind-protect (5 &body))
|
1998-05-22 04:42:48 +00:00
|
|
|
|
(when 1)
|
2001-11-12 19:58:33 +00:00
|
|
|
|
(with-accessors . multiple-value-bind)
|
|
|
|
|
(with-condition-restarts . multiple-value-bind)
|
1998-11-16 06:37:55 +00:00
|
|
|
|
(with-output-to-string (4 2))
|
2000-08-07 16:51:57 +00:00
|
|
|
|
(with-slots . multiple-value-bind)
|
1998-05-22 04:42:48 +00:00
|
|
|
|
(with-standard-io-syntax (2)))))
|
2001-11-12 19:58:33 +00:00
|
|
|
|
(dolist (el l)
|
|
|
|
|
(put (car el) 'common-lisp-indent-function
|
|
|
|
|
(if (symbolp (cdr el))
|
|
|
|
|
(get (cdr el) 'common-lisp-indent-function)
|
|
|
|
|
(car (cdr el))))))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;(defun foo (x)
|
|
|
|
|
; (tagbody
|
|
|
|
|
; foo
|
|
|
|
|
; (bar)
|
|
|
|
|
; baz
|
|
|
|
|
; (when (losing)
|
|
|
|
|
; (with-big-loser
|
|
|
|
|
; (yow)
|
|
|
|
|
; ((lambda ()
|
|
|
|
|
; foo)
|
|
|
|
|
; big)))
|
|
|
|
|
; (flet ((foo (bar baz zap)
|
|
|
|
|
; (zip))
|
|
|
|
|
; (zot ()
|
|
|
|
|
; quux))
|
|
|
|
|
; (do ()
|
|
|
|
|
; ((lose)
|
|
|
|
|
; (foo 1))
|
|
|
|
|
; (quux)
|
|
|
|
|
; foo
|
|
|
|
|
; (lose))
|
|
|
|
|
; (cond ((x)
|
|
|
|
|
; (win 1 2
|
|
|
|
|
; (foo)))
|
|
|
|
|
; (t
|
|
|
|
|
; (lose
|
|
|
|
|
; 3))))))
|
2000-06-20 15:01:59 +00:00
|
|
|
|
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
|
|
|
|
;(put 'while 'common-lisp-indent-function 1)
|
|
|
|
|
;(put 'defwrapper'common-lisp-indent-function ...)
|
|
|
|
|
;(put 'def 'common-lisp-indent-function ...)
|
|
|
|
|
;(put 'defflavor 'common-lisp-indent-function ...)
|
|
|
|
|
;(put 'defsubst 'common-lisp-indent-function ...)
|
|
|
|
|
|
|
|
|
|
;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
|
|
|
|
|
;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
|
2000-12-04 17:05:57 +00:00
|
|
|
|
;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
|
1991-05-09 21:50:45 +00:00
|
|
|
|
;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
|
|
|
|
|
;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
|
2000-12-04 17:05:57 +00:00
|
|
|
|
;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
|
|
|
|
|
;(put 'defgeneric 'common-lisp-indent-function 'defun)
|
1991-05-09 21:50:45 +00:00
|
|
|
|
|
1992-05-30 23:54:21 +00:00
|
|
|
|
;;; cl-indent.el ends here
|