在所有文件的顶部隐藏长版权信息

时间:2011-02-07 16:04:21

标签: emacs hide copyright-display

我们在所有源代码文件的顶部都有15行长版权信息。

当我在emacs中打开它时,浪费了很多宝贵的空间 是否有任何方法可以让emacs始终隐藏某个消息但仍留在文件中?

3 个答案:

答案 0 :(得分:7)

您可以使用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)

请注意,它并不专门隐藏只是版权,而是隐藏有用文档的完整初始注释块。

答案 1 :(得分: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。

在实践中,您可能希望通过检查隐藏的版权注释是否实际存在,或者仅当文件位于某个目录中时运行hide-copyright-note等,才能使您的钩子函数更加聪明。< / p>

例如,要坚持使用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-<)时,您将不得不再次手动向下滚动。缩小解决方案不会出现此问题。

答案 2 :(得分:1)

看看folding-mode。基本上,您只需要一种识别要折叠的部分的方法,然后使用folding-top-markfolding-bottom-mark来标记它们。顺便说一句,有一些黑客可以使用EMACS elisp代码,因此您应该能够轻松找到可以调整的代码。