title: Compact C Folding in Vim
body: <p>I'm trying to make a simple Vim script that would create very compact top-level folds for c files. Ideally, if it was run on this code:</p>
  
  <pre><code>static void funca(...)
  {
     ... 
  }
  
  /* Example comment */
  static void funcb(...)
  {
     ... 
  }
  </code></pre>
  
  <p>Then it would create folds which would look like this when closed: </p>
  
  <pre><code>+-- x Lines: static void funca(...)----------------------
  +-- x Lines: static void funcb(...)----------------------
  </code></pre>
  
  <p>So basically it would be like foldmethod=syntax with foldlevel=1, except that each fold would start one line further up, and would extend further down to include all following blank lines.</p>
  
  <p>I know how to make one of these folds (assuming foldmethod=manual):</p>
  
  <pre><code>/^{&lt;cr&gt;kVnn?^$&lt;cr&gt;zf
  </code></pre>
  
  <p>But I'm not sure how to put it into a function. This is my effort:</p>
  
  <pre><code>function Cfold()
    set foldmethod=manual  " Manual folds
    ggzE                   " Delete all folds
    while (/^{&lt;cr&gt;)        " Somehow loop through each match
       kVnn?^$&lt;cr&gt;zf       " This would work fine except for the last function
    endwhile
  endfunction
  map &lt;Leader&gt;f  :call Cfold()&lt;cr&gt;
  </code></pre>
  
  <p>But it isn't valid, I'm not entirely sure how functions work. Also, it won't work for the last function in the file, since it won't find '^{' again. If someone could help me get this working, and somehow add a case for the last function in a file, I would be extremely grateful.  </p>
  
  <p>Thanks in advance :)</p>