Autocomplete aliases in console

This commit is contained in:
James R 2020-08-14 00:00:16 -07:00
parent 3e02f5d0cb
commit aa19bce4a9
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 partial The partial name of the command (potentially).
* \param skips Number of commands to skip. * \param skips Number of commands to skip.
* \return The complete command name, or NULL. * \return The complete command name, or NULL.
* \sa CV_CompleteVar * \sa CV_CompleteAlias, CV_CompleteVar
*/ */
const char *COM_CompleteCommand(const char *partial, INT32 skips) const char *COM_CompleteCommand(const char *partial, INT32 skips)
{ {
@ -581,6 +581,32 @@ const char *COM_CompleteCommand(const char *partial, INT32 skips)
return NULL; 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. /** 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. * 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 partial The partial name of the variable (potentially).
* \param skips Number of variables to skip. * \param skips Number of variables to skip.
* \return The complete variable name, or NULL. * \return The complete variable name, or NULL.
* \sa COM_CompleteCommand * \sa COM_CompleteCommand, CV_CompleteAlias
*/ */
const char *CV_CompleteVar(char *partial, INT32 skips) const char *CV_CompleteVar(char *partial, INT32 skips)
{ {

View File

@ -49,6 +49,8 @@ size_t COM_FirstOption(void);
// match existing command or NULL // match existing command or NULL
const char *COM_CompleteCommand(const char *partial, INT32 skips); 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) // insert at queu (at end of other command)
#define COM_BufAddText(s) COM_BufAddTextEx(s, 0) #define COM_BufAddText(s) COM_BufAddTextEx(s, 0)
void COM_BufAddTextEx(const char *btext, int flags); 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 // sequential completions a la 4dos
static char completion[80]; 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; INT32 key;
if (chat_on) if (chat_on)
@ -1007,7 +1012,6 @@ boolean CON_Responder(event_t *ev)
if (!input_len || input_len >= 40 || strchr(inputlines[inputline], ' ')) if (!input_len || input_len >= 40 || strchr(inputlines[inputline], ' '))
return true; return true;
strcpy(completion, inputlines[inputline]); strcpy(completion, inputlines[inputline]);
comskips = varskips = 0;
} }
len = strlen(completion); len = strlen(completion);
@ -1023,6 +1027,14 @@ boolean CON_Responder(event_t *ev)
CONS_Printf(" \x83" "%s" "\x80" "%s\n", completion, cmd+len); CONS_Printf(" \x83" "%s" "\x80" "%s\n", completion, cmd+len);
if (i == 0) CONS_Printf(" (none)\n"); 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; return true;
} }
// --- // ---
@ -1091,43 +1103,64 @@ boolean CON_Responder(event_t *ev)
if (!input_len || input_len >= 40 || strchr(inputlines[inputline], ' ')) if (!input_len || input_len >= 40 || strchr(inputlines[inputline], ' '))
return true; return true;
strcpy(completion, inputlines[inputline]); strcpy(completion, inputlines[inputline]);
comskips = varskips = 0; skips = 0;
com_skips = 0;
var_skips = 0;
alias_skips = 0;
} }
else else
{ {
if (shiftdown) if (shiftdown)
{ {
if (comskips < 0) if (skips > 0)
{ skips--;
if (--varskips < 0)
comskips = -comskips - 2;
}
else if (comskips > 0) comskips--;
} }
else else
{ {
if (comskips < 0) varskips++; skips++;
else comskips++;
} }
} }
if (comskips >= 0) if (skips <= com_skips)
{ {
cmd = COM_CompleteCommand(completion, comskips); cmd = COM_CompleteCommand(completion, skips);
if (!cmd) // dirty: make sure if comskips is zero, to have a neg value
comskips = -comskips - 1; 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) if (cmd)
{
CON_InputSetString(va("%s ", cmd)); CON_InputSetString(va("%s ", cmd));
}
else else
{ {
if (comskips > 0) skips--;
comskips--;
else if (varskips > 0)
varskips--;
} }
return true; return true;