1
;;; haskell-mode.el --- A Haskell editing mode -*-coding: iso-8859-1;-*-
3
;; Copyright (C) 2003, 2004 Free Software Foundation, Inc
4
;; Copyright (C) 1992, 1997-1998 Simon Marlow, Graeme E Moss, and Tommy Thorn
6
;; Authors: 1992 Simon Marlow
7
;; 1997-1998 Graeme E Moss <gem@cs.york.ac.uk> and
8
;; Tommy Thorn <thorn@irisa.fr>,
9
;; 2001-2002 Reuben Thomas (>=v1.4)
10
;; 2003 Dave Love <fx@gnu.org>
11
;; Keywords: faces files Haskell
13
;; URL: http://www.haskell.org/haskell-mode/
15
;;; This file is not part of GNU Emacs.
17
;; This file is free software; you can redistribute it and/or modify
18
;; it under the terms of the GNU General Public License as published by
19
;; the Free Software Foundation; either version 2, or (at your option)
22
;; This file is distributed in the hope that it will be useful,
23
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
;; GNU General Public License for more details.
27
;; You should have received a copy of the GNU General Public License
28
;; along with GNU Emacs; see the file COPYING. If not, write to the
29
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30
;; Boston, MA 02111-1307, USA.
37
;; To provide a pleasant mode to browse and edit Haskell files, linking
38
;; into the following supported modules:
40
;; `haskell-font-lock', Graeme E Moss and Tommy Thorn
41
;; Fontifies standard Haskell keywords, symbols, functions, etc.
43
;; `haskell-decl-scan', Graeme E Moss
44
;; Scans top-level declarations, and places them in a menu.
46
;; `haskell-doc', Hans-Wolfgang Loidl
47
;; Echoes types of functions or syntax of keywords when the cursor is idle.
49
;; `haskell-indent', Guy Lapalme
50
;; Intelligent semi-automatic indentation.
52
;; `haskell-simple-indent', Graeme E Moss and Heribert Schuetz
53
;; Simple indentation.
56
;; Interaction with an inferior Haskell process.
57
;; It replaces the previous two modules:
58
;; `haskell-hugs', Guy Lapalme
59
;; `haskell-ghci', Chris Web
62
;; This mode supports full Haskell 1.4 including literate scripts.
63
;; In some versions of (X)Emacs it may only support Latin-1, not Unicode.
67
;; Put in your ~/.emacs:
69
;; (setq auto-mode-alist
70
;; (append auto-mode-alist
71
;; '(("\\.[hg]s$" . haskell-mode)
72
;; ("\\.hi$" . haskell-mode)
73
;; ("\\.l[hg]s$" . literate-haskell-mode))))
75
;; (autoload 'haskell-mode "haskell-mode"
76
;; "Major mode for editing Haskell scripts." t)
77
;; (autoload 'literate-haskell-mode "haskell-mode"
78
;; "Major mode for editing literate Haskell scripts." t)
80
;; with `haskell-mode.el' accessible somewhere on the load-path.
81
;; To add a directory `~/lib/emacs' (for example) to the load-path,
82
;; add the following to .emacs:
84
;; (setq load-path (cons "~/lib/emacs" load-path))
86
;; To turn any of the supported modules on for all buffers, add the
87
;; appropriate line(s) to .emacs:
89
;; (add-hook 'haskell-mode-hook 'turn-on-haskell-decl-scan)
90
;; (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
91
;; (add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
92
;; (add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
94
;; Make sure the module files are also on the load-path. Note that
95
;; the two indentation modules are mutually exclusive: Use only one.
100
;; Set the value of `haskell-literate-default' to your preferred
101
;; literate style: 'bird or 'latex, within .emacs as follows:
103
;; (setq haskell-literate-default 'latex)
105
;; Also see the customisations of the modules.
110
;; This mode is based on an editing mode by Simon Marlow 11/1/92
111
;; and heavily modified by Graeme E Moss and Tommy Thorn 7/11/98.
113
;; If you have any problems or suggestions specific to a supported
114
;; module, consult that module for a list of known bugs, and an
115
;; author to contact via email. For general problems or suggestions,
116
;; consult the list below, then email gem@cs.york.ac.uk and
117
;; thorn@irisa.fr quoting the version of the mode you are using, the
118
;; version of Emacs you are using, and a small example of the problem
122
;; Various tweaks to doc strings and customization support from
123
;; Ville Skytt� <scop@xemacs.org>.
126
;; Added autoload for GHCi inferior mode (thanks to Scott
127
;; Williams for the bug report and fix).
130
;; Improved packaging, and made a couple more variables
131
;; interactively settable.
134
;; Added GHCi mode from Chris Webb, and tidied up a little.
137
;; The literate or non-literate style of a buffer is now indicated
138
;; by just the variable haskell-literate: nil, 'bird, or 'latex.
139
;; For literate buffers with ambiguous style, the value of
140
;; haskell-literate-default is used.
143
;; Separated off font locking, declaration scanning and simple
144
;; indentation, and made them separate modules. Modules can be
145
;; added easily now. Support for modules haskell-doc,
146
;; haskell-indent, and haskell-hugs. Literate and non-literate
147
;; modes integrated into one mode, and literate buffer indicated by
148
;; value of haskell-literate(-bird-style).
151
;; Added support for declaration scanning under XEmacs via
152
;; func-menu. Moved operators to level two fontification.
155
;; Added a nice indention support from Heribert Schuetz
156
;; <Heribert.Schuetz@informatik.uni-muenchen.de>:
158
;; I have just hacked an Emacs Lisp function which you might prefer
159
;; to `indent-relative' in haskell-mode.el. See below. It is not
160
;; really Haskell-specific because it does not take into account
161
;; keywords like `do', `of', and `let' (where the layout rule
162
;; applies), but I already find it useful.
164
;; Cleaned up the imenu support. Added support for literate scripts.
166
;; Version 0.103 [HWL]:
167
;; From Hans Wolfgang Loidl <hwloidl@dcs.gla.ac.uk>:
169
;; I (HWL) added imenu support by copying the appropriate functions
170
;; from hugs-mode. A menu-bar item "Declarations" is now added in
171
;; haskell mode. The new code, however, needs some clean-up.
175
;; Moved C-c C-c key binding to comment-region. Leave M-g M-g to do
176
;; the work. comment-start-skip is changed to comply with comment-start.
180
;; Altered indent-line-function to indent-relative.
184
;; First official release.
186
;; Present Limitations/Future Work (contributions are most welcome!):
188
;; . Would like RET in Bird-style literate mode to add a ">" at the
189
;; start of a line when previous line starts with ">". Or would
192
;; . Support for GreenCard?
197
(eval-when-compile (require 'cl))
199
;; All functions/variables start with `(literate-)haskell-'.
202
(defconst haskell-version "1.43"
203
"`haskell-mode' version number.")
204
(defun haskell-version ()
205
"Echo the current version of `haskell-mode' in the minibuffer."
207
(message "Using haskell-mode version %s" haskell-version))
209
(defgroup haskell nil
210
"Major mode for editing Haskell programs."
214
;; Set up autoloads for the modules we supply
215
(autoload 'turn-on-haskell-decl-scan "haskell-decl-scan"
216
"Turn on Haskell declaration scanning." t)
217
(autoload 'turn-on-haskell-doc-mode "haskell-doc"
218
"Turn on Haskell Doc minor mode." t)
219
(autoload 'turn-on-haskell-indent "haskell-indent"
220
"Turn on Haskell indentation." t)
221
(autoload 'turn-on-haskell-simple-indent "haskell-simple-indent"
222
"Turn on simple Haskell indentation." t)
224
;; Functionality provided in other files.
225
(autoload 'haskell-ds-create-imenu-index "haskell-decl-scan")
226
(autoload 'haskell-font-lock-choose-keywords "haskell-font-lock")
227
(autoload 'haskell-doc-current-info "haskell-doc")
229
;; Obsolete functions.
230
(defun turn-on-haskell-font-lock ()
233
(message "turn-on-haskell-font-lock is obsolete. Use turn-on-font-lock instead."))
234
(defun turn-on-haskell-hugs ()
236
(message "haskell-hugs is obsolete.")
237
(load "haskell-hugs")
238
(turn-on-haskell-hugs))
239
(defun turn-on-haskell-ghci ()
241
(message "haskell-ghci is obsolete.")
242
(load "haskell-ghci")
243
(turn-on-haskell-ghci))
246
;; Are we looking at a literate script?
247
(defvar haskell-literate nil
248
"*If not nil, the current buffer contains a literate Haskell script.
249
Possible values are: `bird' and `latex', for Bird-style and LaTeX-style
250
literate scripts respectively. Set by `haskell-mode' and
251
`literate-haskell-mode'. For an ambiguous literate buffer -- ie. does
252
not contain either \"\\begin{code}\" or \"\\end{code}\" on a line on
253
its own, nor does it contain \">\" at the start of a line -- the value
254
of `haskell-literate-default' is used.
256
Always buffer-local.")
257
(make-variable-buffer-local 'haskell-literate)
258
;; Default literate style for ambiguous literate buffers.
259
(defcustom haskell-literate-default 'bird
260
"*Default value for `haskell-literate'.
261
Used if the style of a literate buffer is ambiguous. This variable should
262
be set to the preferred literate style. For example, place within
265
(setq haskell-literate-default 'latex)"
266
:type '(choice bird latex nil)
270
(defvar haskell-mode-map
271
(let ((map (make-sparse-keymap)))
272
;; Bindings for the inferior haskell process:
273
;; (define-key map [?\M-C-x] 'inferior-haskell-send-defun)
274
;; (define-key map [?\C-x ?\C-e] 'inferior-haskell-send-last-sexp)
275
;; (define-key map [?\C-c ?\C-r] 'inferior-haskell-send-region)
276
(define-key map [?\C-c ?\C-z] 'switch-to-haskell)
277
(define-key map [?\C-c ?\C-l] 'inferior-haskell-load-file)
278
;; Non standard in other inferior-modes, but traditional in haskell.
279
(define-key map [?\C-c ?\C-r] 'inferior-haskell-reload-file)
280
(define-key map [?\C-c ?\C-b] 'switch-to-haskell)
281
;; (define-key map [?\C-c ?\C-s] 'inferior-haskell-start-process)
283
;; That's what M-; is for.
284
;; (define-key map "\C-c\C-c" 'comment-region)
286
"Keymap used in Haskell mode.")
289
(defvar haskell-mode-syntax-table
290
(let ((table (make-syntax-table)))
291
(modify-syntax-entry ?\ " " table)
292
(modify-syntax-entry ?\t " " table)
293
(modify-syntax-entry ?\" "\"" table)
294
(modify-syntax-entry ?\' "\'" table)
295
(modify-syntax-entry ?_ "w" table)
296
(modify-syntax-entry ?\( "()" table)
297
(modify-syntax-entry ?\) ")(" table)
298
(modify-syntax-entry ?\[ "(]" table)
299
(modify-syntax-entry ?\] ")[" table)
301
(cond ((featurep 'xemacs)
302
;; I don't know whether this is equivalent to the below
303
;; (modulo nesting). -- fx
304
(modify-syntax-entry ?{ "(}5" table)
305
(modify-syntax-entry ?} "){8" table)
306
(modify-syntax-entry ?- "_ 1267" table))
308
;; The following get comment syntax right, similarly to C++
309
;; In Emacs 21, the `n' indicates that they nest.
310
;; The `b' annotation is actually ignored because it's only
311
;; meaningful on the second char of a comment-starter, so
312
;; on Emacs 20 and before we get wrong results. --Stef
313
(modify-syntax-entry ?\{ "(}1nb" table)
314
(modify-syntax-entry ?\} "){4nb" table)
315
(modify-syntax-entry ?- "_ 123" table)))
316
(modify-syntax-entry ?\n ">" table)
322
;; The current Emacs 22 codebase can pass either a char
331
(modify-syntax-entry i "_" table))
333
(standard-syntax-table)))
335
(modify-syntax-entry ?\` "$`" table)
336
(modify-syntax-entry ?\\ "\\" table)
338
(modify-syntax-entry x "_" table))
339
;; Some of these are actually OK by default.
340
"!#$%&*+./:<=>?@^|~")
341
(unless (featurep 'mule)
342
;; Non-ASCII syntax should be OK, at least in Emacs.
344
(modify-syntax-entry x "_" table))
345
(concat "�������������������������������"
348
(modify-syntax-entry x "w" table))
349
(concat "�����������������������"
351
"�����������������������"
354
"Syntax table used in Haskell mode.")
356
;; Various mode variables.
357
(defun haskell-vars ()
358
(kill-all-local-variables)
359
(make-local-variable 'paragraph-start)
360
(setq paragraph-start (concat "^$\\|" page-delimiter))
361
(make-local-variable 'paragraph-separate)
362
(setq paragraph-separate paragraph-start)
363
(make-local-variable 'comment-start)
364
(setq comment-start "-- ")
365
(make-local-variable 'comment-padding)
366
(setq comment-padding 0)
367
(set (make-local-variable 'comment-start-skip) "[-{]-[ \t]*")
368
(make-local-variable 'comment-end)
369
(setq comment-end "")
370
(set (make-local-variable 'comment-end-skip) "[ \t]*\\(-}\\|\\s>\\)")
371
;; Set things up for eldoc-mode.
372
(set (make-local-variable 'eldoc-print-current-symbol-info-function)
373
'haskell-doc-current-info)
374
;; Set things up for imenu.
375
(set (make-local-variable 'imenu-create-index-function)
376
'haskell-ds-create-imenu-index)
377
;; Set things up for font-lock.
378
(set (make-local-variable 'font-lock-defaults)
379
'(haskell-font-lock-choose-keywords
380
nil nil ((?\' . "w") (?_ . "w")) nil
381
(font-lock-syntactic-keywords
382
. haskell-font-lock-choose-syntactic-keywords)
383
(font-lock-syntactic-face-function
384
. haskell-syntactic-face-function)
385
;; Get help from font-lock-syntactic-keywords.
386
(parse-sexp-lookup-properties . t)))
387
;; Haskell's layout rules mean that TABs have to be handled with extra care.
388
;; The safer option is to avoid TABs. The second best is to make sure
389
;; TABs stops are 8 chars apart, as mandated by the Haskell Report. --Stef
390
(set (make-local-variable 'indent-tabs-mode) nil)
391
(set (make-local-variable 'tab-width) 8))
393
(defcustom haskell-mode-hooks nil
394
"Hook run after entering Haskell mode."
396
:options '(turn-on-haskell-indent turn-on-font-lock turn-on-eldoc-mode
397
imenu-add-menubar-index))
399
;; The main mode functions
401
(defun haskell-mode ()
402
"Major mode for editing Haskell programs. Last adapted for Haskell 1.4.
403
Blank lines separate paragraphs, comments start with `-- '.
405
\\<haskell-mode-map>\\[indent-for-comment] will place a comment at an appropriate place on the current line.
406
\\[comment-region] comments (or with prefix arg, uncomments) each line in the region.
408
Literate scripts are supported via `literate-haskell-mode'. The
409
variable `haskell-literate' indicates the style of the script in the
410
current buffer. See the documentation on this variable for more
413
Modules can hook in via `haskell-mode-hook'. The following modules
414
are supported with an `autoload' command:
416
`haskell-decl-scan', Graeme E Moss
417
Scans top-level declarations, and places them in a menu.
419
`haskell-doc', Hans-Wolfgang Loidl
420
Echoes types of functions or syntax of keywords when the cursor is idle.
422
`haskell-indent', Guy Lapalme
423
Intelligent semi-automatic indentation.
425
`haskell-simple-indent', Graeme E Moss and Heribert Schuetz
428
Module X is activated using the command `turn-on-X'. For example,
429
`haskell-font-lock' is activated using `turn-on-haskell-font-lock'.
430
For more information on a module, see the help for its `turn-on-X'
431
function. Some modules can be deactivated using `turn-off-X'. (Note
432
that `haskell-doc' is irregular in using `turn-(on/off)-haskell-doc-mode'.)
434
Use `haskell-version' to find out what version this is.
436
Invokes `haskell-mode-hook' if not nil."
439
(haskell-mode-generic nil))
442
(defun literate-haskell-mode ()
443
"As `haskell-mode' but for literate scripts."
446
(haskell-mode-generic
448
(goto-char (point-min))
450
((re-search-forward "^\\\\\\(begin\\|end\\){code}$" nil t) 'latex)
451
((re-search-forward "^>" nil t) 'bird)
452
(t haskell-literate-default)))))
454
(defun haskell-mode-generic (literate)
455
"Common part of `haskell-mode' and `literate-haskell-mode'.
456
Former calls this with LITERATE nil. Latter calls with LITERATE `bird' or
460
(setq major-mode 'haskell-mode)
461
(setq mode-name "Haskell")
462
(setq haskell-literate literate)
463
(use-local-map haskell-mode-map)
464
(set-syntax-table haskell-mode-syntax-table)
465
(run-hooks 'haskell-mode-hook))
467
;;;###autoload(add-to-list 'auto-mode-alist '("\\.\\(?:[gh]s\\|hi\\)\\'" . haskell-mode))
468
;;;###autoload(add-to-list 'auto-mode-alist '("\\.l[gh]s\\'" . literate-haskell-mode))
469
;;;###autoload(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
470
;;;###autoload(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
472
;; Provide ourselves:
474
(provide 'haskell-mode)
476
;;; haskell-mode.el ends here