File 在emacs lisp的源目录中查找文件

File 在emacs lisp的源目录中查找文件,file,emacs,path,elisp,File,Emacs,Path,Elisp,我有一个文本文件,其中包含我的elisp代码所依赖的单词列表。我想把它保存在与源文件相同的目录中 使用相对路径似乎不起作用,因为当触发读取的函数运行时,路径与您当前所在的缓冲区相对 守则: (defconst etype-lines-file "etype.lines") (defun etype-read-file () (with-temp-buffer (insert-file-contents (expand-file-name etype-lines-file defau

我有一个文本文件,其中包含我的elisp代码所依赖的单词列表。我想把它保存在与源文件相同的目录中

使用相对路径似乎不起作用,因为当触发读取的函数运行时,路径与您当前所在的缓冲区相对

守则:

(defconst etype-lines-file "etype.lines")

(defun etype-read-file ()
  (with-temp-buffer
    (insert-file-contents (expand-file-name etype-lines-file default-directory))
    (apply
     'vector
     (split-string
      (buffer-substring-no-properties (point-min) (point-max)) "\n"))))

是否有任何方法可以实现相对于源代码文件位置的路径?

您可以使用
加载文件名
。编译或加载Emacs Lisp文件时,它会绑定到源文件的名称。以下是elisp手册中的一个示例:

 (defconst superfrobnicator-base (file-name-directory load-file-name))

 (defun superfrobnicator-fetch-image (file)
   (expand-file-name file superfrobnicator-base))

这意味着您必须通过
load file
emacs lisp byte compile and load
以独占方式加载文件,而不是计算缓冲区的部分,因为在后一种情况下
load file name
nil

是的,因为此代码:

(插入文件内容(展开文件名etype行文件
默认目录
))`

相反,可以将您想要的目录作为参数传递给函数,并在此处使用它来代替
默认目录
,也可以通过
defconst
)为目录使用全局变量(或“常量”(代替
默认目录