1

I'm a long-term Emacs user who recently started to learn C++. I was surprised to discover that indentation for C++ is actually rather bad, specifically in files containing multi-line comment blocks.

In the example below I copy/paste a C++ exercise description into a multi-line comment block, then I start writing the main function. Notice that the main function header line is incorrectly indented. Notice that the closing bracket is incorrectly indented as well.

/* ch6_6
Write a program that checks if a sentence is correct according to the
“English” grammar in §6.4.1.  Assume that every sentence is terminated
by a full stop (.) surrounded by whitespace.  For example, `birds fly
but the fish swim .' is a sentence, but `birds fly but the fish swim'
(terminating dot missing) and `birds fly but the fish swim.' (no space
before dot) are not.  For each sentence entered, the program should
simply respond “OK” or “not OK.”  Hint: Don’t bother with tokens; just
read into a string using `>>'.
*/

  int main() {
  return 0;
                                 }

If I remove the multi-line comment block indentation is correct, but multi-line comment blocks seem to be rather fundamental to writing real-world code.

This cannot possibly be right. Why does the present of a comment influence indentation at all? / What am I doing wrong here? I'm on GNU Emacs 25.2.1 (Fedora 25), started with emacs -q.

3

It's due to having the ( in column 0. You can set open-paren-in-column-0-is-defun-start to nil to prevent the bad indentation, although this may cause Emacs to take a long time indenting and/or fontifying in some situations. Note that if you indent the comment as well this moves the ( forward, avoiding the problem.

It seems to be fixed in Emacs 26.

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.