12 #include "../histedit.h"
15 static int continuation
;
16 volatile sig_atomic_t gotsig
;
21 static wchar_t a
[] = L
"\1\e[7m\1Edit$\1\e[0m\1 ";
22 static wchar_t b
[] = L
"Edit> ";
24 return continuation
? b
: a
;
35 my_wcstombs(const wchar_t *wstr
)
42 int needed
= wcstombs(0, wstr
, 0) + 1;
43 if (needed
> buf
.len
) {
44 buf
.str
= malloc(needed
);
47 wcstombs(buf
.str
, wstr
, needed
);
48 buf
.str
[needed
- 1] = 0;
55 complete(EditLine
*el
, int ch
)
57 DIR *dd
= opendir(".");
61 const LineInfoW
*lf
= el_wline(el
);
65 /* Find the last word */
66 for (ptr
= lf
->cursor
-1; !iswspace(*ptr
) && ptr
> lf
->buffer
; --ptr
)
68 len
= lf
->cursor
- ++ptr
;
70 /* Convert last word to multibyte encoding, so we can compare to it */
71 wctomb(NULL
, 0); /* Reset shift state */
72 mblen
= MB_LEN_MAX
* len
+ 1;
73 buf
= bptr
=(char *)malloc(mblen
);
74 for (i
= 0; i
< len
; ++i
) {
75 /* Note: really should test for -1 return from wctomb */
76 bptr
+= wctomb(bptr
, ptr
[i
]);
78 *bptr
= 0; /* Terminate multibyte string */
81 /* Scan directory for matching name */
82 for (dp
= readdir(dd
); dp
!= NULL
; dp
= readdir(dd
)) {
83 if (mblen
> strlen(dp
->d_name
))
85 if (strncmp(dp
->d_name
, buf
, mblen
) == 0) {
86 if (el_insertstr(el
, &dp
->d_name
[mblen
]) == -1)
101 main(int argc
, char *argv
[])
104 int numc
, ncontinuation
;
113 setlocale(LC_ALL
, "");
115 (void)signal(SIGINT
, sig
);
116 (void)signal(SIGQUIT
, sig
);
117 (void)signal(SIGHUP
, sig
);
118 (void)signal(SIGTERM
, sig
);
120 hist
= history_winit(); /* Init built-in history */
121 history_w(hist
, &ev
, H_SETSIZE
, 100); /* Remember 100 events */
123 tok
= tok_winit(NULL
); /* Init the tokenizer */
125 el
= el_init(argv
[0], stdin
, stdout
, stderr
);
127 el_wset(el
, EL_EDITOR
, L
"vi"); /* Default editor is vi */
128 el_wset(el
, EL_SIGNAL
, 1); /* Handle signals gracefully */
129 el_wset(el
, EL_PROMPT_ESC
, prompt
, '\1'); /* Set the prompt function */
131 el_wset(el
, EL_HIST
, history_w
, hist
); /* FIXME - history_w? */
133 /* Add a user-defined function */
134 el_wset(el
, EL_ADDFN
, L
"ed-complete", L
"Complete argument", complete
);
136 /* Bind <tab> to it */
137 el_wset(el
, EL_BIND
, L
"^I", L
"ed-complete", NULL
);
140 * Bind j, k in vi command mode to previous and next line, instead
141 * of previous and next history.
143 el_wset(el
, EL_BIND
, L
"-a", L
"k", L
"ed-prev-line", NULL
);
144 el_wset(el
, EL_BIND
, L
"-a", L
"j", L
"ed-next-line", NULL
);
146 /* Source the user's defaults file. */
149 while((line
= el_wgets(el
, &numc
)) != NULL
&& numc
!= 0) {
157 (void)fwprintf(stderr
, L
"==> got %d %ls", numc
, line
);
158 (void)fwprintf(stderr
, L
" > li `%.*ls_%.*ls'\n",
159 (li
->cursor
- li
->buffer
), li
->buffer
,
160 (li
->lastchar
- 1 - li
->cursor
),
161 (li
->cursor
>= li
->lastchar
) ? L
"" : li
->cursor
);
165 (void)fprintf(stderr
, "Got signal %d.\n", gotsig
);
170 if(!continuation
&& numc
== 1)
171 continue; /* Only got a linefeed */
174 ncontinuation
= tok_wline(tok
, li
, &ac
, &av
, &cc
, &co
);
175 if (ncontinuation
< 0) {
176 (void) fprintf(stderr
, "Internal error\n");
182 (void)fprintf(stderr
, " > nc %d ac %d cc %d co %d\n",
183 ncontinuation
, ac
, cc
, co
);
185 history_w(hist
, &ev
, continuation
? H_APPEND
: H_ENTER
, line
);
187 continuation
= ncontinuation
;
193 for (i
= 0; i
< ac
; ++i
) {
194 (void)fwprintf(stderr
, L
" > arg# %2d ", i
);
196 (void)fwprintf(stderr
, L
"`%ls'\n", av
[i
]);
198 (void)fwprintf(stderr
, L
"`%.*ls_%ls'\n",
199 co
, av
[i
], av
[i
] + co
);
203 if (wcscmp (av
[0], L
"history") == 0) {
206 for(rc
= history_w(hist
, &ev
, H_LAST
);
208 rc
= history_w(hist
, &ev
, H_PREV
))
209 (void)fwprintf(stdout
, L
"%4d %ls",
213 if (wcscmp(av
[1], L
"clear") == 0)
214 history_w(hist
, &ev
, H_CLEAR
);
219 if (wcscmp(av
[1], L
"load") == 0)
220 history_w(hist
, &ev
, H_LOAD
,
222 else if (wcscmp(av
[1], L
"save") == 0)
223 history_w(hist
, &ev
, H_SAVE
,
230 (void)fprintf(stderr
,
231 "Bad history arguments\n");
234 } else if (el_wparse(el
, ac
, av
) == -1) {
237 Tokenizer
*ntok
= tok_init(NULL
);
240 tok_str(ntok
, my_wcstombs(line
), &nargc
, &nav
);
241 execvp(nav
[0],(char **)nav
);
253 (void)fprintf(stderr
, "Exit %x\n", rc
);
265 fprintf(stdout
, "\n");