hekevintran
hekevintran

Reputation: 23732

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 Emacs Lisp expert. What is wrong with it?

Upvotes: 7

Views: 8879

Answers (6)

Arthur叔
Arthur叔

Reputation: 40

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

Upvotes: 0

yPhil
yPhil

Reputation: 8387

(defun print-current-dir ()
      (interactive)
      (message "The current directory is %s" default-directory))

Upvotes: -1

AAAfarmclub
AAAfarmclub

Reputation: 2390

In linux, how about:

(getenv "PWD")

Upvotes: 4

mqtthiqs
mqtthiqs

Reputation: 465

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).

Upvotes: 3

sanityinc
sanityinc

Reputation: 15242

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.

Upvotes: 2

maxelost
maxelost

Reputation: 1567

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")).

Upvotes: 0

Related Questions