web developer & system programmer

coder . cl

ramblings and thoughts on programming...

haskell and emacs

published: 20-02-2012 / updated: 20-02-2012
posted in: development, emacs, haskell, programming, tips
by Daniel Molina Wegener

I think that I have a very nice integration between Haskell and Emacs. The first stuff that you should do to work with Haskell under Emacs, is to integrate the haskell-mode in Emacs with the proper hook to get the haskell-mode fully functional and working fine with Haskell code, so you can use Emacs as most likely an IDE rather than a simple text editor. That can be done thanks to the powerful Emacs Lisp interpreter that has embedded the Emacs editor.

(defun dmw-haskell-pointfree-region ()
  "Executes the Haskell pointfree too on the marked region."
  (let ((pfcmd (format "pointfree %s"
                       (shell-quote-argument (buffer-substring-no-properties
    (message (format "%s" (shell-command-to-string pfcmd)))))

(defun dmw-haskell-pointfree-replace ()
  "Replaces the marked region with the Haskell pointfree evaluation."
  (let ((pfcmd (format "pointfree %s"
                       (shell-quote-argument (buffer-substring-no-properties
    (shell-command-on-region (region-beginning) (region-end) pfcmd t)))

(defun dmw-haskell-hlint-buffer ()
  "Runs hlint on the currently edited file."
  (set (make-local-variable 'compile-command)
       (let ((file (file-name-nondirectory buffer-file-name)))
         (format "hlint %s" file)))
  (message compile-command)
  (compile compile-command))

(defun dmw-haskell-mode-hook ()
  "Haskell Mode Hook."
  (setq haskell-program-name "/usr/bin/ghci"
        indent-tabs-mode nil)
  (capitalized-words-mode t)
  (define-key haskell-mode-map "C-cC-p" 'dmw-haskell-pointfree-region)
  (define-key haskell-mode-map "C-cC-r" 'dmw-haskell-pointfree-replace)
  (define-key haskell-mode-map "C-cC-c" 'dmw-haskell-hlint-buffer)
  (message ">>> done dmw-haskell-mode-hook..."))

So, some nice thing about this mode-hook, is the fact that it can search for documentation on each function using C-c M-/ and find definitions using C-c M-.. With two custom commands which can allow you to use Pointfree — which can be installed using cabal — and Hlint — which can be found usually under your operating system without problems and can be installed using cabal too. So, to use Hlint with this hook, you can start it with C-c C-c or C-c C-v, and you can see if there is a point free style replacement for the selected expression — using Emacs region selection — with the binding C-c C-p, and if you like the point free style replacement, you can replace the selected region with the point free style expression using the binding C-c C-r.

I will try to add more nice stuff to my Haskell hook, probably something like searching modules and ordering imports, because would be nice to have similar stuff running under Emacs, to allow more easy to handle Haskell edition under Emacs. Enjoy!

No coments yet.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>