[awesome-menu] Complete file completion

This commit is contained in:
Julien Danjou 2008-03-08 16:49:33 +01:00
parent b5ad123e60
commit f3c632aebf
1 changed files with 42 additions and 21 deletions

View File

@ -180,46 +180,53 @@ config_parse(const char *confpatharg)
static void static void
item_list_fill_file(void) item_list_fill_file(void)
{ {
char cwd[PATH_MAX]; char cwd[PATH_MAX], *fcwd;
DIR *dir; DIR *dir;
struct dirent *dirinfo; struct dirent *dirinfo;
item_t *item; item_t *item;
ssize_t len;
item_list_wipe(&globalconf.items); item_list_wipe(&globalconf.items);
if(!getcwd(cwd, sizeof(cwd))) if(a_strlen(globalconf.text)
{ && (fcwd = strrchr(globalconf.text, ' ')))
warn("cannot get current directory "); a_strcpy(cwd, sizeof(cwd), ++fcwd);
perror(NULL); else
return; a_strcpy(cwd, sizeof(cwd), ".");
}
if(!(dir = opendir(cwd))) if(!(dir = opendir(cwd)))
{
warn("unable to open current directory ");
perror(NULL);
return; return;
}
while((dirinfo = readdir(dir))) while((dirinfo = readdir(dir)))
{ {
item = p_new(item_t, 1); item = p_new(item_t, 1);
item->data = a_strdup(dirinfo->d_name); len = a_strlen(dirinfo->d_name) + a_strlen(cwd) + 1;
item->data = p_new(char, len);
if(a_strcmp(cwd, "."))
a_strcpy(item->data, len, cwd);
a_strcat(item->data, len, dirinfo->d_name);
item_list_push(&globalconf.items, item); item_list_push(&globalconf.items, item);
} }
closedir(dir); closedir(dir);
} }
static char *
get_last_word(char *text)
{
char *last_word = text;
if((last_word = strrchr(text, ' ')))
last_word++;
return last_word;
}
static void static void
complete(Bool reverse) complete(Bool reverse)
{ {
item_t *item = NULL; item_t *item = NULL;
item_t *(*item_iter)(item_t **, item_t *) = item_list_next; item_t *(*item_iter)(item_t **, item_t *) = item_list_next;
if(!globalconf.items)
item_list_fill_file();
if(reverse) if(reverse)
item_iter = item_list_prev; item_iter = item_list_prev;
@ -236,7 +243,8 @@ complete(Bool reverse)
for(; item; item = item_iter(&globalconf.items, item)) for(; item; item = item_iter(&globalconf.items, item))
if(item->match) if(item->match)
{ {
a_strcpy(globalconf.text, ssizeof(globalconf.text), item->data); /* XXX sizeof wrong */
a_strcpy(get_last_word(globalconf.text), ssizeof(globalconf.text), item->data);
globalconf.item_selected = item; globalconf.item_selected = item;
return; return;
} }
@ -245,17 +253,30 @@ complete(Bool reverse)
static void static void
compute_match(void) compute_match(void)
{ {
ssize_t len = a_strlen(globalconf.text);
item_t *item; item_t *item;
char *last_word;
/* reset the selected item to NULL */ /* reset the selected item to NULL */
globalconf.item_selected = NULL; globalconf.item_selected = NULL;
if(globalconf.text[len - 1] == '/')
item_list_fill_file();
last_word = get_last_word(globalconf.text);
if(a_strlen(last_word))
{
for(item = globalconf.items; item; item = item->next) for(item = globalconf.items; item; item = item->next)
if(!a_strncmp(globalconf.text, item->data, a_strlen(globalconf.text))) if(!a_strncmp(last_word, item->data, a_strlen(last_word)))
item->match = True; item->match = True;
else else
item->match = False; item->match = False;
} }
else
for(item = globalconf.items; item; item = item->next)
item->match = True;
}
/* Why not? */ /* Why not? */
#define MARGIN 10 #define MARGIN 10