Hi ZyX!

On Mi, 24 Aug 2011, ZyX wrote:

> I guess you need the same for expand() (it may be less trivial due to 
> expand('`=["abc", "def"]`') and expand('`find ...`') with NL=next file).
> About the advantage: I have actually never seen glob() without 
> surrounding split(). Not having to type this split() is better 
> advantage then NL-in-filenames handling.

Here is an updated patch. Possibly, we would also need this for 
globpath(), but I haven't looked at it yet.


regards,
Christian
-- 
Wir neigen dazu, Erfolg eher nach der Höhe unserer Gehälter oder nach
der Grösse unserer Autos zu bestimmen als nach dem Grad unserer
Hilfsbereitschaft und dem Maß unserer Menschlichkeit.
                -- Martin Luther King

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -1744,7 +1744,8 @@
 extend( {expr1}, {expr2} [, {expr3}])
 				List/Dict insert items of {expr2} into {expr1}
 exp( {expr})			Float	exponential of {expr}
-expand( {expr} [, {flag}])	String	expand special keywords in {expr}
+expand( {expr} [, {flag} [, {list}]])
+				any	expand special keywords in {expr}
 feedkeys( {string} [, {mode}])	Number	add key sequence to typeahead buffer
 filereadable( {file})		Number	TRUE if {file} is a readable file
 filewritable( {file})		Number	TRUE if {file} is a writable file
@@ -1798,7 +1799,8 @@
 getwinposx()			Number	X coord in pixels of GUI Vim window
 getwinposy()			Number	Y coord in pixels of GUI Vim window
 getwinvar( {nr}, {varname})	any	variable {varname} in window {nr}
-glob( {expr} [, {flag}])	String	expand file wildcards in {expr}
+glob( {expr} [, {flag} [, {list}]])
+				any	expand file wildcards in {expr}
 globpath( {path}, {expr} [, {flag}])
 				String	do glob({expr}) for all dirs in {path}
 has( {feature})			Number	TRUE if feature {feature} supported
@@ -2729,7 +2731,7 @@
 		{only available when compiled with the |+float| feature}
 
 
-expand({expr} [, {flag}])				*expand()*
+expand({expr} [, {flag} [, {list}]])				*expand()*
 		Expand wildcards and the following special keywords in {expr}.
 		The result is a String.  'wildignorecase' applies.
 
@@ -2789,7 +2791,8 @@
 		When {expr} does not start with '%', '#' or '<', it is
 		expanded like a file name is expanded on the command line.
 		'suffixes' and 'wildignore' are used, unless the optional
-		{flag} argument is given and it is non-zero.  Names for
+		{flag} argument is given and it is non-zero. If {list} is
+		given and it is non-zero, a List will be returned. Names for
 		non-existing files are included.  The "**" item can be used to
 		search in a directory tree.  For example, to find all "README"
 		files in the current directory and below: >
@@ -3436,7 +3439,7 @@
 			:let list_is_on = getwinvar(2, '&list')
 			:echo "myvar = " . getwinvar(1, 'myvar')
 <
-glob({expr} [, {flag}])					*glob()*
+glob({expr} [, {flag} [, {list}]])				*glob()*
 		Expand the file wildcards in {expr}.  See |wildcards| for the
 		use of special characters.
 		The result is a String.
@@ -3447,7 +3450,12 @@
 		one of the patterns in 'wildignore' will be skipped and
 		'suffixes' affect the ordering of matches.
 		'wildignorecase' always applies.
-		If the expansion fails, the result is an empty string.
+
+		When {list} is there and it is non-zero returns a list
+		with all matching files. The advantage of using a list is,
+		you also get filenames containing newlines correctly.
+
+		If the expansion fails, the result is an empty string or list.
 		A name for a non-existing file is not included.
 
 		For most systems backticks can be used to get files names from
diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -7775,7 +7775,7 @@
 #ifdef FEAT_FLOAT
     {"exp",		1, 1, f_exp},
 #endif
-    {"expand",		1, 2, f_expand},
+    {"expand",		1, 3, f_expand},
     {"extend",		2, 3, f_extend},
     {"feedkeys",	1, 2, f_feedkeys},
     {"file_readable",	1, 1, f_filereadable},	/* obsolete */
@@ -7826,7 +7826,7 @@
     {"getwinposx",	0, 0, f_getwinposx},
     {"getwinposy",	0, 0, f_getwinposy},
     {"getwinvar",	2, 2, f_getwinvar},
-    {"glob",		1, 2, f_glob},
+    {"glob",		1, 3, f_glob},
     {"globpath",	2, 3, f_globpath},
     {"has",		1, 1, f_has},
     {"has_key",		2, 2, f_has_key},
@@ -9932,24 +9932,63 @@
     s = get_tv_string(&argvars[0]);
     if (*s == '%' || *s == '#' || *s == '<')
     {
-	++emsg_off;
-	rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
-	--emsg_off;
+	if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN
+		&& get_tv_number_chk(&argvars[2], &error))
+	{
+	  if (!error)
+	  {
+	    char_u *result;
+	    rettv->v_type = VAR_LIST;
+	    rettv->vval.v_list = NULL;
+	    if (rettv_list_alloc(rettv) != FAIL)
+	    {
+	      result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
+	      if (result)
+		list_append_string(rettv->vval.v_list, result, -1);
+	    }
+	  }
+	}
+	else
+	{
+	    ++emsg_off;
+	    rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
+	    --emsg_off;
+	}
     }
     else
     {
 	/* When the optional second argument is non-zero, don't remove matches
 	 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
-	if (argvars[1].v_type != VAR_UNKNOWN
-				    && get_tv_number_chk(&argvars[1], &error))
-	    options |= WILD_KEEP_ALL;
+	if (argvars[1].v_type != VAR_UNKNOWN)
+	{
+	    if (get_tv_number_chk(&argvars[1], &error))
+		options |= WILD_KEEP_ALL;
+
+	    if (argvars[2].v_type != VAR_UNKNOWN
+					&& get_tv_number_chk(&argvars[2], &error))
+	    {
+		rettv->v_type = VAR_LIST;
+		rettv->vval.v_list = NULL;
+	    }
+	}
 	if (!error)
 	{
 	    ExpandInit(&xpc);
 	    xpc.xp_context = EXPAND_FILES;
 	    if (p_wic)
 		options += WILD_ICASE;
-	    rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
+	    if (rettv->v_type == VAR_STRING)
+		rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
+	    if (rettv->v_type == VAR_LIST && rettv_list_alloc(rettv) != FAIL)
+	    {
+	      int i=0;
+	      ExpandOne(&xpc, get_tv_string(&argvars[0]),
+						       NULL, options, WILD_EXPAND_KEEP);
+	      for (i=0; i < xpc.xp_numfiles; i++)
+		list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
+
+	      ExpandCleanup(&xpc);
+	    }
 	}
 	else
 	    rettv->vval.v_string = NULL;
@@ -11740,20 +11779,41 @@
     expand_T	xpc;
     int		error = FALSE;
 
+    rettv->v_type = VAR_STRING;
     /* When the optional second argument is non-zero, don't remove matches
     * for 'wildignore' and don't put matches for 'suffixes' at the end. */
-    if (argvars[1].v_type != VAR_UNKNOWN
-				&& get_tv_number_chk(&argvars[1], &error))
-	options |= WILD_KEEP_ALL;
-    rettv->v_type = VAR_STRING;
+    if (argvars[1].v_type != VAR_UNKNOWN)
+    {
+	if (get_tv_number_chk(&argvars[1], &error))
+	    options |= WILD_KEEP_ALL;
+    /* don't clean the xpc struct after globbing, if the second argument
+     * has been given */
+	if (argvars[2].v_type != VAR_UNKNOWN
+				    && get_tv_number_chk(&argvars[2], &error))
+	{
+	    rettv->v_type = VAR_LIST;
+	    rettv->vval.v_list = NULL;
+	}
+    }
     if (!error)
     {
 	ExpandInit(&xpc);
 	xpc.xp_context = EXPAND_FILES;
 	if (p_wic)
 	    options += WILD_ICASE;
-	rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
+	if (rettv->v_type == VAR_STRING)
+	    rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
 						     NULL, options, WILD_ALL);
+	if (rettv->v_type == VAR_LIST && rettv_list_alloc(rettv) != FAIL)
+	{
+	  int i=0;
+	  ExpandOne(&xpc, get_tv_string(&argvars[0]),
+						   NULL, options, WILD_EXPAND_KEEP);
+	  for (i=0; i < xpc.xp_numfiles; i++)
+	    list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
+
+	  ExpandCleanup(&xpc);
+	}
     }
     else
 	rettv->vval.v_string = NULL;

Raspunde prin e-mail lui