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 Answers1

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.

phils
  • 50,977
  • 3
  • 79
  • 122
  • Thanks but it still messages me nope. If I manually open a file in the project directory it prints it just fine. – my_display_name Nov 02 '23 at 08: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 02 '23 at 12:35