]> code.delx.au - gnu-emacs/blobdiff - lisp/files.el
Update copyright year to 2015
[gnu-emacs] / lisp / files.el
index 0f54a22d61e2d2e71a883f91f484e842dcf5cd29..22362ca13ca77d2eb66c60060250adb4ccfea2d3 100644 (file)
@@ -1,6 +1,6 @@
 ;;; files.el --- file input and output commands for Emacs  -*- lexical-binding:t -*-
 
-;; Copyright (C) 1985-1987, 1992-2014 Free Software Foundation, Inc.
+;; Copyright (C) 1985-1987, 1992-2015 Free Software Foundation, Inc.
 
 ;; Maintainer: emacs-devel@gnu.org
 ;; Package: emacs
@@ -743,8 +743,7 @@ The ACTION is applied to each subdirectory before descending into
 it, and if nil is returned at that point, the descent will be
 prevented.  Directory entries are sorted with string-lessp."
   (cond ((file-directory-p dir)
-        (or (char-equal ?/ (aref dir (1- (length dir))))
-            (setq dir (file-name-as-directory dir)))
+        (setq dir (file-name-as-directory dir))
         (let ((lst (directory-files dir nil nil t))
               fullname file)
           (while lst
@@ -762,6 +761,35 @@ prevented.  Directory entries are sorted with string-lessp."
                (file-name-nondirectory dir)
                args))))
 
+(defsubst directory-name-p (name)
+  "Return non-nil if NAME ends with a slash character."
+  (and (> (length name) 0)
+       (char-equal (aref name (1- (length name))) ?/)))
+
+(defun directory-files-recursively (dir match &optional include-directories)
+  "Return all files under DIR that have file names matching MATCH (a regexp).
+This function works recursively.  Files are returned in \"depth first\"
+and alphabetical order.
+If INCLUDE-DIRECTORIES, also include directories that have matching names."
+  (let ((result nil)
+       (files nil))
+    (dolist (file (sort (file-name-all-completions "" dir)
+                       'string<))
+      (unless (member file '("./" "../"))
+       (if (directory-name-p file)
+           (let* ((leaf (substring file 0 (1- (length file))))
+                  (path (expand-file-name leaf dir)))
+             ;; Don't follow symlinks to other directories.
+             (unless (file-symlink-p path)
+               (setq result (nconc result (directory-files-recursively
+                                           path match include-directories))))
+             (when (and include-directories
+                        (string-match match leaf))
+               (setq result (nconc result (list path)))))
+         (when (string-match match file)
+           (push (expand-file-name file dir) files)))))
+    (nconc result (nreverse files))))
+
 (defun load-file (file)
   "Load the Lisp file named FILE."
   ;; This is a case where .elc makes a lot of sense.