From 68fc1605e7d3a317477abf1afc6410581724b72d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20M=C3=BCnnich?= Date: Sat, 5 Dec 2015 17:34:49 +0100 Subject: Completion of command names and variables New completion entry functions for readline specific code: - compl_var for variables - compl_fn for functions - compl_builtin for builtins - compl_extcmd for external commands in path - compl_command, which combines the last three The first three are simple wrappers for compl_name, which iterates over a typical rc table (an array of structs) yielding the name fields. The appropriate completion entry function is chosen based on the character/token that preceeds the word to complete: '$' for variable, beginning of line or one of [`@|&(){;] for command. Otherwise the shell falls back to readline's default completion, which it also does, if the chosen completion entry function finds no match. --- hash.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 6a176a4..32b2207 100644 --- a/hash.c +++ b/hash.c @@ -316,3 +316,28 @@ extern void whatare_all_vars(bool showfn, bool showvar) { if (fp[i].name != NULL && fp[i].name != dead) prettyprint_fn(1, fp[i].name, fnlookup(fp[i].name)); } + +extern char *compl_name(const char *text, int state, char **p, size_t count, ssize_t inc) { + static char **n; + static size_t i, len; + char *name; + + if (!state) { + n = p; + i = 0; + len = strlen(text); + } + for (name = NULL; name == NULL && i < count; i++, n += inc) + if (*n != NULL && strncmp(*n, text, len) == 0) + name = strdup(*n); + return name; +} + +extern char *compl_fn(const char *text, int state) { + return compl_name(text, state, &fp[0].name, fsize, &fp[1].name - &fp[0].name); +} + +extern char *compl_var(const char *text, int state) { + return compl_name(text, state, &vp[0].name, vsize, &vp[1].name - &vp[0].name); +} + -- cgit v1.2.3