Skip to content

Commit 288fdd8

Browse files
committed
Demo user-defineable completion function
1 parent 1a4923b commit 288fdd8

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

readline.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ char **builtin_completion(const char *text, int UNUSED start, int UNUSED end) {
168168
return matches;
169169
}
170170

171+
static List *cmdcomplete(char *prefix) {
172+
List *fn = varlookup("fn-%complete", NULL);
173+
if (fn == NULL)
174+
return NULL;
175+
gcdisable();
176+
fn = append(fn, mklist(mkstr(str("%s", prefix)), NULL));
177+
gcenable();
178+
return eval(fn, NULL, 0);
179+
}
180+
181+
char **es_completion(UNUSED const char *text, UNUSED int start, UNUSED int end) {
182+
char **matches;
183+
184+
complprefix = "";
185+
wordslistgen = cmdcomplete;
186+
187+
matches = rl_completion_matches(text, list_completion_function);
188+
189+
rl_attempted_completion_over = 1; /* suppress "default" completions */
190+
return matches;
191+
}
192+
171193
static void initreadline(void) {
172194
rl_readline_name = "es";
173195

@@ -177,23 +199,24 @@ static void initreadline(void) {
177199
rl_completer_quote_characters = "'";
178200
rl_special_prefixes = "$";
179201

180-
rl_attempted_completion_function = builtin_completion;
181-
182202
rl_filename_quote_characters = " \t\n\\`'$><=;|&{()}";
183203
rl_filename_quoting_function = quote;
184204
rl_filename_dequoting_function = unquote;
185205
}
186206

187207
/* set up readline for the next call */
188-
extern void rlsetup(UNUSED Boolean fromprim) {
208+
extern void rlsetup(Boolean fromprim) {
189209
static Boolean initialized = FALSE;
190210
if (!initialized) {
191211
initreadline();
192212
initialized = TRUE;
193213
}
194214

195-
/* TODO: from-primitive completion function */
196-
rl_attempted_completion_function = (fromprim ? NULL : builtin_completion);
215+
if (fromprim) {
216+
rl_attempted_completion_function = es_completion;
217+
} else {
218+
rl_attempted_completion_function = builtin_completion;
219+
}
197220

198221
if (reloadhistory)
199222
reload_history();
@@ -249,7 +272,7 @@ PRIM(resetterminal) {
249272
}
250273

251274
static char *callreadline(char *prompt) {
252-
char *r, *volatile line;
275+
char *r, *volatile line = NULL;
253276
/* should this be called after each interruption, or? */
254277
rlsetup(TRUE);
255278
interrupted = FALSE;
@@ -273,11 +296,13 @@ static char *callreadline(char *prompt) {
273296

274297
PRIM(readline) {
275298
char *line;
276-
Ref(char *, prompt, (list == NULL ? "" : getstr(list->term)));
299+
/* TODO: estrdup? */
300+
char *prompt = (list == NULL ? "" : strdup(getstr(list->term)));
277301
do {
278302
line = callreadline(prompt);
279303
} while (line == NULL && errno == EINTR);
280-
RefEnd(prompt);
304+
if (prompt != NULL)
305+
efree(prompt);
281306
if (line == NULL)
282307
return NULL;
283308
list = mklist(mkstr(line), NULL);

0 commit comments

Comments
 (0)