2010-02-12 64 views
8

有沒有辦法使用摺疊或其他Vim腳本黑魔法來隱藏文件頂部的許可證塊?我不喜歡他們佔用我編輯窗格中的很大一部分;我喜歡瞭解一個文件在我第一次打開時的作用,而不是滿臉的樣板。我可以讓Vim忽略文件頂部的許可證塊嗎?

+0

想要忽略的文本格式是什麼? – 2010-02-12 16:50:08

回答

4

在自動命令試試這個。

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

適當調整第4行的範圍。在最差的情況下,版權開始於不同的地方,並且這種模式應該是可變長度:

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

我在fold命令前面使用'silent!'建議,這樣如果許可證塊不存在,Vim就不會抱怨。 – ereOn 2010-05-17 17:03:50

0

刪除它們怎麼樣?認真。

源代碼受版權所有權和許可保護,而不是樣板文件。它不需要在那裏 - 至少在大多數情況下。

在GPL和其他類似方案的情況下,它們有效地要求文本存在,它可以移動到文件的底部或任何其他地方。

+0

如果希望人們首先閱讀**相關許可證信息** Vim足夠強大,可以自動輕鬆地摺疊它,所以它沒什麼大不了的...... – ereOn 2010-05-17 09:06:39

2

這取決於是否存在與許可證塊一致的形式以及您正在編程的語言。例如,python傾向於使用'foldexpr'來定義摺疊,因此要添加它,您必須替換現有的功能與新的功能(或擺脫現有的摺疊)。我相信C中的默認值是使用手動摺疊(儘管可能我自己配置​​了這種方式:我不記得了),因此添加額外摺疊更容易。

通過一個簡單的GPL版權信息,就像本文末尾的GPL版權信息一樣,您可以將foldmethod設置爲manual,並有一個簡單的函數來創建摺疊。這完全取決於版權的形式,以及保持現有摺疊的重要性。恐怕我需要更多的細節來給出更有用的答案。不管怎樣,下面是可以用來版權聲明在這篇文章的末尾折一個示例腳本:

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

我爲此創建了一個小vim插件。當頁面的第一條評論應該被摺疊時,它就會發生爭執。它對我的測試用例起作用,但是,當然,任何改進都是值得歡迎的。應該很容易添加其他單個或多個線路標識符。

得到它here。像任何其他插件一樣安裝,只需將其放入〜/ .vim /插件。

編輯:將鏈接更改爲vim.org並清理了答案

+0

你爲什麼不把這個vim.org作爲腳本提交? – 2012-11-13 20:41:10

+0

很好的問題。看起來不太有用。 – fotanus 2013-02-01 10:50:13

相關問題