summaryrefslogtreecommitdiff
path: root/gtksourceview
diff options
context:
space:
mode:
authorSébastien Wilmet <swilmet@gnome.org>2015-08-16 18:57:13 +0200
committerSébastien Wilmet <swilmet@gnome.org>2015-08-16 19:07:07 +0200
commitf5fa1340e69973f04080d919c5e2c7e862bb7b44 (patch)
tree2dd5a5329933e3c9b7c7a4ec823b3dab9449d24f /gtksourceview
parenta622c34673f629a8281faf471397edcd6cd7baa6 (diff)
downloadgtksourceview-f5fa1340e69973f04080d919c5e2c7e862bb7b44.tar.gz
view: always do ctrl+backspace behavior for leading spaces
The ctrl+backspace deletes all the leading spaces, if only leading spaces are on the left of the cursor. This was tight to the smart-backspace property, but the smart-backspace is more about removing one indentation level when spaces are used for the indentation. So it's better to always handle the ctrl+backspace behavior, outside of the smart-backspace property. The behavior of ctrl+backspace in GtkTextView is to remove the previous word. But the previous word can be on a previous line, so it's not a great default behavior.
Diffstat (limited to 'gtksourceview')
-rw-r--r--gtksourceview/gtksourceview.c106
1 files changed, 64 insertions, 42 deletions
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 4bc24f0f..ba410437 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -4062,18 +4062,18 @@ gtk_source_view_move_lines (GtkSourceView *view,
}
static gboolean
-gtk_source_view_do_smart_backspace (GtkSourceView *view,
- guint modifiers)
+do_smart_backspace (GtkSourceView *view)
{
GtkTextBuffer *buffer;
gboolean default_editable;
GtkTextIter insert;
GtkTextIter end;
- GtkTextIter iter;
+ GtkTextIter leading;
+ GtkTextIter trailing;
guint visual_column;
gint indent_width;
- buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+ buffer = GTK_TEXT_BUFFER (view->priv->source_buffer);
default_editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (view));
if (gtk_text_buffer_get_selection_bounds (buffer, &insert, &end))
@@ -4081,43 +4081,11 @@ gtk_source_view_do_smart_backspace (GtkSourceView *view,
return FALSE;
}
- if ((modifiers & GDK_CONTROL_MASK) != 0)
- {
- /*
- * A <Control>BackSpace at the beginning of the line should only move us to the
- * end of the previous line. Anything more than that is non-obvious because it requires
- * looking in a position other than where the cursor is.
- */
- if ((gtk_text_iter_get_line_offset (&insert) == 0) &&
- (gtk_text_iter_get_line (&insert) > 0))
- {
- gtk_text_iter_backward_cursor_position (&insert);
- gtk_text_buffer_delete_interactive (buffer, &insert, &end, default_editable);
- return TRUE;
- }
- }
-
/* If the line isn't empty up to our cursor, ignore. */
- iter = insert;
- gtk_text_iter_set_line_offset (&iter, 0);
- while (gtk_text_iter_compare (&iter, &insert) < 0)
+ get_leading_trailing (&insert, &leading, &trailing);
+ if (gtk_text_iter_compare (&leading, &insert) < 0)
{
- gunichar ch = gtk_text_iter_get_char (&iter);
-
- if (!g_unichar_isspace (ch))
- {
- return FALSE;
- }
-
- gtk_text_iter_forward_char (&iter);
- }
-
- /* If <Control>BackSpace was specified, delete up to the zero position. */
- if ((modifiers & GDK_CONTROL_MASK) != 0)
- {
- gtk_text_iter_set_line_offset (&insert, 0);
- gtk_text_buffer_delete_interactive (buffer, &insert, &end, default_editable);
- return TRUE;
+ return FALSE;
}
visual_column = gtk_source_view_get_visual_column (view, &insert);
@@ -4180,6 +4148,50 @@ gtk_source_view_do_smart_backspace (GtkSourceView *view,
}
static gboolean
+do_ctrl_backspace (GtkSourceView *view)
+{
+ GtkTextBuffer *buffer;
+ GtkTextIter insert;
+ GtkTextIter end;
+ GtkTextIter leading;
+ GtkTextIter trailing;
+ gboolean default_editable;
+
+ buffer = GTK_TEXT_BUFFER (view->priv->source_buffer);
+ default_editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (view));
+
+ if (gtk_text_buffer_get_selection_bounds (buffer, &insert, &end))
+ {
+ return FALSE;
+ }
+
+ /* A <Control>BackSpace at the beginning of the line should only move us to the
+ * end of the previous line. Anything more than that is non-obvious because it requires
+ * looking in a position other than where the cursor is.
+ */
+ if ((gtk_text_iter_get_line_offset (&insert) == 0) &&
+ (gtk_text_iter_get_line (&insert) > 0))
+ {
+ gtk_text_iter_backward_cursor_position (&insert);
+ gtk_text_buffer_delete_interactive (buffer, &insert, &end, default_editable);
+ return TRUE;
+ }
+
+ /* If only leading whitespaces are on the left of the cursor, delete up
+ * to the zero position.
+ */
+ get_leading_trailing (&insert, &leading, &trailing);
+ if (gtk_text_iter_compare (&insert, &leading) <= 0)
+ {
+ gtk_text_iter_set_line_offset (&insert, 0);
+ gtk_text_buffer_delete_interactive (buffer, &insert, &end, default_editable);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
gtk_source_view_key_press_event (GtkWidget *widget,
GdkEventKey *event)
{
@@ -4289,11 +4301,21 @@ gtk_source_view_key_press_event (GtkWidget *widget,
return GDK_EVENT_STOP;
}
- if ((key == GDK_KEY_BackSpace) && view->priv->smart_backspace)
+ if (key == GDK_KEY_BackSpace)
{
- if (gtk_source_view_do_smart_backspace (view, (event->state & modifiers)))
+ if ((event->state & modifiers) == 0)
{
- return GDK_EVENT_STOP;
+ if (view->priv->smart_backspace && do_smart_backspace (view))
+ {
+ return GDK_EVENT_STOP;
+ }
+ }
+ else if ((event->state & modifiers) == GDK_CONTROL_MASK)
+ {
+ if (do_ctrl_backspace (view))
+ {
+ return GDK_EVENT_STOP;
+ }
}
}