Follow

Dear lazyweb,

How do I set up :emacs: to detect .h files that are in fact C++ or Objective-C headers, and use an appropriate mode for them automatically?

And why doesn't it do that out of the box?

· · Web · 1 · 7 · 3

@bugaevc Emacs should do that out of the box (tested with emacs -Q and it detected a C++ header file correctly). Does the detection fail every time? Are your .h files distinguishable from C headers?

@bugaevc If it only happens with some headers, .dir-locals.el might be usable workaround: https://emacs.stackexchange.com/a/18069. Instead of file-name-nondirectory you’d use file-name-extension.

((nil (eval (lambda () (when (string= (file-name-extension buffer-file-name) "h") (c++-mode))))))

@tastytea thanks!

Indeed, it seems to work out of the box (with -Q) for C++ headers. This means something in Spacemacs, which I'm using, breaks it. But even then, it doesn't seem to work with Objective-C headers?

I don't want to either do project-specific configuration or set up .h to always mean c++-mode — I want it to always do the right thing, automatically.

@bugaevc I don’t use Spacemacs, but this seems to be the culprit: https://www.spacemacs.org/layers/+lang/c-c++/README.html#default-mode-for-header-files. Maybe the autodetection works again if you set it to nil?

I never used Objective-C, so I haven’t a clue about that, sorry.

@tastytea some research led me to this page emacswiki.org/emacs/ObjectiveC which even has an example of how to set up objc autodetection, then the same could be done for C++. Thanks again for your help!

Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!