Commit 33b5014e authored by Christian Hergert's avatar Christian Hergert
Browse files

GbSourceView: allow overwriting braces like } ) ] ' and "

This can be handy instead of having to move over the character with
arrow keys and such.
parent c1cab8f6
......@@ -68,6 +68,7 @@ struct _GbSourceViewPrivate
guint auto_indent : 1;
guint enable_word_completion : 1;
guint show_shadow : 1;
guint overwrite_braces : 1;
};
typedef void (*GbSourceViewMatchFunc) (GbSourceView *view,
......@@ -82,6 +83,7 @@ enum {
PROP_AUTO_INDENT,
PROP_ENABLE_WORD_COMPLETION,
PROP_FONT_NAME,
PROP_OVERWRITE_BRACES,
PROP_SEARCH_HIGHLIGHTER,
PROP_SHOW_SHADOW,
PROP_SMART_HOME_END_SIMPLE,
......@@ -109,6 +111,28 @@ gb_source_view_get_vim (GbSourceView *view)
return view->priv->vim;
}
gboolean
gb_source_view_get_overwrite_braces (GbSourceView *view)
{
g_return_val_if_fail (GB_IS_SOURCE_VIEW (view), FALSE);
return view->priv->overwrite_braces;
}
void
gb_source_view_set_overwrite_braces (GbSourceView *view,
gboolean overwrite_braces)
{
g_return_if_fail (GB_IS_SOURCE_VIEW (view));
if (view->priv->overwrite_braces != overwrite_braces)
{
view->priv->overwrite_braces = overwrite_braces;
g_object_notify_by_pspec (G_OBJECT (view),
gParamSpecs [PROP_OVERWRITE_BRACES]);
}
}
gboolean
gb_source_view_get_enable_word_completion (GbSourceView *view)
{
......@@ -217,6 +241,9 @@ gb_source_view_connect_settings (GbSourceView *view)
g_settings_bind (settings, "insert-spaces-instead-of-tabs",
view, "insert-spaces-instead-of-tabs",
G_SETTINGS_BIND_GET);
g_settings_bind (settings, "overwrite-braces",
view, "overwrite-braces",
G_SETTINGS_BIND_GET);
g_settings_bind (settings, "right-margin-position",
view, "right-margin-position",
G_SETTINGS_BIND_GET);
......@@ -1214,6 +1241,65 @@ gb_source_view_notify_buffer (GObject *object,
}
}
static gboolean
gb_source_view_maybe_overwrite (GbSourceView *view,
GdkEventKey *event)
{
g_return_val_if_fail (GB_IS_SOURCE_VIEW (view), FALSE);
g_return_val_if_fail (event, FALSE);
if (view->priv->overwrite_braces)
{
GtkTextMark *mark;
GtkTextBuffer *buffer;
GtkTextIter iter;
gunichar ch;
gboolean ignore = FALSE;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
ch = gtk_text_iter_get_char (&iter);
switch (event->keyval)
{
case GDK_KEY_parenright:
ignore = (ch == ')');
break;
case GDK_KEY_bracketright:
ignore = (ch == ']');
break;
case GDK_KEY_braceright:
ignore = (ch == '}');
break;
case GDK_KEY_quotedbl:
ignore = (ch == '"');
break;
case GDK_KEY_quoteright:
ignore = (ch == '\'');
break;
default:
break;
}
if (ignore && !gtk_text_buffer_get_has_selection (buffer))
{
if (!gtk_text_iter_forward_char (&iter))
gtk_text_buffer_get_end_iter (buffer, &iter);
gtk_text_buffer_select_range (buffer, &iter, &iter);
return TRUE;
}
}
return FALSE;
}
static gboolean
gb_source_view_key_press_event (GtkWidget *widget,
GdkEventKey *event)
......@@ -1292,6 +1378,13 @@ gb_source_view_key_press_event (GtkWidget *widget,
if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
return TRUE;
/*
* Possibly overwrite the next character if the keypress keycode matches
* the next bracket/brace/paren/quotation.
*/
if (gb_source_view_maybe_overwrite (view, event))
return TRUE;
/*
* If we have an auto-indenter and the event is for a trigger key, then we
* chain up to the parent class to insert the character, and then let the
......@@ -1866,6 +1959,12 @@ gb_source_view_get_property (GObject *object,
case PROP_ENABLE_WORD_COMPLETION:
g_value_set_boolean (value,
gb_source_view_get_enable_word_completion (view));
break;
case PROP_OVERWRITE_BRACES:
g_value_set_boolean (value,
gb_source_view_get_overwrite_braces (view));
break;
case PROP_SEARCH_HIGHLIGHTER:
g_value_set_object (value, gb_source_view_get_search_highlighter (view));
......@@ -1904,6 +2003,10 @@ gb_source_view_set_property (GObject *object,
gb_source_view_set_font_name (view, g_value_get_string (value));
break;
case PROP_OVERWRITE_BRACES:
gb_source_view_set_overwrite_braces (view, g_value_get_boolean (value));
break;
case PROP_SEARCH_HIGHLIGHTER:
gb_source_view_set_search_highlighter (view, g_value_get_object (value));
break;
......@@ -1968,6 +2071,16 @@ gb_source_view_class_init (GbSourceViewClass *klass)
g_object_class_install_property (object_class, PROP_FONT_NAME,
gParamSpecs [PROP_FONT_NAME]);
gParamSpecs [PROP_OVERWRITE_BRACES] =
g_param_spec_boolean ("overwrite-braces",
_("Overwrite Braces"),
_("If we should overwrite braces, brackets, "
"parenthesis and quotes."),
FALSE,
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_OVERWRITE_BRACES,
gParamSpecs [PROP_OVERWRITE_BRACES]);
gParamSpecs[PROP_SHOW_SHADOW] =
g_param_spec_boolean ("show-shadow",
_("Show Shadow"),
......
......@@ -68,22 +68,25 @@ struct _GbSourceViewClass
const gchar *search_text);
};
void gb_source_view_begin_search (GbSourceView *view,
GtkDirectionType direction,
const gchar *search_text);
void gb_source_view_clear_snippets (GbSourceView *view);
GbSourceAutoIndenter *gb_source_view_get_auto_indenter (GbSourceView *view);
gboolean gb_source_view_get_show_shadow (GbSourceView *view);
GType gb_source_view_get_type (void) G_GNUC_CONST;
void gb_source_view_indent_selection (GbSourceView *view);
void gb_source_view_push_snippet (GbSourceView *view,
GbSourceSnippet *snippet);
void gb_source_view_set_font_name (GbSourceView *view,
const gchar *font_name);
void gb_source_view_set_show_shadow (GbSourceView *view,
gboolean show_shadow);
void gb_source_view_unindent_selection (GbSourceView *view);
GbSourceVim *gb_source_view_get_vim (GbSourceView *view);
void gb_source_view_begin_search (GbSourceView *view,
GtkDirectionType direction,
const gchar *search_text);
void gb_source_view_clear_snippets (GbSourceView *view);
GbSourceAutoIndenter *gb_source_view_get_auto_indenter (GbSourceView *view);
gboolean gb_source_view_get_overwrite_braces (GbSourceView *view);
gboolean gb_source_view_get_show_shadow (GbSourceView *view);
GType gb_source_view_get_type (void) G_GNUC_CONST;
void gb_source_view_indent_selection (GbSourceView *view);
void gb_source_view_push_snippet (GbSourceView *view,
GbSourceSnippet *snippet);
void gb_source_view_set_font_name (GbSourceView *view,
const gchar *font_name);
void gb_source_view_set_overwrite_braces (GbSourceView *view,
gboolean overwrite_braces);
void gb_source_view_set_show_shadow (GbSourceView *view,
gboolean show_shadow);
void gb_source_view_unindent_selection (GbSourceView *view);
GbSourceVim *gb_source_view_get_vim (GbSourceView *view);
G_END_DECLS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment