Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have Vim ignore a license block at the top of a file?

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.

like image 522
Steven Dee Avatar asked Feb 12 '10 06:02

Steven Dee


2 Answers

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/
like image 78
Steve K Avatar answered Nov 11 '22 12:11

Steve K


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...
like image 22
DrAl Avatar answered Nov 11 '22 13:11

DrAl