diff options
Diffstat (limited to 'src/completion.c')
-rw-r--r-- | src/completion.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/completion.c b/src/completion.c new file mode 100644 index 0000000..b90f249 --- /dev/null +++ b/src/completion.c @@ -0,0 +1,88 @@ + +#include <stdio.h> +#include <sys/types.h> +#include <sys/file.h> +#include <sys/stat.h> +#include <sys/errno.h> + +#include <readline/readline.h> +#include <readline/history.h> + +#include <utils.h> +#include <commands.h> + +/* **************************************************************** */ +/* */ +/* Interface to Readline Completion */ +/* */ +/* **************************************************************** */ + +/* + Generator function for command completion. STATE lets us know whether + to start from scratch; without any state (i.e. STATE == 0), then we + start at the top of the list. + */ +char *command_generator( const char *text, int state ) +{ + static int list_index, len; + char *name; + + /* + If this is a new word to complete, initialize now. This includes + saving the length of TEXT for efficiency, and initializing the index + variable to 0. + */ + if( !state ) + { + list_index = 0; + len = strlen( text ); + } + + /* Return the next name which partially matches from the command list. */ + while( (name = current->list[list_index].name) ) + { + list_index++; + + if( strncmp(name, text, len) == 0 ) + return( dupstr(name) ); + } + + /* If no names matched, then return NULL. */ + return( (char *)NULL ); +} + +/* + Attempt to complete on the contents of TEXT. START and END show the + region of TEXT that contains the word to complete. We can use the + entire line in case we want to do some simple parsing. Return the + array of matches, or NULL if there aren't any. + */ +char **sila_completion( char *text, int start, int end ) +{ + char **matches; + + matches = (char **)NULL; + + /* If this word is at the start of the line, then it is a command + to complete. Otherwise it is the name of a file in the current + directory. */ + if (start == 0) + matches = rl_completion_matches (text, command_generator); + + return (matches); +} + +/* + Tell the GNU Readline library how to complete. We want to try to complete + on command names if this is the first word in the line, or on filenames + if not. + */ +void initialize_readline() +{ + /* Allow conditional parsing of the ~/.inputrc file. */ + rl_readline_name = "sila"; + + /* Tell the completer that we want a crack first. */ + rl_attempted_completion_function = (rl_completion_func_t *)sila_completion; +} + |