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

sli*_*cki 5 emacs hide copyright-display

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

当我在emacs中打开它们时,浪费了很多宝贵的空间.
有没有办法让emacs始终隐藏某个消息,但仍然留在文件中?

acu*_*ich 7

您可以使用hideshow minor模式,这是一个标准的内置包,它具有一个通用命令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)
Run Code Online (Sandbox Code Playgroud)

请注意,这并不能掩盖明确刚刚的版权,但其可能隐藏有用的文档,以及完整的初始注释块.


Tho*_*mas 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))))
Run Code Online (Sandbox Code Playgroud)

然后您需要做的就是确保为每个包含版权说明的文件调用此函数。这可以通过添加一个钩子来完成,最好是添加到文件的主模式。例如,您可以将上述函数定义和以下行添加到您的 .emacs 文件中:

(add-hook 'c-mode-hook 'hide-copyright-note)
Run Code Online (Sandbox Code Playgroud)

每当您打开 C 文件时,这都会调用函数“hide-copyright-note”。

在实践中,您可能希望使您的钩子函数更加聪明,可以通过检查要隐藏的版权说明是否实际存在,或者仅在文件位于某个目录中时运行 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")))
Run Code Online (Sandbox Code Playgroud)

至于您关心的其他问题:

当我在 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)))
Run Code Online (Sandbox Code Playgroud)

然而,我推荐第一个变体的原因是,如果您只是自动向下滚动,那么每当您跳到缓冲区的开头 ( M-<) 时,您都必须再次手动向下滚动。使用缩小解决方案不会出现此问题。