Complete variable names in shell (only at start of argument)

This commit is contained in:
K. Lange 2018-08-01 09:35:46 +09:00
parent eed3926793
commit 2b8ad34533

View File

@ -227,6 +227,7 @@ void tab_complete_func(rline_context_t * c) {
#define COMPLETE_FILE 1
#define COMPLETE_COMMAND 2
#define COMPLETE_CUSTOM 3
#define COMPLETE_VARIABLE 4
int complete_mode = COMPLETE_FILE;
int command_adj = 0;
@ -248,6 +249,11 @@ void tab_complete_func(rline_context_t * c) {
complete_mode = COMPLETE_CUSTOM;
}
/* complete variable names */
if (*prefix == '$') {
complete_mode = COMPLETE_VARIABLE;
}
if (complete_mode == COMPLETE_COMMAND) {
/* Complete a command name */
@ -331,6 +337,25 @@ void tab_complete_func(rline_context_t * c) {
completions++;
}
} else if (complete_mode == COMPLETE_VARIABLE) {
char ** envvar = environ;
free_matches = 1;
while (*envvar) {
char * tmp = strdup(*envvar);
char * c = strchr(tmp, '=');
*c = '\0';
if (strstr(tmp, prefix+1) == tmp) {
char * m = malloc(strlen(tmp)+2);
sprintf(m, "$%s", tmp);
list_insert(matches, m);
match = m;
}
free(tmp);
envvar++;
}
}
if (matches->length == 1) {