Skip to content

Instantly share code, notes, and snippets.

@Philonous
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save Philonous/f51e27913a7f0d6baa7c to your computer and use it in GitHub Desktop.

Select an option

Save Philonous/f51e27913a7f0d6baa7c to your computer and use it in GitHub Desktop.
haskell-add-import-line
(defvar module-abbrevs nil)
(setq module-abbrevs '(("Text"
"Data.Text (Text)"
"qualified Data.Text as Text")
("Encoding" "qualified Data.Text.Encoding as Text")
("Map" "qualified Data.Map as Map"
"Data.Map (Map)"
)
("Set" "Data.Set (Set)"
"qualified Data.Set as Set")
("App" "Control.Applicative")
("Monad" "Control.Monad")
("BS" "qualified Data.ByteString as BS"
"Data.ByteString (ByteString)")
("BS8" "qualified Data.ByteString.Char8 as BS8")
("BSL" "qualified Data.ByteString.Lazy as BSL")
("Concurrent" "Control.Concurrent")
("STM" "Control.Concurrent.STM")
("Xmpp" "qualified Network.Xmpp as Xmpp")
("XML" "qualified Data.XML.Types as XML")
("Ex" "qualified Control.Exception as Ex")
("Monoid" "Data.Monoid")
("Function" "Data.Function")
("Ord" "Data.Ord")
("Char" "Data.Char")
("List" "qualified Data.List as List")
("IO" "Control.Monad.IO.Class")
("STM" "Control.Concurrent.STM")
("IORef" "Data.IORef")
("UUID" "Data.UUID (UUID)"
"qualified Data.UUID as UUID")
))
(defun haskell-exists-import-line (line)
(save-excursion
(beginning-of-buffer)
(not (not (re-search-forward (concat "^" "import[ ]*" line "$")
(point-max) t)))))
(defun haskell-add-import-lines (import-line)
"Add an import line to the current buffer.
import-line can be an abbreviation defined in the
module-abbrevs variable. If this is the case, all the elements
in the matching entry are inserted (no further expansion
is performed)
If no abbreviation entry matches, the input it is inserted verbatim
Prefixing the input with an exclamation mark (!) forces the
rest of the input to be treated as an abbreviation. An error
is thrown when the given string doesn't match any entry
"
(interactive "Mmodules: ")
(require 'haskell-align-imports)
(require 'haskell-sort-imports)
(save-excursion
(let* ((force-abbrev (string= (substring import-line 0 1) "!"))
(import (if force-abbrev (substring import-line 1) import-line))
(decls (or (cdr (assoc import module-abbrevs))
(when force-abbrev
(error "Module abbreviation \"%s\" not found" import))
(list import))))
(mapc (lambda (decl)
(unless (haskell-exists-import-line decl)
(beginning-of-buffer)
(haskell-navigate-imports)
(insert (concat "import " decl "\n"))))
decls)
(haskell-sort-imports)
(haskell-align-imports))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment