[gtksourceview/wip/chergert/vim] simplify command setup



commit b763da83bed4140f9ffbd4d5c7a8afa471096996
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 3 12:45:26 2021 -0700

    simplify command setup
    
    this will likely have more stuff going on where we want duplicated char
    for linewise commands like yy or dd

 gtksourceview/vim/gtk-source-vim-normal.c | 115 ++++++++++++------------------
 1 file changed, 46 insertions(+), 69 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index 3247599e..ca99be9b 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -213,9 +213,11 @@ static void
 gtk_source_vim_normal_begin_command (GtkSourceVimNormal *self,
                                      GtkSourceVimState  *insert_motion,
                                      GtkSourceVimState  *selection_motion,
-                                     const char         *command_str)
+                                     const char         *command_str,
+                                     guint               linewise_keyval)
 {
        GtkSourceVimCommand *command;
+       gboolean pop_command = TRUE;
        int count;
 
        g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
@@ -224,32 +226,53 @@ gtk_source_vim_normal_begin_command (GtkSourceVimNormal *self,
 
        count = self->count, self->count = 0;
 
-       if (insert_motion)
+       if (insert_motion != NULL)
                gtk_source_vim_state_set_count (GTK_SOURCE_VIM_STATE (insert_motion), count);
 
        if (selection_motion)
                gtk_source_vim_state_set_count (GTK_SOURCE_VIM_STATE (selection_motion), count);
 
        command = g_object_new (GTK_SOURCE_TYPE_VIM_COMMAND,
-                               "motion", insert_motion,
-                               "selection-motion", selection_motion,
-                               "command", command_str,
-                               NULL);
+                               "motion", insert_motion,
+                               "selection-motion", selection_motion,
+                               "command", command_str,
+                               NULL);
 
-       /* If there are no motions, then we need to apply the count
-        * somewhere, just send it to the command.
+       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self),
+                                  GTK_SOURCE_VIM_STATE (command));
+
+       /* If there is not yet a motion to apply, then that will get
+        * applied to the command as a whole (which will then in turn
+        * repeat motions).
         */
-       if (insert_motion == NULL && selection_motion == NULL)
+       if (insert_motion == NULL)
        {
                gtk_source_vim_state_set_count (GTK_SOURCE_VIM_STATE (command), count);
+
+               /* If we got a linewise keyval, then we want to let the motion
+                * know to use the gtk_source_vim_motion_new_down(-1) style
+                * motion. Generally for things like yy, dd, etc.
+                */
+               if (linewise_keyval != 0)
+               {
+                       insert_motion = gtk_source_vim_motion_new ();
+                       gtk_source_vim_motion_set_apply_on_leave (GTK_SOURCE_VIM_MOTION (insert_motion),
+                                                                 FALSE);
+                       gtk_source_vim_motion_set_linewise_keyval (GTK_SOURCE_VIM_MOTION (insert_motion),
+                                                                  linewise_keyval);
+                       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (command),
+                                                  g_object_ref (insert_motion));
+                       pop_command = FALSE;
+               }
        }
 
        g_clear_object (&insert_motion);
        g_clear_object (&selection_motion);
 
-       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self),
-                                  GTK_SOURCE_VIM_STATE (command));
-       gtk_source_vim_state_pop (GTK_SOURCE_VIM_STATE (command));
+       if (pop_command)
+       {
+               gtk_source_vim_state_pop (GTK_SOURCE_VIM_STATE (command));
+       }
 }
 
 static gboolean
@@ -470,17 +493,17 @@ key_handler_command (GtkSourceVimNormal *self,
                        gtk_source_vim_normal_begin_command (self,
                                                             gtk_source_vim_motion_new_next_line_end_with_nl 
(),
                                                             gtk_source_vim_motion_new_line_start (),
-                                                            ":join");
+                                                            ":join", 0);
                        return TRUE;
 
                case GDK_KEY_u:
