You are looking for the function locate-dominating-file
. Here is the emacs documentation for this function:
(locate-dominating-file FILE NAME)
Look up the directory hierarchy from FILE
for a directory containing
NAME
. Stop at the first parent directory containing a file NAME
, and
return the directory. Return nil
if not found. Instead of a string,
NAME
can also be a predicate taking one argument (a directory) and
returning a non-nil value if that directory is the one for which we're
looking.
Using this, abo-abo's answer can be shortened to
(defun desperately-compile ()
"Traveling up the path, find a Makefile and `compile'."
(interactive)
(when (locate-dominating-file default-directory "Makefile")
(with-temp-buffer
(cd (locate-dominating-file default-directory "Makefile"))
(compile "make -k"))))