在所有文件顶部隐藏长版权消息
我们所有源代码文件的顶部都有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 hideshow 次要模式,这是一个标准的内置软件包,具有一个名为
hs-hide-initial-comment-block
的通用命令,可以执行您想要的操作,而无需知道顶部评论部分的长度。您可以将其添加到任何语言的模式挂钩中,但这里有一个使用 C 的示例:注意,它并不专门隐藏只是版权,而是可能隐藏有用文档的完整初始注释块,以及。
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:Note, it does not hide specifically just the copyrights, but the full initial comment block which may hide useful documentation, as well.
您可以编写一个函数,将缓冲区缩小到除前 15 行之外的所有内容。
然后您需要做的就是确保为每个包含版权说明的文件调用此函数。这可以通过添加一个钩子来完成,最好是添加到文件的主模式。例如,您可以将上述函数定义和以下行添加到您的 .emacs 文件中:
每当您打开 C 文件时,这都会调用函数“hide-copyright-note”。
在实践中,您可能希望使您的钩子函数更加聪明,可以通过检查要隐藏的版权说明是否实际存在,或者仅在文件位于某个目录中时运行 hide-copyright-note 等。
例如,坚持使用 C 示例,您可以将以下测试插入到上述函数中:
至于您的其他问题:
...或者你可以向下滚动。为了自动实现这一点,我们可以使用以下函数来代替
hide-copyright-note
:但是,我推荐第一个变体的原因是,如果您只是自动向下滚动,那么每当您跳转到缓冲区的开头(
M-<
),您必须再次手动向下滚动。使用缩小解决方案不会出现此问题。You could write a function that narrows your buffer to everything but the first 15 lines.
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:
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:
As for your other concern:
...or you could just scroll down. To achieve this automatically, we could use the following function instead of
hide-copyright-note
: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.查看折叠模式。基本上,您所需要的只是一种识别要折叠的部分的方法,然后使用
folding-top-mark
和folding-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
andfolding-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.自 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
.