[gtksourceview/wip/chergert/vim] implement zz zt and zb in visual mode



commit af97442f141f509609a468afb369e1ee179ad0d7
Author: Christian Hergert <chergert redhat com>
Date:   Sat Oct 30 16:06:56 2021 -0700

    implement zz zt and zb in visual mode

 gtksourceview/vim/gtk-source-vim-visual.c | 85 ++++++++++++++++++++++++++++---
 1 file changed, 77 insertions(+), 8 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-visual.c b/gtksourceview/vim/gtk-source-vim-visual.c
index fdd9ace5..89c22ebf 100644
--- a/gtksourceview/vim/gtk-source-vim-visual.c
+++ b/gtksourceview/vim/gtk-source-vim-visual.c
@@ -26,16 +26,26 @@
 #include "gtk-source-vim-motion.h"
 #include "gtk-source-vim-visual.h"
 
+typedef gboolean (*KeyHandler) (GtkSourceVimVisual *self,
+                                guint               keyval,
+                                guint               keycode,
+                                GdkModifierType     mods,
+                                const char         *string);
+
 struct _GtkSourceVimVisual
 {
        GtkSourceVimState parent_class;
 
        GtkSourceVimVisualMode mode;
 
+       KeyHandler handler;
+
        GtkTextMark *started_at;
        GtkTextMark *cursor;
 };
 
+static gboolean gtk_source_vim_visual_bail (GtkSourceVimVisual *self);
+
 G_DEFINE_TYPE (GtkSourceVimVisual, gtk_source_vim_visual, GTK_SOURCE_TYPE_VIM_STATE)
 
 static void
@@ -189,6 +199,69 @@ gtk_source_vim_visual_get_command_bar_text (GtkSourceVimState *state)
        }
 }
 
+static gboolean
+key_handler_z (GtkSourceVimVisual *self,
+              guint               keyval,
+              guint               keycode,
+              GdkModifierType     mods,
+              const char         *string)
+{
+       GtkSourceVimState *state = GTK_SOURCE_VIM_STATE (self);
+
+       switch (keyval)
+       {
+               case GDK_KEY_z:
+                       gtk_source_vim_state_z_scroll (state, 0.5);
+                       return TRUE;
+
+               case GDK_KEY_b:
+                       gtk_source_vim_state_z_scroll (state, 1.0);
+                       return TRUE;
+
+               case GDK_KEY_t:
+                       gtk_source_vim_state_z_scroll (state, 0.0);
+                       return TRUE;
+
+               default:
+                       return gtk_source_vim_visual_bail (self);
+       }
+}
+
+static gboolean
+key_handler_initial (GtkSourceVimVisual *self,
+                     guint               keyval,
+                     guint               keycode,
+                     GdkModifierType     mods,
+                     const char         *string)
+{
+       GtkSourceVimState *motion;
+
+       switch (keyval)
+       {
+               case GDK_KEY_z:
+                       self->handler = key_handler_z;
+                       return TRUE;
+
+               default:
+                       break;
+       }
+
+       /* Try to apply a motion to our cursor */
+       motion = gtk_source_vim_motion_new ();
+       gtk_source_vim_motion_set_mark (GTK_SOURCE_VIM_MOTION (motion), self->cursor);
+       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), motion);
+       gtk_source_vim_state_synthesize (motion, keyval, mods);
+
+       return TRUE;
+}
+
+static gboolean
+gtk_source_vim_visual_bail (GtkSourceVimVisual *self)
+{
+       self->handler = key_handler_initial;
+       return TRUE;
+}
+
 static void
 gtk_source_vim_visual_enter (GtkSourceVimState *state)
 {
@@ -257,6 +330,8 @@ gtk_source_vim_visual_resume (GtkSourceVimState *state,
        {
                gtk_source_vim_visual_track_motion (self);
        }
+
+       self->handler = key_handler_initial;
 }
 
 static gboolean
@@ -267,7 +342,6 @@ gtk_source_vim_visual_handle_keypress (GtkSourceVimState *state,
                                        const char        *string)
 {
        GtkSourceVimVisual *self = (GtkSourceVimVisual *)state;
-       GtkSourceVimState *motion;
 
        g_assert (GTK_SOURCE_IS_VIM_VISUAL (state));
 
@@ -278,13 +352,7 @@ gtk_source_vim_visual_handle_keypress (GtkSourceVimState *state,
                return TRUE;
        }
 
-       /* Try to apply a motion to our cursor */
-       motion = gtk_source_vim_motion_new ();
-       gtk_source_vim_motion_set_mark (GTK_SOURCE_VIM_MOTION (motion), self->cursor);
-       gtk_source_vim_state_push (state, motion);
-       gtk_source_vim_state_synthesize (motion, keyval, mods);
-
-       return TRUE;
+       return self->handler (self, keyval, keycode, mods, string);
 }
 
 static void
@@ -331,6 +399,7 @@ gtk_source_vim_visual_class_init (GtkSourceVimVisualClass *klass)
 static void
 gtk_source_vim_visual_init (GtkSourceVimVisual *self)
 {
+       self->handler = key_handler_initial;
 }
 
 GtkSourceVimState *


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