diff --git a/moo/mooedit/context.xml b/moo/mooedit/context.xml index fbcc888a..5ba8ec1c 100644 --- a/moo/mooedit/context.xml +++ b/moo/mooedit/context.xml @@ -1,5 +1,5 @@ - + C, GAP extensions = [[['.h', '.hh', '.hpp', '.hxx', '.H'], ['.c', '.cc', '.cpp', '.cxx', '.C']], diff --git a/moo/mooedit/glade/mooedittools.glade b/moo/mooedit/glade/mooedittools.glade index 367ff5a8..627345c3 100644 --- a/moo/mooedit/glade/mooedittools.glade +++ b/moo/mooedit/glade/mooedittools.glade @@ -414,60 +414,6 @@ GTK_POS_TOP False False - - - - - - - - True - - False - False - GTK_JUSTIFY_LEFT - False - False - 0.5 - 0.5 - 0 - 0 - PANGO_ELLIPSIZE_NONE - -1 - False - 0 - - - tab - - - - - - - - - - True - - False - False - GTK_JUSTIFY_LEFT - False - False - 0.5 - 0.5 - 0 - 0 - PANGO_ELLIPSIZE_NONE - -1 - False - 0 - - - tab - - diff --git a/moo/mooedit/moocommand-script.c b/moo/mooedit/moocommand-script.c index 70bb3c1e..143054e2 100644 --- a/moo/mooedit/moocommand-script.c +++ b/moo/mooedit/moocommand-script.c @@ -11,13 +11,16 @@ * See COPYING file that comes with this distribution. */ +#define MOOEDIT_COMPILATION #include "mooedit/moocommand-script.h" #include "mooedit/mooedit-script.h" #include "mooedit/mooedittools-glade.h" #include "mooedit/mooeditor.h" +#include "mooedit/mootext-private.h" #include "mooscript/mooscript-parser.h" #include "mooutils/mooi18n.h" #include "mooutils/mooglade.h" +#include "mooutils/mooundo.h" #include @@ -53,6 +56,8 @@ static void moo_command_script_run (MooCommand *cmd_base, MooCommandContext *ctx) { + gpointer doc; + MooUndoStack *undo_stack = NULL; MSValue *ret; MSContext *script_ctx; MooCommandScript *cmd = MOO_COMMAND_SCRIPT (cmd_base); @@ -65,8 +70,21 @@ moo_command_script_run (MooCommand *cmd_base, moo_command_context_foreach (ctx, set_variable, script_ctx); + doc = moo_command_context_get_doc (ctx); + if (MOO_IS_TEXT_VIEW (doc)) + { + MooTextBuffer *buffer = MOO_TEXT_BUFFER (gtk_text_view_get_buffer (doc)); + undo_stack = _moo_text_buffer_get_undo_stack (buffer); + } + + if (undo_stack) + moo_undo_stack_start_group (undo_stack); + ret = ms_top_node_eval (cmd->priv->script, script_ctx); + if (undo_stack) + moo_undo_stack_end_group (undo_stack); + if (!ret) { g_print ("%s\n", ms_context_get_error_msg (script_ctx)); diff --git a/moo/mooedit/moocommand.c b/moo/mooedit/moocommand.c index 4cd99623..4ac261aa 100644 --- a/moo/mooedit/moocommand.c +++ b/moo/mooedit/moocommand.c @@ -515,6 +515,27 @@ moo_command_context_set (MooCommandContext *ctx, } +void +moo_command_context_set_string (MooCommandContext *ctx, + const char *name, + const char *value) +{ + if (value) + { + GValue gval; + gval.g_type = 0; + g_value_init (&gval, G_TYPE_STRING); + g_value_set_string (&gval, value); + moo_command_context_set (ctx, name, &gval); + g_value_unset (&gval); + } + else + { + moo_command_context_unset (ctx, name); + } +} + + gboolean moo_command_context_get (MooCommandContext *ctx, const char *name, @@ -525,6 +546,7 @@ moo_command_context_get (MooCommandContext *ctx, g_return_val_if_fail (MOO_IS_COMMAND_CONTEXT (ctx), FALSE); g_return_val_if_fail (name != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); + g_return_val_if_fail (G_VALUE_TYPE (value) == 0, FALSE); var = g_hash_table_lookup (ctx->priv->vars, name); @@ -537,6 +559,25 @@ moo_command_context_get (MooCommandContext *ctx, } +const char * +moo_command_context_get_string (MooCommandContext *ctx, + const char *name) +{ + Variable *var; + + g_return_val_if_fail (MOO_IS_COMMAND_CONTEXT (ctx), NULL); + g_return_val_if_fail (name != NULL, NULL); + + var = g_hash_table_lookup (ctx->priv->vars, name); + + if (!var) + return NULL; + + g_return_val_if_fail (G_VALUE_TYPE (&var->value) == G_TYPE_STRING, NULL); + return g_value_get_string (&var->value); +} + + void moo_command_context_unset (MooCommandContext *ctx, const char *name) diff --git a/moo/mooedit/moocommand.h b/moo/mooedit/moocommand.h index 5a501032..5358337b 100644 --- a/moo/mooedit/moocommand.h +++ b/moo/mooedit/moocommand.h @@ -173,6 +173,11 @@ gboolean moo_command_context_get (MooCommandContext *ctx, GValue *value); void moo_command_context_unset (MooCommandContext *ctx, const char *name); +void moo_command_context_set_string (MooCommandContext *ctx, + const char *name, + const char *value); +const char *moo_command_context_get_string (MooCommandContext *ctx, + const char *name); typedef void (*MooCommandContextForeachFunc) (const char *name, const GValue *value, diff --git a/moo/mooedit/moousertools-prefs.c b/moo/mooedit/moousertools-prefs.c index 3d1bca24..6a393b9b 100644 --- a/moo/mooedit/moousertools-prefs.c +++ b/moo/mooedit/moousertools-prefs.c @@ -66,25 +66,28 @@ get_helper (MooPrefsDialogPage *page) } -static void -tool_parse_func (MooUserToolInfo *info, - gpointer data) -{ - GtkTreeIter iter; - GtkListStore *store = data; - - if (info->os_type == MOO_USER_TOOL_THIS_OS) - { - gtk_list_store_append (store, &iter); - gtk_list_store_set (store, &iter, COLUMN_INFO, info, -1); - } -} - static void populate_store (GtkListStore *store, MooUserToolType type) { - _moo_edit_parse_user_tools (type, tool_parse_func, store); + GtkTreeIter iter; + GSList *list; + + list = _moo_edit_parse_user_tools (type); + + while (list) + { + MooUserToolInfo *info = list->data; + + if (info->os_type == MOO_USER_TOOL_THIS_OS) + { + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, COLUMN_INFO, info, -1); + } + + _moo_user_tool_info_unref (info); + list = g_slist_delete_link (list, list); + } } @@ -95,6 +98,7 @@ new_row (MooPrefsDialogPage *page, { GtkTreeIter iter; MooUserToolInfo *info; + GtkTreeViewColumn *column; info = _moo_user_tool_info_new (); info->cmd_data = moo_command_data_new (); @@ -107,6 +111,9 @@ new_row (MooPrefsDialogPage *page, gtk_tree_path_get_indices(path)[0]); gtk_list_store_set (GTK_LIST_STORE (model), &iter, COLUMN_INFO, info, -1); + column = gtk_tree_view_get_column (GET_WID ("treeview"), 0); + gtk_tree_view_set_cursor (GET_WID ("treeview"), path, column, TRUE); + _moo_user_tool_info_unref (info); return TRUE; } @@ -243,6 +250,35 @@ name_data_func (G_GNUC_UNUSED GtkTreeViewColumn *column, _moo_user_tool_info_unref (info); } +static void +name_cell_edited (MooPrefsDialogPage *page, + char *path_string, + char *text) +{ + GtkTreeModel *model; + GtkTreePath *path; + GtkTreeIter iter; + MooUserToolInfo *info = NULL; + + path = gtk_tree_path_new_from_string (path_string); + model = gtk_tree_view_get_model (GET_WID ("treeview")); + + if (gtk_tree_model_get_iter (model, &iter, path)) + { + gtk_tree_model_get (model, &iter, COLUMN_INFO, &info, -1); + g_return_if_fail (info != NULL); + + if (strcmp (info->name ? info->name : "", text) != 0) + { + set_changed (page, TRUE); + g_free (info->name); + info->name = g_strdup (text); + } + } + + gtk_tree_path_free (path); +} + static void command_page_init (MooPrefsDialogPage *page, MooUserToolType type) @@ -272,6 +308,8 @@ command_page_init (MooPrefsDialogPage *page, (GtkTreeCellDataFunc) name_data_func, NULL, NULL); gtk_tree_view_append_column (treeview, column); + g_object_set (cell, "editable", TRUE, NULL); + g_signal_connect_swapped (cell, "edited", G_CALLBACK (name_cell_edited), page); populate_store (store, type); diff --git a/moo/mooedit/moousertools.c b/moo/mooedit/moousertools.c index 979f70d0..403e4871 100644 --- a/moo/mooedit/moousertools.c +++ b/moo/mooedit/moousertools.c @@ -256,8 +256,7 @@ check_info (MooUserToolInfo *info, } static void -load_tool_func (MooUserToolInfo *info, - G_GNUC_UNUSED gpointer user_data) +load_tool (MooUserToolInfo *info) { ToolInfo *tool_info; gpointer klass = NULL; @@ -376,16 +375,24 @@ load_tool_func (MooUserToolInfo *info, void _moo_edit_load_user_tools (MooUserToolType type) { + GSList *list; + unload_user_tools (type); - _moo_edit_parse_user_tools (type, load_tool_func, NULL); + + list = _moo_edit_parse_user_tools (type); + + while (list) + { + load_tool (list->data); + _moo_user_tool_info_unref (list->data); + list = g_slist_delete_link (list, list); + } } -static void +static MooUserToolInfo * parse_element (MooMarkupNode *node, MooUserToolType type, - MooToolFileParseFunc func, - gpointer data, const char *file) { const char *os; @@ -393,17 +400,16 @@ parse_element (MooMarkupNode *node, MooMarkupNode *cmd_node, *child; MooUserToolInfo *info; + if (strcmp (node->name, ELEMENT_TOOL)) + { + g_warning ("invalid element %s in file %s", node->name, file); + return NULL; + } + info = _moo_user_tool_info_new (); info->type = type; info->file = g_strdup (file); info->position = MOO_USER_TOOL_POS_END; - - if (strcmp (node->name, ELEMENT_TOOL)) - { - g_warning ("invalid element %s in file %s", node->name, file); - return; - } - info->name = g_strdup (moo_markup_get_prop (node, PROP_NAME)); info->enabled = moo_markup_get_bool_prop (node, PROP_ENABLED, TRUE); @@ -427,7 +433,7 @@ parse_element (MooMarkupNode *node, g_warning ("duplicated element '%s' in tool %s in file %s", child->name, info->name, file); _moo_user_tool_info_unref (info); - return; + return NULL; } info->filter = g_strdup (moo_markup_get_content (child)); @@ -439,7 +445,7 @@ parse_element (MooMarkupNode *node, g_warning ("duplicated element '%s' in tool %s in file %s", child->name, info->name, file); _moo_user_tool_info_unref (info); - return; + return NULL; } info->accel = g_strdup (moo_markup_get_content (child)); @@ -455,7 +461,7 @@ parse_element (MooMarkupNode *node, g_warning ("duplicated element '%s' in tool %s in file %s", child->name, info->name, file); _moo_user_tool_info_unref (info); - return; + return NULL; } info->menu = g_strdup (moo_markup_get_content (child)); @@ -467,7 +473,7 @@ parse_element (MooMarkupNode *node, g_warning ("duplicated element '%s' in tool %s in file %s", child->name, info->name, file); _moo_user_tool_info_unref (info); - return; + return NULL; } info->langs = g_strdup (moo_markup_get_content (child)); @@ -496,7 +502,7 @@ parse_element (MooMarkupNode *node, { g_warning ("command missing for tool '%s' in file %s", info->name, file); _moo_user_tool_info_unref (info); - return; + return NULL; } info->cmd_data = _moo_command_parse_markup (cmd_node, &info->cmd_type, &info->options); @@ -505,62 +511,64 @@ parse_element (MooMarkupNode *node, { g_warning ("could not get command data for tool '%s' in file %s", info->name, file); _moo_user_tool_info_unref (info); - return; + return NULL; } - func (info, data); - - _moo_user_tool_info_unref (info); + return info; } -static void +static GSList * parse_doc (MooMarkupDoc *doc, - MooUserToolType type, - MooToolFileParseFunc func, - gpointer data) + MooUserToolType type) { MooMarkupNode *root, *child; + GSList *list = NULL; root = moo_markup_get_root_element (doc, ELEMENT_TOOLS); if (!root) { g_warning ("no '" ELEMENT_TOOLS "' element in file '%s'", doc->name); - return; + return NULL; } for (child = root->children; child != NULL; child = child->next) { if (MOO_MARKUP_IS_ELEMENT (child)) - parse_element (child, type, func, data, doc->name); + { + MooUserToolInfo *info = parse_element (child, type, doc->name); + + if (info) + list = g_slist_prepend (list, info); + } } + + return g_slist_reverse (list); } -void -_moo_edit_parse_user_tools (MooUserToolType type, - MooToolFileParseFunc func, - gpointer data) +GSList * +_moo_edit_parse_user_tools (MooUserToolType type) { char *file; MooMarkupDoc *doc; GError *error = NULL; + GSList *list = NULL; - g_return_if_fail (type < N_TOOLS); - g_return_if_fail (func != NULL); + g_return_val_if_fail (type < N_TOOLS, NULL); _moo_command_init (); file = find_user_tools_file (type); if (!file) - return; + return NULL; doc = moo_markup_parse_file (file, &error); if (doc) { - parse_doc (doc, type, func, data); + list = parse_doc (doc, type); moo_markup_doc_unref (doc); } else @@ -570,6 +578,7 @@ _moo_edit_parse_user_tools (MooUserToolType type, } g_free (file); + return list; } @@ -821,13 +830,63 @@ _moo_tool_action_class_init (MooToolActionClass *klass) } +static void +get_extension (const char *string, + char **base, + char **ext) +{ + char *dot; + + g_return_if_fail (string != NULL); + + dot = strrchr (string, '.'); + + if (dot) + { + *base = g_strndup (string, dot - string); + *ext = g_strdup (dot); + } + else + { + *base = g_strdup (string); + *ext = g_strdup (""); + } +} + static MooCommandContext * create_command_context (gpointer window, gpointer doc) { MooCommandContext *ctx; + char *user_dir; ctx = moo_command_context_new (doc, window); + if (MOO_IS_EDIT (doc) && moo_edit_get_filename (doc)) + { + const char *filename, *basename; + char *dirname, *base, *extension; + + filename = moo_edit_get_filename (doc); + basename = moo_edit_get_basename (doc); + dirname = g_path_get_dirname (filename); + get_extension (basename, &base, &extension); + + moo_command_context_set_string (ctx, "DOC", filename); + moo_command_context_set_string (ctx, "DOC_DIR", dirname); + moo_command_context_set_string (ctx, "DOC_BASE", base); + moo_command_context_set_string (ctx, "DOC_EXT", extension); + + g_free (dirname); + g_free (base); + g_free (extension); + } + + user_dir = moo_get_user_data_dir (); + + moo_command_context_set_string (ctx, "DATA_DIR", user_dir); + moo_command_context_set_string (ctx, "APP_PID", _moo_get_pid_string ()); + + g_free (user_dir); return ctx; } diff --git a/moo/mooedit/moousertools.h b/moo/mooedit/moousertools.h index bbc65286..13748ba7 100644 --- a/moo/mooedit/moousertools.h +++ b/moo/mooedit/moousertools.h @@ -71,9 +71,8 @@ void _moo_edit_load_user_tools (MooUserToolType type); typedef void (*MooToolFileParseFunc) (MooUserToolInfo *info, gpointer data); -void _moo_edit_parse_user_tools (MooUserToolType type, - MooToolFileParseFunc func, - gpointer data); +/* caller must free the list and unref() the contents */ +GSList *_moo_edit_parse_user_tools (MooUserToolType type); void _moo_edit_save_user_tools (MooUserToolType type, const GSList *info);