diff options
Diffstat (limited to 'src/completion.c')
-rw-r--r-- | src/completion.c | 154 |
1 files changed, 81 insertions, 73 deletions
diff --git a/src/completion.c b/src/completion.c index bb8ee0d..fba9daa 100644 --- a/src/completion.c +++ b/src/completion.c @@ -28,95 +28,99 @@ typedef struct { char* prefix; } Completion; -static GList* completion_init_completion(GList* target, GList* source, +static GList* completion_init_completion(Client* c, GList* target, GList* source, Comp_Func func, const char* input, const char* prefix); -static GList* completion_update(GList* completion, GList* active, gboolean back); -static void completion_show(gboolean back); -static void completion_set_entry_text(Completion* c); -static char* completion_get_text(Completion* c); +static GList* completion_update(Client* c, GList* completion, GList* active, gboolean back); +static void completion_show(Client* c, gboolean back); +static void completion_set_entry_text(Client* c, Completion* completion); +static char* completion_get_text(Client* c, Completion* completion); static Completion* completion_get_new(const char* label, const char* prefix); -gboolean completion_complete(gboolean back) +gboolean completion_complete(Client* c, gboolean back) { - const char* input = GET_TEXT(); + const char* input = gtk_entry_get_text(GTK_ENTRY(c->gui.inputbox)); GList* source = NULL; - if (vp.comps.completions - && vp.comps.active - && (vp.state.mode & VP_MODE_COMPLETE) + if (c->comps.completions + && c->comps.active + && (c->state.mode & VP_MODE_COMPLETE) ) { - char* text = completion_get_text((Completion*)vp.comps.active->data); + char* text = completion_get_text(c, (Completion*)c->comps.active->data); if (!strcmp(input, text)) { /* updatecompletions */ - vp.comps.active = completion_update(vp.comps.completions, vp.comps.active, back); + c->comps.active = completion_update(c, c->comps.completions, c->comps.active, back); return TRUE; } else { /* if current input isn't the content of the completion item */ - completion_clean(); + completion_clean(c); } } /* create new completion */ #ifdef HAS_GTK3 - vp.gui.compbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); - gtk_box_set_homogeneous(GTK_BOX(vp.gui.compbox), TRUE); + c->gui.compbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + gtk_box_set_homogeneous(GTK_BOX(c->gui.compbox), TRUE); #else - vp.gui.compbox = gtk_vbox_new(TRUE, 0); + c->gui.compbox = gtk_vbox_new(TRUE, 0); #endif - gtk_box_pack_start(GTK_BOX(vp.gui.box), vp.gui.compbox, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(c->gui.box), c->gui.compbox, FALSE, FALSE, 0); /* TODO move these decision to a more generic place */ if (!strncmp(input, ":set ", 5)) { source = g_hash_table_get_keys(core.settings); source = g_list_sort(source, (GCompareFunc)g_strcmp0); - vp.comps.completions = completion_init_completion( - vp.comps.completions, source, (Comp_Func)g_str_has_prefix, &input[5], ":set " + c->comps.completions = completion_init_completion( + c, + c->comps.completions, source, (Comp_Func)g_str_has_prefix, &input[5], ":set " ); } else if (!strncmp(input, ":open ", 6)) { source = url_history_get_all(); - vp.comps.completions = completion_init_completion( - vp.comps.completions, source, (Comp_Func)util_strcasestr, &input[6], ":open " + c->comps.completions = completion_init_completion( + c, + c->comps.completions, source, (Comp_Func)util_strcasestr, &input[6], ":open " ); } else if (!strncmp(input, ":tabopen ", 9)) { source = url_history_get_all(); - vp.comps.completions = completion_init_completion( - vp.comps.completions, source, (Comp_Func)util_strcasestr, &input[9], ":tabopen " + c->comps.completions = completion_init_completion( + c, + c->comps.completions, source, (Comp_Func)util_strcasestr, &input[9], ":tabopen " ); } else { source = g_hash_table_get_keys(core.behave.commands); source = g_list_sort(source, (GCompareFunc)g_strcmp0); - vp.comps.completions = completion_init_completion( - vp.comps.completions, source, (Comp_Func)g_str_has_prefix, &input[1], ":" + c->comps.completions = completion_init_completion( + c, + c->comps.completions, source, (Comp_Func)g_str_has_prefix, &input[1], ":" ); } - if (!vp.comps.completions) { + if (!c->comps.completions) { return FALSE; } - completion_show(back); + completion_show(c, back); return TRUE; } -void completion_clean(void) +void completion_clean(Client* c) { - g_list_free_full(vp.comps.completions, (GDestroyNotify)g_free); - vp.comps.completions = NULL; + g_list_free_full(c->comps.completions, (GDestroyNotify)g_free); + c->comps.completions = NULL; - if (vp.gui.compbox) { - gtk_widget_destroy(vp.gui.compbox); - vp.gui.compbox = NULL; + if (c->gui.compbox) { + gtk_widget_destroy(c->gui.compbox); + c->gui.compbox = NULL; } - OVERWRITE_STRING(vp.comps.prefix, NULL); - vp.comps.active = NULL; - vp.comps.count = 0; + OVERWRITE_STRING(c->comps.prefix, NULL); + c->comps.active = NULL; + c->comps.count = 0; /* remove completion flag from mode */ - vp.state.mode &= ~VP_MODE_COMPLETE; + c->state.mode &= ~VP_MODE_COMPLETE; } -static GList* completion_init_completion(GList* target, GList* source, +static GList* completion_init_completion(Client* c, GList* target, GList* source, Comp_Func func, const char* input, const char* prefix) { char* command = NULL; @@ -125,8 +129,8 @@ static GList* completion_init_completion(GList* target, GList* source, char **token = NULL; /* remove counts before command and save it to print it later in inputbox */ - vp.comps.count = g_ascii_strtoll(input, &command, 10); - OVERWRITE_STRING(vp.comps.prefix, prefix); + c->comps.count = g_ascii_strtoll(input, &command, 10); + OVERWRITE_STRING(c->comps.prefix, prefix); token = g_strsplit(command, " ", -1); @@ -146,10 +150,10 @@ static GList* completion_init_completion(GList* target, GList* source, } } if (match) { - Completion* c = completion_get_new(data, prefix); - gtk_box_pack_start(GTK_BOX(vp.gui.compbox), c->event, TRUE, TRUE, 0); + Completion* completion = completion_get_new(data, prefix); + gtk_box_pack_start(GTK_BOX(c->gui.compbox), completion->event, TRUE, TRUE, 0); /* use prepend because that faster */ - target = g_list_prepend(target, c); + target = g_list_prepend(target, completion); } } @@ -159,10 +163,10 @@ static GList* completion_init_completion(GList* target, GList* source, return target; } -static GList* completion_update(GList* completion, GList* active, gboolean back) +static GList* completion_update(Client* c, GList* completion, GList* active, gboolean back) { GList *old, *new; - Completion *c; + Completion *comp; int length = g_list_length(completion); int max = core.config.max_completion_items; @@ -177,10 +181,10 @@ static GList* completion_update(GList* completion, GList* active, gboolean back) new = g_list_last(completion); } if (position - 1 > offset && position < items - offset + r) { - c = g_list_nth(completion, position - offset - 2)->data; - gtk_widget_show_all(c->event); - c = g_list_nth(completion, position + offset - r)->data; - gtk_widget_hide(c->event); + comp = g_list_nth(completion, position - offset - 2)->data; + gtk_widget_show_all(comp->event); + comp = g_list_nth(completion, position + offset - r)->data; + gtk_widget_hide(comp->event); } else if (position == 1) { int i = 0; for (GList *l = g_list_first(completion); l && i < max; l = l->next, i++) { @@ -188,8 +192,8 @@ static GList* completion_update(GList* completion, GList* active, gboolean back) } i = 0; for (GList *l = g_list_last(completion); l && i < max; l = l->prev, i++) { - c = l->data; - gtk_widget_show_all(c->event); + comp = l->data; + gtk_widget_show_all(comp->event); } } } else { @@ -197,10 +201,10 @@ static GList* completion_update(GList* completion, GList* active, gboolean back) new = g_list_first(completion); } if (position > offset && position < items - offset - 1 + r) { - c = g_list_nth(completion, position - offset - 1)->data; - gtk_widget_hide(c->event); - c = g_list_nth(completion, position + offset + 1 - r)->data; - gtk_widget_show_all(c->event); + comp = g_list_nth(completion, position - offset - 1)->data; + gtk_widget_hide(comp->event); + comp = g_list_nth(completion, position + offset + 1 - r)->data; + gtk_widget_show_all(comp->event); } else if (position == items || position == 1) { int i = 0; for (GList *l = g_list_last(completion); l && i < max; l = l->prev, i++) { @@ -219,56 +223,60 @@ static GList* completion_update(GList* completion, GList* active, gboolean back) VP_WIDGET_SET_STATE(((Completion*)new->data)->event, VP_GTK_STATE_ACTIVE); active = new; - completion_set_entry_text(active->data); + completion_set_entry_text(c, active->data); return active; } /* allow to chenge the direction of display */ -static void completion_show(gboolean back) +static void completion_show(Client* c, gboolean back) { guint max = core.config.max_completion_items; int i = 0; if (back) { - vp.comps.active = g_list_last(vp.comps.completions); - for (GList *l = vp.comps.active; l && i < max; l = l->prev, i++) { + c->comps.active = g_list_last(c->comps.completions); + for (GList *l = c->comps.active; l && i < max; l = l->prev, i++) { gtk_widget_show_all(((Completion*)l->data)->event); } } else { - vp.comps.active = g_list_first(vp.comps.completions); - for (GList *l = vp.comps.active; l && i < max; l = l->next, i++) { + c->comps.active = g_list_first(c->comps.completions); + for (GList *l = c->comps.active; l && i < max; l = l->next, i++) { gtk_widget_show_all(((Completion*)l->data)->event); } } - if (vp.comps.active != NULL) { - Completion* active = (Completion*)vp.comps.active->data; + if (c->comps.active != NULL) { + Completion* active = (Completion*)c->comps.active->data; VP_WIDGET_SET_STATE(active->label, VP_GTK_STATE_ACTIVE); VP_WIDGET_SET_STATE(active->event, VP_GTK_STATE_ACTIVE); - completion_set_entry_text(active); - gtk_widget_show(vp.gui.compbox); + completion_set_entry_text(c, active); + gtk_widget_show(c->gui.compbox); } } -static void completion_set_entry_text(Completion* c) +static void completion_set_entry_text(Client* c, Completion* completion) { - char* text = completion_get_text(c); - gtk_entry_set_text(GTK_ENTRY(vp.gui.inputbox), text); - gtk_editable_set_position(GTK_EDITABLE(vp.gui.inputbox), -1); + char* text = completion_get_text(c, completion); + gtk_entry_set_text(GTK_ENTRY(c->gui.inputbox), text); + gtk_editable_set_position(GTK_EDITABLE(c->gui.inputbox), -1); g_free(text); } /** * Retrieves the full new allocated entry text for given completion item. */ -static char* completion_get_text(Completion* c) +static char* completion_get_text(Client* c, Completion* completion) { char* text = NULL; /* print the previous typed command count into inputbox too */ - if (vp.comps.count) { - text = g_strdup_printf("%s%d%s", c->prefix, vp.comps.count, gtk_label_get_text(GTK_LABEL(c->label))); + if (c->comps.count) { + text = g_strdup_printf( + "%s%d%s", completion->prefix, c->comps.count, gtk_label_get_text(GTK_LABEL(completion->label)) + ); } else { - text = g_strdup_printf("%s%s", c->prefix, gtk_label_get_text(GTK_LABEL(c->label))); + text = g_strdup_printf( + "%s%s", completion->prefix, gtk_label_get_text(GTK_LABEL(completion->label)) + ); } return text; |