Skip to content

Commit 79d599b

Browse files
Shougobrammool
authored andcommitted
patch 8.2.4903: cannot get the current cmdline completion type and position
Problem: Cannot get the current cmdline completion type and position. Solution: Add getcmdcompltype() and getcmdscreenpos(). (Shougo Matsushita, closes vim#10344)
1 parent c27747e commit 79d599b

File tree

11 files changed

+125
-3
lines changed

11 files changed

+125
-3
lines changed

runtime/doc/builtin.txt

+20
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,12 @@ getcharmod() Number modifiers for the last typed character
213213
getcharpos({expr}) List position of cursor, mark, etc.
214214
getcharsearch() Dict last character search
215215
getcharstr([expr]) String get one character from the user
216+
getcmdcompltype() String return the type of the current
217+
command-line completion
216218
getcmdline() String return the current command-line
217219
getcmdpos() Number return cursor position in command-line
220+
getcmdscreenpos() Number return cursor screen position in
221+
command-line
218222
getcmdtype() String return current command-line type
219223
getcmdwintype() String return current command-line window type
220224
getcompletion({pat}, {type} [, {filtered}])
@@ -3208,6 +3212,13 @@ getcharstr([expr]) *getcharstr()*
32083212
Otherwise this works like |getchar()|, except that a number
32093213
result is converted to a string.
32103214

3215+
getcmdcompltype() *getcmdcompltype()*
3216+
Return the type of the current command-line completion.
3217+
Only works when the command line is being edited, thus
3218+
requires use of |c_CTRL-\_e| or |c_CTRL-R_=|.
3219+
See |command-completion| for the return string.
3220+
Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|.
3221+
Returns an empty string when completion is not defined.
32113222

32123223
getcmdline() *getcmdline()*
32133224
Return the current command-line. Only works when the command
@@ -3227,6 +3238,15 @@ getcmdpos() *getcmdpos()*
32273238
Returns 0 otherwise.
32283239
Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|.
32293240

3241+
getcmdscreenpos() *getcmdscreenpos()*
3242+
Return the screen position of the cursor in the command line
3243+
as a byte count. The first column is 1.
3244+
Instead of |getcmdpos()|, it adds the prompt position.
3245+
Only works when editing the command line, thus requires use of
3246+
|c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping.
3247+
Returns 0 otherwise.
3248+
Also see |getcmdpos()|, |setcmdpos()|.
3249+
32303250
getcmdtype() *getcmdtype()*
32313251
Return the current command-line type. Possible return values
32323252
are:

runtime/doc/usr_41.txt

+4
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,12 @@ Buffers, windows and the argument list:
976976
swapname() get the swap file path of a buffer
977977

978978
Command line: *command-line-functions*
979+
getcmdcompltype() get the type of the current command line
980+
completion
979981
getcmdline() get the current command line
980982
getcmdpos() get position of the cursor in the command line
983+
getcmdscreenpos() get screen position of the cursor in the
984+
command line
981985
setcmdpos() set position of the cursor in the command line
982986
getcmdtype() return the current command-line type
983987
getcmdwintype() return the current command-line window type

src/cmdexpand.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
static int cmd_showtail; // Only show path tail in lists ?
1717

18-
static void set_expand_context(expand_T *xp);
1918
static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
2019
char_u ***matches, int *numMatches,
2120
char_u *((*func)(expand_T *, int)), int escaped);
@@ -1230,7 +1229,7 @@ addstar(
12301229
* EXPAND_ENV_VARS Complete environment variable names
12311230
* EXPAND_USER Complete user names
12321231
*/
1233-
static void
1232+
void
12341233
set_expand_context(expand_T *xp)
12351234
{
12361235
cmdline_info_T *ccline = get_cmdline_info();

src/evalfunc.c

+4
Original file line numberDiff line numberDiff line change
@@ -1850,10 +1850,14 @@ static funcentry_T global_functions[] =
18501850
ret_dict_any, f_getcharsearch},
18511851
{"getcharstr", 0, 1, 0, arg1_bool,
18521852
ret_string, f_getcharstr},
1853+
{"getcmdcompltype", 0, 0, 0, NULL,
1854+
ret_string, f_getcmdcompltype},
18531855
{"getcmdline", 0, 0, 0, NULL,
18541856
ret_string, f_getcmdline},
18551857
{"getcmdpos", 0, 0, 0, NULL,
18561858
ret_number, f_getcmdpos},
1859+
{"getcmdscreenpos", 0, 0, 0, NULL,
1860+
ret_number, f_getcmdscreenpos},
18571861
{"getcmdtype", 0, 0, 0, NULL,
18581862
ret_string, f_getcmdtype},
18591863
{"getcmdwintype", 0, 0, 0, NULL,

src/ex_getln.c

+58
Original file line numberDiff line numberDiff line change
@@ -4118,6 +4118,42 @@ get_cmdline_str(void)
41184118
return vim_strnsave(p->cmdbuff, p->cmdlen);
41194119
}
41204120

4121+
/*
4122+
* Get the current command-line completion type.
4123+
*/
4124+
static char_u *
4125+
get_cmdline_completion(void)
4126+
{
4127+
cmdline_info_T *p;
4128+
4129+
if (cmdline_star > 0)
4130+
return NULL;
4131+
4132+
p = get_ccline_ptr();
4133+
if (p && p->xpc != NULL)
4134+
{
4135+
char_u *cmd_compl;
4136+
4137+
set_expand_context(p->xpc);
4138+
4139+
cmd_compl = cmdcomplete_type_to_str(p->xpc->xp_context);
4140+
if (cmd_compl != NULL)
4141+
return vim_strnsave(cmd_compl, strlen((char *)cmd_compl));
4142+
}
4143+
4144+
return NULL;
4145+
}
4146+
4147+
/*
4148+
* "getcmdcompltype()" function
4149+
*/
4150+
void
4151+
f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv)
4152+
{
4153+
rettv->v_type = VAR_STRING;
4154+
rettv->vval.v_string = get_cmdline_completion();
4155+
}
4156+
41214157
/*
41224158
* "getcmdline()" function
41234159
*/
@@ -4141,6 +4177,28 @@ f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
41414177
rettv->vval.v_number = p->cmdpos + 1;
41424178
}
41434179

