Я мои файлы проект, установленный в этом формате:Найти относительный путь к файлу в проекте
/home/user/proj/source
/home/user/proj/source/src1
/home/user/proj/source/src1
/home/user/proj/header ...etc
У меня есть способ, чтобы найти путь к проекту при просмотре любого исходного файла
"/home/user/proj"
Кроме того, (buffer-file-name) дает полный абсолютный путь к данному исходному файлу.
Как написать функцию lisp, которая извлекает относительный путь исходного файла?
Значение, если я просмотр
/home/user/proj/source/src1/file.c
Я хотел бы иметь на пути
"source/src1/file.c"
Следующая функция дает мне путь проекта:
(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)))
Я всегда «Makefile» в основной папке проекта, поэтому
(setq dirname (upward-find-file "Makefile" startdir))
Заботится об этом.
Да. Это оно. Brilliant. – SFbay007