Getting the current directory in Emacs Lisp
Asked Answered
N

6

7

I am trying to write a .dir-locals.el file. I want to dynamically find the directory that the file is in and concatenate it with "TAGS". This was my first try:

((nil . ((tags-file-name . (concat default-directory "TAGS")))))

This doesn't work. I am not an Emacs Lisp expert. What is wrong with it?

Nugget answered 6/5, 2011 at 22:55 Comment(0)
B
4

In linux, how about:

(getenv "PWD")
Barrick answered 1/1, 2017 at 20:8 Comment(0)
W
3

Combining sanityinc's solution and some other snippet I found elsewhere, I get:

((nil . ((eval . (setq tags-file-name (concat (locate-dominating-file buffer-file-name ".dir-locals.el") "TAGS"))))))

I think it does what you want (in a slightly inefficient manner, since we have to look for .dir-locals.el twice).

Winer answered 30/7, 2011 at 23:52 Comment(1)
This is the correct answer and should be accepted. The problem is that, while executing .dir-locals.el, the buffer-local variable default-directory is set to the location of the buffer you are visiting, not the directory of the .dir-locals.el file. The call that to locate-dominating-file that @Winer gives fixes the default-directory problem.Edytheee
K
2

Technically, you'd need to do something like this to get code forms to evaluate inside .dir-locals.el:

((nil . ((eval . (setq tags-file-name (concat default-directory "TAGS"))))))

However, I tried this, and default-directory appears to be nil at the time when the code in dir-locals is executed, so it looks impossible do what you are trying.

That said, tags-file-name doesn't look like it's meant to be set manually. Rather, it gets set by the tags code when you first access the tags file.

So why not leave it unset and just use the tag functions? TAGS is the default tag file name, after all.

Edit: you might also consider using the add-on project-local-variables library, which uses a similar per-project .el file, but is more flexible about the code you can put inside it. This is how I would personally solve your issue.

Kentkenta answered 7/5, 2011 at 13:15 Comment(1)
Does this mean that after starting Emacs, I must always point it to the tags file when I call find-tag? Does Emacs remember which tags file I used the next time I start it?Nugget
R
0

It's not clear to me what you want, but (concat default-directory "TAGS") looks correct.

If you want to set the tags-file-name variable, you can do it like this: (setq tags-file-name (concat default-directory "TAGS")).

Release answered 7/5, 2011 at 11:21 Comment(0)
L
0
((nil . ((tags-file-name . (expand-file-name "TAGS" default-directory)))))
Langue answered 5/12, 2022 at 2:10 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Stole
V
-1
(defun print-current-dir ()
      (interactive)
      (message "The current directory is %s" default-directory))
Virtuoso answered 27/11, 2022 at 20:2 Comment(3)
How does this attempt to answer the question?Coinsurance
It addresses the question asked in the title ; That's how.Virtuoso
The question body clarifies that they are trying to do this in a .dir-locals.el file. They already know about default-directory in general and have tried to use it. So why isn't it working in this particular case?Coinsurance

© 2022 - 2024 — McMap. All rights reserved.