Initial Commit
[packages] / xemacs-packages / haskell-mode / haskell-ghci.el
1 ;;; haskell-ghci.el --- A GHCi interaction mode
2
3 ;; Copyright (C) 2004, 2005  Free Software Foundation, Inc.
4 ;; Copyright (C) 2001  Chris Webb
5 ;; Copyright (C) 1998, 1999  Guy Lapalme
6
7 ;; Keywords: inferior mode, GHCi interaction mode, Haskell
8
9 ;;; This file is not part of GNU Emacs.
10
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 \f
27 ;;; Commentary:
28
29 ;; Purpose:
30 ;;
31 ;; To send a Haskell buffer to another buffer running a GHCi
32 ;; interpreter.
33 ;;
34 ;; This mode is derived from version 1.1 of Guy Lapalme's
35 ;; haskell-hugs.el, which can be obtained from:
36 ;;
37 ;;   http://www.iro.umontreal.ca/~lapalme/Hugs-interaction.html
38 ;;
39 ;; This in turn was adapted from Chris Van Humbeeck's hugs-mode.el,
40 ;; which can be obtained from:
41 ;;
42 ;;   http://www-i2.informatik.rwth-aachen.de/Forschung/FP/Haskell/hugs-mode.el
43 ;;
44 ;;
45 ;; Installation:
46 ;;
47 ;; To use with Moss and Thorn's haskell-mode.el
48 ;;
49 ;;   http://www.haskell.org/haskell-mode
50 ;;
51 ;; add this to .emacs:
52 ;;
53 ;;   (add-hook haskell-mode-hook 'turn-on-haskell-ghci)
54 ;;
55 ;;
56 ;; Customisation:
57 ;;
58 ;; The name of the GHCi interpreter is in haskell-ghci-program-name.
59 ;;
60 ;; Arguments can be sent to the GHCi interpreter when it is started by
61 ;; setting haskell-ghci-program-args (empty by default) to a list of
62 ;; string args to pass it.  This value can be set interactively by
63 ;; calling C-c C-s with an argument (i.e. C-u C-c C-s).
64 ;;
65 ;; `haskell-ghci-hook' is invoked in the *ghci* buffer once GHCi is
66 ;; started.
67 ;;
68 ;; All functions/variables start with `turn-{on,off}-haskell-ghci' or
69 ;; `haskell-ghci-'.
70
71 ;;; Code:
72
73 (defgroup haskell-ghci nil
74   "Major mode for interacting with an inferior GHCi session."
75   :group 'haskell
76   :prefix "haskell-ghci-")
77
78 (defun turn-on-haskell-ghci ()
79   "Turn on Haskell interaction mode with a GHCi interpreter running in an
80 another Emacs buffer named *ghci*.
81 Maps the following commands in the haskell keymap:
82     \\<haskell-mode-map>\\[haskell-ghci-start-process] to create the GHCi buffer and start a GHCi process in it.
83     \\[haskell-ghci-load-file] to save the current buffer and load it by sending the :load command to GHCi.
84     \\[haskell-ghci-reload-file] to send the :reload command to GHCi without saving the buffer.
85     \\[haskell-ghci-show-ghci-buffer] to show the GHCi buffer and go to it."
86   (interactive)
87   (local-set-key "\C-c\C-s" 'haskell-ghci-start-process)
88   (local-set-key "\C-c\C-l" 'haskell-ghci-load-file)
89   (local-set-key "\C-c\C-r" 'haskell-ghci-reload-file)
90   (local-set-key "\C-c\C-n" 'haskell-ghci-locate-next-error)
91   (local-set-key "\C-c\C-b" 'haskell-ghci-show-ghci-buffer))
92
93 (defun turn-off-haskell-ghci ()
94   "Turn off Haskell interaction mode with a GHCi interpreter within a buffer."
95   (interactive)
96   (local-unset-key  "\C-c\C-s")
97   (local-unset-key  "\C-c\C-l")
98   (local-unset-key  "\C-c\C-r")
99   (local-unset-key  "\C-c\C-b"))
100
101 (defun haskell-ghci-mode ()
102   "Major mode for interacting with an inferior GHCi session.
103
104 The commands available from within a Haskell script are:
105     \\<haskell-mode-map>\\[haskell-ghci-start-process] to create the GHCi buffer and start a GHCi process in it.
106     \\[haskell-ghci-load-file] to save the current buffer and load it by sending the :load command to GHCi.
107     \\[haskell-ghci-reload-file] to send the :reload command to GHCi without saving the buffer.
108     \\[haskell-ghci-show-ghci-buffer] to show the GHCi buffer and go to it.
109
110 \\<haskell-ghci-mode-map>Commands:
111 \\[comint-send-input] after end of GHCi output sends line as input to GHCi.
112 \\[comint-send-input] before end of GHCI output copies rest of line and sends it to GHCI as input.
113 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
114 \\[comint-interrupt-subjob] interrupts the comint or its current subjob if any.
115 \\[comint-stop-subjob] stops, likewise. \\[comint-quit-subjob] sends quit signal."
116   (interactive)
117   (comint-mode)
118   (setq major-mode 'haskell-ghci-mode)
119   (setq mode-name "Haskell GHCi")
120   (if haskell-ghci-mode-map
121       nil
122     (setq haskell-ghci-mode-map (copy-keymap comint-mode-map)))
123   (use-local-map haskell-ghci-mode-map))
124
125
126 ;; GHCi interface:
127
128 (require 'comint)
129 (require 'shell)
130
131 (defvar haskell-ghci-process nil
132   "The active GHCi subprocess corresponding to current buffer.")
133
134 (defvar haskell-ghci-process-buffer nil
135   "*Buffer used for communication with GHCi subprocess for current buffer.")
136
137 (defcustom haskell-ghci-program-name "ghci"
138   "*The name of the GHCi interpreter program."
139   :type 'string
140   :group 'haskell-ghci)
141
142 (defcustom haskell-ghci-program-args nil
143   "*A list of string args to pass when starting the GHCi interpreter."
144   :type '(repeat string)
145   :group 'haskell-ghci)
146
147 (defvar haskell-ghci-load-end nil
148   "Position of the end of the last load command.")
149
150 (defvar haskell-ghci-error-pos nil
151   "Position of the end of the last load command.")
152
153 (defvar haskell-ghci-send-end nil
154   "Position of the end of the last send command.")
155
156 (defun haskell-ghci-start-process (arg)
157   "Start a GHCi process and invoke `haskell-ghci-hook' if not nil.
158 Prompt for a list of args if called with an argument."
159   (interactive "P")
160   (if arg
161       ;; XXX [CDW] Fix to use more natural 'string' version of the
162       ;; XXX arguments rather than a sexp.
163       (setq haskell-ghci-program-args
164             (read-minibuffer (format "List of args for %s:"
165                                      haskell-ghci-program-name)
166                              (prin1-to-string haskell-ghci-program-args))))
167
168   ;; Start the GHCi process in a new comint buffer.
169   (message "Starting GHCi process `%s'." haskell-ghci-program-name)
170   (setq haskell-ghci-process-buffer
171         (apply 'make-comint
172                "ghci" haskell-ghci-program-name nil
173                haskell-ghci-program-args))
174   (setq haskell-ghci-process
175         (get-buffer-process haskell-ghci-process-buffer))
176
177   ;; Select GHCi buffer temporarily.
178   (set-buffer haskell-ghci-process-buffer)
179   (haskell-ghci-mode)
180   (make-local-variable 'shell-cd-regexp)
181   (make-local-variable 'shell-dirtrackp)
182
183   ;; Track directory changes using the `:cd' command.
184   (setq shell-cd-regexp ":cd")
185   (setq shell-dirtrackp t)
186   (add-hook 'comint-input-filter-functions 'shell-directory-tracker)
187
188   ;; GHCi prompt should be of the form `ModuleName> '.
189   (setq comint-prompt-regexp  "^\\*?[A-Z][\\._a-zA-Z0-9]*\\( \\*?[A-Z][\\._a-zA-Z0-9]*\\)*> ")
190
191   ;; History syntax of comint conflicts with Haskell, e.g. !!, so better
192   ;; turn it off.
193   (setq comint-input-autoexpand nil)
194   (run-hooks 'haskell-ghci-hook)
195
196   ;; Clear message area.
197   (message ""))
198
199 (defun haskell-ghci-wait-for-output ()
200   "Wait until output arrives and go to the last input."
201   (while (progn                 
202            (goto-char comint-last-input-end)
203            (not (re-search-forward comint-prompt-regexp nil t)))
204     (accept-process-output haskell-ghci-process)))
205
206 (defun haskell-ghci-send (&rest string)
207   "Send `haskell-ghci-process' the arguments (one or more strings).
208 A newline is sent after the strings and they are inserted into the
209 current buffer after the last output."
210   (haskell-ghci-wait-for-output)        ; wait for prompt
211   (goto-char (point-max))               ; position for this input
212   (apply 'insert string)
213   (comint-send-input)
214   (setq haskell-ghci-send-end (marker-position comint-last-input-end)))
215
216 (defun haskell-ghci-go (load-command cd)
217   "Save the current buffer and load its file into the GHCi process.
218 The first argument LOAD-COMMAND specifies how the file should be
219 loaded: as a new file (\":load \") or as a reload (\":reload \").
220
221 If the second argument CD is non-nil, change directory in the GHCi
222 process to the current buffer's directory before loading the file.
223
224 If the variable `haskell-ghci-command' is set then its value will be
225 sent to the GHCi process after the load command. This can be used for a
226 top-level expression to evaluate."
227   (hack-local-variables)                ; in case they've changed
228   (save-buffer)
229   (let ((file (if (string-equal load-command ":load ")
230                   (concat "\"" buffer-file-name "\"")
231                 ""))
232         (dir (expand-file-name default-directory))
233         (cmd (and (boundp 'haskell-ghci-command)
234                   haskell-ghci-command
235                   (if (stringp haskell-ghci-command)
236                       haskell-ghci-command
237                     (symbol-name haskell-ghci-command)))))
238     (if (and haskell-ghci-process-buffer
239              (eq (process-status haskell-ghci-process) 'run))
240         ;; Ensure the GHCi buffer is selected.
241         (set-buffer haskell-ghci-process-buffer) 
242       ;; Start Haskell-GHCi process.
243       (haskell-ghci-start-process nil))
244
245     (if cd (haskell-ghci-send (concat ":cd " dir)))
246     ;; Wait until output arrives and go to the last input.
247     (haskell-ghci-wait-for-output)
248     (haskell-ghci-send load-command file)
249     ;; Error message search starts from last load command.
250     (setq haskell-ghci-load-end (marker-position comint-last-input-end))
251     (setq haskell-ghci-error-pos haskell-ghci-load-end)
252     (if cmd (haskell-ghci-send cmd))
253     ;; Wait until output arrives and go to the last input.
254     (haskell-ghci-wait-for-output)))
255
256 (defun haskell-ghci-load-file (cd)
257   "Save a ghci buffer file and load its file.
258 If CD (prefix argument if interactive) is non-nil, change directory in
259 the GHCi process to the current buffer's directory before loading the
260 file. If there is an error, set the cursor at the error line otherwise
261 show the *ghci* buffer."
262   (interactive "P")
263   (haskell-ghci-gen-load-file ":load " cd))
264
265 (defun haskell-ghci-reload-file (cd)
266   "Save a ghci buffer file and load its file.
267 If CD (prefix argument if interactive) is non-nil, change the GHCi
268 process to the current buffer's directory before loading the file.
269 If there is an error, set the cursor at the error line otherwise show
270 the *ghci* buffer."
271   (interactive "P")
272   (haskell-ghci-gen-load-file ":reload " cd))
273
274 (defun haskell-ghci-gen-load-file (cmd cd)
275   "Save a ghci buffer file and load its file or reload depending on CMD.
276 If CD is non-nil, change the process to the current buffer's directory
277 before loading the file. If there is an error, set the cursor at the
278 error line otherwise show the *ghci* buffer."
279
280   ;; Execute (re)load command.
281   (save-excursion (haskell-ghci-go cmd cd))
282
283   ;; Show *ghci* buffer.
284   (pop-to-buffer haskell-ghci-process-buffer)
285   (goto-char haskell-ghci-load-end)
286
287   ;; Did we finish loading without error?
288   (if (re-search-forward
289        "^Ok, modules loaded" nil t)
290       (progn (goto-char (point-max))
291              (recenter 2)
292              (message "There were no errors."))
293
294     ;; Something went wrong. If possible, be helpful and pinpoint the
295     ;; first error in the file whilst leaving the error visible in the
296     ;; *ghci* buffer.
297     (goto-char haskell-ghci-load-end)
298     (haskell-ghci-locate-next-error)))
299
300
301 (defun haskell-ghci-locate-next-error () 
302   "Go to the next error shown in the *ghci* buffer."
303   (interactive)
304   (if (buffer-live-p haskell-ghci-process-buffer)
305       (progn (pop-to-buffer haskell-ghci-process-buffer)
306              (goto-char haskell-ghci-error-pos)
307              (if (re-search-forward
308                   "^[^\/]*\\([^:\n]+\\):\\([0-9]+\\)" nil t)
309                  (let ((efile (buffer-substring (match-beginning 1)
310                                                 (match-end 1)))
311                        (eline (string-to-int 
312                                (buffer-substring (match-beginning 2)
313                                                  (match-end 2)))))
314
315                    (recenter 2)
316                    (setq haskell-ghci-error-pos (point))
317                    (message "GHCi error on line %d of %s."
318                    eline (file-name-nondirectory efile))
319                    (if (file-exists-p efile)
320                        (progn (find-file-other-window efile)
321                               (goto-line eline)
322                               (recenter))))
323
324       ;; We got an error without a file and line number, so put the
325       ;; point at end of the *ghci* buffer ready to deal with it.
326                (goto-char (point-max))
327                (recenter -2)
328                (message "No more errors found.")))
329     (message "No *ghci* buffer found.")))     
330
331 (defun haskell-ghci-show-ghci-buffer ()
332   "Go to the *ghci* buffer."
333   (interactive)
334   (if (or (not haskell-ghci-process-buffer)
335           (not (buffer-live-p haskell-ghci-process-buffer)))
336       (haskell-ghci-start-process nil))
337   (pop-to-buffer  haskell-ghci-process-buffer))
338
339 (provide 'haskell-ghci)                 
340
341 ;;; haskell-ghci.el ends here