Code folding in Emacs

Code folding is a feature I’ve taken for granted in Windows IDEs, but now I am using Emacs more and more, I wanted it there too, and it turns out getting it is straightforward enough.

  1. Download Folding Mode and save folding.el in the Emacs site lisp directory (on my Debian machine that is /usr/share/emacs23/site-lisp/, probably the same on any Linux). Surprisingly there’s no .deb package for it or apt-get install emacs-goodies-el.
  2. Add the following to your ~/.emacs:
    (load "folding" 'nomessage 'noerror)
    (folding-add-to-marks-list 'haskell-mode "--{{{"  "--}}}"  nil t)
    (add-hook 'haskell-mode-hook 'folding-mode)
    (folding-add-to-marks-list 'tuareg-mode "(*{{{"  "(*}}}*)"  nil t)
    (add-hook 'tuareg-mode-hook 'folding-mode)

    Lines 4 and 7 cause folding-mode to be automatically entered after haskell-mode or tuareg-mode respectively, which seems to be necessary, rather than just loading it on Emacs startup with auto-load. This is slightly different from the instructions on the page, perhaps it was written for an older Emacs.

  3. Delimit the code with the appropriate markers. These can be anything you put in .emacs, {{{ and }}} in conjunction with the language’s own comment characters seems to be a convention. Unfortunately Emacs doesn’t seem to be able to do this automagically the way other language-sensitive editors would (then again, those editors don’t allow you to define folding positions yourself, they always do it on functions or other obvious blocks)

As an example you can then toggle between:


With right-click.

About Gaius

Jus' a good ol' boy, never meanin' no harm
This entry was posted in Emacs, Haskell, Linux, Ocaml. Bookmark the permalink.

5 Responses to Code folding in Emacs

  1. ruediger says:

    Of course, there is a .deb package. folding.el is included in emacs-goodies-el 🙂

    • Gaius says:

      Hah, intuitive 🙂

      Annoyingly it wants a bunch of dependencies which it doesn’t really need, as it runs perfectly if you install it by hand. Like, perl-doc, I don’t even like Perl… And if you do it through dselect it also wants to install a new IM client!

      • Mehdi Dogguy says:

        That’s probably because the package contains other extentions with some needs. Besides, apt-cache would have found it if the description of emacs-goodies-el did contain “folding”… but it doesn’t. But then, one might to check if it’s the case for other extesions as well before submitting a bugreport.

  2. Pingback: Komodo 6 | So I decided to take my work back underground

  3. jinfei zhang says:

    you might want to give a try: no “markers” are needed, since no specially added markers are appropriate.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s