Skip to content

Commit 2f6efac

Browse files
yegappanchrisbra
authored andcommitted
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 <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent aeb1c97 commit 2f6efac

8 files changed

Lines changed: 123 additions & 35 deletions

File tree

runtime/doc/options.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3552,7 +3552,7 @@ A jump table for the options with a short description can be found at |Q_op|.
35523552
eob EndOfBuffer |hl-EndOfBuffer|
35533553
lastline NonText |hl-NonText|
35543554

3555-
*'findexpr'* *'fexpr'*
3555+
*'findexpr'* *'fexpr'* *E1514*
35563556
'findexpr' 'fexpr' string (default "")
35573557
global or local to buffer |global-local|
35583558
{not available when compiled without the |+eval|

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4574,6 +4574,7 @@ E1510 change.txt /*E1510*
45744574
E1511 options.txt /*E1511*
45754575
E1512 options.txt /*E1512*
45764576
E1513 message.txt /*E1513*
4577+
E1514 options.txt /*E1514*
45774578
E152 helphelp.txt /*E152*
45784579
E153 helphelp.txt /*E153*
45794580
E154 helphelp.txt /*E154*

src/cmdexpand.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,7 @@ expand_files_and_dirs(
28192819
{
28202820
int free_pat = FALSE;
28212821
int i;
2822-
int ret;
2822+
int ret = FAIL;
28232823

28242824
// for ":set path=" and ":set tags=" halve backslashes for escaped
28252825
// space
@@ -2850,19 +2850,28 @@ expand_files_and_dirs(
28502850
}
28512851
}
28522852

2853-
if (xp->xp_context == EXPAND_FILES)
2854-
flags |= EW_FILE;
2855-
else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2856-
flags |= (EW_FILE | EW_PATH);
2857-
else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2858-
flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2853+
if (xp->xp_context == EXPAND_FILES_IN_PATH && *get_findexpr() != NUL)
2854+
{
2855+
#ifdef FEAT_EVAL
2856+
ret = expand_findexpr(pat, matches, numMatches);
2857+
#endif
2858+
}
28592859
else
2860-
flags = (flags | EW_DIR) & ~EW_FILE;
2861-
if (options & WILD_ICASE)
2862-
flags |= EW_ICASE;
2860+
{
2861+
if (xp->xp_context == EXPAND_FILES)
2862+
flags |= EW_FILE;
2863+
else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2864+
flags |= (EW_FILE | EW_PATH);
2865+
else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2866+
flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2867+
else
2868+
flags = (flags | EW_DIR) & ~EW_FILE;
2869+
if (options & WILD_ICASE)
2870+
flags |= EW_ICASE;
28632871

2864-
// Expand wildcards, supporting %:h and the like.
2865-
ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2872+
// Expand wildcards, supporting %:h and the like.
2873+
ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2874+
}
28662875
if (free_pat)
28672876
vim_free(pat);
28682877
#ifdef BACKSLASH_IN_FILENAME

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,3 +3652,5 @@ EXTERN char e_wrong_character_width_for_field_str[]
36523652
INIT(= N_("E1512: Wrong character width for field \"%s\""));
36533653
EXTERN char e_winfixbuf_cannot_go_to_buffer[]
36543654
INIT(= N_("E1513: Cannot switch buffer. 'winfixbuf' is enabled"));
3655+
EXTERN char e_invalid_return_type_from_findexpr[]
3656+
INIT(= N_("E1514: findexpr did not return a List type"));

src/ex_docmd.c

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6923,39 +6923,27 @@ ex_wrongmodifier(exarg_T *eap)
69236923
eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
69246924
}
69256925

6926-
#ifdef FEAT_EVAL
6926+
#if defined(FEAT_EVAL) || defined(PROTO)
69276927
/*
69286928
* Evaluate the 'findexpr' expression and return the result. When evaluating
69296929
* the expression, v:fname is set to the ":find" command argument.
69306930
*/
69316931
static list_T *
6932-
eval_findexpr(char_u *ptr, int len)
6932+
eval_findexpr(char_u *ptr)
69336933
{
69346934
sctx_T saved_sctx = current_sctx;
6935-
int use_sandbox = FALSE;
69366935
char_u *findexpr;
69376936
char_u *arg;
69386937
typval_T tv;
69396938
list_T *retlist = NULL;
69406939

6941-
if (*curbuf->b_p_fexpr == NUL)
6942-
{
6943-
use_sandbox = was_set_insecurely((char_u *)"findexpr", OPT_GLOBAL);
6944-
findexpr = p_fexpr;
6945-
}
6946-
else
6947-
{
6948-
use_sandbox = was_set_insecurely((char_u *)"findexpr", OPT_LOCAL);
6949-
findexpr = curbuf->b_p_fexpr;
6950-
}
6940+
findexpr = get_findexpr();
69516941

6952-
set_vim_var_string(VV_FNAME, ptr, len);
6942+
set_vim_var_string(VV_FNAME, ptr, -1);
69536943
current_sctx = curbuf->b_p_script_ctx[BV_FEXPR];
69546944

69556945
arg = skipwhite(findexpr);
69566946

6957-
if (use_sandbox)
6958-
++sandbox;
69596947
++textlock;
69606948

69616949
// Evaluate the expression. If the expression is "FuncName()" call the
@@ -6966,10 +6954,10 @@ eval_findexpr(char_u *ptr, int len)
69666954
{
69676955
if (tv.v_type == VAR_LIST)
69686956
retlist = list_copy(tv.vval.v_list, TRUE, TRUE, get_copyID());
6957+
else
6958+
emsg(_(e_invalid_return_type_from_findexpr));
69696959
clear_tv(&tv);
69706960
}
6971-
if (use_sandbox)
6972-
--sandbox;
69736961
--textlock;
69746962
clear_evalarg(&EVALARG_EVALUATE, NULL);
69756963

@@ -6979,6 +6967,61 @@ eval_findexpr(char_u *ptr, int len)
69796967
return retlist;
69806968
}
69816969

6970+
/*
6971+
* Find file names matching "pat" using 'findexpr' and return it in "files".
6972+
* Used for expanding the :find, :sfind and :tabfind command argument.
6973+
* Returns OK on success and FAIL otherwise.
6974+
*/
6975+
int
6976+
expand_findexpr(char_u *pat, char_u ***files, int *numMatches)
6977+
{
6978+
list_T *l;
6979+
int len;
6980+
char_u *regpat;
6981+
6982+
*numMatches = 0;
6983+
*files = NULL;
6984+
6985+
// File name expansion uses wildchars. But the 'findexpr' expression
6986+
// expects a regular expression argument. So convert wildchars in the
6987+
// argument to regular expression patterns.
6988+
regpat = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6989+
if (regpat == NULL)
6990+
return FAIL;
6991+
6992+
l = eval_findexpr(regpat);
6993+
6994+
vim_free(regpat);
6995+
6996+
if (l == NULL)
6997+
return FAIL;
6998+
6999+
len = list_len(l);
7000+
if (len == 0) // empty List
7001+
return FAIL;
7002+
7003+
*files = ALLOC_MULT(char_u *, len);
7004+
if (*files == NULL)
7005+
return FAIL;
7006+
7007+
// Copy all the List items
7008+
listitem_T *li;
7009+
int idx = 0;
7010+
FOR_ALL_LIST_ITEMS(l, li)
7011+
{
7012+
if (li->li_tv.v_type == VAR_STRING)
7013+
{
7014+
(*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7015+
idx++;
7016+
}
7017+
}
7018+
7019+
*numMatches = idx;
7020+
list_free(l);
7021+
7022+
return OK;
7023+
}
7024+
69827025
/*
69837026
* Use 'findexpr' to find file 'findarg'. The 'count' argument is used to find
69847027
* the n'th matching file.
@@ -6994,7 +7037,7 @@ findexpr_find_file(char_u *findarg, int findarg_len, int count)
69947037
cc = findarg[findarg_len];
69957038
findarg[findarg_len] = NUL;
69967039

6997-
fname_list = eval_findexpr(findarg, findarg_len);
7040+
fname_list = eval_findexpr(findarg);
69987041
fname_count = list_len(fname_list);
69997042

70007043
if (fname_count == 0)

src/proto/ex_docmd.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void tabpage_close_other(tabpage_T *tp, int forceit);
4646
void ex_stop(exarg_T *eap);
4747
void handle_drop(int filec, char_u **filev, int split, void (*callback)(void *), void *cookie);
4848
void handle_any_postponed_drop(void);
49+
int expand_findexpr(char_u *pat, char_u ***files, int *numMatches);
4950
void ex_splitview(exarg_T *eap);
5051
void tabpage_new(void);
5152
void do_exedit(exarg_T *eap, win_T *old_curwin);

src/testdir/test_findfile.vim

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ func Test_findexpr()
294294
" basic tests
295295
func FindExpr1()
296296
let fnames = ['Xfindexpr1.c', 'Xfindexpr2.c', 'Xfindexpr3.c']
297-
"return fnames->copy()->filter('v:val =~? v:fname')->join("\n")
298297
return fnames->copy()->filter('v:val =~? v:fname')
299298
endfunc
300299

@@ -353,8 +352,8 @@ func Test_findexpr()
353352
set findexpr=FindExpr2()
354353
call assert_fails('find Xfindexpr1.c', 'find error')
355354

356-
" Try using a null string as the expression
357-
set findexpr=test_null_string()
355+
" Try using a null List as the expression
356+
set findexpr=test_null_list()
358357
call assert_fails('find Xfindexpr1.c', 'E345: Can''t find file "Xfindexpr1.c" in path')
359358

360359
" Try to create a new window from the find expression
@@ -373,6 +372,10 @@ func Test_findexpr()
373372
set findexpr=FindExpr4()
374373
call assert_fails('find Xfindexpr1.c', 'E565: Not allowed to change text or change window')
375374

375+
" Expression returning a string
376+
set findexpr='abc'
377+
call assert_fails('find Xfindexpr1.c', 'E1514: findexpr did not return a List type')
378+
376379
set findexpr&
377380
delfunc! FindExpr1
378381
delfunc! FindExpr2
@@ -449,4 +452,31 @@ func Test_findexpr_scriptlocal_func()
449452
delfunc s:FindExprScript
450453
endfunc
451454

455+
" Test for expanding the argument to the :find command using 'findexpr'
456+
func Test_findexpr_expand_arg()
457+
func FindExpr1()
458+
let fnames = ['Xfindexpr1.c', 'Xfindexpr2.c', 'Xfindexpr3.c']
459+
return fnames->copy()->filter('v:val =~? v:fname')
460+
endfunc
461+
set findexpr=FindExpr1()
462+
463+
call feedkeys(":find \<Tab>\<C-B>\"\<CR>", "xt")
464+
call assert_equal('"find Xfindexpr1.c', @:)
465+
466+
call feedkeys(":find Xfind\<Tab>\<Tab>\<C-B>\"\<CR>", "xt")
467+
call assert_equal('"find Xfindexpr2.c', @:)
468+
469+
call feedkeys(":find *3*\<Tab>\<C-B>\"\<CR>", "xt")
470+
call assert_equal('"find Xfindexpr3.c', @:)
471+
472+
call feedkeys(":find Xfind\<C-A>\<C-B>\"\<CR>", "xt")
473+
call assert_equal('"find Xfindexpr1.c Xfindexpr2.c Xfindexpr3.c', @:)
474+
475+
call feedkeys(":find abc\<Tab>\<C-B>\"\<CR>", "xt")
476+
call assert_equal('"find abc', @:)
477+
478+
set findexpr&
479+
delfunc! FindExpr1
480+
endfunc
481+
452482
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
811,
707709
/**/
708710
810,
709711
/**/

0 commit comments

Comments
 (0)