Merge branch 'autocomplete-aliases' into 'next'

Autocomplete aliases in console

See merge request STJr/SRB2!1113
This commit is contained in:
James R 2020-10-08 20:30:07 -04:00
commit 355cc1a697
3 changed files with 85 additions and 24 deletions

View File

@ -560,7 +560,7 @@ static boolean COM_Exists(const char *com_name)
* \param partial The partial name of the command (potentially).
* \param skips Number of commands to skip.
* \return The complete command name, or NULL.
* \sa CV_CompleteVar
* \sa CV_CompleteAlias, CV_CompleteVar
*/
const char *COM_CompleteCommand(const char *partial, INT32 skips)
{
@ -581,6 +581,32 @@ const char *COM_CompleteCommand(const char *partial, INT32 skips)
return NULL;
}
/** Completes the name of an alias.
*
* \param partial The partial name of the alias (potentially).
* \param skips Number of aliases to skip.
* \return The complete alias name, or NULL.
* \sa CV_CompleteCommand, CV_CompleteVar
*/
const char *COM_CompleteAlias(const char *partial, INT32 skips)
{
cmdalias_t *a;
size_t len;
len = strlen(partial);
if (!len)
return NULL;
// check functions
for (a = com_alias; a; a = a->next)
if (!strncmp(partial, a->name, len))
if (!skips--)
return a->name;
return NULL;
}
/** Parses a single line of text into arguments and tries to execute it.
* The text can come from the command buffer, a remote client, or stdin.
*
@ -1321,7 +1347,7 @@ static const char *CV_StringValue(const char *var_name)
* \param partial The partial name of the variable (potentially).
* \param skips Number of variables to skip.
* \return The complete variable name, or NULL.
* \sa COM_CompleteCommand
* \sa COM_CompleteCommand, CV_CompleteAlias
*/
const char *CV_CompleteVar(char *partial, INT32 skips)
{

View File

@ -49,6 +49,8 @@ size_t COM_FirstOption(void);
// match existing command or NULL
const char *COM_CompleteCommand(const char *partial, INT32 skips);
const char *COM_CompleteAlias(const char *partial, INT32 skips);
// insert at queu (at end of other command)
#define COM_BufAddText(s) COM_BufAddTextEx(s, 0)
void COM_BufAddTextEx(const char *btext, int flags);

View File

@ -870,9 +870,14 @@ boolean CON_Responder(event_t *ev)
// sequential completions a la 4dos
static char completion[80];
static INT32 comskips, varskips;
const char *cmd = "";
static INT32 skips;
static INT32 com_skips;
static INT32 var_skips;
static INT32 alias_skips;
const char *cmd = NULL;
INT32 key;
if (chat_on)
@ -1007,7 +1012,6 @@ boolean CON_Responder(event_t *ev)
if (!input_len || input_len >= 40 || strchr(inputlines[inputline], ' '))
return true;
strcpy(completion, inputlines[inputline]);
comskips = varskips = 0;
}
len = strlen(completion);
@ -1023,6 +1027,14 @@ boolean CON_Responder(event_t *ev)
CONS_Printf(" \x83" "%s" "\x80" "%s\n", completion, cmd+len);
if (i == 0) CONS_Printf(" (none)\n");
//and finally aliases
CONS_Printf("Aliases:\n");
for (i = 0, cmd = COM_CompleteAlias(completion, i); cmd; cmd = COM_CompleteAlias(completion, ++i))
CONS_Printf(" \x83" "%s" "\x80" "%s\n", completion, cmd+len);
if (i == 0) CONS_Printf(" (none)\n");
completion[0] = 0;
return true;
}
// ---
@ -1091,43 +1103,64 @@ boolean CON_Responder(event_t *ev)
if (!input_len || input_len >= 40 || strchr(inputlines[inputline], ' '))
return true;
strcpy(completion, inputlines[inputline]);
comskips = varskips = 0;
skips = 0;
com_skips = 0;
var_skips = 0;
alias_skips = 0;
}
else
{
if (shiftdown)
{
if (comskips < 0)
{
if (--varskips < 0)
comskips = -comskips - 2;
}
else if (comskips > 0) comskips--;
if (skips > 0)
skips--;
}
else
{
if (comskips < 0) varskips++;
else comskips++;
skips++;
}
}
if (comskips >= 0)
if (skips <= com_skips)
{
cmd = COM_CompleteCommand(completion, comskips);
if (!cmd) // dirty: make sure if comskips is zero, to have a neg value
comskips = -comskips - 1;
cmd = COM_CompleteCommand(completion, skips);
if (cmd && skips == com_skips)
{
com_skips ++;
var_skips ++;
alias_skips++;
}
}
if (!cmd && skips <= var_skips)
{
cmd = CV_CompleteVar(completion, skips - com_skips);
if (cmd && skips == var_skips)
{
var_skips ++;
alias_skips++;
}
}
if (!cmd && skips <= alias_skips)
{
cmd = COM_CompleteAlias(completion, skips - var_skips);
if (cmd && skips == alias_skips)
{
alias_skips++;
}
}
if (comskips < 0)
cmd = CV_CompleteVar(completion, varskips);
if (cmd)
{
CON_InputSetString(va("%s ", cmd));
}
else
{
if (comskips > 0)
comskips--;
else if (varskips > 0)
varskips--;
skips--;
}
return true;