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

Jac*_*son 3 emacs elisp relative-path

假设我正在编写一个 emacs lisp 函数,该函数与一个文件接口,该文件相对于定义该函数的文件。

- bin/executable
- foo.el
Run Code Online (Sandbox Code Playgroud)

foo.el

(defun foo ()
  (shell-command-to-string
   (format "echo '%s' | ./bin/executable"
           (buffer-substring-no-properties
            (point-min)
            (point-max)))))
Run Code Online (Sandbox Code Playgroud)

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

无论在哪里调用函数,如何./bin/executable从内部可靠地引用foo.el

d58*_*884 6

使用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))))
Run Code Online (Sandbox Code Playgroud)