mirror of
https://github.com/louisrubet/rpn
synced 2025-01-29 20:34:21 +01:00
Autocompletion and history with ReadLine
This commit is contained in:
parent
4eb7c132ca
commit
b9d240478b
3 changed files with 18 additions and 22 deletions
|
@ -16,6 +16,6 @@ rpn_SOURCES = src/rpn.cpp \
|
||||||
src/rpn-trig.h \
|
src/rpn-trig.h \
|
||||||
src/stack.h
|
src/stack.h
|
||||||
|
|
||||||
rpn_LDFLAGS = -lreadline
|
rpn_LDFLAGS = -Wl,--no-as-needed -lreadline
|
||||||
|
|
||||||
dist_noinst_SCRIPTS = autogen.sh
|
dist_noinst_SCRIPTS = autogen.sh
|
||||||
|
|
29
src/parse.h
29
src/parse.h
|
@ -78,7 +78,7 @@ static bool get_keyword(const string& entry, object*& obj, unsigned int& obj_siz
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
// could be a command
|
// could be a command
|
||||||
if (get_fn(entry, fn, type) == ret_ok)
|
if (get_fn(entry.c_str(), fn, type) == ret_ok)
|
||||||
{
|
{
|
||||||
if (type == cmd_keyword)
|
if (type == cmd_keyword)
|
||||||
{
|
{
|
||||||
|
@ -299,14 +299,10 @@ static ret_value entry(program& prog)
|
||||||
|
|
||||||
static char** entry_completion(const char* text, int start, int end)
|
static char** entry_completion(const char* text, int start, int end)
|
||||||
{
|
{
|
||||||
char** matches;
|
char** matches = NULL;
|
||||||
|
|
||||||
matches = (char**)NULL;
|
|
||||||
|
|
||||||
if (start == 0)
|
if (start == 0)
|
||||||
matches = rl_completion_matches((char*)text, &entry_completion_generator);
|
matches = rl_completion_matches((char*)text, &entry_completion_generator);
|
||||||
else
|
|
||||||
rl_bind_key('\t',rl_abort);
|
|
||||||
|
|
||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
@ -314,9 +310,8 @@ static char** entry_completion(const char* text, int start, int end)
|
||||||
static char* entry_completion_generator(const char* text, int state)
|
static char* entry_completion_generator(const char* text, int state)
|
||||||
{
|
{
|
||||||
static int list_index, len, too_far;
|
static int list_index, len, too_far;
|
||||||
char* name;
|
|
||||||
|
|
||||||
if (!state)
|
if (state == 0)
|
||||||
{
|
{
|
||||||
list_index = 0;
|
list_index = 0;
|
||||||
too_far = 0;
|
too_far = 0;
|
||||||
|
@ -325,19 +320,19 @@ static char* entry_completion_generator(const char* text, int state)
|
||||||
|
|
||||||
while(too_far == 0)
|
while(too_far == 0)
|
||||||
{
|
{
|
||||||
if (_keywords[list_index].fn == NULL)
|
list_index++;
|
||||||
|
if (_keywords[list_index].fn != NULL)
|
||||||
{
|
{
|
||||||
|
// compare list entry with text, return if match
|
||||||
|
if (strncmp(_keywords[list_index].name, text, len) == 0)
|
||||||
|
return entry_completion_dupstr(_keywords[list_index].name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// fn=NULL and size=0 = last entry in list -> go out
|
||||||
if (_keywords[list_index].comment.size() == 0)
|
if (_keywords[list_index].comment.size() == 0)
|
||||||
too_far = 1;
|
too_far = 1;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list_index++;
|
|
||||||
|
|
||||||
// mpo ! ne pas stocker c_str
|
|
||||||
name = (char*)_keywords[list_index].name.c_str();
|
|
||||||
if (strncmp(name, text, len) == 0)
|
|
||||||
return entry_completion_dupstr(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If no names matched, then return NULL. */
|
/* If no names matched, then return NULL. */
|
||||||
|
|
|
@ -520,17 +520,18 @@ public:
|
||||||
struct keyword_t
|
struct keyword_t
|
||||||
{
|
{
|
||||||
cmd_type_t type;
|
cmd_type_t type;
|
||||||
string name;
|
char name[24];
|
||||||
program_fn_t fn;
|
program_fn_t fn;
|
||||||
string comment;
|
string comment;
|
||||||
};
|
};
|
||||||
static keyword_t _keywords[g_max_commands];
|
static keyword_t _keywords[g_max_commands];
|
||||||
|
|
||||||
static ret_value get_fn(const string& fn_name, program_fn_t& fn, cmd_type_t& type)
|
static ret_value get_fn(const char* fn_name, program_fn_t& fn, cmd_type_t& type)
|
||||||
{
|
{
|
||||||
for(unsigned int i=0; (i<sizeof(_keywords)/sizeof(_keywords[0])) && (_keywords[i].type != cmd_max); i++)
|
for(unsigned int i=0; (i<sizeof(_keywords)/sizeof(_keywords[0])) && (_keywords[i].type != cmd_max); i++)
|
||||||
{
|
{
|
||||||
if ((_keywords[i].name.size()>0) && (fn_name == _keywords[i].name))
|
if ((strnlen(_keywords[i].name, sizeof(_keywords[i].name))>0)
|
||||||
|
&& (strncmp(fn_name, _keywords[i].name, sizeof(_keywords[i].name)) == 0))
|
||||||
{
|
{
|
||||||
fn = _keywords[i].fn;
|
fn = _keywords[i].fn;
|
||||||
type = _keywords[i].type;
|
type = _keywords[i].type;
|
||||||
|
|
Loading…
Add table
Reference in a new issue