[Haskell-cafe] Re: Using Cabal during development

Jose A. Ortega Ruiz jao at gnu.org
Tue Feb 9 13:41:47 EST 2010

Johan Tibell <johan.tibell at gmail.com> writes:

> It's really unfortunate that this approach doesn't work for .hsc
> files. When writing low level libraries I often have a couple of these
> which forces me out of my nice Emacs workflow into an Emacs + terminal
> + Cabal workflow.

I use the elisp code below to run cabal without leaving emacs, with its
output going into a compilation buffer (so that one can jump to the
errors). C-c c will find the cabal file nearest to the current buffer
and invoke cabal.


--8<---------------cut here---------------start------------->8---

(defun jao-locate-dominating-files (regexp &optional file)
 "Look up the directory hierarchy from FILE for a file matching REGEXP.
 Stop at the first parent where a matching file is found and return the
 of files that that match in this directory."
 (catch 'found
   (let ((dir (file-name-as-directory (or file (buffer-file-name))))
     (while (and dir
                 (not (string-match locate-dominating-stop-dir-regexp
       (if (setq files (condition-case nil
                           (directory-files dir 'full regexp 'nosort)
                         (error nil)))
           (throw 'found files)
         (if (equal dir
                    (setq dir (file-name-directory
                               (directory-file-name dir))))
             (setq dir nil))))

(defun jao-haskell-locate-cabal-file ()
 "Find the cabal file associated with current buffer."
 (car (jao-locate-dominating-files ".+\\.cabal")))

(eval-after-load 'haskell-mode
 '(add-hook 'haskell-mode-hook
            (lambda ()
              (set (make-local-variable 'compile-command) "cabal

(defun jao-haskell-cabal-build ()
 "Run, in a compilation buffer, a cabal command, after finding
the cabal file associated with this buffer."
 (let ((cabal-file (jao-haskell-locate-cabal-file)))
   (unless cabal-file
     (error "Couldn't find associated cabal file"))
   (let ((default-directory (file-name-directory cabal-file)))
     (call-interactively 'compile))))

(eval-after-load 'haskell-mode
  '(define-key haskell-mode-map [?\C-c ?c] 'jao-haskell-cabal-build))

--8<---------------cut here---------------end--------------->8---

More information about the Haskell-Cafe mailing list