19 #include "../histedit.h"
22 static int continuation;
23 volatile sig_atomic_t gotsig;
24 static const char hfile[] = ".whistory";
29 static wchar_t a[] = L"\1\033[7m\1Edit$\1\033[0m\1 ";
30 static wchar_t b[] = L"Edit> ";
32 return continuation ? b : a;
43 my_wcstombs(const wchar_t *wstr)
50 int needed = wcstombs(0, wstr, 0) + 1;
51 if (needed > buf.len) {
52 buf.str = malloc(needed);
55 wcstombs(buf.str, wstr, needed);
56 buf.str[needed - 1] = 0;
63 complete(EditLine *el, int ch)
65 DIR *dd = opendir(".");
69 const LineInfoW *lf = el_wline(el);
71 unsigned char res = 0;
74 /* Find the last word */
75 for (ptr = lf->cursor -1; !iswspace(*ptr) && ptr > lf->buffer; --ptr)
77 len = lf->cursor - ++ptr;
79 /* Convert last word to multibyte encoding, so we can compare to it */
80 wctomb(NULL, 0); /* Reset shift state */
81 mblen = MB_LEN_MAX * len + 1;
82 buf = bptr = malloc(mblen);
85 for (i = 0; i < len; ++i) {
86 /* Note: really should test for -1 return from wctomb */
87 bptr += wctomb(bptr, ptr[i]);
89 *bptr = 0; /* Terminate multibyte string */
92 /* Scan directory for matching name */
93 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
94 if (mblen > strlen(dp->d_name))
96 if (strncmp(dp->d_name, buf, mblen) == 0) {
97 mbstowcs(dir, &dp->d_name[mblen],
98 sizeof(dir) / sizeof(*dir));
99 if (el_winsertstr(el, dir) == -1)
114 main(int argc, char *argv[])
117 int numc, ncontinuation;
126 setlocale(LC_ALL, "");
128 (void)signal(SIGINT, sig);
129 (void)signal(SIGQUIT, sig);
130 (void)signal(SIGHUP, sig);
131 (void)signal(SIGTERM, sig);
133 hist = history_winit(); /* Init built-in history */
134 history_w(hist, &ev, H_SETSIZE, 100); /* Remember 100 events */
135 history_w(hist, &ev, H_LOAD, hfile);
137 tok = tok_winit(NULL); /* Init the tokenizer */
139 el = el_init(argv[0], stdin, stdout, stderr);
141 el_wset(el, EL_EDITOR, L"vi"); /* Default editor is vi */
142 el_wset(el, EL_SIGNAL, 1); /* Handle signals gracefully */
143 el_wset(el, EL_PROMPT_ESC, prompt, '\1'); /* Set the prompt function */
145 el_wset(el, EL_HIST, history_w, hist); /* FIXME - history_w? */
147 /* Add a user-defined function */
148 el_wset(el, EL_ADDFN, L"ed-complete", L"Complete argument", complete);
150 /* Bind <tab> to it */
151 el_wset(el, EL_BIND, L"^I", L"ed-complete", NULL);
154 * Bind j, k in vi command mode to previous and next line, instead
155 * of previous and next history.
157 el_wset(el, EL_BIND, L"-a", L"k", L"ed-prev-line", NULL);
158 el_wset(el, EL_BIND, L"-a", L"j", L"ed-next-line", NULL);
160 /* Source the user's defaults file. */
163 while((line = el_wgets(el, &numc)) != NULL && numc != 0) {
171 (void)fwprintf(stderr, L"==> got %d %ls", numc, line);
172 (void)fwprintf(stderr, L" > li `%.*ls_%.*ls'\n",
173 (li->cursor - li->buffer), li->buffer,
174 (li->lastchar - 1 - li->cursor),
175 (li->cursor >= li->lastchar) ? L"" : li->cursor);
179 (void)fprintf(stderr, "Got signal %d.\n", (int)gotsig);
184 if(!continuation && numc == 1)
185 continue; /* Only got a linefeed */
188 ncontinuation = tok_wline(tok, li, &ac, &av, &cc, &co);
189 if (ncontinuation < 0) {
190 (void) fprintf(stderr, "Internal error\n");
196 (void)fprintf(stderr, " > nc %d ac %d cc %d co %d\n",
197 ncontinuation, ac, cc, co);
199 history_w(hist, &ev, continuation ? H_APPEND : H_ENTER, line);
201 continuation = ncontinuation;
207 for (i = 0; i < ac; ++i) {
208 (void)fwprintf(stderr, L" > arg# %2d ", i);
210 (void)fwprintf(stderr, L"`%ls'\n", av[i]);
212 (void)fwprintf(stderr, L"`%.*ls_%ls'\n",
213 co, av[i], av[i] + co);
217 if (wcscmp (av[0], L"history") == 0) {
220 for(rc = history_w(hist, &ev, H_LAST);
222 rc = history_w(hist, &ev, H_PREV))
223 (void)fwprintf(stdout, L"%4d %ls",
227 if (wcscmp(av[1], L"clear") == 0)
228 history_w(hist, &ev, H_CLEAR);
233 if (wcscmp(av[1], L"load") == 0)
234 history_w(hist, &ev, H_LOAD,
236 else if (wcscmp(av[1], L"save") == 0)
237 history_w(hist, &ev, H_SAVE,
244 (void)fprintf(stderr,
245 "Bad history arguments\n");
248 } else if (el_wparse(el, ac, av) == -1) {
251 Tokenizer *ntok = tok_init(NULL);
254 tok_str(ntok, my_wcstombs(line), &nargc, &nav);
255 execvp(nav[0],(char **)nav);
267 (void)fprintf(stderr, "Exit %x\n", rc);
277 history_w(hist, &ev, H_SAVE, hfile);
280 fprintf(stdout, "\n");