views:

133

answers:

3

Is there a way to use folds or some other Vim-script black magic to hide license blocks at the top of files? I don't like that they take up such a large section of my editing pane; I like to get a sense for what a file is doing when I first open it, rather than a face-full of boilerplate.

A: 

How about deleting them? Seriously.

Source code is protected by rights ownership and licensing, not the boilerplate. It doesn't need to be there—at least in most cases.

In the case of GPL and other similar schemes which do effectively require the text be present, it can be moved to the bottom of the file or whatever.

wallyk
And what If one wants people to read the relevant license information **first** ? Vim is powerful enough to fold it automatically and easily so it's no big deal anyway...
ereOn
+2  A: 

It depends on whether there's a consistent form to the licence block and what language you're programming in. For example, python tends to use a 'foldexpr' to define folding, so to add this you'd have to replace the existing function with a new one (or get rid of the existing folding). I believe the default in C is to use manual folding (although it's possible that I configured it that way myself: I can't remember), so this is much easier to add extra folding.

With a simple GPL copyright message like the one at the end of this post, you could set foldmethod to manual and have a simple function that creates a fold. It all depends on the form of the copyright and how important it is for you to maintain the existing folding. I'm afraid I'd need a bit more detail to give a more useful answer. Anyway, here's an example script that could be used to fold the copyright notice at the end of this post:

function! CreateCopyrightFold()
    let InCopyright = 0
    set foldmethod=manual
    for Line in range(1,line('$'))
        let LineContents = getline(Line)
        if LineContents !~ "^#"
            if InCopyright
                let CopyrightEnd = Line - 1
                exe CopyrightStart . ',' . CopyrightEnd . 'fold'
            endif
            break
        elseif LineContents =~ "Copyright"
            let InCopyright = 1
            let CopyrightStart = Line
        endif
    endfor
endfunction
au BufRead *.py call CreateCopyrightFold()

Assuming a copyright notice like this one:

# Copyright (C) 2009 Some Company or Something
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import sys
# Code continues...
Al
+3  A: 

Try this in an autocommand.

function! FoldCopyright
  if !exists( "b:foldedCopyright" )
    let b:foldedCopyright = 1
    1,15fold
  endif
endfunction

Adjust the range on line 4 appropriately. In the worst case where the copyright starts in different places and is a variable length this pattern should do:

1,/Beginning of copyright/;/End of copyright/
Steve K
I recommand using `silent!` in front of the fold command so that if the license block doesn't exist, Vim doesn't complain.
ereOn