~ubuntu-branches/ubuntu/hardy/haskell-mode/hardy

« back to all changes in this revision

Viewing changes to haskell-mode.el~

  • Committer: Bazaar Package Importer
  • Author(s): Petr Rockai
  • Date: 2007-12-06 01:02:06 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206010206-amqror2nijod4wos
Tags: 2.3-1
* New upstream release (closes: #443400, #448658, #411955),
* re-debianized, using cdbs and debhelper,
* enabling the byte-compile of emacs-lisp sources (closes:  #408260),
* new maintainer, keeping Isaac as co-maintainer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
;;; haskell-mode.el --- A Haskell editing mode    -*-coding: iso-8859-1;-*-
2
 
 
3
 
;; Copyright (C) 2003, 2004  Free Software Foundation, Inc
4
 
;; Copyright (C) 1992, 1997-1998 Simon Marlow, Graeme E Moss, and Tommy Thorn
5
 
 
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
12
 
;; Version: 1.43
13
 
;; URL: http://www.haskell.org/haskell-mode/
14
 
 
15
 
;;; This file is not part of GNU Emacs.
16
 
 
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)
20
 
;; any later version.
21
 
 
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.
26
 
 
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.
31
 
 
32
 
 
33
 
;;; Commentary:
34
 
 
35
 
;; Purpose:
36
 
;;
37
 
;; To provide a pleasant mode to browse and edit Haskell files, linking
38
 
;; into the following supported modules:
39
 
;;
40
 
;; `haskell-font-lock', Graeme E Moss and Tommy Thorn
41
 
;;   Fontifies standard Haskell keywords, symbols, functions, etc.
42
 
;;
43
 
;; `haskell-decl-scan', Graeme E Moss
44
 
;;   Scans top-level declarations, and places them in a menu.
45
 
;;
46
 
;; `haskell-doc', Hans-Wolfgang Loidl
47
 
;;   Echoes types of functions or syntax of keywords when the cursor is idle.
48
 
;;
49
 
;; `haskell-indent', Guy Lapalme
50
 
;;   Intelligent semi-automatic indentation.
51
 
;;
52
 
;; `haskell-simple-indent', Graeme E Moss and Heribert Schuetz
53
 
;;   Simple indentation.
54
 
;;
55
 
;; `inf-haskell'
56
 
;;   Interaction with an inferior Haskell process.
57
 
;;   It replaces the previous two modules:
58
 
;;     `haskell-hugs', Guy Lapalme
59
 
;;     `haskell-ghci', Chris Web
60
 
;;
61
 
;;
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.
64
 
;;
65
 
;; Installation:
66
 
;; 
67
 
;; Put in your ~/.emacs:
68
 
;;
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))))
74
 
;;
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)
79
 
;;
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:
83
 
;;
84
 
;;    (setq load-path (cons "~/lib/emacs" load-path))
85
 
;;
86
 
;; To turn any of the supported modules on for all buffers, add the
87
 
;; appropriate line(s) to .emacs:
88
 
;;
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)
93
 
;;
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.
96
 
;;
97
 
;;
98
 
;; Customisation:
99
 
;;
100
 
;; Set the value of `haskell-literate-default' to your preferred
101
 
;; literate style: 'bird or 'latex, within .emacs as follows:
102
 
;;
103
 
;;    (setq haskell-literate-default 'latex)
104
 
;;
105
 
;; Also see the customisations of the modules.
106
 
;;
107
 
;;
108
 
;; History:
109
 
;;
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.
112
 
;; 
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
119
 
;; or suggestion.
120
 
;;
121
 
;; Version 1.43:
122
 
;;   Various tweaks to doc strings and customization support from
123
 
;;   Ville Skytt� <scop@xemacs.org>.
124
 
;;
125
 
;; Version 1.42:
126
 
;;   Added autoload for GHCi inferior mode (thanks to Scott
127
 
;;   Williams for the bug report and fix).
128
 
;;
129
 
;; Version 1.41:
130
 
;;   Improved packaging, and made a couple more variables
131
 
;;   interactively settable.
132
 
;;
133
 
;; Version 1.4:
134
 
;;   Added GHCi mode from Chris Webb, and tidied up a little.
135
 
;;
136
 
;; Version 1.3:
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.
141
 
;;
142
 
;; Version 1.2:
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).
149
 
;;
150
 
;; Version 1.1:
151
 
;;   Added support for declaration scanning under XEmacs via
152
 
;;   func-menu.  Moved operators to level two fontification.
153
 
;;
154
 
;; Version 1.0:
155
 
;;   Added a nice indention support from Heribert Schuetz
156
 
;;   <Heribert.Schuetz@informatik.uni-muenchen.de>:
157
 
;;
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.
163
 
;;
164
 
;;   Cleaned up the imenu support.  Added support for literate scripts.
165
 
;;
166
 
;; Version 0.103 [HWL]:
167
 
;;   From Hans Wolfgang Loidl <hwloidl@dcs.gla.ac.uk>:
168
 
;;
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.
172
 
;;
173
 
;; Version 0.102:
174
 
;;
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.
177
 
;;
178
 
;; Version 0.101:
179
 
;;
180
 
;;   Altered indent-line-function to indent-relative.
181
 
;;
182
 
;; Version 0.100:
183
 
;; 
184
 
;;   First official release.
185
 
 
186
 
;; Present Limitations/Future Work (contributions are most welcome!):
187
 
;;
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
190
 
;;   "> " be better?
191
 
;;
192
 
;; . Support for GreenCard?
193
 
;;
194
 
 
195
 
;;; Code:
196
 
 
197
 
(eval-when-compile (require 'cl))
198
 
 
199
 
;; All functions/variables start with `(literate-)haskell-'.
200
 
 
201
 
