0

I am using spacemacs to work on a few different projects and these projects have vastly different coding standards. Tabs vs spaces, 80 columns vs 100 columns, etc, etc. I'm trying to define a way to apply a handful of different styles based on directory. I don't want to hard code project paths into my .spacemacs nor do I want to litter my project directory with editor config data. For this reason, I'm trying to define a class of settings and then apply them to a directory.

I have this chunk in my .spacemacs in dotspacemacs/user-config:

  (dir-locals-set-class-variables 'linux-kernel                                                                                                                                                                                                                                  
                                  '(c-mode . ((indent-tabs-mode . 't)                                                                                                                                                                                                            
                                              (c-default-style  . "linux")                                                                                                                                                                                                       
                                              (c-basic-offset   . 8)                                                                                                                                                                                                             
                                              (tab-width        . 8)                                                                                                                                                                                                             
                                              (fill-column      . 80)                                                                                                                                                                                                            
                                              (evil-shift-width . 8))))

Then I open up a file in a kernel tree and none of these settings are applied. Of course this is expected because I have defined a dir locals class and not applied it. So then I do :(dir-locals-set-directory-class "/path/to/some/kernel/dir" 'linux-kernel), but the kernel code that I was editing (under the folder mentioned in the previous command) doesn't have any of the new settings applied. Any additional files opened in that directory also don't have the settings applied.

I'm pretty new to emacs, so I could be making a really basic mistake. Did I apply the class correctly?

2

Your definition is malformed.

(dir-locals-set-class-variables 'linux-kernel '(c-mode ...))

should be:

(dir-locals-set-class-variables 'linux-kernel '((c-mode ...)))

because that is a list of (potentially) many sets of definitions. e.g.:

'((nil ...)
  (c-mode ...)
  (emacs-lisp-mode ...)
  ...)

the kernel code that I was editing ... doesn't have any of the new settings applied.

The local variables won't take effect in pre-existing buffers unless you revert those buffers (or you could call M-x normal-mode).

1

you can also use .dir-locals.el files which reside in the working directories and apply the settings stated in them to the whole subdir tree. maybe this is a bit more reliable than a complex list in your init files only –– since .dir-locals.el is loaded when a directory or a file in this tree is opened, its settings are applied last and override any casual settings of a mode or temporary global setting.

  • Another advantage of .dir-locals.el is if another Emacs user happens to end up on the same project, they're already ready to go, whereas if your config is only in your local files, they can't benefit. Share & enjoy! – genehack Oct 20 '17 at 21:39
  • Neither of the two approaches is more "reliable" or "complex" than the other. They use the exact same data format. – phils Oct 20 '17 at 23:34
  • phils: you misunderstood my use of „complex”, I only meant a loong nested list. And I hope my last edit of the answer clarifies why I think it is more reliable. – user17303 Oct 23 '17 at 7:36
  • A .dir-locals.el file is not more reliable, because the code-based approach in the question is equivalent to using a .dir-locals.el file. Both are methods for setting directory-local variables. – phils Nov 5 '17 at 9:47
  • Personally, I dislike the idea of .dir-locals.el because I don't like editor/IDE crap mixed in with my code. – Dave Nov 7 '17 at 18:01

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.