Compact C Warehouse in Vim

I am trying to create a simple Vim script that will create very compact top-level folds for c files. Ideally, if it was running on this code:

static void funca(...) { ... } /* Example comment */ static void funcb(...) { ... } 

Then he will create folds that will look like this:

 +-- x Lines: static void funca(...)---------------------- +-- x Lines: static void funcb(...)---------------------- 

So basically it would be like foldmethod = syntax with foldlevel = 1, except that each summary would start one line further and expand further to include all the following empty lines.

I know how to make one of these folds (assuming foldmethod = manual):

 /^{<cr>kVnn?^$<cr>zf 

But I'm not sure how to include it in a function. These are my efforts:

 function Cfold() set foldmethod=manual " Manual folds ggzE " Delete all folds while (/^{<cr>) " Somehow loop through each match kVnn?^$<cr>zf " This would work fine except for the last function endwhile endfunction map <Leader>f :call Cfold()<cr> 

But this is not true, I'm not quite sure how the functions work. In addition, it will not work for the last function in the file, since it will no longer find "^ {". If someone can help me get this work and somehow add a case for the last function to the file, I would be extremely grateful.

Thanks in advance:)

+3
source share
1 answer

You can create software folds using foldexpr and foldtext . Try this, although you may need to configure CFoldLevel so that it does not swallow the broken parts of the code:

 function! CFoldLevel(lnum) let line = getline(a:lnum) if line =~ '^/\*' return '>1' " A new fold of level 1 starts here. else return '1' " This line has a foldlevel of 1. endif endfunction function! CFoldText() " Look through all of the folded text for the function signature. let signature = '' let i = v:foldstart while signature == '' && i < v:foldend let line = getline(i) if line =~ '\w\+(.*)$' let signature = line endif let i = i + 1 endwhile " Return what the fold should show when folded. return '+-- ' . (v:foldend - v:foldstart) . ' Lines: ' . signature . ' ' endfunction function! CFold() set foldenable set foldlevel=0 set foldmethod=expr set foldexpr=CFoldLevel(v:lnum) set foldtext=CFoldText() set foldnestmax=1 endfunction 

See :help 'foldexpr' .

+2
source

All Articles