Getting the current directory in Emacs Lisp
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 开发者_Go百科Emacs Lisp expert. What is wrong with it?
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).
In linux, how about:
(getenv "PWD")
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.
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"))
.
((nil . ((tags-file-name . (expand-file-name "TAGS" default-directory)))))
(defun print-current-dir ()
(interactive)
(message "The current directory is %s" default-directory))
精彩评论