David
David

Reputation: 2190

Emacs auto-indentation prevent double indentation on constructors

I'm using Emacs 24.3 on Windows. I have auto-indentation set up, but I would like to have constructors in C++ only indent one level. I've read that setting the substatement-open value to 0 can fix this issue, however, I'm still having this issue.

What I'm currently seeing:

class A 
{  
public:
    A() 
        {
            // code
        }    
};

What I would like to see:

class A 
{  
public:
    A() 
    {
        // code
    }    
};

Would anyone be able to tell me what's wrong with or missing in my .emacs to correct this?

(setq c-default-style "stroustrup"
      c-basic-offset 4)

; no extra indentation on constructors
(defun my-cpp-mode-hook ()
  (setq c-basic-offset 4)
  (c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-cpp-mode-hook)

; auto-indentation
(add-hook 'c-mode-common-hook (lambda () (c-toggle-auto-state 1)))

Upvotes: 1

Views: 179

Answers (1)

Chris
Chris

Reputation: 137268

Try modifying my-cpp-mode-hook to set inline-open to 0 as well:

(defun my-cpp-mode-hook ()
  (setq c-basic-offset 4)
  (c-set-offset 'substatement-open 0)
  (c-set-offset 'inline-open 0))

In general, you can inspect indentation rules in c-mode (and similar modes) by moving to the line in question and using c-show-syntactic-information (bound to C-c C-s by default), which in this case gives Syntactic analysis: ((inclass 10) (inline-open)).

Upvotes: 1

Related Questions