2

Emacs version 26.3 on a mac

I have this (simplified) c(c)-mode config:

(add-hook 'c-mode-common-hook
      '(lambda ()
         (c-set-style "bsd")))

which works when I visit a c file.

When I convert to

(use-package cc-mode
   :config
   (c-set-style "bsd")
  )

and restart emacs, this snippet gets applied to the scratch buffer and I get this odd error:

Debugger entered--Lisp error: (error "Buffer *scratch* is not a CC Mode buffer (c-set-style)")

The scratch buffer is shown as a Fundamental buffer, not an interactive-lisp buffer. Strangely, any added config, even a :bind (("C-c t" . tags-search)) fixes this.

So, with this,

(use-package cc-mode
  :bind  (("C-c t"         . tags-search))
  :config
  (c-set-style "bsd")
)

when I visit a c file, say foo.c, I now get

Debugger entered--Lisp error: (error "Buffer foo.c is not a CC Mode buffer (c-set-style)") The buffer/file is loaded, but the mode is Fundamental.

Is there a good way to use use-package with cc-mode, given that it supports modes for many different types of code and separable hooks for a lot of these?

Thanks,

Kannan

  • If you just want to have some configuration running, after your package has loaded (and such configuration that does not require a running buffer with the package's mode), then you might use "eval-after-load". – Peter Wildemann May 15 at 6:21
2

The code after :config runs as soon as the package is loaded, which is when you config file is loaded unless you delay loading the package somehow. So the c-set-style function is getting called somewhere except a c-mode file and triggering an error before Emacs is even able to switch *scratch* into the proper mode.

Adding :bind appears to fix the problem as :bind implies :defer, which delays loading the package until needed.

use-package doesn't remove the need for hooks, it just changes where you define them. Put your add-hook in the :config section of use-package

(use-package cc-mode
   :config
   (add-hook 'c-mode-common-hook
    (lambda ()
       (c-set-style "bsd")))
  )
| improve this answer | |
  • Kannan Varadhan: Note also that the lambda form is not quoted, which is as it should be -- don't quote your lambdas. But also, don't use lambdas with hooks at all. Use a named function instead -- it avoids lots of potential problems with updating/removing hook functions, inspecting hook variables, and visiting the function definitions. – phils May 15 at 6:02
  • That works. Quick question: do I then move the config section into the add-hook, now that I need it at least one piece of the configuration? I didnt need it for other uses earlier... – Kannan Varadhan May 15 at 18:09
  • @phils Thanks, that is a good tip. Not quoting the lambda form, I will use. Not sure why i got into that habit, I think I pulled it from somewhere and never thought about it. Not everything works for me as you suggested though. I tried: ` (defun kv-style () "test" (c-set-style "bsd")) (add-hook 'c-mode-common-hook (kv-style)) ` and ran into the same issue. – Kannan Varadhan May 15 at 18:13
  • You don't need to move any other config. Think about it this way: add-hook is just setting a variable (c-mode-common-hook). And you set it alongside all of you setqs in the :config section. If you use a named function you just pass the function, but don't evaluate it: (add-hook 'c-mode-common-hook 'kv-style). I would just use a lambda myself; I find it easier to read – erikstokes May 15 at 18:23

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.