patch 9.1.0811: :find expansion does not consider 'findexpr'

Problem:  :find expansion does not consider 'findexpr'
Solution: Support expanding :find command argument using 'findexpr'
          (Yegappan Lakshmanan)

closes: #15929

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/cmdexpand.c b/src/cmdexpand.c
index 3f74f47..406135c 100644
--- a/src/cmdexpand.c
+++ b/src/cmdexpand.c
@@ -2819,7 +2819,7 @@
 {
     int		free_pat = FALSE;
     int		i;
-    int		ret;
+    int		ret = FAIL;
 
     // for ":set path=" and ":set tags=" halve backslashes for escaped
     // space
@@ -2850,19 +2850,28 @@
 	    }
     }
 
-    if (xp->xp_context == EXPAND_FILES)
-	flags |= EW_FILE;
-    else if (xp->xp_context == EXPAND_FILES_IN_PATH)
-	flags |= (EW_FILE | EW_PATH);
-    else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
-	flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
+    if (xp->xp_context == EXPAND_FILES_IN_PATH && *get_findexpr() != NUL)
+    {
+#ifdef FEAT_EVAL
+	ret = expand_findexpr(pat, matches, numMatches);
+#endif
+    }
     else
-	flags = (flags | EW_DIR) & ~EW_FILE;
-    if (options & WILD_ICASE)
-	flags |= EW_ICASE;
+    {
+	if (xp->xp_context == EXPAND_FILES)
+	    flags |= EW_FILE;
+	else if (xp->xp_context == EXPAND_FILES_IN_PATH)
+	    flags |= (EW_FILE | EW_PATH);
+	else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
+	    flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
+	else
+	    flags = (flags | EW_DIR) & ~EW_FILE;
+	if (options & WILD_ICASE)
+	    flags |= EW_ICASE;
 
-    // Expand wildcards, supporting %:h and the like.
-    ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
+	// Expand wildcards, supporting %:h and the like.
+	ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
+    }
     if (free_pat)
 	vim_free(pat);
 #ifdef BACKSLASH_IN_FILENAME
diff --git a/src/errors.h b/src/errors.h
index 03b3c08..2299f2c 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -3652,3 +3652,5 @@
 	INIT(= N_("E1512: Wrong character width for field \"%s\""));
 EXTERN char e_winfixbuf_cannot_go_to_buffer[]
 	INIT(= N_("E1513: Cannot switch buffer. 'winfixbuf' is enabled"));
+EXTERN char e_invalid_return_type_from_findexpr[]
+	INIT(= N_("E1514: findexpr did not return a List type"));
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index 9447a2d..fa370fe 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -6923,39 +6923,27 @@
     eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
 }
 
-#ifdef FEAT_EVAL
+#if defined(FEAT_EVAL) || defined(PROTO)
 /*
  * Evaluate the 'findexpr' expression and return the result.  When evaluating
  * the expression, v:fname is set to the ":find" command argument.
  */
     static list_T *
