自动折叠Emacs中的多行注释

3

在我的.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”的一部分被调用,可以自动折叠长注释行。
然而,上述方法是不加区分的,无论我是使用单行注释(例如描述结构体字段)还是多行注释(描述一些复杂的代码),都会自动折叠长注释行。
因此,基本问题是,如何仅在多行注释时自动折叠长注释行?
谢谢 Anupam
编辑-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个回答

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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接