]> granicus.if.org Git - vim/commitdiff
patch 8.2.2468: not easy to get the full command name from a shortened one v8.2.2468
authorBram Moolenaar <Bram@vim.org>
Sat, 6 Feb 2021 11:38:51 +0000 (12:38 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 6 Feb 2021 11:38:51 +0000 (12:38 +0100)
Problem:    Not easy to get the full command name from a shortened one.
Solution:   Add fullcommand(). (Martin Tournoij, closes #7777)

runtime/doc/eval.txt
runtime/doc/usr_41.txt
src/evalfunc.c
src/ex_docmd.c
src/proto/evalfunc.pro
src/testdir/test_cmdline.vim
src/version.c

index 8bab492727678f19871efeb4024295cfb47f419c..0773b247b34bdddb87d7e2eeae862f8993ba3a41 100644 (file)
@@ -2562,6 +2562,7 @@ foldlevel({lnum})         Number  fold level at {lnum}
 foldtext()                     String  line displayed for closed fold
 foldtextresult({lnum})         String  text for closed fold at {lnum}
 foreground()                   Number  bring the Vim window to the foreground
+fullcommand({name})            String  get full command from {name}
 funcref({name} [, {arglist}] [, {dict}])
                                Funcref reference to function {name}
 function({name} [, {arglist}] [, {dict}])
@@ -4902,6 +4903,21 @@ foreground()     Move the Vim window to the foreground.  Useful when sent from
                {only in the Win32, Athena, Motif and GTK GUI versions and the
                Win32 console version}
 
+fullcommand({name})                                            *fullcommand()*
+               Get the full command name from a short abbreviated command
+               name; see |20.2| for details on command abbreviations.
+
+               {name} may start with a `:` and can include a [range], these
+               are skipped and not returned.
+               Returns an empty string if a command doesn't exist or if it's
+               ambiguous (for user-defined functions).
+
+               For example `fullcommand('s')`, `fullcommand('sub')`,
+               `fullcommand(':%substitute')` all return "substitute".
+
+               Can also be used as a |method|: >
+                       GetName()->fullcommand()
+<
                                                *funcref()*
 funcref({name} [, {arglist}] [, {dict}])
                Just like |function()|, but the returned Funcref will lookup
index 98e9a10a61e7a1c5f5dc153b917b39dfafd34bbb..7c143d7199d174467d1fc496f75906b6e205af52 100644 (file)
@@ -883,6 +883,7 @@ Command line:                                       *command-line-functions*
        getcmdtype()            return the current command-line type
        getcmdwintype()         return the current command-line window type
        getcompletion()         list of command-line completion matches
+       fullcommand()           get full command name
 
 Quickfix and location lists:                   *quickfix-functions*
        getqflist()             list of quickfix errors
index f15139f362c7dfb2f6e17ccab2f045e081f84fc5..01994b047563dcac39eb2b9bb460516e2b87500f 100644 (file)
@@ -978,6 +978,8 @@ static funcentry_T global_functions[] =
                        ret_string,         f_foldtextresult},
     {"foreground",     0, 0, 0,            NULL,
                        ret_void,           f_foreground},
+    {"fullcommand",    1, 1, FEARG_1,      arg1_string,
+                       ret_string,         f_fullcommand},
     {"funcref",                1, 3, FEARG_1,      NULL,
                        ret_func_any,       f_funcref},
     {"function",       1, 3, FEARG_1,      NULL,
index d31b56c8cea14a7e4ba319d3e5ce40e9a24ebd9e..da499c9e4af16902a5aca33e6c7ce99393c9e74b 100644 (file)
@@ -3668,6 +3668,33 @@ cmd_exists(char_u *name)
        return 0;       // trailing garbage
     return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
 }
+
+/*
+ * "fullcommand" function
+ */
+    void
+f_fullcommand(typval_T *argvars, typval_T *rettv)
+{
+    exarg_T  ea;
+    char_u   *name = argvars[0].vval.v_string;
+    char_u   *p;
+
+    while (name[0] != NUL && name[0] == ':')
+       name++;
+    name = skip_range(name, TRUE, NULL);
+
+    rettv->v_type = VAR_STRING;
+
+    ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
+    ea.cmdidx = (cmdidx_T)0;
+    p = find_ex_command(&ea, NULL, NULL, NULL);
+    if (p == NULL || ea.cmdidx == CMD_SIZE)
+       return;
+
+    rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
+                                   ? get_user_commands(NULL, ea.useridx)
+                                   : cmdnames[ea.cmdidx].cmd_name);
+}
 #endif
 
     cmdidx_T
index 13dffacfff44a63208c372843fbb5292bf49990c..2b194486f69558a403df0136c2719feb7af8fecd 100644 (file)
@@ -23,4 +23,5 @@ void range_list_materialize(list_T *list);
 float_T vim_round(float_T f);
 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);
 void f_string(typval_T *argvars, typval_T *rettv);
+void f_fullcommand(typval_T *argvars, typval_T *rettv);
 /* vim: set ft=c : */
index 7f62dfe77487fc3307613e685c647f3b81e8f8b0..8faa25fe8af70316c4f427bb89416fdd8adfcfbc 100644 (file)
@@ -442,6 +442,43 @@ func Test_getcompletion()
   call assert_fails('call getcompletion("abc", [])', 'E475:')
 endfunc
 
+func Test_fullcommand()
+  let tests = {
+        \ '':           '',
+        \ ':':          '',
+        \ ':::':        '',
+        \ ':::5':       '',
+        \ 'not_a_cmd':  '',
+        \ 'Check':      '',
+        \ 'syntax':     'syntax',
+        \ ':syntax':    'syntax',
+        \ '::::syntax': 'syntax',
+        \ 'sy':         'syntax',
+        \ 'syn':        'syntax',
+        \ 'synt':       'syntax',
+        \ ':sy':        'syntax',
+        \ '::::sy':     'syntax',
+        \ 'match':      'match',
+        \ '2match':     'match',
+        \ '3match':     'match',
+        \ 'aboveleft':  'aboveleft',
+        \ 'abo':        'aboveleft',
+        \ 's':          'substitute',
+        \ '5s':         'substitute',
+        \ ':5s':        'substitute',
+        \ "'<,'>s":     'substitute',
+        \ ":'<,'>s":    'substitute',
+        \ 'CheckUni':   'CheckUnix',
+        \ 'CheckUnix':  'CheckUnix',
+  \ }
+
+  for [in, want] in items(tests)
+    call assert_equal(want, fullcommand(in))
+  endfor
+
+  call assert_equal('syntax', 'syn'->fullcommand())
+endfunc
+
 func Test_shellcmd_completion()
   let save_path = $PATH
 
index df206d1ccbcfc698d41808240aa0cef5dc9bfa51..e8c286b107ab2c393e33ab3e804dfa04971b13ed 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2468,
 /**/
     2467,
 /**/