16 #include "../histedit.h"
19 static int continuation;
20 volatile sig_atomic_t gotsig;
21 static const char hfile[] = ".whistory";
26 static wchar_t a[] = L"\1\033[7m\1Edit$\1\033[0m\1 ";
27 static wchar_t b[] = L"Edit> ";
29 return continuation ? b : a;
40 my_wcstombs(const wchar_t *wstr)
47 int needed = wcstombs(0, wstr, 0) + 1;
48 if (needed > buf.len) {
49 buf.str = malloc(needed);
52 wcstombs(buf.str, wstr, needed);
53 buf.str[needed - 1] = 0;
60 complete(EditLine *el, int ch)
62 DIR *dd = opendir(".");
66 const LineInfoW *lf = el_wline(el);
68 unsigned char res = 0;
71 /* Find the last word */
72 for (ptr = lf->cursor -1; !iswspace(*ptr) && ptr > lf->buffer; --ptr)
74 len = lf->cursor - ++ptr;
76 /* Convert last word to multibyte encoding, so we can compare to it */
77 wctomb(NULL, 0); /* Reset shift state */
78 mblen = MB_LEN_MAX * len + 1;
79 buf = bptr = malloc(mblen);
82 for (i = 0; i < len; ++i) {
83 /* Note: really should test for -1 return from wctomb */
84 bptr += wctomb(bptr, ptr[i]);
86 *bptr = 0; /* Terminate multibyte string */
89 /* Scan directory for matching name */
90 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
91 if (mblen > strlen(dp->d_name))
93 if (strncmp(dp->d_name, buf, mblen) == 0) {
94 mbstowcs(dir, &dp->d_name[mblen],
95 sizeof(dir) / sizeof(*dir));
96 if (el_winsertstr(el, dir) == -1)
111 main(int argc, char *argv[])
114 int numc, ncontinuation;
123 setlocale(LC_ALL, "");
125 (void)signal(SIGINT, sig);
126 (void)signal(SIGQUIT, sig);
127 (void)signal(SIGHUP, sig);
128 (void)signal(SIGTERM, sig);
130 hist = history_winit(); /* Init built-in history */
131 history_w(hist, &ev, H_SETSIZE, 100); /* Remember 100 events */
132 history_w(hist, &ev, H_LOAD, hfile);
134 tok = tok_winit(NULL); /* Init the tokenizer */
136 el = el_init(argv[0], stdin, stdout, stderr);
138 el_wset(el, EL_EDITOR, L"vi"); /* Default editor is vi */
139 el_wset(el, EL_SIGNAL, 1); /* Handle signals gracefully */
140 el_wset(el, EL_PROMPT_ESC, prompt, '\1'); /* Set the prompt function */
142 el_wset(el, EL_HIST, history_w, hist); /* FIXME - history_w? */
144 /* Add a user-defined function */
145 el_wset(el, EL_ADDFN, L"ed-complete", L"Complete argument", complete);
147 /* Bind <tab> to it */
148 el_wset(el, EL_BIND, L"^I", L"ed-complete", NULL);
151 * Bind j, k in vi command mode to previous and next line, instead
152 * of previous and next history.
154 el_wset(el, EL_BIND, L"-a", L"k", L"ed-prev-line", NULL);
155 el_wset(el, EL_BIND, L"-a", L"j", L"ed-next-line", NULL);
157 /* Source the user's defaults file. */
160 while((line = el_wgets(el, &numc)) != NULL && numc != 0) {
168 (void)fwprintf(stderr, L"==> got %d %ls", numc, line);
169 (void)fwprintf(stderr, L" > li `%.*ls_%.*ls'\n",
170 (li->cursor - li->buffer), li->buffer,
171 (li->lastchar - 1 - li->cursor),
172 (li->cursor >= li->lastchar) ? L"" : li->cursor);
176 (void)fprintf(stderr, "Got signal %d.\n", (int)gotsig);
181 if(!continuation && numc == 1)
182 continue; /* Only got a linefeed */
185 ncontinuation = tok_wline(tok, li, &ac, &av, &cc, &co);
186 if (ncontinuation < 0) {
187 (void) fprintf(stderr, "Internal error\n");
193 (void)fprintf(stderr, " > nc %d ac %d cc %d co %d\n",
194 ncontinuation, ac, cc, co);
196 history_w(hist, &ev, continuation ? H_APPEND : H_ENTER, line);
198 continuation = ncontinuation;
204 for (i = 0; i < ac; ++i) {
205 (void)fwprintf(stderr, L" > arg# %2d ", i);
207 (void)fwprintf(stderr, L"`%ls'\n", av[i]);
209 (void)fwprintf(stderr, L"`%.*ls_%ls'\n",
210 co, av[i], av[i] + co);
214 if (wcscmp (av[0], L"history") == 0) {
217 for(rc = history_w(hist, &ev, H_LAST);
219 rc = history_w(hist, &ev, H_PREV))
220 (void)fwprintf(stdout, L"%4d %ls",
224 if (wcscmp(av[1], L"clear") == 0)
225 history_w(hist, &ev, H_CLEAR);
230 if (wcscmp(av[1], L"load") == 0)
231 history_w(hist, &ev, H_LOAD,
233 else if (wcscmp(av[1], L"save") == 0)
234 history_w(hist, &ev, H_SAVE,
241 (void)fprintf(stderr,
242 "Bad history arguments\n");
245 } else if (el_wparse(el, ac, av) == -1) {
248 Tokenizer *ntok = tok_init(NULL);
251 tok_str(ntok, my_wcstombs(line), &nargc, &nav);
252 execvp(nav[0],(char **)nav);
264 (void)fprintf(stderr, "Exit %x\n", rc);
274 history_w(hist, &ev, H_SAVE, hfile);
277 fprintf(stdout, "\n");