anupam
anupam

Reputation: 91

folding multi-line-comments in emacs automatically

in my .emacs configuration, i have the following :

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

the above gets invoked as part of "c-mode-common-hook" and correctly provides folding long comment lines automatically.

however, the above thing works indiscriminately, whether i am using a single line comment e.g. describing struct fields, or multi-line comments describing some complicated piece of code.

so, the basic question is, how can i get automatic folding of long comment lines only if it is a multi-line comment ?

thanks anupam

edit-1: multi-line-comment explanation when i say "multi-line-comment", it basically means comments like this:

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

a correspondingly, a single line comment would be something like this

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

the above elisp code, dutifully folds both these comment lines. i would like to fold only the former, not the latter.

Upvotes: 3

Views: 730

Answers (1)

Stefan
Stefan

Reputation: 28541

If you only want it to affect auto-fill-mode and not general filling (e.g. not when you hit M-q), then your code can be replaced by setting comment-auto-fill-only-comments. As for having it apply only to "multi-line comments", I think you're first going to have to explain what is the difference between. Are you saying that you only want to auto-fill when the comment already spans more than one line, or is there some other characteristic of a comment that can let Emacs figure out that a comment that currently only spans a single line can be spread over multiple lines.

You could try something like:

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

Upvotes: 1

Related Questions