Emacs doesn't properly indent C++ class definitions for allocators that have initializers with colons in them. I think that this is because lines with colons are left-indented in many cases.
I would like to fix this.
Here is an example of what I am talking about.
EMACS indents the code like this:
class demo {
int x;
demo(){
}
demo(int y):x(y){
};
};
But it should really indent it like this:
class demo {
int x;
demo(){
}
demo(int y):x(y){
};
};
Is there a way to fix this behavior? Presumably we need some elisp...
Thanks!
Emacs (at least version 23) doesn't do this in C mode, but it does in C++ mode since in C the part before the colon can only be a label. Make sure you're in C++ mode (M-x c++-mode).
Related
I´m used to Eclipse, where when you press Ctrl+Shift+C it add two bars to the most left side of the file but keep your indentation (I´m using the Eclipse Keymaps extension for using Eclipse shortcuts), for example:
//Comment here
fn main() {
// Comment here
println!("Hello Stackoverflow!");
// Imagine more indentation here
}
On VS Code comments are handled like this:
//Comment here
fn main() {
//Comment here
println!("Hello Stackoverflow!");
//Imagine more indentation here
}
I don´t like it and it´s a deal breaker for me, I love the editor otherwise.
Is there a way to change this?
I tried a couple of extension for customizing comments and playing around with the settings, but nothing worked for me.
Thank you in advance
If I enter "foo ()\n\t{\n"
foo ()
{
<- cursor is now here
I'd like it to be at the same level of indent as the line above (4 spaces in this example), not pushed in a level (8 spaces in this example). So:
foo ()
{
<- cursor here and all will be well in the world
I've tried completely turning off all the indent/tab settings I can find. I'm even prepared to hack around with an extension so a pointer to any in-depth documentation around type/tab commands would also be welcome.
Well, erm, there is an Editor Setting in Preferences, where you can adjust the tabs "editor.tabSize": 4, but I don't there is any level scaling, but I've also discovered a little "hack". If you make double slashes after
{//
and hit return it looks like this:
{//
Hello_world!!
}
Kind of silly, I guess.
Emacs 24.2 for Windows.
case TYPE_LONG:
{ <------- 65383
traceCodePath(20);
tempLongVal = iterator_long(it);
tempLongVal = 10;
if (isBigEndian())
{
swap_endian64(&tempLongValCon, &tempLongVal);
traceCodePath(25); <------- 65601
if (i > 0)
{ <------ get weird from here /* ((substatement-open 65601)) */
} <------ this line aligns with the { below "case" /* ((block-close 65383)) */
} <------ the code afterward all align with { below "case" /* ((block-close 65383)) */
c-mode indentation worked well since the beginning, until I added some Preprocessor macro, #ifdef, #else, #end, after that, from some point in code, all afterward indentations get messy.
Every line's syntatic symbol is right, but the anchor positions are all wrong, seems they can not recognize the { at their same level, but anchor to some { before them at a upper level.
What's more weird, even I remove those preprocessor macro's again, indentation won't recover again.
What's more more weird, sometimes later, it recovered for no reason. I don't know what operations triggered this, but most possible I think is "save" "revert-buffer" etc.
Hope someone can figure the reason out, thanks in advance.
This may very well be related to "bug #14133 24.2; c functions recognition breaks on certain preprocessor macros":
http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-04/msg00055.html
The problem seems to be with spaces preceding #. The way I noticed this is that C-M-a and C-M-e stopped working correctly, and auto-indenting regions was strange just as in your case.
Recoveries were not consistent for me neither.
The patch proposed by Alan Mackenzie solved the problem for my case, you should try it out.
You can also try with emacs 23, I could not reproduce the error in this version.
I am writing Ocaml code under Emacs. Currently, the indent style is like the following, that is, the code where a variable is defined by let is a little bit on the left of let.
let v = 5 in
Printf.printf "v= %d\n" v
I also see some files written by others, when I open them, the indent style is that the code is in the same column as let:
let v = 5 in
Printf.printf "v= %d\n" v
I think the second style looks better, does anyone know where I could setup Emacs so that my Emacs takes the second style?
Thank you very much!
You can also use ocp-indent which indents by default using the second style.
look to the 'tuareg-in-indent' setting, that sets how much ident should be done (0 to disable) - you can customize it with M-x customize-variable...
P.S. you can also look to 'tuareg-let-always-indent' variable...
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();
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.
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?
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....
I use modeline-posn package. It highlights column number in the modeline if it's greater than specified value.
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.
Try
'(c-max-one-liner-length 80)
'(fill-column 80)
'(c-ignore-auto-fill (quote (string cpp)))
Hope it helps.
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