文件树如下:
- foo
- lorem
- ipsum <-
- baz <-
- bar
- baz
当前访问的文件为ipsum
。现在我想找到第一个
baz
及其所在的目录。如何从树上走过
在lisp中ipsum
?
答案 0 :(得分:27)
您想要locate-dominating-file
。
答案 1 :(得分:15)
(defun parent-directory (dir)
(unless (equal "/" dir)
(file-name-directory (directory-file-name dir))))
(defun find-file-in-heirarchy (current-dir fname)
"Search for a file named FNAME upwards through the directory hierarchy, starting from CURRENT-DIR"
(let ((file (concat current-dir fname))
(parent (parent-directory (expand-file-name current-dir))))
(if (file-exists-p file)
file
(when parent
(find-file-in-heirarchy parent fname)))))
如果结果不是nil,您可以使用file-name-directory
提取文件的目录,如下所示:
(let ((file (find-file-in-heirarchy (buffer-file-name) "baz")))
(when file
(file-name-directory file)))