4

I want to define some variables in .dir-locals.el, then I add a hook to python-mode-hook, in the hook, I want to read the variables defined in .dir-locals. The problem is that mode hook is called before loading .dir-locals.el

Any ideas to swap the loading sequence?

Drew
  • 29,895
  • 7
  • 74
  • 104
jiluo
  • 2,296
  • 3
  • 21
  • 34

1 Answers1

7

You cannot swap the sequence. It is hardcoded in normal-mode.

However, you can hook in at hack-local-variables-hook, which runs after all local variables were set. Assume you have the following currently:

(defun my-python-hook-function ()
  (message "Hello world"))

(add-hook 'python-mode-hook #'my-python-hook-function)

You can simply change this to:

(defun my-local-variables-hook ()
  (when (derived-mode-p 'python-mode) (my-python-hook-function)))

(add-hook 'hack-local-variables-hook #'my-local-variables-hook)