自动折叠emacs中的多行注释

时间:2012-05-06 07:38:03

标签: emacs comments long-integer lines folding

在我的.emacs配置中,我有以下内容:

(defun fold-long-comment-lines ()
"This functions allows us to fold long comment lines
 automatically in programming modes. Quite handy."
 (auto-fill-mode 1)
 (set (make-local-variable 'fill-no-break-predicate)
     (lambda ()
         (not (eq (get-text-property (point) 'face)
                'font-lock-comment-face)))))

以上内容作为“c-mode-common-hook”的一部分被调用,并正确地自动提供折叠长注释行。

但是,无论我使用的是单行注释,上述内容都是不分青红皂白的。描述结构字段,或描述一些复杂代码的多行注释。

所以,基本的问题是,只有在多行注释的情况下,如何才能自动折叠长注释行?

感谢 阿努邦

edit-1:多行注释说明 当我说“多行评论”时,它基本上意味着这样的评论:

/*
 * this following piece of code does something totally funky with client
 * state, but it is ok.
*/
code follows

相应地,单行评论将是这样的

struct foo {
   char buf_state : 3; // client protocol state 
   char buf_value : 5; // some value
}

上面的elisp代码,尽职地折叠这些注释行。我想只折叠前者,而不是后者。

1 个答案:

答案 0 :(得分:1)

如果您只希望它影响auto-fill-mode而不是一般填充(例如,当您点击M-q时),则可以通过设置comment-auto-fill-only-comments来替换您的代码。至于它只适用于“多行评论”,我认为你首先要解释它们之间的区别。你是说你只想在评论已经超过一行时自动填充,或者是否有评论的其他特征可以让Emacs发现当前只跨越一行的评论可以分散多行。

您可以尝试以下方式:

(set (make-local-variable 'fill-no-break-predicate)
     (lambda ()
       (let ((ppss (syntax-ppss)))
         (or (null (nth 4 ppss)) ;; Not inside a comment.
             (save-excursion
               (goto-char (nth 8 ppss))
               (skip-chars-backward " \t")
               (not (bolp))))))) ;; Comment doesn't start at indentation.
相关问题