ghc-ins-mod.el 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;
  3. ;;; ghc-ins-mod.el
  4. ;;;
  5. ;; Author: Kazu Yamamoto <Kazu@Mew.org>
  6. ;; Created: Dec 27, 2011
  7. ;;; Code:
  8. (defvar ghc-hoogle-command "hoogle")
  9. (defun ghc-insert-module ()
  10. (interactive)
  11. (if (not (executable-find ghc-hoogle-command))
  12. (message "\"%s\" not found" ghc-hoogle-command)
  13. (let* ((expr0 (ghc-things-at-point))
  14. (expr (ghc-read-expression expr0)))
  15. (let ((mods (ghc-function-to-modules expr)))
  16. (if (null mods)
  17. (message "No module guessed")
  18. (let* ((first (car mods))
  19. (mod (if (= (length mods) 1)
  20. first
  21. (completing-read "Module name: " mods nil t first))))
  22. (save-excursion
  23. (ghc-goto-module-position)
  24. (insert "import " mod "\n"))))))))
  25. (defun ghc-goto-module-position ()
  26. (goto-char (point-max))
  27. (if (re-search-backward "^import" nil t)
  28. (ghc-goto-empty-line)
  29. (if (re-search-backward "^module" nil t)
  30. (ghc-goto-empty-line)
  31. (goto-char (point-min)))))
  32. (defun ghc-goto-empty-line ()
  33. (unless (re-search-forward "^$" nil t)
  34. (forward-line)))
  35. ;; To avoid Data.Functor
  36. (defvar ghc-applicative-operators '("<$>" "<$" "<*>" "<**>" "<*" "*>" "<|>"))
  37. (defun ghc-function-to-modules (fn)
  38. (if (member fn ghc-applicative-operators)
  39. '("Control.Applicative")
  40. (ghc-function-to-modules-hoogle fn)))
  41. (defun ghc-function-to-modules-hoogle (fn)
  42. (with-temp-buffer
  43. (let* ((fn1 (if (string-match "^[a-zA-Z0-9'_]+$" fn)
  44. fn
  45. (concat "(" fn ")")))
  46. (regex (concat "^\\([a-zA-Z0-9.]+\\) " fn1 " "))
  47. ret)
  48. (call-process ghc-hoogle-command nil t nil "search" fn1)
  49. (goto-char (point-min))
  50. (while (re-search-forward regex nil t)
  51. (setq ret (cons (match-string 1) ret)))
  52. (nreverse ret))))
  53. (provide 'ghc-ins-mod)