How to enforce maximum line length in Emacs?
Asked Answered
D

7

23

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();
Desmid answered 7/4, 2009 at 1:39 Comment(1)
Great idea! +1, I always try to maintain 100 char/line in VS so that I can consistently see my code with the same size of window. Tools to automate this would be cool.Vera
N
5

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.

Nascent answered 13/4, 2009 at 18:15 Comment(4)
It didn't answer the question. Pls provide expilicit solution to enforce inserting newline(breaking the line) when it reaches x, otherwise don't confuse people.Avoidance
@Avoidance Nobody "answered the question", because (apparently) the solution doesn't (yet) exist in Emacs. Yet each answer attempted to give Frank something which might work to help him. Please add constructive feedback - we're a community which builds people up, as opposed to tearing them down. I doubt anyone was "confused" by my answer (or the other answers) because nobody provided an answer which misled people.Nascent
I don't think your answer has anything to do with a question. You are supposed to answer a question not something "that might work for some person" in the SO question, because it exist for many who seek the answer. Even if you help 1 you confuse a whole bunch of peopleAvoidance
And my answer is different than the other answers to this question, how? I don't see you commenting on theirs. You can think the answer has nothing to do with the question, but I'd counter that two other answers are of the same flavor, and they've been upvoted as well (no downvotes at this point, am surprised you haven't downvoted given your stance. Oh it's b/c you're so new you don't have that privilege. I await the downvote.). Channel your energy into writing a working solution for this and I'll give you a 500 point bounty. Then the world will be a better place.Nascent
S
4

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?

Sisterhood answered 7/4, 2009 at 13:10 Comment(3)
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.Desmid
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).Sisterhood
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.Desmid
G
2

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

Glyceride answered 7/4, 2009 at 1:54 Comment(0)
S
1

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

Subminiature answered 7/4, 2009 at 2:7 Comment(0)
P
1

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.

Pinchhit answered 17/4, 2009 at 0:52 Comment(0)
D
0

Try

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

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

Hope it helps.

Divisive answered 15/5, 2009 at 12:43 Comment(0)
P
0

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

President answered 12/12, 2020 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.