diff src/fileio.c @ 20873:69055d27e85e v8.2.0988

patch 8.2.0988: getting directory contents is always case sorted Commit: https://github.com/vim/vim/commit/84cf6bd81bec93b49166cd48fccc7087fdbaa6fc Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jun 16 20:03:43 2020 +0200 patch 8.2.0988: getting directory contents is always case sorted Problem: Getting directory contents is always case sorted. Solution: Add sort options and v:collate. (Christian Brabandt, closes https://github.com/vim/vim/issues/6229)
author Bram Moolenaar <Bram@vim.org>
date Tue, 16 Jun 2020 20:15:05 +0200
parents 9064044fd4f6
children 334a8a5a6267
line wrap: on
line diff
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -35,6 +35,10 @@ static linenr_T readfile_linenr(linenr_T
 static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
 
+#ifdef FEAT_EVAL
+static int readdirex_sort;
+#endif
+
     void
 filemess(
     buf_T	*buf,
@@ -4645,7 +4649,23 @@ compare_readdirex_item(const void *p1, c
 
     name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
     name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
-    return STRCMP(name1, name2);
+    if (readdirex_sort == READDIR_SORT_BYTE)
+	return STRCMP(name1, name2);
+    else if (readdirex_sort == READDIR_SORT_IC)
+	return STRICMP(name1, name2);
+    else
+	return STRCOLL(name1, name2);
+}
+
+    static int
+compare_readdir_item(const void *s1, const void *s2)
+{
+    if (readdirex_sort == READDIR_SORT_BYTE)
+	return STRCMP(*(char **)s1, *(char **)s2);
+    else if (readdirex_sort == READDIR_SORT_IC)
+	return STRICMP(*(char **)s1, *(char **)s2);
+    else
+	return STRCOLL(*(char **)s1, *(char **)s2);
 }
 #endif
 
@@ -4663,7 +4683,8 @@ readdir_core(
     char_u	*path,
     int		withattr UNUSED,
     void	*context,
-    int		(*checkitem)(void *context, void *item))
+    int		(*checkitem)(void *context, void *item),
+    int         sort)
 {
     int			failed = FALSE;
     char_u		*p;
@@ -4687,6 +4708,8 @@ readdir_core(
 	else \
 	    vim_free(item); \
     } while (0)
+
+    readdirex_sort = READDIR_SORT_BYTE;
 # else
 #  define FREE_ITEM(item)   vim_free(item)
 # endif
@@ -4844,15 +4867,19 @@ readdir_core(
 
 # undef FREE_ITEM
 
-    if (!failed && gap->ga_len > 0)
+    if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
     {
 # ifdef FEAT_EVAL
+	readdirex_sort = sort;
 	if (withattr)
 	    qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
 		    compare_readdirex_item);
 	else
+	    qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
+		    compare_readdir_item);
+# else
+	    sort_strings((char_u **)gap->ga_data, gap->ga_len);
 # endif
-	    sort_strings((char_u **)gap->ga_data, gap->ga_len);
     }
 
     return failed ? FAIL : OK;
@@ -4883,7 +4910,7 @@ delete_recursive(char_u *name)
 	exp = vim_strsave(name);
 	if (exp == NULL)
 	    return -1;
-	if (readdir_core(&ga, exp, FALSE, NULL, NULL) == OK)
+	if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
 	{
 	    for (i = 0; i < ga.ga_len; ++i)
 	    {