To: vim_dev@googlegroups.com Subject: Patch 9.0.1231 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.1231 Problem: Completion of :runtime does not handle {where} argument. Solution: Parse the {where} argument. (closes #11863) Files: runtime/doc/builtin.txt, src/cmdexpand.c, src/filepath.c, src/findfile.c, src/proto/cmdexpand.pro, src/proto/scriptfile.pro, src/scriptfile.c, src/vim.h, src/testdir/test_cmdline.vim, src/testdir/test_packadd.vim *** ../vim-9.0.1230/runtime/doc/builtin.txt 2023-01-20 16:00:49.538341275 +0000 --- runtime/doc/builtin.txt 2023-01-22 18:32:11.846245608 +0000 *************** *** 3528,3533 **** --- 3528,3534 ---- messages |:messages| suboptions option options packadd optional package |pack-add| names + runtime runtime file names |:runtime| scriptnames sourced script names |:scriptnames| shellcmd Shell command sign |:sign| suboptions *** ../vim-9.0.1230/src/cmdexpand.c 2023-01-21 21:56:02.186002026 +0000 --- src/cmdexpand.c 2023-01-22 18:32:11.846245608 +0000 *************** *** 2315,2322 **** break; case CMD_runtime: ! xp->xp_context = EXPAND_RUNTIME; ! xp->xp_pattern = arg; break; case CMD_compiler: --- 2315,2321 ---- break; case CMD_runtime: ! set_context_in_runtime_cmd(xp, arg); break; case CMD_compiler: *************** *** 3028,3036 **** } 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) { --- 3027,3033 ---- } if (xp->xp_context == EXPAND_RUNTIME) { ! return expand_runtime_cmd(pat, numMatches, matches); } if (xp->xp_context == EXPAND_COMPILER) { *************** *** 3612,3624 **** /* * Expand "file" for all comma-separated directories in "path". * Adds the matches to "ga". Caller must init "ga". */ void globpath( char_u *path, char_u *file, garray_T *ga, ! int expand_options) { expand_T xpc; char_u *buf; --- 3609,3623 ---- /* * Expand "file" for all comma-separated directories in "path". * Adds the matches to "ga". Caller must init "ga". + * If "dirs" is TRUE only expand directory names. */ void globpath( char_u *path, char_u *file, garray_T *ga, ! int expand_options, ! int dirs) { expand_T xpc; char_u *buf; *************** *** 3631,3637 **** return; ExpandInit(&xpc); ! xpc.xp_context = EXPAND_FILES; // Loop over all entries in {path}. while (*path != NUL) --- 3630,3636 ---- return; ExpandInit(&xpc); ! xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES; // Loop over all entries in {path}. while (*path != NUL) *************** *** 4038,4043 **** --- 4037,4047 ---- xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); } # endif + if (xpc.xp_context == EXPAND_RUNTIME) + { + set_context_in_runtime_cmd(&xpc, xpc.xp_pattern); + xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); + } } if (cmdline_fuzzy_completion_supported(&xpc)) *** ../vim-9.0.1230/src/filepath.c 2023-01-09 19:04:19.304528371 +0000 --- src/filepath.c 2023-01-22 18:32:11.846245608 +0000 *************** *** 1388,1394 **** if (file != NULL && !error) { ga_init2(&ga, sizeof(char_u *), 10); ! globpath(tv_get_string(&argvars[0]), file, &ga, flags); if (rettv->v_type == VAR_STRING) rettv->vval.v_string = ga_concat_strings(&ga, "\n"); else if (rettv_list_alloc(rettv) == OK) --- 1388,1394 ---- if (file != NULL && !error) { ga_init2(&ga, sizeof(char_u *), 10); ! globpath(tv_get_string(&argvars[0]), file, &ga, flags, FALSE); if (rettv->v_type == VAR_STRING) rettv->vval.v_string = ga_concat_strings(&ga, "\n"); else if (rettv_list_alloc(rettv) == OK) *** ../vim-9.0.1230/src/findfile.c 2023-01-09 19:04:19.304528371 +0000 --- src/findfile.c 2023-01-22 18:32:11.846245608 +0000 *************** *** 2533,2539 **** glob_flags |= WILD_ICASE; if (flags & EW_ADDSLASH) glob_flags |= WILD_ADD_SLASH; ! globpath(paths, pattern, gap, glob_flags); vim_free(paths); return gap->ga_len; --- 2533,2539 ---- glob_flags |= WILD_ICASE; if (flags & EW_ADDSLASH) glob_flags |= WILD_ADD_SLASH; ! globpath(paths, pattern, gap, glob_flags, FALSE); vim_free(paths); return gap->ga_len; *** ../vim-9.0.1230/src/proto/cmdexpand.pro 2022-11-12 17:44:08.268849881 +0000 --- src/proto/cmdexpand.pro 2023-01-22 18:36:51.666257831 +0000 *************** *** 14,20 **** void set_expand_context(expand_T *xp); void set_cmd_context(expand_T *xp, char_u *str, int len, int col, int use_ccline); int expand_cmdline(expand_T *xp, char_u *str, int col, int *matchcount, char_u ***matches); ! void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options); int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_wild_list); int wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp); void wildmenu_cleanup(cmdline_info_T *cclp); --- 14,20 ---- void set_expand_context(expand_T *xp); void set_cmd_context(expand_T *xp, char_u *str, int len, int col, int use_ccline); int expand_cmdline(expand_T *xp, char_u *str, int col, int *matchcount, char_u ***matches); ! void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options, int dirs); int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_wild_list); int wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp); void wildmenu_cleanup(cmdline_info_T *cclp); *** ../vim-9.0.1230/src/proto/scriptfile.pro 2022-08-22 13:14:31.892769316 +0100 --- src/proto/scriptfile.pro 2023-01-22 18:37:01.046258138 +0000 *************** *** 6,11 **** --- 6,13 ---- estack_T *estack_pop(void); char_u *estack_sfile(estack_arg_T which); void ex_runtime(exarg_T *eap); + void set_context_in_runtime_cmd(expand_T *xp, char_u *arg); + int expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches); int find_script_by_name(char_u *name); int get_new_scriptitem_for_fname(int *error, char_u *fname); int do_in_path(char_u *path, char_u *name, int flags, void (*callback)(char_u *fname, void *ck), void *cookie); *** ../vim-9.0.1230/src/scriptfile.c 2023-01-21 21:56:02.186002026 +0000 --- src/scriptfile.c 2023-01-22 18:32:11.850245608 +0000 *************** *** 231,270 **** } /* ! * ":runtime [what] {name}" */ ! void ! ex_runtime(exarg_T *eap) { ! char_u *arg = eap->arg; char_u *p = skiptowhite(arg); ! int len = (int)(p - arg); ! int flags = eap->forceit ? DIP_ALL : 0; ! if (STRNCMP(arg, "START", len) == 0) { ! flags += DIP_START + DIP_NORTP; ! arg = skipwhite(arg + len); } ! else if (STRNCMP(arg, "OPT", len) == 0) { ! flags += DIP_OPT + DIP_NORTP; ! arg = skipwhite(arg + len); } ! else if (STRNCMP(arg, "PACK", len) == 0) { ! flags += DIP_START + DIP_OPT + DIP_NORTP; ! arg = skipwhite(arg + len); } ! else if (STRNCMP(arg, "ALL", len) == 0) { ! flags += DIP_START + DIP_OPT; ! arg = skipwhite(arg + len); } source_runtime(arg, flags); } static void source_callback(char_u *fname, void *cookie) { --- 231,310 ---- } /* ! * Get DIP_ flags from the [what] argument of a :runtime command. ! * "*argp" is advanced to after the [what] argument. */ ! static int ! get_runtime_cmd_flags(char_u **argp) { ! char_u *arg = *argp; char_u *p = skiptowhite(arg); ! int what_len = (int)(p - arg); ! if (what_len == 0) ! return 0; ! ! if (STRNCMP(arg, "START", what_len) == 0) { ! *argp = skipwhite(arg + what_len); ! return DIP_START + DIP_NORTP; } ! if (STRNCMP(arg, "OPT", what_len) == 0) { ! *argp = skipwhite(arg + what_len); ! return DIP_OPT + DIP_NORTP; } ! if (STRNCMP(arg, "PACK", what_len) == 0) { ! *argp = skipwhite(arg + what_len); ! return DIP_START + DIP_OPT + DIP_NORTP; } ! if (STRNCMP(arg, "ALL", what_len) == 0) { ! *argp = skipwhite(arg + what_len); ! return DIP_START + DIP_OPT; } + return 0; + } + + /* + * ":runtime [what] {name}" + */ + void + ex_runtime(exarg_T *eap) + { + char_u *arg = eap->arg; + int flags = eap->forceit ? DIP_ALL : 0; + + flags += get_runtime_cmd_flags(&arg); source_runtime(arg, flags); } + static int runtime_expand_flags; + + /* + * Set the completion context for the :runtime command. + */ + void + set_context_in_runtime_cmd(expand_T *xp, char_u *arg) + { + runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags(&arg); + xp->xp_context = EXPAND_RUNTIME; + xp->xp_pattern = arg; + } + + /* + * Handle command line completion for :runtime command. + */ + int + expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches) + { + char *directories[] = {"", NULL}; + return ExpandRTDir(pat, runtime_expand_flags, numMatches, matches, + directories); + } + static void source_callback(char_u *fname, void *cookie) { *************** *** 959,970 **** } /* ! * Expand color scheme, compiler or filetype names. * Search from 'runtimepath': * 'runtimepath'/{dirnames}/{pat}.vim ! * When "flags" has DIP_START: search also from 'start' of 'packpath': * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim ! * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim * "dirnames" is an array with one or more directory names. */ --- 999,1010 ---- } /* ! * Expand runtime file names. * Search from 'runtimepath': * 'runtimepath'/{dirnames}/{pat}.vim ! * When "flags" has DIP_START: search also from "start" of 'packpath': * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim ! * When "flags" has DIP_OPT: search also from "opt" of 'packpath': * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim * "dirnames" is an array with one or more directory names. */ *************** *** 990,1105 **** for (i = 0; dirnames[i] != NULL; ++i) { ! size_t buflen = STRLEN(dirnames[i]) + pat_len * 2 + 17; ! char_u *buf = alloc(buflen); if (buf == NULL) { ga_clear_strings(&ga); return FAIL; } ! 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) ! { ! for (i = 0; dirnames[i] != NULL; ++i) { ! s = alloc(STRLEN(dirnames[i]) + pat_len + 22); ! if (s == NULL) ! { ! ga_clear_strings(&ga); ! return FAIL; ! } ! sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); ! globpath(p_pp, s, &ga, 0); ! vim_free(s); } - } ! if (flags & DIP_OPT) ! { ! for (i = 0; dirnames[i] != NULL; ++i) { ! s = alloc(STRLEN(dirnames[i]) + pat_len + 20); ! if (s == NULL) ! { ! ga_clear_strings(&ga); ! return FAIL; ! } ! sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); ! globpath(p_pp, s, &ga, 0); ! vim_free(s); } } for (i = 0; i < ga.ga_len; ++i) { 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) ! { ! 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; } ! 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; ! } } if (ga.ga_len == 0) --- 1030,1105 ---- for (i = 0; dirnames[i] != NULL; ++i) { ! size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22; ! char *buf = alloc(buf_len); if (buf == NULL) { ga_clear_strings(&ga); return FAIL; } ! char *tail = buf + 15; ! size_t tail_buflen = buf_len - 15; ! int glob_flags = 0; ! int expand_dirs = FALSE; ! ! if (*(dirnames[i]) == NUL) // empty dir used for :runtime ! vim_snprintf(tail, tail_buflen, "%s*.vim", pat); else + vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat); + + expand: + if ((flags & DIP_NORTP) == 0) + globpath(p_rtp, (char_u *)tail, &ga, glob_flags, expand_dirs); + + if (flags & DIP_START) { ! memcpy(tail - 15, "pack/*/start/*/", 15); ! globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs); } ! if (flags & DIP_OPT) { ! memcpy(tail - 13, "pack/*/opt/*/", 13); ! globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs); } ! if (*(dirnames[i]) == NUL && !expand_dirs) { ! // expand dir names in another round ! vim_snprintf(tail, tail_buflen, "%s*", pat); ! glob_flags = WILD_ADD_SLASH; ! expand_dirs = TRUE; ! goto expand; } + + vim_free(buf); } + int pat_pathsep_cnt = 0; + for (i = 0; i < pat_len; ++i) + if (vim_ispathsep(pat[i])) + ++pat_pathsep_cnt; + for (i = 0; i < ga.ga_len; ++i) { match = ((char_u **)ga.ga_data)[i]; s = match; e = s + STRLEN(s); ! if (e - 4 > s && (flags & DIP_KEEPEXT) == 0 ! && STRNICMP(e - 4, ".vim", 4) == 0) { ! e -= 4; *e = NUL; } ! int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0; ! for (s = e; s > match; MB_PTR_BACK(match, s)) ! if (s < match || (vim_ispathsep(*s) ! && ++match_pathsep_cnt > pat_pathsep_cnt)) ! break; ! ++s; ! *e = NUL; ! mch_memmove(match, s, e - s + 1); } if (ga.ga_len == 0) *************** *** 1143,1149 **** return FAIL; } sprintf((char *)s, "pack/*/opt/%s*", pat); ! globpath(p_pp, s, &ga, 0); vim_free(s); for (i = 0; i < ga.ga_len; ++i) --- 1143,1149 ---- return FAIL; } sprintf((char *)s, "pack/*/opt/%s*", pat); ! globpath(p_pp, s, &ga, 0, TRUE); vim_free(s); for (i = 0; i < ga.ga_len; ++i) *** ../vim-9.0.1230/src/vim.h 2023-01-21 21:56:02.186002026 +0000 --- src/vim.h 2023-01-22 18:32:11.850245608 +0000 *************** *** 2662,2668 **** #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 --- 2662,2668 ---- #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_KEEPEXT 0x100 // for completion: include file extension // Lowest number used for window ID. Cannot have this many windows. #define LOWEST_WIN_ID 1000 *** ../vim-9.0.1230/src/testdir/test_cmdline.vim 2023-01-22 12:41:52.000484614 +0000 --- src/testdir/test_cmdline.vim 2023-01-22 18:32:11.850245608 +0000 *************** *** 553,567 **** call assert_true(index(l, '') >= 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 =~ "\\./"')) --- 553,558 ---- *** ../vim-9.0.1230/src/testdir/test_packadd.vim 2022-09-30 21:57:07.543153405 +0100 --- src/testdir/test_packadd.vim 2023-01-22 18:32:11.850245608 +0000 *************** *** 190,197 **** --- 190,199 ---- call mkdir(optdir1 . '/pluginA', 'p') call mkdir(optdir1 . '/pluginC', 'p') + call writefile([], optdir1 . '/unrelated') call mkdir(optdir2 . '/pluginB', 'p') call mkdir(optdir2 . '/pluginC', 'p') + call writefile([], optdir2 . '/unrelated') let li = [] call feedkeys(":packadd \')\call add(li, '\", 't') *************** *** 367,370 **** --- 369,434 ---- call assert_equal('runstartopt', g:sequence) endfunc + func Test_runtime_completion() + let rundir = &packpath . '/runtime/Xextra' + let startdir = &packpath . '/pack/mine/start/foo/Xextra' + let optdir = &packpath . '/pack/mine/opt/bar/Xextra' + call mkdir(rundir . '/Xrunbaz', 'p') + call mkdir(startdir . '/Xstartbaz', 'p') + call mkdir(optdir . '/Xoptbaz', 'p') + call writefile([], rundir . '/../Xrunfoo.vim') + call writefile([], rundir . '/Xrunbar.vim') + call writefile([], rundir . '/Xunrelated') + call writefile([], rundir . '/../Xunrelated') + call writefile([], startdir . '/../Xstartfoo.vim') + call writefile([], startdir . '/Xstartbar.vim') + call writefile([], startdir . '/Xunrelated') + call writefile([], startdir . '/../Xunrelated') + call writefile([], optdir . '/../Xoptfoo.vim') + call writefile([], optdir . '/Xoptbar.vim') + call writefile([], optdir . '/Xunrelated') + call writefile([], optdir . '/../Xunrelated') + exe 'set rtp=' . &packpath . '/runtime' + + func Check_runtime_completion(arg, arg1, res) + call feedkeys(':runtime ' .. a:arg .. "\\\"\", 'xt') + call assert_equal('"runtime ' .. a:arg1 .. join(a:res), @:) + call assert_equal(a:res, getcompletion(a:arg, 'runtime')) + + call feedkeys(':runtime ' .. a:arg .. "X\\\"\", 'xt') + call assert_equal('"runtime ' .. a:arg1 .. join(a:res), @:) + call assert_equal(a:res, getcompletion(a:arg .. 'X', 'runtime')) + endfunc + + call Check_runtime_completion('', '', + \ ['Xextra/', 'Xrunfoo.vim']) + call Check_runtime_completion('Xextra/', '', + \ ['Xextra/Xrunbar.vim', 'Xextra/Xrunbaz/']) + + call Check_runtime_completion('START ', 'START ', + \ ['Xextra/', 'Xstartfoo.vim']) + call Check_runtime_completion('START Xextra/', 'START ', + \ ['Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/']) + + call Check_runtime_completion('OPT ', 'OPT ', + \ ['Xextra/', 'Xoptfoo.vim']) + call Check_runtime_completion('OPT Xextra/', 'OPT ', + \ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/']) + + call Check_runtime_completion('PACK ', 'PACK ', + \ ['Xextra/', 'Xoptfoo.vim', 'Xstartfoo.vim']) + call Check_runtime_completion('PACK Xextra/', 'PACK ', + \ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/', + \ 'Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/']) + + call Check_runtime_completion('ALL ', 'ALL ', + \ ['Xextra/', 'Xoptfoo.vim', 'Xrunfoo.vim', 'Xstartfoo.vim']) + call Check_runtime_completion('ALL Xextra/', 'ALL ', + \ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/', + \ 'Xextra/Xrunbar.vim', 'Xextra/Xrunbaz/', + \ 'Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/']) + + delfunc Check_runtime_completion + endfunc + " vim: shiftwidth=2 sts=2 expandtab *** ../vim-9.0.1230/src/version.c 2023-01-22 18:16:41.366116369 +0000 --- src/version.c 2023-01-22 18:34:01.298251223 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 1231, /**/ -- On the other hand, you have different fingers. -- Steven Wright /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///