summaryrefslogtreecommitdiffhomepage
path: root/prog/jao-dominating-file.el
blob: f845abc4b57c51030afb86d5c046951e048aa018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(defun jao-locate-dominating-files (regexp &optional file)
  "Look up the directory hierarchy from FILE for a file matching REGEXP.
  Stop at the first parent where a matching file is found and return the list
  of files that that match in this directory."
  (catch 'found
    (let ((dir (file-name-as-directory (or file (buffer-file-name))))
          files)
      (while (and dir
                  (not (string-match locate-dominating-stop-dir-regexp
                                     dir)))
        (if (setq files (condition-case nil
                            (directory-files dir 'full regexp 'nosort)
                          (error nil)))
            (throw 'found files)
          (if (equal dir
                     (setq dir (file-name-directory
                                (directory-file-name dir))))
              (setq dir nil))))
      nil)))


(defun jao-locate-dominating-file (regexp &optional from)
  (car (jao-locate-dominating-files regexp from)))

(defun jao-relative-path (regexp &optional from)
  (let* ((from (or from (buffer-file-name)))
         (dfile (jao-locate-dominating-file regexp from))
         (ddir (and dfile (file-name-directory dfile)))
         (fdir (file-name-directory from)))
    (when ddir
      (and (string-match (format "%s\\(.+\\)/" (regexp-quote ddir)) fdir)
           (match-string-no-properties 1 fdir)))))

(provide 'jao-dominating-file)