haskell-ghci.el
1 ;;; haskell-ghci.el --- A GHCi interaction mode 2 3 ;; Copyright (C) 2004, 2005, 2006 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 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 (local-set-key "\C-c\C-s" 'haskell-ghci-start-process) 87 (local-set-key "\C-c\C-l" 'haskell-ghci-load-file) 88 (local-set-key "\C-c\C-r" 'haskell-ghci-reload-file) 89 (local-set-key "\C-c\C-n" 'haskell-ghci-locate-next-error) 90 (local-set-key "\C-c\C-b" 'haskell-ghci-show-ghci-buffer)) 91 92 (defun turn-off-haskell-ghci () 93 "Turn off Haskell interaction mode with a GHCi interpreter within a buffer." 94 (local-unset-key "\C-c\C-s") 95 (local-unset-key "\C-c\C-l") 96 (local-unset-key "\C-c\C-r") 97 (local-unset-key "\C-c\C-b")) 98 99 (define-derived-mode haskell-ghci-mode comint-mode "Haskell GHCi" 100 "Major mode for interacting with an inferior GHCi session. 101 102 The commands available from within a Haskell script are: 103 \\<haskell-mode-map>\\[haskell-ghci-start-process] to create the GHCi buffer and start a GHCi process in it. 104 \\[haskell-ghci-load-file] to save the current buffer and load it by sending the :load command to GHCi. 105 \\[haskell-ghci-reload-file] to send the :reload command to GHCi without saving the buffer. 106 \\[haskell-ghci-show-ghci-buffer] to show the GHCi buffer and go to it. 107 108 \\<haskell-ghci-mode-map>Commands: 109 \\[comint-send-input] after end of GHCi output sends line as input to GHCi. 110 \\[comint-send-input] before end of GHCI output copies rest of line and sends it to GHCI as input. 111 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing. 112 \\[comint-interrupt-subjob] interrupts the comint or its current subjob if any. 113 \\[comint-stop-subjob] stops, likewise. \\[comint-quit-subjob] sends quit signal.") 114 115 116 ;; GHCi interface: 117 118 (require 'comint) 119 (require 'shell) 120 121 (defvar haskell-ghci-process nil 122 "The active GHCi subprocess corresponding to current buffer.") 123 124 (defvar haskell-ghci-process-buffer nil 125 "*Buffer used for communication with GHCi subprocess for current buffer.") 126 127 (defcustom haskell-ghci-program-name "ghci" 128 "*The name of the GHCi interpreter program." 129 :type 'string 130 :group 'haskell-ghci) 131 132 (defcustom haskell-ghci-program-args nil 133 "*A list of string args to pass when starting the GHCi interpreter." 134 :type '(repeat string) 135 :group 'haskell-ghci) 136 137 (defvar haskell-ghci-load-end nil 138 "Position of the end of the last load command.") 139 140 (defvar haskell-ghci-error-pos nil 141 "Position of the end of the last load command.") 142 143 (defvar haskell-ghci-send-end nil 144 "Position of the end of the last send command.") 145 146 (defun haskell-ghci-start-process (arg) 147 "Start a GHCi process and invoke `haskell-ghci-hook' if not nil. 148 Prompt for a list of args if called with an argument." 149 (interactive "P") 150 (if arg 151 ;; XXX [CDW] Fix to use more natural 'string' version of the 152 ;; XXX arguments rather than a sexp. 153 (setq haskell-ghci-program-args 154 (read-minibuffer (format "List of args for %s:" 155 haskell-ghci-program-name) 156 (prin1-to-string haskell-ghci-program-args)))) 157 158 ;; Start the GHCi process in a new comint buffer. 159 (message "Starting GHCi process `%s'." haskell-ghci-program-name) 160 (setq haskell-ghci-process-buffer 161 (apply 'make-comint 162 "ghci" haskell-ghci-program-name nil 163 haskell-ghci-program-args)) 164 (setq haskell-ghci-process 165 (get-buffer-process haskell-ghci-process-buffer)) 166 167 ;; Select GHCi buffer temporarily. 168 (set-buffer haskell-ghci-process-buffer) 169 (haskell-ghci-mode) 170 (make-local-variable 'shell-cd-regexp) 171 (make-local-variable 'shell-dirtrackp) 172 173 ;; Track directory changes using the `:cd' command. 174 (setq shell-cd-regexp ":cd") 175 (setq shell-dirtrackp t) 176 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil 'local) 177 178 ;; GHCi prompt should be of the form `ModuleName> '. 179 (setq comint-prompt-regexp "^\\*?[A-Z][\\._a-zA-Z0-9]*\\( \\*?[A-Z][\\._a-zA-Z0-9]*\\)*> ") 180 181 ;; History syntax of comint conflicts with Haskell, e.g. !!, so better 182 ;; turn it off. 183 (setq comint-input-autoexpand nil) 184 (run-hooks 'haskell-ghci-hook) 185 186 ;; Clear message area. 187 (message "")) 188 189 (defun haskell-ghci-wait-for-output () 190 "Wait until output arrives and go to the last input." 191 (while (progn 192 (goto-char comint-last-input-end) 193 (not (re-search-forward comint-prompt-regexp nil t))) 194 (accept-process-output haskell-ghci-process))) 195 196 (defun haskell-ghci-send (&rest string) 197 "Send `haskell-ghci-process' the arguments (one or more strings). 198 A newline is sent after the strings and they are inserted into the 199 current buffer after the last output." 200 (haskell-ghci-wait-for-output) ; wait for prompt 201 (goto-char (point-max)) ; position for this input 202 (apply 'insert string) 203 (comint-send-input) 204 (setq haskell-ghci-send-end (marker-position comint-last-input-end))) 205 206 (defun haskell-ghci-go (load-command cd) 207 "Save the current buffer and load its file into the GHCi process. 208 The first argument LOAD-COMMAND specifies how the file should be 209 loaded: as a new file (\":load \") or as a reload (\":reload \"). 210 211 If the second argument CD is non-nil, change directory in the GHCi 212 process to the current buffer's directory before loading the file. 213 214 If the variable `haskell-ghci-command' is set then its value will be 215 sent to the GHCi process after the load command. This can be used for a 216 top-level expression to evaluate." 217 (hack-local-variables) ; in case they've changed 218 (save-buffer) 219 (let ((file (if (string-equal load-command ":load ") 220 (concat "\"" buffer-file-name "\"") 221 "")) 222 (dir (expand-file-name default-directory)) 223 (cmd (and (boundp 'haskell-ghci-command) 224 haskell-ghci-command 225 (if (stringp haskell-ghci-command) 226 haskell-ghci-command 227 (symbol-name haskell-ghci-command))))) 228 (if (and haskell-ghci-process-buffer 229 (eq (process-status haskell-ghci-process) 'run)) 230 ;; Ensure the GHCi buffer is selected. 231 (set-buffer haskell-ghci-process-buffer) 232 ;; Start Haskell-GHCi process. 233 (haskell-ghci-start-process nil)) 234 235 (if cd (haskell-ghci-send (concat ":cd " dir))) 236 ;; Wait until output arrives and go to the last input. 237 (haskell-ghci-wait-for-output) 238 (haskell-ghci-send load-command file) 239 ;; Error message search starts from last load command. 240 (setq haskell-ghci-load-end (marker-position comint-last-input-end)) 241 (setq haskell-ghci-error-pos haskell-ghci-load-end) 242 (if cmd (haskell-ghci-send cmd)) 243 ;; Wait until output arrives and go to the last input. 244 (haskell-ghci-wait-for-output))) 245 246 (defun haskell-ghci-load-file (cd) 247 "Save a ghci buffer file and load its file. 248 If CD (prefix argument if interactive) is non-nil, change directory in 249 the GHCi process to the current buffer's directory before loading the 250 file. If there is an error, set the cursor at the error line otherwise 251 show the *ghci* buffer." 252 (interactive "P") 253 (haskell-ghci-gen-load-file ":load " cd)) 254 255 (defun haskell-ghci-reload-file (cd) 256 "Save a ghci buffer file and load its file. 257 If CD (prefix argument if interactive) is non-nil, change the GHCi 258 process to the current buffer's directory before loading the file. 259 If there is an error, set the cursor at the error line otherwise show 260 the *ghci* buffer." 261 (interactive "P") 262 (haskell-ghci-gen-load-file ":reload " cd)) 263 264 (defun haskell-ghci-gen-load-file (cmd cd) 265 "Save a ghci buffer file and load its file or reload depending on CMD. 266 If CD is non-nil, change the process to the current buffer's directory 267 before loading the file. If there is an error, set the cursor at the 268 error line otherwise show the *ghci* buffer." 269 270 ;; Execute (re)load command. 271 (save-excursion (haskell-ghci-go cmd cd)) 272 273 ;; Show *ghci* buffer. 274 (pop-to-buffer haskell-ghci-process-buffer) 275 (goto-char haskell-ghci-load-end) 276 277 ;; Did we finish loading without error? 278 (if (re-search-forward 279 "^Ok, modules loaded" nil t) 280 (progn (goto-char (point-max)) 281 (recenter 2) 282 (message "There were no errors.")) 283 284 ;; Something went wrong. If possible, be helpful and pinpoint the 285 ;; first error in the file whilst leaving the error visible in the 286 ;; *ghci* buffer. 287 (goto-char haskell-ghci-load-end) 288 (haskell-ghci-locate-next-error))) 289 290 291 (defun haskell-ghci-locate-next-error () 292 "Go to the next error shown in the *ghci* buffer." 293 (interactive) 294 (if (buffer-live-p haskell-ghci-process-buffer) 295 (progn (pop-to-buffer haskell-ghci-process-buffer) 296 (goto-char haskell-ghci-error-pos) 297 (if (re-search-forward 298 "^[^\/]*\\([^:\n]+\\):\\([0-9]+\\)" nil t) 299 (let ((efile (buffer-substring (match-beginning 1) 300 (match-end 1))) 301 (eline (string-to-int 302 (buffer-substring (match-beginning 2) 303 (match-end 2))))) 304 305 (recenter 2) 306 (setq haskell-ghci-error-pos (point)) 307 (message "GHCi error on line %d of %s." 308 eline (file-name-nondirectory efile)) 309 (if (file-exists-p efile) 310 (progn (find-file-other-window efile) 311 (goto-line eline) 312 (recenter)))) 313 314 ;; We got an error without a file and line number, so put the 315 ;; point at end of the *ghci* buffer ready to deal with it. 316 (goto-char (point-max)) 317 (recenter -2) 318 (message "No more errors found."))) 319 (message "No *ghci* buffer found."))) 320 321 (defun haskell-ghci-show-ghci-buffer () 322 "Go to the *ghci* buffer." 323 (interactive) 324 (if (or (not haskell-ghci-process-buffer) 325 (not (buffer-live-p haskell-ghci-process-buffer))) 326 (haskell-ghci-start-process nil)) 327 (pop-to-buffer haskell-ghci-process-buffer)) 328 329 (provide 'haskell-ghci) 330 331 ;; arch-tag: f0bade4b-288d-4329-9791-98c1e24167ac 332 ;;; haskell-ghci.el ends here