syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.64k stars 4.89k forks source link

C-C++ Layer: not respecting c-basic-offset setting #10080

Closed aurabindo closed 4 years ago

aurabindo commented 6 years ago

Description :octocat:

C-C++ Layer: not respecting c-basic-offset setting. My spacemacs config contains:

(defun dotspacemacs/user-config ()
  "Configuration for user code:
This function is called at the very end of Spacemacs startup, after layer
configuration.
Put your configuration code here, except for variables that should be set
before packages are loaded."
  (setq-default
   c-default-style "bsd"
   c-basic-offset 4)
  )

Reproduction guide :beetle:

Observed behaviour: :eyes: :broken_heart:

Offset defaults to default value of 2 in C-C++ mode

Expected behaviour: :heart: :smile:

offset must be set to the value specified in user-config()

System Info :computer:

Backtrace :paw_prints:

<<BACKTRACE IF RELEVANT>>
aurabindo commented 6 years ago

Just saw #10051 So I'll close this.

aurabindo commented 6 years ago

Reopening since the suggested fix in #10051 did not fix my problem. Did the way to change the default C style change in C-C++ layer ?

syl20bnr commented 6 years ago

So this is not working as intended ?

https://github.com/syl20bnr/spacemacs/blob/develop/layers/+lang/c-c++/README.org#enable-google-set-c-style

syl20bnr commented 6 years ago

The above layer variable should be nil by default.

aurabindo commented 6 years ago

@syl20bnr I enable clang-format as mentioned in the Readme, and I also have the following in dotspacemacs/user-config

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

I am not enabling google style explicitly. But my above setting is not being respected as SPC j = formats my C code with indentation of 2 rather than 4. What do I need to tweak to get this fixed ?

myme commented 6 years ago

I bisected this down to 16fd3e85a4e0941c365722d28cbe81eef02a1305, which landed in develop sometime in December. It seem like the when checks in c-c++/init-google-c-style doesn't respect the layer variables correctly:

(defun c-c++/init-google-c-style ()
  (use-package google-c-style
    :if (or 'c-c++-enable-google-style 'c-c++-enable-google-newline)
    :config (progn
    (when 'c-c++-enable-google-style (add-hook 'c-mode-common-hook 'google-set-c-style))
    (when 'c-c++-enable-google-newline (add-hook 'c-mode-common-hook 'google-make-newline-indent)))))

So:

(when 'c-c++-enable-google-style (print "FOO"))

prints "FOO", while:

(when c-c++-enable-google-style (print "FOO"))

does not and returns nil. The variables are supposed to be evaluated and not quoted, I suppose?

myme commented 6 years ago

To add to that:

c-mode-common-hook is a variable defined in ‘cc-vars.el’.
Its value is
(google-make-newline-indent google-set-c-style spacemacs//init-company-c-mode-common company-mode)

Original value was nil

  This variable may be risky if used as a file-local variable.

It's clear that the functions have been added to the c-mode-common-hook while c-c++-enable-google-style and c-c++-enable-google-newline are set to nil.

syl20bnr commented 6 years ago

@myme thank you for the fix, should be fixed in develop.

lovrolu commented 6 years ago

Confirmed, this fixes the issue for me.

sunlin7 commented 6 years ago

I use these slice of code to modify the c-basic-indent in google-c-style without exclude it,

(eval-after-load 'google-c-style
    (dolist (v google-c-style)
      (when (and (listp v) (eq (car v) 'c-basic-offset)) 
        (setcdr v 4))))
github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

c02y commented 4 years ago

Tried https://github.com/syl20bnr/spacemacs/issues/10051#issuecomment-605979333 ?