;; Version of mode.
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."
206
 
  (interactive)
207
 
  (message "Using haskell-mode version %s" haskell-version))
208
 
 
209
 
(defgroup haskell nil
210
 
  "Major mode for editing Haskell programs."
211
 
  :group 'languages
212
 
  :prefix "haskell-")
213
 
 
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)
223
 
 
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")
228
 
 
229
 
;; Obsolete functions.
230
 
(defun turn-on-haskell-font-lock ()
231
 
  (interactive)
232
 
  (turn-on-font-lock)
233
 
  (message "turn-on-haskell-font-lock is obsolete.  Use turn-on-font-lock instead."))
234
 
(defun turn-on-haskell-hugs ()
235
 
  (interactive)
236
 
  (message "haskell-hugs is obsolete.")
237
 
  (load "haskell-hugs")
238
 
  (turn-on-haskell-hugs))
239
 
(defun turn-on-haskell-ghci ()
240
 
  (interactive)
241
 
  (message "haskell-ghci is obsolete.")
242
 
  (load "haskell-ghci")
243
 
  (turn-on-haskell-ghci))
244
 
 
245
 
 
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.
255
 
 
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
263
 
.emacs:
264
 
 
265
 
   (setq haskell-literate-default 'latex)"
266
 
  :type '(choice bird latex nil)
267
 
  :group 'haskell)
268
 
 
269
 
;; Mode maps.
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)
282
 
 
283
 
    ;; That's what M-; is for.
284
 
    ;; (define-key map "\C-c\C-c" 'comment-region)
285
 
    map)
286
 
  "Keymap used in Haskell mode.")
287
 
 
288
 
;; Syntax table.
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)
300
 
 
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))
307
 
          (t
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)
317
 
 
318
 
    (let (i lim)
319
 
      (map-char-table
320
 
       (lambda (k v)
321
 
         (when (equal v '(1))
322
 
           ;; The current Emacs 22 codebase can pass either a char
323
 
           ;; or a char range.
324
 
           (if (consp k)
325
 
               (setq i (car k)
326
 
                     lim (cdr k))
327
 
             (setq i k 
328
 
                   lim k))
329
 
           (while (<= i lim)
330
 
             (when (> i 127)
331
 
               (modify-syntax-entry i "_" table))
332
 
             (setq i (1+ i)))))
333
 
       (standard-syntax-table)))
334
 
    
335
 
    (modify-syntax-entry ?\` "$`" table)
336
 
    (modify-syntax-entry ?\\ "\\" table)
337
 
    (mapcar (lambda (x)
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.
343
 
      (mapcar (lambda (x)
344
 
                (modify-syntax-entry x "_" table))
345
 
              (concat "�������������������������������"
346
 
                      "��"))
347
 
      (mapcar (lambda (x)
348
 
                (modify-syntax-entry x "w" table))
349
 
              (concat "�����������������������"
350
 
                      "��������"
351
 
                      "�����������������������"
352
 
                      "��������")))
353
 
    table)
354
 
  "Syntax table used in Haskell mode.")
355
 
 
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))
392
 
 
393
 
(defcustom haskell-mode-hooks nil
394
 
  "Hook run after entering Haskell mode."
395
 
  :type 'hook
396
 
  :options '(turn-on-haskell-indent turn-on-font-lock turn-on-eldoc-mode
397
 
             imenu-add-menubar-index))
398
 
 
399
 
;; The main mode functions
400
 
;;;###autoload
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 `-- '.
404
 
 
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.
407
 
 
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
411
 
details.
412
 
 
413
 
Modules can hook in via `haskell-mode-hook'.  The following modules
414
 
are supported with an `autoload' command:
415
 
 
416
 
   `haskell-decl-scan', Graeme E Moss
417
 
     Scans top-level declarations, and places them in a menu.
418
 
 
419
 
   `haskell-doc', Hans-Wolfgang Loidl
420
 
     Echoes types of functions or syntax of keywords when the cursor is idle.
421
 
 
422
 
   `haskell-indent', Guy Lapalme
423
 
     Intelligent semi-automatic indentation.
424
 
 
425
 
   `haskell-simple-indent', Graeme E Moss and Heribert Schuetz
426
 
     Simple indentation.
427
 
 
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'.)
433
 
 
434
 
Use `haskell-version' to find out what version this is.
435
 
 
436
 
Invokes `haskell-mode-hook' if not nil."
437
 
 
438
 
  (interactive)
439
 
  (haskell-mode-generic nil))
440
 
 
441
 
;;;###autoload
442
 
(defun literate-haskell-mode ()
443
 
  "As `haskell-mode' but for literate scripts."
444
 
 
445
 
  (interactive)
446
 
  (haskell-mode-generic
447
 
   (save-excursion
448
 
     (goto-char (point-min))
449
 
     (cond
450
 
      ((re-search-forward "^\\\\\\(begin\\|end\\){code}$" nil t) 'latex)
451
 
      ((re-search-forward "^>" nil t) 'bird)
452
 
      (t haskell-literate-default)))))
453
 
 
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
457
 
`latex'."
458
 
 
459
 
  (haskell-vars)
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))
466
 
 
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)
471
 
 
472
 
;; Provide ourselves:
473
 
 
474
 
(provide 'haskell-mode)
475
 
 
476
 
;;; haskell-mode.el ends here