diff --git a/elisp/ghc-command.el b/elisp/ghc-command.el index 687a736..f9603e8 100644 --- a/elisp/ghc-command.el +++ b/elisp/ghc-command.el @@ -51,7 +51,31 @@ (lambda () (re-search-forward "^import\\( *qualified\\)? *" nil t) nil) - 'end-of-line))))) + 'end-of-line)) + (ghc-merge-lines)))) + +(defun ghc-merge-lines () + (let ((case-fold-search nil)) + (goto-char (point-min)) + (while (not (eolp)) + (if (looking-at "^import *\\([A-Z][^ \n]+\\) *(\\(.*\\))$") + (let ((mod (match-string-no-properties 1)) + (syms (match-string-no-properties 2)) + (beg (point))) + (forward-line) + (ghc-merge-line beg mod syms)) + (forward-line))))) + +(defun ghc-merge-line (beg mod syms) + (let ((regex (concat "^import *" (regexp-quote mod) " *(\\(.*\\))$")) + duplicated) + (while (looking-at regex) + (setq duplicated t) + (setq syms (concat syms ", " (match-string-no-properties 1))) + (forward-line)) + (when duplicated + (delete-region beg (point)) + (insert "import " mod " (" syms ")\n")))) (defun ghc-save-buffer () (interactive)