Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1255 rivejä
49 KiB

4 vuotta sitten
  1. ;;; haskell-indentation.el --- indentation module for Haskell Mode -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2013 Kristof Bastiaensen, Gergely Risko
  3. ;; Author: Kristof Bastiaensen <kristof.bastiaensen@vleeuwen.org>
  4. ;; Author: Gergely Risko <errge@nilcons.com>
  5. ;; Keywords: indentation haskell
  6. ;; URL: https://github.com/haskell/haskell-mode
  7. ;; This file is not part of GNU Emacs.
  8. ;; This file is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; This file is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Installation:
  20. ;;
  21. ;; To turn indentation on for all Haskell buffers under Haskell mode add
  22. ;; this to your configuration file:
  23. ;;
  24. ;; (add-hook haskell-mode-hook 'haskell-indentation-mode)
  25. ;;
  26. ;; Otherwise, call `haskell-indentation-mode'.
  27. ;;; Code:
  28. ;; TODO eliminate magic number 2 where possible, use a variable
  29. ;; TODO `haskell-indentation-find-indentation' — fix it, get rid of "safe"
  30. ;; version
  31. (require 'cl-lib)
  32. (require 'haskell-lexeme)
  33. ;;;###autoload
  34. (defgroup haskell-indentation nil
  35. "Haskell indentation."
  36. :link '(custom-manual "(haskell-mode)Indentation")
  37. :group 'haskell
  38. :prefix "haskell-indentation-")
  39. (defcustom haskell-indentation-layout-offset 2
  40. "Extra indentation to add before expressions in a Haskell layout list."
  41. :type 'integer
  42. :group 'haskell-indentation)
  43. (defcustom haskell-indentation-starter-offset 2
  44. "Extra indentation after an opening keyword (e.g. \"let\")."
  45. :type 'integer
  46. :group 'haskell-indentation)
  47. (defcustom haskell-indentation-left-offset 2
  48. "Extra indentation after an indentation to the left (e.g. after \"do\")."
  49. :type 'integer
  50. :group 'haskell-indentation)
  51. (defcustom haskell-indentation-where-pre-offset 2
  52. "Extra indentation before the keyword \"where\"."
  53. :type 'integer
  54. :group 'haskell-indentation)
  55. (defcustom haskell-indentation-where-post-offset 2
  56. "Extra indentation after the keyword \"where\"."
  57. :type 'integer
  58. :group 'haskell-indentation)
  59. (defcustom haskell-indentation-electric-flag nil
  60. "Non-nil means insertion of some characters may auto reindent the line.
  61. If the variable `electric-indent-mode' is non-nil then this variable is
  62. overridden."
  63. :type 'symbol
  64. :group 'haskell-indentation)
  65. (make-variable-buffer-local 'haskell-indentation-electric-flag)
  66. (defvar haskell-indentation-mode-map
  67. (let ((map (make-sparse-keymap)))
  68. (define-key map (kbd "RET") #'haskell-indentation-newline-and-indent)
  69. (define-key map (kbd "<backtab>") #'haskell-indentation-indent-backwards)
  70. (define-key map (kbd ",") #'haskell-indentation-common-electric-command)
  71. (define-key map (kbd ";") #'haskell-indentation-common-electric-command)
  72. (define-key map (kbd ")") #'haskell-indentation-common-electric-command)
  73. (define-key map (kbd "}") #'haskell-indentation-common-electric-command)
  74. (define-key map (kbd "]") #'haskell-indentation-common-electric-command)
  75. map)
  76. "Keymap for `haskell-indentation-mode'.")
  77. ;;;###autoload
  78. (define-minor-mode haskell-indentation-mode
  79. "Haskell indentation mode that deals with the layout rule.
  80. It rebinds RET, DEL and BACKSPACE, so that indentations can be
  81. set and deleted as if they were real tabs."
  82. :keymap haskell-indentation-mode-map
  83. (kill-local-variable 'indent-line-function)
  84. (kill-local-variable 'indent-region-function)
  85. (when haskell-indentation-mode
  86. (when (and (bound-and-true-p haskell-indent-mode)
  87. (fboundp 'turn-off-haskell-indent))
  88. (turn-off-haskell-indent))
  89. (setq-local indent-line-function #'haskell-indentation-indent-line)
  90. (setq-local indent-region-function #'haskell-indentation-indent-region)))
  91. ;;;###autoload
  92. (defun turn-on-haskell-indentation ()
  93. "Turn on the haskell-indentation minor mode."
  94. (interactive)
  95. (haskell-indentation-mode t))
  96. (make-obsolete 'turn-on-haskell-indentation
  97. 'haskell-indentation-mode
  98. "2015-05-25")
  99. (defvar haskell-literate) ; defined in haskell-mode.el
  100. (defun haskell-indentation-bird-p ()
  101. "Return t if this is a literate Haskell buffer in bird style, NIL otherwise."
  102. (eq haskell-literate 'bird))
  103. ;;----------------------------------------------------------------------------
  104. ;; UI starts here
  105. (defun haskell-indentation-reindent-to (col &optional move)
  106. "Reindent current line to COL, move the point there if MOVE is non-NIL."
  107. (let* ((ci (haskell-indentation-current-indentation)))
  108. (save-excursion
  109. (move-to-column ci)
  110. (if (<= ci col)
  111. (insert-before-markers (make-string (- col ci) ? ))
  112. (delete-char (- col ci))))
  113. (when move
  114. (move-to-column col))))
  115. (defun haskell-indentation-indent-rigidly (start end arg)
  116. "Indent all lines starting in the region sideways by ARG columns.
  117. Called from a program, takes three arguments, START, END and ARG.
  118. You can remove all indentation from a region by giving a large
  119. negative ARG. Handles bird style literate Haskell too."
  120. (interactive "*r\np")
  121. (save-excursion
  122. (goto-char end)
  123. (let ((end-marker (point-marker)))
  124. (goto-char start)
  125. (or (bolp) (forward-line 0))
  126. (while (< (point) end-marker)
  127. (let ((ci (haskell-indentation-current-indentation)))
  128. (when (and t
  129. (eq (char-after) ?>))
  130. (forward-char 1))
  131. (skip-syntax-forward "-")
  132. (unless (eolp)
  133. (haskell-indentation-reindent-to (max 0 (+ ci arg))))
  134. (forward-line 1)))
  135. (move-marker end-marker nil))))
  136. (defun haskell-indentation-current-indentation ()
  137. "Column position of first non-whitespace character in current line."
  138. (save-excursion
  139. (beginning-of-line)
  140. (when (haskell-indentation-bird-p)
  141. (forward-char))
  142. (skip-syntax-forward "-")
  143. (current-column)))
  144. (defun haskell-indentation-bird-outside-code-p ()
  145. "Non-NIL if we are in bird literate mode, but outside of code."
  146. (and (haskell-indentation-bird-p)
  147. (or (< (current-column) 2)
  148. (save-excursion
  149. (beginning-of-line)
  150. (not (eq (char-after) ?>))))))
  151. (defun haskell-indentation-newline-and-indent ()
  152. "Insert newline and indent."
  153. (interactive "*")
  154. ;; On RET (or C-j), we:
  155. ;; - just jump to the next line if literate haskell, but outside code
  156. (if (haskell-indentation-bird-outside-code-p)
  157. (progn
  158. (delete-horizontal-space)
  159. (newline))
  160. ;; - save the current column
  161. (let ((ci (haskell-indentation-current-indentation)))
  162. ;; - jump to the next line and reindent to at the least same level
  163. (delete-horizontal-space)
  164. (newline)
  165. ;; calculate indentation after newline is inserted because if we
  166. ;; break an identifier we might create a keyword, for example
  167. ;; "dowhere" => "do where"
  168. (let ((indentations (or (haskell-indentation-find-indentations)
  169. '(0))))
  170. (when (haskell-indentation-bird-p)
  171. (insert "> "))
  172. (haskell-indentation-reindent-to
  173. (haskell-indentation-next-indentation (- ci 1) indentations 'nofail)
  174. 'move)))))
  175. (defun haskell-indentation-next-indentation (col indentations &optional nofail)
  176. "Find the leftmost indentation which is greater than COL.
  177. Indentations are taken from INDENTATIONS, which should be a
  178. list. Return the last indentation if there are no bigger ones and
  179. NOFAIL is non-NIL."
  180. (when (null indentations)
  181. (error "haskell-indentation-next-indentation called with empty list"))
  182. (or (cl-find-if (lambda (i) (> i col)) indentations)
  183. (when nofail
  184. (car (last indentations)))))
  185. (defun haskell-indentation-previous-indentation (col indentations &optional nofail)
  186. "Find the rightmost indentation less than COL from INDENTATIONS.
  187. When no indentations are less than COL, return the rightmost indentation
  188. if NOFAIL is non-nil, or nil otherwise."
  189. (when (null indentations)
  190. (error "haskell-indentation-previous-indentation called with empty list"))
  191. (let ((rev (reverse indentations)))
  192. (or (cl-find-if (lambda (i) (< i col)) rev)
  193. (when nofail
  194. (car rev)))))
  195. (defvar haskell-indentation-dyn-last-direction nil
  196. "") ; FIXME
  197. (defvar haskell-indentation-dyn-last-indentations nil
  198. "") ; FIXME
  199. (defun haskell-indentation-indent-line ()
  200. "Indent current line, cycle though indentation positions.
  201. Do nothing inside multiline comments and multiline strings.
  202. Start enumerating the indentation points to the right. The user
  203. can continue by repeatedly pressing TAB. When there is no more
  204. indentation points to the right, we switch going to the left."
  205. (interactive "*")
  206. ;; try to repeat
  207. (when (not (haskell-indentation-indent-line-repeat))
  208. (setq haskell-indentation-dyn-last-direction nil)
  209. ;; parse error is intentionally not cought here, it may come from
  210. ;; `haskell-indentation-find-indentations', but escapes the scope
  211. ;; and aborts the opertaion before any moving happens
  212. (let* ((cc (current-column))
  213. (ci (haskell-indentation-current-indentation))
  214. (inds (save-excursion
  215. (move-to-column ci)
  216. (or (haskell-indentation-find-indentations)
  217. '(0))))
  218. (valid (memq ci inds))
  219. (cursor-in-whitespace (< cc ci)))
  220. (if (and valid cursor-in-whitespace)
  221. (move-to-column ci)
  222. (haskell-indentation-reindent-to
  223. (haskell-indentation-next-indentation ci inds 'nofail)
  224. cursor-in-whitespace))
  225. (setq haskell-indentation-dyn-last-direction 'right
  226. haskell-indentation-dyn-last-indentations inds))))
  227. (defun haskell-indentation-indent-line-repeat ()
  228. "Cycle though indentation positions."
  229. (cond
  230. ((and (memq last-command
  231. '(indent-for-tab-command
  232. haskell-indentation-indent-backwards))
  233. (eq haskell-indentation-dyn-last-direction 'region))
  234. (let ((mark-even-if-inactive t))
  235. (haskell-indentation-indent-rigidly
  236. (region-beginning)
  237. (region-end)
  238. 1))
  239. t)
  240. ((and (eq last-command 'indent-for-tab-command)
  241. (memq haskell-indentation-dyn-last-direction '(left right))
  242. haskell-indentation-dyn-last-indentations)
  243. (let ((ci (haskell-indentation-current-indentation)))
  244. (if (eq haskell-indentation-dyn-last-direction 'left)
  245. (haskell-indentation-reindent-to
  246. (haskell-indentation-previous-indentation
  247. ci haskell-indentation-dyn-last-indentations 'nofail))
  248. ;; right
  249. (if (haskell-indentation-next-indentation
  250. ci haskell-indentation-dyn-last-indentations)
  251. (haskell-indentation-reindent-to
  252. (haskell-indentation-next-indentation
  253. ci haskell-indentation-dyn-last-indentations 'nofail))
  254. ;; but failed, switch to left
  255. (setq haskell-indentation-dyn-last-direction 'left)
  256. (haskell-indentation-indent-line-repeat)))
  257. t))
  258. (t nil)))
  259. (defun haskell-indentation-indent-region (_start _end)
  260. "This function does nothing.
  261. It is better to do nothing to indent region in Haskell than to
  262. break the semantics of indentation. This function is used for
  263. `indent-region-function' because the default is to call
  264. `indent-line-function' on every line from START to END and that
  265. also produces catastrophic results.
  266. Someday we will have indent region that preserves semantics and
  267. fixes up only indentation."
  268. nil)
  269. (defun haskell-indentation-indent-backwards ()
  270. "Indent the current line to the previous indentation point."
  271. (interactive "*")
  272. (cond
  273. ((and (memq last-command
  274. '(indent-for-tab-command haskell-indentation-indent-backwards))
  275. (eq haskell-indentation-dyn-last-direction 'region))
  276. (let ((mark-even-if-inactive t))
  277. (haskell-indentation-indent-rigidly (region-beginning) (region-end) -1)))
  278. ((use-region-p)
  279. (setq haskell-indentation-dyn-last-direction 'region)
  280. (haskell-indentation-indent-rigidly (region-beginning) (region-end) -1)
  281. (message "Press TAB or S-TAB again to indent the region more"))
  282. (t
  283. (setq haskell-indentation-dyn-last-direction nil)
  284. (let* ((cc (current-column))
  285. (ci (haskell-indentation-current-indentation))
  286. (inds (save-excursion
  287. (move-to-column ci)
  288. (or (haskell-indentation-find-indentations)
  289. '(0))))
  290. (cursor-in-whitespace (< cc ci))
  291. (pi (haskell-indentation-previous-indentation ci inds)))
  292. (if (null pi)
  293. ;; if there are no more indentations to the left, just go to column 0
  294. (haskell-indentation-reindent-to
  295. (car (haskell-indentation-first-indentation)) cursor-in-whitespace)
  296. (haskell-indentation-reindent-to pi cursor-in-whitespace))))))
  297. (defun haskell-indentation-common-electric-command (arg)
  298. "Call `self-insert-command' to insert the character typed ARG times
  299. and indent when all of the following are true:
  300. 1) The character is the first non-whitespace character on the line.
  301. 2) There is only one possible indentation position.
  302. 3) The variable `electric-indent-mode' or `haskell-indentation-electric-flag'
  303. is non-nil.
  304. 4) The point is not in a comment, string, or quasiquote."
  305. (interactive "*p")
  306. (let ((col (current-column))
  307. ind)
  308. (self-insert-command arg)
  309. (when (and (or haskell-indentation-electric-flag
  310. electric-indent-mode)
  311. (= (haskell-indentation-current-indentation)
  312. col)
  313. (> arg 0)
  314. (not (nth 8 (syntax-ppss)))
  315. (= 1 (save-excursion
  316. (move-to-column col)
  317. (length (setq ind (haskell-indentation-find-indentations))))))
  318. (haskell-indentation-reindent-to (car ind)))))
  319. ;;----------------------------------------------------------------------------
  320. ;; Parser Starts Here
  321. ;; The parser is implemented as a recursive descent parser. Each parser
  322. ;; advances the point to after the expression it parses, and sets the
  323. ;; dynamic scoped variables containing the information about the
  324. ;; indentations. The dynamic scoping allows transparent backtracking to
  325. ;; previous states of these variables. A new state can be set using `let'.
  326. ;; When the scope of this function ends, the variable is automatically
  327. ;; reverted to its old value.
  328. ;; This is basicly a performance hack. It would have been possible to
  329. ;; thread this state using a association-list through the parsers, but it
  330. ;; would be probably more complicated and slower due to the lack of real
  331. ;; closures in Emacs Lisp.
  332. ;;
  333. ;; When finished parsing, the tokenizer returns 'end-token, and
  334. ;; following-token is set to the token after point. The parser adds its
  335. ;; indentations to possible-indentations and returns to it's parent, or
  336. ;; exits non-locally by throwing parse-end, so that the parent will not add
  337. ;; new indentations to it.
  338. ;; the parse state:
  339. (defvar following-token) ;; the next token after parsing finished
  340. ;; the token at the current parser point or a pseudo-token (see
  341. ;; `haskell-indentation-read-next-token')
  342. (defvar current-token)
  343. (defvar previous-token)
  344. (defvar left-indent) ;; most left possible indentation
  345. (defvar starter-indent) ;; column at a keyword
  346. (defvar current-indent) ;; the most right indentation
  347. (defvar layout-indent) ;; the column of the layout list
  348. (defvar possible-indentations) ;; the return value of the indentations
  349. (defvar indentation-point) ;; where to stop parsing
  350. (defvar implicit-layout-active) ;; is "off-side" rule active?
  351. (defun haskell-indentation-goto-least-indentation ()
  352. "" ; FIXME
  353. (beginning-of-line)
  354. (if (haskell-indentation-bird-p)
  355. (catch 'return
  356. (while t
  357. (when (not (eq (char-after) ?>))
  358. (forward-line)
  359. (forward-char 2)
  360. (throw 'return nil))
  361. (let ((ps (nth 8 (syntax-ppss))))
  362. (when ps ;; inside comment or string
  363. (goto-char ps)
  364. (beginning-of-line)))
  365. (when (and (>= 2 (haskell-indentation-current-indentation))
  366. (not (looking-at ">\\s-*$")))
  367. (forward-char 2)
  368. (throw 'return nil))
  369. (when (bobp)
  370. (forward-char 2)
  371. (throw 'return nil))
  372. (forward-line -1)))
  373. ;; not bird style
  374. (catch 'return
  375. (while (not (bobp))
  376. (let ((point (point)))
  377. ;; (forward-comment -1) gets lost if there are unterminated
  378. ;; string constants and does not move point anywhere. We fix
  379. ;; that case with (forward-line -1)
  380. (forward-comment (- (buffer-size)))
  381. (if (equal (point) point)
  382. (forward-line -1)
  383. (beginning-of-line)))
  384. (let* ((ps (syntax-ppss))
  385. (start-of-comment-or-string (nth 8 ps))
  386. (start-of-list-expression (nth 1 ps)))
  387. (cond
  388. (start-of-comment-or-string
  389. ;; inside comment or string
  390. (goto-char start-of-comment-or-string))
  391. (start-of-list-expression
  392. ;; inside a parenthesized expression
  393. (goto-char start-of-list-expression))
  394. ((= 0 (haskell-indentation-current-indentation))
  395. (throw 'return nil))))))
  396. (beginning-of-line)
  397. (when (bobp)
  398. (forward-comment (buffer-size)))))
  399. (defun haskell-indentation-parse-to-indentations ()
  400. "" ; FIXME
  401. (save-excursion
  402. (skip-syntax-forward "-")
  403. (let ((indentation-point (point))
  404. (layout-indent 0)
  405. (current-indent haskell-indentation-layout-offset)
  406. (starter-indent haskell-indentation-layout-offset)
  407. (left-indent haskell-indentation-layout-offset)
  408. (case-fold-search nil)
  409. current-token
  410. previous-token
  411. following-token
  412. possible-indentations
  413. implicit-layout-active)
  414. (haskell-indentation-goto-least-indentation)
  415. (if (<= indentation-point (point))
  416. (haskell-indentation-first-indentation)
  417. (setq current-token (haskell-indentation-peek-token))
  418. (catch 'parse-end
  419. (haskell-indentation-toplevel))
  420. possible-indentations))))
  421. (defun haskell-indentation-first-indentation ()
  422. "Return column of first indentation."
  423. (list (if (haskell-indentation-bird-p) 2 0)))
  424. (defun haskell-indentation-find-indentations ()
  425. "Return list of indentation positions corresponding to actual cursor position."
  426. (let ((ppss (syntax-ppss)))
  427. (cond
  428. ((nth 3 ppss)
  429. (if (save-excursion
  430. (and (forward-line -1)
  431. (< (nth 8 ppss) (point))))
  432. ;; if this string goes over more than one line we want to
  433. ;; sync with the last line, not the first one
  434. (list (save-excursion
  435. (forward-line -1)
  436. (current-indentation)))
  437. (append
  438. (haskell-indentation-first-indentation)
  439. (list (save-excursion
  440. (goto-char (nth 8 ppss))
  441. (current-column))))))
  442. ((nth 4 ppss)
  443. (if (save-excursion
  444. (and (skip-syntax-forward "-")
  445. (eolp)
  446. (not (> (forward-line 1) 0))
  447. (not (nth 4 (syntax-ppss)))))
  448. (haskell-indentation-parse-to-indentations)
  449. (haskell-indentation-first-indentation)))
  450. (t
  451. (haskell-indentation-parse-to-indentations)))))
  452. (defconst haskell-indentation-unicode-tokens
  453. '(("" . "->") ;; #x2192 RIGHTWARDS ARROW
  454. ("" . "::") ;; #x2237 PROPORTION
  455. ("" . "<-") ;; #x2190 LEFTWARDS ARROW
  456. ("" . "=>") ;; #x21D2 RIGHTWARDS DOUBLE ARROW
  457. ("" . "forall") ;; #x2200 FOR ALL
  458. ("" . "-<") ;; #x2919 LEFTWARDS ARROW-TAIL
  459. ("" . ">-") ;; #x291A RIGHTWARDS ARROW-TAIL
  460. ("" . "-<<") ;; #x291B LEFTWARDS DOUBLE ARROW-TAIL
  461. ("" . ">>-") ;; #x291C RIGHTWARDS DOUBLE ARROW-TAIL
  462. ("" . "*")) ;; #x2605 BLACK STAR
  463. "Translation from UnicodeSyntax tokens to their ASCII representation.")
  464. (defconst haskell-indentation-toplevel-list
  465. `(("module" . haskell-indentation-module)
  466. ("signature" . haskell-indentation-module)
  467. ("data" . haskell-indentation-data)
  468. ("type" . haskell-indentation-data)
  469. ("newtype" . haskell-indentation-data)
  470. ("import" . haskell-indentation-import)
  471. ("foreign" . haskell-indentation-foreign)
  472. ("where" . haskell-indentation-toplevel-where)
  473. ("class" . haskell-indentation-class-declaration)
  474. ("instance" . haskell-indentation-class-declaration)
  475. ("deriving" . haskell-indentation-deriving))
  476. "Alist of toplevel keywords with associated parsers.")
  477. (defconst haskell-indentation-type-list
  478. `(("::" .
  479. ,(apply-partially 'haskell-indentation-with-starter
  480. (apply-partially 'haskell-indentation-separated
  481. 'haskell-indentation-type '("->" "=>"))))
  482. ("(" .
  483. ,(apply-partially 'haskell-indentation-list
  484. 'haskell-indentation-type ")" ","))
  485. ("[" .
  486. ,(apply-partially 'haskell-indentation-list
  487. 'haskell-indentation-type "]" ","))
  488. ("{" .
  489. ,(apply-partially 'haskell-indentation-list
  490. 'haskell-indentation-type "}" ",")))
  491. "Alist of tokens in type declarations with associated parsers.")
  492. (defconst haskell-indentation-expression-list
  493. `(("data" . haskell-indentation-data)
  494. ("type" . haskell-indentation-data)
  495. ("newtype" . haskell-indentation-data)
  496. ("if" . haskell-indentation-if)
  497. ("let" .
  498. ,(apply-partially 'haskell-indentation-phrase
  499. '(haskell-indentation-declaration-layout
  500. "in" haskell-indentation-expression)))
  501. ("do" .
  502. ,(apply-partially 'haskell-indentation-with-starter
  503. 'haskell-indentation-expression-layout))
  504. ("mdo" .
  505. ,(apply-partially 'haskell-indentation-with-starter
  506. 'haskell-indentation-expression-layout))
  507. ("rec" .
  508. ,(apply-partially 'haskell-indentation-with-starter
  509. 'haskell-indentation-expression-layout))
  510. ("case" .
  511. ,(apply-partially 'haskell-indentation-phrase
  512. '(haskell-indentation-expression
  513. "of" haskell-indentation-case-layout)))
  514. ("\\" .
  515. ,(apply-partially 'haskell-indentation-with-starter
  516. 'haskell-indentation-lambda-maybe-lambdacase))
  517. ("proc" .
  518. ,(apply-partially 'haskell-indentation-phrase
  519. '(haskell-indentation-expression
  520. "->" haskell-indentation-expression)))
  521. ("where" .
  522. ,(apply-partially 'haskell-indentation-with-starter
  523. 'haskell-indentation-declaration-layout nil t))
  524. ("::" . haskell-indentation-scoped-type)
  525. ("=" .
  526. ,(apply-partially 'haskell-indentation-statement-right
  527. 'haskell-indentation-expression))
  528. ("<-" .
  529. ,(apply-partially 'haskell-indentation-statement-right
  530. 'haskell-indentation-expression))
  531. ("(" .
  532. ,(apply-partially 'haskell-indentation-list
  533. 'haskell-indentation-expression
  534. ")"
  535. '(list "," "->")))
  536. ("[" .
  537. ,(apply-partially 'haskell-indentation-list
  538. 'haskell-indentation-expression "]" "," "|"))
  539. ("{" .
  540. ,(apply-partially 'haskell-indentation-list
  541. 'haskell-indentation-expression "}" ",")))
  542. "Alist of keywords in expressions with associated parsers.")
  543. (defun haskell-indentation-expression-layout ()
  544. "Parse layout list with expressions, such as after \"do\"."
  545. (haskell-indentation-layout #'haskell-indentation-expression))
  546. (defun haskell-indentation-declaration-layout ()
  547. "Parse layout list with declarations, such as after \"where\"."
  548. (haskell-indentation-layout #'haskell-indentation-declaration))
  549. (defun haskell-indentation-case-layout ()
  550. "Parse layout list with case expressions."
  551. (haskell-indentation-layout #'haskell-indentation-case))
  552. (defun haskell-indentation-lambda-maybe-lambdacase ()
  553. "Parse lambda or lambda-case expression.
  554. After a lambda (backslash) there are two possible cases:
  555. - the new lambdacase expression, that can be recognized by the
  556. next token being \"case\";
  557. - or simply an anonymous function definition in the form of
  558. \"expression -> expression\"."
  559. (if (string= current-token "case")
  560. (haskell-indentation-with-starter
  561. #'haskell-indentation-case-layout)
  562. (haskell-indentation-phrase-rest
  563. '(haskell-indentation-expression "->" haskell-indentation-expression))))
  564. (defun haskell-indentation-fundep ()
  565. "Parse functional dependency."
  566. (haskell-indentation-with-starter
  567. (apply-partially #'haskell-indentation-separated
  568. #'haskell-indentation-fundep1 ",")))
  569. (defun haskell-indentation-fundep1 ()
  570. "Parse an item in functional dependency declaration."
  571. (let ((current-indent (current-column)))
  572. (while (member current-token '(value "->"))
  573. (haskell-indentation-read-next-token))
  574. (when (and (eq current-token 'end-tokens)
  575. (member following-token '(value "->")))
  576. (haskell-indentation-add-indentation current-indent))))
  577. (defun haskell-indentation-toplevel ()
  578. "Parse toplevel statements."
  579. (haskell-indentation-layout
  580. (lambda ()
  581. (let ((parser (assoc current-token haskell-indentation-toplevel-list)))
  582. (if parser
  583. (funcall (cdr parser))
  584. (haskell-indentation-declaration))))))
  585. (defun haskell-indentation-type ()
  586. "Parse type declaration."
  587. (let ((current-indent (current-column)))
  588. (catch 'return
  589. (while t
  590. (cond
  591. ((member current-token '(value operator "->" "=>"))
  592. (haskell-indentation-read-next-token))
  593. ((eq current-token 'end-tokens)
  594. (when (member following-token
  595. '(value operator no-following-token
  596. "(" "[" "{" "::"))
  597. (if (equal following-token "=>")
  598. (haskell-indentation-add-indentation starter-indent)
  599. (haskell-indentation-add-indentation current-indent))
  600. (haskell-indentation-add-indentation left-indent))
  601. (throw 'return nil))
  602. (t (let ((parser (assoc current-token haskell-indentation-type-list)))
  603. (if (not parser)
  604. (throw 'return nil)
  605. (funcall (cdr parser))))))))))
  606. (defun haskell-indentation-type-1 ()
  607. "Parse a single type declaration."
  608. (let ((current-indent (current-column)))
  609. (catch 'return
  610. (cond
  611. ((member current-token '(value operator "->" "=>"))
  612. (haskell-indentation-read-next-token))
  613. ((eq current-token 'end-tokens)
  614. (when (member following-token
  615. '(value operator no-following-token
  616. "->" "=>" "(" "[" "{" "::"))
  617. (haskell-indentation-add-indentation current-indent))
  618. (throw 'return nil))
  619. (t (let ((parser (assoc current-token haskell-indentation-type-list)))
  620. (if (not parser)
  621. (throw 'return nil)
  622. (funcall (cdr parser)))))))))
  623. (defun haskell-indentation-scoped-type ()
  624. "Parse scoped type declaration.
  625. For example
  626. let x :: Int = 12
  627. do x :: Int <- return 12"
  628. (haskell-indentation-with-starter
  629. (apply-partially #'haskell-indentation-separated #'haskell-indentation-type '("->" "=>")))
  630. (when (member current-token '("<-" "="))
  631. (haskell-indentation-statement-right #'haskell-indentation-expression)))
  632. (defun haskell-indentation-data ()
  633. "Parse data or type declaration."
  634. (haskell-indentation-read-next-token)
  635. (when (string= current-token "instance")
  636. (haskell-indentation-read-next-token))
  637. (haskell-indentation-type)
  638. (cond ((eq current-token 'end-tokens)
  639. (when (member following-token '("=" "where"))
  640. (haskell-indentation-add-indentation current-indent)
  641. (throw 'parse-end nil)))
  642. ((string= current-token "=")
  643. (let ((starter-indent-inside (current-column)))
  644. (haskell-indentation-with-starter
  645. (lambda ()
  646. (haskell-indentation-separated
  647. #'haskell-indentation-expression "|")))
  648. (cond
  649. ((equal current-token 'end-tokens)
  650. (when (string= following-token "deriving")
  651. (haskell-indentation-push-indentation starter-indent-inside)
  652. (haskell-indentation-add-left-indent)))
  653. ((equal current-token "deriving")
  654. (haskell-indentation-with-starter
  655. #'haskell-indentation-type-1)))))
  656. ((string= current-token "where")
  657. (haskell-indentation-with-starter
  658. #'haskell-indentation-expression-layout nil)
  659. (cond
  660. ((equal current-token 'end-tokens)
  661. (when (string= following-token "deriving")
  662. (haskell-indentation-add-left-indent)))
  663. ((equal current-token "deriving")
  664. (haskell-indentation-with-starter
  665. #'haskell-indentation-type-1))))))
  666. (defun haskell-indentation-import ()
  667. "Parse import declaration."
  668. (haskell-indentation-with-starter #'haskell-indentation-expression))
  669. (defun haskell-indentation-foreign ()
  670. "Parse foreign import declaration."
  671. (haskell-indentation-with-starter (apply-partially #'haskell-indentation-expression '(value operator "import"))))
  672. (defun haskell-indentation-class-declaration ()
  673. "Parse class declaration."
  674. (haskell-indentation-with-starter
  675. (lambda ()
  676. (haskell-indentation-type)
  677. (when (string= current-token "|")
  678. (haskell-indentation-fundep))
  679. (when (string= current-token "where")
  680. (haskell-indentation-with-starter
  681. #'haskell-indentation-declaration-layout nil)))))
  682. (defun haskell-indentation-deriving ()
  683. "Parse standalone declaration."
  684. (haskell-indentation-with-starter
  685. (lambda ()
  686. (when (string= "instance" current-token)
  687. (haskell-indentation-read-next-token))
  688. (when (equal current-token 'end-tokens)
  689. (haskell-indentation-add-left-indent)
  690. (throw 'parse-end nil))
  691. (haskell-indentation-type)
  692. (when (string= current-token "|")
  693. (haskell-indentation-fundep)))))
  694. (defun haskell-indentation-module ()
  695. "Parse module declaration."
  696. (haskell-indentation-with-starter
  697. (lambda ()
  698. (haskell-indentation-read-next-token)
  699. (when (equal current-token 'layout-item)
  700. (haskell-indentation-read-next-token))
  701. (when (string= current-token "(")
  702. (haskell-indentation-list
  703. #'haskell-indentation-module-export
  704. ")" ","))
  705. (if (string= current-token "where")
  706. (haskell-indentation-read-next-token)
  707. (when (eq current-token 'end-tokens)
  708. (when (member following-token '(value no-following-token "("))
  709. (haskell-indentation-add-indentation
  710. (+ starter-indent haskell-indentation-starter-offset))
  711. (haskell-indentation-add-indentation
  712. (+ left-indent haskell-indentation-starter-offset))
  713. (throw 'parse-end nil))
  714. (haskell-indentation-add-layout-indent)
  715. (throw 'parse-end nil))))))
  716. (defun haskell-indentation-toplevel-where ()
  717. "Parse 'where' that we may hit as a standalone in module declaration."
  718. (haskell-indentation-read-next-token)
  719. (when (eq current-token 'end-tokens)
  720. (haskell-indentation-add-layout-indent)
  721. (throw 'parse-end nil)))
  722. (defun haskell-indentation-module-export ()
  723. "Parse export list."
  724. (cond ((string= current-token "module")
  725. (let ((current-indent (current-column)))
  726. (haskell-indentation-read-next-token)
  727. (cond ((eq current-token 'end-tokens)
  728. (haskell-indentation-add-indentation current-indent))
  729. ((eq current-token 'value)
  730. (haskell-indentation-read-next-token)))))
  731. (t (haskell-indentation-type))))
  732. (defun haskell-indentation-list (parser end sep &optional stmt-sep)
  733. "Parse a list, pair or other expression containing multiple
  734. items parsed by PARSER, separated by SEP or STMT-SEP, and ending
  735. with END."
  736. ;; note that we use macro expansion here to preserver Emacs 23
  737. ;; compatibility and its lack of lexical binding
  738. (haskell-indentation-with-starter
  739. `(lambda ()
  740. (let ((implicit-layout-active nil))
  741. (haskell-indentation-separated
  742. #',parser ,sep ,stmt-sep)))
  743. end))
  744. (defun haskell-indentation-with-starter (parser &optional end where-expr?)
  745. "Parse an expression starting with a keyword or parenthesis.
  746. Skip the keyword or parenthesis." ; FIXME: better description needed
  747. (let ((starter-column (current-column))
  748. (current-indent current-indent)
  749. (left-indent
  750. (if (= (current-column) (haskell-indentation-current-indentation))
  751. (current-column)
  752. left-indent)))
  753. (haskell-indentation-read-next-token)
  754. (when (eq current-token 'end-tokens)
  755. (cond ((equal following-token end)
  756. ;; indent before keyword or parenthesis
  757. (haskell-indentation-add-indentation starter-column))
  758. (where-expr?
  759. ;; left indent + where post indent
  760. (haskell-indentation-add-where-post-indent left-indent))
  761. (t
  762. (haskell-indentation-add-left-indent)))
  763. (throw 'parse-end nil))
  764. (let* ((current-indent (current-column))
  765. (starter-indent (min starter-column current-indent))
  766. (left-indent
  767. (if end
  768. (+ starter-indent haskell-indentation-starter-offset)
  769. left-indent)))
  770. (funcall parser)
  771. (cond ((eq current-token 'end-tokens)
  772. (when (equal following-token end)
  773. ;; indent before keyword or parenthesis
  774. (haskell-indentation-add-indentation starter-indent))
  775. ;; add no more indentations if we expect a closing keyword
  776. (when end
  777. (throw 'parse-end nil)))
  778. ((equal current-token end)
  779. (haskell-indentation-read-next-token))))))
  780. (defun haskell-indentation-case-alternative ()
  781. "" ; FIXME
  782. (setq left-indent (current-column))
  783. (haskell-indentation-separated #'haskell-indentation-expression "," nil)
  784. (cond ((eq current-token 'end-tokens)
  785. (haskell-indentation-add-indentation current-indent))
  786. ((string= current-token "->")
  787. (haskell-indentation-statement-right #'haskell-indentation-expression))
  788. ;; otherwise fallthrough
  789. ))
  790. (defun haskell-indentation-case ()
  791. "" ; FIXME
  792. (haskell-indentation-expression)
  793. (cond ((eq current-token 'end-tokens)
  794. (haskell-indentation-add-indentation current-indent))
  795. ((string= current-token "|")
  796. (haskell-indentation-with-starter
  797. (apply-partially #'haskell-indentation-separated
  798. #'haskell-indentation-case-alternative "|" nil)
  799. nil))
  800. ((string= current-token "->")
  801. (haskell-indentation-statement-right #'haskell-indentation-expression))
  802. ;; otherwise fallthrough
  803. ))
  804. (defun haskell-indentation-statement-right (parser)
  805. "Process right side of a statement.
  806. Set `current-indent' to the current column and calls the given
  807. parser. If parsing ends here, set indentation to left-indent."
  808. (haskell-indentation-read-next-token)
  809. (when (eq current-token 'end-tokens)
  810. (haskell-indentation-add-left-indent)
  811. (haskell-indentation-add-indentation current-indent)
  812. (throw 'parse-end nil))
  813. (funcall parser)
  814. (when (equal current-token "where")
  815. (haskell-indentation-with-starter
  816. #'haskell-indentation-expression-layout nil)))
  817. (defun haskell-indentation-guard ()
  818. "Parse \"guard\" statement."
  819. (setq left-indent (current-column))
  820. (haskell-indentation-separated
  821. #'haskell-indentation-expression "," nil))
  822. (defun haskell-indentation-declaration ()
  823. "Parse function or type declaration."
  824. (haskell-indentation-separated #'haskell-indentation-expression "," nil)
  825. (when (string= current-token "|")
  826. (haskell-indentation-with-starter
  827. (apply-partially #'haskell-indentation-separated
  828. #'haskell-indentation-guard "|" nil)
  829. nil))
  830. (when (eq current-token 'end-tokens)
  831. (when (member following-token '("|" "=" "::" ","))
  832. (haskell-indentation-add-indentation current-indent)
  833. (throw 'parse-end nil))))
  834. (defun haskell-indentation-layout (parser)
  835. "Parse layout list, where each layout item is parsed by parser."
  836. (if (string= current-token "{")
  837. (haskell-indentation-list parser "}" ";") ; explicit layout
  838. (haskell-indentation-implicit-layout-list parser)))
  839. (defun haskell-indentation-expression-token-p (token)
  840. "Return non-NIL value if TOKEN is an expression token."
  841. (member token
  842. '("if" "let" "do" "case" "\\" "(" "{" "[" "::"
  843. value operator no-following-token)))
  844. (defun haskell-indentation-expression (&optional accepted-tokens)
  845. "Parse an expression until an unknown token is encountered."
  846. (catch 'return
  847. (let ((current-indent (current-column)))
  848. (unless accepted-tokens
  849. (setq accepted-tokens '(value operator)))
  850. (while t
  851. (cond
  852. ((memq current-token accepted-tokens)
  853. (haskell-indentation-read-next-token))
  854. ((eq current-token 'end-tokens)
  855. (cond ((string= following-token "where")
  856. (haskell-indentation-add-where-pre-indent)) ; before a where
  857. ((haskell-indentation-expression-token-p following-token)
  858. ;; a normal expression can be either continued or have
  859. ;; left indent
  860. (haskell-indentation-add-indentation
  861. current-indent)
  862. (haskell-indentation-add-indentation
  863. left-indent)))
  864. (throw 'return nil))
  865. (t (let ((parser (assoc current-token
  866. haskell-indentation-expression-list)))
  867. (when (null parser)
  868. (throw 'return nil)) ; not expression token, so exit
  869. (funcall (cdr parser)) ; run parser
  870. ;; after an 'open' expression such as 'if', exit
  871. (unless (member (car parser) '("(" "[" "{" "case"))
  872. (throw 'return nil)))))))))
  873. (defun haskell-indentation-separated (parser separator &optional stmt-separator)
  874. "Evaluate PARSER separated by SEPARATOR and STMT-SEPARATOR.
  875. If STMT-SEPARATOR is not NIL, it will be used to set a new starter-indent.
  876. For example:
  877. [ i | i <- [1..10]
  878. ,"
  879. (catch 'return
  880. (unless (listp separator)
  881. (setq separator (list separator)))
  882. (unless (listp stmt-separator)
  883. (setq stmt-separator (list stmt-separator)))
  884. (while t
  885. (funcall parser)
  886. (cond ((member current-token separator)
  887. (haskell-indentation-at-separator))
  888. ((member current-token stmt-separator)
  889. (setq starter-indent (current-column))
  890. (haskell-indentation-at-separator))
  891. ((eq current-token 'end-tokens)
  892. (when (or (member following-token separator)
  893. (member following-token stmt-separator))
  894. ;; Set an indentation before a separator, for example:
  895. ;; [ 1 or [ 1 | a
  896. ;; , 2 , 20
  897. (haskell-indentation-add-indentation starter-indent)
  898. (when (< left-indent starter-indent)
  899. (haskell-indentation-add-indentation left-indent))
  900. (throw 'parse-end nil))
  901. (when (equal following-token 'no-following-token)
  902. ;; Set an indentation before a separator, for example:
  903. ;; [ 1 or [ 1 | a
  904. ;; , 2 , 20
  905. (haskell-indentation-add-indentation starter-indent)
  906. (haskell-indentation-add-indentation left-indent))
  907. (throw 'return nil))
  908. (t (throw 'return nil))))))
  909. (defun haskell-indentation-at-separator ()
  910. "At a separator.
  911. If at a new line, set starter-indent at the separator
  912. and current-indent after the separator, for example:
  913. l = [ 1
  914. , 2
  915. , -- start now here."
  916. (let ((separator-column
  917. (and (= (current-column) (haskell-indentation-current-indentation))
  918. (current-column))))
  919. (haskell-indentation-read-next-token)
  920. (cond ((eq current-token 'end-tokens)
  921. (haskell-indentation-add-indentation current-indent)
  922. (haskell-indentation-add-indentation left-indent)
  923. (throw 'return nil))
  924. (separator-column ; on the beginning of the line
  925. (setq current-indent (current-column))
  926. (setq starter-indent separator-column)
  927. (setq left-indent
  928. (+ starter-indent haskell-indentation-starter-offset))))))
  929. (defun haskell-indentation-implicit-layout-list (parser)
  930. "An implicit layout list, elements are parsed with PARSER.
  931. This sets the `layout-indent' variable to the column where the
  932. layout starts."
  933. (let* ((layout-indent (current-column))
  934. (current-indent (current-column))
  935. (left-indent (current-column))
  936. (implicit-layout-active t))
  937. (catch 'return
  938. (while t
  939. (let ((left-indent left-indent))
  940. (funcall parser))
  941. (cond ((member current-token '(layout-item ";"))
  942. (haskell-indentation-read-next-token))
  943. ((eq current-token 'end-tokens)
  944. (when (or (and
  945. (not (member following-token '("{" operator)))
  946. (not (member previous-token '(operator)))
  947. (haskell-indentation-expression-token-p following-token))
  948. (string= following-token ";")
  949. (and (equal layout-indent 0)
  950. (member following-token (mapcar #'car haskell-indentation-toplevel-list))
  951. (not (string= following-token "where"))))
  952. (haskell-indentation-add-layout-indent))
  953. (throw 'return nil))
  954. (t (throw 'return nil))))))
  955. ;; put `haskell-indentation-read-next-token' outside the current-indent
  956. ;; definition so it will not return 'layout-end again
  957. (when (eq current-token 'layout-end)
  958. (let ((implicit-layout-active t))
  959. ;; leave layout at 'layout-end or illegal token
  960. (haskell-indentation-read-next-token))))
  961. (defun haskell-indentation-if ()
  962. "" ; FIXME
  963. (haskell-indentation-with-starter
  964. (lambda ()
  965. (if (string= current-token "|")
  966. (haskell-indentation-with-starter
  967. (lambda ()
  968. (haskell-indentation-separated
  969. #'haskell-indentation-case-alternative "|" nil))
  970. nil)
  971. (haskell-indentation-phrase-rest
  972. '(haskell-indentation-expression
  973. "then" haskell-indentation-expression
  974. "else" haskell-indentation-expression))))
  975. nil))
  976. (defun haskell-indentation-phrase (phrase)
  977. "" ; FIXME
  978. (haskell-indentation-with-starter
  979. (apply-partially #'haskell-indentation-phrase-rest phrase)
  980. nil))
  981. (defun haskell-indentation-phrase-rest (phrase1)
  982. "" ; FIXME
  983. (while phrase1
  984. (let ((phrase phrase1))
  985. (setq phrase1 nil)
  986. (let ((current-indent (current-column))
  987. (left-indent left-indent)
  988. (layout-indent layout-indent))
  989. (funcall (car phrase)))
  990. (cond
  991. ((eq current-token 'end-tokens)
  992. (cond ((null (cdr phrase))) ;; fallthrough
  993. ((equal following-token (cadr phrase))
  994. (haskell-indentation-add-indentation starter-indent)
  995. (unless (member following-token '("," ";"))
  996. ;; we want to keep comma and semicolon aligned always
  997. (haskell-indentation-add-indentation left-indent))
  998. (throw 'parse-end nil))
  999. ((string= (cadr phrase) "in")
  1000. (when (= left-indent layout-indent)
  1001. (haskell-indentation-add-layout-indent)
  1002. (throw 'parse-end nil)))
  1003. (t (throw 'parse-end nil))))
  1004. ((null (cdr phrase)))
  1005. ((equal (cadr phrase) current-token)
  1006. (haskell-indentation-read-next-token)
  1007. (when (eq current-token 'end-tokens)
  1008. (haskell-indentation-add-indentation
  1009. (+ starter-indent haskell-indentation-starter-offset))
  1010. (haskell-indentation-add-indentation
  1011. (+ left-indent haskell-indentation-starter-offset))
  1012. (throw 'parse-end nil))
  1013. (setq phrase1 (cddr phrase)))
  1014. ((string= (cadr phrase) "in"))))))
  1015. (defun haskell-indentation-add-indentation (indent)
  1016. "" ; FIXME
  1017. (haskell-indentation-push-indentation
  1018. (if (<= indent layout-indent)
  1019. (+ layout-indent haskell-indentation-layout-offset)
  1020. indent)))
  1021. (defun haskell-indentation-add-layout-indent ()
  1022. "" ; FIXME
  1023. (haskell-indentation-push-indentation layout-indent))
  1024. (defun haskell-indentation-add-where-pre-indent ()
  1025. "" ; FIXME
  1026. (haskell-indentation-push-indentation
  1027. (+ layout-indent haskell-indentation-where-pre-offset))
  1028. (if (= layout-indent haskell-indentation-layout-offset)
  1029. (haskell-indentation-push-indentation
  1030. haskell-indentation-where-pre-offset)))
  1031. (defun haskell-indentation-add-where-post-indent (indent)
  1032. "" ; FIXME
  1033. (haskell-indentation-push-indentation
  1034. (+ indent haskell-indentation-where-post-offset)))
  1035. (defun haskell-indentation-add-left-indent ()
  1036. "" ; FIXME
  1037. (haskell-indentation-add-indentation
  1038. (+ left-indent haskell-indentation-left-offset)))
  1039. (defun haskell-indentation-push-indentation (indent)
  1040. "Add INDENT to list of possible indentations.
  1041. Add INDENT to `possible-indentations' if it is not there
  1042. yet. Keep the list in ascending order."
  1043. (unless (member indent possible-indentations)
  1044. (setq possible-indentations
  1045. (sort (cons indent possible-indentations) #'<))))
  1046. (defun haskell-indentation-read-next-token ()
  1047. "Go to the next token and set current-token to the next token.
  1048. The following symbols are used as pseudo tokens:
  1049. 'layout-item: A new item in a layout list. The next token
  1050. will be the first token from the item.
  1051. 'layout-end: the end of a layout list. Next token will be
  1052. the first token after the layout list.
  1053. 'end-tokens: back at point where we started, following-token
  1054. will be set to the next token.
  1055. Pseudo tokens are used only when implicit-layout-active is
  1056. t. That is the case only after keywords \"do\", \"where\",
  1057. \"let\" and \"of\".
  1058. If we are at a new line, parse-line is increased, and
  1059. current-indent and left-indent are set to the indentation of the
  1060. line."
  1061. (cond ((and implicit-layout-active
  1062. (eq current-token 'end-tokens))
  1063. 'end-tokens)
  1064. ((and implicit-layout-active
  1065. (eq current-token 'layout-end))
  1066. (cond ((> layout-indent (current-column))
  1067. 'layout-end)
  1068. ((= layout-indent (current-column))
  1069. (setq current-token 'layout-item))
  1070. ((< layout-indent (current-column))
  1071. (setq current-token (haskell-indentation-peek-token)))))
  1072. ((and implicit-layout-active
  1073. (eq current-token 'layout-item))
  1074. (setq current-token (haskell-indentation-peek-token)))
  1075. ((and implicit-layout-active
  1076. (> layout-indent (current-column)))
  1077. (setq current-token 'layout-end))
  1078. (t
  1079. (setq previous-token (haskell-indentation-peek-token))
  1080. (haskell-indentation-skip-token)
  1081. (if (>= (point) indentation-point)
  1082. (progn
  1083. (setq following-token
  1084. (if (and (not (eobp))
  1085. (= (point) indentation-point))
  1086. (haskell-indentation-peek-token)
  1087. 'no-following-token))
  1088. (setq current-token 'end-tokens))
  1089. (when (= (current-column) (haskell-indentation-current-indentation))
  1090. ;; on a new line
  1091. (setq current-indent (current-column)))
  1092. (cond ((and implicit-layout-active
  1093. (> layout-indent (current-column)))
  1094. (setq current-token 'layout-end))
  1095. ((and implicit-layout-active
  1096. (= layout-indent (current-column)))
  1097. (setq current-token 'layout-item))
  1098. (t (setq current-token (haskell-indentation-peek-token))))))))
  1099. (defun haskell-indentation-peek-token ()
  1100. "Return token starting at point."
  1101. (cond ((looking-at "\\(if\\|then\\|else\\|let\\|in\\|mdo\\|rec\\|do\\|proc\\|case\\|of\\|where\\|module\\|signature\\|deriving\\|import\\|data\\|type\\|newtype\\|class\\|instance\\)\\([^[:alnum:]'_]\\|$\\)")
  1102. (match-string-no-properties 1))
  1103. ((looking-at "[][(){}[,;]")
  1104. (match-string-no-properties 0))
  1105. ((looking-at "\\(\\\\\\|->\\|<-\\|::\\|=\\||\\|=>\\)\\([^-:!#$%&*+./<=>?@\\\\^|~]\\|$\\)")
  1106. (match-string-no-properties 1))
  1107. ((looking-at "\\(→\\|←\\|∷\\|⇒\\)\\([^-:!#$%&*+./<=>?@\\\\^|~]\\|$\\)")
  1108. (let ((tok (match-string-no-properties 1)))
  1109. (or (cdr (assoc tok haskell-indentation-unicode-tokens)) tok)))
  1110. ((looking-at"[-:!#$%&*+./<=>?@\\\\^|~`]" )
  1111. 'operator)
  1112. (t 'value)))
  1113. (defun haskell-indentation-skip-token ()
  1114. "Skip to the next token."
  1115. (if (haskell-lexeme-looking-at-token)
  1116. (goto-char (match-end 0))
  1117. ;; otherwise skip until space found
  1118. (skip-syntax-forward "^-"))
  1119. ;; we have to skip unterminated string fence at the end of line
  1120. (skip-chars-forward "\n")
  1121. (forward-comment (buffer-size))
  1122. (while (and (haskell-indentation-bird-p)
  1123. (bolp)
  1124. (eq (char-after) ?>))
  1125. (forward-char)
  1126. (forward-comment (buffer-size))))
  1127. (provide 'haskell-indentation)
  1128. ;; Local Variables:
  1129. ;; tab-width: 8
  1130. ;; End:
  1131. ;;; haskell-indentation.el ends here