More user tools thing...

master
Yevgen Muntyan 2006-08-19 00:21:29 -05:00
parent c13754c92f
commit 9b6f203042
8 changed files with 215 additions and 109 deletions

View File

@ -1,5 +1,5 @@
<tools>
<tool name="_Switch Header And Implementation">
<tool name="Switch Header And Implementation">
<langs>C, GAP</langs>
<command type="MooScript" options="need-file">
<code>extensions = [[['.h', '.hh', '.hpp', '.hxx', '.H'], ['.c', '.cc', '.cpp', '.cxx', '.C']],

View File

@ -414,60 +414,6 @@
<property name="tab_pos">GTK_POS_TOP</property>
<property name="scrollable">False</property>
<property name="enable_popup">False</property>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkLabel" id="label10">
<property name="visible">True</property>
<property name="label" translatable="yes"></property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="type">tab</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkLabel" id="label11">
<property name="visible">True</property>
<property name="label" translatable="yes"></property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="type">tab</property>
</packing>
</child>
</widget>
</child>
</widget>

View File

@ -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 <string.h>
@ -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));

View File

@ -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)

View File

@ -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,

View File

@ -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);

View File

@ -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;
}

View File

@ -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);