patch 9.1.0811: :find expansion does not consider 'findexpr' Commit: https://github.com/vim/vim/commit/2f6efaccfd5c4e7df1f54ed0f41f329abbe05f60 Author: Yegappan Lakshmanan <yegap...@yahoo.com> Date: Wed Oct 23 21:06:10 2024 +0200
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 <yegap...@yahoo.com> Signed-off-by: Christian Brabandt <c...@256bit.org> diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 5593523a0..229ce13fd 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -3552,7 +3552,7 @@ A jump table for the options with a short description can be found at |Q_op|. eob EndOfBuffer |hl-EndOfBuffer| lastline NonText |hl-NonText| - *'findexpr'* *'fexpr'* + *'findexpr'* *'fexpr'* *E1514* 'findexpr' 'fexpr' string (default "") global or local to buffer |global-local| {not available when compiled without the |+eval| diff --git a/runtime/doc/tags b/runtime/doc/tags index 7a6e84c85..0c1b31a66 100644 --- a/runtime/doc/tags +++ b/runtime/doc/tags @@ -4574,6 +4574,7 @@ E1510 change.txt /*E1510* E1511 options.txt /*E1511* E1512 options.txt /*E1512* E1513 message.txt /*E1513* +E1514 options.txt /*E1514* E152 helphelp.txt /*E152* E153 helphelp.txt /*E153* E154 helphelp.txt /*E154* diff --git a/src/cmdexpand.c b/src/cmdexpand.c index 3f74f471b..406135cff 100644 --- a/src/cmdexpand.c +++ b/src/cmdexpand.c @@ -2819,7 +2819,7 @@ expand_files_and_dirs( { 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 @@ expand_files_and_dirs( } } - 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 03b3c0877..2299f2c50 100644 --- a/src/errors.h +++ b/src/errors.h @@ -3652,3 +3652,5 @@ EXTERN char e_wrong_character_width_for_field_str[] 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 9447a2d2e..fa370fefc 100644 --- a/src/ex_docmd.c +++ b/src/ex_docmd.c @@ -6923,39 +6923,27 @@ ex_wrongmodifier(exarg_T *eap) 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 @@ eval_findexpr(char_u *ptr, int len) { 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); @@ -6979,6 +6967,61 @@ eval_findexpr(char_u *ptr, int len) return retlist; } +/* + * 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 @@ findexpr_find_file(char_u *findarg, int findarg_len, int count) 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 3bd7705cb..d3f1a4ea7 100644 --- a/src/proto/ex_docmd.pro +++ b/src/proto/ex_docmd.pro @@ -46,6 +46,7 @@ void tabpage_close_other(tabpage_T *tp, int forceit); 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 f8be7136e..3a1066bf3 100644 --- a/src/testdir/test_findfile.vim +++ b/src/testdir/test_findfile.vim @@ -294,7 +294,6 @@ func Test_findexpr() " basic tests func FindExpr1() let fnames = ['Xfindexpr1.c', 'Xfindexpr2.c', 'Xfindexpr3.c'] - "return fnames->copy()->filter('v:val =~? v:fname')->join(" ") return fnames->copy()->filter('v:val =~? v:fname') endfunc @@ -353,8 +352,8 @@ func Test_findexpr() 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 @@ func Test_findexpr() 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 @@ func Test_findexpr_scriptlocal_func() 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 c60a7e9a1..226a05eed 100644 --- a/src/version.c +++ b/src/version.c @@ -704,6 +704,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 811, /**/ 810, /**/ -- -- 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 --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/vim_dev/E1t3gox-002XXH-H0%40256bit.org.