views:

1439

answers:

6

In Emacs, how can I enforce a maximum line length of, say, 80 characters? I want it to insert proper line breaks in my code, much like fill-paragraph for text, if possible, with the correct insertion of the second part of the line.

Here a little example:

LongNameType<PrettyLong, AlsoLong> doSomethingWithLongFunctionName(int a, int b);
foo();

If I actually do fill-paragraph it becomes:

LongNameType<PrettyLong, AlsoLong>
doSomethingWithLongFunctionName(int a, int b); foo();

whereas I'd prefer this:

LongNameType<PrettyLong, AlsoLong>
  doSomethingWithLongFunctionName(int a, int b);
foo();
A: 

Not really an emacser, but what happens if you turn on auto-fill-mode while in c++-mode?

C++ mode should give you auto-indent, and auto-fill-mode gives you line-wrapping....

Stobor
The example I gave was already in c++-mode ...
A: 

I use modeline-posn package. It highlights column number in the modeline if it's greater than specified value.

Eugene Morozov
+3  A: 

fill-paragraph and auto-fill-mode deliberately don't wrap code. There are just too many ways to do it and it'd probably get it wrong. They will wrap comments, but that doesn't help you here.

The only way I've ever done it to to explicitly put the where I want the text to break. Then the auto-indent should put the broken line in the right place.

Are you trying to reflow a large body of existing code? Or trying to have auto-fill work on code you are writing now?

Dave Jennings
But I would expect the C++-mode to redefine fill-paragraph so that it actually does the right thing for C++. That's not harder than the rest of the C++-mode implementation. Yes, ideally I'd like to change my complete code. But also, I want it for new code that I'm writing in Emacs.
I think it is harder, because there are many more ways to break a line than there are to indent. Many times breaking at the 80 column mark (or where-ever) is not the best option for laying out the code (do you break after each argument for instance in a long list of arguments).
Dave Jennings
I think you just break after the last comma before the limit. There might be some exceptions to this, but that should be the main rule.
+1  A: 

There are a number of packages which warn you of line length limits. Personally, I use wide-column, which changes the cursor color depending on its current column.

Trey Jackson
A: 

You should check out one of the many "vertical line" libraries for Emacs. Some keep a vertical highlight line over the entire buffer at point at all times (not really what you want) but other libraries put the vertical highlight on a fix column at all times, which is not really what you want, but you can immediately see when you ought to be wrapping lines.

Deniz Dogan
A: 

Try

'(c-max-one-liner-length 80)

'(fill-column 80)
'(c-ignore-auto-fill (quote (string cpp)))

Hope it helps.