[gtksourceview/wip/chergert/vim] make code more readable with va args



commit 44ff4831e6a1ba9ce6a3acd576a4780fe30e2afe
Author: Christian Hergert <chergert redhat com>
Date:   Wed Oct 27 11:35:06 2021 -0700

    make code more readable with va args

 gtksourceview/vim/gtk-source-vim-normal.c | 131 +++++++++++++++++++++---------
 1 file changed, 91 insertions(+), 40 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index 818879fb..bafb4cb7 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -103,36 +103,6 @@ key_handler_repeat (GtkSourceVimNormal *self,
        return TRUE;
 }
 
-static GtkSourceVimState *
-begin_insert (GtkSourceVimNormal *self,
-              GtkSourceVimState  *motion,
-              gboolean            after_char)
-{
-       GtkSourceVimState *ret;
-
-       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);
-       }
-
-       ret = gtk_source_vim_insert_new ();
-       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), ret);
-       return ret;
-}
-
 static gboolean
 key_handler_undo (GtkSourceVimNormal *self,
                   guint               keyval,
@@ -165,6 +135,60 @@ key_handler_undo (GtkSourceVimNormal *self,
        return TRUE;
 }
 
+typedef enum
+{
+       INSERT_HERE,
+       INSERT_AFTER_CHAR,
+       INSERT_AFTER_CHAR_UNLESS_SOL,
+       INSERT_AFTER_CHAR_UNLESS_BOF,
+} InsertAt;
+
+G_GNUC_NULL_TERMINATED
+static GtkSourceVimState *
+gtk_source_vim_normal_begin_insert (GtkSourceVimNormal *self,
+                                    GtkSourceVimState  *motion,
+                                    InsertAt            at,
+                                    ...)
+{
+       GtkSourceVimState *ret;
+       const char *first_property_name;
+       va_list args;
+
+       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 (g_steal_pointer (&motion));
+       }
+
+       if (at == INSERT_AFTER_CHAR || at == INSERT_AFTER_CHAR_UNLESS_BOF)
+       {
+               GtkTextIter iter;
+
+               gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, NULL);
+
+               if (at == INSERT_AFTER_CHAR ||
+                   (at == INSERT_AFTER_CHAR_UNLESS_BOF && !gtk_text_iter_is_start (&iter)) ||
+                   (at == INSERT_AFTER_CHAR_UNLESS_SOL && !gtk_text_iter_starts_line (&iter)))
+               {
+                       gtk_text_iter_forward_char (&iter);
+               }
+
+               gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &iter);
+       }
+
+       va_start (args, at);
+       first_property_name = va_arg (args, const char *);
+       ret = GTK_SOURCE_VIM_STATE (g_object_new_valist (GTK_SOURCE_TYPE_VIM_INSERT, first_property_name, 
args));
+       va_end (args);
+
+       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), ret);
+
+       return ret;
+}
+
 static gboolean
 key_handler_command (GtkSourceVimNormal *self,
                      guint               keyval,
@@ -178,34 +202,61 @@ key_handler_command (GtkSourceVimNormal *self,
 
        switch (keyval)
        {
-               case GDK_KEY_i:
-                       begin_insert (self, gtk_source_vim_motion_new_none (), FALSE);
-                       return TRUE;
-
                case GDK_KEY_R:
                        new_state = gtk_source_vim_replace_new ();
                        gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), new_state);
                        return TRUE;
 
+               case GDK_KEY_i:
+                       gtk_source_vim_normal_begin_insert (self,
+                                                           gtk_source_vim_motion_new_none (),
+                                                           INSERT_HERE,
+                                                           NULL);
+                       return TRUE;
+
                case GDK_KEY_I:
-                       begin_insert (self, gtk_source_vim_motion_new_first_char (), FALSE);
+                       gtk_source_vim_normal_begin_insert (self,
+                                                           gtk_source_vim_motion_new_first_char (),
+                                                           INSERT_HERE,
+                                                           NULL);
+                       return TRUE;
+
+               case GDK_KEY_a:
+                       gtk_source_vim_normal_begin_insert (self,
+                                                           gtk_source_vim_motion_new_none (),
+                                                           INSERT_AFTER_CHAR,
+                                                           NULL);
                        return TRUE;
 
                case GDK_KEY_A:
-                       begin_insert (self, gtk_source_vim_motion_new_line_end (), TRUE);
+                       gtk_source_vim_normal_begin_insert (self,
+                                                           gtk_source_vim_motion_new_line_end (),
+                                                           INSERT_AFTER_CHAR,
+                                                           NULL);
                        return TRUE;
 
                case GDK_KEY_o:
-                       new_state = begin_insert (self, gtk_source_vim_motion_new_line_end (), TRUE);
-                       /* TODO: synthesize key event for \n so we get indentation */
+                       gtk_source_vim_normal_begin_insert (self,
+                                                           gtk_source_vim_motion_new_line_end (),
+                                                           INSERT_AFTER_CHAR,
+                                                           "prefix", "\n",
+                                                           "indent", TRUE,
+                                                           NULL);
+                       return TRUE;
+
+               case GDK_KEY_O:
+                       gtk_source_vim_normal_begin_insert (self,
+                                                           gtk_source_vim_motion_new_line_start (),
+                                                           INSERT_HERE,
+                                                           "suffix", "\n",
+                                                           "indent", TRUE,
+                                                           NULL);
                        return TRUE;
 
-               case GDK_KEY_a:
                case GDK_KEY_asciitilde:
                case GDK_KEY_C:
                case GDK_KEY_D:
                case GDK_KEY_J:
-               case GDK_KEY_O:
                case GDK_KEY_p:
                case GDK_KEY_P:
                case GDK_KEY_period:


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