diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index 3b8b4fb3a9..2f15176e79 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -2917,7 +2917,7 @@ Contents of Directories the latter case, it can optionally display information about each file, depending on the options passed to the @code{ls} command. -@defun directory-files directory &optional full-name match-regexp nosort +@defun directory-files directory &optional full-name match-regexp nosort count This function returns a list of the names of the files in the directory @var{directory}. By default, the list is in alphabetical order. @@ -2937,6 +2937,13 @@ Contents of Directories are processed in. If the order of processing is visible to the user, then the user will probably be happier if you do sort the names. +If @var{count} is non-@code{nil}, the function will return first +@var{count} number of files, or all files, whichever occurs +first. @var{count} has to be a natural number (> 0). You can use this +function to short circuit evaluation in case you are just interested to +find if a directory is empty or not (request one file and tell it to +ignore dot-files). + @example @group (directory-files "~lewis") @@ -2946,6 +2953,14 @@ Contents of Directories @end group @end example +@example +@group + (null (directory-files directory-name nil + "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*" t 1)) + @result{} nil +@end group +@end example + An error is signaled if @var{directory} is not the name of a directory that can be read. @end defun @@ -2996,7 +3011,7 @@ Contents of Directories non-@code{nil} if that directory is the one it is looking for. @end defun -@defun directory-files-and-attributes directory &optional full-name match-regexp nosort id-format +@defun directory-files-and-attributes directory &optional full-name match-regexp nosort id-format count This is similar to @code{directory-files} in deciding which files to report on and how to report their names. However, instead of returning a list of file names, it returns for each file a diff --git a/etc/NEWS b/etc/NEWS index 1838b6b38a..25c54d3dfe 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1550,6 +1550,13 @@ ledit.el, lmenu.el, lucid.el and old-whitespace.el. * Lisp Changes in Emacs 28.1 ++++ +** 'diirectory-files' function can now take an additional count parameter +This option makes directory-files return COUNT first files in +directory. If match is given, the function vill return first COUNT files +that match the expression. The option is useful for checking if +directory is empty since it will check at most 3 files when COUNT = 1. + +++ ** 'truncate-string-ellipsis' now uses '…' by default. Modes that use 'truncate-string-to-width' with non-nil, non-string diff --git a/lisp/net/ange-ftp.el b/lisp/net/ange-ftp.el index 0cb8d7cb83..335a07914c 100644 --- a/lisp/net/ange-ftp.el +++ b/lisp/net/ange-ftp.el @@ -3427,8 +3427,8 @@ ange-ftp-file-accessible-directory-p (and (file-directory-p name) (file-readable-p name))) -(defun ange-ftp-directory-files (directory &optional full match - &rest v19-args) +(defun ange-ftp-directory-files (directory &optional full match nosort + count) (setq directory (expand-file-name directory)) (if (ange-ftp-ftp-name directory) (progn @@ -3444,18 +3444,19 @@ ange-ftp-directory-files (setq files (cons (if full (concat directory f) f) files)))) (nreverse files))) - (apply 'ange-ftp-real-directory-files directory full match v19-args))) + (apply 'ange-ftp-real-directory-files directory full match nosort count))) (defun ange-ftp-directory-files-and-attributes - (directory &optional full match nosort id-format) + (directory &optional full match nosort attrs id-format count) (setq directory (expand-file-name directory)) (if (ange-ftp-ftp-name directory) (mapcar (lambda (file) (cons file (file-attributes (expand-file-name file directory)))) - (ange-ftp-directory-files directory full match nosort)) + (ange-ftp-directory-files directory full match nosort attrs + id_format count)) (ange-ftp-real-directory-files-and-attributes - directory full match nosort id-format))) + directory full match nosort attrs id-format count))) (defun ange-ftp-file-attributes (file &optional id-format) (setq file (expand-file-name file)) diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el index 49ecaa58ee..26ec24a075 100644 --- a/lisp/net/tramp-adb.el +++ b/lisp/net/tramp-adb.el @@ -301,7 +301,7 @@ tramp-do-parse-file-attributes-with-ls file-properties))) (defun tramp-adb-handle-directory-files-and-attributes - (directory &optional full match nosort id-format) + (directory &optional full match nosort id-format count) "Like `directory-files-and-attributes' for Tramp files." (unless (file-exists-p directory) (tramp-error @@ -312,7 +312,7 @@ tramp-adb-handle-directory-files-and-attributes (copy-tree (with-tramp-file-property v localname (format "directory-files-and-attributes-%s-%s-%s-%s" - full match id-format nosort) + full match id-format nosort count) (with-current-buffer (tramp-get-buffer v) (when (tramp-adb-send-command-and-check v (format "%s -a -l %s" diff --git a/lisp/net/tramp-crypt.el b/lisp/net/tramp-crypt.el index 3e96daa7b1..bda3735db4 100644 --- a/lisp/net/tramp-crypt.el +++ b/lisp/net/tramp-crypt.el @@ -667,7 +667,10 @@ tramp-crypt-handle-delete-file (let (tramp-crypt-enabled) (delete-file (tramp-crypt-encrypt-file-name filename) trash)))) -(defun tramp-crypt-handle-directory-files (directory &optional full match nosort) +;; This function does not seem to pass match and nosort into +;; directory-files at all; is that intentional or bug? +(defun tramp-crypt-handle-directory-files (directory &optional full + match nosort count) "Like `directory-files' for Tramp files." (unless (file-exists-p directory) (tramp-error diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el index 3701bfc22c..787eead807 100644 --- a/lisp/net/tramp-rclone.el +++ b/lisp/net/tramp-rclone.el @@ -300,8 +300,10 @@ tramp-rclone-handle-delete-file (tramp-flush-file-properties v localname) (tramp-rclone-flush-directory-cache v))) +;; This function did not pass nosort arguemnt into directory-files +;; not sure if intentional or bug (defun tramp-rclone-handle-directory-files - (directory &optional full match nosort) + (directory &optional full match nosort count) "Like `directory-files' for Tramp files." (unless (file-exists-p directory) (tramp-error @@ -312,7 +314,8 @@ tramp-rclone-handle-directory-files (with-parsed-tramp-file-name directory nil (let ((result (directory-files - (tramp-rclone-local-file-name directory) full match))) + (tramp-rclone-local-file-name directory) full match + nosort count))) ;; Massage the result. (when full (let ((local (concat "^" (regexp-quote (tramp-rclone-mount-point v)))) diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index 15eab0a4de..7a969979ac 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -1701,9 +1701,9 @@ tramp-sh-handle-file-ownership-preserved-p (tramp-get-remote-gid v 'integer))))))))) ;; Directory listings. - +;; what about perl & sta -> need to fix list to count? (defun tramp-sh-handle-directory-files-and-attributes - (directory &optional full match nosort id-format) + (directory &optional full match nosort id-format count) "Like `directory-files-and-attributes' for Tramp files." (unless id-format (setq id-format 'integer)) (unless (file-exists-p directory) @@ -1743,7 +1743,7 @@ tramp-sh-handle-directory-files-and-attributes (sort result (lambda (x y) (string< (car x) (car y))))) ;; The scripts could fail, for example with huge file size. (tramp-handle-directory-files-and-attributes - directory full match nosort id-format))))) + directory full match nosort id-format count))))) (defun tramp-do-directory-files-and-attributes-with-perl (vec localname &optional id-format) diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el index 1b6af2a2e3..62135f514d 100644 --- a/lisp/net/tramp-smb.el +++ b/lisp/net/tramp-smb.el @@ -690,29 +690,36 @@ tramp-smb-handle-delete-file v 'file-error "%s `%s'" (match-string 0) filename)))))) (defun tramp-smb-handle-directory-files - (directory &optional full match nosort) + (directory &optional full match nosort count) "Like `directory-files' for Tramp files." (unless (file-exists-p directory) (tramp-error (tramp-dissect-file-name directory) tramp-file-missing "No such file or directory" directory)) - (let ((result (mapcar #'directory-file-name - (file-name-all-completions "" directory)))) - ;; Discriminate with regexp. - (when match - (setq result - (delete nil - (mapcar (lambda (x) (when (string-match-p match x) x)) - result)))) - ;; Prepend directory. - (when full - (setq result - (mapcar - (lambda (x) (format "%s/%s" (directory-file-name directory) x)) - result))) - ;; Sort them if necessary. - (unless nosort (setq result (sort result #'string-lessp))) - result)) + (let ((result nil) + (numres 0)) + (when (or (not count) (> count 0)) + (setq result (mapcar #'directory-file-name + (file-name-all-completions "" directory))) + ;; Discriminate with regexp. + (when match + (setq result + (delete nil + (mapcar (lambda (x) (when (string-match-p match x) x)) + result)))) + + ;; return [0,count) number of results + (setq result (cl-subseq result 0 count)) + + ;; Prepend directory. + (when full + (setq result + (mapcar + (lambda (x) (format "%s/%s" (directory-file-name directory) x)) + result))) + ;; Sort them if necessary. + (unless nosort (setq result (sort result #'string-lessp))) + result))) (defun tramp-smb-handle-expand-file-name (name &optional dir) "Like `expand-file-name' for Tramp files." diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index 6d44ad23ad..a99af70196 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -3114,7 +3114,8 @@ tramp-handle-directory-file-name (setq directory (substring directory 0 -1))) directory) -(defun tramp-handle-directory-files (directory &optional full match nosort) +(defun tramp-handle-directory-files (directory &optional full match + nosort count) "Like `directory-files' for Tramp files." (unless (file-exists-p directory) (tramp-error @@ -3133,13 +3134,13 @@ tramp-handle-directory-files (if nosort result (sort result #'string<))))) (defun tramp-handle-directory-files-and-attributes - (directory &optional full match nosort id-format) + (directory &optional full match nosort id-format count) "Like `directory-files-and-attributes' for Tramp files." (mapcar (lambda (x) (cons x (file-attributes (if full x (expand-file-name x directory)) id-format))) - (directory-files directory full match nosort))) + (directory-files directory full match nosort count))) (defun tramp-handle-dired-uncache (dir) "Like `dired-uncache' for Tramp files." diff --git a/src/dired.c b/src/dired.c index 1584b6acf0..1fc8dd27fa 100644 --- a/src/dired.c +++ b/src/dired.c @@ -17,7 +17,6 @@ You should have received a copy of the GNU General Public License along with GNU Emacs. If not, see . */ - #include #include @@ -165,8 +164,26 @@ read_dirent (DIR *dir, Lisp_Object dirname) Lisp_Object directory_files_internal (Lisp_Object directory, Lisp_Object full, Lisp_Object match, Lisp_Object nosort, bool attrs, - Lisp_Object id_format) + Lisp_Object id_format, Lisp_Object return_count) { + ptrdiff_t ind = 0, last = 0; + + /* check count first for early exit */ + if (FIXNUMP(return_count)) + { + last = XFIXNUM (return_count); + if (last <= 0) + return Qnil; + } + + if (FIXNATP(return_count)) + { + last = XFIXNAT(return_count); + + if (!last) + return Qnil; + } + if (!NILP (match)) CHECK_STRING (match); @@ -267,6 +284,10 @@ directory_files_internal (Lisp_Object directory, Lisp_Object full, else finalname = name; + if (last && ind == last) + break; + ind ++; + list = Fcons (attrs ? Fcons (finalname, fileattrs) : finalname, list); } @@ -287,8 +308,7 @@ directory_files_internal (Lisp_Object directory, Lisp_Object full, return list; } - -DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 4, 0, +DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 5, 0, doc: /* Return a list of names of files in DIRECTORY. There are three optional arguments: If FULL is non-nil, return absolute file names. Otherwise return names @@ -296,9 +316,12 @@ DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 4, 0, If MATCH is non-nil, mention only file names that match the regexp MATCH. If NOSORT is non-nil, the list is not sorted--its order is unpredictable. Otherwise, the list returned is sorted with `string-lessp'. - NOSORT is useful if you plan to sort the result yourself. */) + NOSORT is useful if you plan to sort the result yourself. +If COUNT is non-nil, the function will return max of COUNT and length + files, where length is number of files in the directory. COUNT has to + be a natural number > 0. */) (Lisp_Object directory, Lisp_Object full, Lisp_Object match, - Lisp_Object nosort) + Lisp_Object nosort, Lisp_Object count) { directory = Fexpand_file_name (directory, Qnil); @@ -306,34 +329,38 @@ DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 4, 0, call the corresponding file name handler. */ Lisp_Object handler = Ffind_file_name_handler (directory, Qdirectory_files); if (!NILP (handler)) - return call5 (handler, Qdirectory_files, directory, - full, match, nosort); + return call6 (handler, Qdirectory_files, directory, + full, match, nosort, count); - return directory_files_internal (directory, full, match, nosort, false, Qnil); + return directory_files_internal (directory, full, match, nosort, + false, Qnil, count); } DEFUN ("directory-files-and-attributes", Fdirectory_files_and_attributes, - Sdirectory_files_and_attributes, 1, 5, 0, - doc: /* Return a list of names of files and their attributes in DIRECTORY. + Sdirectory_files_and_attributes, 1, 6, 0, doc + : /* Return a list of names of files and their attributes in DIRECTORY. Value is a list of the form: - ((FILE1 . FILE1-ATTRS) (FILE2 . FILE2-ATTRS) ...) +((FILE1 . FILE1-ATTRS) (FILE2 . FILE2-ATTRS) ...) where each FILEn-ATTRS is the attributes of FILEn as returned by `file-attributes'. This function accepts four optional arguments: If FULL is non-nil, return absolute file names. Otherwise return names - that are relative to the specified directory. +that are relative to the specified directory. If MATCH is non-nil, mention only file names that match the regexp MATCH. If NOSORT is non-nil, the list is not sorted--its order is unpredictable. - NOSORT is useful if you plan to sort the result yourself. +NOSORT is useful if you plan to sort the result yourself. ID-FORMAT specifies the preferred format of attributes uid and gid, see `file-attributes' for further documentation. On MS-Windows, performance depends on `w32-get-true-file-attributes', -which see. */) - (Lisp_Object directory, Lisp_Object full, Lisp_Object match, - Lisp_Object nosort, Lisp_Object id_format) +which see. +If COUNT is non-nil, the function will return max of COUNT and length + files, where length is number of files in the directory. COUNT has to + be a natural number > 0. */) +(Lisp_Object directory, Lisp_Object full, Lisp_Object match, + Lisp_Object nosort, Lisp_Object id_format, Lisp_Object count) { directory = Fexpand_file_name (directory, Qnil); @@ -342,11 +369,11 @@ DEFUN ("directory-files-and-attributes", Fdirectory_files_and_attributes, Lisp_Object handler = Ffind_file_name_handler (directory, Qdirectory_files_and_attributes); if (!NILP (handler)) - return call6 (handler, Qdirectory_files_and_attributes, - directory, full, match, nosort, id_format); + return call7 (handler, Qdirectory_files_and_attributes, + directory, full, match, nosort, id_format, count); return directory_files_internal (directory, full, match, nosort, - true, id_format); + true, id_format, count); } diff --git a/src/lisp.h b/src/lisp.h index 45353fbef3..a3cfb5044d 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4612,7 +4612,7 @@ maybe_disable_address_randomization (int argc, char **argv) extern void syms_of_dired (void); extern Lisp_Object directory_files_internal (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, - bool, Lisp_Object); + bool, Lisp_Object, Lisp_Object); /* Defined in term.c. */ extern int *char_ins_del_vector; diff --git a/src/sysdep.c b/src/sysdep.c index f6c0ddee01..9111f1863e 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -2892,7 +2892,7 @@ list_system_processes (void) process. */ procdir = build_string ("/proc"); match = build_string ("[0-9]+"); - proclist = directory_files_internal (procdir, Qnil, match, Qt, false, Qnil); + proclist = directory_files_internal (procdir, Qnil, match, Qt, false, Qnil, Qnil); /* `proclist' gives process IDs as strings. Destructively convert each string into a number. */ diff --git a/test/src/dired-tests.el b/test/src/dired-tests.el new file mode 100644 index 0000000000..3b739e59cc --- /dev/null +++ b/test/src/dired-tests.el @@ -0,0 +1,92 @@ +;;; dired-tests.el --- Tests for directory-files in dired.c -*- lexical-binding: t; -*- + +;; Copyright (C) 2015-2020 Free Software Foundation, Inc. + +;; Author: Arthur Miller +;; Keywords: + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; + +;;; Code: +(require 'ert) + +(ert-deftest directory-files-tests () + (let ((name (expand-file-name "directory-files-test" + (temporary-file-directory))) + ;; nodots expression from dired+ by Drew A. + (nodots "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")) + + (message name) + (when (file-directory-p name) + (delete-directory name t)) + (make-directory name) + (when (file-directory-p name) + (should (= 2 (length (directory-files name)))) + (should-not (directory-files name nil nodots t 1)) + (dolist (file '(a b c d)) + (make-empty-file (expand-file-name (symbol-name file) name))) + (should (= 6 (length (directory-files name)))) + (should (equal "abcd" (string-join (directory-files name nil + nodots) ""))) + (should (= 2 (length (directory-files name nil "[bc]")))) + (should (= 3 (length (directory-files name nil nodots nil 3)))) + (dolist (file '(5 4 3 2 1)) + (make-empty-file (expand-file-name (number-to-string file) name))) + (should (= 0 (length (directory-files name nil "[0-9]" t -1)))) + (should (= 5 (length (directory-files name nil "[0-9]" t)))) + (should (= 5 (length (directory-files name nil "[0-9]" t 50)))) + ))) + +(ert-deftest directory-files-and-attributes-tests () + (let ((name (expand-file-name "directory-files-test" + (temporary-file-directory))) + ;; nodots expression from dired+ by Drew A. + (nodots "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")) + + (message name) + (when (file-directory-p name) + (delete-directory name t)) + (make-directory name) + (when (file-directory-p name) + (should (= 2 (length (directory-files name)))) + (should-not (directory-files-and-attributes name t nodots t 1)) + (dolist (file '(a b c d)) + (make-directory (expand-file-name (symbol-name file) name))) + (should (= 6 (length (directory-files-and-attributes name)))) + (dolist (dir (directory-files-and-attributes name t nodots)) + (should (file-directory-p (car dir))) + (should-not (file-regular-p (car dir)))) + (should (= 2 (length + (directory-files-and-attributes name nil "[bc]")))) + (should (= 3 (length + (directory-files-and-attributes name nil nodots + nil nil 3)))) + (dolist (file '(5 4 3 2 1)) + (make-empty-file (expand-file-name (number-to-string file) name))) + (should (= 0 (length + (directory-files-and-attributes name nil + "[0-9]" t 1 -1)))) + (should (= 5 (length + (directory-files-and-attributes name nil "[0-9]" t)))) + (should (= 5 (length + (directory-files-and-attributes name nil + "[0-9]" t nil 50)))) + ))) + +(provide 'dired-tests) +;;; dired-tests.el ends here