views:

477

answers:

5

There are several questions on SO about how to get code folding in emacs, without having to add any special characters like "markers" in the comments for example. Someone said that there was "no perfect solution."
It seems that it could be done by parsing the source of the program being written and look for matching parenthesis or bracket, or to do it based on indentation. You could also use a combination of scripts that use different methods.
So why is it commonly accepted that there is no "perfect" and straightforward way to get code-folding in emac? Is there something in emacs or its architecture that makes it hard to program? If it were easy, after so many years of smart people using emacs you would think that someone would have wrote it.

+8  A: 

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'

rryan
+2  A: 

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

Hamza Yerlikaya
i would have thought that outline-minor-mode would be the accepted answer instead of cedet, since it comes inbuilt with emacs. (http://mytechrants.wordpress.com/2009/01/05/emacs-tip-of-the-day-collapsing-functions/)
vedang
+3  A: 

You should play with hideshow (hs-minor-mode, http://www.emacswiki.org/emacs/HideShow) combined with fold-dwim.el (http://www.emacswiki.org/emacs/FoldDwim). 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 :) (see below, note the clickable +'s in the margin).

alt text

Leo Alekseyev
A: 

I have been using folding-mode for quite some time. With auto-insert template and abrevs it works quite well for me for for some nice bricks of code.

Being able to produce the buffer folded (for printing/emailing) has always been a desire of mine. Some of my folding tags are for secure / password hiding.

hpavc
A: 

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

imz
Though, 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.
imz