/local-lisp/slime/contrib/slime-indentation.el

https://bitbucket.org/sakito/dot.emacs.d/ · Emacs Lisp · 1167 lines · 724 code · 123 blank · 320 comment · 23 complexity · 1246630040ef1e066b870981693b9e8a MD5 · raw file

  1. (define-slime-contrib slime-indentation
  2. "cl-indent.el as a slime-contrib module"
  3. (:swank-dependencies swank-indentation)
  4. (:on-load (run-hooks 'cl-indent:load-hook)))
  5. ;; redefine this for cl-indent:method
  6. (defun slime-handle-indentation-update (alist)
  7. "Update Lisp indent information for slime-indentation.el.
  8. ALIST is a list of (SYMBOL-NAME . INDENT-SPEC) of proposed indentation
  9. settings for `common-lisp-indent-function'. The appropriate property
  10. is setup, unless the user already set one explicitly."
  11. (dolist (info alist)
  12. (let ((symbol (intern (car info)))
  13. (indent (cdr info)))
  14. (define-cl-indent (cons symbol
  15. (etypecase indent
  16. (number (list indent))
  17. (cons (labels ((walk (indent)
  18. (etypecase indent
  19. ((or number null) indent)
  20. (cons (cons (walk (car indent))
  21. (walk (cdr indent))))
  22. (string (intern (downcase indent))))))
  23. (list (walk indent))))
  24. (string (intern (downcase indent))))))
  25. (run-hook-with-args 'slime-indentation-update-hooks symbol indent))))
  26. ;; $ITI: cl-indent.el,v 1.6 1995/09/10 14:13:34 schrod Exp $
  27. ;; ----------------------------------------------------------------------
  28. ;; Copyright (C) 1987, 1993 Free Software Foundation, Inc.
  29. ;; Written by Richard Mlynarik July 1987
  30. ;; Merged with cl-indent-patches.el by Marco Baringer (2007-11-14)
  31. ;; Documented and intensively modified by Joachim Schrod
  32. ;; <jschrod@acm.org>, history at end.
  33. ;; Send bug reports, gripes, patches to me.
  34. ;;
  35. ;; cl-indent.el --- highly configurable indentation for Lisp modes
  36. ;;
  37. ;; This file is part of GNU Emacs.
  38. ;; GNU Emacs is free software; you can redistribute it and/or modify
  39. ;; it under the terms of the GNU General Public License as published by
  40. ;; the Free Software Foundation; either version 2, or (at your option)
  41. ;; any later version.
  42. ;; GNU Emacs is distributed in the hope that it will be useful,
  43. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. ;; GNU General Public License for more details.
  46. ;; You should have received a copy of the GNU General Public License
  47. ;; along with GNU Emacs; see the file COPYING. If not, write to
  48. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  49. ;; ----------------------------------------------------------------------
  50. ;; USAGE:
  51. ;; This file delivers highly configurable indentation of Lisp code.
  52. ;; Eval (cl-indent) to use this indentation for a specific file,
  53. ;; (setq lisp-indent-function 'cl-indent:function) to use it for all
  54. ;; Lisp files.
  55. ;; The indentation for a specific form may be defined by
  56. ;; (define-cl-indent SPEC &optional MODE-METHODS). Indentation specs
  57. ;; for Common Lisp constructs are given already. Check the on-line
  58. ;; documentation of this function for more information.
  59. ;; Actually, the whole (`real') documentation of this source is stored
  60. ;; as the documentation strings of respective functions. Start with
  61. ;; #'define-cl-indent, you'll find references to all other relevant
  62. ;; functions.
  63. ;; It's also possible to specify specific indentations for a mode
  64. ;; (e.g., some special Lisp-mode) and even specific ones for a file.
  65. ;; File specific indentations are taken from the alist bound to
  66. ;; cl-indent:local-methods, you can set it in your `Local Variables'
  67. ;; section. Mode-specific methods are stored in hash tables, the mode
  68. ;; setup must bind cl-indent:mode-methods to the name of that hash
  69. ;; table.
  70. ;; You may want to override my global indentation specs. If you load
  71. ;; this file immediately, just issue some #'define-cl-indent calls. If
  72. ;; you use autoload, add an appropriate hook function to
  73. ;; 'cl-indent:load-hook.
  74. ;; I'm interested in feedback on this module. Do you use it, was it
  75. ;; useful to you? (Further development depends on the amount of people
  76. ;; who send feedback. :-)
  77. ;; Send email to <jschrod@acm.org>.
  78. ;; ------------------------------------------------------------
  79. ;;>> TODO
  80. ;; Urgently need better user documentation, it's hard to get a grasp
  81. ;; for the overall strategy how this package may be customized. One
  82. ;; has to look at too many function documentation strings.
  83. ;; Have to check if the usage of hash tables makes this package XEmacs
  84. ;; specific. If FSF Emacs doesn't have them, they might be emulated by
  85. ;; alists or obarrays. (I don't have FSF Emacs available, may somebody
  86. ;; please check this, maybe even send patches?)
  87. ;; Realize `parent method tables', to be able to inherit an indentation
  88. ;; method table. `(make-method-table &optional size parent)' ?! That's
  89. ;; needed before the CL specific indentation is moved to an own table,
  90. ;; as some modes (e.g., stil-mode) may inherit their indentation from
  91. ;; CL definitions.
  92. ;; Common Lisp specific indentation methods should be moved to a
  93. ;; method table, it's not good to have them globally for all kinds of
  94. ;; Lisp modes. How about a table for Elisp indentations as well?
  95. ;; special handling of keywords in forms, e.g.,
  96. ;;
  97. ;; :foo
  98. ;; bar
  99. ;; :baz
  100. ;; zap
  101. ;;
  102. ;; &key (like &body)??
  103. ;; &rest 1 in lambda-lists doesn't work, really want
  104. ;;
  105. ;; (foo bar
  106. ;; baz)
  107. ;;
  108. ;; not
  109. ;;
  110. ;; (foo bar
  111. ;; baz)
  112. ;;
  113. ;; Need something better than &rest for such cases. Perhaps a function
  114. ;; that just returns normal-point? Might work...
  115. ;;; ------------------------------------------------------------
  116. ;;;
  117. ;;; USER TOP-LEVEL FUNCTION
  118. ;;;
  119. ;;;###autoload
  120. (defun cl-indent ()
  121. "Switch on Common Lisp indentation for the current buffer.
  122. May also be used as hook function, e.g., in lisp-mode-hook.
  123. If you want to do use this indentation for all Lisp buffers, incl.
  124. Emacs Lisp code, simply eval
  125. (setq lisp-indent-function 'cl-indent:function)
  126. You might want to do this in some setup file, e.g., in ~/.emacs ."
  127. (interactive)
  128. (make-local-variable 'lisp-indent-function)
  129. (setq lisp-indent-function 'cl-indent:function))
  130. ;;; ------------------------------------------------------------
  131. ;;;
  132. ;;; Configuration:
  133. ;;;
  134. (defvar cl-indent::maximum-backtracking 3
  135. "Maximum depth to backtrack out from a sublist for structured indentation.
  136. If this variable is 0, no backtracking will occur and forms such as flet
  137. may not be correctly indented.")
  138. (defvar cl-indent:tag-indentation 1
  139. "*Indentation of tags relative to containing list.
  140. This variable is used by the function cl-indent:tagbody.")
  141. (defvar cl-indent:tag-body-indentation 3
  142. "*Indentation of non-tagged lines relative to containing list.
  143. This variable is used by the function cl-indent:tagbody to indent normal
  144. lines (lines without tags).
  145. The indentation is relative to the indentation of the parenthesis enclosing
  146. he special form. If the value is t, the body of tags will be indented
  147. as a block at the same indentation as the first s-expression following
  148. the tag. In this case, any forms before the first tag are indented
  149. by lisp-body-indent.")
  150. ;;; ============================================================
  151. ;;;
  152. ;;; compute the indentation of the current line
  153. ;;;
  154. ;;;###autoload
  155. (defun common-lisp-indent-function (indent-point state)
  156. "Old name of #'cl-indent:function."
  157. (cl-indent:function indent-point state))
  158. (make-obsolete #'common-lisp-indent-function #'cl-indent:function)
  159. ;;;###autoload
  160. (defun cl-indent:function (indent-point state)
  161. "Compute the indentation of the current line of Common Lisp code.
  162. INDENT-POINT is the current point. STATE is the result of a
  163. #'parse-partial-sexp from the start of the current function to the
  164. start of the line this function was called.
  165. The indentation is determined by the expressions point is in.
  166. When this function is called, the column of point may be used as the
  167. normal indentation. Therefore we call this position _normal
  168. point_. Actually, if the first element of the current expression is a
  169. list, it's at the start of this element. Otherwise it's at the start
  170. of first expression on the same line as the last complete expression.
  171. Within a quoted list or a non-form list, all subsequent lines are
  172. indented to the column directly after the opening parenthesis. Quoted
  173. lists are those that are prefixed with ?\`, ?\', or ?\#. Note that the
  174. quote must be immediately in front of the opening parenthesis. I.e.,
  175. if you want to use automatic code indentation in a macro expansion
  176. formulated with a backquoted list, add a blank between the backquote
  177. and the expansion form.
  178. Within a list form, the indentation is determined by the indentation
  179. method associated to the form symbol. (See #'cl-indent::method.)
  180. ** If the indentation method is nil, the form is assumed to be a
  181. function call, arguments are aligned beneath each other if the first
  182. argument was written behind the function symbol, otherwise they're
  183. aligned below the function symbol.
  184. ** If the indentation method is a symbol, a function must be bound to
  185. that symbol that will compute the current indentation. Such a function
  186. is named an _indentation function_ and is called with 5 arguments:
  187. (1) PATH is a list of numbers, the path from the top-level form to
  188. the current structural element (the first element is number 0).
  189. E.g., `foo' has a path of (0 3 1) in `((a b c (d foo) f) g)'.
  190. (2) STATE is passed.
  191. (3) INDENT-POINT is passed.
  192. (4) SEXP-COLUMN is the column where the innermost form starts.
  193. (5) NORMAL-INDENT is the column of normal point.
  194. ** If the indentation method is a list, this list specifies the form
  195. structure and the indentation of each substructure. The possible list
  196. structure and elements are described in #'cl-indent::form-method.
  197. ** If the indentation method is the number $n$, the first $n$
  198. arguments are _distinguished arguments_; they are indented by 4
  199. spaces. Further arguments are indented by lisp-body-indent. That's
  200. roughly equivalent to '(4 4 ... &body)' with $n$ 4s.
  201. ** Furthermore values as described for #'lisp-indent-function may be
  202. used for upward compatibility."
  203. (let ((normal-indent (current-column)))
  204. ;; Walk up list levels until we see something
  205. ;; which does special things with subforms.
  206. (let ((depth 0)
  207. ;; Path describes the position of point in terms of
  208. ;; list-structure with respect to contining lists.
  209. ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
  210. (path ())
  211. ;; set non-nil when somebody works out the indentation to use
  212. calculated
  213. (last-point indent-point)
  214. ;; the position of the open-paren of the innermost containing list
  215. (containing-form-start (elt state 1))
  216. ;; the column of the above
  217. sexp-column)
  218. ;; Move to start of innermost containing list
  219. (goto-char containing-form-start)
  220. (setq sexp-column (current-column))
  221. ;; Look over successively less-deep containing forms
  222. (while (and (not calculated)
  223. (< depth cl-indent::maximum-backtracking))
  224. (let ((containing-sexp (point)))
  225. (forward-char 1)
  226. (parse-partial-sexp (point) indent-point 1 t)
  227. ;; Move to the car of the relevant containing form
  228. (let (tem function method)
  229. (if (not (looking-at "\\sw\\|\\s_"))
  230. ;; This form doesn't seem to start with a symbol
  231. (setq function nil method nil)
  232. (setq tem (point))
  233. (forward-sexp 1)
  234. (setq function (downcase (buffer-substring tem (point))))
  235. (goto-char tem)
  236. (setq tem (intern-soft function)
  237. method (get tem 'cl-indent:method))
  238. (cond ((and (null method)
  239. (string-match ":[^:]+" function))
  240. ;; The pleblisp package feature
  241. (setq function (substring function
  242. (1+ (match-beginning 0)))
  243. method (get (intern-soft function)
  244. 'cl-indent:method)))
  245. ((and (null method))
  246. ;; backwards compatibility
  247. (setq method (get tem 'lisp-indent-function)))))
  248. (let ((n 0))
  249. ;; How far into the containing form is the current form?
  250. (if (< (point) indent-point)
  251. (while (condition-case ()
  252. (progn
  253. (forward-sexp 1)
  254. (if (>= (point) indent-point)
  255. nil
  256. (parse-partial-sexp (point)
  257. indent-point 1 t)
  258. (setq n (1+ n))
  259. t))
  260. (error nil))))
  261. (setq path (cons n path)))
  262. ;; backwards compatibility.
  263. (cond ((null function))
  264. ((null method)
  265. (if (null (cdr path))
  266. ;; (package prefix was stripped off above)
  267. (setq method (cond ((string-match "\\`def"
  268. function)
  269. '(4 (&whole 4 &rest 1) &body))
  270. ((string-match "\\`\\(with\\|do\\)-"
  271. function)
  272. '(4 &body))))))
  273. ;; backwards compatibility. Bletch.
  274. ((eq method 'defun)
  275. (setq method '(4 (&whole 4 &rest 1) &body))))
  276. (cond ((and (eql (char-after (1- containing-sexp)) ?\') ; patched to only do this for ' and not `.
  277. (not (eql (char-after (- containing-sexp 2)) ?\#)))
  278. ;; No indentation for "'(...)" elements
  279. (setq calculated (1+ sexp-column)))
  280. ((eql (char-after (1- containing-sexp)) ?\#)
  281. ;; "#(...)"
  282. (setq calculated (1+ sexp-column)))
  283. ((null method))
  284. ((integerp method)
  285. ;; convenient top-level hack.
  286. ;; (also compatible with lisp-indent-function)
  287. ;; The number specifies how many `distinguished'
  288. ;; forms there are before the body starts
  289. ;; Equivalent to (4 4 ... &body)
  290. (setq calculated (cond ((cdr path)
  291. normal-indent)
  292. ((<= (car path) method)
  293. ;; `distinguished' form
  294. (list (+ sexp-column 4)
  295. containing-form-start))
  296. ((= (car path) (1+ method))
  297. ;; first body form.
  298. (+ sexp-column lisp-body-indent))
  299. (t
  300. ;; other body form
  301. normal-indent))))
  302. ((symbolp method)
  303. (setq calculated (funcall method
  304. path state indent-point
  305. sexp-column normal-indent)))
  306. (t
  307. (setq calculated (cl-indent::form-method
  308. method path state indent-point
  309. sexp-column normal-indent)))))
  310. (goto-char containing-sexp)
  311. (setq last-point containing-sexp)
  312. (if (not calculated)
  313. (condition-case ()
  314. (progn (backward-up-list 1)
  315. (setq depth (1+ depth)))
  316. (error (setq depth cl-indent::maximum-backtracking))))))
  317. calculated)))
  318. (defun cl-indent::normal (state)
  319. "Compute normal indentation according to STATE and current position."
  320. ;; Actually, the current column (i.e., the normal point) _is_ a good
  321. ;; approximation for the normal indentation. But lists with a list
  322. ;; as the first element make problems if an &rest or an &body method
  323. ;; is in effect.
  324. ;;
  325. ;; There we can distinguish two cases:
  326. ;;
  327. ;; 1. ((foo) (bar)
  328. ;; (baz))
  329. ;; 2. ((foo)
  330. ;; (bar)
  331. ;; (baz))
  332. ;;
  333. ;; Both are used in do result-forms, or in cond-forms. If
  334. ;; #'cl-indent:function is called in the baz line, the normal point
  335. ;; will be at (foo), i.e., (baz) would be aligned below (foo). (Of
  336. ;; course, if the body indentation is 1, both (bar) and (baz) are
  337. ;; aligned below (foo).) But I want to enable the specification of
  338. ;; alignments like those shown above -- if the user did change the
  339. ;; alignment for the first expression of a body then it should be
  340. ;; used further on, after all. (As usually, we have to assume that
  341. ;; the user knows what he does.)
  342. (let ((normal-point (point))
  343. (current-sexp (elt state 1)))
  344. ;; A necessary precondition for the special situation outlined
  345. ;; above is that the normal point is directly after the start of
  346. ;; the current expression and that a list is there. Only then we
  347. ;; have to calculate the normal indentation, otherwise we can use
  348. ;; the column of normal point.
  349. (if (and (= (1+ current-sexp) normal-point)
  350. (looking-at "\\s("))
  351. ;; OK. Let's determine first the first expression in the line
  352. ;; with the last completed expression before the indentation point.
  353. (let ((last-sexp (elt state 2)))
  354. (goto-char last-sexp)
  355. (beginning-of-line)
  356. (parse-partial-sexp (point) last-sexp 0 t)
  357. (backward-prefix-chars)
  358. ;; If we're now after the current expression, we're in case
  359. ;; 2. We simply use the current column then.
  360. (if (> (point) current-sexp)
  361. (current-column)
  362. ;; Here we have to care for case 1: We determine the
  363. ;; second element of the list and use its column.
  364. (goto-char normal-point) ; start of the first element!
  365. (forward-sexp 1)
  366. (parse-partial-sexp (point) last-sexp 0 t)
  367. (current-column)))
  368. (current-column))))
  369. (defun cl-indent::bad-method (m)
  370. (error "%s has a badly-formed indentation method: %s"
  371. ;; Love them free variable references!!
  372. function m))
  373. ;; Blame the crufty control structure on dynamic scoping
  374. ;; -- not on me!
  375. (defun cl-indent::form-method (method path state indent-point
  376. sexp-column normal-indent)
  377. "Compute the current indentation according to METHOD.
  378. The other arguments are those of an indentation function, see
  379. #'cl-indent:function for further explanation.
  380. METHOD is a list that specifies the indentation of a form:
  381. method-list-spec : '(' method-list ')'
  382. method-list : method * method-finish ?
  383. method : indent-spec
  384. | method-sublist
  385. << the subform must be a list that's indented
  386. as specified >>
  387. indent-spec : Number | Symbol | 'nil'
  388. << indent this subform $Number spaces or compute its
  389. indentation by the indentation function bound to
  390. Symbol. 'nil' tells to use normal function
  391. indentation. >>
  392. method-finish : '&rest' method
  393. << indent the rest of this form as specified by
  394. method. >>
  395. | '&body'
  396. << equivalent to `(&rest ,lisp-body-indent).
  397. I.e., Indent all following forms by
  398. lisp-body-indent spaces. >>
  399. method-sublist : '(' '&whole' indent-spec method-list ')'
  400. << This whole subform has a basic indentation, as
  401. specified by indent-spec. The indentations from
  402. method-list are added to this basic indentation. >>
  403. FIXME (-js): Maybe only list structures up to a depth of
  404. 'cl-indent::maximum-backtracking are supported. Have to analyze the
  405. code for this. If that's the case this variable should be a constant.
  406. "
  407. (catch 'exit
  408. (let ((p path)
  409. (containing-form-start (elt state 1))
  410. n tem)
  411. ;; Isn't tail-recursion wonderful?
  412. (while p
  413. ;; This while loop is for destructuring.
  414. ;; p is set to (cdr p) each iteration.
  415. (if (not (consp method)) (cl-indent::bad-method method))
  416. (setq n (1- (car p)) ; FIXME: that might result in -1 !?
  417. p (cdr p))
  418. (while n
  419. ;; This while loop is for advancing along a method
  420. ;; until the relevant (possibly &rest/&body) pattern
  421. ;; is reached.
  422. ;; n is set to (1- n) and method to (cdr method)
  423. ;; each iteration.
  424. ; (message "trying %s for %s %s" method p function) (sit-for 1)
  425. (setq tem (car method))
  426. (cl-indent::check-method tem method)
  427. (cond ((eq tem '&body)
  428. ;; &body means (&rest <lisp-body-indent>)
  429. (throw 'exit
  430. (if (null p)
  431. (+ sexp-column lisp-body-indent)
  432. normal-indent)))
  433. ((eq tem '&rest)
  434. ;; this pattern holds for all remaining forms
  435. (setf method (list (second method))
  436. n 0))
  437. ((> n 0)
  438. ;; try next element of pattern
  439. (setq n (1- n)
  440. method (cdr method))
  441. (if (< n 0)
  442. ;; Too few elements in pattern.
  443. (throw 'exit normal-indent)))
  444. ((eq tem 'nil)
  445. (throw 'exit (list normal-indent containing-form-start)))
  446. ; ((eq tem '&lambda)
  447. ; ;; abbrev for (&whole 4 &rest 1)
  448. ; (throw 'exit
  449. ; (cond ((null p)
  450. ; (list (+ sexp-column 4) containing-form-start))
  451. ; ((null (cdr p))
  452. ; (+ sexp-column 1))
  453. ; (t normal-indent))))
  454. ((integerp tem)
  455. (throw 'exit
  456. (if (null p) ;not in subforms
  457. (list (+ sexp-column tem) containing-form-start)
  458. normal-indent)))
  459. ((symbolp tem) ;a function to call
  460. (throw 'exit
  461. (funcall tem path state indent-point
  462. sexp-column normal-indent)))
  463. (t
  464. ;; must be a destructing frob
  465. (if (not (null p))
  466. ;; descend
  467. (setq method (cdr (cdr tem))
  468. n nil)
  469. (setq tem (car (cdr tem)))
  470. (throw 'exit
  471. (cond ((eq tem 'nil)
  472. (list normal-indent
  473. containing-form-start))
  474. ((integerp tem)
  475. (list (+ sexp-column tem)
  476. containing-form-start))
  477. (t
  478. (funcall tem path state indent-point
  479. sexp-column normal-indent))))))))))))
  480. (defun cl-indent::check-method (tem method)
  481. "Check validity of one indentation method element.
  482. TEM is that indentation method and METHOD is the rest of the method list."
  483. (if (eq tem 'nil)
  484. ;; default indentation
  485. t
  486. (if (and (eq tem '&body)
  487. (null (cdr method)))
  488. t
  489. (if (and (eq tem '&rest)
  490. (consp (cdr method))
  491. (null (cdr (cdr method))))
  492. t
  493. (if (integerp tem)
  494. t
  495. (if (and (consp tem) ; destructuring
  496. (eq (car tem) '&whole)
  497. (or (symbolp (car (cdr tem)))
  498. (integerp (car (cdr tem)))))
  499. t
  500. (if (and (symbolp tem) ; a function to call to do the work.
  501. (null (cdr method)))
  502. t
  503. (cl-indent::bad-method method))))))))
  504. ;;; ------------------------------------------------------------
  505. ;;;
  506. ;;; A few indentation functions
  507. ;;;
  508. (defun cl-indent:indent-tagbody (path state indent-point
  509. sexp-column normal-indent)
  510. (if (not (null (cdr path)))
  511. normal-indent
  512. (save-excursion
  513. (goto-char indent-point)
  514. (beginning-of-line)
  515. (skip-chars-forward " \t")
  516. (list (cond ((looking-at "\\sw\\|\\s_")
  517. ;; a tagbody tag
  518. (+ sexp-column cl-indent:tag-indentation))
  519. ((integerp cl-indent:tag-body-indentation)
  520. (+ sexp-column cl-indent:tag-body-indentation))
  521. ((eq cl-indent:tag-body-indentation 't)
  522. (condition-case ()
  523. (progn (backward-sexp 1) (current-column))
  524. (error (1+ sexp-column))))
  525. (t (+ sexp-column lisp-body-indent)))
  526. ; (cond ((integerp cl-indent:tag-body-indentation)
  527. ; (+ sexp-column cl-indent:tag-body-indentation))
  528. ; ((eq cl-indent:tag-body-indentation 't)
  529. ; normal-indent)
  530. ; (t
  531. ; (+ sexp-column lisp-body-indent)))
  532. (elt state 1)
  533. ))))
  534. (defun cl-indent:indent-do (path state indent-point
  535. sexp-column normal-indent)
  536. (let ((cl-indent:tag-body-indentation lisp-body-indent))
  537. (funcall #'cl-indent:indent-tagbody
  538. path state indent-point sexp-column normal-indent)))
  539. (defun cl-indent:indent-function-lambda-hack (path state indent-point
  540. sexp-column normal-indent)
  541. ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
  542. (if (or (cdr path) ; wtf?
  543. (> (car path) 3))
  544. ;; line up under previous body form
  545. normal-indent
  546. ;; line up under function rather than under lambda in order to
  547. ;; conserve horizontal space. (Which is what #' is for.)
  548. (condition-case ()
  549. (save-excursion
  550. (backward-up-list 2)
  551. (forward-char 1)
  552. (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
  553. (+ lisp-body-indent -1 (current-column))
  554. (+ sexp-column lisp-body-indent)))
  555. (error (+ sexp-column lisp-body-indent)))))
  556. (defun cl-indent:indent-defmethod (path state indent-point
  557. sexp-column normal-indent)
  558. ;; Look for a method combination specifier...
  559. (let* ((combined (if (and (>= (car path) 3)
  560. (null (cdr path)))
  561. (save-excursion
  562. (goto-char (second state))
  563. (forward-char)
  564. (forward-sexp)
  565. (forward-sexp)
  566. (forward-sexp)
  567. (backward-sexp)
  568. (if (looking-at ":")
  569. t
  570. nil))
  571. nil))
  572. (method (if combined
  573. '(4 4 (&whole 4 &rest 1) &body)
  574. '(4 (&whole 4 &rest 1) &body))))
  575. (funcall #'cl-indent::form-method
  576. method
  577. path state indent-point sexp-column normal-indent)))
  578. (defun cl-indent:indent-defgeneric (path state indent-point
  579. sexp-column normal-indent)
  580. (let ((method '(4 4 &rest 2)))
  581. (when (= 2 (length path))
  582. (ignore-errors
  583. (save-excursion
  584. (save-match-data
  585. (let ((case-fold-search t))
  586. (goto-char (second state))
  587. (down-list)
  588. (skip-chars-forward " \t\n")
  589. (when (looking-at ":method\\W")
  590. (forward-sexp)
  591. (forward-sexp)
  592. (backward-sexp)
  593. (setq method (if (looking-at ":")
  594. '(4 (&whole 4 &rest 1) &body)
  595. '((&whole 4 &rest 1) &body))
  596. path (cdr path)))
  597. (when (looking-at ":method-combination\\W")
  598. (setq method '(4 &rest 2)
  599. path (cdr path))))))))
  600. (funcall #'cl-indent::form-method
  601. method
  602. path state indent-point sexp-column normal-indent)))
  603. (defun cl-indent::line-number ()
  604. "Compatability implementation of emacs23's line-number-at-pos."
  605. (cond
  606. ((fboundp 'line-number-at-pos)
  607. (line-number-at-pos))
  608. ((fboundp 'line-number)
  609. (line-number nil t))
  610. ((fboundp 'count-lines)
  611. (count-lines (point-min) (point)))
  612. (t
  613. (error "Don't know how to count the number of lines from the start of the (narrowed) buffer to point."))))
  614. (defun cl-indent:indent-cond (path state indent-point sexp-column normal-indent)
  615. "Handle indentation of cond.
  616. Cond is either (&rest (&whole 2 1 &rest 1)) or (&rest (&whole 6 1
  617. &rest 1)) depending on whether the first caluse is or isn't on
  618. the same line as the cond symbol.
  619. So if we have:
  620. (cond (a b)
  621. |
  622. we line up the clauses after the cond symbol (6 space of
  623. indentation). wherease if we have:
  624. (cond
  625. (a b)
  626. |)
  627. we line up the clauses two space past the form's indentation."
  628. ;; i'd bet my left pinky there's a better way to implement this...
  629. (let (cond-line-number first-clause-line-number method here)
  630. (save-excursion
  631. ;; narrow to the aera we're interested in because
  632. ;; cl-indent::line-number can, especially on tramp files, be
  633. ;; very slow.
  634. (save-restriction
  635. (narrow-to-region (save-excursion
  636. (backward-up-list)
  637. (point))
  638. (point))
  639. (setf here (point))
  640. (backward-up-list)
  641. (setf cond-line-number (cl-indent::line-number))
  642. (down-list)
  643. (forward-sexp 1)
  644. (setf first-clause-line-number
  645. (progn
  646. (if (= 1 (first path))
  647. ;; we're indenting the first form. use the current line.
  648. (goto-char here)
  649. ;; we're indenting some form which isn't the
  650. ;; first. find out which the line the first clause
  651. ;; starts on.
  652. (forward-sexp 1)
  653. (backward-sexp 1))
  654. (cl-indent::line-number)))))
  655. (cl-indent::form-method
  656. (if (= cond-line-number first-clause-line-number)
  657. '(&rest (&whole 6 &rest 1))
  658. '(&rest (&whole 2 &rest 1)))
  659. path state indent-point sexp-column normal-indent)))
  660. ;;; ============================================================
  661. ;;;
  662. ;;; Define and retrieve indentation method
  663. ;;;
  664. (defun cl-indent::method (function)
  665. "Returns the indentation method associated to FUNCTION (a string).
  666. The indentation method is looked for subsequently as follows:
  667. (1) An indentation method is searched by #'cl-indent::get-method.
  668. (2) If FUNCTION is from a specific package, the package prefix is
  669. discarded and the indentation method from that FUNCTION name is
  670. used.
  671. (4) If FUNCTION starts with 'def', the indentation method \"defun\" is used.
  672. (5) If FUNCTION starts with 'while-' or 'do-', the indentation method
  673. 1 (i.e., one distinguished argument) is used.
  674. If the method determined that way is a string, it's replaced by the
  675. current indentation method of the symbol named by that string."
  676. (let ((method
  677. (cond ((cl-indent::get-method function))
  678. ((string-match ":[^:]+" function)
  679. (cl-indent::method (substring function
  680. (1+ (match-beginning 0)))))
  681. ((string-match "\\`def" function) "defun")
  682. ((string-match "\\`\\(with\\|do\\)-" function) 1))))
  683. (if (stringp method)
  684. (cl-indent::method method)
  685. method)))
  686. (defvar cl-indent:local-methods nil
  687. "*Alist of source-local indentation methods.
  688. Is typically set in a `Local Variables' section.")
  689. (make-variable-buffer-local 'cl-indent:local-methods)
  690. (defvar cl-indent:mode-methods nil
  691. "*Name of hash table with indentation methods for the current buffer.
  692. Is typically set for a mode, during mode setup or in a mode hook.")
  693. (make-variable-buffer-local 'cl-indent:mode-methods)
  694. (defun cl-indent::get-method (function)
  695. "Retrieves an indentation method that is stored for FUNCTION (a string).
  696. (1) Indentation methods may be specified for the current source file,
  697. as an alist that's bound to cl-indent:local-methods . The alist
  698. car is the function symbol, the cdr is the indentation method.
  699. (2) Mode-specific indentation methods are stored in a hash table. The
  700. name of that hash table is bound to cl-indent:mode-methods .
  701. (3) Global indentation methods are stored as the value of the
  702. property 'cl-indent:method. If there is no such property, the
  703. property 'lisp-indent-function is checked, too, for compatibility."
  704. (let ((symbol (intern-soft function)))
  705. (or (cdr (assq symbol cl-indent:local-methods))
  706. ;; An error will be signaled if the value of
  707. ;; cl-indent:mode-methods is not a symbol naming an hash
  708. ;; table. That's fine with me, other packages shouldn't mess
  709. ;; around with my public names...
  710. (and cl-indent:mode-methods
  711. (gethash symbol (symbol-value cl-indent:mode-methods)))
  712. (get symbol 'cl-indent:method)
  713. (get symbol 'lisp-indent-function))))
  714. ;;;
  715. ;;; Try to indent cl:loop
  716. ;;;
  717. (defun cl-indent-parse-state-depth (parse-state)
  718. (car parse-state))
  719. (defun cl-indent-parse-state-start (parse-state)
  720. (car (cdr parse-state)))
  721. (defun cl-indent-parse-state-prev (parse-state)
  722. (car (cdr (cdr parse-state))))
  723. ;; Regexps matching various varieties of loop macro keyword ...
  724. (defvar cl-indent-body-introducing-loop-macro-keyword
  725. "do\\|finally\\|initially\\|doing\\|collect\\|collecting\\|append\\|appending"
  726. "Regexp matching loop macro keywords which introduce body-forms")
  727. ;; This is so "and when" and "else when" get handled right
  728. ;; (not to mention "else do" !!!)
  729. (defvar cl-indent-prefix-loop-macro-keyword
  730. "and\\|else"
  731. "Regexp matching loop macro keywords which are prefixes")
  732. (defvar cl-indent-clause-joining-loop-macro-keyword
  733. "and"
  734. "Regexp matching 'and', and anything else there ever comes to be
  735. like it ...")
  736. ;; This is handled right, but it's incomplete ...
  737. ;; (It could probably get arbitrarily long if I did *every* iteration-path)
  738. (defvar cl-indent-indented-loop-macro-keyword
  739. "into\\|by\\|upto\\|downto\\|above\\|below\\|on\\|in\\|being\\|=\\|first\\|then\\|from\\|to"
  740. "Regexp matching keywords introducing loop subclauses. Always indented two")
  741. (defvar cl-indent-indenting-loop-macro-keyword
  742. "when\\|unless\\|if"
  743. "Regexp matching keywords introducing conditional clauses.
  744. Cause subsequent clauses to be indented")
  745. (defvar cl-indent-loop-macro-else-keyword "else")
  746. ;;; Attempt to indent the loop macro ...
  747. (defun cl-indent::indent-loop-macro
  748. (path parse-state indent-point sexp-column normal-indent)
  749. (list (cl-indent-indent-loop-macro-1 parse-state indent-point)
  750. (cl-indent-parse-state-start parse-state)))
  751. (defun cl-indent-indent-loop-macro-1 (parse-state indent-point)
  752. (catch 'return-indentation
  753. (save-excursion
  754. ;; Find first clause of loop macro, and use it to establish
  755. ;; base column for indentation
  756. (goto-char (cl-indent-parse-state-start parse-state))
  757. (let ((loop-start-column (current-column)))
  758. (cl-indent-loop-advance-past-keyword-on-line)
  759. (if (eolp)
  760. (progn
  761. (forward-line 1)
  762. (end-of-line)
  763. ;; If indenting first line after "(loop <newline>"
  764. ;; cop out ...
  765. (if (<= indent-point (point))
  766. (throw 'return-indentation (+ 2 loop-start-column)))
  767. (back-to-indentation)))
  768. (let* ((case-fold-search t)
  769. (loop-macro-first-clause (point))
  770. (previous-expression-start (cl-indent-parse-state-prev parse-state))
  771. (default-value (current-column))
  772. (loop-body-p nil)
  773. (loop-body-indentation nil)
  774. (indented-clause-indentation (+ 2 default-value)))
  775. ;; Determine context of this loop clause, starting with the
  776. ;; expression immediately preceding the line we're trying to indent
  777. (goto-char previous-expression-start)
  778. ;; Handle a body-introducing-clause which ends a line specially.
  779. (if (looking-at cl-indent-body-introducing-loop-macro-keyword)
  780. (let ((keyword-position (current-column)))
  781. (setq loop-body-p t)
  782. (setq loop-body-indentation
  783. (if (cl-indent-loop-advance-past-keyword-on-line)
  784. (current-column)
  785. (back-to-indentation)
  786. (if (/= (current-column) keyword-position)
  787. (+ 2 (current-column))
  788. (- keyword-position 3)))))
  789. (back-to-indentation)
  790. (if (< (point) loop-macro-first-clause)
  791. (goto-char loop-macro-first-clause))
  792. ;; If there's an "and" or "else," advance over it.
  793. ;; If it is alone on the line, the next "cond" will treat it
  794. ;; as if there were a "when" and indent under it ...
  795. (let ((exit nil))
  796. (while (and (null exit)
  797. (looking-at cl-indent-prefix-loop-macro-keyword))
  798. (if (null (cl-indent-loop-advance-past-keyword-on-line))
  799. (progn (setq exit t)
  800. (back-to-indentation)))))
  801. ;; Found start of loop clause preceding the one we're trying to indent.
  802. ;; Glean context ...
  803. (cond
  804. ((looking-at "(")
  805. ;; We're in the middle of a clause body ...
  806. (setq loop-body-p t)
  807. (setq loop-body-indentation (current-column)))
  808. ((looking-at cl-indent-body-introducing-loop-macro-keyword)
  809. (setq loop-body-p t)
  810. ;; Know there's something else on the line (or would
  811. ;; have been caught above)
  812. (cl-indent-loop-advance-past-keyword-on-line)
  813. (setq loop-body-indentation (current-column)))
  814. (t
  815. (setq loop-body-p nil)
  816. (if (or (looking-at cl-indent-indenting-loop-macro-keyword)
  817. (looking-at cl-indent-prefix-loop-macro-keyword))
  818. (setq default-value (+ 2 (current-column))))
  819. (setq indented-clause-indentation (+ 2 (current-column)))
  820. ;; We still need loop-body-indentation for "syntax errors" ...
  821. (goto-char previous-expression-start)
  822. (setq loop-body-indentation (current-column)))))
  823. ;; Go to first non-blank character of the line we're trying to indent.
  824. ;; (if none, wind up poised on the new-line ...)
  825. (goto-char indent-point)
  826. (back-to-indentation)
  827. (cond
  828. ((looking-at "(")
  829. ;; Clause body ...
  830. loop-body-indentation)
  831. ((or (eolp) (looking-at ";"))
  832. ;; Blank line. If body-p, indent as body, else indent as
  833. ;; vanilla clause.
  834. (if loop-body-p
  835. loop-body-indentation
  836. default-value))
  837. ((looking-at cl-indent-indented-loop-macro-keyword)
  838. indented-clause-indentation)
  839. ((looking-at cl-indent-clause-joining-loop-macro-keyword)
  840. (let ((stolen-indent-column nil))
  841. (forward-line -1)
  842. (while (and (null stolen-indent-column)
  843. (> (point) loop-macro-first-clause))
  844. (back-to-indentation)
  845. (if (and (< (current-column) loop-body-indentation)
  846. (looking-at "\\sw"))
  847. (progn
  848. (if (looking-at cl-indent-loop-macro-else-keyword)
  849. (cl-indent-loop-advance-past-keyword-on-line))
  850. (setq stolen-indent-column
  851. (current-column)))
  852. (forward-line -1)))
  853. (if stolen-indent-column
  854. stolen-indent-column
  855. default-value)))
  856. (t default-value)))))))
  857. (defun cl-indent-loop-advance-past-keyword-on-line ()
  858. (forward-word 1)
  859. (block move-forward
  860. (while (and (looking-at "\\s-") (not (eolp)))
  861. (forward-char 1)
  862. (when (looking-at "\\s<")
  863. ;; eat up the comment (sorry, this will fail for for lisp block comments
  864. (while (and (not (looking-at "\\s>")) (not (eolp)))
  865. (forward-char 1)))))
  866. (if (eolp)
  867. nil
  868. (current-column)))
  869. ;;;###autoload
  870. (defun define-cl-indent (spec &optional mode-methods)
  871. "Define the cl-indent specification SPEC, maybe mode-specific.
  872. The car of SPEC is the symbol for which the indentation shall be specified.
  873. If the cdr is a symbol, then this symbol shall be indented like
  874. the other symbol is indented _currently_ (i.e., eager evaluation is
  875. used, not lazy evaluation).
  876. Otherwise the cadr is taken as the indentation method. Check
  877. #'cl-indent:function for documentation about indentation methods. Note
  878. further that #'cl-indent::method interprets indentation methods that
  879. are strings as aliases, i.e., the indentation method of the string is
  880. looked up and returned (lazy evaluation).
  881. The optional argument MODE-METHODS may be bound to a hash table
  882. where this (presumedly mode-specific) indentation method shall be
  883. stored."
  884. (let* ((symbol (car spec))
  885. (indent (cdr spec))
  886. (method (if (symbolp indent)
  887. ;; If an alias is defined, it might be mode-specific.
  888. ;; Rebind cl-indent:mode-methods for lookup,
  889. ;; that's possible as all symbols have dynamic
  890. ;; scope in Emacs Lisp.
  891. (let ((cl-indent:mode-methods (and mode-methods
  892. 'mode-methods)))
  893. (cl-indent::method (symbol-name indent)))
  894. (car indent))))
  895. (if mode-methods
  896. (puthash symbol method mode-methods)
  897. (put symbol 'cl-indent:method method))))
  898. ;;; ------------------------------------------------------------
  899. ;;;
  900. ;;; issue specifications for Common Lisp forms
  901. ;;;
  902. (mapcar #'define-cl-indent
  903. '((block 1)
  904. (case (4 &rest (&whole 2 &rest 3)))
  905. (ccase . case) (ecase . case)
  906. (typecase . case) (etypecase . case) (ctypecase . case)
  907. (handler-bind . let)
  908. (handler-case (4 &rest (&whole 2 4 &rest 2)))
  909. (catch 1)
  910. (cond cl-indent:indent-cond)
  911. (defvar (4 2 2))
  912. (defconstant . defvar) (defparameter . defvar)
  913. (defclass (6 6 (&whole 2 &rest 1) &rest 2))
  914. (define-modify-macro
  915. (4 &body))
  916. (defsetf (4 (&whole 4 &rest 1) 4 &body))
  917. (defun (4 (&whole 4 &rest 1) &body))
  918. (defmacro . defun) (define-setf-method . defun) (deftype . defun)
  919. (defgeneric cl-indent:indent-defgeneric)
  920. (defmethod cl-indent:indent-defmethod)
  921. (defstruct ((&whole 4 &rest (&whole 2 &rest 1))
  922. &rest (&whole 2 &rest 1)))
  923. (destructuring-bind
  924. ((&whole 6 &rest 1) 4 &body))
  925. (do ((&whole 4 &rest (&whole 1 &rest 2)) ; ((arg step incr))
  926. (&whole 4 &rest 3) ; result: ((condition) (form) ...)
  927. &rest cl-indent:indent-do))
  928. (do* . do)
  929. (do-all-symbols (4 &body))
  930. (do-symbols (4 &body))
  931. (do-external-symbols (4 &body))
  932. (dolist ((&whole 4 2 1) &body))
  933. (dotimes . dolist)
  934. (eval-when 1)
  935. (flet ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
  936. &body))
  937. (labels . flet) (macrolet . flet)
  938. (if (&rest 4))
  939. ;; FIXME: Which of those do I really want?
  940. ;; (lambda ((&whole 4 &rest 1) &body))
  941. (lambda ((&whole 4 &rest 1)
  942. &rest cl-indent:indent-function-lambda-hack))
  943. (let ((&whole 4 &rest (&whole 1 1 2)) &body))
  944. (let* . let) (compiler-let . let)
  945. (locally 1)
  946. (loop cl-indent::indent-loop-macro)
  947. (multiple-value-bind
  948. ((&whole 6 &rest 1) 4 &body))
  949. (multiple-value-call
  950. (4 &body))
  951. (multiple-value-list 1)
  952. (multiple-value-prog1 1)
  953. (multiple-value-setq
  954. (4 2))
  955. (print-unreadable-object 1)
  956. ;; Combines the worst features of BLOCK, LET and TAGBODY
  957. (prog ((&whole 4 &rest 1) &rest cl-indent:indent-tagbody))
  958. (prog* . prog)
  959. (prog1 1)
  960. (prog2 2)
  961. (progn 0)
  962. (progv (4 4 &body))
  963. (restart-case . handler-case)
  964. (return 0)
  965. (return-from (nil &body))
  966. (tagbody cl-indent:indent-tagbody)
  967. (throw 1)
  968. (unless 1)
  969. (unwind-protect
  970. (5 &body))
  971. (values 0)
  972. (when 1)
  973. (with-accessors (6 4 &body))
  974. (with-compilation-unit (4 &body))
  975. (with-hash-table-iterator (4 &body))
  976. (with-output-to-string (4 &body))
  977. (with-input-from-string . with-output-to-string)
  978. (with-open-file (4 &body))
  979. (with-open-stream . with-open-file)
  980. (with-package-iterator (4 &body))
  981. (with-simple-restart (4 &body))
  982. (with-slots (6 4 &body))))
  983. ;;; ======================================================================
  984. ;;
  985. ;; $ITIlog: cl-indent.el,v $
  986. ;; Revision 1.6 1995/09/10 14:13:34 schrod
  987. ;; Add aliassing of indentation methods.
  988. ;; Discard unused variables. Quiet down the byte-compiler. Discard
  989. ;; duplicate indentation specs.
  990. ;; `Define-as' specs in #'define-cl-indent uses the mode-specific
  991. ;; method table for lookup of the reference symbol's indentation method,
  992. ;; if a table was supplied.
  993. ;;
  994. ;; Revision 1.5 1995/08/14 16:49:05 schrod
  995. ;; Provide 'cl-indent, this module may not be required otherwise.
  996. ;;
  997. ;; Revision 1.4 1995/07/24 18:16:50 schrod
  998. ;; Did not work due to spurious closing brace.
  999. ;;
  1000. ;; Revision 1.3 1995/01/17 11:13:25 schrod
  1001. ;; Add support for mode-specific and local indentation methods. Don't
  1002. ;; need STIL indentation support any more, that's an own mode now.
  1003. ;; Provide a load hook to be able to adapt global indentation methods
  1004. ;; to personal preferences.
  1005. ;; All form symbols are finally checked for global indentation
  1006. ;; methods bound to 'lisp-indent-function, for upward compatibility to
  1007. ;; standard Lisp indentation.
  1008. ;; Renamed all symbols to start with `cl-indent:'. Private symbols
  1009. ;; use `::', similar to CL. #'define-cl-indent is an exception, as usual.
  1010. ;; I hope that the new names are more meaningful, too.
  1011. ;; Added some pointers to function documentation to the usage
  1012. ;; comments at the start. Mentioned additional future projects.
  1013. ;;
  1014. ;; Revision 1.2 1994/09/05 17:35:47 schrod
  1015. ;; Added documentation to every function.
  1016. ;; Added #'cl-indent and #'define-cl-indent as user-level functions.
  1017. ;; Renamed all functions from lisp-indent-* to cl-indent-* to avoid
  1018. ;; name clashes with `normal' lisp-mode indentation. In particular,
  1019. ;; rename #'lisp-indent-259 (what a ridiculous name to use for a
  1020. ;; function!) to #'cl-indent-by-method.
  1021. ;; Introduced #'cl-indent-normal to compute the normal (default)
  1022. ;; indentation, since #'current-column does not always deliver the
  1023. ;; correct result.
  1024. ;; Introduced #'get-cl-indent-method to encapsulate the storage of an
  1025. ;; indentation method. Might want to change this later anyhow, to support
  1026. ;; mode- and file-specific indentation.
  1027. ;; Check for a correct method is in an own function now,
  1028. ;; #'cl-indent-by-method was large enough already.
  1029. ;; #'lisp-indent-do is never called for the first two elements in a
  1030. ;; path, this test (and the else form) could be discarded.
  1031. ;; Add support for more CL constructs (CLOS, CLCS, condition stuff
  1032. ;; Define STIL constructs, this should be discarded with the
  1033. ;; introduction of mode-specific indentation methods.
  1034. ;;
  1035. (provide 'slime-indentation)