M-C-d now can display the doc even in the sandbox.

This commit is contained in:
Kazu Yamamoto 2014-03-28 12:55:17 +09:00
parent 955b1b4091
commit 87926e699b
1 changed files with 32 additions and 46 deletions

View File

@ -15,61 +15,47 @@
(defun ghc-browse-document (&optional haskell-org)
(interactive "P")
(let ((mod0 (ghc-extract-module))
(expr0 (ghc-things-at-point)))
(cond
((and (not mod0) expr0)
(let* ((expr (ghc-read-expression expr0))
(info (ghc-get-info expr0))
(mod (ghc-extact-module-from-info info))
(pkg (ghc-resolve-package-name mod)))
(if (and pkg mod)
(ghc-display-document pkg mod haskell-org expr)
(message "No document found"))))
(t
(let* ((mod (ghc-read-module-name mod0))
(pkg (ghc-resolve-package-name mod)))
(if (and pkg mod)
(ghc-display-document pkg mod haskell-org)
(message "No document found")))))))
(expr0 (ghc-things-at-point))
pkg-ver-path mod expr info)
(if (or mod0 (not expr0))
(setq mod (ghc-read-module-name mod0))
(setq expr (ghc-read-expression expr0))
(setq info (ghc-get-info expr0))
(setq mod (ghc-extact-module-from-info info)))
(setq pkg-ver-path (ghc-resolve-document-path mod))
(if (and pkg-ver-path mod)
(ghc-display-document pkg-ver-path mod haskell-org expr)
(message "No document found"))))
(defun ghc-resolve-package-name (mod)
(ghc-defstruct pkg-ver-path pkg ver path)
(defun ghc-resolve-document-path (mod)
(with-temp-buffer
(ghc-call-process "ghc-pkg" nil t nil "find-module" "--simple-output" mod)
(ghc-call-process "ghc-mod" nil t nil "doc" mod)
(goto-char (point-min))
(when (re-search-forward "\\([^ ]+\\)-\\([0-9]*\\(\\.[0-9]+\\)*\\)$" nil t)
(ghc-make-pkg-ver
(when (looking-at "^\\([^ ]+\\)-\\([0-9]*\\(\\.[0-9]+\\)*\\) \\(.*\\)$")
(ghc-make-pkg-ver-path
:pkg (match-string-no-properties 1)
:ver (match-string-no-properties 2)))))
(defun ghc-resolve-document-path (pkg)
(with-temp-buffer
(ghc-call-process "ghc-pkg" nil t nil "field" pkg "haddock-html")
(goto-char (point-max))
(forward-line -1)
(beginning-of-line)
(when (looking-at "^haddock-html: \\([^ \n]+\\)$")
(match-string-no-properties 1))))
:ver (match-string-no-properties 2)
:path (match-string-no-properties 4)))))
(defconst ghc-doc-local-format "file://%s/%s.html")
(defconst ghc-doc-hackage-format
"http://hackage.haskell.org/packages/archive/%s/%s/doc/html/%s.html")
(ghc-defstruct pkg-ver pkg ver)
(defun ghc-display-document (pkg-ver mod haskell-org &optional symbol)
(when (and pkg-ver mod)
(let* ((mod- (ghc-replace-character mod ?. ?-))
(pkg (ghc-pkg-ver-get-pkg pkg-ver))
(ver (ghc-pkg-ver-get-ver pkg-ver))
(pkg-with-ver (format "%s-%s" pkg ver))
(path (ghc-resolve-document-path pkg-with-ver))
(local (format ghc-doc-local-format path mod-))
(remote (format ghc-doc-hackage-format pkg ver mod-))
(file (format "%s/%s.html" path mod-))
(url0 (if (or haskell-org (not (file-exists-p file))) remote local))
(url (if symbol (ghc-add-anchor url0 symbol) url0)))
;; Mac's "open" removes the anchor from "file://", sigh.
(browse-url url))))
(defun ghc-display-document (pkg-ver-path mod haskell-org &optional symbol)
(let* ((mod- (ghc-replace-character mod ?. ?-))
(pkg (ghc-pkg-ver-path-get-pkg pkg-ver-path))
(ver (ghc-pkg-ver-path-get-ver pkg-ver-path))
(path (ghc-pkg-ver-path-get-path pkg-ver-path))
(pkg-with-ver (format "%s-%s" pkg ver))
(local (format ghc-doc-local-format path mod-))
(remote (format ghc-doc-hackage-format pkg ver mod-))
(file (format "%s/%s.html" path mod-))
(url0 (if (or haskell-org (not (file-exists-p file))) remote local))
(url (if symbol (ghc-add-anchor url0 symbol) url0)))
;; Mac's "open" removes the anchor from "file://", sigh.
(browse-url url)))
(defun ghc-add-anchor (url symbol)
(let ((case-fold-search nil))