Function: locate-dominating-file

locate-dominating-file is a byte-compiled function defined in files.el.gz.

Signature

(locate-dominating-file FILE NAME)

Documentation

Starting at FILE, look up directory hierarchy for directory containing NAME.

FILE can be a file or a directory. If it's a file, its directory will serve as the starting point for searching the hierarchy of directories. 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. The predicate will be called with every file/directory the function needs to examine, starting with FILE.

Other relevant functions are documented in the file group.

View in manual

Shortdoc

;; file
(locate-dominating-file "foo.png" "/tmp/foo/bar/zot")
    e.g. => "/tmp/foo.png"

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun locate-dominating-file (file name)
  "Starting at FILE, look up directory hierarchy for directory containing NAME.
FILE can be a file or a directory.  If it's a file, its directory will
serve as the starting point for searching the hierarchy of directories.
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.  The predicate will be called with every file/directory
the function needs to examine, starting with FILE."
  ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
  ;; `name' in /home or in /.
  (setq file (abbreviate-file-name (expand-file-name file)))
  (let ((root nil)
        try)
    (while (not (or root
                    (null file)
                    (string-match locate-dominating-stop-dir-regexp file)))
      (setq try (if (stringp name)
                    (and (file-directory-p file)
                         (file-exists-p (expand-file-name name file)))
                  (funcall name file)))
      (cond (try (setq root file))
            ((equal file (setq file (file-name-directory
                                     (directory-file-name file))))
             (setq file nil))))
    (if root (file-name-as-directory root))))