In C and C++ files, I want to configure the mode so that any time I request indentation, that it will always expand all of the embedded TAB characters in indentation (at the start of the line, and no where else) into space characters. How do I do that using vanilla emacs execution (emacs -Q
) plus some calls to setq
of whatever variables, and not writing special Emacs Lisp or rebinding keys etc.?
Detail:
Version of emacs I am using is:
GNU Emacs 25.2.2 (x86_64-pc-linux-gnu, GTK+ Version 3.22.21) of 2017-09-22, modified by Debian
Execute emacs -Q
(so as to get the default c-style-alist
value which for this should imply using the gnu
style) and then open a .C file and insert in exactly this code:
// A C++ file
void function_using_tabs_inside_indentation () {
printf("A message\n");
if (bad) {
printf("A bad message\n");
if (bad) {
printf("A bad message\n");
if (bad) {
printf("A bad message\n"); // TAB!!
}
}
}
}
Note the comment above about the indented tab.
Then type M-x whitespace-mode
. You should see something like this that clearly shows that there is a TAB code embedded in the indentation:
Now set indent-tabs-mode
to nil
by typing M-: (setq indent-tabs-mode nil)
.
Move your point to the "v" in "void" and type C-M-q
, which should execute the command c-indent-exp
.
But notice that nothing changed: The embedded tab does not get expanded properly into spaces, as the TAB code is still there.
Then, move point directly on the TAB code in that file. Type the TAB key. Notice nothing changes, but I want it to be expanded into the required number of spaces.
Yes, I know I can resort to using expand -2
on the file, but that takes me out of Emacs and I want it to happen automatically when I indent the line.
I also don't want to have to manually backspace over the TAB code and then type the TAB key to insert the required number of spaces, either. If I do that to fix it manually, then this is what I expect to see (well mostly):
Notice how when I "fixed" it, the whitespace-mode highlights it in brighter yellow (brighter than the other space "glyphs" it uses for space characters)? Why? Perhaps it it telling me something is incorrect there?
Update #1:
I know about M-x untabify
and that does what I want, but I want it to happen globally across all C and C++ files I open, and without having to type it. Yes, I can write some Emacs Lisp code to rewire the TAB key binding and the C-M-q bindings, but I hope to avoid that complexity, as I need this to be something I can request my fellow Emacs-using developers to use in our projects development.
M-x whitespace-report-region
and see the report. On my mac, clang style, (two more spaces than gnu style) the report was>= 'tab-width' SPACEs at the beginning of the line
andM-x fixup-whitespace
moved the offending line to start at the first column, so try to increase thetab-width
variable.