0
0
mirror of https://github.com/vim/vim.git synced 2025-09-01 21:03:39 -04:00

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>
This commit is contained in:
Yegappan Lakshmanan 2024-10-23 21:06:10 +02:00 committed by Christian Brabandt
parent aeb1c97db5
commit 2f6efaccfd
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
8 changed files with 123 additions and 35 deletions

View File

@ -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|

View File

@ -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*

View File

@ -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

View File

@ -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"));

View File

@ -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)

View File

@ -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);

View File

@ -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("\n")
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

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
811,
/**/
810,
/**/