Skip to content

Commit 038e09e

Browse files
committed
patch 8.2.2468: not easy to get the full command name from a shortened one
Problem: Not easy to get the full command name from a shortened one. Solution: Add fullcommand(). (Martin Tournoij, closes #7777)
1 parent 139348f commit 038e09e

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

runtime/doc/eval.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,7 @@ foldlevel({lnum}) Number fold level at {lnum}
25622562
foldtext() String line displayed for closed fold
25632563
foldtextresult({lnum}) String text for closed fold at {lnum}
25642564
foreground() Number bring the Vim window to the foreground
2565+
fullcommand({name}) String get full command from {name}
25652566
funcref({name} [, {arglist}] [, {dict}])
25662567
Funcref reference to function {name}
25672568
function({name} [, {arglist}] [, {dict}])
@@ -4902,6 +4903,21 @@ foreground() Move the Vim window to the foreground. Useful when sent from
49024903
{only in the Win32, Athena, Motif and GTK GUI versions and the
49034904
Win32 console version}
49044905

4906+
fullcommand({name}) *fullcommand()*
4907+
Get the full command name from a short abbreviated command
4908+
name; see |20.2| for details on command abbreviations.
4909+
4910+
{name} may start with a `:` and can include a [range], these
4911+
are skipped and not returned.
4912+
Returns an empty string if a command doesn't exist or if it's
4913+
ambiguous (for user-defined functions).
4914+
4915+
For example `fullcommand('s')`, `fullcommand('sub')`,
4916+
`fullcommand(':%substitute')` all return "substitute".
4917+
4918+
Can also be used as a |method|: >
4919+
GetName()->fullcommand()
4920+
<
49054921
*funcref()*
49064922
funcref({name} [, {arglist}] [, {dict}])
49074923
Just like |function()|, but the returned Funcref will lookup

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ Command line: *command-line-functions*
883883
getcmdtype() return the current command-line type
884884
getcmdwintype() return the current command-line window type
885885
getcompletion() list of command-line completion matches
886+
fullcommand() get full command name
886887

887888
Quickfix and location lists: *quickfix-functions*
888889
getqflist() list of quickfix errors

src/evalfunc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,8 @@ static funcentry_T global_functions[] =
978978
ret_string, f_foldtextresult},
979979
{"foreground", 0, 0, 0, NULL,
980980
ret_void, f_foreground},
981+
{"fullcommand", 1, 1, FEARG_1, arg1_string,
982+
ret_string, f_fullcommand},
981983
{"funcref", 1, 3, FEARG_1, NULL,
982984
ret_func_any, f_funcref},
983985
{"function", 1, 3, FEARG_1, NULL,

src/ex_docmd.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,6 +3668,33 @@ cmd_exists(char_u *name)
36683668
return 0; // trailing garbage
36693669
return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
36703670
}
3671+
3672+
/*
3673+
* "fullcommand" function
3674+
*/
3675+
void
3676+
f_fullcommand(typval_T *argvars, typval_T *rettv)
3677+
{
3678+
exarg_T ea;
3679+
char_u *name = argvars[0].vval.v_string;
3680+
char_u *p;
3681+
3682+
while (name[0] != NUL && name[0] == ':')
3683+
name++;
3684+
name = skip_range(name, TRUE, NULL);
3685+
3686+
rettv->v_type = VAR_STRING;
3687+
3688+
ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3689+
ea.cmdidx = (cmdidx_T)0;
3690+
p = find_ex_command(&ea, NULL, NULL, NULL);
3691+
if (p == NULL || ea.cmdidx == CMD_SIZE)
3692+
return;
3693+
3694+
rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3695+
? get_user_commands(NULL, ea.useridx)
3696+
: cmdnames[ea.cmdidx].cmd_name);
3697+
}
36713698
#endif
36723699

36733700
cmdidx_T

src/proto/evalfunc.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ void range_list_materialize(list_T *list);
2323
float_T vim_round(float_T f);
2424
long do_searchpair(char_u *spat, char_u *mpat, char_u *epat, int dir, typval_T *skip, int flags, pos_T *match_pos, linenr_T lnum_stop, long time_limit);
2525
void f_string(typval_T *argvars, typval_T *rettv);
26+
void f_fullcommand(typval_T *argvars, typval_T *rettv);
2627
/* vim: set ft=c : */

src/testdir/test_cmdline.vim

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,43 @@ func Test_getcompletion()
442442
call assert_fails('call getcompletion("abc", [])', 'E475:')
443443
endfunc
444444

445+
func Test_fullcommand()
446+
let tests = {
447+
\ '': '',
448+
\ ':': '',
449+
\ ':::': '',
450+
\ ':::5': '',
451+
\ 'not_a_cmd': '',
452+
\ 'Check': '',
453+
\ 'syntax': 'syntax',
454+
\ ':syntax': 'syntax',
455+
\ '::::syntax': 'syntax',
456+
\ 'sy': 'syntax',
457+
\ 'syn': 'syntax',
458+
\ 'synt': 'syntax',
459+
\ ':sy': 'syntax',
460+
\ '::::sy': 'syntax',
461+
\ 'match': 'match',
462+
\ '2match': 'match',
463+
\ '3match': 'match',
464+
\ 'aboveleft': 'aboveleft',
465+
\ 'abo': 'aboveleft',
466+
\ 's': 'substitute',
467+
\ '5s': 'substitute',
468+
\ ':5s': 'substitute',
469+
\ "'<,'>s": 'substitute',
470+
\ ":'<,'>s": 'substitute',
471+
\ 'CheckUni': 'CheckUnix',
472+
\ 'CheckUnix': 'CheckUnix',
473+
\ }
474+
475+
for [in, want] in items(tests)
476+
call assert_equal(want, fullcommand(in))
477+
endfor
478+
479+
call assert_equal('syntax', 'syn'->fullcommand())
480+
endfunc
481+
445482
func Test_shellcmd_completion()
446483
let save_path = $PATH
447484

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2468,
753755
/**/
754756
2467,
755757
/**/

0 commit comments

Comments
 (0)