Added lang-specifig configs

master
Yevgen Muntyan 2006-04-26 23:04:41 -05:00
parent e22147ef2e
commit ea35f16b38
7 changed files with 294 additions and 51 deletions

View File

@ -291,16 +291,16 @@
</kdevdoctreeview>
<kdevfilecreate>
<filetypes>
<type icon="source" ext="g" create="template" name="GAP source" >
<type icon="source" ext="g" name="GAP source" create="template" >
<descr>A new empty GAP source file</descr>
</type>
<type icon="source_cpp" ext="cpp" create="template" name="C++ Source" >
<type icon="source_cpp" ext="cpp" name="C++ Source" create="template" >
<descr>A new empty C++ file.</descr>
</type>
<type icon="source_h" ext="h" create="template" name="C/C++ Header" >
<type icon="source_h" ext="h" name="C/C++ Header" create="template" >
<descr>A new empty header file for C/C++.</descr>
</type>
<type icon="source_c" ext="c" create="template" name="C Source" >
<type icon="source_c" ext="c" name="C Source" create="template" >
<descr>A new empty C file.</descr>
</type>
</filetypes>

View File

@ -191,7 +191,6 @@
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="spacing">3</property>
<property name="moo_sensitive">usespaces</property>
<child>
<widget class="GtkLabel" id="label32">
<property name="visible">True</property>
@ -309,8 +308,8 @@
<property name="can_focus">True</property>
<property name="adjustment">1.000000 1.000000 100.000000 1.000000 10.000000 10.000000</property>
<property name="climb_rate">1</property>
<property name="moo_sensitive">auto_save</property>
<property name="moo_prefs_key">auto_save_interval</property>
<property name="moo_sensitive">auto_save</property>
</widget>
<packing>
<property name="expand">False</property>
@ -505,8 +504,8 @@
<property name="label">Do not split words</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
<property name="moo_sensitive">enable_wrapping</property>
<property name="moo_prefs_key">wrapping_dont_split_words</property>
<property name="moo_sensitive">enable_wrapping</property>
</widget>
<packing>
<property name="expand">False</property>
@ -742,18 +741,28 @@
<property name="n_rows">2</property>
<property name="n_columns">2</property>
<child>
<widget class="GtkComboBox" id="color_scheme_combo">
<widget class="GtkLabel" id="label99">
<property name="visible">True</property>
<property name="items" translatable="yes">KDE
gvim
</property>
<property name="label">Font:</property>
<property name="moo_sensitive">!use_default_font</property>
</widget>
<packing>
<property name="x_options"></property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkFontButton" id="fontbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="use_font">True</property>
<property name="use_size">True</property>
<property name="moo_prefs_key">font</property>
<property name="moo_sensitive">!use_default_font</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
@ -771,28 +780,18 @@ gvim
</packing>
</child>
<child>
<widget class="GtkFontButton" id="fontbutton">
<widget class="GtkComboBox" id="color_scheme_combo">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="use_font">True</property>
<property name="use_size">True</property>
<property name="moo_sensitive">!use_default_font</property>
<property name="moo_prefs_key">font</property>
<property name="items" translatable="yes">KDE
gvim
</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label99">
<property name="visible">True</property>
<property name="label">Font:</property>
<property name="moo_sensitive">!use_default_font</property>
</widget>
<packing>
<property name="x_options"></property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>

View File

