ghc-doc.el 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;
  3. ;;; ghc.el
  4. ;;;
  5. ;; Author: Kazu Yamamoto <Kazu@Mew.org>
  6. ;; Created: Sep 25, 2009
  7. (require 'ghc-func)
  8. (require 'ghc-comp)
  9. ;;; Code:
  10. (defun ghc-browse-document (&optional haskell-org)
  11. (interactive "P")
  12. (let* ((mod0 (ghc-extract-module))
  13. (mod (ghc-read-module-name mod0))
  14. (pkg (ghc-resolve-package-name mod)))
  15. (if (and pkg mod)
  16. (ghc-display-document pkg mod haskell-org)
  17. (message "No document found"))))
  18. (defun ghc-resolve-package-name (mod)
  19. (with-temp-buffer
  20. (call-process "ghc-pkg" nil t nil "find-module" "--simple-output" mod)
  21. (goto-char (point-min))
  22. (when (re-search-forward "\\([^ ]+\\)-\\([0-9]*\\(\\.[0-9]+\\)*\\)$" nil t)
  23. (ghc-make-pkg-ver
  24. :pkg (match-string-no-properties 1)
  25. :ver (match-string-no-properties 2)))))
  26. (defun ghc-resolve-document-path (pkg)
  27. (with-temp-buffer
  28. (call-process "ghc-pkg" nil t nil "field" pkg "haddock-html")
  29. (goto-char (point-max))
  30. (forward-line -1)
  31. (beginning-of-line)
  32. (when (looking-at "^haddock-html: \\([^ \n]+\\)$")
  33. (match-string-no-properties 1))))
  34. (defconst ghc-doc-local-format "file://%s/%s.html")
  35. (defconst ghc-doc-hackage-format
  36. "http://hackage.haskell.org/packages/archive/%s/%s/doc/html/%s.html")
  37. (ghc-defstruct pkg-ver pkg ver)
  38. (defun ghc-display-document (pkg-ver mod haskell-org)
  39. (when (and pkg-ver mod)
  40. (let* ((mod- (ghc-replace-character mod ?. ?-))
  41. (pkg (ghc-pkg-ver-get-pkg pkg-ver))
  42. (ver (ghc-pkg-ver-get-ver pkg-ver))
  43. (pkg-with-ver (format "%s-%s" pkg ver))
  44. (path (ghc-resolve-document-path pkg-with-ver))
  45. (local (format ghc-doc-local-format path mod-))
  46. (remote (format ghc-doc-hackage-format pkg ver mod-))
  47. (file (format "%s/%s.html" path mod-))
  48. (url (if (or haskell-org (not (file-exists-p file))) remote local)))
  49. (browse-url url))))
  50. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  51. (defvar ghc-input-map nil)
  52. (unless ghc-input-map
  53. (setq ghc-input-map
  54. (if (boundp 'minibuffer-local-map)
  55. (copy-keymap minibuffer-local-map)
  56. (make-sparse-keymap)))
  57. (define-key ghc-input-map "\t" 'ghc-complete))
  58. (defun ghc-read-module-name (def)
  59. (read-from-minibuffer "Module name: " def ghc-input-map))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. (defun ghc-extract-module ()
  62. (interactive)
  63. (save-excursion
  64. (beginning-of-line)
  65. (if (looking-at "^\\(import\\|module\\) +\\(qualified +\\)?\\([^ (\n]+\\)")
  66. (match-string-no-properties 3))))
  67. (provide 'ghc-doc)