Next: , Previous: , Up: Top   [Index]


4 Editing Haskell Code

Haskell Mode is actually a collection of so-called major modes1 one of which is called haskell-mode. To avoid confusion, when referring to this package the name “Haskell mode” is written in a normal font, whereas when referring the major mode of the same name haskell-mode written with a dash in-between in a typewriter font is used.

As one might guess, haskell-mode is the (programming language2) major mode for editing (non-literate) Haskell source code. haskell-mode is associated with the file extensions listed below by default3.

.hs

official file extension for (non-literate) Haskell 98/2010 files

.hsc

“almost-Haskell” input file for the hsc2hs pre-processor

.cpphs

input file for the cpphs pre-processor

The major mode literate-haskell-mode (which is derived from haskell-mode and thus transitively from prog-mode) provides support for literate Haskell programs and is associated with the .lhs file extension by default.

literate-haskell-mode supports Bird-style as well as TeX-style literate Haskell files. The currently detected literate Haskell variant is shown in the mode line (see (emacs)Mode Line) as either ‘LitHaskell/bird’ or ‘LitHaskell/tex’.

4.1 Font Lock Support

haskell-mode supports syntax highlighting via Emacs’ Font Lock minor mode which should be enabled by default in current Emacsen. See (emacs)Font Lock, for more information on how to control font-lock-mode.

anim/font-lock

4.2 Managing imports

There are a few functions for managing imports.

4.2.1 Jump to imports

To jump to your import list, run

M-x haskell-navigate-imports

It’s nicer to have a keybinding to do this, for example:

(define-key haskell-mode-map [f8] 'haskell-navigate-imports)

You can hit it repeatedly to jump between groups of imports. It will cycle.

4.2.2 Format imports

To generally format (sort, align) your imports, you can run

M-x haskell-mode-format-imports

Or C-c C-..

4.2.3 Sort imports

To just sort imports, jump to an import section and run

M-x haskell-sort-imports

4.2.4 Align imports

To just align imports, jump to an import section and run

M-x haskell-align-imports

4.2.5 Entering imports

If you have Haskell Interactive Mode enabled and a session open, you can write

import

And then hit SPC (running M-x haskell-mode-contextual-space) which will prompt a list of all modules, either installed modules or local project directory files. Then it’ll insert it and then format that module section.

4.3 Completion support

haskell-mode can complete symbols, pragma directives and language extensions. This is part of haskell-interactive-mode.

anim/company-mode-language-pragma

If haskell-interactive-mode is enabled and working haskell mode provides completions for import statements taking into account currently loaded and available packages.

anim/company-mode-import-statement

4.4 Profiling and Debugging support

When profiling code with GHC, it is often useful to add cost centres by hand. These allow finer-grained information about program behavior. haskell-mode provides the function haskell-mode-toggle-scc-at-point to make this more convenient. It will remove an SCC annotation at point if one is present, or add one if point is over whitespace. By default it is bound to C-c C-s.


Footnotes

(1)

for more information about the concept of major modes see (emacs)Major Modes

(2)

haskell-mode is derived from prog-mode

(3)

for more information about file associations, see (emacs)Choosing Modes


Next: , Previous: , Up: Top   [Index]