4180+
/*
4181+
* Get the command line cursor screen position.
4182+
*/
4183+
static int
4184+
get_cmdline_screen_pos(void)
4185+
{
4186+
cmdline_info_T *p = get_ccline_ptr();
4187+
4188+
if (p == NULL)
4189+
return -1;
4190+
return p->cmdspos;
4191+
}
4192+
4193+
/*
4194+
* "getcmdscreenpos()" function
4195+
*/
4196+
void
4197+
f_getcmdscreenpos(typval_T *argvars UNUSED, typval_T *rettv)
4198+
{
4199+
rettv->vval.v_number = get_cmdline_screen_pos() + 1;
4200+
}
4201+
41444202
/*
41454203
* Set the command line byte position to "pos". Zero is the first position.
41464204
* Only works when the command line is being edited.

src/proto/cmdexpand.pro

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int showmatches(expand_T *xp, int wildmenu);
1313
char_u *sm_gettail(char_u *s);
1414
char_u *addstar(char_u *fname, int len, int context);
1515
void set_cmd_context(expand_T *xp, char_u *str, int len, int col, int use_ccline);
16+
void set_expand_context(expand_T *xp);
1617
int expand_cmdline(expand_T *xp, char_u *str, int col, int *matchcount, char_u ***matches);
1718
void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options);
1819
int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_wild_list);

src/proto/ex_getln.pro

+2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ char_u *vim_strsave_fnameescape(char_u *fname, int what);
3030
void escape_fname(char_u **pp);
3131
void tilde_replace(char_u *orig_pat, int num_files, char_u **files);
3232
cmdline_info_T *get_cmdline_info(void);
33+
void f_getcmdcompltype(typval_T *argvars, typval_T *rettv);
3334
void f_getcmdline(typval_T *argvars, typval_T *rettv);
3435
void f_getcmdpos(typval_T *argvars, typval_T *rettv);
3536
void f_setcmdpos(typval_T *argvars, typval_T *rettv);
37+
void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv);
3638
void f_getcmdtype(typval_T *argvars, typval_T *rettv);
3739
int get_cmdline_firstc(void);
3840
int get_list_range(char_u **str, int *num1, int *num2);

src/proto/usercmd.pro

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* usercmd.c */
22
char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *complp);
3-
char_u *set_context_in_user_cmdarg(char_u *cmd, char_u *arg, long argt, int context, expand_T *xp, int forceit);
43
char_u *set_context_in_user_cmd(expand_T *xp, char_u *arg_in);
4+
char_u *set_context_in_user_cmdarg(char_u *cmd, char_u *arg, long argt, int context, expand_T *xp, int forceit);
55
char_u *expand_user_command_name(int idx);
66
char_u *get_user_commands(expand_T *xp, int idx);
77
char_u *get_user_command_name(int idx, int cmdidx);
88
char_u *get_user_cmd_addr_type(expand_T *xp, int idx);
99
char_u *get_user_cmd_flags(expand_T *xp, int idx);
1010
char_u *get_user_cmd_nargs(expand_T *xp, int idx);
1111
char_u *get_user_cmd_complete(expand_T *xp, int idx);
12+
char_u *cmdcomplete_type_to_str(int expand);
1213
int cmdcomplete_str_to_type(char_u *complete_str);
1314
char *uc_fun_cmd(void);
1415
int parse_compl_arg(char_u *value, int vallen, int *complp, long *argt, char_u **compl_arg);

