[gtksourceview/wip/chergert/vim: 74/363] add basic example for doing motion + insert




commit 6b8e1130ee8ee9a5536cdc644c525d6f0b38f732
Author: Christian Hergert <chergert redhat com>
Date:   Tue Oct 26 19:28:17 2021 -0700

    add basic example for doing motion + insert

 gtksourceview/vim/gtk-source-vim-motion.c | 22 +++++++++++++++++
 gtksourceview/vim/gtk-source-vim-motion.h | 12 ++++++----
 gtksourceview/vim/gtk-source-vim-normal.c | 40 +++++++++++++++++++++++++++----
 3 files changed, 64 insertions(+), 10 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index 010d41ad..3f3bf38d 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -1308,3 +1308,25 @@ gtk_source_vim_motion_set_apply_on_leave (GtkSourceVimMotion *self,
 
        self->apply_on_leave = !!apply_on_leave;
 }
+
+GtkSourceVimState *
+gtk_source_vim_motion_new_first_char (void)
+{
+       GtkSourceVimMotion *self;
+
+       self = g_object_new (GTK_SOURCE_TYPE_VIM_MOTION, NULL);
+       self->motion = motion_line_first_char;
+
+       return GTK_SOURCE_VIM_STATE (self);
+}
+
+GtkSourceVimState *
+gtk_source_vim_motion_new_line_end (void)
+{
+       GtkSourceVimMotion *self;
+
+       self = g_object_new (GTK_SOURCE_TYPE_VIM_MOTION, NULL);
+       self->motion = motion_line_end;
+
+       return GTK_SOURCE_VIM_STATE (self);
+}
diff --git a/gtksourceview/vim/gtk-source-vim-motion.h b/gtksourceview/vim/gtk-source-vim-motion.h
index 8961fa41..62690952 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.h
+++ b/gtksourceview/vim/gtk-source-vim-motion.h
@@ -31,10 +31,12 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtkSourceVimMotion, gtk_source_vim_motion, GTK_SOURCE, VIM_MOTION, GtkSourceVimState)
 
-GtkSourceVimState *gtk_source_vim_motion_new                (void);
-void               gtk_source_vim_motion_set_apply_on_leave (GtkSourceVimMotion *self,
-                                                             gboolean            apply_on_leave);
-gboolean           gtk_source_vim_motion_apply              (GtkSourceVimMotion *self,
-                                                             GtkTextIter        *iter);
+GtkSourceVimState *gtk_source_vim_motion_new                 (void);
+GtkSourceVimState *gtk_source_vim_motion_new_first_char      (void);
+GtkSourceVimState *gtk_source_vim_motion_new_line_end        (void);
+void               gtk_source_vim_motion_set_apply_on_leave  (GtkSourceVimMotion *self,
+                                                              gboolean            apply_on_leave);
+gboolean           gtk_source_vim_motion_apply               (GtkSourceVimMotion *self,
+                                                              GtkTextIter        *iter);
 
 G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index 7361c089..e0decfae 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -101,12 +101,38 @@ key_handler_repeat (GtkSourceVimNormal *self,
        return TRUE;
 }
 
+static void
+begin_insert (GtkSourceVimNormal *self,
+              GtkSourceVimState  *motion,
+              gboolean            after_char)
+{
+       g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
+       g_assert (!motion || GTK_SOURCE_IS_VIM_MOTION (motion));
+
+       if (motion != NULL)
+       {
+               gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), motion);
+               gtk_source_vim_state_pop (motion);
+       }
+
+       if (after_char)
+       {
+               GtkTextIter iter;
+
+               gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, NULL);
+               gtk_text_iter_forward_char (&iter);
+               gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &iter);
+       }
+
+       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), gtk_source_vim_insert_new ());
+}
+
 static gboolean
 key_handler_command (GtkSourceVimNormal *self,
-                    guint               keyval,
-                    guint               keycode,
-                    GdkModifierType     mods,
-                    const char         *string)
+                     guint               keyval,
+                     guint               keycode,
+                     GdkModifierType     mods,
+                     const char         *string)
 {
        GtkSourceVimState *new_state = NULL;
 
@@ -124,12 +150,16 @@ key_handler_command (GtkSourceVimNormal *self,
                        gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), new_state);
                        return TRUE;
 
+               case GDK_KEY_I:
+                       new_state = gtk_source_vim_motion_new_first_char ();
+                       begin_insert (self, new_state, FALSE);
+                       return TRUE;
+
                case GDK_KEY_a:
                case GDK_KEY_asciitilde:
                case GDK_KEY_A:
                case GDK_KEY_C:
                case GDK_KEY_D:
-               case GDK_KEY_I:
                case GDK_KEY_J:
                case GDK_KEY_o:
                case GDK_KEY_O:


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