ghc-mod/elisp/ghc-doc.el

74 lines
2.1 KiB
EmacsLisp
Raw Normal View History

2010-01-06 05:38:06 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; ghc.el
;;;
;; Author: Kazu Yamamoto <Kazu@Mew.org>
;; Created: Sep 25, 2009
(require 'ghc-func)
(require 'ghc-comp)
;;; Code:
2010-03-10 07:51:42 +00:00
(defun ghc-browse-document (&optional haskell-org)
(interactive "P")
2010-01-06 05:38:06 +00:00
(let* ((mod0 (ghc-extract-module))
(mod (ghc-read-module-name mod0))
(pkg (ghc-resolve-package-name mod)))
2010-03-10 07:51:42 +00:00
(ghc-display-document pkg mod haskell-org)))
2010-01-06 05:38:06 +00:00
(defun ghc-resolve-package-name (mod)
(with-temp-buffer
(call-process "ghc-pkg" nil t nil "find-module" "--simple-output" mod)
(goto-char (point-min))
(when (re-search-forward "[^ ]+-[0-9]*\\(\\.[0-9]+\\)*$")
(match-string-no-properties 0))))
2010-01-06 05:38:06 +00:00
(defun ghc-resolve-document-path (pkg)
(with-temp-buffer
(call-process "ghc-pkg" nil t nil "field" pkg "haddock-html")
2010-03-11 05:15:28 +00:00
(goto-char (point-max))
(forward-line -1)
(beginning-of-line)
2010-01-06 05:38:06 +00:00
(when (looking-at "^haddock-html: \\([^ \n]+\\)$")
(match-string-no-properties 1))))
2010-06-14 03:03:14 +00:00
(defconst ghc-doc-local-format "file://%s/%s.html")
(defconst ghc-doc-hackage-format
2010-03-10 07:51:42 +00:00
"http://hackage.haskell.org/packages/archive/%s/latest/doc/html/%s.html")
(defun ghc-display-document (pkg mod haskell-org)
2010-01-06 05:38:06 +00:00
(when (and pkg mod)
(let* ((mod- (ghc-replace-character mod ?. ?-))
2010-03-10 07:51:42 +00:00
(url (if haskell-org
(format ghc-doc-hackage-format pkg mod-)
(format ghc-doc-local-format
(ghc-resolve-document-path pkg) mod-))))
2010-01-06 05:38:06 +00:00
(browse-url url))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar ghc-input-map nil)
(unless ghc-input-map
(setq ghc-input-map
(if (boundp 'minibuffer-local-map)
(copy-keymap minibuffer-local-map)
(make-sparse-keymap)))
(define-key ghc-input-map "\t" 'ghc-complete))
2010-05-04 07:35:40 +00:00
(defun ghc-read-module-name (def)
(read-from-minibuffer "Module name: " def ghc-input-map))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ghc-extract-module ()
(interactive)
(save-excursion
(beginning-of-line)
(if (looking-at "^\\(import\\|module\\) +\\(qualified +\\)?\\([^ (\n]+\\)")
(match-string-no-properties 3))))
(provide 'ghc-doc)