src/testdir/test_cmdline.vim

+12
Original file line numberDiff line numberDiff line change
@@ -3380,4 +3380,16 @@ func Test_cmdline_complete_substitute_short()
33803380
endfor
33813381
endfunc
33823382

3383+
func Check_completion()
3384+
call assert_equal('let a', getcmdline())
3385+
call assert_equal(6, getcmdpos())
3386+
call assert_equal(7, getcmdscreenpos())
3387+
call assert_equal('var', getcmdcompltype())
3388+
return ''
3389+
endfunc
3390+
3391+
func Test_screenpos_and_completion()
3392+
call feedkeys(":let a\<C-R>=Check_completion()\<CR>\<Esc>", "xt")
3393+
endfunc
3394+
33833395
" vim: shiftwidth=2 sts=2 expandtab

src/usercmd.c

+19
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,25 @@ get_user_cmd_complete(expand_T *xp UNUSED, int idx)
443443
}
444444

445445
#ifdef FEAT_EVAL
446+
/*
447+
* Get the name of completion type "expand" as a string.
448+
*/
449+
char_u *
450+
cmdcomplete_type_to_str(int expand)
451+
{
452+
int i;
453+
454+
for (i = 0; command_complete[i].expand != 0; i++)
455+
if (command_complete[i].expand == expand)
456+
return (char_u *)command_complete[i].name;
457+
458+
return NULL;
459+
}
460+
461+
/*
462+
* Get the index of completion type "complete_str".
463+
* Returns EXPAND_NOTHING if no match found.
464+
*/
446465
int
447466
cmdcomplete_str_to_type(char_u *complete_str)
448467
{

src/version.c

+2
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ static char *(features[]) =
746746

747747
static int included_patches[] =
748748
{ /* Add new patch number below this line */
749+
/**/
750+
4903,
749751
/**/
750752
4902,
751753
/**/

0 commit comments

Comments
 (0)