13

Possible Duplicate:
How to achieve code folding effects in emacs

An excellent feature of Dreamweaver is code folding of any lines of text -- recursively!

It's fantastic, I can fold any text or code, regardless of language. I work with existing systems; I don't edit well-written code or code in one language etc. mostly HTML mixed with god-knows-what. Folding lines makes understanding a lot easier and quicker. Sadly, this is the only feature I like in Dreamweaver.

Is there any code folding for Emacs in a similar aim?

Community
  • 1
  • 1
Christopher Done
  • 5,678
  • 4
  • 31
  • 36

6 Answers6

9

There's folding mode, a minor mode. Unfortunately it's intrusive: you have to manually annotate the folds with specialized comments, which clutter the code when you aren't using the mode (or when sharing code with others who don't use it). A better mode would not change your code to work.

Let me add: there's a duplicate of this that's worth a look: How to achieve code folding effects in Emacs.

Community
  • 1
  • 1
quark
  • 14,370
  • 3
  • 38
  • 29
  • 1
    Thanks, I'd already looked at that question and it doesn't answer my question because I don't want to have to edit my code to do it. But I'll accept this as an answer anyway, i.e. "no." It looks like I'm going to have to write this myself or migrate to vim. – Christopher Done Jul 30 '09 at 21:56
  • 1
    In case anyone comes searching this page later, this tagging based folding library might be of interest: http://lisperati.com/tagging.html – Christopher Done Jul 30 '09 at 22:16
  • 1
    If you *do* end up writing it let us all know! I've been wanting a proper folding mode for Emacs for quite a while. I'd basically given up and started using other editors. – quark Jul 31 '09 at 04:50
8

hide-show (hs-minor-mode) is a minor mode that will do something like this...

The default key-binding to trigger the folding is C-c @ C-c which I find pretty cumbersome. But then I don't use it much, either.

dmckee --- ex-moderator kitten
  • 90,531
  • 23
  • 129
  • 225
  • 1
    For more details, see: http://www.emacswiki.org/emacs/HideShow – Pete Jul 30 '09 at 19:20
  • 1
    I've already seen this. I really need something to hide a given region rather than it trying to understand the syntax, as I said above. Thanks though. – Christopher Done Jul 30 '09 at 21:57
4

You might want to look up the function set-selective-display and the variable selective-display. Not exactly what you want but it lets you hide lines based on indentation level.

Yoo
  • 15,338
  • 6
  • 37
  • 46
  • 1
    Cheers, I managed to [cook up something][1] using `set-selective-display` to at least demonstrate this folding idea for future discussion. It's not a solution because it messes with the code, but it's a good demonstration. I'm going to try using tagging.el as an example from which to gain ideas to implement this properly. [1]: http://chrisdone.com/blog/html/2009-08-01-emacs-folding.html#video – Christopher Done Aug 01 '09 at 17:31
  • The Christopher Done link to an emacs code folding video is now a dead link. – David Spector Jan 17 '19 at 15:39
3

I use fold-dwim.el. From the emacs wiki:

fold-dwim.el is a unified user interface for Emacs folding/outlining modes. It supports folding.el, hideshow.el, outline.el, TeX-fold.el, and nxml-outln.el

You can get it here:

http://www.emacswiki.org/emacs/FoldDwim

I have this in my .emacs:

(require 'fold-dwim)
(global-set-key [(C kp-4)] 'fold-dwim-hide-all)
(global-set-key [(C kp-5)] 'fold-dwim-toggle)
(global-set-key [(C kp-6)] 'fold-dwim-show-all)

Keep in mind that you still need to activate hs-minor-mode, folding-mode, etc. but I find it easier to use them this way.

1

Something else you might look into is nxhtml-mode; it doesn't fold code, but it does highlight mixed code (i.e. HTML and PHP) differently depending on its type. That gives you a similar gain in comprehensibility without the awkwardness of folding-mode. I think that approach is more suited to Emacs anyway, first because code-folding seems like a mouse-oriented idea that doesn't adapt well to the basically keyboard-centric Emacs interface, and second because Emacs eases navigating a large file to an extent that code can stay visible without getting in your way.

JasonFruit
  • 7,283
  • 4
  • 42
  • 60
0

If you actually need "something to hide a given region rather than it trying to understand the syntax" (unlike hideshow and other solutions based on parsing) and you "don't want to have to edit [your] code" (unlike folding), then, I assume, you mean you don't want the regions to be persistent between different editing sessions. Then you might use http://www.emacswiki.org/emacs/HideRegion to hide user-selected regions...

(But that's strange. The folding minor mode with persistent marks seems to be a far more convenient solution.)