1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Refactor autocompleters

This commit is contained in:
James Booth 2013-06-02 19:56:35 +01:00
parent f72e7d89b8
commit af95c82fe7
3 changed files with 362 additions and 330 deletions

View File

@ -45,8 +45,6 @@
#include "ui/ui.h" #include "ui/ui.h"
#include "xmpp/xmpp.h" #include "xmpp/xmpp.h"
typedef char*(*autocomplete_func)(char *);
/* /*
* Command structure * Command structure
* *
@ -73,19 +71,16 @@ static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help,
const char * const display, preference_t pref); const char * const display, preference_t pref);
static void _cmd_complete_parameters(char *input, int *size); static void _cmd_complete_parameters(char *input, int *size);
static void _sub_autocomplete(char *input, int *size);
static void _notify_autocomplete(char *input, int *size); static char * _sub_autocomplete(char *input, int *size);
static void _titlebar_autocomplete(char *input, int *size); static char * _notify_autocomplete(char *input, int *size);
static void _theme_autocomplete(char *input, int *size); static char * _titlebar_autocomplete(char *input, int *size);
static void _autoaway_autocomplete(char *input, int *size); static char * _theme_autocomplete(char *input, int *size);
static void _account_autocomplete(char *input, int *size); static char * _autoaway_autocomplete(char *input, int *size);
static void _who_autocomplete(char *input, int *size); static char * _account_autocomplete(char *input, int *size);
static void _roster_autocomplete(char *input, int *size); static char * _who_autocomplete(char *input, int *size);
static void _group_autocomplete(char *input, int *size); static char * _roster_autocomplete(char *input, int *size);
static void _parameter_autocomplete(char *input, int *size, char *command, static char * _group_autocomplete(char *input, int *size);
autocomplete_func func);
static void _parameter_autocomplete_with_ac(char *input, int *size, char *command,
Autocomplete ac);
static int _strtoi(char *str, int *saveptr, int min, int max); static int _strtoi(char *str, int *saveptr, int min, int max);
@ -1217,76 +1212,159 @@ cmd_execute_default(const char * const inp)
static void static void
_cmd_complete_parameters(char *input, int *size) _cmd_complete_parameters(char *input, int *size)
{ {
_parameter_autocomplete(input, size, "/beep", int i;
prefs_autocomplete_boolean_choice); char *result = NULL;
_parameter_autocomplete(input, size, "/intype",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/states",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/outtype",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/flash",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/splash",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/chlog",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/grlog",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/mouse",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/history",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/vercheck",
prefs_autocomplete_boolean_choice);
_parameter_autocomplete(input, size, "/statuses",
prefs_autocomplete_boolean_choice);
// autocomplete boolean settings
gchar *boolean_choices[] = { "/beep", "/intype", "/states", "/outtype",
"/flash", "/splash", "/chlog", "/grlog", "/mouse", "/history",
"/vercheck", "/statuses" };
for (i = 0; i < ARRAY_SIZE(boolean_choices); i++) {
result = autocomplete_param_with_func(input, size, boolean_choices[i],
prefs_autocomplete_boolean_choice);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
}
// autocomplete nickname in chat rooms
if (ui_current_win_type() == WIN_MUC) { if (ui_current_win_type() == WIN_MUC) {
Autocomplete nick_ac = muc_get_roster_ac(ui_current_recipient()); Autocomplete nick_ac = muc_get_roster_ac(ui_current_recipient());
if (nick_ac != NULL) { if (nick_ac != NULL) {
_parameter_autocomplete_with_ac(input, size, "/msg", nick_ac); gchar *nick_choices[] = { "/msg", "/info", "/caps", "/status", "/software" } ;
_parameter_autocomplete_with_ac(input, size, "/info", nick_ac);
_parameter_autocomplete_with_ac(input, size, "/caps", nick_ac); for (i = 0; i < ARRAY_SIZE(nick_choices); i++) {
_parameter_autocomplete_with_ac(input, size, "/status", nick_ac); result = autocomplete_param_with_ac(input, size, nick_choices[i],
_parameter_autocomplete_with_ac(input, size, "/software", nick_ac); nick_ac);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
}
} }
// otherwise autocomple using roster
} else { } else {
_parameter_autocomplete(input, size, "/msg", gchar *contact_choices[] = { "/msg", "/info", "/status" };
roster_find_contact); for (i = 0; i < ARRAY_SIZE(contact_choices); i++) {
_parameter_autocomplete(input, size, "/info", result = autocomplete_param_with_func(input, size, contact_choices[i],
roster_find_contact); roster_find_contact);
_parameter_autocomplete(input, size, "/caps", if (result != NULL) {
roster_find_resource); inp_replace_input(input, result, size);
_parameter_autocomplete(input, size, "/status", g_free(result);
roster_find_contact); return;
_parameter_autocomplete(input, size, "/software", }
roster_find_resource); }
gchar *resource_choices[] = { "/caps", "/software" };
for (i = 0; i < ARRAY_SIZE(resource_choices); i++) {
result = autocomplete_param_with_func(input, size, resource_choices[i],
roster_find_resource);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
}
} }
_parameter_autocomplete(input, size, "/invite", roster_find_contact); result = autocomplete_param_with_func(input, size, "/invite", roster_find_contact);
_parameter_autocomplete(input, size, "/decline", muc_find_invite); if (result != NULL) {
_parameter_autocomplete(input, size, "/join", muc_find_invite); inp_replace_input(input, result, size);
g_free(result);
return;
}
gchar *invite_choices[] = { "/decline", "/join" };
for (i = 0; i < ARRAY_SIZE(invite_choices); i++) {
result = autocomplete_param_with_func(input, size, invite_choices[i],
muc_find_invite);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
}
_parameter_autocomplete(input, size, "/connect", result = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled);
accounts_find_enabled); if (result != NULL) {
_parameter_autocomplete_with_ac(input, size, "/help", help_ac); inp_replace_input(input, result, size);
_parameter_autocomplete_with_ac(input, size, "/prefs", prefs_ac); g_free(result);
_parameter_autocomplete_with_ac(input, size, "/log", log_ac); return;
_parameter_autocomplete_with_ac(input, size, "/disco", disco_ac); }
_parameter_autocomplete_with_ac(input, size, "/close", close_ac);
_parameter_autocomplete_with_ac(input, size, "/wins", wins_ac);
_who_autocomplete(input, size); gchar *commands[] = { "/help", "/prefs", "/log", "/disco", "/close", "/wins" };
_sub_autocomplete(input, size); Autocomplete completers[] = { help_ac, prefs_ac, log_ac, disco_ac, close_ac, wins_ac };
_notify_autocomplete(input, size);
_autoaway_autocomplete(input, size); for (i = 0; i < ARRAY_SIZE(commands); i++) {
_titlebar_autocomplete(input, size); result = autocomplete_param_with_ac(input, size, commands[i], completers[i]);
_theme_autocomplete(input, size); if (result != NULL) {
_account_autocomplete(input, size); inp_replace_input(input, result, size);
_roster_autocomplete(input, size); g_free(result);
_group_autocomplete(input, size); return;
}
}
result = _who_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _sub_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _notify_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _autoaway_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _titlebar_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _theme_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _account_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _roster_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
result = _group_autocomplete(input, size);
if (result != NULL) {
inp_replace_input(input, result, size);
g_free(result);
return;
}
return;
} }
// The command functions // The command functions
@ -3411,285 +3489,161 @@ _cmd_get_command(const char * const command)
return NULL; return NULL;
} }
static void static char *
_parameter_autocomplete(char *input, int *size, char *command,
autocomplete_func func)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = func(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
}
free(command_cpy);
}
static void
_parameter_autocomplete_with_ac(char *input, int *size, char *command,
Autocomplete ac)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = autocomplete_complete(ac, inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
}
free(command_cpy);
}
static void
_sub_autocomplete(char *input, int *size) _sub_autocomplete(char *input, int *size)
{ {
char *found = NULL; char *result = NULL;
char *auto_msg = NULL; result = autocomplete_param_with_func(input, size, "/sub allow", presence_sub_request_find);
char inp_cpy[*size]; if (result != NULL) {
int i; return result;
if ((strncmp(input, "/sub allow ", 11) == 0) && (*size > 11)) {
for (i = 11; i < *size; i++) {
inp_cpy[i-11] = input[i];
}
inp_cpy[(*size) - 11] = '\0';
found = presence_sub_request_find(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((11 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/sub allow ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/sub deny ", 10) == 0) && (*size > 10)) {
for (i = 10; i < *size; i++) {
inp_cpy[i-10] = input[i];
}
inp_cpy[(*size) - 10] = '\0';
found = presence_sub_request_find(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((10 + (strlen(found) + 0)) * sizeof(char));
strcpy(auto_msg, "/sub deny ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/sub ", 5) == 0) && (*size > 5)) {
_parameter_autocomplete_with_ac(input, size, "/sub", sub_ac);
} }
result = autocomplete_param_with_func(input, size, "/sub deny", presence_sub_request_find);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_ac(input, size, "/sub", sub_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_who_autocomplete(char *input, int *size) _who_autocomplete(char *input, int *size)
{ {
if ((strncmp(input, "/who any ", 9) == 0) && (*size > 9)) { int i = 0;
_parameter_autocomplete(input, size, "/who any", roster_find_group); char *result = NULL;
} else if ((strncmp(input, "/who online ", 12) == 0) && (*size > 12)) { gchar *group_commands[] = { "/who any", "/who online", "/who offline",
_parameter_autocomplete(input, size, "/who online", roster_find_group); "/who chat", "/who away", "/who xa", "/who dnd", "/who available",
} else if ((strncmp(input, "/who offline ", 13) == 0) && (*size > 13)) { "/who unavailable" };
_parameter_autocomplete(input, size, "/who offline", roster_find_group);
} else if ((strncmp(input, "/who chat ", 10) == 0) && (*size > 10)) { for (i = 0; i < ARRAY_SIZE(group_commands); i++) {
_parameter_autocomplete(input, size, "/who chat", roster_find_group); result = autocomplete_param_with_func(input, size, group_commands[i], roster_find_group);
} else if ((strncmp(input, "/who away ", 10) == 0) && (*size > 10)) { if (result != NULL) {
_parameter_autocomplete(input, size, "/who away", roster_find_group); return result;
} else if ((strncmp(input, "/who xa ", 8) == 0) && (*size > 8)) { }
_parameter_autocomplete(input, size, "/who xa", roster_find_group);
} else if ((strncmp(input, "/who dnd ", 9) == 0) && (*size > 9)) {
_parameter_autocomplete(input, size, "/who dnd", roster_find_group);
} else if ((strncmp(input, "/who available ", 15) == 0) && (*size > 15)) {
_parameter_autocomplete(input, size, "/who available", roster_find_group);
} else if ((strncmp(input, "/who unavailable ", 14) == 0) && (*size > 14)) {
_parameter_autocomplete(input, size, "/who unavailable", roster_find_group);
} else if ((strncmp(input, "/who ", 5) == 0) && (*size > 5)) {
_parameter_autocomplete_with_ac(input, size, "/who", who_ac);
} }
result = autocomplete_param_with_ac(input, size, "/who", who_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_roster_autocomplete(char *input, int *size) _roster_autocomplete(char *input, int *size)
{ {
if ((strncmp(input, "/roster nick ", 13) == 0) && (*size > 13)) { char *result = NULL;
_parameter_autocomplete(input, size, "/roster nick", roster_find_jid); result = autocomplete_param_with_func(input, size, "/roster nick", roster_find_jid);
} else if ((strncmp(input, "/roster remove ", 15) == 0) && (*size > 15)) { if (result != NULL) {
_parameter_autocomplete(input, size, "/roster remove", roster_find_jid); return result;
} else if ((strncmp(input, "/roster ", 8) == 0) && (*size > 8)) {
_parameter_autocomplete_with_ac(input, size, "/roster", roster_ac);
} }
result = autocomplete_param_with_func(input, size, "/roster remove", roster_find_jid);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_ac(input, size, "/roster", roster_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_group_autocomplete(char *input, int *size) _group_autocomplete(char *input, int *size)
{ {
if ((strncmp(input, "/group show ", 12) == 0) && (*size > 12)) { char *result = NULL;
_parameter_autocomplete(input, size, "/group show", roster_find_group); result = autocomplete_param_with_func(input, size, "/group show", roster_find_group);
} else if ((strncmp(input, "/group add ", 11) == 0) && (*size > 11)) { if (result != NULL) {
_parameter_autocomplete(input, size, "/group add", roster_find_group); return result;
} else if ((strncmp(input, "/group remove ", 14) == 0) && (*size > 14)) {
_parameter_autocomplete(input, size, "/group remove", roster_find_group);
} else if ((strncmp(input, "/group ", 7) == 0) && (*size > 7)) {
_parameter_autocomplete_with_ac(input, size, "/group", group_ac);
} }
result = autocomplete_param_with_func(input, size, "/group add", roster_find_group);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_func(input, size, "/group remove", roster_find_group);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_ac(input, size, "/group", group_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_notify_autocomplete(char *input, int *size) _notify_autocomplete(char *input, int *size)
{ {
char *found = NULL; int i = 0;
char *auto_msg = NULL; char *result = NULL;
char inp_cpy[*size];
int i;
if ((strncmp(input, "/notify message ", 16) == 0) && (*size > 16)) { gchar *boolean_choices[] = { "/notify message", "/notify typing",
for(i = 16; i < *size; i++) { "/notify invite", "/notify sub" };
inp_cpy[i-16] = input[i]; for (i = 0; i < ARRAY_SIZE(boolean_choices); i++) {
result = autocomplete_param_with_func(input, size, boolean_choices[i],
prefs_autocomplete_boolean_choice);
if (result != NULL) {
return result;
} }
inp_cpy[(*size) - 16] = '\0';
found = prefs_autocomplete_boolean_choice(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((16 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/notify message ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/notify typing ", 15) == 0) && (*size > 15)) {
for(i = 15; i < *size; i++) {
inp_cpy[i-15] = input[i];
}
inp_cpy[(*size) - 15] = '\0';
found = prefs_autocomplete_boolean_choice(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((15 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/notify typing ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/notify invite ", 15) == 0) && (*size > 15)) {
for(i = 15; i < *size; i++) {
inp_cpy[i-15] = input[i];
}
inp_cpy[(*size) - 15] = '\0';
found = prefs_autocomplete_boolean_choice(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((15 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/notify invite ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/notify sub ", 12) == 0) && (*size > 12)) {
for(i = 12; i < *size; i++) {
inp_cpy[i-12] = input[i];
}
inp_cpy[(*size) - 12] = '\0';
found = prefs_autocomplete_boolean_choice(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((12 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/notify sub ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/notify ", 8) == 0) && (*size > 8)) {
_parameter_autocomplete_with_ac(input, size, "/notify", notify_ac);
} }
result = autocomplete_param_with_ac(input, size, "/notify", notify_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_titlebar_autocomplete(char *input, int *size) _titlebar_autocomplete(char *input, int *size)
{ {
char *found = NULL; char *result = NULL;
char *auto_msg = NULL; result = autocomplete_param_with_func(input, size, "/titlebar version",
char inp_cpy[*size]; prefs_autocomplete_boolean_choice);
int i; if (result != NULL) {
return result;
if ((strncmp(input, "/titlebar version ", 18) == 0) && (*size > 18)) {
for(i = 18; i < *size; i++) {
inp_cpy[i-18] = input[i];
}
inp_cpy[(*size) - 18] = '\0';
found = prefs_autocomplete_boolean_choice(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((18 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/titlebar version ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/titlebar ", 10) == 0) && (*size > 10)) {
_parameter_autocomplete_with_ac(input, size, "/titlebar", titlebar_ac);
} }
result = autocomplete_param_with_ac(input, size, "/titlebar", titlebar_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_autoaway_autocomplete(char *input, int *size) _autoaway_autocomplete(char *input, int *size)
{ {
char *found = NULL; char *result = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
if ((strncmp(input, "/autoaway mode ", 15) == 0) && (*size > 15)) { result = autocomplete_param_with_ac(input, size, "/autoaway mode", autoaway_mode_ac);
_parameter_autocomplete_with_ac(input, size, "/autoaway mode", autoaway_mode_ac); if (result != NULL) {
} else if ((strncmp(input, "/autoaway check ", 16) == 0) && (*size > 16)) { return result;
for(i = 16; i < *size; i++) {
inp_cpy[i-16] = input[i];
}
inp_cpy[(*size) - 16] = '\0';
found = prefs_autocomplete_boolean_choice(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((16 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/autoaway check ");
strcat(auto_msg, found);
inp_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
} else if ((strncmp(input, "/autoaway ", 10) == 0) && (*size > 10)) {
_parameter_autocomplete_with_ac(input, size, "/autoaway", autoaway_ac);
} }
result = autocomplete_param_with_func(input, size, "/autoaway check",
prefs_autocomplete_boolean_choice);
if (result != NULL) {
return result;
}
result = autocomplete_param_with_ac(input, size, "/autoaway", autoaway_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_theme_autocomplete(char *input, int *size) _theme_autocomplete(char *input, int *size)
{ {
char *result = NULL;
if ((strncmp(input, "/theme set ", 11) == 0) && (*size > 11)) { if ((strncmp(input, "/theme set ", 11) == 0) && (*size > 11)) {
if (theme_load_ac == NULL) { if (theme_load_ac == NULL) {
theme_load_ac = autocomplete_new(); theme_load_ac = autocomplete_new();
@ -3700,29 +3654,42 @@ _theme_autocomplete(char *input, int *size)
} }
g_slist_free(themes); g_slist_free(themes);
autocomplete_add(theme_load_ac, "default"); autocomplete_add(theme_load_ac, "default");
} }
_parameter_autocomplete_with_ac(input, size, "/theme set", theme_load_ac); result = autocomplete_param_with_ac(input, size, "/theme set", theme_load_ac);
} else if ((strncmp(input, "/theme ", 7) == 0) && (*size > 7)) { if (result != NULL) {
_parameter_autocomplete_with_ac(input, size, "/theme", theme_ac); return result;
}
} }
result = autocomplete_param_with_ac(input, size, "/theme", theme_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static void static char *
_account_autocomplete(char *input, int *size) _account_autocomplete(char *input, int *size)
{ {
if ((strncmp(input, "/account set ", 13) == 0) && (*size > 13)) { char *result = NULL;
_parameter_autocomplete(input, size, "/account set", accounts_find_all); int i = 0;
} else if ((strncmp(input, "/account show ", 14) == 0) && (*size > 14)) { gchar *account_choice[] = { "/account set", "/account show", "/account enable",
_parameter_autocomplete(input, size, "/account show", accounts_find_all); "/account disable", "/account rename" };
} else if ((strncmp(input, "/account enable ", 16) == 0) && (*size > 16)) {
_parameter_autocomplete(input, size, "/account enable", accounts_find_all); for (i = 0; i < ARRAY_SIZE(account_choice); i++) {
} else if ((strncmp(input, "/account disable ", 17) == 0) && (*size > 17)) { result = autocomplete_param_with_func(input, size, account_choice[i],
_parameter_autocomplete(input, size, "/account disable", accounts_find_all); accounts_find_all);
} else if ((strncmp(input, "/account rename ", 16) == 0) && (*size > 16)) { if (result != NULL) {
_parameter_autocomplete(input, size, "/account rename", accounts_find_all); return result;
} else if ((strncmp(input, "/account ", 9) == 0) && (*size > 9)) { }
_parameter_autocomplete_with_ac(input, size, "/account", account_ac);
} }
result = autocomplete_param_with_ac(input, size, "/account", account_ac);
if (result != NULL) {
return result;
}
return NULL;
} }
static int static int

View File

@ -20,6 +20,7 @@
* *
*/ */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -201,6 +202,65 @@ autocomplete_complete(Autocomplete ac, gchar *search_str)
} }
} }
char *
autocomplete_param_with_func(char *input, int *size, char *command,
autocomplete_func func)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = func(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
free(found);
}
}
free(command_cpy);
return auto_msg;
}
char *
autocomplete_param_with_ac(char *input, int *size, char *command,
Autocomplete ac)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = autocomplete_complete(ac, inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
free(found);
}
}
free(command_cpy);
return auto_msg;
}
static gchar * static gchar *
_search_from(Autocomplete ac, GSList *curr) _search_from(Autocomplete ac, GSList *curr)
{ {

View File

@ -25,6 +25,7 @@
#include <glib.h> #include <glib.h>
typedef char*(*autocomplete_func)(char *);
typedef struct autocomplete_t *Autocomplete; typedef struct autocomplete_t *Autocomplete;
typedef const char * (*PStrFunc)(const void *obj); typedef const char * (*PStrFunc)(const void *obj);
typedef void * (*PCopyFunc)(const void *obj); typedef void * (*PCopyFunc)(const void *obj);
@ -42,5 +43,9 @@ gboolean autocomplete_remove(Autocomplete ac, const char * const item);
GSList * autocomplete_get_list(Autocomplete ac); GSList * autocomplete_get_list(Autocomplete ac);
gchar * autocomplete_complete(Autocomplete ac, gchar *search_str); gchar * autocomplete_complete(Autocomplete ac, gchar *search_str);
gint autocomplete_length(Autocomplete ac); gint autocomplete_length(Autocomplete ac);
char * autocomplete_param_with_func(char *input, int *size, char *command,
autocomplete_func func);
char * autocomplete_param_with_ac(char *input, int *size, char *command,
Autocomplete ac);
#endif #endif