Can I mix the use of fold-expr with fold-marker just as mixing fold-expr with fold-manual?

69 views
Skip to first unread message

Bao Niu

unread,
Jan 3, 2015, 6:59:14 PM1/3/15
to vim...@googlegroups.com
I read the below documentation:
>If you use the "expr" method to define folds, but they are not exactly how
>you want them, you could switch to the "manual" method. This will not remove
>the defined folds. Then you can delete or add folds manually.

It works very well.

However, if I want to switch to "marker" instead of "manual", I would lose all the foldings! Is there a way to mix up the expression fold-method with the marker fold-method? Thanks.

Bao Niu

unread,
Jan 6, 2015, 10:16:09 PM1/6/15
to vim...@googlegroups.com
Let me clarify it a little bit. What I'm looking for is first using fold-expr to automatically fold up those Python classes and methods/functions, then switching not into manual mode, but *marker* mode and still retain those folds set up by fold-expr. Is this possible with GVim 7.4? Many thanks.

Ben Fritz

unread,
Jan 6, 2015, 11:03:31 PM1/6/15
to vim...@googlegroups.com
No. Because folds with "marker" method are defined by inserting text into the buffer.

Just changing foldmethod does not modify the buffer for you.

You could perhaps script something that would find all fold start/end locations and modify the buffer to insert markers but that would not be trivial and I'm not sure you'd want to have that happen in a code file anyway.

Ben Fritz

unread,
Jan 6, 2015, 11:05:09 PM1/6/15
to vim...@googlegroups.com
On Tuesday, January 6, 2015 9:16:09 PM UTC-6, Bao Niu wrote:
What is your eventual goal? If you just want to make manual folds persistent, you can save a view with the :mkview command, and load it with :loadview.

Bao Niu

unread,
Jan 6, 2015, 11:16:38 PM1/6/15
to vim...@googlegroups.com
Thank you very much Ben!

My goal would be folding Python classes/functions automatically based on indentation, and also folding the import part at the top and some python long dict. The latter does not necessarily conform to indentation rules so marker would be ideal.

The only concern about using "manual" method is once I switch to manual, can I still automatically fold up classes that are created after switching? What do pros normally do to solve this? Any hint or url would be highly appreciated. Thanks.


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/ibbZ08p2cNg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Fritz

unread,
Jan 7, 2015, 8:22:13 AM1/7/15
to vim...@googlegroups.com
On Tuesday, January 6, 2015 10:16:38 PM UTC-6, Bao Niu wrote:
> Thank you very much Ben!
>
> My goal would be folding Python classes/functions automatically based on indentation, and also folding the import part at the top and some python long dict. The latter does not necessarily conform to indentation rules so marker would be ideal.
>
> The only concern about using "manual" method is once I switch to manual, can I still automatically fold up classes that are created after switching? What do pros normally do to solve this? Any hint or url would be highly appreciated. Thanks.
>
>
>


In that case, it sounds like you want to define a custom function to return a fold level, and use that as the foldexpr with foldmethod=expr.

When writing this function, the indent() function should be helpful.

Example here, plus some links and plugins: http://vim.wikia.com/wiki/Folding_for_plain_text_files_based_on_indentation

There may be better examples, that's what I could find in 5 seconds or less :-)

Bao Niu

unread,
Jan 7, 2015, 4:02:21 PM1/7/15
to vim...@googlegroups.com
Thanks. Where does Vim store all my defined folds? It creates a separate buffer like NERDTree does?

Ben Fritz

unread,
Jan 8, 2015, 11:02:57 AM1/8/15
to vim...@googlegroups.com
On Wednesday, January 7, 2015 3:02:21 PM UTC-6, Bao Niu wrote:
> Thanks. Where does Vim store all my defined folds? It creates a separate buffer like NERDTree does?
>

Please don't top-post. Reply BENEATH the message you are responding to, as I am doing here.

I don't understand your question. Vim does not "store" your folds anywhere, unless you're referring to the :mkview command, in which case a separate file is used. Where that file is stored should be documented in :help :mkview or one of the linked help topics. I suggested you create a fold expression to use with foldmethod=expr. This expression is best placed into a function which is loaded with the rest of your Vim config. Alternatively, you can create a one-liner expression (not a function) and put it in a modeline in the file itself, but that is generally less useful.

Bao Niu

unread,
Jan 8, 2015, 9:04:18 PM1/8/15
to vim...@googlegroups.com
Thanks Ben, I will conform to the convention and won't top-post in future.
By 'storing' I actually did not refer to persisting, but temporary. When I use foldmethod=expr to automatically fold up my classes, the foldlevel must be 'stored' somewhere, or otherwise after I switch to manual it would have been all lost. Is there something I'm wrong about this model? Then where does Vim temporarily store such thing? Enviroment variables? Or something?

Ben Fritz

unread,
Jan 9, 2015, 12:02:09 PM1/9/15
to vim...@googlegroups.com
On Thursday, January 8, 2015 at 8:04:18 PM UTC-6, Bao Niu wrote:
>
> Thanks Ben, I will conform to the convention and won't top-post in future.
> By 'storing' I actually did not refer to persisting, but temporary. When I use foldmethod=expr to automatically fold up my classes, the foldlevel must be 'stored' somewhere, or otherwise after I switch to manual it would have been all lost. Is there something I'm wrong about this model? Then where does Vim temporarily store such thing? Enviroment variables? Or something?

The idea with using a fold expression is that you won't NEED to switch to manual mode. All your folds would be defined automatically by the function you write.

When you switch fold method, Vim knows in its own internal window state where folds are, and it just keeps them around to initialize the manual folding. There is no associated buffer or user-accessible variable containing all the folds, but you can figure them out using the foldlevel(), foldclosed(), foldclosedend() functions.
Reply all
Reply to author
Forward
0 new messages