1

I am using emacs 24.5 and I ran into a problem with file local variables.

I am working on a git project where file local variables are set like:

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */

However, they seem to be using unindented namespaces. But when I indent the whole buffer I also get namespaces indented.

Is this the desired default behaviour? How can I override the namespace indendation?

I tried the answer mentioned here: https://stackoverflow.com/questions/2619853/emacs-override-indentation

But I want to do this only for the current project, thus I tried to put it in a .dir-locals.el file.

I tried:

((cc-mode (eval add-hook 'c++-mode-hook  (lambda() (c-set-offset innamespace [0])))))

I also tried:

 ((cc-mode . (c-offsets-alist . ((innamespace . [0])))))

I also tried changing the style in my dir-locals.el, but none of them seem to override the file local setting. Is this a priority issue of file local variables?

  • Please do not post the same question both here and at StackOverflow. Either is OK, not both. Please choose one and delete the other. Thx. – Drew Dec 29 '16 at 1:05
  • "Is this a priority issue of file local variables?" -- Yes, file-local variables absolutely take precedence over directory-local variables (exactly as they should -- the other way around makes no sense). – phils Mar 29 '17 at 10:21
1

I figured out a way to do it, i don't know if it is elegant, but it works quite well.

I created a new MAJORMODE-local-vars-hook by adding this to my init.

;; Provide a new MAJORMODE-local-vars-hook
 (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
 (defun run-local-vars-mode-hook ()
   "Run a hook for the major-mode after the local variables have been processed."
   (run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))

And then i added the c++-mode-local-vars-hook in my dir-locals

(nil . (eval . (add-hook 'c++-mode-local-vars-hook  (lambda () (c-set-offset 'innamespace 0)))))

I guess just adding a c++-mode hook did not do the job because mode hooks execute before local variables are processed.

I don't know why the other method failed though.

-1

editorconfig may be a good and universal solution for problems like this. Emacs plugin also exists and works good.

With editorconfig you control project settings not only for Emacs, but also for different supported IDE and editors.

  • Yes, but editorconfig doesn't provide enough level of control to do this. – Gilles Dec 24 '17 at 22:30

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.