我的项目文件以这种格式设置:
/home/user/proj/source
/home/user/proj/source/src1
/home/user/proj/source/src1
/home/user/proj/header ...etc
Run Code Online (Sandbox Code Playgroud)
我有办法在查看任何源文件时找到项目路径
"/home/user/proj"
Run Code Online (Sandbox Code Playgroud)
此外,(buffer-file-name)给出给定源文件的完整绝对路径.
如何编写一个提取源文件相对路径的lisp函数?
意思是,如果我正在观看
/home/user/proj/source/src1/file.c
Run Code Online (Sandbox Code Playgroud)
我想有这条路
"source/src1/file.c"
Run Code Online (Sandbox Code Playgroud)
以下函数为我提供了项目路径:
(defun upward-find-file (filename &optional startdir)
(let ((dirname (expand-file-name
(if startdir startdir ".")))
(found nil) ; found is set as a flag to leave loop if we find it
(top nil)) ; top is set when we get
; to / so that we only check it once
; While we've neither been at the top last time nor have we found
; the file.
(while (not (or found top))
; If we're at / set top flag.
(if (string= (expand-file-name dirname) "/")
(setq top t))
; Check for the file
(if (file-exists-p (expand-file-name filename dirname))
(setq found t)
; If not, move up a directory
(setq dirname (expand-file-name ".." dirname))))
; return statement
(if found (concat dirname "/") nil)))
Run Code Online (Sandbox Code Playgroud)
我总是在主项目文件夹中有"Makefile",所以
(setq dirname (upward-find-file "Makefile" startdir))
Run Code Online (Sandbox Code Playgroud)
照顾好这一点.
尝试locate-dominating-file和file-relative-name.
(let ((fname (buffer-file-name)))
(file-relative-name fname (locate-dominating-file fname "Makefile")))
Run Code Online (Sandbox Code Playgroud)
如果找不到任何东西,NB会locate-dominiating-file返回nil.
| 归档时间: |
|
| 查看次数: |
740 次 |
| 最近记录: |