map name autocompletion

This commit is contained in:
JBerg 2020-09-05 22:32:13 -04:00
parent ffe9997735
commit a843ef416f
3 changed files with 128 additions and 1 deletions

View file

@ -325,8 +325,48 @@ CompleteCommand(void)
{
key_lines[edit_line][key_linepos] = 0;
}
}
return;
return;
}
void
CompleteMapNameCommand(void)
{
int i;
char *s, *t, *cmdArg;
const char *mapCmdString = "map ";
s = key_lines[edit_line] + 1;
if ((*s == '\\') || (*s == '/'))
{
s++;
}
t = s;
for (i = 0; i < strlen(mapCmdString); i++)
{
if (t[i] == mapCmdString[i])
{
s++;
}
else
{
return;
}
}
cmdArg = Cmd_CompleteMapCommand(s);
if (cmdArg)
{
key_lines[edit_line][1] = '/';
strcpy(key_lines[edit_line] + 2, mapCmdString);
key_linepos = strlen(key_lines[edit_line]);
strcpy(key_lines[edit_line] + key_linepos, cmdArg);
key_linepos = key_linepos + strlen(cmdArg);
}
}
@ -407,6 +447,8 @@ Key_Console(int key)
{
/* command completion */
CompleteCommand();
CompleteMapNameCommand();
return;
}

View file

@ -888,6 +888,89 @@ Cmd_CompleteCommand(char *partial)
return NULL;
}
char *
Cmd_CompleteMapCommand(char *partial)
{
char **mapNames;
int i, j, k, nbMatches, len, nMaps;
char *mapName;
char *pmatch[1024];
qboolean partialFillContinue = true;
if ((mapNames = FS_ListFiles2("maps/*.bsp", &nMaps, 0, 0)) != 0)
{
len = strlen(partial);
nbMatches = 0;
memset(retval, 0, strlen(retval));
for (i = 0; i < nMaps - 1; i++)
{
if (strrchr(mapNames[i], '/'))
{
mapName = strrchr(mapNames[i], '/') + 1;
}
else
{
mapName = mapNames[i];
}
mapName = strtok(mapName, ".");
/* check for exact match */
if (!strcmp(partial, mapName))
{
strcpy(retval, partial);
}
/* check for partial match */
else if (!strncmp(partial, mapName, len))
{
pmatch[nbMatches] = mapName;
nbMatches++;
}
}
if (nbMatches == 1)
{
strcpy(retval, pmatch[0]);
}
else if (nbMatches > 1)
{
Com_Printf("\n=================\n\n");
for (j = 0; j < nbMatches; j++)
{
Com_Printf("%s\n", pmatch[j]);
}
//partial complete
for (j = 0; j < strlen(pmatch[0]); j++)
{
for (k = 1; k < nbMatches; k++)
{
if (j >= strlen(pmatch[k]) || pmatch[0][j] != pmatch[k][j])
{
partialFillContinue = false;
break;
}
}
if (partialFillContinue)
{
retval[j] = pmatch[0][j];
}
else
{
break;
}
}
}
FS_FreeList(mapNames, nMaps);
}
return retval;
}
qboolean
Cmd_IsComplete(char *command)
{

View file

@ -371,6 +371,8 @@ qboolean Cmd_Exists(char *cmd_name);
char *Cmd_CompleteCommand(char *partial);
char *Cmd_CompleteMapCommand(char *partial);
/* attempts to match a partial command for automatic command line completion */
/* returns NULL if nothing fits */