2011-12-27 07:28:02 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;;
|
|
|
|
;;; ghc-ins-mod.el
|
|
|
|
;;;
|
|
|
|
|
|
|
|
;; Author: Kazu Yamamoto <Kazu@Mew.org>
|
|
|
|
;; Created: Dec 27, 2011
|
|
|
|
|
2014-03-25 05:58:20 +00:00
|
|
|
(require 'ghc-process)
|
|
|
|
|
2011-12-27 07:28:02 +00:00
|
|
|
;;; Code:
|
|
|
|
|
2014-03-25 05:29:18 +00:00
|
|
|
(defvar ghc-ins-mod-rendezvous nil)
|
2014-03-25 13:39:59 +00:00
|
|
|
(defvar ghc-ins-mod-results nil)
|
2011-12-27 07:28:02 +00:00
|
|
|
|
|
|
|
(defun ghc-insert-module ()
|
|
|
|
(interactive)
|
2014-03-25 05:29:18 +00:00
|
|
|
(let* ((expr0 (ghc-things-at-point))
|
|
|
|
(expr (ghc-read-expression expr0)))
|
2014-03-25 05:58:20 +00:00
|
|
|
(ghc-ins-mod expr)))
|
|
|
|
|
|
|
|
(defun ghc-ins-mod (expr)
|
2014-03-26 02:32:08 +00:00
|
|
|
(let (prefix fun mods)
|
|
|
|
(if (not (string-match "^\\([^.]+\\)\\\.\\([^.]+\\)$" expr))
|
|
|
|
(setq fun expr)
|
|
|
|
(setq prefix (match-string 1 expr))
|
|
|
|
(setq fun (match-string 2 expr)))
|
|
|
|
(setq mods (ghc-function-to-modules fun))
|
2014-03-25 05:58:20 +00:00
|
|
|
(if (null mods)
|
|
|
|
(message "No module guessed")
|
2014-03-31 05:15:05 +00:00
|
|
|
(let* ((fmt (if prefix
|
|
|
|
(concat "Module name for \"" prefix "\" (%s): ")
|
|
|
|
"Module name (%s): "))
|
|
|
|
(mod (ghc-completing-read fmt mods)))
|
2014-03-25 05:58:20 +00:00
|
|
|
(save-excursion
|
|
|
|
(ghc-goto-module-position)
|
2014-03-26 02:32:08 +00:00
|
|
|
(if prefix
|
|
|
|
(insert "import qualified " mod " as " prefix "\n")
|
|
|
|
(insert "import " mod " (" (ghc-enclose expr) ")\n")))))))
|
2014-03-25 05:29:18 +00:00
|
|
|
|
|
|
|
(defun ghc-completing-read (fmt lst)
|
|
|
|
(let* ((def (car lst))
|
|
|
|
(prompt (format fmt def))
|
|
|
|
(inp (completing-read prompt lst)))
|
|
|
|
(if (string= inp "") def inp)))
|
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)
|
2014-03-25 05:58:20 +00:00
|
|
|
(progn
|
|
|
|
(ghc-goto-empty-line)
|
|
|
|
(forward-line)
|
|
|
|
(unless (eolp)
|
|
|
|
(save-excursion
|
|
|
|
(insert "\n"))))
|
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)))
|
|
|
|
|
2014-03-25 05:29:18 +00:00
|
|
|
(defun ghc-function-to-modules (fun)
|
|
|
|
(setq ghc-ins-mod-rendezvous nil)
|
2014-03-25 13:39:59 +00:00
|
|
|
(setq ghc-ins-mod-results nil)
|
2014-03-25 05:29:18 +00:00
|
|
|
(ghc-with-process
|
|
|
|
(lambda () (ghc-ins-mod-send fun))
|
|
|
|
'ghc-ins-mod-callback)
|
|
|
|
(while (null ghc-ins-mod-rendezvous)
|
|
|
|
(sit-for 0.01))
|
2014-03-25 13:39:59 +00:00
|
|
|
ghc-ins-mod-results)
|
2014-03-25 05:29:18 +00:00
|
|
|
|
|
|
|
(defun ghc-ins-mod-send (fun)
|
|
|
|
(concat "find " fun "\n"))
|
|
|
|
|
|
|
|
(defun ghc-ins-mod-callback ()
|
|
|
|
(let (lines line beg)
|
|
|
|
(while (not (eobp))
|
|
|
|
(setq beg (point))
|
|
|
|
(forward-line)
|
|
|
|
(setq line (buffer-substring-no-properties beg (1- (point))))
|
|
|
|
(setq lines (cons line lines)))
|
2014-03-25 13:39:59 +00:00
|
|
|
(setq ghc-ins-mod-rendezvous t)
|
|
|
|
(setq ghc-ins-mod-results (nreverse (cdr lines))))) ;; removing "OK"
|
2011-12-27 07:28:02 +00:00
|
|
|
|
|
|
|
(provide 'ghc-ins-mod)
|