lib/widget/input_complete.c: apply coding guidelines.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2016-04-29 10:45:18 +03:00
parent 1bfe137d2a
commit abbfde35b2

View File

@ -118,13 +118,13 @@ show_c_flags (input_complete_t flags)
{ {
static char s_cf[] = "FHCVUDS"; static char s_cf[] = "FHCVUDS";
s_cf[0] = (flags & INPUT_COMPLETE_FILENAMES) ? 'F' : ' '; s_cf[0] = (flags & INPUT_COMPLETE_FILENAMES) != 0 ? 'F' : ' ';
s_cf[1] = (flags & INPUT_COMPLETE_HOSTNAMES) ? 'H' : ' '; s_cf[1] = (flags & INPUT_COMPLETE_HOSTNAMES) != 0 ? 'H' : ' ';
s_cf[2] = (flags & INPUT_COMPLETE_COMMANDS) ? 'C' : ' '; s_cf[2] = (flags & INPUT_COMPLETE_COMMANDS) != 0 ? 'C' : ' ';
s_cf[3] = (flags & INPUT_COMPLETE_VARIABLES) ? 'V' : ' '; s_cf[3] = (flags & INPUT_COMPLETE_VARIABLES) != 0 ? 'V' : ' ';
s_cf[4] = (flags & INPUT_COMPLETE_USERNAMES) ? 'U' : ' '; s_cf[4] = (flags & INPUT_COMPLETE_USERNAMES) != 0 ? 'U' : ' ';
s_cf[5] = (flags & INPUT_COMPLETE_CD) ? 'D' : ' '; s_cf[5] = (flags & INPUT_COMPLETE_CD) != 0 ? 'D' : ' ';
s_cf[6] = (flags & INPUT_COMPLETE_SHELL_ESC) ? 'S' : ' '; s_cf[6] = (flags & INPUT_COMPLETE_SHELL_ESC) != 0 ? 'S' : ' ';
return s_cf; return s_cf;
} }
@ -139,15 +139,15 @@ filename_completion_function (const char *text, int state, input_complete_t flag
static char *filename = NULL; static char *filename = NULL;
static char *dirname = NULL; static char *dirname = NULL;
static char *users_dirname = NULL; static char *users_dirname = NULL;
static size_t filename_len; static size_t filename_len = 0;
int isdir = 1, isexec = 0;
static vfs_path_t *dirname_vpath = NULL; static vfs_path_t *dirname_vpath = NULL;
gboolean isdir = TRUE, isexec = FALSE;
struct dirent *entry = NULL; struct dirent *entry = NULL;
SHOW_C_CTX ("filename_completion_function"); SHOW_C_CTX ("filename_completion_function");
if (text && (flags & INPUT_COMPLETE_SHELL_ESC)) if (text != NULL && (flags & INPUT_COMPLETE_SHELL_ESC) != 0)
{ {
char *u_text; char *u_text;
char *result; char *result;
@ -203,7 +203,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag
/* Now that we have some state, we can read the directory. */ /* Now that we have some state, we can read the directory. */
while (directory && (entry = mc_readdir (directory))) while (directory != NULL && (entry = mc_readdir (directory)) != NULL)
{ {
if (!str_is_valid_string (entry->d_name)) if (!str_is_valid_string (entry->d_name))
continue; continue;
@ -221,11 +221,13 @@ filename_completion_function (const char *text, int state, input_complete_t flag
it may be a match. */ it may be a match. */
if ((entry->d_name[0] != filename[0]) || if ((entry->d_name[0] != filename[0]) ||
((NLENGTH (entry)) < filename_len) || ((NLENGTH (entry)) < filename_len) ||
strncmp (filename, entry->d_name, filename_len)) strncmp (filename, entry->d_name, filename_len) != 0)
continue; continue;
} }
isdir = 1;
isexec = 0; isdir = TRUE;
isexec = FALSE;
{ {
struct stat tempstat; struct stat tempstat;
vfs_path_t *tmp_vpath; vfs_path_t *tmp_vpath;
@ -235,37 +237,42 @@ filename_completion_function (const char *text, int state, input_complete_t flag
/* Unix version */ /* Unix version */
if (mc_stat (tmp_vpath, &tempstat) == 0) if (mc_stat (tmp_vpath, &tempstat) == 0)
{ {
uid_t my_uid = getuid (); uid_t my_uid;
gid_t my_gid = getgid (); gid_t my_gid;
my_uid = getuid ();
my_gid = getgid ();
if (!S_ISDIR (tempstat.st_mode)) if (!S_ISDIR (tempstat.st_mode))
{ {
isdir = 0; isdir = FALSE;
if ((!my_uid && (tempstat.st_mode & 0111)) ||
(my_uid == tempstat.st_uid && (tempstat.st_mode & 0100)) || if ((my_uid == 0 && (tempstat.st_mode & 0111) != 0) ||
(my_gid == tempstat.st_gid && (tempstat.st_mode & 0010)) || (my_uid == tempstat.st_uid && (tempstat.st_mode & 0100) != 0) ||
(tempstat.st_mode & 0001)) (my_gid == tempstat.st_gid && (tempstat.st_mode & 0010) != 0) ||
isexec = 1; (tempstat.st_mode & 0001) != 0)
isexec = TRUE;
} }
} }
else else
{ {
/* stat failed, strange. not a dir in any case */ /* stat failed, strange. not a dir in any case */
isdir = 0; isdir = FALSE;
} }
vfs_path_free (tmp_vpath); vfs_path_free (tmp_vpath);
} }
if ((flags & INPUT_COMPLETE_COMMANDS) && (isexec || isdir))
if ((flags & INPUT_COMPLETE_COMMANDS) != 0 && (isexec || isdir))
break; break;
if ((flags & INPUT_COMPLETE_CD) && isdir) if ((flags & INPUT_COMPLETE_CD) != 0 && isdir)
break; break;
if (flags & (INPUT_COMPLETE_FILENAMES)) if ((flags & INPUT_COMPLETE_FILENAMES) != 0)
break; break;
} }
if (entry == NULL) if (entry == NULL)
{ {
if (directory) if (directory != NULL)
{ {
mc_closedir (directory); mc_closedir (directory);
directory = NULL; directory = NULL;
@ -306,8 +313,8 @@ filename_completion_function (const char *text, int state, input_complete_t flag
static char * static char *
username_completion_function (const char *text, int state, input_complete_t flags) username_completion_function (const char *text, int state, input_complete_t flags)
{ {
static struct passwd *entry; static struct passwd *entry = NULL;
static size_t userlen; static size_t userlen = 0;
(void) flags; (void) flags;
SHOW_C_CTX ("username_completion_function"); SHOW_C_CTX ("username_completion_function");
@ -319,12 +326,13 @@ username_completion_function (const char *text, int state, input_complete_t flag
setpwent (); setpwent ();
userlen = strlen (text + 1); userlen = strlen (text + 1);
} }
while ((entry = getpwent ()) != NULL) while ((entry = getpwent ()) != NULL)
{ {
/* Null usernames should result in all users as possible completions. */ /* Null usernames should result in all users as possible completions. */
if (userlen == 0) if (userlen == 0)
break; break;
if (text[1] == entry->pw_name[0] && !strncmp (text + 1, entry->pw_name, userlen)) if (text[1] == entry->pw_name[0] && strncmp (text + 1, entry->pw_name, userlen) == 0)
break; break;
} }
@ -342,25 +350,26 @@ username_completion_function (const char *text, int state, input_complete_t flag
static char * static char *
variable_completion_function (const char *text, int state, input_complete_t flags) variable_completion_function (const char *text, int state, input_complete_t flags)
{ {
static char **env_p; static char **env_p = NULL;
static unsigned int isbrace; static gboolean isbrace = FALSE;
static size_t varlen; static size_t varlen = 0;
const char *p = NULL; const char *p;
(void) flags; (void) flags;
SHOW_C_CTX ("variable_completion_function"); SHOW_C_CTX ("variable_completion_function");
if (state == 0) if (state == 0)
{ /* Initialization stuff */ { /* Initialization stuff */
isbrace = (text[1] == '{') ? 1 : 0; isbrace = (text[1] == '{');
varlen = strlen (text + 1 + isbrace); varlen = strlen (text + 1 + isbrace);
env_p = environ; env_p = environ;
} }
while (*env_p) while (*env_p != NULL)
{ {
p = strchr (*env_p, '='); p = strchr (*env_p, '=');
if (p && ((size_t) (p - *env_p) >= varlen) && !strncmp (text + 1 + isbrace, *env_p, varlen)) if (p != NULL && ((size_t) (p - *env_p) >= varlen)
&& strncmp (text + 1 + isbrace, *env_p, varlen) == 0)
break; break;
env_p++; env_p++;
} }
@ -373,7 +382,7 @@ variable_completion_function (const char *text, int state, input_complete_t flag
temp = g_string_new_len (*env_p, p - *env_p); temp = g_string_new_len (*env_p, p - *env_p);
if (isbrace != 0) if (isbrace)
{ {
g_string_prepend_c (temp, '{'); g_string_prepend_c (temp, '{');
g_string_append_c (temp, '}'); g_string_append_c (temp, '}');
@ -391,30 +400,34 @@ variable_completion_function (const char *text, int state, input_complete_t flag
static void static void
fetch_hosts (const char *filename) fetch_hosts (const char *filename)
{ {
FILE *file = fopen (filename, "r"); FILE *file;
char buffer[256], *name; char buffer[256];
char *name;
char *lc_start; char *lc_start;
char *bi; char *bi;
if (!file) file = fopen (filename, "r");
if (file == NULL)
return; return;
while (fgets (buffer, sizeof (buffer) - 1, file) != NULL) while (fgets (buffer, sizeof (buffer) - 1, file) != NULL)
{ {
/* Skip to first character. */ /* Skip to first character. */
for (bi = buffer; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi)); for (bi = buffer; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi))
;
/* Ignore comments... */ /* Ignore comments... */
if (bi[0] == '#') if (bi[0] == '#')
continue; continue;
/* Handle $include. */ /* Handle $include. */
if (!strncmp (bi, "$include ", 9)) if (strncmp (bi, "$include ", 9) == 0)
{ {
char *includefile = bi + 9; char *includefile, *t;
char *t;
/* Find start of filename. */ /* Find start of filename. */
while (*includefile && whitespace (*includefile)) includefile = bi + 9;
while (*includefile != '\0' && whitespace (*includefile))
includefile++; includefile++;
t = includefile; t = includefile;
@ -438,27 +451,31 @@ fetch_hosts (const char *filename)
str_next_char (&bi); str_next_char (&bi);
if (bi[0] == '#') if (bi[0] == '#')
continue; continue;
for (lc_start = bi; bi[0] != '\0' && !str_isspace (bi); str_next_char (&bi)); for (lc_start = bi; bi[0] != '\0' && !str_isspace (bi); str_next_char (&bi))
;
if (bi - lc_start == 0) if (bi == lc_start)
continue; continue;
name = g_strndup (lc_start, bi - lc_start); name = g_strndup (lc_start, bi - lc_start);
{ {
char **host_p; char **host_p;
int j;
if (hosts_p - hosts >= hosts_alloclen) j = hosts_p - hosts;
if (j >= hosts_alloclen)
{ {
int j;
j = hosts_p - hosts;
hosts_alloclen += 30; hosts_alloclen += 30;
hosts = g_renew (char *, hosts, hosts_alloclen + 1); hosts = g_renew (char *, hosts, hosts_alloclen + 1);
hosts_p = hosts + j; hosts_p = hosts + j;
} }
for (host_p = hosts; host_p < hosts_p; host_p++) for (host_p = hosts; host_p < hosts_p; host_p++)
if (!strcmp (name, *host_p)) if (strcmp (name, *host_p) == 0)
break; /* We do not want any duplicates */ break; /* We do not want any duplicates */
if (host_p == hosts_p) if (host_p == hosts_p)
{ {
*(hosts_p++) = name; *(hosts_p++) = name;
@ -469,6 +486,7 @@ fetch_hosts (const char *filename)
} }
} }
} }
fclose (file); fclose (file);
} }
@ -477,9 +495,9 @@ fetch_hosts (const char *filename)
static char * static char *
hostname_completion_function (const char *text, int state, input_complete_t flags) hostname_completion_function (const char *text, int state, input_complete_t flags)
{ {
static char **host_p; static char **host_p = NULL;
static unsigned int textstart; static size_t textstart = 0;
static size_t textlen; static size_t textlen = 0;
(void) flags; (void) flags;
SHOW_C_CTX ("hostname_completion_function"); SHOW_C_CTX ("hostname_completion_function");
@ -540,16 +558,15 @@ hostname_completion_function (const char *text, int state, input_complete_t flag
static char * static char *
command_completion_function (const char *text, int state, input_complete_t flags) command_completion_function (const char *text, int state, input_complete_t flags)
{ {
char *u_text; static const char *path_end = NULL;
static const char *path_end; static gboolean isabsolute = FALSE;
static gboolean isabsolute; static int phase = 0;
static int phase; static size_t text_len = 0;
static size_t text_len; static const char *const *words = NULL;
static const char *const *words; static char *path = NULL;
static char *path; static char *cur_path = NULL;
static char *cur_path; static char *cur_word = NULL;
static char *cur_word; static int init_state = 0;
static int init_state;
static const char *const bash_reserved[] = { static const char *const bash_reserved[] = {
"if", "then", "else", "elif", "fi", "case", "esac", "for", "if", "then", "else", "elif", "fi", "case", "esac", "for",
"select", "while", "until", "do", "done", "in", "function", 0 "select", "while", "until", "do", "done", "in", "function", 0
@ -563,12 +580,14 @@ command_completion_function (const char *text, int state, input_complete_t flags
"shift", "source", "suspend", "test", "times", "trap", "type", "shift", "source", "suspend", "test", "times", "trap", "type",
"typeset", "ulimit", "umask", "unalias", "unset", "wait", 0 "typeset", "ulimit", "umask", "unalias", "unset", "wait", 0
}; };
char *u_text;
char *p, *found; char *p, *found;
SHOW_C_CTX ("command_completion_function"); SHOW_C_CTX ("command_completion_function");
if (!(flags & INPUT_COMPLETE_COMMANDS)) if ((flags & INPUT_COMPLETE_COMMANDS) == 0)
return 0; return NULL;
u_text = strutils_shell_unescape (text); u_text = strutils_shell_unescape (text);
flags &= ~INPUT_COMPLETE_SHELL_ESC; flags &= ~INPUT_COMPLETE_SHELL_ESC;
@ -590,9 +609,7 @@ command_completion_function (const char *text, int state, input_complete_t flags
p = path; p = path;
path_end = strchr (p, '\0'); path_end = strchr (p, '\0');
while ((p = strchr (p, PATH_ENV_SEP)) != NULL) while ((p = strchr (p, PATH_ENV_SEP)) != NULL)
{
*p++ = '\0'; *p++ = '\0';
}
} }
} }
} }
@ -626,6 +643,7 @@ command_completion_function (const char *text, int state, input_complete_t flags
} }
phase++; phase++;
words = bash_builtins; words = bash_builtins;
/* fallthrough */
case 1: /* Builtin commands */ case 1: /* Builtin commands */
for (; *words != NULL; words++) for (; *words != NULL; words++)
if (strncmp (*words, u_text, text_len) == 0) if (strncmp (*words, u_text, text_len) == 0)
@ -634,30 +652,32 @@ command_completion_function (const char *text, int state, input_complete_t flags
return g_strdup (*(words++)); return g_strdup (*(words++));
} }
phase++; phase++;
if (!path) if (path == NULL)
break; break;
cur_path = path; cur_path = path;
cur_word = NULL; cur_word = NULL;
/* fallthrough */
case 2: /* And looking through the $PATH */ case 2: /* And looking through the $PATH */
while (!found) while (found == NULL)
{ {
if (!cur_word) if (cur_word == NULL)
{ {
char *expanded; char *expanded;
if (cur_path >= path_end) if (cur_path >= path_end)
break; break;
expanded = tilde_expand (*cur_path ? cur_path : "."); expanded = tilde_expand (*cur_path != '\0' ? cur_path : ".");
cur_word = mc_build_filename (expanded, u_text, (char *) NULL); cur_word = mc_build_filename (expanded, u_text, (char *) NULL);
g_free (expanded); g_free (expanded);
canonicalize_pathname (cur_word); canonicalize_pathname (cur_word);
cur_path = strchr (cur_path, 0) + 1; cur_path = strchr (cur_path, '\0') + 1;
init_state = state; init_state = state;
} }
found = filename_completion_function (cur_word, state - init_state, flags); found = filename_completion_function (cur_word, state - init_state, flags);
if (!found) if (found == NULL)
MC_PTR_FREE (cur_word); MC_PTR_FREE (cur_word);
} }
/* fallthrough */
default: default:
break; break;
} }
@ -670,6 +690,7 @@ command_completion_function (const char *text, int state, input_complete_t flags
if (p != NULL) if (p != NULL)
{ {
char *tmp = found; char *tmp = found;
found = strutils_shell_escape (p + 1); found = strutils_shell_escape (p + 1);
g_free (tmp); g_free (tmp);
} }
@ -725,9 +746,10 @@ completion_matches (const char *text, CompletionFunction entry_function, input_c
/* If there were any matches, then look through them finding out the /* If there were any matches, then look through them finding out the
lowest common denominator. That then becomes match_list[0]. */ lowest common denominator. That then becomes match_list[0]. */
if (matches) if (matches == 0)
MC_PTR_FREE (match_list); /* There were no matches. */
else
{ {
/* If only one match, just use that. */ /* If only one match, just use that. */
if (matches == 1) if (matches == 1)
{ {
@ -752,7 +774,7 @@ completion_matches (const char *text, CompletionFunction entry_function, input_c
char *si, *sj; char *si, *sj;
char *ni, *nj; char *ni, *nj;
for (si = match_list[i], sj = match_list[j]; si[0] && sj[0];) for (si = match_list[i], sj = match_list[j]; si[0] != '\0' && sj[0] != '\0';)
{ {
ni = str_get_next_char (si); ni = str_get_next_char (si);
@ -787,8 +809,6 @@ completion_matches (const char *text, CompletionFunction entry_function, input_c
match_list[0] = g_strndup (match_list[1], low); match_list[0] = g_strndup (match_list[1], low);
} }
} }
else /* There were no matches. */
MC_PTR_FREE (match_list);
return match_list; return match_list;
} }
@ -860,9 +880,9 @@ try_complete_commands_prepare (try_complete_automation_state_t * state, char *te
static void static void
try_complete_find_start_sign (try_complete_automation_state_t * state) try_complete_find_start_sign (try_complete_automation_state_t * state)
{ {
if (state->flags & INPUT_COMPLETE_COMMANDS) if ((state->flags & INPUT_COMPLETE_COMMANDS) != 0)
state->p = strrchr (state->word, '`'); state->p = strrchr (state->word, '`');
if (state->flags & (INPUT_COMPLETE_COMMANDS | INPUT_COMPLETE_VARIABLES)) if ((state->flags & (INPUT_COMPLETE_COMMANDS | INPUT_COMPLETE_VARIABLES)) != 0)
{ {
state->q = strrchr (state->word, '$'); state->q = strrchr (state->word, '$');
@ -879,9 +899,9 @@ try_complete_find_start_sign (try_complete_automation_state_t * state)
state->q = NULL; state->q = NULL;
} }
} }
if (state->flags & INPUT_COMPLETE_HOSTNAMES) if ((state->flags & INPUT_COMPLETE_HOSTNAMES) != 0)
state->r = strrchr (state->word, '@'); state->r = strrchr (state->word, '@');
if (state->q && state->q[1] == '(' && (state->flags & INPUT_COMPLETE_COMMANDS)) if (state->q != NULL && state->q[1] == '(' && (state->flags & INPUT_COMPLETE_COMMANDS) != 0)
{ {
if (state->q > state->p) if (state->q > state->p)
state->p = str_get_next_char (state->q); state->p = str_get_next_char (state->q);
@ -914,23 +934,26 @@ try_complete_all_possible (try_complete_automation_state_t * state, char *text,
{ {
state->q = text + *lc_start; state->q = text + *lc_start;
for (state->p = text; for (state->p = text;
*state->p && state->p < state->q && (*state->p == ' ' || *state->p == '\t'); *state->p != '\0' && state->p < state->q && (*state->p == ' '
|| *state->p == '\t');
str_next_char (&state->p)) str_next_char (&state->p))
; ;
if (!strncmp (state->p, "cd", 2)) if (strncmp (state->p, "cd", 2) == 0)
for (state->p += 2; for (state->p += 2;
*state->p && state->p < state->q && (*state->p == ' ' || *state->p == '\t'); *state->p != '\0' && state->p < state->q && (*state->p == ' '
|| *state->p == '\t');
str_next_char (&state->p)) str_next_char (&state->p))
; ;
if (state->p == state->q) if (state->p == state->q)
{ {
char *const cdpath_ref = g_strdup (getenv ("CDPATH")); char *cdpath_ref, *cdpath;
char *cdpath = cdpath_ref;
char c; char c;
cdpath_ref = g_strdup (getenv ("CDPATH"));
cdpath = cdpath_ref;
c = (cdpath == NULL) ? '\0' : ':'; c = (cdpath == NULL) ? '\0' : ':';
while (!matches && c == ':') while (matches == NULL && c == ':')
{ {
char *s; char *s;
@ -964,10 +987,12 @@ try_complete_all_possible (try_complete_automation_state_t * state, char *text,
static gboolean static gboolean
insert_text (WInput * in, char *text, ssize_t size) insert_text (WInput * in, char *text, ssize_t size)
{ {
size_t text_len;
int buff_len; int buff_len;
text_len = strlen (text);
buff_len = str_length (in->buffer); buff_len = str_length (in->buffer);
size = MIN (size, (ssize_t) strlen (text)) + start - end; size = MIN (size, (ssize_t) text_len) + start - end;
if (strlen (in->buffer) + size >= (size_t) in->current_max_size) if (strlen (in->buffer) + size >= (size_t) in->current_max_size)
{ {
/* Expand the buffer */ /* Expand the buffer */
@ -990,6 +1015,7 @@ insert_text (WInput * in, char *text, ssize_t size)
input_update (in, TRUE); input_update (in, TRUE);
end += size; end += size;
} }
return size != 0; return size != 0;
} }
@ -1080,6 +1106,7 @@ query_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *d
buff[bl++] = (char) parm; buff[bl++] = (char) parm;
buff[bl] = '\0'; buff[bl] = '\0';
switch (str_is_valid_char (buff, bl)) switch (str_is_valid_char (buff, bl))
{ {
case -1: case -1:
@ -1172,8 +1199,8 @@ query_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *d
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** Returns 1 if the user would like to see us again */ /** Returns TRUE if the user would like to see us again */
static int static gboolean
complete_engine (WInput * in, int what_to_do) complete_engine (WInput * in, int what_to_do)
{ {
if (in->completions != NULL && str_offset_to_pos (in->buffer, in->point) != end) if (in->completions != NULL && str_offset_to_pos (in->buffer, in->point) != end)
@ -1182,24 +1209,24 @@ complete_engine (WInput * in, int what_to_do)
if (in->completions == NULL) if (in->completions == NULL)
complete_engine_fill_completions (in); complete_engine_fill_completions (in);
if (in->completions != NULL) if (in->completions == NULL)
tty_beep ();
else
{ {
if (what_to_do & DO_INSERTION || ((what_to_do & DO_QUERY) && !in->completions[1])) if ((what_to_do & DO_INSERTION) != 0
|| ((what_to_do & DO_QUERY) != 0 && in->completions[1] == NULL))
{ {
char *lc_complete = in->completions[0]; char *lc_complete = in->completions[0];
if (insert_text (in, lc_complete, strlen (lc_complete)))
{ if (!insert_text (in, lc_complete, strlen (lc_complete)) || in->completions[1] != NULL)
if (in->completions[1])
tty_beep ();
else
input_free_completions (in);
}
else
tty_beep (); tty_beep ();
else
input_free_completions (in);
} }
if ((what_to_do & DO_QUERY) && in->completions && in->completions[1])
if ((what_to_do & DO_QUERY) != 0 && in->completions != NULL && in->completions[1] != NULL)
{ {
int maxlen = 0, i, count = 0; int maxlen = 0, count = 0, i;
int x, y, w, h; int x, y, w, h;
int start_x, start_y; int start_x, start_y;
char **p, *q; char **p, *q;
@ -1212,6 +1239,7 @@ complete_engine (WInput * in, int what_to_do)
if (i > maxlen) if (i > maxlen)
maxlen = i; maxlen = i;
} }
start_x = WIDGET (in)->x; start_x = WIDGET (in)->x;
start_y = WIDGET (in)->y; start_y = WIDGET (in)->y;
if (start_y - 2 >= count) if (start_y - 2 >= count)
@ -1219,18 +1247,15 @@ complete_engine (WInput * in, int what_to_do)
y = start_y - 2 - count; y = start_y - 2 - count;
h = 2 + count; h = 2 + count;
} }
else if (start_y >= LINES - start_y - 1)
{
y = 0;
h = start_y;
}
else else
{ {
if (start_y >= LINES - start_y - 1) y = start_y + 1;
{ h = LINES - start_y - 1;
y = 0;
h = start_y;
}
else
{
y = start_y + 1;
h = LINES - start_y - 1;
}
} }
x = start - in->term_first_shown - 2 + start_x; x = start - in->term_first_shown - 2 + start_x;
w = maxlen + 4; w = maxlen + 4;
@ -1240,36 +1265,39 @@ complete_engine (WInput * in, int what_to_do)
x = 0; x = 0;
if (x + w > COLS) if (x + w > COLS)
w = COLS; w = COLS;
input = in; input = in;
min_end = end; min_end = end;
query_height = h; query_height = h;
query_width = w; query_width = w;
query_dlg = dlg_create (TRUE, y, x, query_height, query_width, query_dlg = dlg_create (TRUE, y, x, query_height, query_width,
dialog_colors, query_callback, NULL, dialog_colors, query_callback, NULL,
"[Completion]", NULL, DLG_COMPACT); "[Completion]", NULL, DLG_COMPACT);
query_list = listbox_new (1, 1, h - 2, w - 2, FALSE, NULL); query_list = listbox_new (1, 1, h - 2, w - 2, FALSE, NULL);
add_widget (query_dlg, query_list); add_widget (query_dlg, query_list);
for (p = in->completions + 1; *p; p++)
for (p = in->completions + 1; *p != NULL; p++)
listbox_add_item (query_list, LISTBOX_APPEND_AT_END, 0, *p, NULL, FALSE); listbox_add_item (query_list, LISTBOX_APPEND_AT_END, 0, *p, NULL, FALSE);
dlg_run (query_dlg);
i = dlg_run (query_dlg);
q = NULL; q = NULL;
if (query_dlg->ret_value == B_ENTER) if (i == B_ENTER)
{ {
listbox_get_current (query_list, &q, NULL); listbox_get_current (query_list, &q, NULL);
if (q) if (q != NULL)
insert_text (in, q, strlen (q)); insert_text (in, q, strlen (q));
} }
if (q || end != min_end) if (q != NULL || end != min_end)
input_free_completions (in); input_free_completions (in);
i = query_dlg->ret_value; /* B_USER if user wants to start over again */
dlg_destroy (query_dlg); dlg_destroy (query_dlg);
if (i == B_USER)
return 1; /* B_USER if user wants to start over again */
return (i == B_USER);
} }
} }
else
tty_beep (); return FALSE;
return 0;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -1295,7 +1323,7 @@ try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
the start of the line (ignoring preceding whitespace), or if it the start of the line (ignoring preceding whitespace), or if it
appears after a character that separates commands. And we have to appears after a character that separates commands. And we have to
be in a INPUT_COMPLETE_COMMANDS flagged Input line. */ be in a INPUT_COMPLETE_COMMANDS flagged Input line. */
if (!state.is_cd && (flags & INPUT_COMPLETE_COMMANDS)) if (!state.is_cd && (flags & INPUT_COMPLETE_COMMANDS) != 0)
try_complete_commands_prepare (&state, text, lc_start); try_complete_commands_prepare (&state, text, lc_start);
try_complete_find_start_sign (&state); try_complete_find_start_sign (&state);
@ -1307,7 +1335,7 @@ try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
matches = completion_matches (str_cget_next_char (state.p), matches = completion_matches (str_cget_next_char (state.p),
command_completion_function, command_completion_function,
state.flags & (~INPUT_COMPLETE_FILENAMES)); state.flags & (~INPUT_COMPLETE_FILENAMES));
if (matches) if (matches != NULL)
*lc_start += str_get_next_char (state.p) - state.word; *lc_start += str_get_next_char (state.p) - state.word;
} }
@ -1316,7 +1344,7 @@ try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
{ {
SHOW_C_CTX ("try_complete:var_subst"); SHOW_C_CTX ("try_complete:var_subst");
matches = completion_matches (state.q, variable_completion_function, state.flags); matches = completion_matches (state.q, variable_completion_function, state.flags);
if (matches) if (matches != NULL)
*lc_start += state.q - state.word; *lc_start += state.q - state.word;
} }
@ -1326,14 +1354,14 @@ try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
{ {
SHOW_C_CTX ("try_complete:host_subst"); SHOW_C_CTX ("try_complete:host_subst");
matches = completion_matches (state.r, hostname_completion_function, state.flags); matches = completion_matches (state.r, hostname_completion_function, state.flags);
if (matches) if (matches != NULL)
*lc_start += state.r - state.word; *lc_start += state.r - state.word;
} }
/* Starts with '~' and there is no slash in the word, then /* Starts with '~' and there is no slash in the word, then
try completing this word as a username. */ try completing this word as a username. */
if (!matches && *state.word == '~' && (state.flags & INPUT_COMPLETE_USERNAMES) if (matches == NULL && *state.word == '~' && (state.flags & INPUT_COMPLETE_USERNAMES) != 0
&& !strchr (state.word, PATH_SEP)) && strchr (state.word, PATH_SEP) == NULL)
{ {
SHOW_C_CTX ("try_complete:user_subst"); SHOW_C_CTX ("try_complete:user_subst");
matches = completion_matches (state.word, username_completion_function, state.flags); matches = completion_matches (state.word, username_completion_function, state.flags);