How to enable auto filling in emacs' c-mode?

848 Views Asked by At

all

Now I'm editing c sources with emacs under c-mode. How ever auto-fill-mode doesn't seem to work at all. Here how I enabled and tried to use it.

  1. M-x auto-fill-mode (enable auto-fill-mode)
  2. Typed in a line longer than auto-fill size(which 80 characters for now) --> didn't break the line
  3. Tried to auto-filled by issuing M-q

However above attempt didn't work out at all. Could anybody point out that what have I done wrong?

Thanks for your help in advance.

1

There are 1 best solutions below

0
On

When you use auto-fill-mode in c-mode, the default behavior is to wrap text only when writing text, as in a comment. You can override this by customizing the value of c-ignore-auto-fill. Note that emacs will wrap and indent your code as text, which is probably not what you want.

A better solution is probably to bind space to a function like this:

(defun insert-space-or-newline-and-indent ()
  (interactive)
  (if (>= (current-column) fill-column)
      (newline-and-indent)
    (insert-char ? )))