Frank
Frank

Reputation: 66214

How to enforce maximum line length in Emacs?

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

Upvotes: 23

Views: 17097

Answers (7)

I.Omar
I.Omar

Reputation: 542

You could use the more advanced clang-format package.

  • You have to install clang-fromat along with it's emacs package.
  • Add this to your .emacs (setq clang-format-style "file") or add to custom-set-variables '(clang-format-style "file").
  • Generate your template style by clang-format -style=gnu -dump-config > .clang-format then place into your project's root.
  • Customize .clang-format as you like and change ColumnLimit: 120 to 80 or whatever value you want.

That would force column limit using clang-format tool.

Reference: ClangFormatStyleOptions. Related question

Upvotes: 0

Nishith
Nishith

Reputation: 469

Try

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

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

Hope it helps.

Upvotes: 0

Deniz Dogan
Deniz Dogan

Reputation: 26227

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.

Upvotes: 1

Trey Jackson
Trey Jackson

Reputation: 74460

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.

Upvotes: 6

Dave Jennings
Dave Jennings

Reputation: 316

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?

Upvotes: 4

Eugene Morozov
Eugene Morozov

Reputation: 15836

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

Upvotes: 1

Stobor
Stobor

Reputation: 45132

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

Upvotes: 2

Related Questions