patch 9.0.1238: :runtime completion can be further improved

Problem:    :runtime completion can be further improved.
Solution:   Also complete the {where} argument values and adjust the
            completion for that. (closes #11874)
diff --git a/src/scriptfile.c b/src/scriptfile.c
index aa563d9..630a116 100644
--- a/src/scriptfile.c
+++ b/src/scriptfile.c
@@ -230,37 +230,35 @@
 }
 
 /*
- * Get DIP_ flags from the [what] argument of a :runtime command.
- * "*argp" is advanced to after the [what] argument.
+ * Get DIP_ flags from the [where] argument of a :runtime command.
+ * "*argp" is advanced to after the [where] argument if it is found.
  */
     static int
-get_runtime_cmd_flags(char_u **argp)
+get_runtime_cmd_flags(char_u **argp, size_t where_len)
 {
     char_u *arg = *argp;
-    char_u  *p = skiptowhite(arg);
-    int	    what_len = (int)(p - arg);
 
-    if (what_len == 0)
+    if (where_len == 0)
 	return 0;
 
-    if (STRNCMP(arg, "START", what_len) == 0)
+    if (STRNCMP(arg, "START", where_len) == 0)
     {
-	*argp = skipwhite(arg + what_len);
+	*argp = skipwhite(arg + where_len);
 	return DIP_START + DIP_NORTP;
     }
-    if (STRNCMP(arg, "OPT", what_len) == 0)
+    if (STRNCMP(arg, "OPT", where_len) == 0)
     {
-	*argp = skipwhite(arg + what_len);
+	*argp = skipwhite(arg + where_len);
 	return DIP_OPT + DIP_NORTP;
     }
-    if (STRNCMP(arg, "PACK", what_len) == 0)
+    if (STRNCMP(arg, "PACK", where_len) == 0)
     {
-	*argp = skipwhite(arg + what_len);
+	*argp = skipwhite(arg + where_len);
 	return DIP_START + DIP_OPT + DIP_NORTP;
     }
-    if (STRNCMP(arg, "ALL", what_len) == 0)
+    if (STRNCMP(arg, "ALL", where_len) == 0)
     {
-	*argp = skipwhite(arg + what_len);
+	*argp = skipwhite(arg + where_len);
 	return DIP_START + DIP_OPT;
     }
 
@@ -268,15 +266,15 @@
 }
 
 /*
- * ":runtime [what] {name}"
+ * ":runtime [where] {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);
+    char_u  *p = skiptowhite(arg);
+    flags += get_runtime_cmd_flags(&arg, p - arg);
     source_runtime(arg, flags);
 }
 
@@ -288,22 +286,13 @@
     void
 set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
 {
-    runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags(&arg);
+    char_u  *p = skiptowhite(arg);
+    runtime_expand_flags
+	= *p != NUL ? get_runtime_cmd_flags(&arg, p - arg) : 0;
     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)
 {
@@ -997,6 +986,96 @@
 	}
 }
 
+    static void
+ExpandRTDir_int(
+    char_u	*pat,
+    size_t	pat_len,
+    int		flags,
+    int		keep_ext,
+    garray_T	*gap,
+    char	*dirnames[])
+{
+    for (int 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(gap);
+	    return;
+	}
+	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, gap, glob_flags, expand_dirs);
+
+	if (flags & DIP_START)
+	{
+	    memcpy(tail - 15, "pack/*/start/*/", 15);
+	    globpath(p_pp, (char_u *)tail - 15, gap, glob_flags, expand_dirs);
+	}
+
+	if (flags & DIP_OPT)
+	{
+	    memcpy(tail - 13, "pack/*/opt/*/", 13);
+	    globpath(p_pp, (char_u *)tail - 13, gap, 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 (size_t i = 0; i < pat_len; ++i)
+	if (vim_ispathsep(pat[i]))
+	    ++pat_pathsep_cnt;
+
+    for (int i = 0; i < gap->ga_len; ++i)
+    {
+	char_u *match = ((char_u **)gap->ga_data)[i];
+	char_u *s = match;
+	char_u *e = s + STRLEN(s);
+	if (e - 4 > s && !keep_ext && 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;
+	if (s != match)
+	    mch_memmove(match, s, e - s + 1);
+    }
+
+    if (gap->ga_len == 0)
+	return;
+
+    // Sort and remove duplicates which can happen when specifying multiple
+    // directories in dirnames.
+    remove_duplicates(gap);
+}
+
 /*
  * Expand runtime file names.
  * Search from 'runtimepath':
@@ -1015,101 +1094,56 @@
     char_u	***file,
     char	*dirnames[])
 {
-    char_u	*s;
-    char_u	*e;
-    char_u	*match;
-    garray_T	ga;
-    int		i;
-    int		pat_len;
-
     *num_file = 0;
     *file = NULL;
-    pat_len = (int)STRLEN(pat);
+
+    garray_T	ga;
     ga_init2(&ga, sizeof(char *), 10);
 
-    for (i = 0; dirnames[i] != NULL; ++i)
+    ExpandRTDir_int(pat, STRLEN(pat), flags, FALSE, &ga, dirnames);
+
+    if (ga.ga_len == 0)
+	return FAIL;
+
+    *file = ga.ga_data;
+    *num_file = ga.ga_len;
+    return OK;
+}
+
+/*
+ * Handle command line completion for :runtime command.
+ */
+    int
+expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
+{
+    *numMatches = 0;
+    *matches = NULL;
+
+    garray_T	ga;
+    ga_init2(&ga, sizeof(char *), 10);
+
+    size_t pat_len = (int)STRLEN(pat);
+    char *dirnames[] = {"", NULL};
+    ExpandRTDir_int(pat, pat_len, runtime_expand_flags, TRUE, &ga, dirnames);
+
+    // Try to complete values for [where] argument when none was found.
+    if (runtime_expand_flags == 0)
     {
-	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);
+	char *where_values[] = {"START", "OPT", "PACK", "ALL"};
+	for (size_t i = 0; i < ARRAY_LENGTH(where_values); ++i)
+	    if (STRNCMP(pat, where_values[i], pat_len) == 0)
+	    {
+		char_u *p = vim_strsave((char_u *)where_values[i]);
+		if (p != NULL && ga_add_string(&ga, p) == FAIL)
+		    vim_free(p);
+	    }
     }
 
     if (ga.ga_len == 0)
 	return FAIL;
 
-    // Sort and remove duplicates which can happen when specifying multiple
-    // directories in dirnames.
-    remove_duplicates(&ga);
-
-    *file = ga.ga_data;
-    *num_file = ga.ga_len;
+    *matches = ga.ga_data;
+    *numMatches = ga.ga_len;
     return OK;
 }