Wikia

Vim Tips Wiki

Changes: Folding functions with the prototype included

Edit

Back to page

(Merged with 108. All functionality now covered by syntax files. Deprecated.)
(not covered in folding tip, explain why)
 
Line 1: Line 1:
{{merged|108}}
+
{{Review}}
{{deprecated|All functionality is better covered in syntax folding in [[Folding|Tip 108: Folding]]. Deprecated, as syntax files are now used.}}
 
 
{{TipImported
 
{{TipImported
 
|id=523
 
|id=523
Line 12: Line 12:
 
|category2=Folding
 
|category2=Folding
 
}}
 
}}
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.
+
The built-in [[Folding|syntax based folding]] of C/C++ files is good enough for most users, but it will only fold functions from the "{" to the "}". A user may want to fold the prototype as well. Using foldexpr allows this.
   
 
<pre>
 
<pre>
Line 28: Line 28:
 
</pre>
 
</pre>
   
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, for example:
  +
<pre>
 
void func()
 
void func()
 
{
 
{
 
....
 
....
 
}
 
}
  +
</pre>
   
 
==Comments==
 
==Comments==
  +
{{todo}}
  +
*Merge in comments below, it looks like there's an improved version of the script, should test
  +
*Address some shortcomings? This is actually a very difficult problem, Perl syntax tries to do it and fails.
  +
*Maybe use the syntax items in the fold expression for help
  +
  +
----
  +
 
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.
 
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.
   

Latest revision as of 04:49, January 12, 2011

Tip 523 Printable Monobook Previous Next

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


The built-in syntax based folding of C/C++ files is good enough for most users, but it will only fold functions from the "{" to the "}". A user may want 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, for example:

  void func()
  {
    ....
  }

CommentsEdit

 TO DO 

  • Merge in comments below, it looks like there's an improved version of the script, should test
  • Address some shortcomings? This is actually a very difficult problem, Perl syntax tries to do it and fails.
  • Maybe use the syntax items in the fold expression for help

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

Around Wikia's network

Random Wiki