Vim Tips Wiki
m (Refomatting + Caterogies C and Folding)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=523
 
|id=523
  +
|previous=522
|title=Folding functions with the prototype included
 
  +
|next=524
|created=August 2, 2003 14:46
+
|created=August 2, 2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Matt Perry
 
|author=Matt Perry
 
|version=6.0
 
|version=6.0
 
|rating=11/4
 
|rating=11/4
|text=
 
I used to use folding to fold functions in C/C++ from the "{" to the "}", but I wanted a way to fold the prototype as well. Using foldexpr allows this.
 
 
}}
 
}}
 
I used to use folding to fold functions in C/C++ from the "{" to the "}", but I wanted a way to fold the prototype as well. Using foldexpr allows this.
   
  +
<pre>
function FoldBrace()
+
function FoldBrace()
if getline(v:lnum+1)[0] == '{'
+
if getline(v:lnum+1)[0] == '{'
return '&gt;1'
+
return '&gt;1'
endif
+
endif
 
if getline(v:lnum)[0] == '}'
+
if getline(v:lnum)[0] == '}'
return '&lt;1'
+
return '&lt;1'
endif
+
endif
return foldlevel(v:lnum-1)
+
return foldlevel(v:lnum-1)
endfunction
+
endfunction
 
set foldexpr=FoldBrace()
 
 
set foldmethod=expr
set foldexpr=FoldBrace()
 
  +
</pre>
set foldmethod=foldexpr
 
   
Note that this will only work if you put the braces on lines by themselves in the very first column, ie:
+
Note that this will only work if you put the braces on lines by themselves in the very first column, ie:
void func()
+
void func()
{
+
{
....
+
....
 
}
 
}
   
 
==Comments==
 
Johannes Zellner started to define a fold(ing?) plugin for C and C++. I've tried to enhanced it a little, but unfortunately, it is still imperfect.
   
 
See http://hermitte.free.fr/vim/ressources/vimfiles/fold/c-fold.vim
== Comments ==
 
Johannes Zellner started to define a fold(ing?) plugin for C &amp; C++.
 
   
 
--[[User:Luc Hermitte|Luc Hermitte]] 13:39, 7 June 2007 (UTC), August 2, 2003 18:34
I've tried to enhanced it a little, but unfortunately, it is still imperfect.
 
 
You may found it at: http://hermitte.free.fr/vim/ressources/vimfiles/fold/c-fold.vim
 
 
May it inspires you.
 
 
--[[User:Luc Hermitte|Luc Hermitte]] 13:39, 7 June 2007 (UTC), August 2, 2003 18:34
 
 
----
 
----
That certainly is more featureful than my version. But I prefer to use foldnestmax=1 - I find it annoying to have more than 1 fold level.
+
That certainly is more featureful than my version. But I prefer to use foldnestmax=1 - I find it annoying to have more than 1 fold level.
   
I've modified my fold function to work if the { is on the same line as the function, ie:
+
I've modified my fold function to work if the { is on the same line as the function, ie:
void bla() {
+
void bla() {
}
+
}
   
I'd like to be able to use v:foldstart in my fold function so I can check that the } has the same indent as the line containing the {. That way I could match braces that aren't in the first column. But this is good enough for my purposes.
+
I'd like to be able to use v:foldstart in my fold function so I can check that the } has the same indent as the line containing the {. That way I could match braces that aren't in the first column. But this is good enough for my purposes.
   
  +
<pre>
 
function FoldBrace()
 
if getline(v:lnum+1)[0] == '{'
 
return 1
 
endif
 
if getline(v:lnum) =~ '{'
 
return 1
 
endif
 
if getline(v:lnum)[0] =~ '}'
 
return '&lt;1'
 
endif
 
return -1
 
endfunction
  +
</pre>
   
function FoldBrace()
 
if getline(v:lnum+1)[0] == '{'
 
return 1
 
endif
 
if getline(v:lnum) =~ '{'
 
return 1
 
endif
 
if getline(v:lnum)[0] =~ '}'
 
return '&lt;1'
 
endif
 
return -1
 
endfunction
 
 
'''Anonymous''', August 2, 2003 22:40
 
----
 
a minor mistake:
 
 
set foldmethod=expr
 
 
astrobe--AT--netcourrier.com
 
, August 5, 2003 4:18
 
----
 
How can I have both these forms fold andstill see prototype in a single line.
 
 
void func()
 
{
 
...
 
}
 
 
void func() {
 
...
 
}
 
 
vasud--AT--mailcity.com
 
, August 27, 2003 12:40
 
 
----
 
----
<!-- parsed by vimtips.py in 0.442963 seconds-->
 
 
[[Category:C]]
 
[[Category:C]]
 
[[Category:Folding]]
 
[[Category:Folding]]

Revision as of 07:06, 4 November 2007

Tip 523 Printable Monobook Previous Next

created August 2, 2003 · complexity intermediate · author Matt Perry · version 6.0


I used to use folding to fold functions in C/C++ from the "{" to the "}", but I wanted a way to fold the prototype as well. Using foldexpr allows this.

function FoldBrace()
  if getline(v:lnum+1)[0] == '{'
    return '>1'
  endif
  if getline(v:lnum)[0] == '}'
    return '<1'
  endif
  return foldlevel(v:lnum-1)
endfunction
set foldexpr=FoldBrace()
set foldmethod=expr

Note that this will only work if you put the braces on lines by themselves in the very first column, ie:

 void func()
 {
   ....
 }

Comments

Johannes Zellner started to define a fold(ing?) plugin for C and C++. I've tried to enhanced it a little, but unfortunately, it is still imperfect.

See http://hermitte.free.fr/vim/ressources/vimfiles/fold/c-fold.vim

--Luc Hermitte 13:39, 7 June 2007 (UTC), August 2, 2003 18:34


That certainly is more featureful than my version. But I prefer to use foldnestmax=1 - I find it annoying to have more than 1 fold level.

I've modified my fold function to work if the { is on the same line as the function, ie:

void bla() {
}

I'd like to be able to use v:foldstart in my fold function so I can check that the } has the same indent as the line containing the {. That way I could match braces that aren't in the first column. But this is good enough for my purposes.

function FoldBrace()
  if getline(v:lnum+1)[0] == '{'
    return 1
  endif
  if getline(v:lnum) =~ '{'
    return 1
  endif
  if getline(v:lnum)[0] =~ '}'
    return '<1'
  endif
  return -1
endfunction