我们在所有源代码文件的顶部都有15行长的版权信息。
当我在emacs中打开它们时,这会浪费很多宝贵的空间。
有没有办法让emacs总是隐藏某个消息,但仍然把它留在文件中?
发布于 2011-02-08 01:41:22
您可以编写一个函数,将缓冲区范围缩小到除前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中打开它们时,这浪费了很多宝贵的空间。
...or,你可以向下滚动。为了自动实现这一点,我们可以使用以下函数而不是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-<)的开头时,你就必须再次手动向下滚动。使用缩小解决方案时不会出现此问题。
发布于 2012-12-24 12:40:44
您可以使用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)注意,它不仅隐藏了版权,还隐藏了完整的初始注释块,这可能也隐藏了有用的文档。
发布于 2011-02-08 00:08:54
看看folding-mode吧。基本上,您只需要一种方法来识别要折叠的部分,然后使用folding-top-mark和folding-bottom-mark对它们进行标记。顺便说一句,使用EMACS elisp代码可以做到这一点,所以你应该可以很容易地找到可以修改的代码。
https://stackoverflow.com/questions/4923459
复制相似问题