-                       gtk_source_vim_normal_begin_command (self, NULL, NULL, ":undo");
+                       gtk_source_vim_normal_begin_command (self, NULL, NULL, ":undo", 0);
                        return TRUE;
 
                case GDK_KEY_r:
                        if ((mods & GDK_CONTROL_MASK) != 0)
                        {
-                               gtk_source_vim_normal_begin_command (self, NULL, NULL, ":redo");
+                               gtk_source_vim_normal_begin_command (self, NULL, NULL, ":redo", 0);
                                return TRUE;
                        }
                        break;
@@ -499,22 +522,22 @@ key_handler_command (GtkSourceVimNormal *self,
                        gtk_source_vim_normal_begin_command (self,
                                                             gtk_source_vim_motion_new_down (-1),
                                                             gtk_source_vim_motion_new_none (),
-                                                            ":yank");
+                                                            ":yank", 0);
                        return TRUE;
 
                case GDK_KEY_p:
-                       gtk_source_vim_normal_begin_command (self, NULL, NULL, "paste-after");
+                       gtk_source_vim_normal_begin_command (self, NULL, NULL, "paste-after", 0);
                        return TRUE;
 
                case GDK_KEY_P:
-                       gtk_source_vim_normal_begin_command (self, NULL, NULL, "paste-before");
+                       gtk_source_vim_normal_begin_command (self, NULL, NULL, "paste-before", 0);
                        return TRUE;
 
                case GDK_KEY_asciitilde:
                        gtk_source_vim_normal_begin_command (self,
                                                             gtk_source_vim_motion_new_forward_char (),
                                                             NULL,
-                                                            "toggle-case");
+                                                            "toggle-case", 0);
                        return TRUE;
 
                case GDK_KEY_equal:
@@ -673,55 +696,6 @@ key_handler_search (GtkSourceVimNormal *self,
        return TRUE;
 }
 
-static gboolean
-key_handler_yank (GtkSourceVimNormal *self,
-                  guint               keyval,
-                  guint               keycode,
-                  GdkModifierType     mods,
-                  const char         *string)
-{
-       GtkSourceVimState *new_state;
-       GtkSourceVimState *selection_motion;
-       GtkSourceVimState *insert_motion;
-       int count;
-
-       g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
-
-       switch (keyval)
-       {
-               case GDK_KEY_y:
-                       gtk_source_vim_normal_begin_command (self,
-                                                            gtk_source_vim_motion_new_down (-1),
-                                                            gtk_source_vim_motion_new_none (),
-                                                            ":yank");
-                       return TRUE;
-
-               default:
-                       break;
-       }
-
-       selection_motion = gtk_source_vim_motion_new_none ();
-       insert_motion = gtk_source_vim_motion_new ();
-       gtk_source_vim_motion_set_apply_on_leave (GTK_SOURCE_VIM_MOTION (insert_motion), FALSE);
-
-       /* Apply count to motion */
-       count = self->count, self->count = 0;
-       gtk_source_vim_state_set_count (GTK_SOURCE_VIM_STATE (insert_motion), count);
-
-       new_state = g_object_new (GTK_SOURCE_TYPE_VIM_COMMAND,
-                                 "command", ":yank",
-                                 "selection-motion", selection_motion,
-                                 NULL);
-
-       g_object_unref (selection_motion);
-
-       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), new_state);
-       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (new_state), insert_motion);
-       gtk_source_vim_state_synthesize (GTK_SOURCE_VIM_STATE (insert_motion), keyval, mods);
-
-       return TRUE;
-}
-
 static gboolean
 key_handler_register (GtkSourceVimNormal *self,
                       guint               keyval,
@@ -957,7 +931,10 @@ key_handler_initial (GtkSourceVimNormal *self,
                                return TRUE;
 
                        case GDK_KEY_y:
-                               self->handler = key_handler_yank;
+                               gtk_source_vim_normal_begin_command (self,
+                                                                    NULL,
+                                                                    gtk_source_vim_motion_new_none (),
+                                                                    ":yank", GDK_KEY_y);
                                return TRUE;
 
                        case GDK_KEY_d:


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