Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 659 lines (591 sloc) 28.695 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
223 (save-excursion
224 (goto-char (point-min))
225 (if (let ((case-fold-search t))
226 (not (re-search-forward "^hs-source-dirs:[ \t]*\\(.*\\)"
227 nil t)))
228 ;; If there's a Cabal file with no Hs-Source-Dirs, then
229 ;; just use the Cabal file's directory.
230 default-directory
231 ;; If there is an HSD, then check that it's an existing
232 ;; dir (otherwise, it may be a list of dirs and we don't
233 ;; know what to do with those). If it doesn't exist, then
234 ;; give up.
235 (let ((hsd (expand-file-name (match-string 1))))
236 (if (file-directory-p hsd) hsd))))))
237 ;; If there's no Cabal file or it's not helpful, try to look for
238 ;; a "module" statement and count the number of "." in the
239 ;; module name.
240 (save-excursion
241 (goto-char (point-min))
242 (let ((case-fold-search nil))
243 (when (re-search-forward
244 "^module[ \t]+\\([^- \t\n]+\\.[^- \t\n]+\\)[ \t]+where\\>" nil t)
245 (let* ((dir default-directory)
246 (module (match-string 1))
247 (pos 0))
248 (while (string-match "\\." module pos)
249 (setq pos (match-end 0))
250 (setq dir (expand-file-name ".." dir)))
251 ;; Let's check that the module name matches the file name,
252 ;; otherwise the project root is probably not what we think.
253 (if (eq t (compare-strings
254 (file-name-sans-extension buffer-file-name)
255 nil nil
256 (expand-file-name
257 (replace-regexp-in-string "\\." "/" module)
258 dir)
259 nil nil t))
260 dir
261 ;; If they're not equal, it means the local directory
262 ;; hierarchy doesn't match the module name. This seems
263 ;; odd, so let's warn the user about it. May help us
264 ;; debug this code as well.
265 (message "Ignoring inconsistent `module' info: %s in %s"
266 module buffer-file-name)
267 nil)))))))))
268
269
270
4418871 New file.
monnier authored
271 ;;;###autoload
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
272 (defun inferior-haskell-load-file (&optional reload)
4418871 New file.
monnier authored
273 "Pass the current buffer's file to the inferior haskell process."
274 (interactive)
2eaced4 (inferior-haskell-load-file): Save buffer before using buffer-file-name.
monnier authored
275 ;; Save first, so we're sure that `buffer-file-name' is non-nil afterward.
276 (save-buffer)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
277 (let ((buf (current-buffer))
278 (file buffer-file-name)
4418871 New file.
monnier authored
279 (proc (inferior-haskell-process)))
280 (with-current-buffer (process-buffer proc)
281 (compilation-forget-errors)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
282 (let ((parsing-end (marker-position (process-mark proc)))
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
283 root)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
284 ;; Go to the root of the Cabal project, if applicable.
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
285 (when (and inferior-haskell-find-project-root
286 (setq root (inferior-haskell-find-project-root buf)))
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
287 ;; Not sure if it's useful/needed and if it actually works.
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
288 (unless (equal default-directory root)
289 (setq default-directory root)
c7c6278 (inferior-haskell-use-cabal): New custom var.
monnier authored
290 (inferior-haskell-send-command
291 proc (concat ":cd " default-directory)))
292 (setq file (file-relative-name file)))
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
293 (inferior-haskell-send-command
294 proc (if reload ":reload" (concat ":load \"" file "\"")))
30d658f (displayed-month): Remove declaration since it's not used here.
monnier authored
295 ;; Move the parsing-end marker *after* sending the command so
d8a5cc8 (inferior-haskell-load-file): Fix the compilation-parsing-end fiddlin…
monnier authored
296 ;; that it doesn't point just to the insertion point.
297 ;; Otherwise insertion may move the marker (if done with
298 ;; insert-before-markers) and we'd then miss some errors.
299 (if (boundp 'compilation-parsing-end)
300 (if (markerp compilation-parsing-end)
301 (set-marker compilation-parsing-end parsing-end)
302 (setq compilation-parsing-end parsing-end))))
1cda384 (inferior-haskell-load-file): Simplify and make more
monnier authored
303 (with-selected-window (display-buffer (current-buffer))
7410480 (inferior-haskell-wait-for-prompt): New fun, extracted
monnier authored
304 (goto-char (point-max)))
30d658f (displayed-month): Remove declaration since it's not used here.
monnier authored
305 ;; Use compilation-auto-jump-to-first-error if available.
306 ;; (if (and (boundp 'compilation-auto-jump-to-first-error)
307 ;; compilation-auto-jump-to-first-error
308 ;; (boundp 'compilation-auto-jump-to-next))
309 ;; (setq compilation-auto-jump-to-next t)
310 (when inferior-haskell-wait-and-jump
311 (inferior-haskell-wait-for-prompt proc)
312 (ignore-errors ;Don't beep if there were no errors.
313 (next-error)))))) ;; )
8512ba1 (inferior-haskell-mode): Typo.
monnier authored
314
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
315 (defvar inferior-haskell-run-command ":main")
316
317 (defun inferior-haskell-load-and-run (command)
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
318 "Pass the current buffer's file to haskell and then run a COMMAND."
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
319 (interactive
320 (list
321 (if (and inferior-haskell-run-command (not current-prefix-arg))
322 inferior-haskell-run-command
323 (read-string "Command to run: " nil nil inferior-haskell-run-command))))
324 (setq inferior-haskell-run-command command)
f818e57 Removed support for :reload (e.g. removed the C-c C-r binding).
monnier authored
325 (inferior-haskell-load-file)
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
326 ;; FIXME: if the :load encountered errors, we should not `run'.
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
327 (inferior-haskell-send-command (inferior-haskell-process) command))
328
8512ba1 (inferior-haskell-mode): Typo.
monnier authored
329 (defun inferior-haskell-send-command (proc str)
330 (setq str (concat str "\n"))
331 (with-current-buffer (process-buffer proc)
7410480 (inferior-haskell-wait-for-prompt): New fun, extracted
monnier authored
332 (inferior-haskell-wait-for-prompt proc)
8512ba1 (inferior-haskell-mode): Typo.
monnier authored
333 (goto-char (process-mark proc))
334 (insert-before-markers str)
335 (move-marker comint-last-input-end (point))
336 (comint-send-string proc str)))
4418871 New file.
monnier authored
337
9ed46b1 (inferior-haskell-load-file): Re-add the `reload' arg.
monnier authored
338 (defun inferior-haskell-reload-file ()
339 "Tell the inferior haskell process to reread the current buffer's file."
340 (interactive)
341 (inferior-haskell-load-file 'reload))
342
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
343 ;;;###autoload
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
344 (defun inferior-haskell-type (expr &optional insert-value)
345 "Query the haskell process for the type of the given expression.
346 If optional argument `insert-value' is non-nil, insert the type above point
347 in the buffer. This can be done interactively with the \\[universal-argument] prefix.
348 The returned info is cached for reuse by `haskell-doc-mode'."
349 (interactive
350 (let ((sym (haskell-ident-at-point)))
351 (list (read-string (if (> (length sym) 0)
352 (format "Show type of (default %s): " sym)
353 "Show type of: ")
354 nil nil sym)
355 current-prefix-arg)))
356 (if (string-match "\\`\\s_+\\'" expr) (setq expr (concat "(" expr ")")))
357 (let* ((proc (inferior-haskell-process))
358 (type
359 (with-current-buffer (process-buffer proc)
360 (let ((parsing-end ; Remember previous spot.
361 (marker-position (process-mark proc))))
362 (inferior-haskell-send-command proc (concat ":type " expr))
363 ;; Find new point.
364 (goto-char (point-max))
365 (inferior-haskell-wait-for-prompt proc)
366 ;; Back up to the previous end-of-line.
367 (end-of-line 0)
368 ;; Extract the type output
369 (buffer-substring-no-properties
370 (save-excursion (goto-char parsing-end)
371 (line-beginning-position 2))
372 (point))))))
373 (if (not (string-match (concat "\\`" (regexp-quote expr) "[ \t]+::[ \t]*")
374 type))
375 (error "No type info: %s" type)
376
377 ;; Cache for reuse by haskell-doc.
378 (when (and (boundp 'haskell-doc-mode) haskell-doc-mode
379 (boundp 'haskell-doc-user-defined-ids)
380 ;; Haskell-doc only works for idents, not arbitrary expr.
381 (string-match "\\`(?\\(\\s_+\\|\\(\\sw\\|\\s'\\)+\\)?[ \t]*::[ \t]*"
382 type))
383 (let ((sym (match-string 1 type)))
384 (setq haskell-doc-user-defined-ids
385 (cons (cons sym (substring type (match-end 0)))
386 (remove-if (lambda (item) (equal (car item) sym))
387 haskell-doc-user-defined-ids)))))
388
011c502 (inferior-haskell-type): Fix call to message.
monnier authored
389 (if (interactive-p) (message "%s" type))
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
390 (when insert-value
391 (beginning-of-line)
392 (insert type "\n"))
393 type)))
394
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
395 ;;;###autoload
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
396 (defun inferior-haskell-info (sym)
397 "Query the haskell process for the info of the given expression."
398 (interactive
399 (let ((sym (haskell-ident-at-point)))
400 (list (read-string (if (> (length sym) 0)
401 (format "Show info of (default %s): " sym)
402 "Show info of: ")
403 nil nil sym))))
404 (let ((proc (inferior-haskell-process)))
405 (with-current-buffer (process-buffer proc)
406 (let ((parsing-end ; Remember previous spot.
407 (marker-position (process-mark proc))))
408 (inferior-haskell-send-command proc (concat ":info " sym))
409 ;; Find new point.
410 (goto-char (point-max))
411 (inferior-haskell-wait-for-prompt proc)
412 ;; Move to previous end-of-line
413 (end-of-line 0)
414 (let ((result
415 (buffer-substring-no-properties
416 (save-excursion (goto-char parsing-end)
417 (line-beginning-position 2))
418 (point))))
419 ;; Move back to end of process buffer
420 (goto-char (point-max))
421 (if (interactive-p) (message "%s" result))
422 result)))))
423
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
424 ;;;###autoload
36dd8bd (inferior-haskell-info-xref-re): New cst.
monnier authored
425 (defun inferior-haskell-find-definition (sym)
426 "Attempt to locate and jump to the definition of the given expression."
427 (interactive
428 (let ((sym (haskell-ident-at-point)))
429 (list (read-string (if (> (length sym) 0)
430 (format "Find definition of (default %s): " sym)
431 "Find definition of: ")
432 nil nil sym))))
433 (let ((info (inferior-haskell-info sym)))
434 (if (not (string-match inferior-haskell-info-xref-re info))
435 (error "No source information available")
436 (let ((file (match-string-no-properties 1 info))
437 (line (string-to-number
438 (match-string-no-properties 2 info)))
439 (col (string-to-number
440 (match-string-no-properties 3 info))))
441 (when file
442 ;; Push current location marker on the ring used by `find-tag'
443 (require 'etags)
444 (ring-insert find-tag-marker-ring (point-marker))
445 (pop-to-buffer (find-file-noselect file))
446 (when line
447 (goto-line line)
448 (when col (move-to-column col))))))))
449
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
450 ;;; Functions to find the documentation of a given function.
451 ;;
452 ;; TODO for this section:
453 ;;
454 ;; * Support fetching of local Haddock docs pulled directly from source files.
455 ;; * Display docs locally? w3m?
456
457 (defcustom inferior-haskell-use-web-docs
458 'fallback
459 "Whether to use the online documentation. Possible values:
460 `never', meaning always use local documentation, unless the local
461 file doesn't exist, when do nothing, `fallback', which means only
462 use the online documentation when the local file doesn't exist,
463 or `always', meaning always use the online documentation,
464 regardless of existance of local files. Default is `fallback'."
465 :group 'haskell
466 :type '(choice (const :tag "Never" never)
467 (const :tag "As fallback" fallback)
468 (const :tag "Always" always)))
469
470 (defcustom inferior-haskell-web-docs-base
471 "http://haskell.org/ghc/docs/latest/html/libraries/"
472 "The base URL of the online libraries documentation. This will
473 only be used if the value of `inferior-haskell-use-web-docs' is
474 `always' or `fallback'."
475 :group 'haskell
476 :type 'string)
477
478 (defcustom haskell-package-manager-name "ghc-pkg"
479 "Name of the program to consult regarding package details."
480 :group 'haskell
481 :type 'string)
482
483 (defcustom haskell-package-conf-file
484 (ignore-errors
485 (with-temp-buffer
486 (call-process "ghc" nil t nil "--print-libdir")
487 (expand-file-name "package.conf"
488 (buffer-substring (point-min) (1- (point-max))))))
489 "Where the package configuration file for the package manager resides.
490 By default this is set to `ghc --print-libdir`/package.conf."
491 :group 'haskell
492 :type 'string)
493
494 (defun inferior-haskell-get-module (sym)
495 "Fetch the module in which SYM is defined."
496 (let ((info (inferior-haskell-info sym)))
497 (unless (string-match inferior-haskell-module-re info)
498 (error
499 "No documentation information available. Did you forget to C-c C-l?"))
500 (match-string-no-properties 1 info)))
501
502 (defun inferior-haskell-query-ghc-pkg (&rest args)
503 "Send ARGS to ghc-pkg, or whatever the value of
504 `haskell-package-manager' is. Insert the output into the current
505 buffer."
506 (apply 'call-process haskell-package-manager-name nil t nil args))
507
508 (defun inferior-haskell-get-package-list ()
509 "Get the list of packages from ghc-pkg, or whatever
510 `haskell-package-manager-name' is."
511 (with-temp-buffer
512 (inferior-haskell-query-ghc-pkg "--simple-output" "list")
513 (split-string (buffer-substring (point-min) (point-max)))))
514
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
515 (defun inferior-haskell-compute-module-alist ()
516 "Compute a list mapping modules to package names and haddock URLs using ghc-pkg."
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
517 (message "Generating module alist...")
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
518 (let ((module-alist ()))
519 (with-temp-buffer
520 (dolist (package (inferior-haskell-get-package-list))
521 (erase-buffer)
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
522 (inferior-haskell-query-ghc-pkg "describe" package)
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
523
524 (let ((package-w/o-version
525 (replace-regexp-in-string "[-.0-9]*\\'" "" package))
526 ;; Find the Haddock documentation URL for this package
527 (haddock
528 (progn
529 (goto-char (point-min))
bf80267 (inferior-haskell-compute-module-alist): Fix regexps.
monnier authored
530 (when (re-search-forward "haddock-html:[ \t]+\\(.*[^ \t\n]\\)"
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
531 nil t)
532 (match-string 1)))))
533
534 ;; Fetch the list of exposed modules for this package
535 (goto-char (point-min))
bf80267 (inferior-haskell-compute-module-alist): Fix regexps.
monnier authored
536 (when (re-search-forward "^exposed-modules:\\(.*\\(\n[ \t].*\\)*\\)"
537 nil t)
538 (dolist (module (split-string (match-string 1)))
539 (push (list module package-w/o-version haddock)
540 module-alist)))))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
541
542 (message "Generating module alist... done")
543 module-alist)))
544
545
546 (defcustom inferior-haskell-module-alist-file
547 ;; (expand-file-name "~/.inf-haskell-module-alist")
bf80267 (inferior-haskell-compute-module-alist): Fix regexps.
monnier authored
548 (expand-file-name (concat "inf-haskell-module-alist-"
549 (number-to-string (user-uid)))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
550 temporary-file-directory)
551 "Where to save the module -> package lookup table.
552 Set this to `nil' to never cache to a file."
553 :group 'haskell
554 :type '(choice (const :tag "Don't cache to file" nil) string))
555
556 (defvar inferior-haskell-module-alist nil
557 "Association list of modules to their packages.
558 Each element is of the form (MODULE PACKAGE HADDOCK), where
559 MODULE is the name of a module,
560 PACKAGE is the package it belongs to, and
561 HADDOCK is the path to that package's Haddock documentation.
562
563 This is calculated on-demand using `inferior-haskell-compute-module-alist'.
564 It's also cached in the file `inferior-haskell-module-alist-file',
565 so that it can be obtained more quickly next time.")
566
567 (defun inferior-haskell-module-alist ()
568 "Get the module alist from cache or ghc-pkg's info."
569 (or
570 ;; If we already have computed the alist, use it...
571 inferior-haskell-module-alist
572 (setq inferior-haskell-module-alist
573 (or
574 ;; ...otherwise try to read it from the cache file...
575 (and
576 inferior-haskell-module-alist-file
577 (file-readable-p inferior-haskell-module-alist-file)
578 (file-newer-than-file-p inferior-haskell-module-alist-file
579 haskell-package-conf-file)
580 (with-temp-buffer
581 (insert-file-contents inferior-haskell-module-alist-file)
582 (goto-char (point-min))
583 (prog1 (read (current-buffer))
584 (message "Read module alist from file cache."))))
585
586 ;; ...or generate it again and save it in a file for later.
587 (let ((alist (inferior-haskell-compute-module-alist)))
588 (when inferior-haskell-module-alist-file
6e300b7 (inferior-haskell-find-project-root): New var, to
monnier authored
589 (with-temp-buffer
590 (print alist (current-buffer))
591 ;; Do the write to a temp file first, then rename it.
592 ;; This makes it more atomic, and suffers from fewer security
593 ;; holes related to race conditions if the file is in /tmp.
594 (let ((tmp (make-temp-file inferior-haskell-module-alist-file)))
595 (write-region (point-min) (point-max) tmp)
596 (rename-file tmp inferior-haskell-module-alist-file
597 'ok-if-already-exists))))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
598 alist)))))
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
599
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
600 (defvar inferior-haskell-ghc-internal-ident-alist
601 ;; FIXME: Fill this table, ideally semi-automatically.
602 '(("GHC.Base.return" . "Control.Monad.return")
603 ("GHC.List" . "Data.List")))
604
605 (defun inferior-haskell-map-internal-ghc-ident (ident)
606 "Try to translate some internal GHC identifier to its alter ego in haskell docs."
607 (let ((head ident)
608 (tail "")
609 remapped)
610 (while (and (not
611 (setq remapped
612 (cdr (assoc head
613 inferior-haskell-ghc-internal-ident-alist))))
614 (string-match "\\.[^.]+\\'" head))
615 (setq tail (concat (match-string 0 head) tail))
616 (setq head (substring head 0 (match-beginning 0))))
617 (concat (or remapped head) tail)))
618
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
619 ;;;###autoload
620 (defun inferior-haskell-find-haddock (sym)
621 "Find and open the Haddock documentation of SYM.
622 Make sure to load the file into GHCi or Hugs first by using C-c C-l.
623 Only works for functions in a package installed with ghc-pkg, or
624 whatever the value of `haskell-package-manager-name' is.
625
626 This function needs to find which package a given module belongs
627 to. In order to do this, it computes a module-to-package lookup
628 alist, which is expensive to compute (it takes upwards of five
629 seconds with more than about thirty installed packages). As a
630 result, we cache it across sessions using the cache file
631 referenced by `inferior-haskell-module-alist-file'. We test to
632 see if this is newer than `haskell-package-conf-file' every time
633 we load it."
634 (interactive
635 (let ((sym (haskell-ident-at-point)))
636 (list (read-string (if (> (length sym) 0)
637 (format "Find documentation of (default %s): " sym)
638 "Find documentation of: ")
639 nil nil sym))))
3afdf6b (inferior-haskell-run-command): New var.
monnier authored
640 (setq sym (inferior-haskell-map-internal-ghc-ident sym))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
641 (let* (;; Find the module and look it up in the alist
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
642 (module (inferior-haskell-get-module sym))
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
643 (alist-record (assoc module (inferior-haskell-module-alist)))
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
644 (package (nth 1 alist-record))
645 (file-name (concat (subst-char-in-string ?. ?- module) ".html"))
646 (local-path (concat (nth 2 alist-record) "/" file-name))
647 (url (if (or (eq inferior-haskell-use-web-docs 'always)
648 (and (not (file-exists-p local-path))
649 (eq inferior-haskell-use-web-docs 'fallback)))
650 (concat inferior-haskell-web-docs-base package "/" file-name)
b74e553 (inferior-haskell-module-alist-file): Use a file in /tmp rather than ~/.
monnier authored
651 (and (file-exists-p local-path)
c5f1425 (inferior-haskell-module-alist-file)
monnier authored
652 (concat "file://" local-path)))))
653 (if url (browse-url url) (error "Local file doesn't exist."))))
654
4418871 New file.
monnier authored
655 (provide 'inf-haskell)
bfd15e1 (inferior-haskell-mode): Hide compilation-mode bindings.
monnier authored
656
657 ;; arch-tag: 61804287-63dd-4052-bc0e-90f691b34b40
4418871 New file.
monnier authored
658 ;;; inf-haskell.el ends here
Something went wrong with that request. Please try again.