Foldmethod=Syntax

1 view
Skip to first unread message

thilo.b...@googlemail.com

unread,
Dec 17, 2008, 9:16:04 AM12/17/08
to vim_use
Hi,

while editing source code, e.g. C/C++ code, and foldmethod=syntax is
set, inserting syntax sensible characters, e.g. '{' or '}' causes all
following folds to open unintentionally. Which might be caused by a
confused syntax-parser.

This behaviour is not influenced by the "foldopen" option.

Does anyone know about this and has a solution or an idea how this
behaviour can be supressed?

An applicable workaround might be changing the foldmethod when
inserting/exiting insert mode to a non-confused method - may be
someone has an idea or skeleton for such a script/plugin?

Thanks inadvance,
T. Borgmann

anhnmncb

unread,
Dec 17, 2008, 9:03:15 PM12/17/08
to vim...@googlegroups.com

Hi, that's annoying me with a long time too, I have to :syntax enable then
zMi<ESC> to restore a outline layout.


--
Regards,
anhnmncb

fritzophrenic

unread,
Dec 18, 2008, 11:12:09 AM12/18/08
to vim_use


On Dec 17, 8:03 pm, anhnmncb <anhnm...@sina.com> wrote:
>
> Hi, that's annoying me with a long time too, I have to :syntax enable then
> zMi<ESC> to restore a outline layout.
>

Also see ;help zx and :help zX for a potentially better way to restore
the fold layout.

T. Borgmann

unread,
Dec 18, 2008, 2:14:28 PM12/18/08
to vim_use
Thanks for your appreciation, folks!

Well yes you're right, but restoring my foldings manually would
require me to assimilate two more shortcuts into my brain ;)

Here is my temporary solution, temporary, because it shows an
unexpected behaviour which is also unwanted but not as disattracting
to me like all following folds to get opened.

My approach uses the "InsertEnter" and "InsertLeave" events called -
you guess it - when entering or leaving insert mode.
I wrote two functions for theese events to store the current folding
mehtod (foldmethod, e.g. foldmehtod=syntax ;) ) into a temporary
variable, which shall reside in the same scope than "foldmethod"
itself does. Therefore it should be as window dependend as
"foldmethod" is. The temporarily stored original fold method is then
restored when leaving insertion mode. During insertion mode,
"foldmethod" is set to "manual" which should be most applicable.

Now comes the drawback. For some reason, all new top-level functions
writting during this "manual-mode-insertion-session" become folded. I
think the reason for that might be vim reparsing for folding issues
automatically when changing the fold method used.
Nevertheless new text inserted in some existing functions does not
become folded.

Anyway, I'm much more pleased with that - in fact, autofolding my new
function can also be seen as a handy feature - solution, than with the
standard behaviour.

So, here it is. I pasted it into my .gvimrc, if you are more strict
than me concerning file integrety, you might want to put theese lines
into a .vim file and put it into your plugins directory.

Any comments & proposals etc. are most welcome and forgive me, this is
my very first self-made-function-based script (after about 7 years of
using the worlds best editor)


""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" for correct syntax-folding while inserting new text/code
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

function PreserveFoldingsEnter()
let w:preservedMethod = &foldmethod
let &foldmethod="manual"
endfunction

function PreserveFoldingsLeave()
let &foldmethod=w:preservedMethod
endfunction

autocmd InsertEnter * call PreserveFoldingsEnter()
autocmd InsertLeave * call PreserveFoldingsLeave()

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

anhnmncb

unread,
Dec 20, 2008, 6:51:57 AM12/20/08
to vim...@googlegroups.com
On 2008-12-18, fritzophrenic wrote:

Yes, it works like zMi<ESC>, nice tip, thank you!

But :syntax enable is still needed.


--
Regards,
anhnmncb

Reply all
Reply to author
Forward
0 new messages