Code folding in Emacs [duplicate]
Asked Answered
C

6

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?

Changeling answered 30/7, 2009 at 18:58 Comment(1)
Previously here: #1085670Supermundane
A
10

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.

Animalist answered 30/7, 2009 at 19:3 Comment(3)
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.Changeling
In case anyone comes searching this page later, this tagging based folding library might be of interest: lisperati.com/tagging.htmlChangeling
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.Animalist
S
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.

Supermundane answered 30/7, 2009 at 19:15 Comment(2)
For more details, see: emacswiki.org/emacs/HideShowVlaminck
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.Changeling
A
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.

Areopagus answered 31/7, 2009 at 14:9 Comment(2)
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]: chrisdone.com/blog/html/2009-08-01-emacs-folding.html#videoChangeling
The Christopher Done link to an emacs code folding video is now a dead link.Hbeam
F
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.

Fuliginous answered 5/8, 2009 at 10:7 Comment(0)
T
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.

Thrombokinase answered 30/7, 2009 at 19:19 Comment(0)
P
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.)

Piefer answered 25/7, 2010 at 2:41 Comment(2)
HideRegion is too complicated. I need something that just works!Tankard
@Robin But what do you want it to do? My answer was about just hiding an arbitrary region once.Piefer

© 2022 - 2024 — McMap. All rights reserved.