diff --git a/moo/mooedit/Makefile.am b/moo/mooedit/Makefile.am index da2faa64..2215729f 100644 --- a/moo/mooedit/Makefile.am +++ b/moo/mooedit/Makefile.am @@ -54,6 +54,7 @@ completion_sources = \ mooedit_sources = \ $(mooedit_include_headers) \ $(moocommand_stuff) \ + $(completion_sources) \ moocmdview.c \ moocommand.c \ moocommanddisplay.c \ @@ -207,7 +208,6 @@ BUILT_SOURCES += mooedit-ui.h medit-ui.h CLEANFILES += mooedit-ui.h medit-ui.h EXTRA_DIST += \ - $(completion_sources) \ $(mooedit_tools_DATA) \ $(mooedit_lua_DATA) \ medit-ui.xml \ diff --git a/moo/mooedit/moocompletionsimple.c b/moo/mooedit/moocompletionsimple.c index 1dbc973b..1edb21eb 100644 --- a/moo/mooedit/moocompletionsimple.c +++ b/moo/mooedit/moocompletionsimple.c @@ -12,10 +12,8 @@ #include "mooedit/moocompletionsimple.h" #include "mooedit/mootextpopup.h" -#include "mooedit/mooedit-script.h" #include "mooutils/moomarshals.h" -#include "mooutils/eggregex.h" -#include "mooscript/mooscript-parser.h" +#include #include #include #include @@ -50,14 +48,14 @@ struct _MooCompletionSimplePrivate { struct _MooCompletionGroup { char *name; - EggRegex *regex; + GRegex *regex; guint *parens; guint n_parens; GCompletion *cmpl; GList *data; char *suffix; - MSNode *script; + gpointer script; MooCompletionFreeFunc free_func; }; @@ -230,36 +228,34 @@ list_sort_func (GtkTreeModel *model, static void -moo_completion_simple_exec_script (MooCompletionSimple *cmpl, - MooCompletionGroup *group, - GtkTextIter *start, - GtkTextIter *end, - const char *completion) +moo_completion_simple_exec_script (G_GNUC_UNUSED MooCompletionSimple *cmpl, + G_GNUC_UNUSED MooCompletionGroup *group, + G_GNUC_UNUSED GtkTextIter *start, + G_GNUC_UNUSED GtkTextIter *end, + G_GNUC_UNUSED const char *completion) { - MSContext *ctx; - char *match; - MSValue *result; - GtkTextView *doc; - - doc = moo_text_completion_get_doc (MOO_TEXT_COMPLETION (cmpl)); - ctx = moo_edit_script_context_new (doc, NULL); - match = gtk_text_iter_get_slice (start, end); - - ms_context_assign_string (ctx, MOO_COMPLETION_VAR_MATCH, match); - ms_context_assign_string (ctx, MOO_COMPLETION_VAR_COMPLETION, completion); - - gtk_text_buffer_delete (get_buffer (cmpl), start, end); - gtk_text_buffer_place_cursor (get_buffer (cmpl), start); - result = ms_top_node_eval (group->script, ctx); - - if (result) - ms_value_unref (result); - else - g_warning ("%s: %s", G_STRLOC, - ms_context_get_error_msg (ctx)); - - g_free (match); - g_object_unref (ctx); +// char *match; +// GtkTextView *doc; +// +// doc = moo_text_completion_get_doc (MOO_TEXT_COMPLETION (cmpl)); +// ctx = moo_edit_script_context_new (doc, NULL); +// match = gtk_text_iter_get_slice (start, end); +// +// ms_context_assign_string (ctx, MOO_COMPLETION_VAR_MATCH, match); +// ms_context_assign_string (ctx, MOO_COMPLETION_VAR_COMPLETION, completion); +// +// gtk_text_buffer_delete (get_buffer (cmpl), start, end); +// gtk_text_buffer_place_cursor (get_buffer (cmpl), start); +// result = ms_top_node_eval (group->script, ctx); +// +// if (result) +// ms_value_unref (result); +// else +// g_warning ("%s: %s", G_STRLOC, +// ms_context_get_error_msg (ctx)); +// +// g_free (match); +// g_object_unref (ctx); } @@ -286,18 +282,16 @@ moo_completion_simple_complete (MooTextCompletion *text_cmpl, moo_text_completion_get_region (MOO_TEXT_COMPLETION (cmpl), &start, &end); old_text = gtk_text_iter_get_slice (&start, &end); - gtk_text_buffer_begin_user_action (get_buffer (cmpl)); - if (group->script) { moo_completion_simple_exec_script (cmpl, group, &start, &end, text); } else { - if (strcmp (text, old_text)) + if (strcmp (text, old_text) != 0) { - gtk_text_buffer_delete (get_buffer (cmpl), &start, &end); - gtk_text_buffer_insert (get_buffer (cmpl), &start, text, -1); + _moo_text_completion_replace_text (MOO_TEXT_COMPLETION (cmpl), + &start, &end, text); set_cursor = TRUE; } @@ -324,7 +318,7 @@ moo_completion_simple_complete (MooTextCompletion *text_cmpl, if (do_insert) { - gtk_text_buffer_insert (get_buffer (cmpl), &start, group->suffix, -1); + _moo_text_completion_replace_text (text_cmpl, &start, &start, group->suffix); set_cursor = TRUE; } } @@ -333,10 +327,6 @@ moo_completion_simple_complete (MooTextCompletion *text_cmpl, if (set_cursor) gtk_text_buffer_place_cursor (get_buffer (cmpl), &start); - gtk_text_buffer_end_user_action (get_buffer (cmpl)); - - moo_text_completion_hide (MOO_TEXT_COMPLETION (cmpl)); - g_free (old_text); } @@ -534,6 +524,19 @@ moo_completion_group_add_data (MooCompletionGroup *group, } +void +moo_completion_group_remove_data (MooCompletionGroup *group, + GList *data) +{ + g_return_if_fail (group != NULL); + + if (!data) + return; + + g_completion_remove_items (group->cmpl, data); +} + + static gboolean moo_completion_group_find (MooCompletionGroup *group, const char *line, @@ -554,8 +557,8 @@ moo_completion_group_find (MooCompletionGroup *group, { int start_pos = -1, end_pos = -1; - g_regex_fetch_pos (match_info, group->parens[i], - &start_pos, &end_pos); + g_match_info_fetch_pos (match_info, group->parens[i], + &start_pos, &end_pos); if (start_pos >= 0 && end_pos >= 0) { @@ -599,7 +602,7 @@ moo_completion_group_set_pattern (MooCompletionGroup *group, const guint *parens, guint n_parens) { - EggRegex *regex; + GRegex *regex; GError *error = NULL; char *real_pattern; @@ -617,7 +620,8 @@ moo_completion_group_set_pattern (MooCompletionGroup *group, goto err; } - g_regex_free (group->regex); + if (group->regex) + g_regex_unref (group->regex); group->regex = regex; g_free (group->parens); @@ -639,8 +643,9 @@ moo_completion_group_set_pattern (MooCompletionGroup *group, err: if (error) g_error_free (error); + if (regex) + g_regex_unref (regex); g_free (real_pattern); - g_regex_free (regex); } @@ -659,19 +664,19 @@ moo_completion_group_set_suffix (MooCompletionGroup *group, void -moo_completion_group_set_script (MooCompletionGroup *group, - const char *script) +moo_completion_group_set_script (G_GNUC_UNUSED MooCompletionGroup *group, + G_GNUC_UNUSED const char *script) { - g_return_if_fail (group != NULL); - - if (group->script) - { - ms_node_unref (group->script); - group->script = NULL; - } - - if (script) - group->script = ms_script_parse (script); +// g_return_if_fail (group != NULL); +// +// if (group->script) +// { +// ms_node_unref (group->script); +// group->script = NULL; +// } +// +// if (script) +// group->script = ms_script_parse (script); } @@ -680,14 +685,15 @@ moo_completion_group_free (MooCompletionGroup *group) { g_return_if_fail (group != NULL); - g_regex_free (group->regex); + if (group->regex) + g_regex_unref (group->regex); g_free (group->parens); g_completion_free (group->cmpl); g_free (group->suffix); g_free (group->name); - if (group->script) - ms_node_unref (group->script); +// if (group->script) +// ms_node_unref (group->script); if (group->free_func) g_list_foreach (group->data, (GFunc) group->free_func, NULL); diff --git a/moo/mooedit/moocompletionsimple.h b/moo/mooedit/moocompletionsimple.h index ede2d600..5922d210 100644 --- a/moo/mooedit/moocompletionsimple.h +++ b/moo/mooedit/moocompletionsimple.h @@ -30,7 +30,7 @@ G_BEGIN_DECLS typedef struct _MooCompletionSimple MooCompletionSimple; typedef struct _MooCompletionSimplePrivate MooCompletionSimplePrivate; typedef struct _MooCompletionSimpleClass MooCompletionSimpleClass; -typedef struct _MooCompletionGroup MooCompletionGroup; +typedef struct _MooCompletionGroup MooCompletionGroup; struct _MooCompletionSimple { @@ -49,6 +49,8 @@ GType moo_completion_simple_get_type (void) G_GNUC_CONST; /* steals data */ void moo_completion_group_add_data (MooCompletionGroup *group, GList *data); +void moo_completion_group_remove_data (MooCompletionGroup *group, + GList *data); void moo_completion_group_set_pattern (MooCompletionGroup *group, const char *pattern, diff --git a/moo/mooedit/mootextcompletion.c b/moo/mooedit/mootextcompletion.c index 646a84ce..d9686011 100644 --- a/moo/mooedit/mootextcompletion.c +++ b/moo/mooedit/mootextcompletion.c @@ -48,11 +48,12 @@ static void moo_text_completion_populate (MooTextCompletion *cmp const char *text); static void moo_text_completion_complete (MooTextCompletion *cmpl, GtkTreeIter *iter); -static char *moo_text_completion_get_text (MooTextCompletion *cmpl, - GtkTreeModel *model, - GtkTreeIter *iter); static void moo_text_completion_update (MooTextCompletion *cmpl); static void moo_text_completion_update_real (MooTextCompletion *cmpl); +static void moo_text_completion_replace_text_real (MooTextCompletion *cmpl, + GtkTextIter *start, + GtkTextIter *end, + const char *text); static gboolean moo_text_completion_empty (MooTextCompletion *cmpl); static gboolean moo_text_completion_unique (MooTextCompletion *cmpl, @@ -93,6 +94,7 @@ moo_text_completion_class_init (MooTextCompletionClass *klass) klass->try_complete = moo_text_completion_try_complete_real; klass->update = moo_text_completion_update_real; klass->complete = moo_text_completion_complete_real; + klass->replace_text = moo_text_completion_replace_text_real; signals[FINISH] = g_signal_new ("finish", G_TYPE_FROM_CLASS (klass), @@ -243,12 +245,7 @@ moo_text_completion_try_complete_real (MooTextCompletion *cmpl, prefix = find_common_prefix (cmpl, text); if (prefix && strcmp (text, prefix) != 0) - { - gtk_text_buffer_begin_user_action (cmpl->priv->buffer); - gtk_text_buffer_delete (cmpl->priv->buffer, &start, &end); - gtk_text_buffer_insert (cmpl->priv->buffer, &start, prefix, -1); - gtk_text_buffer_end_user_action (cmpl->priv->buffer); - } + _moo_text_completion_replace_text (cmpl, &start, &end, prefix); } moo_text_completion_show (cmpl); @@ -266,13 +263,42 @@ finish: return; } +void +_moo_text_completion_replace_text (MooTextCompletion *cmpl, + GtkTextIter *start, + GtkTextIter *end, + const char *text) +{ + g_return_if_fail (MOO_IS_TEXT_COMPLETION (cmpl)); + g_return_if_fail (start != NULL && end != NULL); + + gtk_text_buffer_begin_user_action (cmpl->priv->buffer); + MOO_TEXT_COMPLETION_GET_CLASS (cmpl)->replace_text (cmpl, start, end, text); + gtk_text_buffer_end_user_action (cmpl->priv->buffer); +} + +static void +moo_text_completion_replace_text_real (MooTextCompletion *cmpl, + GtkTextIter *start, + GtkTextIter *end, + const char *text) +{ + if (!gtk_text_iter_equal (start, end)) + gtk_text_buffer_delete (cmpl->priv->buffer, start, end); + if (text && text[0]) + gtk_text_buffer_insert (cmpl->priv->buffer, start, text, -1); +} static void moo_text_completion_complete (MooTextCompletion *cmpl, GtkTreeIter *iter) { g_return_if_fail (MOO_TEXT_COMPLETION_GET_CLASS(cmpl)->complete != NULL); + + gtk_text_buffer_begin_user_action (cmpl->priv->buffer); MOO_TEXT_COMPLETION_GET_CLASS(cmpl)->complete (cmpl, cmpl->priv->model, iter); + gtk_text_buffer_end_user_action (cmpl->priv->buffer); + moo_text_completion_finish (cmpl); } @@ -292,31 +318,28 @@ moo_text_completion_complete_real (MooTextCompletion *cmpl, old_text = gtk_text_buffer_get_slice (cmpl->priv->buffer, &start, &end, TRUE); - gtk_text_buffer_begin_user_action (cmpl->priv->buffer); - - if (strcmp (text, old_text)) + if (strcmp (text, old_text) != 0) { - gtk_text_buffer_delete (cmpl->priv->buffer, &start, &end); - gtk_text_buffer_insert (cmpl->priv->buffer, &start, text, -1); + _moo_text_completion_replace_text (cmpl, &start, &end, text); set_cursor = TRUE; } if (set_cursor) gtk_text_buffer_place_cursor (cmpl->priv->buffer, &start); - gtk_text_buffer_end_user_action (cmpl->priv->buffer); - - moo_text_completion_hide (cmpl); - g_free (old_text); + g_free (text); } -static char * +char * moo_text_completion_get_text (MooTextCompletion *cmpl, GtkTreeModel *model, GtkTreeIter *iter) { + g_return_val_if_fail (MOO_IS_TEXT_COMPLETION (cmpl), NULL); + g_return_val_if_fail (model == cmpl->priv->model, NULL); + g_return_val_if_fail (iter != NULL, NULL); g_return_val_if_fail (cmpl->priv->text_func != NULL, NULL); return cmpl->priv->text_func (model, iter, cmpl->priv->text_func_data); } diff --git a/moo/mooedit/mootextcompletion.h b/moo/mooedit/mootextcompletion.h index 8c34a460..2e5cd87e 100644 --- a/moo/mooedit/mootextcompletion.h +++ b/moo/mooedit/mootextcompletion.h @@ -53,6 +53,11 @@ struct _MooTextCompletionClass GtkTreeModel *model, GtkTextIter *cursor, const char *text); + + void (*replace_text) (MooTextCompletion *cmpl, + GtkTextIter *start, + GtkTextIter *end, + const char *text); }; typedef char *(*MooTextCompletionTextFunc) (GtkTreeModel *model, @@ -89,8 +94,17 @@ void moo_text_completion_set_text_func (MooTextCompletion *cmpl, gpointer data, GDestroyNotify notify); +char *moo_text_completion_get_text (MooTextCompletion *cmpl, + GtkTreeModel *model, + GtkTreeIter *iter); + MooTextPopup *moo_text_completion_get_popup (MooTextCompletion *cmpl); +void _moo_text_completion_replace_text (MooTextCompletion *cmpl, + GtkTextIter *start, + GtkTextIter *end, + const char *text); + G_END_DECLS