]> granicus.if.org Git - vim/commitdiff
patch 9.0.1227: no cmdline completion for :runtime v9.0.1227
authorroot <root@acermirko.emind.lan>
Sat, 21 Jan 2023 21:56:06 +0000 (21:56 +0000)
committerBram Moolenaar <Bram@vim.org>
Sat, 21 Jan 2023 21:56:06 +0000 (21:56 +0000)
Problem:    No cmdline completion for :runtime.
Solution:   Add completion for :runtime. (closes #11853, closes #11447)
            Improve the resulting matches.

src/cmdexpand.c
src/scriptfile.c
src/testdir/test_cmdline.vim
src/usercmd.c
src/version.c
src/vim.h

index 5a47f6a24a9cafbb759dc238886b1671e3603881..4fe9bd35bcceaecb4370e49aec3d7dc7a3f7e5e6 100644 (file)
@@ -56,6 +56,7 @@ cmdline_fuzzy_completion_supported(expand_T *xp)
            && xp->xp_context != EXPAND_OLD_SETTING
            && xp->xp_context != EXPAND_OWNSYNTAX
            && xp->xp_context != EXPAND_PACKADD
+           && xp->xp_context != EXPAND_RUNTIME
            && xp->xp_context != EXPAND_SHELLCMD
            && xp->xp_context != EXPAND_TAGS
            && xp->xp_context != EXPAND_TAGS_LISTFILES
@@ -1362,6 +1363,7 @@ addstar(
        // For a tag pattern starting with "/" no translation is needed.
        if (context == EXPAND_HELP
                || context == EXPAND_COLORS
+               || context == EXPAND_RUNTIME
                || context == EXPAND_COMPILER
                || context == EXPAND_OWNSYNTAX
                || context == EXPAND_FILETYPE
@@ -2312,6 +2314,11 @@ set_context_by_cmdname(
            xp->xp_pattern = arg;
            break;
 
+       case CMD_runtime:
+           xp->xp_context = EXPAND_RUNTIME;
+           xp->xp_pattern = arg;
+           break;
+
        case CMD_compiler:
            xp->xp_context = EXPAND_COMPILER;
            xp->xp_pattern = arg;
@@ -3019,6 +3026,12 @@ ExpandFromContext(
        return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
                                                                directories);
     }
+    if (xp->xp_context == EXPAND_RUNTIME)
+    {
+       char *directories[] = {"", NULL};
+       return ExpandRTDir(pat, DIP_START + DIP_OPT + DIP_PRNEXT, numMatches,
+                                                        matches, directories);
+    }
     if (xp->xp_context == EXPAND_COMPILER)
     {
        char *directories[] = {"compiler", NULL};
index b5a381cff3f86a7bbbb63d5adc4f8d1d572a2983..3404c22d1aa73e9dc5ad9030ae5610031bc5d4f4 100644 (file)
@@ -990,18 +990,34 @@ ExpandRTDir(
 
     for (i = 0; dirnames[i] != NULL; ++i)
     {
-       s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
-       if (s == NULL)
+       size_t buflen = STRLEN(dirnames[i]) + pat_len * 2 + 17;
+       char_u *buf = alloc(buflen);
+       if (buf == NULL)
        {
            ga_clear_strings(&ga);
            return FAIL;
        }
-       sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
-       globpath(p_rtp, s, &ga, 0);
-       vim_free(s);
+       if (*(dirnames[i]) == NUL)
+       {
+           // empty dir used for :runtime
+           if (gettail(pat) == pat)
+               // no path separator, match dir names and script files
+               vim_snprintf((char *)buf, buflen, "\\(%s*.vim\\)\\|\\(%s*\\)",
+                                                                    pat, pat);
+           else
+               // has path separator, match script files
+               vim_snprintf((char *)buf, buflen, "%s*.vim", pat);
+       }
+       else
+       {
+           vim_snprintf((char *)buf, buflen, "%s/%s*.vim", dirnames[i], pat);
+       }
+       globpath(p_rtp, buf, &ga, 0);
+       vim_free(buf);
     }
 
-    if (flags & DIP_START) {
+    if (flags & DIP_START)
+    {
        for (i = 0; dirnames[i] != NULL; ++i)
        {
            s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
@@ -1016,7 +1032,8 @@ ExpandRTDir(
        }
     }
 
-    if (flags & DIP_OPT) {
+    if (flags & DIP_OPT)
+    {
        for (i = 0; dirnames[i] != NULL; ++i)
        {
            s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
@@ -1036,15 +1053,52 @@ ExpandRTDir(
        match = ((char_u **)ga.ga_data)[i];
        s = match;
        e = s + STRLEN(s);
+       char_u *res_start = s;
+       if ((flags & DIP_PRNEXT) != 0)
+       {
+           char_u *p = (char_u *)strstr((char *)match, (char *)pat);
+           if (p != NULL)
+               // Drop what comes before "pat" in the match, so that for
+               // match "/long/path/syntax/cpp.vim" with pattern
+               // "syntax/cp" we only keep "syntax/cpp.vim".
+               res_start = p;
+       }
+
        if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
        {
-           e -= 4;
-           for (s = e; s > match; MB_PTR_BACK(match, s))
-               if (s < match || vim_ispathsep(*s))
-                   break;
-           ++s;
+           if (res_start == s)
+           {
+               // Only keep the file name.
+               // Remove file ext only if flag DIP_PRNEXT is not present.
+               if ((flags & DIP_PRNEXT) == 0)
+                   e -= 4;
+               for (s = e; s > match; MB_PTR_BACK(match, s))
+               {
+                   if (s < match)
+                       break;
+                   if (vim_ispathsep(*s))
+                   {
+                       res_start = s + 1;
+                       break;
+                   }
+               }
+           }
+
            *e = NUL;
-           mch_memmove(match, s, e - s + 1);
+       }
+
+       if (res_start > match)
+           mch_memmove(match, res_start, e - res_start + 1);
+
+       // remove entries that look like backup files
+       if (e > s && e[-1] == '~')
+       {
+           vim_free(match);
+           char_u  **fnames = (char_u **)ga.ga_data;
+           for (int j = i + 1; j < ga.ga_len; ++j)
+               fnames[j - 1] = fnames[j];
+           --ga.ga_len;
+           --i;
        }
     }
 
index 9eca9186fa9b265a45841ff07975207be5dd5fc6..9406a7eac4a6634f6d4220b8c3234ce4e43e606b 100644 (file)
@@ -552,6 +552,15 @@ func Test_getcompletion()
   call assert_true(index(l, '<buffer>') >= 0)
   let l = getcompletion('not', 'mapclear')
   call assert_equal([], l)
+  
+  let l = getcompletion('', 'runtime')
+  call assert_true(index(l, 'defaults.vim') >= 0)
+  let l = getcompletion('synt', 'runtime')
+  call assert_true(index(l, 'syntax') >= 0)
+  let l = getcompletion('syntax/vi', 'runtime')
+  call assert_true(index(l, 'syntax/vim.vim') >= 0)
+  let l = getcompletion('notexitsts', 'runtime')
+  call assert_equal([], l)
 
   let l = getcompletion('.', 'shellcmd')
   call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
index 3bd6fd536ff09cd080da69afe65b8d6178b42dea..9dcc20e7f44ab97312657d55d37efa63bf0ae7a7 100644 (file)
@@ -86,6 +86,7 @@ static struct
 #endif
     {EXPAND_SETTINGS, "option"},
     {EXPAND_PACKADD, "packadd"},
+    {EXPAND_RUNTIME, "runtime"},
     {EXPAND_SHELLCMD, "shellcmd"},
 #if defined(FEAT_SIGNS)
     {EXPAND_SIGN, "sign"},
index 73211b6a5a2d4f0ed6cab3cb8f6e7203734cf432..ebcd70ff358fe8f097c1e7fef0cd2ed0e2e14a64 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1227,
 /**/
     1226,
 /**/
index 7d58b06a36840a40c294b058c4efc824fff1a0d8..930b17b104c591d6cf640b712f5953ba058fbb06 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -811,6 +811,7 @@ extern int (*dyn_libintl_wputenv)(const wchar_t *envstring);
 #define EXPAND_DISASSEMBLE     50
 #define EXPAND_BREAKPOINT      51
 #define EXPAND_SCRIPTNAMES     52
+#define EXPAND_RUNTIME         53
 
 // Values for exmode_active (0 is no exmode)
 #define EXMODE_NORMAL          1
@@ -2661,6 +2662,7 @@ typedef enum {
 #define DIP_NORTP   0x20       // do not use 'runtimepath'
 #define DIP_NOAFTER 0x40       // skip "after" directories
 #define DIP_AFTER   0x80       // only use "after" directories
+#define DIP_PRNEXT  0x100      // for print also file extension
 
 // Lowest number used for window ID. Cannot have this many windows.
 #define LOWEST_WIN_ID 1000