Better Enter key behaviour
parent
c223ff71f7
commit
c2ca12a203
|
@ -56,12 +56,14 @@ enum {
|
|||
PROP_0,
|
||||
PROP_TAB_WIDTH,
|
||||
PROP_USE_TABS,
|
||||
PROP_STRIP,
|
||||
PROP_INDENT,
|
||||
PROP_DOC
|
||||
};
|
||||
|
||||
enum {
|
||||
SETTING_USE_TABS,
|
||||
SETTING_STRIP,
|
||||
SETTING_INDENT_WIDTH,
|
||||
LAST_SETTING
|
||||
};
|
||||
|
@ -86,55 +88,43 @@ moo_indenter_class_init (MooIndenterClass *klass)
|
|||
klass->config_changed = config_changed_default;
|
||||
|
||||
settings[SETTING_USE_TABS] = moo_edit_config_install_setting (
|
||||
g_param_spec_boolean ("indent-use-tabs",
|
||||
"indent-use-tabs",
|
||||
"indent-use-tabs",
|
||||
TRUE,
|
||||
G_PARAM_READWRITE));
|
||||
g_param_spec_boolean ("indent-use-tabs", "indent-use-tabs", "indent-use-tabs",
|
||||
TRUE, G_PARAM_READWRITE));
|
||||
moo_edit_config_install_alias ("indent-use-tabs", "use-tabs");
|
||||
|
||||
settings[SETTING_STRIP] = moo_edit_config_install_setting (
|
||||
g_param_spec_boolean ("indent-strip", "indent-strip", "indent-strip",
|
||||
TRUE, G_PARAM_READWRITE));
|
||||
moo_edit_config_install_alias ("indent-strip", "edit-strip");
|
||||
|
||||
settings[SETTING_INDENT_WIDTH] = moo_edit_config_install_setting (
|
||||
g_param_spec_uint ("indent-width",
|
||||
"indent-width",
|
||||
"indent-width",
|
||||
1, G_MAXUINT, 8,
|
||||
G_PARAM_READWRITE));
|
||||
g_param_spec_uint ("indent-width", "indent-width", "indent-width",
|
||||
1, G_MAXUINT, 8, G_PARAM_READWRITE));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_DOC,
|
||||
g_param_spec_object ("doc",
|
||||
"doc",
|
||||
"doc",
|
||||
MOO_TYPE_EDIT,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
||||
g_object_class_install_property (gobject_class, PROP_DOC,
|
||||
g_param_spec_object ("doc", "doc", "doc",
|
||||
MOO_TYPE_EDIT,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_TAB_WIDTH,
|
||||
g_param_spec_uint ("tab-width",
|
||||
"tab-width",
|
||||
"tab-width",
|
||||
1,
|
||||
G_MAXUINT,
|
||||
8,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
g_object_class_install_property (gobject_class, PROP_TAB_WIDTH,
|
||||
g_param_spec_uint ("tab-width", "tab-width", "tab-width",
|
||||
1, G_MAXUINT, 8,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_USE_TABS,
|
||||
g_param_spec_boolean ("use-tabs",
|
||||
"use-tabs",
|
||||
"use-tabs",
|
||||
TRUE,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
g_object_class_install_property (gobject_class, PROP_USE_TABS,
|
||||
g_param_spec_boolean ("use-tabs", "use-tabs", "use-tabs",
|
||||
TRUE,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_INDENT,
|
||||
g_param_spec_uint ("indent",
|
||||
"indent",
|
||||
"indent",
|
||||
1,
|
||||
G_MAXUINT,
|
||||
8,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
g_object_class_install_property (gobject_class, PROP_STRIP,
|
||||
g_param_spec_boolean ("strip", "strip", "strip",
|
||||
FALSE,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
|
||||
g_object_class_install_property (gobject_class, PROP_INDENT,
|
||||
g_param_spec_uint ("indent", "indent", "indent",
|
||||
1, G_MAXUINT, 8,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
||||
|
||||
signals[CONFIG_CHANGED] =
|
||||
g_signal_new ("config-changed",
|
||||
|
@ -236,6 +226,11 @@ static void moo_indenter_set_property (GObject *object,
|
|||
g_object_notify (object, "use-tabs");
|
||||
break;
|
||||
|
||||
case PROP_STRIP:
|
||||
indenter->strip = g_value_get_boolean (value);
|
||||
g_object_notify (object, "strip");
|
||||
break;
|
||||
|
||||
case PROP_INDENT:
|
||||
indenter->indent = g_value_get_uint (value);
|
||||
g_object_notify (object, "indent");
|
||||
|
@ -269,6 +264,10 @@ static void moo_indenter_get_property (GObject *object,
|
|||
g_value_set_boolean (value, indenter->use_tabs);
|
||||
break;
|
||||
|
||||
case PROP_STRIP:
|
||||
g_value_set_boolean (value, indenter->strip);
|
||||
break;
|
||||
|
||||
case PROP_INDENT:
|
||||
g_value_set_uint (value, indenter->indent);
|
||||
break;
|
||||
|
@ -370,7 +369,8 @@ moo_indenter_make_space (MooIndenter *indenter,
|
|||
|
||||
/* computes amount of leading white space on the given line;
|
||||
returns TRUE if line contains some non-whitespace chars;
|
||||
if returns TRUE, then iter points to the first non-white-space char */
|
||||
if returns TRUE, then iter points to the first non-white-space char,
|
||||
otherwise it points to the end of line */
|
||||
static gboolean
|
||||
compute_line_offset (GtkTextIter *dest,
|
||||
guint tab_width,
|
||||
|
@ -378,6 +378,7 @@ compute_line_offset (GtkTextIter *dest,
|
|||
{
|
||||
guint offset = 0;
|
||||
GtkTextIter iter = *dest;
|
||||
gboolean retval = FALSE;
|
||||
|
||||
while (!gtk_text_iter_ends_line (&iter))
|
||||
{
|
||||
|
@ -394,15 +395,16 @@ compute_line_offset (GtkTextIter *dest,
|
|||
}
|
||||
else
|
||||
{
|
||||
*offsetp = offset;
|
||||
*dest = iter;
|
||||
return TRUE;
|
||||
retval = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
gtk_text_iter_forward_char (&iter);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
*offsetp = offset;
|
||||
*dest = iter;
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
@ -411,32 +413,45 @@ character_default (MooIndenter *indenter,
|
|||
gunichar inserted_char,
|
||||
GtkTextIter *where)
|
||||
{
|
||||
int i;
|
||||
char *indent_string = NULL;
|
||||
GtkTextBuffer *buffer = gtk_text_iter_get_buffer (where);
|
||||
guint offset;
|
||||
GtkTextIter iter;
|
||||
gboolean ws_line;
|
||||
|
||||
if (inserted_char != '\n')
|
||||
return;
|
||||
|
||||
for (i = gtk_text_iter_get_line (where) - 1; i >= 0; --i)
|
||||
{
|
||||
guint offset;
|
||||
GtkTextIter iter;
|
||||
|
||||
gtk_text_buffer_get_iter_at_line (buffer, &iter, i);
|
||||
|
||||
if (compute_line_offset (&iter, indenter->tab_width, &offset))
|
||||
{
|
||||
indent_string = moo_indenter_make_space (indenter, offset, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (indent_string)
|
||||
iter = *where;
|
||||
gtk_text_iter_backward_line (&iter);
|
||||
ws_line = !compute_line_offset (&iter, indenter->tab_width, &offset);
|
||||
|
||||
if (!offset)
|
||||
return;
|
||||
|
||||
if (offset)
|
||||
{
|
||||
indent_string = moo_indenter_make_space (indenter, offset, 0);
|
||||
gtk_text_buffer_insert (buffer, where, indent_string, -1);
|
||||
g_free (indent_string);
|
||||
}
|
||||
|
||||
if (ws_line && indenter->strip)
|
||||
{
|
||||
GtkTextMark *saved_location;
|
||||
GtkTextIter iter2;
|
||||
|
||||
saved_location = gtk_text_buffer_create_mark (buffer, NULL, where, FALSE);
|
||||
|
||||
iter = *where;
|
||||
gtk_text_iter_backward_line (&iter);
|
||||
iter2 = iter;
|
||||
gtk_text_iter_forward_to_line_end (&iter2);
|
||||
gtk_text_buffer_delete (buffer, &iter, &iter2);
|
||||
|
||||
gtk_text_buffer_get_iter_at_mark (buffer, where, saved_location);
|
||||
gtk_text_buffer_delete_mark (buffer, saved_location);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -724,6 +739,11 @@ config_changed_default (MooIndenter *indenter,
|
|||
guint width = moo_edit_config_get_uint (doc->config, "indent-width");
|
||||
g_object_set (indenter, "indent", width, NULL);
|
||||
}
|
||||
else if (var_id == settings[SETTING_STRIP])
|
||||
{
|
||||
gboolean strip = moo_edit_config_get_bool (doc->config, "indent-strip");
|
||||
g_object_set (indenter, "strip", strip, NULL);
|
||||
}
|
||||
else if (!strcmp (pspec->name, "tab-width"))
|
||||
{
|
||||
guint tab_width = moo_edit_config_get_uint (doc->config, "tab-width");
|
||||
|
|
|
@ -36,6 +36,7 @@ struct _MooIndenter
|
|||
|
||||
gpointer doc; /* MooEdit* */
|
||||
gboolean use_tabs;
|
||||
gboolean strip;
|
||||
guint tab_width;
|
||||
guint indent;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue