1 ;;; haskell-hugs.el --- simplistic interaction mode with a
3 ;; Copyright 2004, 2005 Free Software Foundation, Inc.
4 ;; Copyright 1998, 1999 Guy Lapalme
6 ;; Hugs interpreter for Haskell developped by
7 ;; The University of Nottingham and Yale University, 1994-1997.
8 ;; Web: http://www.haskell.org/hugs.
9 ;; In standard Emacs terminology, this would be called
12 ;; Keywords: Hugs inferior mode, Hugs interaction mode
13 ;; URL: http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/CONTRIB/haskell-modes/emacs/haskell-hugs.el?rev=HEAD
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 send a Haskell buffer to another buffer running a Hugs interpreter
38 ;; The functions are adapted from the Hugs Mode developed by
39 ;; Chris Van Humbeeck <chris.vanhumbeeck@cs.kuleuven.ac.be>
40 ;; which can be obtained at:
41 ;; http://www-i2.informatik.rwth-aachen.de/Forschung/FP/Haskell/hugs-mode.el
45 ;; To use with the Haskell mode of
46 ;; Moss&Thorn <http://www.haskell.org/haskell-mode>
47 ;; add this to .emacs:
49 ;; (add-hook haskell-mode-hook 'turn-on-haskell-hugs)
52 ;; The name of the hugs interpreter is in variable
53 ;; haskell-hugs-program-name
54 ;; Arguments can be sent to the Hugs interpreter when it is called
55 ;; by setting the value of the variable
56 ;; haskell-hugs-program-args
57 ;; which by default contains '("+.") so that the progress of the
58 ;; interpreter is visible without any "^H" in the *hugs* Emacs buffer.
60 ;; This value can be interactively by calling C-cC-s with an
63 ;; If the command does not seem to respond, see the
64 ;; content of the `comint-prompt-regexp' variable
65 ;; to check that it waits for the appropriate Hugs prompt
66 ;; the current value is appropriate for Hugs 1.3 and 1.4
69 ;; `haskell-hugs-hook' is invoked in the *hugs* once it is started.
71 ;;; All functions/variables start with
72 ;;; `(turn-(on/off)-)haskell-hugs' or `haskell-hugs-'.
74 (defgroup haskell-hugs nil
75 "Major mode for interacting with an inferior Hugs session."
77 :prefix "haskell-hugs-")
79 (defun turn-on-haskell-hugs ()
80 "Turn on Haskell interaction mode with a Hugs interpreter running in an
81 another Emacs buffer named *hugs*.
82 Maps the followind commands in the haskell keymap.
83 \\[haskell-hugs-load-file]
84 to save the current buffer and load it by sending the :load command
86 \\[haskell-hugs-reload-file]
87 to send the :reload command to Hugs without saving the buffer.
88 \\[haskell-hugs-show-hugs-buffer]
89 to show the Hugs buffer and go to it."
91 (local-set-key "\C-c\C-s" 'haskell-hugs-start-process)
92 (local-set-key "\C-c\C-l" 'haskell-hugs-load-file)
93 (local-set-key "\C-c\C-r" 'haskell-hugs-reload-file)
94 (local-set-key "\C-c\C-b" 'haskell-hugs-show-hugs-buffer)
97 (defun turn-off-haskell-hugs ()
98 "Turn off Haskell interaction mode with a Hugs interpreter within a buffer."
100 (local-unset-key "\C-c\C-s")
101 (local-unset-key "\C-c\C-l")
102 (local-unset-key "\C-c\C-r")
103 (local-unset-key "\C-c\C-b")
106 (define-derived-mode haskell-hugs-mode comint-mode "Haskell Hugs"
107 ;; called by haskell-hugs-start-process,
108 ;; itself called by haskell-hugs-load-file
109 ;; only when the file is loaded the first time
110 "Major mode for interacting with an inferior Hugs session.
112 The commands available from within a Haskell script are:
113 \\<haskell-mode-map>\\[haskell-hugs-load-file]
114 to save the current buffer and load it by sending the :load command
116 \\[haskell-hugs-reload-file]
117 to send the :reload command to Hugs without saving the buffer.
118 \\[haskell-hugs-show-hugs-buffer]
119 to show the Hugs buffer and go to it.
121 \\<haskell-hugs-mode-map>
123 Return at end of buffer sends line as input.
124 Return not at end copies rest of line to end and sends it.
125 \\[comint-kill-input] and \\[backward-kill-word] are kill commands,
126 imitating normal Unix input editing.
127 \\[comint-interrupt-subjob] interrupts the comint or its current
129 \\[comint-stop-subjob] stops, likewise.
130 \\[comint-quit-subjob] sends quit signal."
138 (defvar haskell-hugs-process nil
139 "The active Hugs subprocess corresponding to current buffer.")
141 (defvar haskell-hugs-process-buffer nil
142 "*Buffer used for communication with Hugs subprocess for current buffer.")
144 (defcustom haskell-hugs-program-name "hugs"
145 "*The name of the command to start the Hugs interpreter."
147 :group 'haskell-hugs)
149 (defcustom haskell-hugs-program-args '("+.")
150 "*A list of string args to send to the hugs process."
151 :type '(repeat string)
152 :group 'haskell-hugs)
154 (defvar haskell-hugs-load-end nil
155 "Position of the end of the last load command.")
157 (defvar haskell-hugs-send-end nil
158 "Position of the end of the last send command.")
160 (defalias 'run-hugs 'haskell-hugs-start-process)
162 (defun haskell-hugs-start-process (arg)
163 "Start a Hugs process and invokes `haskell-hugs-hook' if not nil.
164 Prompts for a list of args if called with an argument."
166 (message "Starting `hugs-process' %s" haskell-hugs-program-name)
168 (setq haskell-hugs-program-args
169 (read-minibuffer "List of args for Hugs:"
170 (prin1-to-string haskell-hugs-program-args))))
171 (setq haskell-hugs-process-buffer
173 "hugs" haskell-hugs-program-name nil
174 haskell-hugs-program-args))
175 (setq haskell-hugs-process
176 (get-buffer-process haskell-hugs-process-buffer))
177 ;; Select Hugs buffer temporarily
178 (set-buffer haskell-hugs-process-buffer)
180 (make-local-variable 'shell-cd-regexp)
181 (make-local-variable 'shell-dirtrackp)
182 (setq shell-cd-regexp ":cd")
183 (setq shell-dirtrackp t)
184 (add-hook 'comint-input-filter-functions 'shell-directory-tracker)
185 ; ? or module name in Hugs 1.4
186 (setq comint-prompt-regexp "^\? \\|^[A-Z][_a-zA-Z0-9\.]*> ")
187 ;; comint's history syntax conflicts with Hugs syntax, eg. !!
188 (setq comint-input-autoexpand nil)
189 (run-hooks 'haskell-hugs-hook)
193 (defun haskell-hugs-wait-for-output ()
194 "Wait until output arrives and go to the last input."
196 (goto-char comint-last-input-end)
198 (not (re-search-forward comint-prompt-regexp nil t))
199 (accept-process-output haskell-hugs-process)))))
201 (defun haskell-hugs-send (&rest string)
202 "Send `haskell-hugs-process' the arguments (one or more strings).
203 A newline is sent after the strings and they are inserted into the
204 current buffer after the last output."
205 ;; Wait until output arrives and go to the last input.
206 (haskell-hugs-wait-for-output)
207 ;; Position for this input.
208 (goto-char (point-max))
209 (apply 'insert string)
211 (setq haskell-hugs-send-end (marker-position comint-last-input-end)))
213 (defun haskell-hugs-go (load-command cd)
214 "Save the current buffer and load its file into the Hugs process.
215 The first argument LOAD-COMMAND specifies how the file should be
216 loaded: as a new file (\":load \") or as a reload (\":reload \").
218 If the second argument CD is non-nil, change the Haskell-Hugs process to the
219 current buffer's directory before loading the file.
221 If the variable `haskell-hugs-command' is set then its value will be sent to
222 the Hugs process after the load command. This can be used for a
223 top-level expression to evaluate."
224 (hack-local-variables) ;; In case they've changed
226 (let ((file (if (string-equal load-command ":load ")
227 (concat "\"" buffer-file-name "\"")
229 (dir (expand-file-name default-directory))
230 (cmd (and (boundp 'haskell-hugs-command)
232 (if (stringp haskell-hugs-command)
234 (symbol-name haskell-hugs-command)))))
235 (if (and haskell-hugs-process-buffer
236 (eq (process-status haskell-hugs-process) 'run))
237 ;; Ensure the Hugs buffer is selected.
238 (set-buffer haskell-hugs-process-buffer)
239 ;; Start Haskell-Hugs process.
240 (haskell-hugs-start-process nil))
242 (if cd (haskell-hugs-send (concat ":cd " dir)))
243 ;; Wait until output arrives and go to the last input.
244 (haskell-hugs-wait-for-output)
245 (haskell-hugs-send load-command file)
246 ;; Error message search starts from last load command.
247 (setq haskell-hugs-load-end (marker-position comint-last-input-end))
248 (if cmd (haskell-hugs-send cmd))
249 ;; Wait until output arrives and go to the last input.
250 (haskell-hugs-wait-for-output)))
252 (defun haskell-hugs-load-file (cd)
253 "Save a hugs buffer file and load its file.
254 If CD (prefix argument if interactive) is non-nil, change the Hugs
255 process to the current buffer's directory before loading the file.
256 If there is an error, set the cursor at the error line otherwise show
259 (haskell-hugs-gen-load-file ":load " cd)
262 (defun haskell-hugs-reload-file (cd)
263 "Save a hugs buffer file and load its file.
264 If CD (prefix argument if interactive) is non-nil, change the Hugs
265 process to the current buffer's directory before loading the file.
266 If there is an error, set the cursor at the error line otherwise show
269 (haskell-hugs-gen-load-file ":reload " cd)
272 (defun haskell-hugs-gen-load-file (cmd cd)
273 "Save a hugs buffer file and load its file or reload depending on CMD.
274 If CD is non-nil, change the process to the current buffer's directory
275 before loading the file. If there is an error, set the cursor at the
276 error line otherwise show the Hugs buffer."
277 (save-excursion (haskell-hugs-go cmd cd))
278 ;; Ensure the Hugs buffer is selected.
279 (set-buffer haskell-hugs-process-buffer)
280 ;; Error message search starts from last load command.
281 (goto-char haskell-hugs-load-end)
282 (if (re-search-forward
283 "^ERROR \"\\([^ ]*\\)\"\\( (line \\([0-9]*\\))\\|\\)" nil t)
284 (let ((efile (buffer-substring (match-beginning 1)
286 (eline (if (match-beginning 3)
287 (string-to-int (buffer-substring (match-beginning 3)
289 (emesg (buffer-substring (1+ (point))
290 (save-excursion (end-of-line) (point)))))
291 (pop-to-buffer haskell-hugs-process-buffer) ; show *hugs* buffer