emacs - elisp - 获取相对于脚本的文件路径

标签 emacs elisp relative-path

假设我正在编写一个 emacs lisp 函数,该函数与位于定义函数的文件相关的文件进行接口(interface)。

- bin/executable
- foo.el
foo.el :
(defun foo ()
  (shell-command-to-string
   (format "echo '%s' | ./bin/executable"
           (buffer-substring-no-properties
            (point-min)
            (point-max)))))

如果我从 foo.el 运行它然后效果很好。如果我在编辑任何其他文件时调用该函数,则它不起作用,因为路径不正确。

我怎样才能可靠地引用 ./bin/executable来自foo.el无论函数在哪里调用?

最佳答案

使用load-file-name多变的。

(defconst directory-of-foo (file-name-directory load-file-name))

(defun foo ()
  (shell-command-to-string
   (format "echo '%s' | %s"
           (buffer-substring-no-properties
            (point-min)
            (point-max))
           (expand-file-name "./bin/executable" directory-of-foo))))

关于emacs - elisp - 获取相对于脚本的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26991001/

相关文章:

Java 编码风格和 Emacs cc-mode 配置

html - 为什么相对路径路径不显示HTML5字幕

c# - 如何映射 Controller 外部的相对路径?

emacs - 更新emacs软件包时出错:无法下载 'gnu'存档

windows - 使用左侧的 org 文件和右侧的议程启动 Emacs

emacs - 在 Emacs 中显示所有打开的缓冲区

emacs - emacs lisp 中符号名和变量名的区别

php - php中的文件路径要求

macos - 在VT100/xterm终端(Mac OS X的终端)中将 "C-("发送到Emacs吗?

emacs - 避免\printbbliography被Org-mode标题吞噬