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

发布于 2024-10-16 04:03:23 字数 108 浏览 6 评论 0原文

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

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

We have 15 line long copyright messages at the top of all our source-code files.

When I have them open in emacs, that wastes a lot of valuable space.
Is there any way to get emacs to always a hide a certain message but still leave it in the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷梦 2024-10-23 04:03:23

您可以使用 hideshow 次要模式,这是一个标准的内置软件包,具有一个名为 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)

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

You can use hideshow minor mode which is a standard built-in package that has a generalized command called hs-hide-initial-comment-block that will do what you want without having to know how long the top comment section is. You can add it to the mode-hook of any language, but here's an example using C:

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

Note, it does not hide specifically just the copyrights, but the full initial comment block which may hide useful documentation, as well.

注定孤独终老 2024-10-23 04:03:23

您可以编写一个函数,将缓冲区缩小到除前 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 等。

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

You could write a function that narrows your buffer to everything but the first 15 lines.

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

Then all you need to do is make sure that this function is called for every file that contains a copyright note. This can be done by adding a hook, preferably to the major mode of your file. For instance you could add the above function definition and the following line to your .emacs file:

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

This would call the function 'hide-copyright-note whenever you open a C file.

In practice, you would probably want to make your hook-function more clever, either by checking if a copyright note to hide actually exists or by running hide-copyright-note only if a file is in a certain directory etc.

For instance, to stick with the C example, you could insert the following test into the above function:

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

As for your other concern:

When I have them open in emacs, that wastes a lot of valuable space.

...or you could just scroll down. To achieve this automatically, we could use the following function instead of 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)))

However, the reason I recommended the first variation is that if you merely scroll down automatically, then whenever you jump to the beginning of the buffer (M-<) you'll have to scroll down again manually. This problem does not occur with the narrowing solution.

一直在等你来 2024-10-23 04:03:23

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

Have a look at folding-mode. Basically, all you need is a way to identify the parts to be folded, and then use folding-top-mark and folding-bottom-mark to mark them. There are hacks to do that with EMACS elisp code by the way, so you should be easily able to find code that can be adapted.

我是有多爱你 2024-10-23 04:03:23

自 Emacs-21 起,Emacs 就专门为此提供了 elide-head
在 Emacs-29 中,我们将其重命名为 elide-head-mode。

Emacs comes with elide-head, specifically for that, since Emacs-21.
In Emacs-29 we rename it to elide-head-mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文