我可以让Vim忽略文件顶部的许可证块吗?

时间:2010-02-12 06:18:43

标签: vim plugins licensing folding boilerplate

有没有办法使用折叠或其他一些Vim脚本黑魔法来隐藏文件顶部的许可证块?我不喜欢他们占据我编辑窗格的这么大一部分;我喜欢在第一次打开它时感觉文件正在做什么,而不是满脸的样板。

4 个答案:

答案 0 :(得分:4)

在自动命令中尝试此操作。

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

适当调整第4行的范围。在最坏的情况下,版权在不同的地方开始,并且是可变长度,这种模式应该这样做:

1,/Beginning of copyright/;/End of copyright/

答案 1 :(得分:2)

这取决于许可证块是否具有一致的形式以及您正在编程的语言。例如,python倾向于使用'foldexpr'来定义折叠,所以要添加它,您必须更换现有的新功能(或摆脱现有的折叠)。我相信C中的默认值是使用手动折叠(虽然我可能会自己配置它:我不记得了),所以这更容易添加额外的折叠。

使用简单的GPL版权信息(如本文末尾的版权信息),您可以将foldmethod设置为手动,并具有创建折叠的简单功能。这一切都取决于版权的形式以及维持现有折叠的重要性。我担心我需要更多细节来提供更有用的答案。无论如何,这是一个示例脚本,可用于在本文末尾折叠版权声明:

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()

假设有这样的版权声明:

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

答案 2 :(得分:2)

我为此创建了一个小的vim插件。它应该折叠页面的第一个评论时。它适用于我的测试用例,但是,当然,任何改进都是受欢迎的。添加其他单行或多行标识符应该很容易。

获取here。要像任何其他插件一样安装,只需将其放入〜/ .vim / plugin。

修改:将链接更改为vim.org并清理了答案

答案 3 :(得分:0)

删除它们怎么样?严重。

源代码受权利所有权和许可保护,而不是样板。它不需要在那里 - 至少在大多数情况下。

如果GPL和其他类似的方案确实有效地要求文本存在,它可以移动到文件的底部或其他任何内容。