13 #include "../histedit.h"
16 static int continuation
;
17 volatile sig_atomic_t gotsig
;
18 static const char hfile
[] = ".whistory";
23 static wchar_t a
[] = L
"\1\033[7m\1Edit$\1\033[0m\1 ";
24 static wchar_t b
[] = L
"Edit> ";
26 return continuation
? b
: a
;
37 my_wcstombs(const wchar_t *wstr
)
44 int needed
= wcstombs(0, wstr
, 0) + 1;
45 if (needed
> buf
.len
) {
46 buf
.str
= malloc(needed
);
49 wcstombs(buf
.str
, wstr
, needed
);
50 buf
.str
[needed
- 1] = 0;
57 complete(EditLine
*el
, int ch
)
59 DIR *dd
= opendir(".");
63 const LineInfoW
*lf
= el_wline(el
);
65 unsigned char res
= 0;
68 /* Find the last word */
69 for (ptr
= lf
->cursor
-1; !iswspace(*ptr
) && ptr
> lf
->buffer
; --ptr
)
71 len
= lf
->cursor
- ++ptr
;
73 /* Convert last word to multibyte encoding, so we can compare to it */
74 wctomb(NULL
, 0); /* Reset shift state */
75 mblen
= MB_LEN_MAX
* len
+ 1;
76 buf
= bptr
= malloc(mblen
);
79 for (i
= 0; i
< len
; ++i
) {
80 /* Note: really should test for -1 return from wctomb */
81 bptr
+= wctomb(bptr
, ptr
[i
]);
83 *bptr
= 0; /* Terminate multibyte string */
86 /* Scan directory for matching name */
87 for (dp
= readdir(dd
); dp
!= NULL
; dp
= readdir(dd
)) {
88 if (mblen
> strlen(dp
->d_name
))
90 if (strncmp(dp
->d_name
, buf
, mblen
) == 0) {
91 mbstowcs(dir
, &dp
->d_name
[mblen
],
92 sizeof(dir
) / sizeof(*dir
));
93 if (el_winsertstr(el
, dir
) == -1)
108 main(int argc
, char *argv
[])
111 int numc
, ncontinuation
;
120 setlocale(LC_ALL
, "");
122 (void)signal(SIGINT
, sig
);
123 (void)signal(SIGQUIT
, sig
);
124 (void)signal(SIGHUP
, sig
);
125 (void)signal(SIGTERM
, sig
);
127 hist
= history_winit(); /* Init built-in history */
128 history_w(hist
, &ev
, H_SETSIZE
, 100); /* Remember 100 events */
129 history_w(hist
, &ev
, H_LOAD
, hfile
);
131 tok
= tok_winit(NULL
); /* Init the tokenizer */
133 el
= el_init(argv
[0], stdin
, stdout
, stderr
);
135 el_wset(el
, EL_EDITOR
, L
"vi"); /* Default editor is vi */
136 el_wset(el
, EL_SIGNAL
, 1); /* Handle signals gracefully */
137 el_wset(el
, EL_PROMPT_ESC
, prompt
, '\1'); /* Set the prompt function */
139 el_wset(el
, EL_HIST
, history_w
, hist
); /* FIXME - history_w? */
141 /* Add a user-defined function */
142 el_wset(el
, EL_ADDFN
, L
"ed-complete", L
"Complete argument", complete
);
144 /* Bind <tab> to it */
145 el_wset(el
, EL_BIND
, L
"^I", L
"ed-complete", NULL
);
148 * Bind j, k in vi command mode to previous and next line, instead
149 * of previous and next history.
151 el_wset(el
, EL_BIND
, L
"-a", L
"k", L
"ed-prev-line", NULL
);
152 el_wset(el
, EL_BIND
, L
"-a", L
"j", L
"ed-next-line", NULL
);
154 /* Source the user's defaults file. */
157 while((line
= el_wgets(el
, &numc
)) != NULL
&& numc
!= 0) {
165 (void)fwprintf(stderr
, L
"==> got %d %ls", numc
, line
);
166 (void)fwprintf(stderr
, L
" > li `%.*ls_%.*ls'\n",
167 (li
->cursor
- li
->buffer
), li
->buffer
,
168 (li
->lastchar
- 1 - li
->cursor
),
169 (li
->cursor
>= li
->lastchar
) ? L
"" : li
->cursor
);
173 (void)fprintf(stderr
, "Got signal %d.\n", gotsig
);
178 if(!continuation
&& numc
== 1)
179 continue; /* Only got a linefeed */
182 ncontinuation
= tok_wline(tok
, li
, &ac
, &av
, &cc
, &co
);
183 if (ncontinuation
< 0) {
184 (void) fprintf(stderr
, "Internal error\n");
190 (void)fprintf(stderr
, " > nc %d ac %d cc %d co %d\n",
191 ncontinuation
, ac
, cc
, co
);
193 history_w(hist
, &ev
, continuation
? H_APPEND
: H_ENTER
, line
);
195 continuation
= ncontinuation
;
201 for (i
= 0; i
< ac
; ++i
) {
202 (void)fwprintf(stderr
, L
" > arg# %2d ", i
);
204 (void)fwprintf(stderr
, L
"`%ls'\n", av
[i
]);
206 (void)fwprintf(stderr
, L
"`%.*ls_%ls'\n",
207 co
, av
[i
], av
[i
] + co
);
211 if (wcscmp (av
[0], L
"history") == 0) {
214 for(rc
= history_w(hist
, &ev
, H_LAST
);
216 rc
= history_w(hist
, &ev
, H_PREV
))
217 (void)fwprintf(stdout
, L
"%4d %ls",
221 if (wcscmp(av
[1], L
"clear") == 0)
222 history_w(hist
, &ev
, H_CLEAR
);
227 if (wcscmp(av
[1], L
"load") == 0)
228 history_w(hist
, &ev
, H_LOAD
,
230 else if (wcscmp(av
[1], L
"save") == 0)
231 history_w(hist
, &ev
, H_SAVE
,
238 (void)fprintf(stderr
,
239 "Bad history arguments\n");
242 } else if (el_wparse(el
, ac
, av
) == -1) {
245 Tokenizer
*ntok
= tok_init(NULL
);
248 tok_str(ntok
, my_wcstombs(line
), &nargc
, &nav
);
249 execvp(nav
[0],(char **)nav
);
261 (void)fprintf(stderr
, "Exit %x\n", rc
);
271 history_w(hist
, &ev
, H_SAVE
, hfile
);
274 fprintf(stdout
, "\n");