-eval_findexpr(char_u *ptr, int len)
+eval_findexpr(char_u *ptr)
 {
     sctx_T	saved_sctx = current_sctx;
-    int		use_sandbox = FALSE;
     char_u	*findexpr;
     char_u	*arg;
     typval_T	tv;
     list_T	*retlist = NULL;
 
-    if (*curbuf->b_p_fexpr == NUL)
-    {
-	use_sandbox = was_set_insecurely((char_u *)"findexpr", OPT_GLOBAL);
-	findexpr = p_fexpr;
-    }
-    else
-    {
-	use_sandbox = was_set_insecurely((char_u *)"findexpr", OPT_LOCAL);
-	findexpr = curbuf->b_p_fexpr;
-    }
+    findexpr = get_findexpr();
 
-    set_vim_var_string(VV_FNAME, ptr, len);
+    set_vim_var_string(VV_FNAME, ptr, -1);
     current_sctx = curbuf->b_p_script_ctx[BV_FEXPR];
 
     arg = skipwhite(findexpr);
 
-    if (use_sandbox)
-	++sandbox;
     ++textlock;
 
     // Evaluate the expression.  If the expression is "FuncName()" call the
@@ -6966,10 +6954,10 @@
     {
 	if (tv.v_type == VAR_LIST)
 	    retlist = list_copy(tv.vval.v_list, TRUE, TRUE, get_copyID());
+	else
+	    emsg(_(e_invalid_return_type_from_findexpr));
 	clear_tv(&tv);
     }
-    if (use_sandbox)
-	--sandbox;
     --textlock;
     clear_evalarg(&EVALARG_EVALUATE, NULL);
 
@@ -6980,6 +6968,61 @@
 }
 
 /*
+ * Find file names matching "pat" using 'findexpr' and return it in "files".
+ * Used for expanding the :find, :sfind and :tabfind command argument.
+ * Returns OK on success and FAIL otherwise.
+ */
+    int
+expand_findexpr(char_u *pat, char_u ***files, int *numMatches)
+{
+    list_T	*l;
+    int		len;
+    char_u	*regpat;
+
+    *numMatches = 0;
+    *files = NULL;
+
+    // File name expansion uses wildchars.  But the 'findexpr' expression
+    // expects a regular expression argument.  So convert wildchars in the
+    // argument to regular expression patterns.
+    regpat = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
+    if (regpat == NULL)
+	return FAIL;
+
+    l = eval_findexpr(regpat);
+
+    vim_free(regpat);
+
+    if (l == NULL)
+	return FAIL;
+
+    len = list_len(l);
+    if (len == 0)	    // empty List
+	return FAIL;
+
+    *files = ALLOC_MULT(char_u *, len);
+    if (*files == NULL)
+	return FAIL;
+
+    // Copy all the List items
+    listitem_T *li;
+    int idx = 0;
+    FOR_ALL_LIST_ITEMS(l, li)
+    {
+	if (li->li_tv.v_type == VAR_STRING)
+	{
+	    (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
+	    idx++;
+	}
+    }
+
+    *numMatches = idx;
+    list_free(l);
+
+    return OK;
+}
+
+/*
  * Use 'findexpr' to find file 'findarg'.  The 'count' argument is used to find
  * the n'th matching file.
  */
@@ -6994,7 +7037,7 @@
     cc = findarg[findarg_len];
     findarg[findarg_len] = NUL;
 
-    fname_list = eval_findexpr(findarg, findarg_len);
+    fname_list = eval_findexpr(findarg);
     fname_count = list_len(fname_list);
 
     if (fname_count == 0)
diff --git a/src/proto/ex_docmd.pro b/src/proto/ex_docmd.pro
index 3bd7705..d3f1a4e 100644
--- a/src/proto/ex_docmd.pro
+++ b/src/proto/ex_docmd.pro
@@ -46,6 +46,7 @@
 void ex_stop(exarg_T *eap);
 void handle_drop(int filec, char_u **filev, int split, void (*callback)(void *), void *cookie);
 void handle_any_postponed_drop(void);
+int expand_findexpr(char_u *pat, char_u ***files, int *numMatches);
 void ex_splitview(exarg_T *eap);
 void tabpage_new(void);
 void do_exedit(exarg_T *eap, win_T *old_curwin);
diff --git a/src/testdir/test_findfile.vim b/src/testdir/test_findfile.vim
index f8be713..3a1066b 100644
--- a/src/testdir/test_findfile.vim
+++ b/src/testdir/test_findfile.vim
@@ -294,7 +294,6 @@
   " basic tests
   func FindExpr1()
     let fnames = ['Xfindexpr1.c', 'Xfindexpr2.c', 'Xfindexpr3.c']
-    "return fnames->copy()->filter('v:val =~? v:fname')->join("\n")
     return fnames->copy()->filter('v:val =~? v:fname')
   endfunc
 
@@ -353,8 +352,8 @@
   set findexpr=FindExpr2()
   call assert_fails('find Xfindexpr1.c', 'find error')
 
-  " Try using a null string as the expression
-  set findexpr=test_null_string()
+  " Try using a null List as the expression
+  set findexpr=test_null_list()
   call assert_fails('find Xfindexpr1.c', 'E345: Can''t find file "Xfindexpr1.c" in path')
 
   " Try to create a new window from the find expression
@@ -373,6 +372,10 @@
   set findexpr=FindExpr4()
   call assert_fails('find Xfindexpr1.c', 'E565: Not allowed to change text or change window')
 
+  " Expression returning a string
+  set findexpr='abc'
+  call assert_fails('find Xfindexpr1.c', 'E1514: findexpr did not return a List type')
+
   set findexpr&
   delfunc! FindExpr1
   delfunc! FindExpr2
@@ -449,4 +452,31 @@
   delfunc s:FindExprScript
 endfunc
 
+" Test for expanding the argument to the :find command using 'findexpr'
+func Test_findexpr_expand_arg()
+  func FindExpr1()
+    let fnames = ['Xfindexpr1.c', 'Xfindexpr2.c', 'Xfindexpr3.c']
+    return fnames->copy()->filter('v:val =~? v:fname')
+  endfunc
+  set findexpr=FindExpr1()
+
+  call feedkeys(":find \<Tab>\<C-B>\"\<CR>", "xt")
+  call assert_equal('"find Xfindexpr1.c', @:)
+
+  call feedkeys(":find Xfind\<Tab>\<Tab>\<C-B>\"\<CR>", "xt")
+  call assert_equal('"find Xfindexpr2.c', @:)
+
+  call feedkeys(":find *3*\<Tab>\<C-B>\"\<CR>", "xt")
+  call assert_equal('"find Xfindexpr3.c', @:)
+
+  call feedkeys(":find Xfind\<C-A>\<C-B>\"\<CR>", "xt")
+  call assert_equal('"find Xfindexpr1.c Xfindexpr2.c Xfindexpr3.c', @:)
+
+  call feedkeys(":find abc\<Tab>\<C-B>\"\<CR>", "xt")
+  call assert_equal('"find abc', @:)
+
+  set findexpr&
+  delfunc! FindExpr1
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index c60a7e9..226a05e 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    811,
+/**/
     810,
 /**/
     809,