Launcher: rearrange completion

This commit is contained in:
Philippe Pepiot 2009-10-13 20:11:14 +02:00
parent 3fa21c0d2e
commit 23848e8291

View File

@ -33,26 +33,21 @@
#include "wmfs.h" #include "wmfs.h"
#include <dirent.h> #include <dirent.h>
#define PATHMAX 4095 static char *complete_on_command(char*, size_t);
void void
launcher_execute(Launcher launcher) launcher_execute(Launcher launcher)
{ {
BarWindow *bw; BarWindow *bw;
Bool stop, found; Bool found;
Bool lastwastab = False; Bool lastwastab = False;
Bool my_guitar_gently_wheeps = True; Bool my_guitar_gently_wheeps = True;
char tmp[32] = { 0 }; char tmp[32] = { 0 };
char buf[512] = { 0 }; char buf[512] = { 0 };
char tabbuf[512] = { 0 }; char tmpbuf[512] = { 0 };
char *searchpath; char *complete;
char *start, *end;
char currentpath[PATHMAX];
DIR *dir;
struct dirent *dirent;
int pos = 0, x; int pos = 0, x;
int tabhits = 0; int tabhits = 0;
int searchhits = 0;
KeySym ks; KeySym ks;
XEvent ev; XEvent ev;
@ -107,60 +102,27 @@ launcher_execute(Launcher launcher)
my_guitar_gently_wheeps = 0; my_guitar_gently_wheeps = 0;
break; break;
case XK_Tab: case XK_Tab:
stop = found = False; buf[pos] = '\0';
searchpath = getenv("PATH");
start = searchpath;
if (lastwastab) if (lastwastab)
{
strcpy(buf, tabbuf);
tabhits++; tabhits++;
}
else else
tabhits = 1;
searchhits = 0;
do
{ {
end = strchr(start, ':'); tabhits = 1;
if (end == NULL) strcpy(tmpbuf, buf);
{ }
stop = True;
strncpy(currentpath, start, PATHMAX);
}
else
{
strncpy(currentpath, start, end - start);
currentpath[end - start] = '\0';
}
if (!stop)
start = end + 1;
dir = opendir(currentpath);
if (dir)
{
while ((dirent = readdir(dir)) != NULL)
{
if (!strncmp(dirent->d_name, buf, strlen(buf)))
{
searchhits++;
if (searchhits == tabhits)
{
strcpy(tabbuf, buf);
strcpy(buf, dirent->d_name);
pos = strlen(dirent->d_name);
buf[pos] = '\0';
found = stop = True;
}
}
}
closedir(dir); if (pos)
{
complete = complete_on_command(tmpbuf, tabhits);
if (complete)
{
strcpy(buf, tmpbuf);
strncat(buf, complete, sizeof(buf));
found = True;
free(complete);
} }
} }
while (!stop);
lastwastab = True; lastwastab = True;
@ -222,3 +184,44 @@ uicb_launcher(uicb_t cmd)
return; return;
} }
static char *
complete_on_command(char *start, size_t hits)
{
char *path;
char *dirname;
char *ret = NULL;
DIR *dir;
struct dirent *content;
size_t count = 0;
if (!getenv("PATH") || !start || hits <= 0)
return NULL;
path = _strdup(getenv("PATH"));
dirname = strtok(path, ":");
while (dirname)
{
if ((dir = opendir(dirname)))
{
while ((content = readdir(dir)))
if (!strncmp(content->d_name, start, strlen(start)) && ++count == hits)
{
ret = _strdup(content->d_name + strlen(start));
break;
}
closedir(dir);
}
if (ret)
break;
dirname = strtok(NULL, ":");
}
free(path);
return ret;
}