2011-02-07 55 views
5

我們在所有源代碼文件的頂部有15行長的版權信息。隱藏所有文件頂部的長版權信息

當我在emacs中打開它們時,浪費了很多寶貴的空間。
有什麼辦法讓emacs總是隱藏某個消息,但仍然保留在文件中?

回答

3

您可以編寫一個函數,將緩衝區縮小到除前15行外的所有內容。

(defun hide-copyright-note() 
    "Narrows the current buffer so that the first 15 lines are 
hidden." 
    (interactive) 
    (save-excursion 
    (goto-char (point-min)) 
    (forward-line 15) 
    (narrow-to-region (point) (point-max)))) 

然後,您需要做的就是確保爲包含版權備註的每個文件調用此函數。這可以通過添加一個鉤子來完成,最好是添加到文件的主要模式。例如,你可以在上面的函數定義和下面的行添加到您的.emacs文件:

(add-hook 'c-mode-hook 'hide-copyright-note) 

每當你打開一個C文件這將調用函數「隱藏受版權保護的音符。

實際上,您可能希望通過檢查隱藏的版權說明是否真實存在,或僅在文件位於某個目錄中時才運行hide-copyright-note,以使其更加靈活。

例如,要堅持用C例如,您可以插入下面的測試到上述功能:

(defun hide-copyright-note() 
    "Narrows the current buffer so that the first 15 lines are 
hidden." 
    (interactive) 
    (when (copyright-message-p) 
    (save-excursion 
     (goto-char (point-min)) 
     (forward-line 15) 
     (narrow-to-region (point) (point-max))))) 

(defun copyright-message-p() 
    "Returns t when the current buffer starts with a Copyright 
note inside a C-style comment" 
    (save-excursion 
    (goto-char (point-min)) 
    (looking-at "\\s */\\*\\(:?\\s \\|\\*\\)*Copyright\\b"))) 

至於你的其他擔憂:

當我在emacs中打開它們時,浪費了很多寶貴的空間。

...或者你可以向下滾動。爲了自動實現這一點,我們可以使用下面的函數,而不是hide-copyright-note

(defun scroll-on-copyright() 
    "Scrolls down to the 16th line when the current buffer starts 
with a copyright note." 
    (interactive) 
    (when (copyright-message-p) 
    (goto-char (point-min)) 
    (beginning-of-line 16) 
    (recenter 0))) 

但是,我之所以推薦第一個變化是,如果你僅僅是下自動滾動,然後當你跳轉到緩衝區的開始(M-<),您將不得不再次手動向下滾動。縮小解決方案不會出現此問題。

+0

謝謝,我會試一試。 – sligocki 2011-02-08 15:15:52

0

看一看folding-mode。基本上,您只需要一種方法來識別要摺疊的部件,然後使用folding-top-markfolding-bottom-mark來標記它們。順便說一句,EMACS elisp代碼有這樣的做法,所以你應該很容易找到可以修改的代碼。

+0

使用[hideshow minor mode](http:// stackoverflow。com/a/14017141/462302),這是一個內置的軟件包,而不是摺疊模式。 – aculich 2012-12-24 04:42:59

5

您可以使用hideshow minor mode這是一個標準的內置軟件包,它具有一個名爲hs-hide-initial-comment-block的通用命令,它可以完成您想要的功能,而無需知道頂級註釋部分有多長。你可以把它添加到任何語言的方式掛接,但在這裏是用C的例子:

(add-hook 'c-mode-common-hook 'hs-minor-mode t) 
(add-hook 'c-mode-common-hook 'hs-hide-initial-comment-block t) 

注意,它並沒有特別隱藏只是的版權,但完整的初始註釋塊可能隱藏有用文檔,以及。