Search code examples
emacsdot-emacsautofill

How to turn on emacs auto-fill-mode only for code comments?


I have tried

(set (make-local-variable 'comment-auto-fill-only-comments) t)

and also

(auto-fill-mode 0)

Though amazingly, neither of those work after emacs is restarted.

I am using eschulte's emacs starter kit

Toggling it works fine with M-x auto-fill-mode.


UPDATE

Using a combination of (thanks Rémi):

(auto-fill-mode 1)
(setq comment-auto-fill-only-comments t) 

It works perfectly within programming files, where there are comments. However, in text mode, it auto-fills everywhere.

How can I turn off auto-fill-mode completely when inside text documents?


Solution

  • If you want Emacs to auto-fill comments you must not make comment-auto-fill-only-comments a local variable:

    (setq comment-auto-fill-only-comments t)
    

    If you want it only in some mode but not all you have to add it to the correct hook:

    (add-hook 'ruby-mode-hook 
              (lambda () ((set (make-local-variable 'comment-auto-fill-only-comments) t)))
    

    UPDATE answer

    To remove auto-fill from text mode, you have to use hook:

    (add-hook 'text-mode-hook 
              (lambda () (auto-fill-mode -1)))
    

    Note that this will change also the auto-fill state in mode deriving off text-mode (latex-mode is one examples, there are a lot of other such mode)