获取 Emacs Lisp 中的当前目录

发布于 2024-11-06 05:59:27 字数 203 浏览 2 评论 0原文

我正在尝试编写 .dir-locals.el 文件。我想动态查找文件所在的目录并将其与“TAGS”连接起来。这是我的第一次尝试:

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

这不起作用。我不是 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迎风吟唱 2024-11-13 05:59:27

在linux下,怎么样:

(getenv "PWD")

In linux, how about:

(getenv "PWD")
喜爱纠缠 2024-11-13 05:59:27

结合 sanityinc 的解决方案和我在其他地方找到的其他一些片段,我得到:

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

我认为它可以满足您的要求(以稍微低效的方式,因为我们必须查找 .dir-locals.el 两次)。

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

东风软 2024-11-13 05:59:27

从技术上讲,您需要执行类似的操作才能获取代码表单以在 .dir-locals.el 内进行评估:

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

但是,我尝试了此操作,并且出现了 default-directory当执行 dir-locals 中的代码时为 nil ,因此看起来不可能执行您正在尝试的操作。

也就是说,tags-file-name 看起来并不像是需要手动设置的。相反,当您第一次访问标签文件时,它是由标签代码设置的。

那么为什么不保留它并只使用标签功能呢?毕竟,TAGS 是默认的标记文件名。

编辑:您还可以考虑使用附加project-local-variables库,它使用类似的每个项目.el文件,但代码更灵活你可以把它放进去。这就是我个人解决您的问题的方式。

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.

执笏见 2024-11-13 05:59:27

我不清楚你想要什么,但 (concat default-directory "TAGS") 看起来是正确的。

如果你想设置tags-file-name变量,你可以这样做:(setq tags-file-name (concat default-directory "TAGS"))

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

☆獨立☆ 2024-11-13 05:59:27
((nil . ((tags-file-name . (expand-file-name "TAGS" default-directory)))))
((nil . ((tags-file-name . (expand-file-name "TAGS" default-directory)))))
吾家有女初长成 2024-11-13 05:59:27
(defun print-current-dir ()
      (interactive)
      (message "The current directory is %s" default-directory))
(defun print-current-dir ()
      (interactive)
      (message "The current directory is %s" default-directory))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文