Klimi's new dotfiles with stow.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1214 рядки
53 KiB

5 роки тому
  1. ;;; haskell-mode.el --- A Haskell editing mode -*- coding: utf-8; lexical-binding: t -*-
  2. ;; Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2016
  3. ;; Free Software Foundation, Inc
  4. ;; Copyright © 1992, 1997-1998 Simon Marlow, Graeme E Moss, and Tommy Thorn
  5. ;; Author: 1992 Simon Marlow
  6. ;; 1997-1998 Graeme E Moss <gem@cs.york.ac.uk> and
  7. ;; Tommy Thorn <thorn@irisa.fr>,
  8. ;; 2001-2002 Reuben Thomas (>=v1.4)
  9. ;; 2003 Dave Love <fx@gnu.org>
  10. ;; 2016 Arthur Fayzrakhmanov
  11. ;; Keywords: faces files Haskell
  12. ;; Version: 16.2-git
  13. ;; URL: https://github.com/haskell/haskell-mode
  14. ;; This file is not part of GNU Emacs.
  15. ;; This file is free software; you can redistribute it and/or modify
  16. ;; it under the terms of the GNU General Public License as published by
  17. ;; the Free Software Foundation; either version 3, or (at your option)
  18. ;; any later version.
  19. ;; This file is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;; GNU General Public License for more details.
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. ;;; Commentary:
  26. ;; A major mode for editing Haskell (the functional programming
  27. ;; language, see URL `http://www.haskell.org') in Emacs.
  28. ;;
  29. ;; Some of its major features include:
  30. ;;
  31. ;; - syntax highlighting (font lock),
  32. ;;
  33. ;; - automatic indentation,
  34. ;;
  35. ;; - on-the-fly documentation,
  36. ;;
  37. ;; - interaction with inferior GHCi/Hugs instance,
  38. ;;
  39. ;; - scans declarations and places them in a menu.
  40. ;;
  41. ;; See URL `https://github.com/haskell/haskell-mode' and/or
  42. ;; Info node `(haskell-mode)Introduction' for more information.
  43. ;;
  44. ;; Use `M-x haskell-mode-view-news` (after Haskell Mode is installed)
  45. ;; to show information on recent changes in Haskell Mode.
  46. ;;; Change Log:
  47. ;; This mode is based on an editing mode by Simon Marlow 11/1/92
  48. ;; and heavily modified by Graeme E Moss and Tommy Thorn 7/11/98.
  49. ;;
  50. ;; Version 1.5:
  51. ;; Added autoload for haskell-indentation
  52. ;;
  53. ;; Version 1.43:
  54. ;; Various tweaks to doc strings and customization support from
  55. ;; Ville Skyttä <scop@xemacs.org>.
  56. ;;
  57. ;; Version 1.42:
  58. ;; Added autoload for GHCi inferior mode (thanks to Scott
  59. ;; Williams for the bug report and fix).
  60. ;;
  61. ;; Version 1.41:
  62. ;; Improved packaging, and made a couple more variables
  63. ;; interactively settable.
  64. ;;
  65. ;; Version 1.4:
  66. ;; Added GHCi mode from Chris Webb, and tidied up a little.
  67. ;;
  68. ;; Version 1.3:
  69. ;; The literate or non-literate style of a buffer is now indicated
  70. ;; by just the variable haskell-literate: nil, `bird', or `tex'.
  71. ;; For literate buffers with ambiguous style, the value of
  72. ;; haskell-literate-default is used.
  73. ;;
  74. ;; Version 1.2:
  75. ;; Separated off font locking, declaration scanning and simple
  76. ;; indentation, and made them separate modules. Modules can be
  77. ;; added easily now. Support for modules haskell-doc,
  78. ;; haskell-indent, and haskell-hugs. Literate and non-literate
  79. ;; modes integrated into one mode, and literate buffer indicated by
  80. ;; value of haskell-literate(-bird-style).
  81. ;;
  82. ;; Version 1.1:
  83. ;; Added support for declaration scanning under XEmacs via
  84. ;; func-menu. Moved operators to level two fontification.
  85. ;;
  86. ;; Version 1.0:
  87. ;; Added a nice indention support from Heribert Schuetz
  88. ;; <Heribert.Schuetz@informatik.uni-muenchen.de>:
  89. ;;
  90. ;; I have just hacked an Emacs Lisp function which you might prefer
  91. ;; to `indent-relative' in haskell-mode.el. See below. It is not
  92. ;; really Haskell-specific because it does not take into account
  93. ;; keywords like `do', `of', and `let' (where the layout rule
  94. ;; applies), but I already find it useful.
  95. ;;
  96. ;; Cleaned up the imenu support. Added support for literate scripts.
  97. ;;
  98. ;; Version 0.103 [HWL]:
  99. ;; From Hans Wolfgang Loidl <hwloidl@dcs.gla.ac.uk>:
  100. ;;
  101. ;; I (HWL) added imenu support by copying the appropriate functions
  102. ;; from hugs-mode. A menu-bar item "Declarations" is now added in
  103. ;; haskell mode. The new code, however, needs some clean-up.
  104. ;;
  105. ;; Version 0.102:
  106. ;;
  107. ;; Moved C-c C-c key binding to comment-region. Leave M-g M-g to do
  108. ;; the work. comment-start-skip is changed to comply with comment-start.
  109. ;;
  110. ;; Version 0.101:
  111. ;;
  112. ;; Altered indent-line-function to indent-relative.
  113. ;;
  114. ;; Version 0.100:
  115. ;;
  116. ;; First official release.
  117. ;;; Code:
  118. (require 'haskell-customize)
  119. (require 'ansi-color)
  120. (require 'dabbrev)
  121. (require 'compile)
  122. (require 'etags)
  123. (require 'flymake)
  124. (require 'outline)
  125. (require 'cl-lib)
  126. (require 'haskell-ghc-support)
  127. (require 'haskell-complete-module)
  128. (require 'haskell-compat)
  129. (require 'haskell-align-imports)
  130. (require 'haskell-lexeme)
  131. (require 'haskell-sort-imports)
  132. (require 'haskell-string)
  133. (require 'haskell-indentation)
  134. (require 'haskell-font-lock)
  135. (require 'haskell-cabal)
  136. ;; All functions/variables start with `(literate-)haskell-'.
  137. ;; Version of mode.
  138. (defconst haskell-version "16.2-git"
  139. "The release version of `haskell-mode'.")
  140. ;;;###autoload
  141. (defun haskell-version (&optional here)
  142. "Show the `haskell-mode` version in the echo area.
  143. With prefix argument HERE, insert it at point."
  144. (interactive "P")
  145. (let* ((haskell-mode-dir (ignore-errors
  146. (file-name-directory (or (locate-library "haskell-mode") ""))))
  147. (version (format "haskell-mode version %s (%s)"
  148. haskell-version
  149. haskell-mode-dir)))
  150. (if here
  151. (insert version)
  152. (message "%s" version))))
  153. ;;;###autoload
  154. (defun haskell-mode-view-news ()
  155. "Display information on recent changes to haskell-mode."
  156. (interactive)
  157. (with-current-buffer (find-file-read-only (expand-file-name "NEWS" haskell-mode-pkg-base-dir))
  158. (goto-char (point-min))
  159. (outline-hide-sublevels 1)
  160. (outline-next-visible-heading 1)
  161. (outline-show-subtree)))
  162. ;; Are we looking at a literate script?
  163. (defvar-local haskell-literate nil
  164. "If not nil, the current buffer contains a literate Haskell script.
  165. Possible values are: `bird' and `tex', for Bird-style and LaTeX-style
  166. literate scripts respectively. Set by `haskell-mode' and
  167. `literate-haskell-mode'. For an ambiguous literate buffer -- i.e. does
  168. not contain either \"\\begin{code}\" or \"\\end{code}\" on a line on
  169. its own, nor does it contain \">\" at the start of a line -- the value
  170. of `haskell-literate-default' is used.")
  171. (put 'haskell-literate 'safe-local-variable 'symbolp)
  172. ;; Default literate style for ambiguous literate buffers.
  173. (defcustom haskell-literate-default 'bird
  174. "Default value for `haskell-literate'.
  175. Used if the style of a literate buffer is ambiguous. This variable should
  176. be set to the preferred literate style."
  177. :group 'haskell
  178. :type '(choice (const bird) (const tex) (const nil)))
  179. (defvar haskell-mode-map
  180. (let ((map (make-sparse-keymap)))
  181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  182. ;; Editing-specific commands
  183. (define-key map (kbd "C-c C-,") 'haskell-mode-format-imports)
  184. (define-key map [remap delete-indentation] 'haskell-delete-indentation)
  185. (define-key map (kbd "C-c C-l") 'haskell-mode-enable-process-minor-mode)
  186. (define-key map (kbd "C-c C-b") 'haskell-mode-enable-process-minor-mode)
  187. (define-key map (kbd "C-c C-v") 'haskell-mode-enable-process-minor-mode)
  188. (define-key map (kbd "C-c C-t") 'haskell-mode-enable-process-minor-mode)
  189. (define-key map (kbd "C-c C-i") 'haskell-mode-enable-process-minor-mode)
  190. (define-key map (kbd "C-c C-s") 'haskell-mode-toggle-scc-at-point)
  191. map)
  192. "Keymap used in `haskell-mode'.")
  193. (defun haskell-mode-enable-process-minor-mode ()
  194. "Tell the user to choose a minor mode for process interaction."
  195. (interactive)
  196. (error "Run `C-h f haskell-mode` for instruction how to setup a Haskell interaction mode."))
  197. (easy-menu-define haskell-mode-menu haskell-mode-map
  198. "Menu for the Haskell major mode."
  199. ;; Suggestions from Pupeno <pupeno@pupeno.com>:
  200. ;; - choose the underlying interpreter
  201. ;; - look up docs
  202. `("Haskell"
  203. ["Indent line" indent-according-to-mode]
  204. ["(Un)Comment region" comment-region mark-active]
  205. "---"
  206. ["Start interpreter" haskell-interactive-switch]
  207. ["Load file" haskell-process-load-file]
  208. "---"
  209. ["Load tidy core" ghc-core-create-core]
  210. "---"
  211. ,(if (default-boundp 'eldoc-documentation-function)
  212. ["Doc mode" eldoc-mode
  213. :style toggle :selected (bound-and-true-p eldoc-mode)]
  214. ["Doc mode" haskell-doc-mode
  215. :style toggle :selected (and (boundp 'haskell-doc-mode) haskell-doc-mode)])
  216. ["Customize" (customize-group 'haskell)]
  217. ))
  218. ;; Procedurally generated (see Lexeme.hs in ghc).
  219. ;; This is a bit unsightly: it's generated by making a list of all
  220. ;; unicode characters whose Unicode general category ghc would
  221. ;; recognize as valid symbol (or identifier, below) constituent.
  222. (defvar haskell--char-syntax-symbols
  223. '((161 . 169) 172 (174 . 177) 180 (182 . 184) 191 215 247
  224. (706 . 709) (722 . 735) (741 . 747) 749 (751 . 767) 885
  225. 894 (900 . 901) 903 1014 1154 (1370 . 1375) (1417 . 1418)
  226. (1421 . 1423) 1470 1472 1475 1478 (1523 . 1524) (1542 . 1551)
  227. 1563 (1566 . 1567) (1642 . 1645) 1748 1758 1769 (1789 . 1790)
  228. (1792 . 1805) (2038 . 2041) (2096 . 2110) 2142 (2404 . 2405)
  229. 2416 (2546 . 2547) (2554 . 2555) (2800 . 2801) 2928
  230. (3059 . 3066) 3199 3449 3572 3647 3663 (3674 . 3675)
  231. (3841 . 3863) (3866 . 3871) 3892 3894 3896 3973 (4030 . 4037)
  232. (4039 . 4044) (4046 . 4058) (4170 . 4175) (4254 . 4255)
  233. 4347 (4960 . 4968) (5008 . 5017) 5120 (5741 . 5742)
  234. (5867 . 5869) (5941 . 5942) (6100 . 6102) (6104 . 6107)
  235. (6144 . 6154) 6464 (6468 . 6469) (6622 . 6623) (6624 . 6655)
  236. (6686 . 6687) (6816 . 6822) (6824 . 6829) (7002 . 7018)
  237. (7028 . 7036) (7164 . 7167) (7227 . 7231) (7294 . 7295)
  238. (7360 . 7367) 7379 8125 (8127 . 8129) (8141 . 8143)
  239. (8157 . 8159) (8173 . 8175) (8189 . 8190) (8208 . 8215)
  240. (8224 . 8231) (8240 . 8248) (8251 . 8260) (8263 . 8286)
  241. (8314 . 8316) (8330 . 8332) (8352 . 8381) (8448 . 8449)
  242. (8451 . 8454) (8456 . 8457) 8468 (8470 . 8472) (8478 . 8483)
  243. 8485 8487 8489 8494 (8506 . 8507) (8512 . 8516) (8522 . 8525)
  244. 8527 (8592 . 8703) (8704 . 8959) (8960 . 8967) (8972 . 9000)
  245. (9003 . 9210) (9216 . 9254) (9280 . 9290) (9372 . 9449)
  246. (9472 . 9599) (9600 . 9631) (9632 . 9727) (9728 . 9983)
  247. (9984 . 10087) (10132 . 10175) (10176 . 10180) (10183 . 10213)
  248. (10224 . 10239) (10240 . 10495) (10496 . 10623) (10624 . 10626)
  249. (10649 . 10711) (10716 . 10747) (10750 . 10751) (10752 . 11007)
  250. (11008 . 11123) (11126 . 11157) (11160 . 11193) (11197 . 11208)
  251. (11210 . 11217) (11493 . 11498) (11513 . 11516) (11518 . 11519)
  252. 11632 (11776 . 11777) (11782 . 11784) 11787 (11790 . 11803)
  253. (11806 . 11807) (11818 . 11822) (11824 . 11841) (11904 . 11929)
  254. (11931 . 12019) (12032 . 12245) (12272 . 12283) (12289 . 12292)
  255. (12306 . 12307) 12316 12320 12336 (12342 . 12343)
  256. (12349 . 12351) (12443 . 12444) 12448 12539 (12688 . 12689)
  257. (12694 . 12703) (12736 . 12771) (12800 . 12830) (12842 . 12871)
  258. 12880 (12896 . 12927) (12938 . 12976) (12992 . 13054)
  259. (13056 . 13311) (19904 . 19967) (42128 . 42182) (42238 . 42239)
  260. (42509 . 42511) 42611 42622 (42738 . 42743) (42752 . 42774)
  261. (42784 . 42785) (42889 . 42890) (43048 . 43051) (43062 . 43065)
  262. (43124 . 43127) (43214 . 43215) (43256 . 43258) (43310 . 43311)
  263. 43359 (43457 . 43469) (43486 . 43487) (43612 . 43615)
  264. (43639 . 43641) (43742 . 43743) (43760 . 43761) 43867
  265. 44011 64297 (64434 . 64449) (65020 . 65021) (65040 . 65046)
  266. 65049 (65072 . 65076) (65093 . 65094) (65097 . 65103)
  267. (65104 . 65106) (65108 . 65112) (65119 . 65126) (65128 . 65131)
  268. (65281 . 65287) (65290 . 65295) (65306 . 65312) 65340
  269. (65342 . 65344) 65372 65374 65377 (65380 . 65381)
  270. (65504 . 65510) (65512 . 65518) (65532 . 65533) (65792 . 65794)
  271. (65847 . 65855) (65913 . 65929) 65932 (65936 . 65947)
  272. 65952 (66000 . 66044) 66463 66512 66927 67671 (67703 . 67704)
  273. 67871 67903 (68176 . 68184) 68223 68296 (68336 . 68342)
  274. (68409 . 68415) (68505 . 68508) (69703 . 69709) (69819 . 69820)
  275. (69822 . 69825) (69952 . 69955) (70004 . 70005) (70085 . 70088)
  276. 70093 (70200 . 70205) 70854 (71105 . 71113) (71233 . 71235)
  277. (74864 . 74868) (92782 . 92783) 92917 (92983 . 92991)
  278. (92996 . 92997) 113820 113823 (118784 . 119029) (119040 . 119078)
  279. (119081 . 119140) (119146 . 119148) (119171 . 119172)
  280. (119180 . 119209) (119214 . 119261) (119296 . 119361)
  281. 119365 (119552 . 119638) 120513 120539 120571 120597
  282. 120629 120655 120687 120713 120745 120771 (126704 . 126705)
  283. (126976 . 127019) (127024 . 127123) (127136 . 127150)
  284. (127153 . 127167) (127169 . 127183) (127185 . 127221)
  285. (127248 . 127278) (127280 . 127339) (127344 . 127386)
  286. (127462 . 127487) (127488 . 127490) (127504 . 127546)
  287. (127552 . 127560) (127568 . 127569) (127744 . 127788)
  288. (127792 . 127869) (127872 . 127950) (127956 . 127991)
  289. (128000 . 128254) (128256 . 128330) (128336 . 128377)
  290. (128379 . 128419) (128421 . 128511) (128512 . 128578)
  291. (128581 . 128591) (128592 . 128639) (128640 . 128719)
  292. (128736 . 128748) (128752 . 128755) (128768 . 128883)
  293. (128896 . 128980) (129024 . 129035) (129040 . 129095)
  294. (129104 . 129113) (129120 . 129159) (129168 . 129197)))
  295. (defvar haskell--char-syntax-identifiers
  296. '(170
  297. (178 . 179) 181 (185 . 186) (188 . 190) (192 . 214) (216 . 246)
  298. (248 . 255) (256 . 383) (384 . 591) (592 . 687) (880 . 883)
  299. (886 . 887) (891 . 893) 895 902 (904 . 906) 908 (910 . 929) (931 . 1013)
  300. (1015 . 1023) (1024 . 1153) (1162 . 1279) (1280 . 1327)
  301. (1329 . 1366) (1377 . 1415) (1488 . 1514) (1520 . 1522) (1568 . 1599)
  302. (1601 . 1610) (1632 . 1641) (1646 . 1647) (1649 . 1747) 1749
  303. (1774 . 1788) 1791 1808 (1810 . 1839) (1869 . 1871) (1872 . 1919)
  304. (1920 . 1957) 1969 (1984 . 2026) (2048 . 2069) (2112 . 2136) (2208 . 2226)
  305. (2308 . 2361) 2365 2384 (2392 . 2401) (2406 . 2415) (2418 . 2431)
  306. 2432 (2437 . 2444) (2447 . 2448) (2451 . 2472) (2474 . 2480)
  307. 2482 (2486 . 2489) 2493 2510 (2524 . 2525) (2527 . 2529) (2534 . 2545)
  308. (2548 . 2553) (2565 . 2570) (2575 . 2576) (2579 . 2600)
  309. (2602 . 2608) (2610 . 2611) (2613 . 2614) (2616 . 2617) (2649 . 2652)
  310. 2654 (2662 . 2671) (2674 . 2676) (2693 . 2701) (2703 . 2705)
  311. (2707 . 2728) (2730 . 2736) (2738 . 2739) (2741 . 2745) 2749 2768
  312. (2784 . 2785) (2790 . 2799) (2821 . 2828) (2831 . 2832) (2835 . 2856)
  313. (2858 . 2864) (2866 . 2867) (2869 . 2873) 2877 (2908 . 2909)
  314. (2911 . 2913) (2918 . 2927) (2929 . 2935) 2947 (2949 . 2954) (2958 . 2960)
  315. (2962 . 2965) (2969 . 2970) 2972 (2974 . 2975) (2979 . 2980)
  316. (2984 . 2986) (2990 . 3001) 3024 (3046 . 3058) (3077 . 3084) (3086 . 3088)
  317. (3090 . 3112) (3114 . 3129) 3133 (3160 . 3161) (3168 . 3169)
  318. (3174 . 3183) (3192 . 3198) (3205 . 3212) (3214 . 3216) (3218 . 3240)
  319. (3242 . 3251) (3253 . 3257) 3261 3294 (3296 . 3297) (3302 . 3311)
  320. (3313 . 3314) (3333 . 3340) (3342 . 3344) (3346 . 3386) 3389
  321. 3406 (3424 . 3425) (3430 . 3445) (3450 . 3455) (3461 . 3478) (3482 . 3505)
  322. (3507 . 3515) 3517 (3520 . 3526) (3558 . 3567) (3585 . 3632)
  323. (3634 . 3635) (3648 . 3653) (3664 . 3673) (3713 . 3714) 3716 (3719 . 3720)
  324. 3722 3725 (3732 . 3735) (3737 . 3743) (3745 . 3747) 3749
  325. 3751 (3754 . 3755) (3757 . 3760) (3762 . 3763) 3773 (3776 . 3780)
  326. (3792 . 3801) (3804 . 3807) 3840 (3872 . 3891) (3904 . 3911) (3913 . 3948)
  327. (3976 . 3980) (4096 . 4138) (4159 . 4169) (4176 . 4181)
  328. (4186 . 4189) 4193 (4197 . 4198) (4206 . 4208) (4213 . 4225) 4238
  329. (4240 . 4249) (4256 . 4293) 4295 4301 (4304 . 4346) (4349 . 4351)
  330. (4352 . 4607) (4608 . 4680) (4682 . 4685) (4688 . 4694) 4696 (4698 . 4701)
  331. (4704 . 4744) (4746 . 4749) (4752 . 4784) (4786 . 4789)
  332. (4792 . 4798) 4800 (4802 . 4805) (4808 . 4822) (4824 . 4880) (4882 . 4885)
  333. (4888 . 4954) (4969 . 4988) (4992 . 5007) (5024 . 5108)
  334. (5121 . 5740) (5743 . 5759) (5761 . 5786) (5792 . 5866) (5873 . 5880)
  335. (5888 . 5900) (5902 . 5905) (5920 . 5937) (5952 . 5969)
  336. (5984 . 5996) (5998 . 6000) (6016 . 6067) 6108 (6112 . 6121) (6128 . 6137)
  337. (6160 . 6169) (6176 . 6210) (6212 . 6263) (6272 . 6312) 6314
  338. (6320 . 6389) (6400 . 6430) (6470 . 6479) (6480 . 6509) (6512 . 6516)
  339. (6528 . 6571) (6593 . 6599) (6608 . 6618) (6656 . 6678)
  340. (6688 . 6740) (6784 . 6793) (6800 . 6809) (6917 . 6963) (6981 . 6987)
  341. (6992 . 7001) (7043 . 7072) (7086 . 7103) (7104 . 7141)
  342. (7168 . 7203) (7232 . 7241) (7245 . 7247) (7248 . 7287) (7401 . 7404)
  343. (7406 . 7409) (7413 . 7414) (7424 . 7467) (7531 . 7543)
  344. (7545 . 7551) (7552 . 7578) (7680 . 7935) (7936 . 7957) (7960 . 7965)
  345. (7968 . 8005) (8008 . 8013) (8016 . 8023) 8025 8027 8029
  346. (8031 . 8061) (8064 . 8116) (8118 . 8124) 8126 (8130 . 8132) (8134 . 8140)
  347. (8144 . 8147) (8150 . 8155) (8160 . 8172) (8178 . 8180)
  348. (8182 . 8188) 8304 (8308 . 8313) (8320 . 8329) 8450 8455 (8458 . 8467)
  349. 8469 (8473 . 8477) 8484 8486 8488 (8490 . 8493) (8495 . 8505)
  350. (8508 . 8511) (8517 . 8521) 8526 (8528 . 8543) (8579 . 8580)
  351. 8585 (9312 . 9371) (9450 . 9471) (10102 . 10131) (11264 . 11310)
  352. (11312 . 11358) (11360 . 11387) (11390 . 11391) (11392 . 11492)
  353. (11499 . 11502) (11506 . 11507) 11517 (11520 . 11557) 11559 11565
  354. (11568 . 11623) (11648 . 11670) (11680 . 11686) (11688 . 11694)
  355. (11696 . 11702) (11704 . 11710) (11712 . 11718) (11720 . 11726)
  356. (11728 . 11734) (11736 . 11742) 12294 12348 (12353 . 12438) 12447
  357. (12449 . 12538) 12543 (12549 . 12589) (12593 . 12686) (12690 . 12693)
  358. (12704 . 12730) (12784 . 12799) (12832 . 12841) (12872 . 12879)
  359. (12881 . 12895) (12928 . 12937) (12977 . 12991) (13312 . 19893)
  360. (19968 . 40908) (40960 . 40980) (40982 . 42124) (42192 . 42231)
  361. (42240 . 42507) (42512 . 42539) (42560 . 42606) (42624 . 42651)
  362. (42656 . 42725) (42786 . 42863) (42865 . 42887) (42891 . 42894)
  363. (42896 . 42925) (42928 . 42929) 42999 (43002 . 43007)
  364. (43008 . 43009) (43011 . 43013) (43015 . 43018) (43020 . 43042)
  365. (43056 . 43061) (43072 . 43123) (43138 . 43187) (43216 . 43225)
  366. (43250 . 43255) 43259 (43264 . 43301) (43312 . 43334) (43360 . 43388)
  367. (43396 . 43442) (43472 . 43481) (43488 . 43492) (43495 . 43518)
  368. (43520 . 43560) (43584 . 43586) (43588 . 43595) (43600 . 43609)
  369. (43616 . 43631) (43633 . 43638) 43642 (43646 . 43647)
  370. (43648 . 43695) 43697 (43701 . 43702) (43705 . 43709) 43712 43714
  371. (43739 . 43740) (43744 . 43754) 43762 (43777 . 43782) (43785 . 43790)
  372. (43793 . 43798) (43808 . 43814) (43816 . 43822) (43824 . 43866)
  373. (43876 . 43877) (43968 . 44002) (44016 . 44025) (44032 . 55203)
  374. (55216 . 55238) (55243 . 55291) (63744 . 64109) (64112 . 64217)
  375. (64256 . 64262) (64275 . 64279) 64285 (64287 . 64296)
  376. (64298 . 64310) (64312 . 64316) 64318 (64320 . 64321) (64323 . 64324)
  377. (64326 . 64335) (64336 . 64433) (64467 . 64829) (64848 . 64911)
  378. (64914 . 64967) (65008 . 65019) (65136 . 65140) (65142 . 65276)
  379. (65296 . 65305) (65313 . 65338) (65345 . 65370) (65382 . 65391)
  380. (65393 . 65437) (65440 . 65470) (65474 . 65479) (65482 . 65487)
  381. (65490 . 65495) (65498 . 65500) (65536 . 65547) (65549 . 65574)
  382. (65576 . 65594) (65596 . 65597) (65599 . 65613) (65616 . 65629)
  383. (65664 . 65786) (65799 . 65843) (65909 . 65912) (65930 . 65931)
  384. (66176 . 66204) (66208 . 66256) (66273 . 66299) (66304 . 66339)
  385. (66352 . 66368) (66370 . 66377) (66384 . 66421) (66432 . 66461)
  386. (66464 . 66499) (66504 . 66511) (66560 . 66639) (66640 . 66687)
  387. (66688 . 66717) (66720 . 66729) (66816 . 66855) (66864 . 66915)
  388. (67072 . 67382) (67392 . 67413) (67424 . 67431) (67584 . 67589)
  389. 67592 (67594 . 67637) (67639 . 67640) 67644 67647 (67648 . 67669)
  390. (67672 . 67679) (67680 . 67702) (67705 . 67711) (67712 . 67742)
  391. (67751 . 67759) (67840 . 67867) (67872 . 67897) (67968 . 67999)
  392. (68000 . 68023) (68030 . 68031) 68096 (68112 . 68115)
  393. (68117 . 68119) (68121 . 68147) (68160 . 68167) (68192 . 68222)
  394. (68224 . 68255) (68288 . 68295) (68297 . 68324) (68331 . 68335)
  395. (68352 . 68405) (68416 . 68437) (68440 . 68447) (68448 . 68466)
  396. (68472 . 68479) (68480 . 68497) (68521 . 68527) (68608 . 68680)
  397. (69216 . 69246) (69635 . 69687) (69714 . 69743) (69763 . 69807)
  398. (69840 . 69864) (69872 . 69881) (69891 . 69926) (69942 . 69951)
  399. (69968 . 70002) 70006 (70019 . 70066) (70081 . 70084) (70096 . 70106)
  400. (70113 . 70132) (70144 . 70161) (70163 . 70187) (70320 . 70366)
  401. (70384 . 70393) (70405 . 70412) (70415 . 70416) (70419 . 70440)
  402. (70442 . 70448) (70450 . 70451) (70453 . 70457) 70461
  403. (70493 . 70497) (70784 . 70831) (70852 . 70853) 70855 (70864 . 70873)
  404. (71040 . 71086) (71168 . 71215) 71236 (71248 . 71257)
  405. (71296 . 71338) (71360 . 71369) (71840 . 71922) 71935 (72384 . 72440)
  406. (73728 . 74648) (77824 . 78894) (92160 . 92728) (92736 . 92766)
  407. (92768 . 92777) (92880 . 92909) (92928 . 92975) (93008 . 93017)
  408. (93019 . 93025) (93027 . 93047) (93053 . 93071) (93952 . 94020)
  409. 94032 (110592 . 110593) (113664 . 113770) (113776 . 113788)
  410. (113792 . 113800) (113808 . 113817) (119648 . 119665) (119808 . 119892)
  411. (119894 . 119964) (119966 . 119967) 119970 (119973 . 119974)
  412. (119977 . 119980) (119982 . 119993) 119995 (119997 . 120003)
  413. (120005 . 120069) (120071 . 120074) (120077 . 120084)
  414. (120086 . 120092) (120094 . 120121) (120123 . 120126) (120128 . 120132)
  415. 120134 (120138 . 120144) (120146 . 120485) (120488 . 120512)
  416. (120514 . 120538) (120540 . 120570) (120572 . 120596)
  417. (120598 . 120628) (120630 . 120654) (120656 . 120686) (120688 . 120712)
  418. (120714 . 120744) (120746 . 120770) (120772 . 120779)
  419. (120782 . 120831) (124928 . 125124) (125127 . 125135) (126464 . 126467)
  420. (126469 . 126495) (126497 . 126498) 126500 126503 (126505 . 126514)
  421. (126516 . 126519) 126521 126523 126530 126535 126537
  422. 126539 (126541 . 126543) (126545 . 126546) 126548 126551 126553
  423. 126555 126557 126559 (126561 . 126562) 126564 (126567 . 126570)
  424. (126572 . 126578) (126580 . 126583) (126585 . 126588) 126590 (126592 . 126601)
  425. (126603 . 126619) (126625 . 126627) (126629 . 126633)
  426. (126635 . 126651) (127232 . 127244) (131072 . 173782) (173824 . 177972)
  427. (177984 . 178205) (194560 . 195101)))
  428. ;; Syntax table.
  429. (defvar haskell-mode-syntax-table
  430. (let ((table (make-syntax-table)))
  431. (modify-syntax-entry ?\ " " table)
  432. (modify-syntax-entry ?\t " " table)
  433. (modify-syntax-entry ?\" "\"" table)
  434. (modify-syntax-entry ?\' "_" table)
  435. (modify-syntax-entry ?_ "_" table)
  436. (modify-syntax-entry ?\( "()" table)
  437. (modify-syntax-entry ?\) ")(" table)
  438. (modify-syntax-entry ?\[ "(]" table)
  439. (modify-syntax-entry ?\] ")[" table)
  440. (modify-syntax-entry ?\{ "(}1nb" table)
  441. (modify-syntax-entry ?\} "){4nb" table)
  442. (modify-syntax-entry ?- ". 123" table)
  443. (modify-syntax-entry ?\n ">" table)
  444. (modify-syntax-entry ?\` "$`" table)
  445. (mapc (lambda (x)
  446. (modify-syntax-entry x "." table))
  447. "!#$%&*+./:<=>?@^|~,;\\")
  448. ;; Haskell symbol characters are treated as punctuation because
  449. ;; they are not able to form identifiers with word constituent 'w'
  450. ;; class characters.
  451. (dolist (charcodes haskell--char-syntax-symbols)
  452. (modify-syntax-entry charcodes "." table))
  453. ;; ... and for identifier characters
  454. (dolist (charcodes haskell--char-syntax-identifiers)
  455. (modify-syntax-entry charcodes "w" table))
  456. table)
  457. "Syntax table used in Haskell mode.")
  458. (defun haskell-syntax-propertize (begin end)
  459. (save-excursion
  460. (when haskell-literate
  461. (goto-char begin)
  462. ;; Algorithm (first matching rule wins):
  463. ;; - current line is latex code if previous non-empty line was
  464. ;; latex code or was \begin{code} and current line is not
  465. ;; \end{code}
  466. ;; - current line is bird code if it starts with >
  467. ;; - else literate comment
  468. (let ((previous-line-latex-code
  469. (catch 'return
  470. (save-excursion
  471. (when (= (forward-line -1) 0)
  472. (while (looking-at-p "^[\t ]*$")
  473. (unless (= (forward-line -1) 0)
  474. (throw 'return nil)))
  475. (or
  476. (and
  477. (not (equal (string-to-syntax "<") (syntax-after (point))))
  478. (not (looking-at-p "^>")))
  479. (looking-at-p "^\\\\begin{code}[\t ]*$")))))))
  480. (while (< (point) end)
  481. (unless (looking-at-p "^[\t ]*$")
  482. (if previous-line-latex-code
  483. (if (looking-at-p "^\\\\end{code}[\t ]*$")
  484. (progn
  485. (put-text-property (point) (1+ (point)) 'syntax-table (string-to-syntax "<"))
  486. (setq previous-line-latex-code nil))
  487. ;; continue latex-code
  488. )
  489. (if (looking-at-p "^>")
  490. ;; this is a whitespace
  491. (put-text-property (point) (1+ (point)) 'syntax-table (string-to-syntax "-"))
  492. ;; this is a literate comment
  493. (progn
  494. (put-text-property (point) (1+ (point)) 'syntax-table (string-to-syntax "<"))
  495. (when (looking-at-p "^\\\\begin{code}[\t ]*$")
  496. (setq previous-line-latex-code t))))))
  497. (forward-line 1))))
  498. (goto-char begin)
  499. (let ((ppss (syntax-ppss)))
  500. (when (nth 4 ppss)
  501. ;; go to the end of a comment, there is nothing to see inside
  502. ;; a comment so we might as well just skip over it
  503. ;; immediatelly
  504. (setq ppss (parse-partial-sexp (point) (point-max) nil nil ppss
  505. 'syntax-table)))
  506. (when (nth 8 ppss)
  507. ;; go to the beginning of a comment or string
  508. (goto-char (nth 8 ppss))
  509. (when (equal ?| (nth 3 ppss))
  510. ;; if this is a quasi quote we need to backtrack even more
  511. ;; to the opening bracket
  512. (skip-chars-backward "^[")
  513. (goto-char (1- (point)))))
  514. (while (< (point) end)
  515. (let
  516. ((token-kind (haskell-lexeme-looking-at-token)))
  517. (cond
  518. ((equal token-kind 'qsymid)
  519. (when (member
  520. (haskell-lexeme-classify-by-first-char (char-after (match-beginning 1)))
  521. '(varsym consym))
  522. ;; we have to neutralize potential comments here
  523. (put-text-property (match-beginning 1) (match-end 1) 'syntax-table (string-to-syntax "."))))
  524. ((equal token-kind 'number)
  525. (put-text-property (match-beginning 0) (match-end 0) 'syntax-table (string-to-syntax "w")))
  526. ((equal token-kind 'char)
  527. (save-excursion
  528. (goto-char (match-beginning 2))
  529. (let ((limit (match-end 2)))
  530. (save-match-data
  531. (while (re-search-forward "\"" limit t)
  532. (put-text-property (match-beginning 0) (match-end 0) 'syntax-table (string-to-syntax ".")))))
  533. ;; Place a generic string delimeter only when an open
  534. ;; quote is closed by end-of-line Emacs acts strangely
  535. ;; when a generic delimiter is not closed so in case
  536. ;; string ends at the end of the buffer we will use
  537. ;; plain string
  538. (if (and (not (match-beginning 3))
  539. (not (equal (match-end 2) (point-max))))
  540. (progn
  541. (put-text-property (match-beginning 1) (match-end 1) 'syntax-table (string-to-syntax "|"))
  542. (put-text-property (match-end 2 ) (1+ (match-end 2)) 'syntax-table (string-to-syntax "|")))
  543. (put-text-property (match-beginning 1) (match-end 1) 'syntax-table (string-to-syntax "\""))
  544. (when (not (equal (match-end 2) (point-max)))
  545. (put-text-property (match-end 2 ) (1+ (match-end 2)) 'syntax-table (string-to-syntax "\""))))))
  546. ((equal token-kind 'string)
  547. (save-excursion
  548. (goto-char (match-beginning 2))
  549. (let ((limit (match-end 2)))
  550. (save-match-data
  551. (while (re-search-forward "\"" limit t)
  552. (put-text-property (match-beginning 0) (match-end 0) 'syntax-table (string-to-syntax ".")))))
  553. ;; Place a generic string delimeter only when an open
  554. ;; quote is closed by end-of-line Emacs acts strangely
  555. ;; when a generic delimiter is not closed so in case
  556. ;; string ends at the end of the buffer we will use
  557. ;; plain string
  558. (when (and (not (match-beginning 3))
  559. (not (equal (match-end 2) (point-max))))
  560. (put-text-property (match-beginning 1) (match-end 1) 'syntax-table (string-to-syntax "|"))
  561. (put-text-property (match-end 2 ) (1+ (match-end 2)) 'syntax-table (string-to-syntax "|")))))
  562. ((equal token-kind 'template-haskell-quasi-quote)
  563. (put-text-property (match-beginning 2) (match-end 2) 'syntax-table (string-to-syntax "\""))
  564. (when (match-beginning 4)
  565. (put-text-property (match-beginning 4) (match-end 4) 'syntax-table (string-to-syntax "\"")))
  566. (save-excursion
  567. (goto-char (match-beginning 3))
  568. (let ((limit (match-end 3)))
  569. (save-match-data
  570. (while (re-search-forward "\"" limit t)
  571. (put-text-property (match-beginning 0) (match-end 0) 'syntax-table (string-to-syntax "."))))))))
  572. (if token-kind
  573. (goto-char (match-end 0))
  574. (goto-char end)))))))
  575. (defun haskell-ident-at-point ()
  576. "Return the identifier near point going backward or nil if none found.
  577. May return a qualified name."
  578. (let ((reg (haskell-ident-pos-at-point)))
  579. (when reg
  580. (buffer-substring-no-properties (car reg) (cdr reg)))))
  581. (defun haskell-spanable-pos-at-point ()
  582. "Like `haskell-ident-pos-at-point', but includes any surrounding backticks."
  583. (save-excursion
  584. (let ((pos (haskell-ident-pos-at-point)))
  585. (when pos
  586. (cl-destructuring-bind (start . end) pos
  587. (if (and (eq ?` (char-before start))
  588. (eq ?` (char-after end)))
  589. (cons (- start 1) (+ end 1))
  590. (cons start end)))))))
  591. (defun haskell-ident-pos-at-point ()
  592. "Return the span of the identifier near point going backward.
  593. Returns nil if no identifier found or point is inside string or
  594. comment. May return a qualified name."
  595. (when (not (nth 8 (syntax-ppss)))
  596. ;; Do not handle comments and strings
  597. (let (start end)
  598. ;; Initial point position is non-deterministic, it may occur anywhere
  599. ;; inside identifier span, so the approach is:
  600. ;; - first try go left and find left boundary
  601. ;; - then try go right and find right boundary
  602. ;;
  603. ;; In both cases assume the longest path, e.g. when going left take into
  604. ;; account than point may occur at the end of identifier, when going right
  605. ;; take into account that point may occur at the beginning of identifier.
  606. ;;
  607. ;; We should handle `.` character very careful because it is heavily
  608. ;; overloaded. Examples of possible cases:
  609. ;; Control.Monad.>>= -- delimiter
  610. ;; Control.Monad.when -- delimiter
  611. ;; Data.Aeson..: -- delimiter and operator symbol
  612. ;; concat.map -- composition function
  613. ;; .? -- operator symbol
  614. (save-excursion
  615. ;; First, skip whitespace if we're on it, moving point to last
  616. ;; identifier char. That way, if we're at "map ", we'll see the word
  617. ;; "map".
  618. (when (and (eolp)
  619. (not (bolp)))
  620. (backward-char))
  621. (when (and (not (eobp))
  622. (eq (char-syntax (char-after)) ? ))
  623. (skip-chars-backward " \t")
  624. (backward-char))
  625. ;; Now let's try to go left.
  626. (save-excursion
  627. (if (not (haskell-mode--looking-at-varsym))
  628. ;; Looking at non-operator char, this is quite simple
  629. (progn
  630. (skip-syntax-backward "w_")
  631. ;; Remember position
  632. (setq start (point)))
  633. ;; Looking at operator char.
  634. (while (and (not (bobp))
  635. (haskell-mode--looking-at-varsym))
  636. ;; skip all operator chars backward
  637. (setq start (point))
  638. (backward-char))
  639. ;; Extra check for case when reached beginning of the buffer.
  640. (when (haskell-mode--looking-at-varsym)
  641. (setq start (point))))
  642. ;; Slurp qualification part if present. If identifier is qualified in
  643. ;; case of non-operator point will stop before `.` dot, but in case of
  644. ;; operator it will stand at `.` delimiting dot. So if we're looking
  645. ;; at `.` let's step one char forward and try to get qualification
  646. ;; part.
  647. (goto-char start)
  648. (when (looking-at-p (rx "."))
  649. (forward-char))
  650. (let ((pos (haskell-mode--skip-qualification-backward)))
  651. (when pos
  652. (setq start pos))))
  653. ;; Finally, let's try to go right.
  654. (save-excursion
  655. ;; Try to slurp qualification part first.
  656. (skip-syntax-forward "w_")
  657. (setq end (point))
  658. (while (and (looking-at (rx "." upper))
  659. (not (zerop (progn (forward-char)
  660. (skip-syntax-forward "w_")))))
  661. (setq end (point)))
  662. ;; If point was at non-operator we already done, otherwise we need an
  663. ;; extra check.
  664. (while (haskell-mode--looking-at-varsym)
  665. (forward-char)
  666. (setq end (point))))
  667. (when (not (= start end))
  668. (cons start end))))))
  669. (defun haskell-mode--looking-at-varsym ()
  670. "Return t when point stands at operator symbol."
  671. (when (not (eobp))
  672. (let ((lex (haskell-lexeme-classify-by-first-char (char-after))))
  673. (or (eq lex 'varsym)
  674. (eq lex 'consym)))))
  675. (defun haskell-mode--skip-qualification-backward ()
  676. "Skip qualified part of identifier backward.
  677. Expects point stands *after* delimiting dot.
  678. Returns beginning position of qualified part or nil if no qualified part found."
  679. (when (not (and (bobp)
  680. (looking-at (rx bol))))
  681. (let ((case-fold-search nil)
  682. pos)
  683. (while (and (eq (char-before) ?.)
  684. (progn (backward-char)
  685. (not (zerop (skip-syntax-backward "w'"))))
  686. (skip-syntax-forward "'")
  687. (looking-at "[[:upper:]]"))
  688. (setq pos (point)))
  689. pos)))
  690. (defun haskell-delete-indentation (&optional arg)
  691. "Like `delete-indentation' but ignoring Bird-style \">\".
  692. Prefix ARG is handled as per `delete-indentation'."
  693. (interactive "*P")
  694. (let ((fill-prefix (or fill-prefix (if (eq haskell-literate 'bird) ">"))))
  695. (delete-indentation arg)))
  696. (defvar eldoc-print-current-symbol-info-function)
  697. (defvar electric-pair-inhibit-predicate)
  698. (declare-function electric-pair-default-inhibit "elec-pair")
  699. (defun haskell-mode--inhibit-bracket-inside-comment-or-default (ch)
  700. "An `electric-pair-mode' inhibit function for character CH."
  701. (or (nth 4 (syntax-ppss))
  702. (funcall #'electric-pair-default-inhibit ch)))
  703. ;; The main mode functions
  704. ;;;###autoload
  705. (define-derived-mode haskell-mode prog-mode "Haskell"
  706. "Major mode for editing Haskell programs.
  707. \\<haskell-mode-map>
  708. Literate Haskell scripts are supported via `literate-haskell-mode'.
  709. The variable `haskell-literate' indicates the style of the script in the
  710. current buffer. See the documentation on this variable for more details.
  711. Use `haskell-version' to find out what version of Haskell mode you are
  712. currently using.
  713. Additional Haskell mode modules can be hooked in via `haskell-mode-hook'.
  714. Indentation modes:
  715. `haskell-indentation-mode', Kristof Bastiaensen, Gergely Risko
  716. Intelligent semi-automatic indentation Mk2
  717. `haskell-indent-mode', Guy Lapalme
  718. Intelligent semi-automatic indentation.
  719. Interaction modes:
  720. `interactive-haskell-mode'
  721. Interact with per-project GHCi processes through a REPL and
  722. directory-aware sessions.
  723. Other modes:
  724. `haskell-decl-scan-mode', Graeme E Moss
  725. Scans top-level declarations, and places them in a menu.
  726. `haskell-doc-mode', Hans-Wolfgang Loidl
  727. Echoes types of functions or syntax of keywords when the cursor is idle.
  728. To activate a minor-mode, simply run the interactive command. For
  729. example, `M-x haskell-doc-mode'. Run it again to disable it.
  730. To enable a mode for every haskell-mode buffer, add a hook in
  731. your Emacs configuration. To do that you can customize
  732. `haskell-mode-hook' or add lines to your .emacs file. For
  733. example, to enable `interactive-haskell-mode', use the following:
  734. (add-hook 'haskell-mode-hook 'interactive-haskell-mode)
  735. Minor modes that work well with `haskell-mode':
  736. - `smerge-mode': show and work with diff3 conflict markers used
  737. by git, svn and other version control systems."
  738. :group 'haskell
  739. (when (version< emacs-version "24.3")
  740. (error "haskell-mode requires at least Emacs 24.3"))
  741. ;; paragraph-{start,separate} should treat comments as paragraphs as well.
  742. (setq-local paragraph-start (concat " *{-\\| *-- |\\|" page-delimiter))
  743. (setq-local paragraph-separate (concat " *$\\| *\\({-\\|-}\\) *$\\|" page-delimiter))
  744. (setq-local fill-paragraph-function 'haskell-fill-paragraph)
  745. ;; (setq-local adaptive-fill-function 'haskell-adaptive-fill)
  746. (setq-local comment-start "-- ")
  747. (setq-local comment-padding 0)
  748. (setq-local comment-start-skip "[-{]-[ \t]*")
  749. (setq-local comment-end "")
  750. (setq-local comment-end-skip "[ \t]*\\(-}\\|\\s>\\)")
  751. (setq-local forward-sexp-function #'haskell-forward-sexp)
  752. (setq-local parse-sexp-ignore-comments nil)
  753. (setq-local syntax-propertize-function #'haskell-syntax-propertize)
  754. ;; Set things up for eldoc-mode.
  755. (setq-local eldoc-documentation-function 'haskell-doc-current-info)
  756. ;; Set things up for imenu.
  757. (setq-local imenu-create-index-function 'haskell-ds-create-imenu-index)
  758. ;; Set things up for font-lock.
  759. (setq-local font-lock-defaults
  760. '((haskell-font-lock-keywords)
  761. nil nil nil nil
  762. (font-lock-syntactic-face-function
  763. . haskell-syntactic-face-function)
  764. ;; Get help from font-lock-syntactic-keywords.
  765. (parse-sexp-lookup-properties . t)
  766. (font-lock-extra-managed-props . (composition haskell-type))))
  767. ;; Preprocessor definitions can have backslash continuations
  768. (setq-local font-lock-multiline t)
  769. ;; Haskell's layout rules mean that TABs have to be handled with extra care.
  770. ;; The safer option is to avoid TABs. The second best is to make sure
  771. ;; TABs stops are 8 chars apart, as mandated by the Haskell Report. --Stef
  772. (setq-local indent-tabs-mode nil)
  773. (setq-local tab-width 8)
  774. (setq-local comment-auto-fill-only-comments t)
  775. ;; Haskell is not generally suitable for electric indentation, since
  776. ;; there is no unambiguously correct indent level for any given line.
  777. (when (boundp 'electric-indent-inhibit)
  778. (setq electric-indent-inhibit t))
  779. ;; dynamic abbrev support: recognize Haskell identifiers
  780. ;; Haskell is case-sensitive language
  781. (setq-local dabbrev-case-fold-search nil)
  782. (setq-local dabbrev-case-distinction nil)
  783. (setq-local dabbrev-case-replace nil)
  784. (setq-local dabbrev-abbrev-char-regexp "\\sw\\|[.]")
  785. (setq haskell-literate nil)
  786. (add-hook 'before-save-hook 'haskell-mode-before-save-handler nil t)
  787. (add-hook 'after-save-hook 'haskell-mode-after-save-handler nil t)
  788. ;; provide non-interactive completion function
  789. (add-hook 'completion-at-point-functions
  790. 'haskell-completions-completion-at-point
  791. nil
  792. t)
  793. ;; Avoid Emacs 25 bug with electric-pair inside comments
  794. (when (eq 25 emacs-major-version)
  795. (setq-local electric-pair-inhibit-predicate 'haskell-mode--inhibit-bracket-inside-comment-or-default))
  796. (haskell-indentation-mode))
  797. (defcustom haskell-mode-hook '(haskell-indentation-mode interactive-haskell-mode)
  798. "List of functions to run after `haskell-mode' is enabled.
  799. Use to enable minor modes coming with `haskell-mode' or run an
  800. arbitrary function.
  801. Note that `haskell-indentation-mode' and `haskell-indent-mode' should not be
  802. run at the same time."
  803. :group 'haskell
  804. :type 'hook
  805. :options '(capitalized-words-mode
  806. flyspell-prog-mode
  807. haskell-decl-scan-mode
  808. haskell-indent-mode
  809. haskell-indentation-mode
  810. highlight-uses-mode
  811. imenu-add-menubar-index
  812. interactive-haskell-mode
  813. turn-on-haskell-unicode-input-method))
  814. (defun haskell-fill-paragraph (justify)
  815. (save-excursion
  816. ;; Fill paragraph should only work in comments.
  817. ;; The -- comments are handled properly by default
  818. ;; The {- -} comments need some extra love.
  819. (let* ((syntax-values (syntax-ppss))
  820. (comment-num (nth 4 syntax-values)))
  821. (cond
  822. ((eq t comment-num)
  823. ;; standard fill works wonders inside a non-nested comment
  824. (fill-comment-paragraph justify))
  825. ((integerp comment-num)
  826. ;; we are in a nested comment. lets narrow to comment content
  827. ;; and use plain paragraph fill for that
  828. (let* ((comment-start-point (nth 8 syntax-values))
  829. (comment-end-point
  830. (save-excursion
  831. (goto-char comment-start-point)
  832. (forward-sexp)
  833. ;; Find end of any comment even if forward-sexp
  834. ;; fails to find the right braces.
  835. (backward-char 3)
  836. (re-search-forward "[ \t]?-}" nil t)
  837. (match-beginning 0)))
  838. (fill-start (+ 2 comment-start-point))
  839. (fill-end comment-end-point)
  840. (fill-paragraph-handle-comment nil))
  841. (save-restriction
  842. (narrow-to-region fill-start fill-end)
  843. (fill-paragraph justify)
  844. ;; If no filling happens, whatever called us should not
  845. ;; continue with standard text filling, so return t
  846. t)))
  847. ((eolp)
  848. ;; do nothing outside of a comment
  849. t)
  850. (t
  851. ;; go to end of line and try again
  852. (end-of-line)
  853. (haskell-fill-paragraph justify))))))
  854. ;; (defun haskell-adaptive-fill ()
  855. ;; ;; We want to use "-- " as the prefix of "-- |", etc.
  856. ;; (let* ((line-end (save-excursion (end-of-line) (point)))
  857. ;; (line-start (point)))
  858. ;; (save-excursion
  859. ;; (unless (in-comment)
  860. ;; ;; Try to find the start of a comment. We only fill comments.
  861. ;; (search-forward-regexp comment-start-skip line-end t))
  862. ;; (when (in-comment)
  863. ;; (let ();(prefix-start (point)))
  864. ;; (skip-syntax-forward "^w")
  865. ;; (make-string (- (point) line-start) ?\s))))))
  866. ;;;###autoload
  867. (defun haskell-forward-sexp (&optional arg)
  868. "Haskell specific version of `forward-sexp'.
  869. Move forward across one balanced expression (sexp). With ARG, do
  870. it that many times. Negative arg -N means move backward across N
  871. balanced expressions. This command assumes point is not in a
  872. string or comment.
  873. If unable to move over a sexp, signal `scan-error' with three
  874. arguments: a message, the start of the obstacle (a parenthesis or
  875. list marker of some kind), and end of the obstacle."
  876. (interactive "^p")
  877. (or arg (setq arg 1))
  878. (if (< arg 0)
  879. (while (< arg 0)
  880. (skip-syntax-backward "->")
  881. ;; Navigate backwards using plain `backward-sexp', assume that it
  882. ;; skipped over at least one Haskell expression, and jump forward until
  883. ;; last possible point before the starting position. If applicable,
  884. ;; `scan-error' is signalled by `backward-sexp'.
  885. (let ((end (point))
  886. (forward-sexp-function nil))
  887. (backward-sexp)
  888. (let ((cur (point)))
  889. (while (< (point) end)
  890. (setf cur (point))
  891. (haskell-forward-sexp)
  892. (skip-syntax-forward "->"))
  893. (goto-char cur)))
  894. (setf arg (1+ arg)))
  895. (save-match-data
  896. (while (> arg 0)
  897. (when (haskell-lexeme-looking-at-token)
  898. (cond ((member (match-string 0) (list "(" "[" "{"))
  899. (goto-char (or (scan-sexps (point) 1) (buffer-end 1))))
  900. ((member (match-string 0) (list ")" "]" "}"))
  901. (signal 'scan-error (list "Containing expression ends prematurely."
  902. (match-beginning 0)
  903. (match-end 0))))
  904. (t (goto-char (match-end 0)))))
  905. (setf arg (1- arg))))))
  906. ;;;###autoload
  907. (define-derived-mode literate-haskell-mode haskell-mode "LitHaskell"
  908. "As `haskell-mode' but for literate scripts."
  909. (setq haskell-literate
  910. (save-excursion
  911. (goto-char (point-min))
  912. (cond
  913. ((re-search-forward "^\\\\\\(begin\\|end\\){code}$" nil t) 'tex)
  914. ((re-search-forward "^>" nil t) 'bird)
  915. (t haskell-literate-default))))
  916. (if (eq haskell-literate 'bird)
  917. ;; fill-comment-paragraph isn't much use there, and even gets confused
  918. ;; by the syntax-table text-properties we add to mark the first char
  919. ;; of each line as a comment-starter.
  920. (setq-local fill-paragraph-handle-comment nil))
  921. (setq-local mode-line-process '("/" (:eval (symbol-name haskell-literate)))))
  922. ;;;###autoload
  923. (add-to-list 'auto-mode-alist '("\\.[gh]s\\'" . haskell-mode))
  924. ;;;###autoload
  925. (add-to-list 'auto-mode-alist '("\\.hsig\\'" . haskell-mode))
  926. ;;;###autoload
  927. (add-to-list 'auto-mode-alist '("\\.l[gh]s\\'" . literate-haskell-mode))
  928. ;;;###autoload
  929. (add-to-list 'auto-mode-alist '("\\.hsc\\'" . haskell-mode))
  930. ;;;###autoload
  931. (add-to-list 'interpreter-mode-alist '("runghc" . haskell-mode))
  932. ;;;###autoload
  933. (add-to-list 'interpreter-mode-alist '("runhaskell" . haskell-mode))
  934. ;;;###autoload
  935. (add-to-list 'completion-ignored-extensions ".hi")
  936. (defcustom haskell-check-command "hlint"
  937. "*Command used to check a Haskell file."
  938. :group 'haskell
  939. :type '(choice (const "hlint")
  940. (const "ghc -fno-code")
  941. (string :tag "Other command")))
  942. (defcustom haskell-tags-on-save nil
  943. "Generate tags via hasktags after saving."
  944. :group 'haskell
  945. :type 'boolean)
  946. (defvar haskell-saved-check-command nil
  947. "Internal use.")
  948. ;; Like Python. Should be abstracted, sigh.
  949. (defun haskell-check (command)
  950. "Check a Haskell file (default current buffer's file).
  951. Runs COMMAND, a shell command, as if by `compile'.
  952. See `haskell-check-command' for the default."
  953. (interactive
  954. (list (read-string "Checker command: "
  955. (or haskell-saved-check-command
  956. (concat haskell-check-command " "
  957. (let ((name (buffer-file-name)))
  958. (if name
  959. (file-name-nondirectory name))))))))
  960. (setq haskell-saved-check-command command)
  961. (save-some-buffers (not compilation-ask-about-save) nil)
  962. (compilation-start command))
  963. ;; This function was renamed and deprecated, but we want clean
  964. ;; byte compilation in all versions.
  965. (defalias 'haskell-flymake-create-temp-buffer-copy
  966. (if (fboundp 'flymake-proc-init-create-temp-buffer-copy)
  967. 'flymake-proc-init-create-temp-buffer-copy
  968. 'flymake-init-create-temp-buffer-copy))
  969. (defun haskell-flymake-init ()
  970. "Flymake init function for Haskell."
  971. (let ((checker-elts (and haskell-saved-check-command
  972. (split-string haskell-saved-check-command))))
  973. (list (car checker-elts)
  974. (append (cdr checker-elts)
  975. (list (haskell-flymake-create-temp-buffer-copy
  976. 'flymake-create-temp-inplace))))))
  977. (add-to-list 'flymake-allowed-file-name-masks '("\\.l?hs\\'" haskell-flymake-init))
  978. (defun haskell-mode-format-imports ()
  979. "Format the imports by aligning and sorting them."
  980. (interactive)
  981. (let ((col (current-column)))
  982. (haskell-sort-imports)
  983. (haskell-align-imports)
  984. (goto-char (+ (line-beginning-position)
  985. col))))
  986. (declare-function haskell-mode-stylish-buffer "haskell-commands")
  987. (defun haskell-mode-before-save-handler ()
  988. "Function that will be called before buffer's saving."
  989. (when haskell-stylish-on-save
  990. (ignore-errors (haskell-mode-stylish-buffer))))
  991. ;; From Bryan O'Sullivan's blog:
  992. ;; http://www.serpentine.com/blog/2007/10/09/using-emacs-to-insert-scc-annotations-in-haskell-code/
  993. (defun haskell-mode-try-insert-scc-at-point ()
  994. "Try to insert an SCC annotation at point. Return true if
  995. successful, nil otherwise."
  996. (if (or (looking-at "\\b\\|[ \t]\\|$")
  997. ;; Allow SCC if point is on a non-letter with whitespace to the left
  998. (and (not (bolp))
  999. (save-excursion
  1000. (forward-char -1)
  1001. (looking-at "[ \t]"))))
  1002. (let ((space-at-point (looking-at "[ \t]")))
  1003. (unless (and (not (bolp)) (save-excursion
  1004. (forward-char -1)
  1005. (looking-at "[ \t]")))
  1006. (insert " "))
  1007. (insert "{-# SCC \"\" #-}")
  1008. (unless space-at-point
  1009. (insert " "))
  1010. (forward-char (if space-at-point -5 -6))
  1011. t )))
  1012. (defun haskell-mode-insert-scc-at-point ()
  1013. "Insert an SCC annotation at point."
  1014. (interactive)
  1015. (if (not (haskell-mode-try-insert-scc-at-point))
  1016. (error "Not over an area of whitespace")))
  1017. (make-obsolete
  1018. 'haskell-mode-insert-scc-at-point
  1019. 'haskell-mode-toggle-scc-at-point
  1020. "2015-11-11")
  1021. (defun haskell-mode-try-kill-scc-at-point ()
  1022. "Try to kill an SCC annotation at point. Return true if
  1023. successful, nil otherwise."
  1024. (save-excursion
  1025. (let ((old-point (point))
  1026. (scc "\\({-#[ \t]*SCC \"[^\"]*\"[ \t]*#-}\\)[ \t]*"))
  1027. (while (not (or (looking-at scc) (bolp)))
  1028. (forward-char -1))
  1029. (if (and (looking-at scc)
  1030. (<= (match-beginning 1) old-point)
  1031. (> (match-end 1) old-point))
  1032. (progn (kill-region (match-beginning 0) (match-end 0))
  1033. t)))))
  1034. ;; Also Bryan O'Sullivan's.
  1035. (defun haskell-mode-kill-scc-at-point ()
  1036. "Kill the SCC annotation at point."
  1037. (interactive)
  1038. (if (not (haskell-mode-try-kill-scc-at-point))
  1039. (error "No SCC at point")))
  1040. (make-obsolete
  1041. 'haskell-mode-kill-scc-at-point
  1042. 'haskell-mode-toggle-scc-at-point
  1043. "2015-11-11")
  1044. (defun haskell-mode-toggle-scc-at-point ()
  1045. "If point is in an SCC annotation, kill the annotation. Otherwise, try to insert a new annotation."
  1046. (interactive)
  1047. (if (not (haskell-mode-try-kill-scc-at-point))
  1048. (if (not (haskell-mode-try-insert-scc-at-point))
  1049. (error "Could not insert or remove SCC"))))
  1050. (defun haskell-guess-module-name-from-file-name (file-name)
  1051. "Guess the module name from FILE-NAME.
  1052. Based on given FILE-NAME this function tries to find path
  1053. components that look like module identifiers and composes full
  1054. module path using this information. For example:
  1055. /Abc/Def/Xyz.lhs => Abc.Def.Xyz
  1056. /Ab-c/Def/Xyz.lhs => Def.Xyz
  1057. src/Abc/Def/Xyz.hs => Abc.Def.Xyz
  1058. c:\\src\\Abc\\Def\\Xyz.hs => Abc.Def.Xyz
  1059. nonmodule.txt => nil
  1060. This function usually will be used with `buffer-file-name':
  1061. (haskell-guess-module-name-from-file-name (buffer-file-name))"
  1062. (let* ((file-name-sans-ext (file-name-sans-extension file-name))
  1063. (components (cl-loop for part
  1064. in (reverse (split-string file-name-sans-ext "/"))
  1065. while (let ((case-fold-search nil))
  1066. (string-match (concat "^" haskell-lexeme-modid "$") part))
  1067. collect part)))
  1068. (when components
  1069. (mapconcat 'identity (reverse components) "."))))
  1070. (defun haskell-guess-module-name ()
  1071. "Guess the current module name of the buffer.
  1072. Uses `haskell-guess-module-name-from-file-name'."
  1073. (haskell-guess-module-name-from-file-name (buffer-file-name)))
  1074. (defvar haskell-auto-insert-module-format-string
  1075. "-- | \n\nmodule %s where\n\n"
  1076. "Template string that will be inserted in new haskell buffers via `haskell-auto-insert-module-template'.")
  1077. (defun haskell-auto-insert-module-template ()
  1078. "Insert a module template for the newly created buffer."
  1079. (interactive)
  1080. (when (and (= (point-min)
  1081. (point-max))
  1082. (buffer-file-name))
  1083. (insert (format haskell-auto-insert-module-format-string (haskell-guess-module-name-from-file-name (buffer-file-name))))
  1084. (goto-char (point-min))
  1085. (end-of-line)))
  1086. ;;;###autoload
  1087. (defun haskell-mode-generate-tags (&optional and-then-find-this-tag)
  1088. "Generate tags using Hasktags. This is synchronous function.
  1089. If optional AND-THEN-FIND-THIS-TAG argument is present it is used
  1090. with function `xref-find-definitions' after new table was
  1091. generated."
  1092. (interactive)
  1093. (let* ((dir (haskell-cabal--find-tags-dir))
  1094. (command (haskell-cabal--compose-hasktags-command dir)))
  1095. (if (not command)
  1096. (error "Unable to compose hasktags command")
  1097. (shell-command command)
  1098. (haskell-mode-message-line "Tags generated.")
  1099. (when and-then-find-this-tag
  1100. (let ((tags-file-name dir))
  1101. (xref-find-definitions and-then-find-this-tag))))))
  1102. ;; Provide ourselves:
  1103. (provide 'haskell-mode)
  1104. ;;; haskell-mode.el ends here