I'm developing a Linux tool with a prompt in C. I have enabled "function" and "file" completion using GNU library. Now I want to extend the support to "variable" name completion as well. Below is the example for better understanding:
WORKING OUTPUT:
build2:/u/anitha> linux_tool
loading history
tool>
tool> ***On pressing TAB twice, the functions are prompted***
Display all 251 possibilities? (y or n)
byte(
dim(
fncc_fft2d(
HasValue(
ifft(
load_csv(
EXPECTED ADDITIONAL SUPPORT:
tool> **var_name**=create(2,2)
2x2x1 array of int, bsq format [16 bytes]
0 1
2 3
tool>
tool> ***on pressing TAB twice, I want the variable name to be prompted for auto completion. i.e.**,*
tool> var_
**var_name**
Version(
Code added for auto completion:
char ** dv_complete_func(char *text, int start, int end);
char *dv_complete_func_generator(const char*, int);
extern struct _vfuncptr vfunclist[];
/* This array has the list of functions that the tool can support */
char **
dv_complete_func(char *text, int start, int end)
{
return rl_completion_matches(text, dv_complete_func_generator);
}
char *dv_complete_func_generator(const char *text, int state)
{
static int list_index, len;
char *name;
if(!state) {
list_index =0;
len=strlen(text);
}
while((name = vfunclist[list_index++].name)) {
if(strncmp(name,text,len)==0) {
char *namedup = strdup(name);
strcat(namedup,"(");
return strdup(namedup);
}
}
return NULL;
}
Let me know if the question is unclear. Please help me with a solution.
(Just a guess, since you don't show your function bound to
rl_attempted_completion_function
as you really should)You probably should also use the
rl_line_buffer
,rl_point
,rl_end
global variables (modified byreadline
, you want only read access to them).Read carefully (and several times) the chapter on custom completers.
BTW, you almost certainly should use
start
andend
arguments todv_complete_func
(assuming you put that inrl_attempted_completion_function
)NB. I did experiment around these ideas in my (defunct) minil project on github. Sadly all comments and variables there are in French (because it was written to convince a French retired AI researcher). And yes,
readline
API is very baroque (and you also should study the source code of programs using it, notably GNU bash). Maybe you should consider using some other library... (look into ncurses and see this question and other libraries)