ghc-mod/elisp/ghc-ins-mod.el

53 lines
1.4 KiB
EmacsLisp
Raw Normal View History

2011-12-27 07:28:02 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; ghc-ins-mod.el
;;;
;; Author: Kazu Yamamoto <Kazu@Mew.org>
;; Created: Dec 27, 2011
;;; Code:
(defvar ghc-hoogle-command "hoogle")
(defun ghc-insert-module ()
(interactive)
(if (not (ghc-which ghc-hoogle-command))
2011-12-27 07:42:44 +00:00
(message "\"%s\" not found" ghc-hoogle-command)
2012-01-23 06:12:24 +00:00
(let* ((expr0 (ghc-things-at-point))
2011-12-27 07:42:44 +00:00
(expr (ghc-read-expression expr0)))
2011-12-28 08:04:47 +00:00
(let ((mods (ghc-function-to-modules expr)))
(if (null mods)
(message "No module guessed")
(let* ((first (car mods))
(mod (if (= (length mods) 1)
first
(completing-read "Module name: " mods nil t first))))
(save-excursion
(ghc-goto-module-position)
(insert "import " mod "\n"))))))))
2011-12-27 07:28:02 +00:00
(defun ghc-goto-module-position ()
(goto-char (point-max))
(if (re-search-backward "^import" nil t)
2012-01-23 06:12:24 +00:00
(ghc-goto-empty-line)
2011-12-27 07:28:02 +00:00
(if (re-search-backward "^module" nil t)
2012-01-23 06:12:24 +00:00
(ghc-goto-empty-line)
2011-12-27 07:28:02 +00:00
(goto-char (point-min)))))
2012-01-23 06:12:24 +00:00
(defun ghc-goto-empty-line ()
(unless (re-search-forward "^$" nil t)
(forward-line)))
2011-12-27 07:28:02 +00:00
(defun ghc-function-to-modules (fn)
(with-temp-buffer
2011-12-28 08:04:47 +00:00
(call-process ghc-hoogle-command nil t nil "search" fn)
2011-12-27 07:28:02 +00:00
(goto-char (point-min))
(let ((regex (concat "^\\([a-zA-Z0-9.]+\\) " fn " "))
ret)
(while (re-search-forward regex nil t)
(setq ret (cons (match-string 1) ret)))
(nreverse ret))))
(provide 'ghc-ins-mod)