I'm adding readline
support for a program with an interactive shell.
I've found some great examples online like this one but I'm having trouble telling readline
to automatically expand file names with \␣
when the file name has a whitespace.
The blog post mentioned above explains how to do that with custom completion. However I have a command of the type load filename
, so for the second argument I'm letting readline
do the default behaviour which is to complete file names by setting rl_attempted_completion_over
to 0.
I'm also reading the api documentation and there are several functions and variables related to that topic, however I'm a little bit overwhelmed with that documentation, that's too much information to take it at once.
So simple example:
#include <readline/readline.h>
int main(void)
{
rl_filename_quoting_desired = 1;
rl_filename_quote_characters = " \t\n\"\\'`@$><=;|&{(";
rl_filename_completion_desired = 1;
rl_filename_quoting_desired = 1;
// rl_completer_quote_characters = "\""; see explanation below
char *buffer = readline("> ");
free(buffer);
return 0;
}
So, if I run this
> fi<TAB>
file with spaces.txt firefox_shaoran/
> fil<TAB>
> file with spaces.txt
But I want readline
to exapnd like bash does, that means after > file<TAB>
I want to get file\ with\ spaces.txt
.
I've been playing around with the different variables provided by readline
, but I cannot get the same result. In the documentation of rl_basic_word_break_characters
it says that bash uses " \t\n\"\\'@$><=;|&{("
. I tried setting rl_filename_completion_desired
and
rl_filename_quoting_desired
to 1, but it doesn't work either.
What should I do in the simple example so that readline
expands file names as bash does?
// edit:
After reading a little bit of the bash source code, I came across the bashline.c
file and found a place where rl_filename_quote_characters
and rl_completer_quote_characters
are set. In my example I commented that line because without it, the behaviour didn't change. However if I uncomment the line, readline
quotes file names with empty spaces, which is great. What I haven't found so far in the documentation is how to escape the completion instead of quoting it.
I also tried rl_completer_quote_characters = "\"";