cl-indent.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. ;;; cl-indent.el --- enhanced lisp-indent mode
  2. ;; Copyright (C) 1987, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
  4. ;; Created: July 1987
  5. ;; Maintainer: FSF
  6. ;; Keywords: lisp, tools
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This package supplies a single entry point, common-lisp-indent-function,
  21. ;; which performs indentation in the preferred style for Common Lisp code.
  22. ;; To enable it:
  23. ;;
  24. ;; (setq lisp-indent-function 'common-lisp-indent-function)
  25. ;;; Code:
  26. (eval-when-compile (require 'cl))
  27. (defgroup lisp-indent nil
  28. "Indentation in Lisp."
  29. :group 'lisp)
  30. (defcustom lisp-indent-maximum-backtracking 3
  31. "Maximum depth to backtrack out from a sublist for structured indentation.
  32. If this variable is 0, no backtracking will occur and forms such as `flet'
  33. may not be correctly indented."
  34. :type 'integer
  35. :group 'lisp-indent)
  36. (defcustom lisp-tag-indentation 1
  37. "Indentation of tags relative to containing list.
  38. This variable is used by the function `lisp-indent-tagbody'."
  39. :type 'integer
  40. :group 'lisp-indent)
  41. (defcustom lisp-tag-body-indentation 3
  42. "Indentation of non-tagged lines relative to containing list.
  43. This variable is used by the function `lisp-indent-tagbody' to indent normal
  44. lines (lines without tags).
  45. The indentation is relative to the indentation of the parenthesis enclosing
  46. the special form. If the value is t, the body of tags will be indented
  47. as a block at the same indentation as the first s-expression following
  48. the tag. In this case, any forms before the first tag are indented
  49. by `lisp-body-indent'."
  50. :type 'integer
  51. :group 'lisp-indent)
  52. (defcustom lisp-backquote-indentation t
  53. "Whether or not to indent backquoted lists as code.
  54. If nil, indent backquoted lists as data, i.e., like quoted lists."
  55. :type 'boolean
  56. :group 'lisp-indent)
  57. (defcustom lisp-loop-keyword-indentation 3
  58. "Indentation of loop keywords in extended loop forms."
  59. :type 'integer
  60. :group 'lisp-indent)
  61. (defcustom lisp-loop-forms-indentation 5
  62. "Indentation of forms in extended loop forms."
  63. :type 'integer
  64. :group 'lisp-indent)
  65. (defcustom lisp-simple-loop-indentation 3
  66. "Indentation of forms in simple loop forms."
  67. :type 'integer
  68. :group 'lisp-indent)
  69. (defcustom lisp-lambda-list-keyword-alignment nil
  70. "Whether to vertically align lambda-list keywords together.
  71. If nil (the default), keyworded lambda-list parts are aligned
  72. with the initial mandatory arguments, like this:
  73. \(defun foo (arg1 arg2 &rest rest
  74. &key key1 key2)
  75. #|...|#)
  76. If non-nil, alignment is done with the first keyword
  77. \(or falls back to the previous case), as in:
  78. \(defun foo (arg1 arg2 &rest rest
  79. &key key1 key2)
  80. #|...|#)"
  81. :version "24.1"
  82. :type 'boolean
  83. :group 'lisp-indent)
  84. (defcustom lisp-lambda-list-keyword-parameter-indentation 2
  85. "Indentation of lambda list keyword parameters.
  86. See `lisp-lambda-list-keyword-parameter-alignment'
  87. for more information."
  88. :version "24.1"
  89. :type 'integer
  90. :group 'lisp-indent)
  91. (defcustom lisp-lambda-list-keyword-parameter-alignment nil
  92. "Whether to vertically align lambda-list keyword parameters together.
  93. If nil (the default), the parameters are aligned
  94. with their corresponding keyword, plus the value of
  95. `lisp-lambda-list-keyword-parameter-indentation', like this:
  96. \(defun foo (arg1 arg2 &key key1 key2
  97. key3 key4)
  98. #|...|#)
  99. If non-nil, alignment is done with the first parameter
  100. \(or falls back to the previous case), as in:
  101. \(defun foo (arg1 arg2 &key key1 key2
  102. key3 key4)
  103. #|...|#)"
  104. :version "24.1"
  105. :type 'boolean
  106. :group 'lisp-indent)
  107. (defvar lisp-indent-defun-method '(4 &lambda &body)
  108. "Defun-like indentation method.
  109. This applies when the value of the `common-lisp-indent-function' property
  110. is set to `defun'.")
  111. (defun extended-loop-p (loop-start)
  112. "True if an extended loop form starts at LOOP-START."
  113. (condition-case ()
  114. (save-excursion
  115. (goto-char loop-start)
  116. (forward-char 1)
  117. (forward-sexp 2)
  118. (backward-sexp 1)
  119. (looking-at "\\sw"))
  120. (error t)))
  121. (defun common-lisp-loop-part-indentation (indent-point state)
  122. "Compute the indentation of loop form constituents."
  123. (let* ((loop-indentation (save-excursion
  124. (goto-char (elt state 1))
  125. (current-column))))
  126. (goto-char indent-point)
  127. (beginning-of-line)
  128. (list
  129. (cond ((not (extended-loop-p (elt state 1)))
  130. (+ loop-indentation lisp-simple-loop-indentation))
  131. ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
  132. (+ loop-indentation lisp-loop-keyword-indentation))
  133. (t
  134. (+ loop-indentation lisp-loop-forms-indentation)))
  135. ;; Tell the caller that the next line needs recomputation, even
  136. ;; though it doesn't start a sexp.
  137. loop-indentation)))
  138. ;; Cf (info "(elisp)Specification List")
  139. ;;;###autoload
  140. (defun common-lisp-indent-function (indent-point state)
  141. "Function to indent the arguments of a Lisp function call.
  142. This is suitable for use as the value of the variable
  143. `lisp-indent-function'. INDENT-POINT is the point at which the
  144. indentation function is called, and STATE is the
  145. `parse-partial-sexp' state at that position. Browse the
  146. `lisp-indent' customize group for options affecting the behavior
  147. of this function.
  148. If the indentation point is in a call to a Lisp function, that
  149. function's `common-lisp-indent-function' property specifies how
  150. this function should indent it. Possible values for this
  151. property are:
  152. * defun, meaning indent according to `lisp-indent-defun-method';
  153. i.e., like (4 &lambda &body), as explained below.
  154. * any other symbol, meaning a function to call. The function should
  155. take the arguments: PATH STATE INDENT-POINT SEXP-COLUMN NORMAL-INDENT.
  156. PATH is a list of integers describing the position of point in terms of
  157. list-structure with respect to the containing lists. For example, in
  158. ((a b c (d foo) f) g), foo has a path of (0 3 1). In other words,
  159. to reach foo take the 0th element of the outermost list, then
  160. the 3rd element of the next list, and finally the 1st element.
  161. STATE and INDENT-POINT are as in the arguments to
  162. `common-lisp-indent-function'. SEXP-COLUMN is the column of
  163. the open parenthesis of the innermost containing list.
  164. NORMAL-INDENT is the column the indentation point was
  165. originally in. This function should behave like `lisp-indent-259'.
  166. * an integer N, meaning indent the first N arguments like
  167. function arguments, and any further arguments like a body.
  168. This is equivalent to (4 4 ... &body).
  169. * a list. The list element in position M specifies how to indent the Mth
  170. function argument. If there are fewer elements than function arguments,
  171. the last list element applies to all remaining arguments. The accepted
  172. list elements are:
  173. * nil, meaning the default indentation.
  174. * an integer, specifying an explicit indentation.
  175. * &lambda. Indent the argument (which may be a list) by 4.
  176. * &rest. When used, this must be the penultimate element. The
  177. element after this one applies to all remaining arguments.
  178. * &body. This is equivalent to &rest lisp-body-indent, i.e., indent
  179. all remaining elements by `lisp-body-indent'.
  180. * &whole. This must be followed by nil, an integer, or a
  181. function symbol. This indentation is applied to the
  182. associated argument, and as a base indent for all remaining
  183. arguments. For example, an integer P means indent this
  184. argument by P, and all remaining arguments by P, plus the
  185. value specified by their associated list element.
  186. * a symbol. A function to call, with the 6 arguments specified above.
  187. * a list, with elements as described above. This applies when the
  188. associated function argument is itself a list. Each element of the list
  189. specifies how to indent the associated argument.
  190. For example, the function `case' has an indent property
  191. \(4 &rest (&whole 2 &rest 1)), meaning:
  192. * indent the first argument by 4.
  193. * arguments after the first should be lists, and there may be any number
  194. of them. The first list element has an offset of 2, all the rest
  195. have an offset of 2+1=3."
  196. (if (save-excursion (goto-char (elt state 1))
  197. (looking-at "([Ll][Oo][Oo][Pp]"))
  198. (common-lisp-loop-part-indentation indent-point state)
  199. (common-lisp-indent-function-1 indent-point state)))
  200. (defun common-lisp-indent-function-1 (indent-point state)
  201. (let ((normal-indent (current-column)))
  202. ;; Walk up list levels until we see something
  203. ;; which does special things with subforms.
  204. (let ((depth 0)
  205. ;; Path describes the position of point in terms of
  206. ;; list-structure with respect to containing lists.
  207. ;; `foo' has a path of (0 3 1) in `((a b c (d foo) f) g)'.
  208. (path ())
  209. ;; set non-nil when somebody works out the indentation to use
  210. calculated
  211. ;; If non-nil, this is an indentation to use
  212. ;; if nothing else specifies it more firmly.
  213. tentative-calculated
  214. (last-point indent-point)
  215. ;; the position of the open-paren of the innermost containing list
  216. (containing-form-start (elt state 1))
  217. ;; the column of the above
  218. sexp-column)
  219. ;; Move to start of innermost containing list
  220. (goto-char containing-form-start)
  221. (setq sexp-column (current-column))
  222. ;; Look over successively less-deep containing forms
  223. (while (and (not calculated)
  224. (< depth lisp-indent-maximum-backtracking))
  225. (let ((containing-sexp (point)))
  226. (forward-char 1)
  227. (parse-partial-sexp (point) indent-point 1 t)
  228. ;; Move to the car of the relevant containing form
  229. (let (tem function method tentative-defun)
  230. (if (not (looking-at "\\sw\\|\\s_"))
  231. ;; This form doesn't seem to start with a symbol
  232. (setq function nil method nil)
  233. (setq tem (point))
  234. (forward-sexp 1)
  235. (setq function (downcase (buffer-substring-no-properties
  236. tem (point))))
  237. (goto-char tem)
  238. (setq tem (intern-soft function)
  239. method (get tem 'common-lisp-indent-function))
  240. (cond ((and (null method)
  241. (string-match ":[^:]+" function))
  242. ;; The pleblisp package feature
  243. (setq function (substring function
  244. (1+ (match-beginning 0)))
  245. method (get (intern-soft function)
  246. 'common-lisp-indent-function)))
  247. ((and (null method))
  248. ;; backwards compatibility
  249. (setq method (get tem 'lisp-indent-function)))))
  250. (let ((n 0))
  251. ;; How far into the containing form is the current form?
  252. (if (< (point) indent-point)
  253. (while (condition-case ()
  254. (progn
  255. (forward-sexp 1)
  256. (if (>= (point) indent-point)
  257. nil
  258. (parse-partial-sexp (point)
  259. indent-point 1 t)
  260. (setq n (1+ n))
  261. t))
  262. (error nil))))
  263. (setq path (cons n path)))
  264. ;; backwards compatibility.
  265. (cond ((null function))
  266. ((null method)
  267. (when (null (cdr path))
  268. ;; (package prefix was stripped off above)
  269. (cond ((string-match "\\`def"
  270. function)
  271. (setq tentative-defun t))
  272. ((string-match
  273. (eval-when-compile
  274. (concat "\\`\\("
  275. (regexp-opt '("with" "without" "do"))
  276. "\\)-"))
  277. function)
  278. (setq method '(&lambda &body))))))
  279. ;; backwards compatibility. Bletch.
  280. ((eq method 'defun)
  281. (setq method lisp-indent-defun-method)))
  282. (cond ((and (or (eq (char-after (1- containing-sexp)) ?\')
  283. (and (not lisp-backquote-indentation)
  284. (eq (char-after (1- containing-sexp)) ?\`)))
  285. (not (eq (char-after (- containing-sexp 2)) ?\#)))
  286. ;; No indentation for "'(...)" elements
  287. (setq calculated (1+ sexp-column)))
  288. ((or (eq (char-after (1- containing-sexp)) ?\,)
  289. (and (eq (char-after (1- containing-sexp)) ?\@)
  290. (eq (char-after (- containing-sexp 2)) ?\,)))
  291. ;; ",(...)" or ",@(...)"
  292. (setq calculated normal-indent))
  293. ((eq (char-after (1- containing-sexp)) ?\#)
  294. ;; "#(...)"
  295. (setq calculated (1+ sexp-column)))
  296. ((null method)
  297. ;; If this looks like a call to a `def...' form,
  298. ;; think about indenting it as one, but do it
  299. ;; tentatively for cases like
  300. ;; (flet ((defunp ()
  301. ;; nil)))
  302. ;; Set both normal-indent and tentative-calculated.
  303. ;; The latter ensures this value gets used
  304. ;; if there are no relevant containing constructs.
  305. ;; The former ensures this value gets used
  306. ;; if there is a relevant containing construct
  307. ;; but we are nested within the structure levels
  308. ;; that it specifies indentation for.
  309. (if tentative-defun
  310. (setq tentative-calculated
  311. (common-lisp-indent-call-method
  312. function lisp-indent-defun-method
  313. path state indent-point
  314. sexp-column normal-indent)
  315. normal-indent tentative-calculated)))
  316. ((integerp method)
  317. ;; convenient top-level hack.
  318. ;; (also compatible with lisp-indent-function)
  319. ;; The number specifies how many `distinguished'
  320. ;; forms there are before the body starts
  321. ;; Equivalent to (4 4 ... &body)
  322. (setq calculated (cond ((cdr path)
  323. normal-indent)
  324. ((<= (car path) method)
  325. ;; `distinguished' form
  326. (list (+ sexp-column 4)
  327. containing-form-start))
  328. ((= (car path) (1+ method))
  329. ;; first body form.
  330. (+ sexp-column lisp-body-indent))
  331. (t
  332. ;; other body form
  333. normal-indent))))
  334. (t
  335. (setq calculated
  336. (common-lisp-indent-call-method
  337. function method path state indent-point
  338. sexp-column normal-indent)))))
  339. (goto-char containing-sexp)
  340. (setq last-point containing-sexp)
  341. (unless calculated
  342. (condition-case ()
  343. (progn (backward-up-list 1)
  344. (setq depth (1+ depth)))
  345. (error (setq depth lisp-indent-maximum-backtracking))))))
  346. (or calculated tentative-calculated))))
  347. (defun common-lisp-indent-call-method (function method path state indent-point
  348. sexp-column normal-indent)
  349. (let ((lisp-indent-error-function function))
  350. (if (symbolp method)
  351. (funcall method
  352. path state indent-point
  353. sexp-column normal-indent)
  354. (lisp-indent-259 method path state indent-point
  355. sexp-column normal-indent))))
  356. ;; Dynamically bound in common-lisp-indent-call-method.
  357. (defvar lisp-indent-error-function)
  358. (defun lisp-indent-report-bad-format (m)
  359. (error "%s has a badly-formed %s property: %s"
  360. ;; Love those free variable references!!
  361. lisp-indent-error-function 'common-lisp-indent-function m))
  362. ;; Lambda-list indentation is now done in LISP-INDENT-LAMBDA-LIST.
  363. ;; See also `lisp-lambda-list-keyword-alignment',
  364. ;; `lisp-lambda-list-keyword-parameter-alignment' and
  365. ;; `lisp-lambda-list-keyword-parameter-indentation' -- dvl
  366. (defvar lisp-indent-lambda-list-keywords-regexp
  367. "&\\(\
  368. optional\\|rest\\|key\\|allow-other-keys\\|aux\\|whole\\|body\\|environment\
  369. \\)\\([ \t]\\|$\\)"
  370. "Regular expression matching lambda-list keywords.")
  371. (defun lisp-indent-lambda-list
  372. (indent-point sexp-column containing-form-start)
  373. (let (limit)
  374. (cond ((save-excursion
  375. (goto-char indent-point)
  376. (beginning-of-line)
  377. (skip-chars-forward " \t")
  378. (setq limit (point))
  379. (looking-at lisp-indent-lambda-list-keywords-regexp))
  380. ;; We're facing a lambda-list keyword.
  381. (if lisp-lambda-list-keyword-alignment
  382. ;; Align to the first keyword if any, or to the beginning of
  383. ;; the lambda-list.
  384. (save-excursion
  385. (goto-char containing-form-start)
  386. (save-match-data
  387. (if (re-search-forward
  388. lisp-indent-lambda-list-keywords-regexp
  389. limit t)
  390. (progn
  391. (goto-char (match-beginning 0))
  392. (current-column))
  393. (1+ sexp-column))))
  394. ;; Align to the beginning of the lambda-list.
  395. (1+ sexp-column)))
  396. (t
  397. ;; Otherwise, align to the first argument of the last lambda-list
  398. ;; keyword, the keyword itself, or the beginning of the
  399. ;; lambda-list.
  400. (save-excursion
  401. (goto-char indent-point)
  402. (forward-line -1)
  403. (end-of-line)
  404. (save-match-data
  405. (if (re-search-backward lisp-indent-lambda-list-keywords-regexp
  406. containing-form-start t)
  407. (let* ((keyword-posn
  408. (progn
  409. (goto-char (match-beginning 0))
  410. (current-column)))
  411. (indented-keyword-posn
  412. (+ keyword-posn
  413. lisp-lambda-list-keyword-parameter-indentation)))
  414. (goto-char (match-end 0))
  415. (skip-chars-forward " \t")
  416. (if (eolp)
  417. indented-keyword-posn
  418. (if lisp-lambda-list-keyword-parameter-alignment
  419. (current-column)
  420. indented-keyword-posn)))
  421. (1+ sexp-column))))))))
  422. ;; Blame the crufty control structure on dynamic scoping
  423. ;; -- not on me!
  424. (defun lisp-indent-259
  425. (method path state indent-point sexp-column normal-indent)
  426. (catch 'exit
  427. (let ((p path)
  428. (containing-form-start (elt state 1))
  429. n tem tail)
  430. ;; Isn't tail-recursion wonderful?
  431. (while p
  432. ;; This while loop is for destructuring.
  433. ;; p is set to (cdr p) each iteration.
  434. (if (not (consp method)) (lisp-indent-report-bad-format method))
  435. (setq n (1- (car p))
  436. p (cdr p)
  437. tail nil)
  438. (while n
  439. ;; This while loop is for advancing along a method
  440. ;; until the relevant (possibly &rest/&body) pattern
  441. ;; is reached.
  442. ;; n is set to (1- n) and method to (cdr method)
  443. ;; each iteration.
  444. (setq tem (car method))
  445. (or (eq tem 'nil) ;default indentation
  446. (eq tem '&lambda) ;lambda list
  447. (and (eq tem '&body) (null (cdr method)))
  448. (and (eq tem '&rest)
  449. (consp (cdr method))
  450. (null (cddr method)))
  451. (integerp tem) ;explicit indentation specified
  452. (and (consp tem) ;destructuring
  453. (eq (car tem) '&whole)
  454. (or (symbolp (cadr tem))
  455. (integerp (cadr tem))))
  456. (and (symbolp tem) ;a function to call to do the work.
  457. (null (cdr method)))
  458. (lisp-indent-report-bad-format method))
  459. (cond ((and tail (not (consp tem)))
  460. ;; indent tail of &rest in same way as first elt of rest
  461. (throw 'exit normal-indent))
  462. ((eq tem '&body)
  463. ;; &body means (&rest <lisp-body-indent>)
  464. (throw 'exit
  465. (if (and (= n 0) ;first body form
  466. (null p)) ;not in subforms
  467. (+ sexp-column
  468. lisp-body-indent)
  469. normal-indent)))
  470. ((eq tem '&rest)
  471. ;; this pattern holds for all remaining forms
  472. (setq tail (> n 0)
  473. n 0
  474. method (cdr method)))
  475. ((> n 0)
  476. ;; try next element of pattern
  477. (setq n (1- n)
  478. method (cdr method))
  479. (if (< n 0)
  480. ;; Too few elements in pattern.
  481. (throw 'exit normal-indent)))
  482. ((eq tem 'nil)
  483. (throw 'exit (if (consp normal-indent)
  484. normal-indent
  485. (list normal-indent containing-form-start))))
  486. ((eq tem '&lambda)
  487. (throw 'exit
  488. (cond ((null p)
  489. (list (+ sexp-column 4) containing-form-start))
  490. ((null (cdr p))
  491. ;; Indentation within a lambda-list. -- dvl
  492. (list (lisp-indent-lambda-list
  493. indent-point
  494. sexp-column
  495. containing-form-start)
  496. containing-form-start))
  497. (t
  498. normal-indent))))
  499. ((integerp tem)
  500. (throw 'exit
  501. (if (null p) ;not in subforms
  502. (list (+ sexp-column tem) containing-form-start)
  503. normal-indent)))
  504. ((symbolp tem) ;a function to call
  505. (throw 'exit
  506. (funcall tem path state indent-point
  507. sexp-column normal-indent)))
  508. (t
  509. ;; must be a destructing frob
  510. (if (not (null p))
  511. ;; descend
  512. (setq method (cddr tem)
  513. n nil)
  514. (setq tem (cadr tem))
  515. (throw 'exit
  516. (cond (tail
  517. normal-indent)
  518. ((eq tem 'nil)
  519. (list normal-indent
  520. containing-form-start))
  521. ((integerp tem)
  522. (list (+ sexp-column tem)
  523. containing-form-start))
  524. (t
  525. (funcall tem path state indent-point
  526. sexp-column normal-indent))))))))))))
  527. (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
  528. (if (not (null (cdr path)))
  529. normal-indent
  530. (save-excursion
  531. (goto-char indent-point)
  532. (beginning-of-line)
  533. (skip-chars-forward " \t")
  534. (list (cond ((looking-at "\\sw\\|\\s_")
  535. ;; a tagbody tag
  536. (+ sexp-column lisp-tag-indentation))
  537. ((integerp lisp-tag-body-indentation)
  538. (+ sexp-column lisp-tag-body-indentation))
  539. ((eq lisp-tag-body-indentation 't)
  540. (condition-case ()
  541. (progn (backward-sexp 1) (current-column))
  542. (error (1+ sexp-column))))
  543. (t (+ sexp-column lisp-body-indent)))
  544. ; (cond ((integerp lisp-tag-body-indentation)
  545. ; (+ sexp-column lisp-tag-body-indentation))
  546. ; ((eq lisp-tag-body-indentation 't)
  547. ; normal-indent)
  548. ; (t
  549. ; (+ sexp-column lisp-body-indent)))
  550. (elt state 1)
  551. ))))
  552. (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
  553. (if (>= (car path) 3)
  554. (let ((lisp-tag-body-indentation lisp-body-indent))
  555. (funcall (function lisp-indent-tagbody)
  556. path state indent-point sexp-column normal-indent))
  557. (funcall (function lisp-indent-259)
  558. '((&whole nil &rest
  559. ;; the following causes weird indentation
  560. ;;(&whole 1 1 2 nil)
  561. )
  562. (&whole nil &rest 1))
  563. path state indent-point sexp-column normal-indent)))
  564. ;; LISP-INDENT-DEFMETHOD now supports the presence of more than one method
  565. ;; qualifier and indents the method's lambda list properly. -- dvl
  566. (defun lisp-indent-defmethod
  567. (path state indent-point sexp-column normal-indent)
  568. (lisp-indent-259
  569. (let ((nqual 0))
  570. (if (and (>= (car path) 3)
  571. (save-excursion
  572. (beginning-of-defun)
  573. (forward-char 1)
  574. (forward-sexp 2)
  575. (skip-chars-forward " \t\n")
  576. (while (looking-at "\\sw\\|\\s_")
  577. (incf nqual)
  578. (forward-sexp)
  579. (skip-chars-forward " \t\n"))
  580. (> nqual 0)))
  581. (append '(4) (make-list nqual 4) '(&lambda &body))
  582. (get 'defun 'common-lisp-indent-function)))
  583. path state indent-point sexp-column normal-indent))
  584. (defun lisp-indent-function-lambda-hack (path state indent-point
  585. sexp-column normal-indent)
  586. ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
  587. (if (or (cdr path) ; wtf?
  588. (> (car path) 3))
  589. ;; line up under previous body form
  590. normal-indent
  591. ;; line up under function rather than under lambda in order to
  592. ;; conserve horizontal space. (Which is what #' is for.)
  593. (condition-case ()
  594. (save-excursion
  595. (backward-up-list 2)
  596. (forward-char 1)
  597. (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
  598. (+ lisp-body-indent -1 (current-column))
  599. (+ sexp-column lisp-body-indent)))
  600. (error (+ sexp-column lisp-body-indent)))))
  601. (let ((l '((block 1)
  602. (case (4 &rest (&whole 2 &rest 1)))
  603. (ccase . case)
  604. (ecase . case)
  605. (typecase . case)
  606. (etypecase . case)
  607. (ctypecase . case)
  608. (catch 1)
  609. (cond (&rest (&whole 2 &rest 1)))
  610. (defvar (4 2 2))
  611. (defclass (6 4 (&whole 2 &rest 1) (&whole 2 &rest 1)))
  612. (defconstant . defvar)
  613. (defcustom (4 2 2 2))
  614. (defparameter . defvar)
  615. (defconst . defcustom)
  616. (define-condition . defclass)
  617. (define-modify-macro (4 &lambda &body))
  618. (defsetf (4 &lambda 4 &body))
  619. (defun (4 &lambda &body))
  620. (defgeneric (4 &lambda &body))
  621. (define-setf-method . defun)
  622. (define-setf-expander . defun)
  623. (defmacro . defun)
  624. (defsubst . defun)
  625. (deftype . defun)
  626. (defmethod lisp-indent-defmethod)
  627. (defpackage (4 2))
  628. (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
  629. &rest (&whole 2 &rest 1)))
  630. (destructuring-bind
  631. ((&whole 6 &rest 1) 4 &body))
  632. (do lisp-indent-do)
  633. (do* . do)
  634. (dolist ((&whole 4 2 1) &body))
  635. (dotimes . dolist)
  636. (eval-when 1)
  637. (flet ((&whole 4 &rest (&whole 1 &lambda &body)) &body))
  638. (labels . flet)
  639. (macrolet . flet)
  640. (generic-flet . flet)
  641. (generic-labels . flet)
  642. (handler-case (4 &rest (&whole 2 &lambda &body)))
  643. (restart-case . handler-case)
  644. ;; `else-body' style
  645. (if (nil nil &body))
  646. ;; single-else style (then and else equally indented)
  647. (if (&rest nil))
  648. (lambda (&lambda &rest lisp-indent-function-lambda-hack))
  649. (let ((&whole 4 &rest (&whole 1 1 2)) &body))
  650. (let* . let)
  651. (compiler-let . let) ;barf
  652. (handler-bind . let)
  653. (restart-bind . let)
  654. (locally 1)
  655. ;(loop lisp-indent-loop)
  656. (:method (&lambda &body)) ; in `defgeneric'
  657. (multiple-value-bind ((&whole 6 &rest 1) 4 &body))
  658. (multiple-value-call (4 &body))
  659. (multiple-value-prog1 1)
  660. (multiple-value-setq (4 2))
  661. (multiple-value-setf . multiple-value-setq)
  662. (pprint-logical-block (4 2))
  663. (print-unreadable-object ((&whole 4 1 &rest 1) &body))
  664. ;; Combines the worst features of BLOCK, LET and TAGBODY
  665. (prog (&lambda &rest lisp-indent-tagbody))
  666. (prog* . prog)
  667. (prog1 1)
  668. (prog2 2)
  669. (progn 0)
  670. (progv (4 4 &body))
  671. (return 0)
  672. (return-from (nil &body))
  673. (symbol-macrolet . let)
  674. (tagbody lisp-indent-tagbody)
  675. (throw 1)
  676. (unless 1)
  677. (unwind-protect (5 &body))
  678. (when 1)
  679. (with-accessors . multiple-value-bind)
  680. (with-condition-restarts . multiple-value-bind)
  681. (with-output-to-string (4 2))
  682. (with-slots . multiple-value-bind)
  683. (with-standard-io-syntax (2)))))
  684. (dolist (el l)
  685. (put (car el) 'common-lisp-indent-function
  686. (if (symbolp (cdr el))
  687. (get (cdr el) 'common-lisp-indent-function)
  688. (car (cdr el))))))
  689. ;(defun foo (x)
  690. ; (tagbody
  691. ; foo
  692. ; (bar)
  693. ; baz
  694. ; (when (losing)
  695. ; (with-big-loser
  696. ; (yow)
  697. ; ((lambda ()
  698. ; foo)
  699. ; big)))
  700. ; (flet ((foo (bar baz zap)
  701. ; (zip))
  702. ; (zot ()
  703. ; quux))
  704. ; (do ()
  705. ; ((lose)
  706. ; (foo 1))
  707. ; (quux)
  708. ; foo
  709. ; (lose))
  710. ; (cond ((x)
  711. ; (win 1 2
  712. ; (foo)))
  713. ; (t
  714. ; (lose
  715. ; 3))))))
  716. ;(put 'while 'common-lisp-indent-function 1)
  717. ;(put 'defwrapper'common-lisp-indent-function ...)
  718. ;(put 'def 'common-lisp-indent-function ...)
  719. ;(put 'defflavor 'common-lisp-indent-function ...)
  720. ;(put 'defsubst 'common-lisp-indent-function ...)
  721. ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
  722. ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
  723. ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((&whole 1))) (3 4 ((&whole 1))) (4 &body)))
  724. ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
  725. ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
  726. ;(put 'defclass 'common-lisp-indent-function '((&whole 2 &rest (&whole 2 &rest 1) &rest (&whole 2 &rest 1)))
  727. ;(put 'defgeneric 'common-lisp-indent-function 'defun)
  728. ;;; cl-indent.el ends here