开发者

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.


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/


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...


I created a small vim plugin for this. It divines when the first comment of the page should be folded. It worked on my test cases, but, of course, any improvement is welcome. It should be easy to add other single or multiple line identifiers.

Get it here. To install, like any other plugin, just drop it into your ~/.vim/plugin.

Edit: Changed the link to vim.org and cleaned up the answer


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