0

I have following .dir-locals.el file in my project directory:

((nil . ((logbook . "todo.org"))))

and following function:

(defun project-switch (&optional project-dir)
  "switch to project"
  (interactive)
  (let* ((project-dir (if project-dir
                         project-dir
                        (project-prompt-project-dir)))
         (default-directory project-dir))

    (message "%s " default-directory)
    (hack-dir-local-variables-non-file-buffer)

      (if (boundp 'logbook)
          (message "logbooK: %s" logbook)
        (message "nope"))))

If I run the function, it messages me nope even though the .dir-locals.el file is present.

EDIT: updated code with feedback. It still messages me nope. If I manually open a file in the project directory it prints it just fine.

1
  • 2
    Note to improve your code : you can avoid using two lets by using let* instead. See this post. Oct 29, 2023 at 12:44

1 Answer 1

3

By default, directory-local variables are processed for file-visiting buffers and not too many other kinds of buffer -- anything else needs to be handled explicitly (using hack-dir-local-variables-non-file-buffer).

So if you didn't run your command from a file-visiting buffer (or had not reverted that buffer since changing the .dir-locals.el file), then it likely wouldn't have contained those local values.

2
  • Thanks but it still messages me nope. If I manually open a file in the project directory it prints it just fine. Nov 2, 2023 at 8:04
  • I tried your code and called (project-switch DIR) for a DIR with your .dir-locals.el and it messaged me the todo.org filename.
    – phils
    Nov 2, 2023 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.