emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Emacs-diffs] master 3431e82: Ignore directory symlinks in directory


From: Lars Magne Ingebrigtsen
Subject: Re: [Emacs-diffs] master 3431e82: Ignore directory symlinks in directory-files-recursively
Date: Sun, 14 Dec 2014 10:33:39 +0100
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> As you pointed out, `file-tree-walk' has the nice property that it can
>> stop recursing based on a predicate, so implementing it on
>> `directory-files-recursively' would not be optimal.
>
> Right, what is needed is to extend directory-files-recursively such that
> it can also use a predicate to decide whether to recurse or not.

file-tree-walk uses the same function for the predicate and for the
... processing function.  So that would be something like the patch
included below.

The interface isn't the same, though.  I think we should perhaps ignore
the non-lexical case and get rid of the ARGS argument.  Supplying a
lexical lambda predicate just makes more sense.

And I don't like passing in the DIR and LEAF separately.  To check
whether the argument is a directory, the predicate can just use
`directory-name-p' .

diff --git a/lisp/files.el b/lisp/files.el
index d55ef1a..14be6c2 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -766,11 +766,17 @@ prevented.  Directory entries are sorted with 
string-lessp."
   (and (> (length name) 0)
        (char-equal (aref name (1- (length name))) ?/)))
 
-(defun directory-files-recursively (dir match &optional include-directories)
+(defun directory-files-recursively (dir match &optional
+                                       include-directories predicate)
   "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."
+If INCLUDE-DIRECTORIES, also include directories that have matching names.
+
+If PREDICATE, this should be a function that will be called on
+each matching file and on all directories.  When called on a
+directory, the predicate should return non-nil if the directory
+should be descended into."
   (let ((result nil)
        (files nil))
     (dolist (file (sort (file-name-all-completions "" dir)
@@ -780,13 +786,17 @@ If INCLUDE-DIRECTORIES, also include directories that 
have matching names."
            (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)
+             (when (and (not (file-symlink-p path))
+                        (or (null predicate)
+                            (funcall predicate 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)
+           (when predicate
+             (funcall predicate (expand-file-name file dir)))
            (push (expand-file-name file dir) files)))))
     (nconc result (nreverse files))))
 

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



reply via email to

[Prev in Thread] Current Thread [Next in Thread]