static unsigned char
complete(EditLine *el, int ch)
{
DIR *dd = opendir(".");
struct dirent *dp;
const wchar_t *ptr;
char *buf, *bptr;
const LineInfoW *lf = el_wline(el);
int len, mblen, i;
unsigned char res = 0;
wchar_t dir[1024];
/* Find the last word */
for (ptr = lf->cursor -1; !iswspace(*ptr) && ptr > lf->buffer; --ptr)
continue;
len = lf->cursor - ++ptr;
/* Convert last word to multibyte encoding, so we can compare to it */
wctomb(NULL, 0); /* Reset shift state */
mblen = MB_LEN_MAX * len + 1;
buf = bptr = malloc(mblen);
if (buf == NULL)
err(1, "malloc");
for (i = 0; i < len; ++i) {
/* Note: really should test for -1 return from wctomb */
bptr += wctomb(bptr, ptr[i]);
}
*bptr = 0; /* Terminate multibyte string */
mblen = bptr - buf;
/* Scan directory for matching name */
for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
if (mblen > strlen(dp->d_name))
continue;
if (strncmp(dp->d_name, buf, mblen) == 0) {
mbstowcs(dir, &dp->d_name[mblen],
sizeof(dir) / sizeof(*dir));
if (el_winsertstr(el, dir) == -1)
res = CC_ERROR;
else
res = CC_REFRESH;
break;
}
}
closedir(dd);
free(buf);
return res;
}
int
main(int argc, char *argv[])
{
EditLine *el = NULL;
int numc, ncontinuation;
const wchar_t *line;
TokenizerW *tok;
HistoryW *hist;
HistEventW ev;
#ifdef DEBUG
int i;
#endif
/* Add a user-defined function */
el_wset(el, EL_ADDFN, L"ed-complete", L"Complete argument", complete);
/* Bind <tab> to it */
el_wset(el, EL_BIND, L"^I", L"ed-complete", NULL);
/*
* Bind j, k in vi command mode to previous and next line, instead
* of previous and next history.
*/
el_wset(el, EL_BIND, L"-a", L"k", L"ed-prev-line", NULL);
el_wset(el, EL_BIND, L"-a", L"j", L"ed-next-line", NULL);
/* Source the user's defaults file. */
el_source(el, NULL);