26.6.6 在标准路径中查找文件

本节介绍如何在目录列表(即路径(path))中搜索文件,或在可执行文件标准目录列表中查找可执行文件。

若需查找用户专属配置文件,See 标准文件名 中的 locate-user-emacs-file 函数。

Function: locate-file filename path &optional suffixes predicate

该函数在 path 指定的目录列表中搜索名为 filename 的文件,并依次尝试 suffixes 中的后缀。若找到匹配文件,返回其绝对路径(see 绝对文件名与相对文件名);否则返回 nil

可选参数 suffixes 给出搜索时追加到 filename 后的文件名后缀列表。locate-file 会对每个目录逐一尝试这些后缀。若 suffixesnil(""),则不使用后缀,直接使用原文件名。常见的 suffixes 取值包括 exec-suffixes(see Functions that Create Subprocesses)、load-suffixesload-file-rep-suffixes 以及 get-load-suffixes 函数的返回值(see 加载后缀)。

path 的常见取值:查找可执行程序时使用 exec-path(see Functions that Create Subprocesses);查找 Lisp 文件时使用 load-path(see 库搜索)。若 filename 为绝对路径,则 path 无效,但仍会尝试 suffixes 中的后缀。

可选参数 predicate 非空时,指定一个谓词函数用于判断候选文件是否可用。该函数接收候选文件名作为唯一参数。若 predicatenil 或省略,locate-file 使用 file-readable-p 作为默认谓词。其他常用谓词可参考 See 区分文件类型,例如 file-executable-pfile-directory-p

该函数默认跳过目录;若需要查找目录,需让谓词函数对目录返回 dir-ok。示例:

(locate-file "html" '("/var/www" "/srv") nil
             (lambda (f) (if (file-directory-p f) 'dir-ok)))

为兼容旧版本,predicate 也可以是符号 executablereadablewritableexists,或由这些符号组成的列表。

Function: executable-find program &optional remote

该函数搜索指定程序 program 的可执行文件,返回其绝对路径(包含文件扩展名,如有)。若未找到则返回 nil。函数会在 exec-path 的所有目录中搜索,并尝试 exec-suffixes 中的所有扩展名(see Functions that Create Subprocesses)。

remote 非空,且 default-directory 为远程目录,则在对应远程主机上搜索 program


emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike