Why is there no code-folding in emacs?

You should look into CEDET. It does code-folding just fine, and many other fancy features that you're probably looking for if you're switching from an IDE to Emacs.

http://cedet.sourceforge.net/

Specifically, look for `global-semantic-tag-folding-mode'


You should play with Hideshow (hs-minor-mode) combined with fold-dwim.el. It does exactly what you suggested -- looks for matching braces/parens, and can be set up to fall back on the indentation.

There's a robust folding solution out there for most common languages, and if there isn't, all the folding packages are highly customizable. In fact, the only downside is the proliferation of folding methods (fold-dwim helps quite a bit with that); I used to think that because nobody could point me to a definitive solution, folding was hard or impossible — in fact, the opposite is true. You just have to experiment a little to see what works best for you.

I have used folding.el (e.g. to group stuff in my .emacs), outline-minor-mode, and now Hideshow. There's some chance that none of them would work exactly the way you want right out of the box (e.g. you might need to set up an outline regex, or define folding marks for folding.el), but it turns out to be easy. The default keybindings can be somewhat baroque, but this is remedied by fold-dwim and/or hideshow-org (highly recommended for Hideshow, cf the Emacswiki hideshow page; you can also mimic hideshow-org's behavior for other folding modes with some quick-and-dirty elisp and fold-dwim). Once you figure out your preferred setup, just turn it on automatically via hooks or buffer-local variables, and watch your code fold away :)


But in fact, there ARE various solutions for Emacs; I have listed some of them (those I have happened to come across) at http://en.wikipedia.org/w/index.php?title=Code_folding&oldid=375300945#cite_note-2.

Though, there are things I'm missing: in some cases, I'd like to combine several mechanisms: for example, for markdown, I'd like to use outline-based folding (for sections) and indentation-based folding (for quotations, code blocks etc.) -- in order not bother with implementing a complete parser for markdown.

Here they are:

Token-based folding in Emacs

Token-based folding in Emacs is impemented by the folding minor mode.

Indentation-based folding in Emacs

One can use the set-selective-display function in Emacs to hide lines based on the indentation level, as suggested in the Universal code folding note.

Syntax-dependent folding in Emacs

Syntax-dependent folding in Emacs is supported by:

  • the outline and allout modes for special dedicated "outline"-syntaxes;

  • by the hideshow minor mode for some programming languages;

  • also,

    • by the semantic-tag-folding minor mode and the senator-fold-tag command for syntaxes supported by semantic,
    • as well as by doc-mode for JavaDoc or Doxygen comments,
    • by TeX-fold-mode
    • sgml-fold-element command,
    • nxml-outln library in the corresponding language-specific modes, and possibly in other modes for particular syntaxes.

Several folding mechanisms are unified by the fold-dwim interface.

See also http://www.emacswiki.org/emacs/CategoryHideStuff.

Folding of user-selected regions in Emacs

Folding of user-selected regions in Emacs is implemented by the hide-region-hide command.


You don't need anything extra, just enable outline-minor-mode for file types you want to fold.

Tags:

Emacs

Folding