[gtksourceview/wip/chergert/vim] simplify repeat/count
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/chergert/vim] simplify repeat/count
- Date: Thu, 28 Oct 2021 00:08:13 +0000 (UTC)
commit 9331430e5119bbed42d910b6329e767caee1b355
Author: Christian Hergert <chergert redhat com>
Date: Wed Oct 27 17:08:08 2021 -0700
simplify repeat/count
gtksourceview/vim/gtk-source-vim-delete.c | 18 ++++++----
gtksourceview/vim/gtk-source-vim-motion.c | 26 ++++++++++----
gtksourceview/vim/gtk-source-vim-normal.c | 58 +++++++++++++++----------------
gtksourceview/vim/gtk-source-vim-state.c | 30 ++++++++++++++--
gtksourceview/vim/gtk-source-vim-state.h | 9 ++---
5 files changed, 91 insertions(+), 50 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-delete.c b/gtksourceview/vim/gtk-source-vim-delete.c
index 4993499f..779892ab 100644
--- a/gtksourceview/vim/gtk-source-vim-delete.c
+++ b/gtksourceview/vim/gtk-source-vim-delete.c
@@ -126,22 +126,26 @@ gtk_source_vim_delete_resume (GtkSourceVimState *state,
}
static void
-gtk_source_vim_delete_repeat (GtkSourceVimState *state,
- int repeat)
+gtk_source_vim_delete_repeat (GtkSourceVimState *state)
{
GtkSourceVimDelete *self = (GtkSourceVimDelete *)state;
- GtkSourceBuffer *buffer;
- GtkTextIter insert, selection;
g_assert (GTK_SOURCE_IS_VIM_DELETE (self));
if (self->motion != NULL)
{
+ GtkSourceBuffer *buffer;
+ GtkTextIter insert, selection;
+ int count = gtk_source_vim_state_get_count (state);
+
buffer = gtk_source_vim_state_get_buffer (state, &insert, &selection);
+
do
{
- gtk_source_vim_motion_apply (self->motion, &insert, TRUE);
- } while (--repeat > 0);
+ if (!gtk_source_vim_motion_apply (self->motion, &insert, TRUE))
+ break;
+ } while (--count > 0);
+
gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &insert, &selection);
if (gtk_text_iter_ends_line (&insert) &&
@@ -158,7 +162,7 @@ gtk_source_vim_delete_leave (GtkSourceVimState *state)
{
g_assert (GTK_SOURCE_IS_VIM_DELETE (state));
- gtk_source_vim_delete_repeat (state, 0);
+ gtk_source_vim_delete_repeat (state);
}
static void
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index d70d78a1..d21d8863 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -397,6 +397,15 @@ motion_line_first_char (GtkTextIter *iter,
return TRUE;
}
+static gboolean
+motion_forward_char_same_line_eol_okay (GtkTextIter *iter,
+ GtkSourceVimMotion *state)
+{
+ if (gtk_text_iter_ends_line (iter))
+ return FALSE;
+ return gtk_text_iter_forward_char (iter);
+}
+
static gboolean
motion_forward_char (GtkTextIter *iter,
GtkSourceVimMotion *state)
@@ -420,16 +429,18 @@ static gboolean
motion_forward_char_same_line (GtkTextIter *iter,
GtkSourceVimMotion *state)
{
- GtkTextIter begin = *iter;
+ if (gtk_text_iter_ends_line (iter))
+ return FALSE;
gtk_text_iter_forward_char (iter);
if (gtk_text_iter_ends_line (iter) && !gtk_text_iter_starts_line (iter))
{
gtk_text_iter_backward_char (iter);
+ return FALSE;
}
- return !gtk_text_iter_equal (&begin, iter);
+ return TRUE;
}
static gboolean
@@ -1235,13 +1246,13 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
}
static void
-gtk_source_vim_motion_repeat (GtkSourceVimState *state,
- int repeat)
+gtk_source_vim_motion_repeat (GtkSourceVimState *state)
{
GtkSourceVimMotion *self = (GtkSourceVimMotion *)state;
GtkSourceBuffer *buffer;
GtkTextIter insert, selection;
gboolean has_selection;
+ int count;
g_assert (GTK_SOURCE_IS_VIM_MOTION (self));
@@ -1252,6 +1263,7 @@ gtk_source_vim_motion_repeat (GtkSourceVimState *state,
buffer = gtk_source_vim_state_get_buffer (state, &insert, &selection);
has_selection = !gtk_text_iter_equal (&insert, &selection);
+ count = gtk_source_vim_state_get_count (state);
do
{
@@ -1259,7 +1271,7 @@ gtk_source_vim_motion_repeat (GtkSourceVimState *state,
{
break;
}
- } while (--repeat > 0);
+ } while (--count > 0);
if (has_selection)
gtk_text_buffer_move_mark (GTK_TEXT_BUFFER (buffer),
@@ -1278,7 +1290,7 @@ gtk_source_vim_motion_leave (GtkSourceVimState *state)
if (self->apply_on_leave)
{
- gtk_source_vim_motion_repeat (state, 1);
+ gtk_source_vim_motion_repeat (state);
}
}
@@ -1439,7 +1451,7 @@ gtk_source_vim_motion_new_forward_char (void)
GtkSourceVimMotion *self;
self = g_object_new (GTK_SOURCE_TYPE_VIM_MOTION, NULL);
- self->motion = motion_forward_char;
+ self->motion = motion_forward_char_same_line_eol_okay;
self->inclusivity = EXCLUSIVE;
return GTK_SOURCE_VIM_STATE (self);
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index f85d1fc1..d8b4ad36 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -28,7 +28,7 @@
#include "gtk-source-vim-normal.h"
#include "gtk-source-vim-replace.h"
-#define REPEAT(_block) do { _block; } while (--self->repeat > 0);
+#define REPLAY(_block) do { _block; } while (--self->count > 0);
typedef gboolean (*KeyHandler) (GtkSourceVimNormal *self,
guint keyval,
@@ -44,9 +44,9 @@ struct _GtkSourceVimNormal
KeyHandler handler;
- int repeat;
+ int count;
- guint has_repeat : 1;
+ guint has_count : 1;
};
static gboolean key_handler_initial (GtkSourceVimNormal *self,
@@ -97,17 +97,17 @@ keep_on_last_char (GtkSourceVimNormal *self)
}
static gboolean
-key_handler_repeat (GtkSourceVimNormal *self,
- guint keyval,
- guint keycode,
- GdkModifierType mods,
- const char *string)
+key_handler_count (GtkSourceVimNormal *self,
+ guint keyval,
+ guint keycode,
+ GdkModifierType mods,
+ const char *string)
{
int n;
g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
- self->has_repeat = TRUE;
+ self->has_count = TRUE;
switch (keyval)
{
@@ -127,7 +127,7 @@ key_handler_repeat (GtkSourceVimNormal *self,
return self->handler (self, keyval, keycode, mods, string);
}
- self->repeat = self->repeat * 10 + n;
+ self->count = self->count * 10 + n;
return TRUE;
}
@@ -152,12 +152,12 @@ key_handler_undo (GtkSourceVimNormal *self,
switch (keyval)
{
case GDK_KEY_u:
- REPEAT (gtk_text_buffer_undo (GTK_TEXT_BUFFER (buffer)));
+ REPLAY (gtk_text_buffer_undo (GTK_TEXT_BUFFER (buffer)));
keep_on_last_char (self);
break;
case GDK_KEY_r:
- REPEAT (gtk_text_buffer_redo (GTK_TEXT_BUFFER (buffer)));
+ REPLAY (gtk_text_buffer_redo (GTK_TEXT_BUFFER (buffer)));
keep_on_last_char (self);
break;
@@ -349,7 +349,7 @@ key_handler_command (GtkSourceVimNormal *self,
return TRUE;
case GDK_KEY_C:
- if (self->repeat != 0)
+ if (self->count != 0)
return gtk_source_vim_normal_bail (self);
gtk_source_vim_normal_begin_change (self,
gtk_source_vim_motion_new_line_end (),
@@ -358,7 +358,7 @@ key_handler_command (GtkSourceVimNormal *self,
return TRUE;
case GDK_KEY_D:
- if (self->repeat != 0)
+ if (self->count != 0)
return gtk_source_vim_normal_bail (self);
gtk_source_vim_normal_begin_delete (self,
gtk_source_vim_motion_new_line_end (),
@@ -443,32 +443,32 @@ key_handler_viewport (GtkSourceVimNormal *self,
switch (keyval)
{
case GDK_KEY_d:
- gtk_source_vim_state_scroll_half_page (state, MAX (1, self->repeat));
+ gtk_source_vim_state_scroll_half_page (state, MAX (1, self->count));
gtk_source_vim_normal_clear (self);
return TRUE;
case GDK_KEY_u:
- gtk_source_vim_state_scroll_half_page (state, MIN (-1, -self->repeat));
+ gtk_source_vim_state_scroll_half_page (state, MIN (-1, -self->count));
gtk_source_vim_normal_clear (self);
return TRUE;
case GDK_KEY_e:
- gtk_source_vim_state_scroll_line (state, MAX (1, self->repeat));
+ gtk_source_vim_state_scroll_line (state, MAX (1, self->count));
gtk_source_vim_normal_clear (self);
return TRUE;
case GDK_KEY_y:
- gtk_source_vim_state_scroll_line (state, MIN (-1, -self->repeat));
+ gtk_source_vim_state_scroll_line (state, MIN (-1, -self->count));
gtk_source_vim_normal_clear (self);
return TRUE;
case GDK_KEY_f:
- gtk_source_vim_state_scroll_page (state, MAX (1, self->repeat));
+ gtk_source_vim_state_scroll_page (state, MAX (1, self->count));
gtk_source_vim_normal_clear (self);
return TRUE;
case GDK_KEY_b:
- gtk_source_vim_state_scroll_page (state, MIN (-1, -self->repeat));
+ gtk_source_vim_state_scroll_page (state, MIN (-1, -self->count));
gtk_source_vim_normal_clear (self);
return TRUE;
@@ -776,8 +776,8 @@ key_handler_initial (GtkSourceVimNormal *self,
case GDK_KEY_7: case GDK_KEY_KP_7:
case GDK_KEY_8: case GDK_KEY_KP_8:
case GDK_KEY_9: case GDK_KEY_KP_9:
- if (self->has_repeat == FALSE)
- self->handler = key_handler_repeat;
+ if (self->has_count == FALSE)
+ self->handler = key_handler_count;
break;
case GDK_KEY_u:
@@ -899,6 +899,11 @@ gtk_source_vim_normal_suspend (GtkSourceVimState *state,
g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
g_assert (GTK_SOURCE_IS_VIM_STATE (to));
+ if (self->count)
+ {
+ gtk_source_vim_state_set_count (to, self->count);
+ }
+
buffer = gtk_source_vim_state_get_buffer (state, NULL, NULL);
gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
}
@@ -919,11 +924,6 @@ gtk_source_vim_normal_resume (GtkSourceVimState *state,
insert = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (buffer));
view = gtk_source_vim_state_get_view (state);
- if (self->repeat > 1)
- {
- gtk_source_vim_state_repeat (from, self->repeat - 1);
- }
-
gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
/* Go back one character if we exited replace/insert state */
@@ -1007,8 +1007,8 @@ gtk_source_vim_normal_clear (GtkSourceVimNormal *self)
g_return_if_fail (GTK_SOURCE_IS_VIM_NORMAL (self));
self->handler = key_handler_initial;
- self->repeat = 0;
- self->has_repeat = FALSE;
+ self->count = 0;
+ self->has_count = FALSE;
g_string_truncate (self->command_text, 0);
}
diff --git a/gtksourceview/vim/gtk-source-vim-state.c b/gtksourceview/vim/gtk-source-vim-state.c
index ffed08f1..ba03fd96 100644
--- a/gtksourceview/vim/gtk-source-vim-state.c
+++ b/gtksourceview/vim/gtk-source-vim-state.c
@@ -32,6 +32,7 @@ typedef struct
GtkSourceVimState *parent;
GtkSourceVimState *child;
GtkSourceView *view;
+ int count;
} GtkSourceVimStatePrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkSourceVimState, gtk_source_vim_state, G_TYPE_OBJECT)
@@ -238,6 +239,9 @@ gtk_source_vim_state_class_init (GtkSourceVimStateClass *klass)
static void
gtk_source_vim_state_init (GtkSourceVimState *self)
{
+ GtkSourceVimStatePrivate *priv = gtk_source_vim_state_get_instance_private (self);
+
+ priv->count = 1;
}
GtkSourceView *
@@ -348,14 +352,13 @@ gtk_source_vim_state_get_root (GtkSourceVimState *self)
}
void
-gtk_source_vim_state_repeat (GtkSourceVimState *self,
- int repeat)
+gtk_source_vim_state_repeat (GtkSourceVimState *self)
{
g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
if (GTK_SOURCE_VIM_STATE_GET_CLASS (self)->repeat)
{
- GTK_SOURCE_VIM_STATE_GET_CLASS (self)->repeat (self, repeat);
+ GTK_SOURCE_VIM_STATE_GET_CLASS (self)->repeat (self);
}
}
@@ -752,3 +755,24 @@ gtk_source_vim_state_append_command (GtkSourceVimState *self,
gtk_source_vim_state_append_command (child, string);
}
}
+
+int
+gtk_source_vim_state_get_count (GtkSourceVimState *self)
+{
+ GtkSourceVimStatePrivate *priv = gtk_source_vim_state_get_instance_private (self);
+
+ g_return_val_if_fail (GTK_SOURCE_IS_VIM_STATE (self), 0);
+
+ return priv->count;
+}
+
+void
+gtk_source_vim_state_set_count (GtkSourceVimState *self,
+ int count)
+{
+ GtkSourceVimStatePrivate *priv = gtk_source_vim_state_get_instance_private (self);
+
+ g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
+
+ priv->count = count ? count : 1;
+}
diff --git a/gtksourceview/vim/gtk-source-vim-state.h b/gtksourceview/vim/gtk-source-vim-state.h
index 5c1b5a0b..c18eb31b 100644
--- a/gtksourceview/vim/gtk-source-vim-state.h
+++ b/gtksourceview/vim/gtk-source-vim-state.h
@@ -52,8 +52,7 @@ struct _GtkSourceVimStateClass
GdkModifierType mods,
const char *string);
gboolean (*get_can_repeat) (GtkSourceVimState *state);
- void (*repeat) (GtkSourceVimState *state,
- int repeat);
+ void (*repeat) (GtkSourceVimState *state);
void (*append_command) (GtkSourceVimState *state,
GString *string);
};
@@ -72,6 +71,9 @@ GtkSourceBuffer *gtk_source_vim_state_get_buffer (GtkSourceVimState
GtkTextIter *selection_bound);
GtkSourceVimState *gtk_source_vim_state_get_root (GtkSourceVimState *self);
GtkSourceVimState *gtk_source_vim_state_get_parent (GtkSourceVimState *self);
+int gtk_source_vim_state_get_count (GtkSourceVimState *self);
+void gtk_source_vim_state_set_count (GtkSourceVimState *self,
+ int count);
gboolean gtk_source_vim_state_handle_event (GtkSourceVimState *self,
GdkEvent *event);
void gtk_source_vim_state_set_overwrite (GtkSourceVimState *self,
@@ -80,8 +82,7 @@ gboolean gtk_source_vim_state_get_can_repeat (GtkSourceVimState
gboolean gtk_source_vim_state_synthesize (GtkSourceVimState *self,
guint keyval,
GdkModifierType mods);
-void gtk_source_vim_state_repeat (GtkSourceVimState *self,
- int repeat);
+void gtk_source_vim_state_repeat (GtkSourceVimState *self);
int gtk_source_vim_state_get_visible_lines (GtkSourceVimState *self);
void gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
int count);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]