Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 657 lines (589 sloc) 28.61 kB
4418871 New file.
monnier authored
1 ;;; inf-haskell.el --- Interaction with an inferior Haskell process.
2
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
3 ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4418871 New file.
monnier authored
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: Haskell
7
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
30d658f (displayed-month): Remove declaration since it's not used here.
monnier authored
10 ;; the Free Software Foundation; either version 3, or (at your option)
4418871 New file.
monnier authored
11 ;; any later version.
12
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; The code is made of 2 parts: a major mode for the buffer that holds the
225a7c5 (inferior-haskell-load-file): Quote file name.
monnier authored
26 ;; inferior process's session and a minor mode for use in source buffers.
4418871 New file.
monnier authored
27
3e6c2e0 *** empty log message ***
monnier authored
28 ;; Todo:
29
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
30 ;; - Check out Shim for ideas.
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
31 ;; - i-h-load-buffer and i-h-send-region.
3e6c2e0 *** empty log message ***
monnier authored
32
4418871 New file.
monnier authored
33 ;;; Code:
34
35 (require 'comint)
36 (require 'shell) ;For directory tracking.
37 (require 'compile)
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
38 (require 'haskell-mode)
7410480 (inferior-haskell-wait-for-prompt): New fun, extracted
monnier authored
39 (eval-when-compile (require 'cl))
4418871 New file.
monnier authored
40
41 ;; Here I depart from the inferior-haskell- prefix.
42 ;; Not sure if it's a good idea.
27cc26c (haskell-program-name): Use ghci if hugs is absent.
monnier authored
43 (defcustom haskell-program-name
44 ;; Arbitrarily give preference to hugs over ghci.
45 (or (cond
46 ((not (fboundp 'executable-find)) nil)
47 ((executable-find "hugs") "hugs \"+.\"")
48 ((executable-find "ghci") "ghci"))
49 "hugs \"+.\"")
4418871 New file.
monnier authored
50 "The name of the command to start the inferior Haskell process.
51 The command can include arguments."
8b661ca (haskell-program-name): Fix defcustom delcaration.
monnier authored
52 ;; Custom only supports the :options keyword for a few types, e.g. not
53 ;; for string.
54 ;; :options '("hugs \"+.\"" "ghci")
4418871 New file.
monnier authored
55 :group 'haskell
56 :type '(choice string (repeat string)))
57
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
58 (defconst inferior-haskell-info-xref-re
59 "\t-- Defined at \\(.+\\):\\([0-9]+\\):\\([0-9]+\\)$")
60
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
61 (defconst inferior-haskell-module-re
62 "\t-- Defined in \\(.+\\)$"
63 "Regular expression for matching module names in :info.")
64
4418871 New file.
monnier authored
65 (defconst inferior-haskell-error-regexp-alist
66 ;; The format of error messages used by Hugs.
e19bf49 (inferior-haskell-error-regexp-alist): Fix GHCi regexp, support warni…
monnier authored
67 `(("^ERROR \"\\(.+?\\)\"\\(:\\| line \\)\\([0-9]+\\) - " 1 3)
4418871 New file.
monnier authored
68 ;; Format of error messages used by GHCi.
a9fe15b (inferior-haskell-error-regexp-alist): Be more careful
monnier authored
69 ("^\\(.+?\\):\\([0-9]+\\):\\(\\([0-9]+\\):\\)?\\( \\|\n *\\)\\(Warning\\)?"
70 1 2 4 ,@(if (fboundp 'compilation-fake-loc)
71 '((6) nil (5 '(face nil font-lock-multiline t)))))
5a72afa (inferior-haskell-error-regexp-alist): Add entries for GHCI's excepti…
monnier authored
72 ;; Runtime exceptions, from ghci.
73 ("^\\*\\*\\* Exception: \\(.+?\\):(\\([0-9]+\\),\\([0-9]+\\))-(\\([0-9]+\\),\\([0-9]+\\)): .*"
74 1 ,@(if (fboundp 'compilation-fake-loc) '((2 . 4) (3 . 5)) '(2 3)))
a9fe15b (inferior-haskell-error-regexp-alist): Be more careful
monnier authored
75 ;; GHCi uses two different forms for line/col ranges, depending on
ff46f40 (inferior-haskell-error-regexp-alist):
monnier authored
76 ;; whether it's all on the same line or not :-( In Emacs-23, I could use
77 ;; explicitly numbered subgroups to merge the two patterns.
5a72afa (inferior-haskell-error-regexp-alist): Add entries for GHCI's excepti…
monnier authored
78 ("^\\*\\*\\* Exception: \\(.+?\\):\\([0-9]+\\):\\([0-9]+\\)-\\([0-9]+\\): .*"
79 1 2 ,(if (fboundp 'compilation-fake-loc) '(3 . 4) 3))
a9fe15b (inferior-haskell-error-regexp-alist): Be more careful
monnier authored
80 ;; Info messages. Not errors per se.
ff46f40 (inferior-haskell-error-regexp-alist):
monnier authored
81 ,@(when (fboundp 'compilation-fake-loc)
82 `(;; Other GHCi patterns used in type errors.
83 ("^[ \t]+at \\(.+\\):\\([0-9]+\\):\\([0-9]+\\)-\\([0-9]+\\)$"
84 1 2 (3 . 4) 0)
85 ;; Foo.hs:318:80:
86 ;; Ambiguous occurrence `Bar'
87 ;; It could refer to either `Bar', defined at Zork.hs:311:5
88 ;; or `Bar', imported from Bars at Frob.hs:32:0-16
89 ;; (defined at Location.hs:97:5)
90 ("[ (]defined at \\(.+\\):\\([0-9]+\\):\\([0-9]+\\))?$" 1 2 3 0)
91 ("imported from .* at \\(.+\\):\\([0-9]+\\):\\([0-9]+\\)-\\([0-9]+\\)$"
92 1 2 (3 . 4) 0)
93 ;; Info xrefs.
94 (,inferior-haskell-info-xref-re 1 2 3 0))))
4418871 New file.
monnier authored
95 "Regexps for error messages generated by inferior Haskell processes.
96 The format should be the same as for `compilation-error-regexp-alist'.")
97
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
98 (defcustom inferior-haskell-find-project-root t
99 "If non-nil, try and find the project root directory of this file.
100 This will either look for a Cabal file or a \"module\" statement in the file."
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
101 :type 'boolean)
102
4418871 New file.
monnier authored
103 (define-derived-mode inferior-haskell-mode comint-mode "Inf-Haskell"
104 "Major mode for interacting with an inferior Haskell process."
105 (set (make-local-variable 'comint-prompt-regexp)
106 "^\\*?[A-Z][\\._a-zA-Z0-9]*\\( \\*?[A-Z][\\._a-zA-Z0-9]*\\)*> ")
107 (set (make-local-variable 'comint-input-autoexpand) nil)
108
109 ;; Setup directory tracking.
110 (set (make-local-variable 'shell-cd-regexp) ":cd")
bbbe054 (inferior-haskell-mode): Use shell-dirtrack-mode if possible.
monnier authored
111 (condition-case nil
112 (shell-dirtrack-mode 1)
113 (error ;The minor mode function may not exist or not accept an arg.
114 (set (make-local-variable 'shell-dirtrackp) t)
115 (add-hook 'comint-input-filter-functions 'shell-directory-tracker
116 nil 'local)))
4418871 New file.
monnier authored
117
118 ;; Setup `compile' support so you can just use C-x ` and friends.
119 (set (make-local-variable 'compilation-error-regexp-alist)
120 inferior-haskell-error-regexp-alist)
5a72afa (inferior-haskell-error-regexp-alist): Add entries for GHCI's excepti…
monnier authored
121 (set (make-local-variable 'compilation-first-column) 0) ;GHCI counts from 0.
bfd15e1 (inferior-haskell-mode): Hide compilation-mode bindings.
monnier authored
122 (if (and (not (boundp 'minor-mode-overriding-map-alist))
123 (fboundp 'compilation-shell-minor-mode))
124 ;; If we can't remove compilation-minor-mode bindings, at least try to
125 ;; use compilation-shell-minor-mode, so there are fewer
126 ;; annoying bindings.
127 (compilation-shell-minor-mode 1)
128 ;; Else just use compilation-minor-mode but without its bindings because
129 ;; things like mouse-2 are simply too annoying.
130 (compilation-minor-mode 1)
131 (let ((map (make-sparse-keymap)))
132 (dolist (keys '([menu-bar] [follow-link]))
133 ;; Preserve some of the bindings.
134 (define-key map keys (lookup-key compilation-minor-mode-map keys)))
135 (add-to-list 'minor-mode-overriding-map-alist
136 (cons 'compilation-minor-mode map)))))
4418871 New file.
monnier authored
137
6784a8b (inferior-haskell-string-to-strings): Remove `separator' argument. C…
monnier authored
138 (defun inferior-haskell-string-to-strings (string)
139 "Split the STRING into a list of strings."
140 (let ((i (string-match "[\"]" string)))
141 (if (null i) (split-string string) ; no quoting: easy
142 (append (unless (eq i 0) (split-string (substring string 0 i)))
4418871 New file.
monnier authored
143 (let ((rfs (read-from-string string i)))
144 (cons (car rfs)
145 (inferior-haskell-string-to-strings
6784a8b (inferior-haskell-string-to-strings): Remove `separator' argument. C…
monnier authored
146 (substring string (cdr rfs)))))))))
4418871 New file.
monnier authored
147
148 (defun inferior-haskell-command (arg)
149 (inferior-haskell-string-to-strings
150 (if (null arg) haskell-program-name
6dd0f3a (inferior-haskell-command): Provide a default.
monnier authored
151 (read-string "Command to run haskell: " haskell-program-name))))
4418871 New file.
monnier authored
152
153 (defvar inferior-haskell-buffer nil
154 "The buffer in which the inferior process is running.")
155
156 (defun inferior-haskell-start-process (command)
157 "Start an inferior haskell process.
6784a8b (inferior-haskell-string-to-strings): Remove `separator' argument. C…
monnier authored
158 With universal prefix \\[universal-argument], prompts for a COMMAND,
4418871 New file.
monnier authored
159 otherwise uses `haskell-program-name'.
160 It runs the hook `inferior-haskell-hook' after starting the process and
161 setting up the inferior-haskell buffer."
162 (interactive (list (inferior-haskell-command current-prefix-arg)))
163 (setq inferior-haskell-buffer
164 (apply 'make-comint "haskell" (car command) nil (cdr command)))
165 (with-current-buffer inferior-haskell-buffer
166 (inferior-haskell-mode)
167 (run-hooks 'inferior-haskell-hook)))
168
169 (defun inferior-haskell-process (&optional arg)
170 (or (if (buffer-live-p inferior-haskell-buffer)
171 (get-buffer-process inferior-haskell-buffer))
172 (progn
173 (let ((current-prefix-arg arg))
174 (call-interactively 'inferior-haskell-start-process))
175 ;; Try again.
176 (inferior-haskell-process arg))))
177
178 ;;;###autoload
179 (defalias 'run-haskell 'switch-to-haskell)
180 ;;;###autoload
181 (defun switch-to-haskell (&optional arg)
182 "Show the inferior-haskell buffer. Start the process if needed."
183 (interactive "P")
184 (let ((proc (inferior-haskell-process arg)))
185 (pop-to-buffer (process-buffer proc))))
186
ce51306 (with-selected-window): Define while compiling.
monnier authored
187 (eval-when-compile
188 (unless (fboundp 'with-selected-window)
189 (defmacro with-selected-window (win &rest body)
190 `(save-selected-window
191 (select-window ,win)
192 ,@body))))
6dd0f3a (inferior-haskell-command): Provide a default.
monnier authored
193
7410480 (inferior-haskell-wait-for-prompt): New fun, extracted
monnier authored
194 (defcustom inferior-haskell-wait-and-jump nil
195 "If non-nil, wait for file loading to terminate and jump to the error."
196 :type 'boolean
197 :group 'haskell)
198
199 (defun inferior-haskell-wait-for-prompt (proc)
200 "Wait until PROC sends us a prompt.
201 The process PROC should be associated to a comint buffer."
202 (with-current-buffer (process-buffer proc)
203 (while (progn
204 (goto-char comint-last-input-end)
205 (and (not (re-search-forward comint-prompt-regexp nil t))
206 (accept-process-output proc))))))
207
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
208 (defvar inferior-haskell-cabal-buffer nil)
209
210 (defun inferior-haskell-cabal-of-buf (buf)
211 (require 'haskell-cabal)
212 (with-current-buffer buf
213 (or inferior-haskell-cabal-buffer
214 (and (not (local-variable-p 'inferior-haskell-cabal-buffer))
215 (set (make-local-variable 'inferior-haskell-cabal-buffer)
216 (haskell-cabal-find-file))))))
217
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
218 (defun inferior-haskell-find-project-root (buf)
219 (with-current-buffer buf
220 (let ((cabal (inferior-haskell-cabal-of-buf buf)))
221 (or (when cabal
222 (with-current-buffer cabal
f11a557 (inferior-haskell-find-project-root): Use it.
monnier authored
223 (let ((hsd (haskell-cabal-get-setting "hs-source-dirs")))
224 (if (null hsd)
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
225 ;; If there's a Cabal file with no Hs-Source-Dirs, then
226 ;; just use the Cabal file's directory.
227 default-directory
228 ;; If there is an HSD, then check that it's an existing
229 ;; dir (otherwise, it may be a list of dirs and we don't
230 ;; know what to do with those). If it doesn't exist, then
231 ;; give up.
f11a557 (inferior-haskell-find-project-root): Use it.
monnier authored
232 (setq hsd (expand-file-name hsd))
233 (if (file-directory-p hsd) hsd)))))
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
234 ;; If there's no Cabal file or it's not helpful, try to look for
235 ;; a "module" statement and count the number of "." in the
236 ;; module name.
237 (save-excursion
238 (goto-char (point-min))
239 (let ((case-fold-search nil))
240 (when (re-search-forward
241 "^module[ \t]+\\([^- \t\n]+\\.[^- \t\n]+\\)[ \t]+where\\>" nil t)
242 (let* ((dir default-directory)
243 (module (match-string 1))
244 (pos 0))
245 (while (string-match "\\." module pos)
246 (setq pos (match-end 0))
247 (setq dir (expand-file-name ".." dir)))
248 ;; Let's check that the module name matches the file name,
249 ;; otherwise the project root is probably not what we think.
250 (if (eq t (compare-strings
251 (file-name-sans-extension buffer-file-name)
252 nil nil
253 (expand-file-name
254 (replace-regexp-in-string "\\." "/" module)
255 dir)
256 nil nil t))
257 dir
258 ;; If they're not equal, it means the local directory
259 ;; hierarchy doesn't match the module name. This seems
260 ;; odd, so let's warn the user about it. May help us
261 ;; debug this code as well.
262 (message "Ignoring inconsistent `module' info: %s in %s"
263 module buffer-file-name)
264 nil)))))))))
265
266
267
4418871 New file.
monnier authored
268 ;;;###autoload
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
269 (defun inferior-haskell-load-file (&optional reload)
0294d90 (inferior-haskell-load-file): Do reload if prefix arg.
monnier authored
270 "Pass the current buffer's file to the inferior haskell process.
271 If prefix arg \\[universal-argument] is given, just reload the previous file."
272 (interactive "p")
2eaced4 (inferior-haskell-load-file): Save buffer before using buffer-file-name.
monnier authored
273 ;; Save first, so we're sure that `buffer-file-name' is non-nil afterward.
274 (save-buffer)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
275 (let ((buf (current-buffer))
276 (file buffer-file-name)
4418871 New file.
monnier authored
277 (proc (inferior-haskell-process)))
278 (with-current-buffer (process-buffer proc)
279 (compilation-forget-errors)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
280 (let ((parsing-end (marker-position (process-mark proc)))
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
281 root)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
282 ;; Go to the root of the Cabal project, if applicable.
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
283 (when (and inferior-haskell-find-project-root
284 (setq root (inferior-haskell-find-project-root buf)))
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
285 ;; Not sure if it's useful/needed and if it actually works.
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
286 (unless (equal default-directory root)
287 (setq default-directory root)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
288 (inferior-haskell-send-command
289 proc (concat ":cd " default-directory)))
290 (setq file (file-relative-name file)))
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
291 (inferior-haskell-send-command
292 proc (if reload ":reload" (concat ":load \"" file "\"")))
30d658f (displayed-month): Remove declaration since it's not used here.
monnier authored
293 ;; Move the parsing-end marker *after* sending the command so
d8a5cc8 (inferior-haskell-load-file): Fix the compilation-parsing-end fiddlin…
monnier authored
294 ;; that it doesn't point just to the insertion point.
295 ;; Otherwise insertion may move the marker (if done with
296 ;; insert-before-markers) and we'd then miss some errors.
297 (if (boundp 'compilation-parsing-end)
298 (if (markerp compilation-parsing-end)
299 (set-marker compilation-parsing-end parsing-end)
300 (setq compilation-parsing-end parsing-end))))
1cda384 (inferior-haskell-load-file): Simplify and make more
monnier authored
301 (with-selected-window (display-buffer (current-buffer))
7410480 (inferior-haskell-wait-for-prompt): New fun, extracted
monnier authored
302 (goto-char (point-max)))
30d658f (displayed-month): Remove declaration since it's not used here.
monnier authored
303 ;; Use compilation-auto-jump-to-first-error if available.
304 ;; (if (and (boundp 'compilation-auto-jump-to-first-error)
305 ;; compilation-auto-jump-to-first-error
306 ;; (boundp 'compilation-auto-jump-to-next))
307 ;; (setq compilation-auto-jump-to-next t)
308 (when inferior-haskell-wait-and-jump
309 (inferior-haskell-wait-for-prompt proc)
310 (ignore-errors ;Don't beep if there were no errors.
311 (next-error)))))) ;; )
8512ba1 (inferior-haskell-mode): Typo.
monnier authored
312
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
313 (defvar inferior-haskell-run-command ":main")
314
315 (defun inferior-haskell-load-and-run (command)
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
316 "Pass the current buffer's file to haskell and then run a COMMAND."
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
317 (interactive
318 (list
319 (if (and inferior-haskell-run-command (not current-prefix-arg))
320 inferior-haskell-run-command
321 (read-string "Command to run: " nil nil inferior-haskell-run-command))))
322 (setq inferior-haskell-run-command command)
f818e57 Removed support for :reload (e.g. removed the C-c C-r binding).
monnier authored
323 (inferior-haskell-load-file)
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
324 ;; FIXME: if the :load encountered errors, we should not `run'.
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
325 (inferior-haskell-send-command (inferior-haskell-process) command))
326
8512ba1 (inferior-haskell-mode): Typo.
monnier authored
327 (defun inferior-haskell-send-command (proc str)
328 (setq str (concat str "\n"))
329 (with-current-buffer (process-buffer proc)
7410480 (inferior-haskell-wait-for-prompt): New fun, extracted
monnier authored
330 (inferior-haskell-wait-for-prompt proc)
8512ba1 (inferior-haskell-mode): Typo.
monnier authored
331 (goto-char (process-mark proc))
332 (insert-before-markers str)
333 (move-marker comint-last-input-end (point))
334 (comint-send-string proc str)))
4418871 New file.
monnier authored
335
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
336 (defun inferior-haskell-reload-file ()
337 "Tell the inferior haskell process to reread the current buffer's file."
338 (interactive)
339 (inferior-haskell-load-file 'reload))
340
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
341 ;;;###autoload
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
342 (defun inferior-haskell-type (expr &optional insert-value)
343 "Query the haskell process for the type of the given expression.
344 If optional argument `insert-value' is non-nil, insert the type above point
345 in the buffer. This can be done interactively with the \\[universal-argument] prefix.
346 The returned info is cached for reuse by `haskell-doc-mode'."
347 (interactive
348 (let ((sym (haskell-ident-at-point)))
349 (list (read-string (if (> (length sym) 0)
350 (format "Show type of (default %s): " sym)
351 "Show type of: ")
352 nil nil sym)
353 current-prefix-arg)))
354 (if (string-match "\\`\\s_+\\'" expr) (setq expr (concat "(" expr ")")))
355 (let* ((proc (inferior-haskell-process))
356 (type
357 (with-current-buffer (process-buffer proc)
358 (let ((parsing-end ; Remember previous spot.
359 (marker-position (process-mark proc))))
360 (inferior-haskell-send-command proc (concat ":type " expr))
361 ;; Find new point.
362 (goto-char (point-max))
363 (inferior-haskell-wait-for-prompt proc)
364 ;; Back up to the previous end-of-line.
365 (end-of-line 0)
366 ;; Extract the type output
367 (buffer-substring-no-properties
368 (save-excursion (goto-char parsing-end)
369 (line-beginning-position 2))
370 (point))))))
371 (if (not (string-match (concat "\\`" (regexp-quote expr) "[ \t]+::[ \t]*")
372 type))
373 (error "No type info: %s" type)
374
375 ;; Cache for reuse by haskell-doc.
376 (when (and (boundp 'haskell-doc-mode) haskell-doc-mode
377 (boundp 'haskell-doc-user-defined-ids)
378 ;; Haskell-doc only works for idents, not arbitrary expr.
379 (string-match "\\`(?\\(\\s_+\\|\\(\\sw\\|\\s'\\)+\\)?[ \t]*::[ \t]*"
380 type))
381 (let ((sym (match-string 1 type)))
382 (setq haskell-doc-user-defined-ids
383 (cons (cons sym (substring type (match-end 0)))
384 (remove-if (lambda (item) (equal (car item) sym))
385 haskell-doc-user-defined-ids)))))
386
011c502 (inferior-haskell-type): Fix call to message.
monnier authored
387 (if (interactive-p) (message "%s" type))
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
388 (when insert-value
389 (beginning-of-line)
390 (insert type "\n"))
391 type)))
392
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
393 ;;;###autoload
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
394 (defun inferior-haskell-info (sym)
395 "Query the haskell process for the info of the given expression."
396 (interactive
397 (let ((sym (haskell-ident-at-point)))
398 (list (read-string (if (> (length sym) 0)
399 (format "Show info of (default %s): " sym)
400 "Show info of: ")
401 nil nil sym))))
402 (let ((proc (inferior-haskell-process)))
403 (with-current-buffer (process-buffer proc)
404 (let ((parsing-end ; Remember previous spot.
405 (marker-position (process-mark proc))))
406 (inferior-haskell-send-command proc (concat ":info " sym))
407 ;; Find new point.
408 (goto-char (point-max))
409 (inferior-haskell-wait-for-prompt proc)
410 ;; Move to previous end-of-line
411 (end-of-line 0)
412 (let ((result
413 (buffer-substring-no-properties
414 (save-excursion (goto-char parsing-end)
415 (line-beginning-position 2))
416 (point))))
417 ;; Move back to end of process buffer
418 (goto-char (point-max))
419 (if (interactive-p) (message "%s" result))
420 result)))))
421
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
422 ;;;###autoload
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
423 (defun inferior-haskell-find-definition (sym)
424 "Attempt to locate and jump to the definition of the given expression."
425 (interactive
426 (let ((sym (haskell-ident-at-point)))
427 (list (read-string (if (> (length sym) 0)
428 (format "Find definition of (default %s): " sym)
429 "Find definition of: ")
430 nil nil sym))))
431 (let ((info (inferior-haskell-info sym)))
432 (if (not (string-match inferior-haskell-info-xref-re info))
433 (error "No source information available")
434 (let ((file (match-string-no-properties 1 info))
435 (line (string-to-number
436 (match-string-no-properties 2 info)))
437 (col (string-to-number
438 (match-string-no-properties 3 info))))
439 (when file
440 ;; Push current location marker on the ring used by `find-tag'
441 (require 'etags)
442 (ring-insert find-tag-marker-ring (point-marker))
443 (pop-to-buffer (find-file-noselect file))
444 (when line
445 (goto-line line)
446 (when col (move-to-column col))))))))
447
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
448 ;;; Functions to find the documentation of a given function.
449 ;;
450 ;; TODO for this section:
451 ;;
452 ;; * Support fetching of local Haddock docs pulled directly from source files.
453 ;; * Display docs locally? w3m?
454
455 (defcustom inferior-haskell-use-web-docs
456 'fallback
457 "Whether to use the online documentation. Possible values:
458 `never', meaning always use local documentation, unless the local
459 file doesn't exist, when do nothing, `fallback', which means only
460 use the online documentation when the local file doesn't exist,
461 or `always', meaning always use the online documentation,
462 regardless of existance of local files. Default is `fallback'."
463 :group 'haskell
464 :type '(choice (const :tag "Never" never)
465 (const :tag "As fallback" fallback)
466 (const :tag "Always" always)))
467
468 (defcustom inferior-haskell-web-docs-base
469 "http://haskell.org/ghc/docs/latest/html/libraries/"
470 "The base URL of the online libraries documentation. This will
471 only be used if the value of `inferior-haskell-use-web-docs' is
472 `always' or `fallback'."
473 :group 'haskell
474 :type 'string)
475
476 (defcustom haskell-package-manager-name "ghc-pkg"
477 "Name of the program to consult regarding package details."
478 :group 'haskell
479 :type 'string)
480
481 (defcustom haskell-package-conf-file
482 (ignore-errors
483 (with-temp-buffer
484 (call-process "ghc" nil t nil "--print-libdir")
485 (expand-file-name "package.conf"
486 (buffer-substring (point-min) (1- (point-max))))))
487 "Where the package configuration file for the package manager resides.
488 By default this is set to `ghc --print-libdir`/package.conf."
489 :group 'haskell
490 :type 'string)
491
492 (defun inferior-haskell-get-module (sym)
493 "Fetch the module in which SYM is defined."
494 (let ((info (inferior-haskell-info sym)))
495 (unless (string-match inferior-haskell-module-re info)
496 (error
497 "No documentation information available. Did you forget to C-c C-l?"))
498 (match-string-no-properties 1 info)))
499
500 (defun inferior-haskell-query-ghc-pkg (&rest args)
501 "Send ARGS to ghc-pkg, or whatever the value of
502 `haskell-package-manager' is. Insert the output into the current
503 buffer."
504 (apply 'call-process haskell-package-manager-name nil t nil args))
505
506 (defun inferior-haskell-get-package-list ()
507 "Get the list of packages from ghc-pkg, or whatever
508 `haskell-package-manager-name' is."
509 (with-temp-buffer
510 (inferior-haskell-query-ghc-pkg "--simple-output" "list")
511 (split-string (buffer-substring (point-min) (point-max)))))
512
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
513 (defun inferior-haskell-compute-module-alist ()
514 "Compute a list mapping modules to package names and haddock URLs using ghc-pkg."
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
515 (message "Generating module alist...")
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
516 (let ((module-alist ()))
517 (with-temp-buffer
518 (dolist (package (inferior-haskell-get-package-list))
519 (erase-buffer)
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
520 (inferior-haskell-query-ghc-pkg "describe" package)
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
521
522 (let ((package-w/o-version
523 (replace-regexp-in-string "[-.0-9]*\\'" "" package))
524 ;; Find the Haddock documentation URL for this package
525 (haddock
526 (progn
527 (goto-char (point-min))
bf80267 (inferior-haskell-compute-module-alist): Fix regexps.
monnier authored
528 (when (re-search-forward "haddock-html:[ \t]+\\(.*[^ \t\n]\\)"
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
529 nil t)
530 (match-string 1)))))
531
532 ;; Fetch the list of exposed modules for this package
533 (goto-char (point-min))
bf80267 (inferior-haskell-compute-module-alist): Fix regexps.
monnier authored
534 (when (re-search-forward "^exposed-modules:\\(.*\\(\n[ \t].*\\)*\\)"
535 nil t)
536 (dolist (module (split-string (match-string 1)))
537 (push (list module package-w/o-version haddock)
538 module-alist)))))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
539
540 (message "Generating module alist... done")
541 module-alist)))
542
543
544 (defcustom inferior-haskell-module-alist-file
545 ;; (expand-file-name "~/.inf-haskell-module-alist")
bf80267 (inferior-haskell-compute-module-alist): Fix regexps.
monnier authored
546 (expand-file-name (concat "inf-haskell-module-alist-"
547 (number-to-string (user-uid)))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
548 temporary-file-directory)
549 "Where to save the module -> package lookup table.
550 Set this to `nil' to never cache to a file."
551 :group 'haskell
552 :type '(choice (const :tag "Don't cache to file" nil) string))
553
554 (defvar inferior-haskell-module-alist nil
555 "Association list of modules to their packages.
556 Each element is of the form (MODULE PACKAGE HADDOCK), where
557 MODULE is the name of a module,
558 PACKAGE is the package it belongs to, and
559 HADDOCK is the path to that package's Haddock documentation.
560
561 This is calculated on-demand using `inferior-haskell-compute-module-alist'.
562 It's also cached in the file `inferior-haskell-module-alist-file',
563 so that it can be obtained more quickly next time.")
564
565 (defun inferior-haskell-module-alist ()
566 "Get the module alist from cache or ghc-pkg's info."
567 (or
568 ;; If we already have computed the alist, use it...
569 inferior-haskell-module-alist
570 (setq inferior-haskell-module-alist
571 (or
572 ;; ...otherwise try to read it from the cache file...
573 (and
574 inferior-haskell-module-alist-file
575 (file-readable-p inferior-haskell-module-alist-file)
576 (file-newer-than-file-p inferior-haskell-module-alist-file
577 haskell-package-conf-file)
578 (with-temp-buffer
579 (insert-file-contents inferior-haskell-module-alist-file)
580 (goto-char (point-min))
581 (prog1 (read (current-buffer))
582 (message "Read module alist from file cache."))))
583
584 ;; ...or generate it again and save it in a file for later.
585 (let ((alist (inferior-haskell-compute-module-alist)))
586 (when inferior-haskell-module-alist-file
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
587 (with-temp-buffer
588 (print alist (current-buffer))
589 ;; Do the write to a temp file first, then rename it.
590 ;; This makes it more atomic, and suffers from fewer security
591 ;; holes related to race conditions if the file is in /tmp.
592 (let ((tmp (make-temp-file inferior-haskell-module-alist-file)))
593 (write-region (point-min) (point-max) tmp)
594 (rename-file tmp inferior-haskell-module-alist-file
595 'ok-if-already-exists))))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
596 alist)))))
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
597
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
598 (defvar inferior-haskell-ghc-internal-ident-alist
599 ;; FIXME: Fill this table, ideally semi-automatically.
600 '(("GHC.Base.return" . "Control.Monad.return")
601 ("GHC.List" . "Data.List")))
602
603 (defun inferior-haskell-map-internal-ghc-ident (ident)
604 "Try to translate some internal GHC identifier to its alter ego in haskell docs."
605 (let ((head ident)
606 (tail "")
607 remapped)
608 (while (and (not
609 (setq remapped
610 (cdr (assoc head
611 inferior-haskell-ghc-internal-ident-alist))))
612 (string-match "\\.[^.]+\\'" head))
613 (setq tail (concat (match-string 0 head) tail))
614 (setq head (substring head 0 (match-beginning 0))))
615 (concat (or remapped head) tail)))
616
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
617 ;;;###autoload
618 (defun inferior-haskell-find-haddock (sym)
619 "Find and open the Haddock documentation of SYM.
620 Make sure to load the file into GHCi or Hugs first by using C-c C-l.
621 Only works for functions in a package installed with ghc-pkg, or
622 whatever the value of `haskell-package-manager-name' is.
623
624 This function needs to find which package a given module belongs
625 to. In order to do this, it computes a module-to-package lookup
626 alist, which is expensive to compute (it takes upwards of five
627 seconds with more than about thirty installed packages). As a
628 result, we cache it across sessions using the cache file
629 referenced by `inferior-haskell-module-alist-file'. We test to
630 see if this is newer than `haskell-package-conf-file' every time
631 we load it."
632 (interactive
633 (let ((sym (haskell-ident-at-point)))
634 (list (read-string (if (> (length sym) 0)
635 (format "Find documentation of (default %s): " sym)
636 "Find documentation of: ")
637 nil nil sym))))
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
638 (setq sym (inferior-haskell-map-internal-ghc-ident sym))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
639 (let* (;; Find the module and look it up in the alist
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
640 (module (inferior-haskell-get-module sym))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
641 (alist-record (assoc module (inferior-haskell-module-alist)))
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
642 (package (nth 1 alist-record))
643 (file-name (concat (subst-char-in-string ?. ?- module) ".html"))
644 (local-path (concat (nth 2 alist-record) "/" file-name))
645 (url (if (or (eq inferior-haskell-use-web-docs 'always)
646 (and (not (file-exists-p local-path))
647 (eq inferior-haskell-use-web-docs 'fallback)))
648 (concat inferior-haskell-web-docs-base package "/" file-name)
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
649 (and (file-exists-p local-path)
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
650 (concat "file://" local-path)))))
651 (if url (browse-url url) (error "Local file doesn't exist."))))
652
4418871 New file.
monnier authored
653 (provide 'inf-haskell)
bfd15e1 (inferior-haskell-mode): Hide compilation-mode bindings.
monnier authored
654
655 ;; arch-tag: 61804287-63dd-4052-bc0e-90f691b34b40
4418871 New file.
monnier authored
656 ;;; inf-haskell.el ends here
Something went wrong with that request. Please try again.