GNU readline: Implement Custom Auto-complete Posted on June 16th
GNU readline implement filename auto-complete by default, it will list all the files in the current directory. We can disable it by binds our TAB key to some other operation. In previous post, I simply abort the operation to ignore users hitting TABs.
Auto-complete are useful if only we can customize it. Well, readline allows us do it by assign our own callback functions. First of all, you may want to read up the manual from HERE. It does provides a c sample codes as well but I find it too complicated, here I provide a simplified version that can be compiled under c++.
I store my ‘keywords’ targeted for auto-completion in an array cmd. Before calling readline(), I must point my completion callback function to rl_attempted_completion_function.
In my_completion(), if the state==1 meaning auto-completing the first word from readline(). If the state > 1 and my_completion() return NULL, filename auto-complete will be trigger automatically, which I find it damn irritating. So when state !=1, I disable it, and enable it back after next readline(). It is a dirty hack to me, I am still searching for a proper way to disable filename completion entirely.
I wrote xmalloc() myself because extern xmalloc doesn’t seems to work while compiling under c++.
To get better understanding of readline auto-complete, please read up the manual carefully and try it out yourself.
At last, hope you enjoy the post.
Trackback URL
Implement Custom Auto-complete…
This tip shows implementing custom auto-complete on linux using GNU readline library…
Commented alldevnet.com on July 26th, 2008.Thanks to the article, it make the life easier and make the commenter come back again and again
Commented autoverleih on August 20th, 2008.