3

I'm using cindent with C++ sources, and when I put some macro, which already ends with a semicolon, like Qt's Q_OBJECT, cindent doesn't understand it and makes an extra indentation on the next line.

For emacs there seems to be an option to use/edit c-macro-names-with-semicolon variable, which apparently would make the indenter understand these particular macros correctly.

Is there any similar option in Vim?

3

The :help 'cindent' mentions :help C-indenting, which contains the following:

Note that 'cindent' indenting does not work for every code scenario.  Vim
is not a C compiler: it does not recognize all syntax.  One requirement is
that toplevel functions have a '{' in the first column.  Otherwise they are
easily confused with declarations.

These four options control C program indenting:
'cindent'       Enables Vim to perform C program indenting automatically.
'cinkeys'       Specifies which keys trigger reindenting in insert mode.
'cinoptions'    Sets your preferred indent style.
'cinwords'      Defines keywords that start an extra indent in the next line.

In order to achieve the what you described it would be necessary to know how the macro would expanded; but this would require that Vim behaving as a compiler, which, as noticed above, it does not.

If there would be an option equivalent to c-macro-names-with-semicolon it would be part of the 'cinoptions'. But if you check :help cinoptions-values you will notice that despite it is possible to make a number of customizations, there is nothing similar to what you are looking for.

But you could try using 'indentexpr' along'cindent'. Also from :help C-indenting

There are in fact four main methods available for indentation, each one
overrides the previous if it is enabled, or non-empty for 'indentexpr':
'autoindent'    uses the indent from the previous line.
'smartindent'   is like 'autoindent' but also recognizes some C syntax to
        increase/reduce the indent where appropriate.
'cindent'   Works more cleverly than the other two and is configurable to
        different indenting styles.
'indentexpr'    The most flexible of all: Evaluates an expression to compute
        the indent of a line.  When non-empty this method overrides
        the other ones.  See |indent-expression|.

By using 'indentexpr' you could use a custom function to analyze the contents of the macro (possibly using ctags) and them return the correct amount of indentation.

If you are concerned with a few known macros you could try using the "genindent.vim plugin: Generic function for indentexpr" to make 'cindent' ignore lines referencing those macros.

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.