@ -71,6 +71,7 @@ struct _MooEditPrivate {
MooEditor *editor;
gulong modified_changed_handler_id;
guint apply_config_idle;
/***********************************************************************/
/* Document

View File

@ -300,6 +300,9 @@ moo_edit_finalize (GObject *object)
edit);
g_object_unref (edit->config);
if (edit->priv->apply_config_idle)
g_source_remove (edit->priv->apply_config_idle);
edit->priv->focus_in_handler_id = 0;
if (edit->priv->file_monitor_id)
_moo_edit_stop_file_watch (edit);
@ -639,15 +642,6 @@ moo_edit_get_editor (MooEdit *doc)
}
static void
moo_edit_set_lang (MooEdit *edit,
MooLang *lang)
{
g_return_if_fail (MOO_IS_EDIT (edit));
moo_text_view_set_lang (MOO_TEXT_VIEW (edit), lang);
}
typedef void (*SetVarFunc) (MooEdit *edit,
char *name,
char *val);
@ -878,18 +872,66 @@ config_changed (MooEdit *edit,
}
static void
moo_edit_set_lang (MooEdit *edit,
MooLang *lang)
{
MooLang *old_lang;
MooEditConfig *config;
old_lang = moo_text_view_get_lang (MOO_TEXT_VIEW (edit));
if (old_lang == lang)
return;
_moo_edit_freeze_config_notify (edit);
moo_edit_config_unset_by_source (edit->config, MOO_EDIT_CONFIG_SOURCE_LANG);
config = moo_edit_config_get_for_lang (moo_lang_id (lang));
moo_edit_config_compose (edit->config, config);
moo_text_view_set_lang (MOO_TEXT_VIEW (edit), lang);
_moo_edit_thaw_config_notify (edit);
}
static void
moo_edit_apply_config (MooEdit *edit)
{
const char *lang_id = moo_edit_config_get_string (edit->config, "lang");
MooLangMgr *mgr = moo_editor_get_lang_mgr (edit->priv->editor);
MooLang *lang = lang_id ? moo_lang_mgr_get_lang (mgr, lang_id) : NULL;
moo_edit_set_lang (edit, lang);
}
static gboolean
do_apply_config (MooEdit *edit)
{
edit->priv->apply_config_idle = 0;
moo_edit_apply_config (edit);
return FALSE;
}
static void
moo_edit_queue_apply_config (MooEdit *edit)
{
if (!edit->priv->apply_config_idle)
edit->priv->apply_config_idle =
g_idle_add ((GSourceFunc) do_apply_config, edit);
}
static void
moo_edit_config_notify (MooEdit *edit,
guint var_id,
G_GNUC_UNUSED GParamSpec *pspec)
{
if (var_id == settings[SETTING_LANG])
{
const char *value = moo_edit_config_get_string (edit->config, "lang");
MooLangMgr *mgr = moo_editor_get_lang_mgr (edit->priv->editor);
MooLang *lang = value ? moo_lang_mgr_get_lang (mgr, value) : NULL;
moo_edit_set_lang (edit, lang);
}
moo_edit_apply_config (edit);
else
moo_edit_queue_apply_config (edit);
}

View File

@ -15,7 +15,10 @@
#define MOOEDIT_COMPILATION
#include "mooedit/mooeditconfig.h"
#include "mooedit/moolang.h"
#include "mooedit/mooeditprefs.h"
#include "mooutils/mooutils-gobject.h"
#include "mooutils/mooprefs.h"
#include <gobject/gvaluecollector.h>
#include <string.h>
@ -55,6 +58,7 @@ static GSList *instances;
static VarArray *vars;
static GQuark prop_id_quark;
static GHashTable *aliases;
static GHashTable *lang_configs;
static void moo_edit_config_finalize (GObject *object);
@ -153,6 +157,9 @@ global_init (void)
g_signal_connect (global, "notify",
G_CALLBACK (global_changed), NULL);
/* XXX read preferences here */
lang_configs = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_object_unref);
}
}
@ -347,6 +354,7 @@ moo_edit_config_lookup_spec (const char *name,
GParamSpec *pspec;
GObjectClass *klass;
const char *real_name;
char *norm_name;
g_return_val_if_fail (name != NULL, NULL);
@ -354,12 +362,13 @@ moo_edit_config_lookup_spec (const char *name,
klass = g_type_class_ref (MOO_TYPE_EDIT_CONFIG);
real_name = name;
norm_name = g_strdelimit (g_strdup (name), "_", '-');
real_name = norm_name;
if (try_alias)
real_name = g_hash_table_lookup (aliases, name);
real_name = g_hash_table_lookup (aliases, norm_name);
if (!real_name)
real_name = name;
real_name = norm_name;
pspec = g_object_class_find_property (klass, real_name);
@ -372,6 +381,7 @@ moo_edit_config_lookup_spec (const char *name,
}
g_type_class_unref (klass);
g_free (norm_name);
return pspec;
}
@ -620,6 +630,37 @@ moo_edit_config_unset_by_source (MooEditConfig *config,
}
void
moo_edit_config_compose (MooEditConfig *target,
MooEditConfig *src)
{
guint i;
g_return_if_fail (MOO_IS_EDIT_CONFIG (target));
g_return_if_fail (MOO_IS_EDIT_CONFIG (src));
g_object_ref (target);
g_object_freeze_notify (G_OBJECT (target));
for (i = 1; i < vars->len; ++i)
{
Value *old = VALUE (target, i);
Value *new = VALUE (src, i);
if (old->source < new->source)
continue;
g_value_copy (&new->gval, &old->gval);
old->source = new->source;
object_notify (target, i);
}
g_object_thaw_notify (G_OBJECT (target));
g_object_unref (target);
}
guint
moo_edit_config_get_setting_id (GParamSpec *pspec)
{
@ -756,3 +797,154 @@ moo_edit_config_install_alias (const char *name,
g_hash_table_insert (aliases, s1, g_strdup (pspec->name));
g_hash_table_insert (aliases, s2, g_strdup (pspec->name));
}
MooEditConfig *
moo_edit_config_get_for_lang (const char *lang)
{
MooEditConfig *config;
global_init ();
if (!lang)
lang = MOO_LANG_NONE;
config = g_hash_table_lookup (lang_configs, lang);
if (!config)
{
config = moo_edit_config_new ();
g_hash_table_insert (lang_configs, g_strdup (lang), config);
}
return config;
}
/***************************************************************************/
/* Loading and saving
*/
#define ELEMENT_LANG_CONFIG MOO_EDIT_PREFS_PREFIX "/filetypes"
#define ELEMENT_LANG "lang"
#define PROP_LANG_ID "id"
static void
load_lang_node (MooMarkupNode *lang_node)
{
const char *lang_id;
MooMarkupNode *node;
MooEditConfig *config = NULL;
lang_id = moo_markup_get_prop (lang_node, PROP_LANG_ID);
g_return_if_fail (lang_id != NULL);
for (node = lang_node->children; node != NULL; node = node->next)
{
if (!MOO_MARKUP_IS_ELEMENT (node))
continue;
if (!moo_edit_config_lookup_spec (node->name, NULL, TRUE))
{
g_warning ("%s: no property named '%s'",
G_STRLOC, node->name);
continue;
}
if (!config)
config = moo_edit_config_get_for_lang (lang_id);
moo_edit_config_parse (config, node->name,
moo_markup_get_content (node),
MOO_EDIT_CONFIG_SOURCE_LANG);
}
}
void
_moo_edit_config_load (void)
{
MooMarkupDoc *xml;
MooMarkupNode *root, *node;
xml = moo_prefs_get_markup ();
g_return_if_fail (xml != NULL);
root = moo_markup_get_element (MOO_MARKUP_NODE (xml),
ELEMENT_LANG_CONFIG);
if (!root)
return;
for (node = root->children; node != NULL; node = node->next)
{
if (!MOO_MARKUP_IS_ELEMENT (node))
continue;
if (strcmp (node->name, ELEMENT_LANG))
{
g_warning ("%s: invalid '%s' element", G_STRLOC, node->name);
continue;
}
load_lang_node (node);
}
}
static void
save_config (const char *lang_id,
MooEditConfig *config,
MooMarkupNode *root)
{
guint i;
MooMarkupNode *lang_node = NULL;
for (i = 1; i < vars->len; ++i)
{
Value *val = VALUE (config, i);
const char *string;
if (val->source != MOO_EDIT_CONFIG_SOURCE_LANG)
continue;
string = moo_value_convert_to_string (&val->gval);
g_return_if_fail (string != NULL);
if (!lang_node)
{
lang_node = moo_markup_create_element (root, ELEMENT_LANG);
g_return_if_fail (lang_node != NULL);
moo_markup_set_prop (lang_node, PROP_LANG_ID, lang_id);
}
moo_markup_create_text_element (lang_node,
vars->data[i].pspec->name,
string);
}
}
void
_moo_edit_config_save (void)
{
MooMarkupDoc *xml;
MooMarkupNode *root;
xml = moo_prefs_get_markup ();
g_return_if_fail (xml != NULL);
root = moo_markup_get_element (MOO_MARKUP_NODE (xml),
ELEMENT_LANG_CONFIG);
if (root)
moo_markup_delete_node (root);
if (!lang_configs || !g_hash_table_size (lang_configs))
return;
root = moo_markup_create_element (MOO_MARKUP_NODE (xml),
ELEMENT_LANG_CONFIG);
g_return_if_fail (root != NULL);
g_hash_table_foreach (lang_configs, (GHFunc) save_config, root);
}

View File

@ -38,8 +38,9 @@ typedef enum {
MOO_EDIT_CONFIG_SOURCE_USER = 0,
MOO_EDIT_CONFIG_SOURCE_FILE = 10,
MOO_EDIT_CONFIG_SOURCE_FILENAME = 20,
MOO_EDIT_CONFIG_SOURCE_PREFS = 30,
MOO_EDIT_CONFIG_SOURCE_AUTO = 40
MOO_EDIT_CONFIG_SOURCE_LANG = 30,
MOO_EDIT_CONFIG_SOURCE_PREFS = 40,
MOO_EDIT_CONFIG_SOURCE_AUTO = 50
} MooEditConfigSource;
struct _MooEditConfig
@ -101,6 +102,13 @@ void moo_edit_config_unset_by_source (MooEditConfig *config,
gboolean moo_edit_config_parse_bool (const char *string,
gboolean *value);
void moo_edit_config_compose (MooEditConfig *target,
MooEditConfig *src);
MooEditConfig *moo_edit_config_get_for_lang (const char *lang);
void _moo_edit_config_load (void);
void _moo_edit_config_save (void);
G_END_DECLS

View File

@ -283,6 +283,7 @@ moo_editor_init (MooEditor *editor)
NULL, (GDestroyNotify) moo_edit_saver_unref);
moo_prefs_new_key_string (moo_edit_setting (MOO_EDIT_PREFS_DEFAULT_LANG), NULL);
_moo_edit_config_load ();
editor->priv->prefs_notify =
moo_prefs_notify_connect (MOO_EDIT_PREFS_PREFIX "/[^/]*",