2

How can I implement folding behaviour like a vim? In vim I has this code:

set foldenable          " enable folding
set foldmethod=indent   " fold sections by indentation
set foldcolumn=1        " display folding column
set foldlevel=0         " first folding level is open

I need close all fold levels 'level by level'. When I unfold some 'folding level', included fold levels must be closed.

Maybe I can implement this with HideShow plugin or another variant?

valignatev
  • 225
  • 1
  • 15
sivakov512
  • 516
  • 3
  • 13
  • If `vimish-fold` doesn't suit your needs, consider submitting a feature request to the maintainer. See: https://github.com/mrkkrp/vimish-fold – lawlist Nov 06 '16 at 05:23
  • @lawlist can you show vimish-fold conf example? As far as I know vimish-fold allow to fold only selected regions of text. Or not? – sivakov512 Nov 06 '16 at 05:26
  • Never used it sorry, but I know it was written specifically for the vim folks about a year ago. Other than the built-in hide-show and standard ability to hide text with the `invisible` property, that's the extent of my knowledge. Perhaps another forum participant can help further. – lawlist Nov 06 '16 at 05:31
  • Checkout [this post](http://emacs.stackexchange.com/questions/14781/how-can-i-replicate-vims-code-folding) if it answers your question. – Compro Prasad Nov 06 '16 at 05:49
  • @ComproPrasad -- that was the thread that inspired the author of `vimish-fold` to write the library about a year ago. :) – lawlist Nov 06 '16 at 05:50
  • @ComproPrasad that's good if folding by text selection needed only, but I need all code folded 'by default' and 'by levels' :) (Sorry for my bad English) – sivakov512 Nov 06 '16 at 05:54

1 Answers1

4

There are a lot of different implementations of folding for emacs. Some are specific to a particular programming language or syntax, others are more generic. Of the generic ones, it sounds like yafolding is exactly what you're asking for. I tried it out, and while it feels somewhat slow, it does work.

Another I tried is called origami, and I liked the results a lot better. It goes by indentation if that's all it knows about your file, but if it's one of a dozen or so supported languages then it uses syntax markers like curly braces to do the folding. It was also much quicker to fold an entire file. On the other hand, it apparently comes with no pre-configured keybindings, so to use it you'll have to set those up yourself (it does provide a keymap to add them to).

There are a number of others you might check out; run M-x list-packages and then search for "fold".

Many people also use outline-minor-mode to do folding and organization of code. It works off of embedded headline markers in comments rather than the structure of the code, but that's handy for many things as well. It also has the advantage of being built-in.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • Thank you! `origami` works well. Maybe you know how fold class methods in python code? For example,use major-mode folding and folding by indentation simultaneously? And...maybe you can answer on this question http://emacs.stackexchange.com/questions/28371/how-to-find-grep-in-python-virtualenv-with-projectile-helm-easier ? :) – sivakov512 Nov 07 '16 at 06:18
  • 1
    So... Now I think that `outline-mode` really better than the `origami-mode` and all other... – sivakov512 Nov 07 '16 at 07:06