--- src/dired.c 2020-10-15 07:43:02.633804624 +0200
+++ ../dired4.c 2020-10-15 07:42:01.358574645 +0200
@@ -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,19 @@
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 (FIXNATP(return_count))
+ {
+ last = XFIXNAT(return_count);
+
+ if (!last)
+ return Qnil;
+ }
+
if (!NILP (match))
CHECK_STRING (match);
@@ -267,6 +277,13 @@
else
finalname = name;
+ if (FIXNATP(return_count))
+ {
+ if (ind == last)
+ break;
+ ind ++;
+ }
+
list = Fcons (attrs ? Fcons (finalname, fileattrs) : finalname, list);
}
@@ -287,8 +304,7 @@
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 +312,14 @@
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. Order
+ in which files are returned is not guaranteed and is file system and
+ OS dependent. COUNT has to be an integral number in interval
+ [1,COUNT]. If 0 files are requested the function will return nil. */)
(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 +327,40 @@
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. Order
+ in which files are returned is not guaranteed and is file system and
+ OS dependent. COUNT has to be an integral number in interval
+ [1,COUNT]. If 0 files are requested the function will return nil. */)
+(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 @@
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);
}