summaryrefslogtreecommitdiffhomepage
path: root/prog/jao-dominating-file.el
diff options
context:
space:
mode:
Diffstat (limited to 'prog/jao-dominating-file.el')
-rw-r--r--prog/jao-dominating-file.el34
1 files changed, 34 insertions, 0 deletions
diff --git a/prog/jao-dominating-file.el b/prog/jao-dominating-file.el
new file mode 100644
index 0000000..f845abc
--- /dev/null
+++ b/prog/jao-dominating-file.el
@@ -0,0 +1,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)