[gtksourceview/wip/chergert/vim] reuse count for number instead of field



commit 74459316c4715ba154004a32d87cfa42515d491a
Author: Christian Hergert <chergert redhat com>
Date:   Thu Oct 28 14:16:12 2021 -0700

    reuse count for number instead of field

 gtksourceview/vim/gtk-source-vim-motion.c | 40 ++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 14 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index d21d8863..d9188f9a 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -43,9 +43,6 @@ struct _GtkSourceVimMotion
        /* A function to apply the motion */
        Motion motion;
 
-       /* A number for the motion like `3e`. */
-       int number;
-
        /* character for f or F */
        gunichar f_char;
 
@@ -70,6 +67,19 @@ struct _GtkSourceVimMotion
 
 G_DEFINE_TYPE (GtkSourceVimMotion, gtk_source_vim_motion, GTK_SOURCE_TYPE_VIM_STATE)
 
+static inline gboolean
+get_number (guint  keyval,
+            int   *n)
+{
+       if (keyval >= GDK_KEY_0 && keyval <= GDK_KEY_9)
+               *n = keyval - GDK_KEY_0;
+       else if (keyval >= GDK_KEY_KP_0 && keyval <= GDK_KEY_KP_9)
+               *n = keyval - GDK_KEY_KP_0;
+       else
+               return FALSE;
+       return TRUE;
+}
+
 static gboolean
 line_is_empty (GtkTextIter *iter)
 {
@@ -1072,11 +1082,15 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                                        const char        *string)
 {
        GtkSourceVimMotion *self = (GtkSourceVimMotion *)state;
+       int count;
+       int n;
 
        g_assert (GTK_SOURCE_IS_VIM_MOTION (self));
 
        g_string_append (self->command_text, string);
 
+       count = gtk_source_vim_state_get_count (state);
+
        if (self->waiting_for_f_char)
        {
                if (string == NULL || string[0] == 0)
@@ -1107,13 +1121,11 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                g_assert_not_reached ();
        }
 
-       if (self->number != 0)
+       if (count != 0 && get_number (keyval, &n))
        {
-               if (keyval >= GDK_KEY_0 && keyval <= GDK_KEY_9)
-               {
-                       self->number = self->number * 10 + (keyval - GDK_KEY_0);
-                       return TRUE;
-               }
+               count = count * 10 + n;
+               gtk_source_vim_state_set_count (state, count);
+               return TRUE;
        }
 
        switch (keyval)
@@ -1133,10 +1145,8 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                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 (keyval >= GDK_KEY_0 && keyval <= GDK_KEY_9)
-                               self->number = keyval - GDK_KEY_0;
-                       else
-                               self->number = keyval - GDK_KEY_KP_0;
+                       get_number (keyval, &n);
+                       gtk_source_vim_state_set_count (state, n);
                        return TRUE;
 
                case GDK_KEY_asciicircum:
@@ -1350,6 +1360,7 @@ gtk_source_vim_motion_apply (GtkSourceVimMotion *self,
 {
        gboolean ret = FALSE;
        guint begin_offset;
+       int count;
 
        g_return_val_if_fail (GTK_SOURCE_IS_VIM_MOTION (self), FALSE);
 
@@ -1359,6 +1370,7 @@ gtk_source_vim_motion_apply (GtkSourceVimMotion *self,
        }
 
        begin_offset = gtk_text_iter_get_offset (iter);
+       count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
 
        do
        {
@@ -1366,7 +1378,7 @@ gtk_source_vim_motion_apply (GtkSourceVimMotion *self,
                {
                        goto do_inclusive;
                }
-       } while (--self->number > 1);
+       } while (--count > 1);
 
        ret = TRUE;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]