3

I'm looking for a way to mix tabs and spaces in my cc-styles.

Whilst I question the sanity of this coding style, and wouldn't ever choose to use it had I the choice (seems other people share my view), in this instance I don't have a choice.

My company has the following coding style wrt tabs and spaces:

  • Indentation should be made using tabs
  • Alignment should be made using spaces

As an example, with -> denoting a tab, the following are valid snippets:

tabs for indentation, spaces for vertical alignment of variable names and initialisation:

struct Foo
{
-> int    bar  = 0;
-> char   baz  = 'c';
-> double fizz = .1;
};

tabs for indentation, spaces for vertical alignment of function parameters:

void some_function(int foo,
                   double bar,
                   char baz)
{
-> if (foo == 0)
-> {
-> -> bar *= 2;
-> }
-> ...
}

tabs for indentation, spaces for vertical alignment of logical statements:

void some_function(int foo,
                   double bar,
                   char fizzbuzz)
{
-> if (foo      == 0 &&
->     fizzbuzz == 'a') // note 1 leading tab for indentation level, then spaces
-> {
-> -> bar *= 2;
-> }
-> ...
}

I use the align package by John Wiegley in order to affect the vertical alignment of variables in the example above.

What do I need to have in my cc-styles in order to conform to my company's coding standards?

1

Smart Tabs is a mode that will do what you want -- indent with tabs, align with spaces.

From the documentation, once you install it, you can disable it normally, but enable it in c-mode this way:

(setq-default indent-tabs-mode nil)

(add-hook 'c-mode-common-hook
          (lambda () (setq indent-tabs-mode t)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.