From 94be5c3760eb186449f770fe67aec30399bb17d4 Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Tue, 27 Dec 2011 16:28:02 +0900 Subject: [PATCH] inserting module. --- elisp/Makefile | 2 +- elisp/ghc-ins-mod.el | 48 ++++++++++++++++++++++++++++++++++++++++++++ elisp/ghc.el | 3 +++ ghc-mod.cabal | 2 +- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 elisp/ghc-ins-mod.el diff --git a/elisp/Makefile b/elisp/Makefile index 598b11e..cb19add 100644 --- a/elisp/Makefile +++ b/elisp/Makefile @@ -1,5 +1,5 @@ SRCS = ghc.el ghc-func.el ghc-doc.el ghc-comp.el ghc-flymake.el \ - ghc-command.el ghc-info.el + ghc-command.el ghc-info.el ghc-ins-mod.el EMACS = emacs TEMPFILE = temp.el diff --git a/elisp/ghc-ins-mod.el b/elisp/ghc-ins-mod.el new file mode 100644 index 0000000..bddea86 --- /dev/null +++ b/elisp/ghc-ins-mod.el @@ -0,0 +1,48 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; +;;; ghc-ins-mod.el +;;; + +;; Author: Kazu Yamamoto +;; Created: Dec 27, 2011 + +;;; Code: + +(defvar ghc-hoogle-command "hoogle") + +(defun ghc-insert-module () + (interactive) + (if (not (ghc-which ghc-hoogle-command)) + (message "%s not found" ghc-hoogle-command) + (let* ((expr0 (thing-at-point 'symbol)) + (expr (ghc-read-expression expr0)) + (mods (ghc-function-to-modules expr)) + (first (car mods)) + (ini (cons first 0)) + (mod (if (= (length mods) 1) + first + (completing-read "Module name: " mods nil t ini)))) + (save-excursion + (ghc-goto-module-position) + (insert "import " mod "\n") + (message "%s was inserted" mod))))) + +(defun ghc-goto-module-position () + (goto-char (point-max)) + (if (re-search-backward "^import" nil t) + (forward-line) + (if (re-search-backward "^module" nil t) + (forward-line) + (goto-char (point-min))))) + +(defun ghc-function-to-modules (fn) + (with-temp-buffer + (call-process ghc-hoogle-command nil t nil fn) + (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) diff --git a/elisp/ghc.el b/elisp/ghc.el index 7364740..a2356f6 100644 --- a/elisp/ghc.el +++ b/elisp/ghc.el @@ -26,6 +26,7 @@ (require 'ghc-info) (require 'ghc-flymake) (require 'ghc-command) +(require 'ghc-ins-mod) (require 'dabbrev) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -50,6 +51,7 @@ (defvar ghc-info-key "\C-c\C-i") (defvar ghc-check-key "\C-x\C-s") (defvar ghc-toggle-key "\C-c\C-c") +(defvar ghc-module-key "\C-c\C-m") (defvar ghc-hoogle-key (format "\C-c%c" (ghc-find-C-h))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -74,6 +76,7 @@ (define-key haskell-mode-map ghc-sort-key 'ghc-sort-lines) (define-key haskell-mode-map ghc-check-key 'ghc-save-buffer) (define-key haskell-mode-map ghc-toggle-key 'ghc-flymake-toggle-command) + (define-key haskell-mode-map ghc-module-key 'ghc-insert-module) (define-key haskell-mode-map ghc-hoogle-key 'haskell-hoogle) (ghc-comp-init) (setq ghc-initialized t))) diff --git a/ghc-mod.cabal b/ghc-mod.cabal index c6a3189..b1b9fe0 100644 --- a/ghc-mod.cabal +++ b/ghc-mod.cabal @@ -20,7 +20,7 @@ Cabal-Version: >= 1.6 Build-Type: Simple Data-Dir: elisp Data-Files: Makefile ghc.el ghc-func.el ghc-doc.el ghc-comp.el - ghc-flymake.el ghc-command.el ghc-info.el + ghc-flymake.el ghc-command.el ghc-info.el ghc-ins-mod.el Executable ghc-mod Main-Is: GHCMod.hs Other-Modules: List Browse Cabal CabalDev Check Info Lang Lint Types ErrMsg Paths_ghc_mod