patch 9.0.1231: completion of :runtime does not handle {where} argument

Problem:    Completion of :runtime does not handle {where} argument.
Solution:   Parse the {where} argument. (closes #11863)
diff --git a/src/scriptfile.c b/src/scriptfile.c
index 3404c22..da6bdd0 100644
--- a/src/scriptfile.c
+++ b/src/scriptfile.c
@@ -231,40 +231,80 @@
 }
 
 /*
+ * 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;
-    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);
-    }
-
+    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,12 +999,12 @@
 }
 
 /*
- * Expand color scheme, compiler or filetype names.
+ * Expand runtime file names.
  * Search from 'runtimepath':
  *   'runtimepath'/{dirnames}/{pat}.vim
- * When "flags" has DIP_START: search also from 'start' of 'packpath':
+ * 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':
+ * 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,116 +1030,76 @@
 
     for (i = 0; dirnames[i] != NULL; ++i)
     {
-	size_t buflen = STRLEN(dirnames[i]) + pat_len * 2 + 17;
-	char_u *buf = alloc(buflen);
+	size_t		buf_len = STRLEN(dirnames[i]) + pat_len + 22;
+	char		*buf = alloc(buf_len);
 	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);
-	}
+	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)
 	{
-	    vim_snprintf((char *)buf, buflen, "%s/%s*.vim", dirnames[i], pat);
+	    memcpy(tail - 15, "pack/*/start/*/", 15);
+	    globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs);
 	}
-	globpath(p_rtp, buf, &ga, 0);
+
+	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);
     }
 
-    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);
-	}
-    }
+    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);
-	char_u *res_start = s;
-	if ((flags & DIP_PRNEXT) != 0)
+	if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
+					    && STRNICMP(e - 4, ".vim", 4) == 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 -= 4;
 	    *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;
-	}
+	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,7 +1143,7 @@
 	return FAIL;
     }
     sprintf((char *)s, "pack/*/opt/%s*", pat);
-    globpath(p_pp, s, &ga, 0);
+    globpath(p_pp, s, &ga, 0, TRUE);
     vim_free(s);
 
     for (i = 0; i < ga.ga_len; ++i)