[gtksourceview/wip/chergert/vim] fix motion with insert



commit 1c938885f5262c71b566eabcfe8ca556729f1247
Author: Christian Hergert <chergert redhat com>
Date:   Wed Oct 27 21:41:11 2021 -0700

    fix motion with insert

 gtksourceview/vim/gtk-source-vim-insert.c | 43 ++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-insert.h | 26 ++++++++++----
 gtksourceview/vim/gtk-source-vim-normal.c | 59 ++++++++++---------------------
 3 files changed, 81 insertions(+), 47 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-insert.c b/gtksourceview/vim/gtk-source-vim-insert.c
index 7df69a71..e7f84473 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.c
+++ b/gtksourceview/vim/gtk-source-vim-insert.c
@@ -35,8 +35,10 @@ struct _GtkSourceVimInsert
 {
        GtkSourceVimState        parent_instance;
        GtkSourceVimTextHistory *history;
+       GtkSourceVimMotion      *motion;
        char                    *prefix;
        char                    *suffix;
+       GtkSourceVimInsertAt     at;
        guint                    indent : 1;
        guint                    finished : 1;
 };
@@ -194,6 +196,27 @@ gtk_source_vim_insert_prepare (GtkSourceVimInsert *self)
        view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
        buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, NULL);
 
+       if (self->motion)
+       {
+               buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, NULL);
+
+               gtk_source_vim_motion_apply (self->motion, &iter, TRUE);
+
+               if (self->at == GTK_SOURCE_VIM_INSERT_AFTER_CHAR ||
+                   self->at == GTK_SOURCE_VIM_INSERT_AFTER_CHAR_UNLESS_BOF)
+               {
+                       if (self->at == GTK_SOURCE_VIM_INSERT_AFTER_CHAR ||
+                           (self->at == GTK_SOURCE_VIM_INSERT_AFTER_CHAR_UNLESS_BOF && 
!gtk_text_iter_is_start (&iter)) ||
+                           (self->at == GTK_SOURCE_VIM_INSERT_AFTER_CHAR_UNLESS_SOL && 
!gtk_text_iter_starts_line (&iter)))
+                       {
+                               if (!gtk_text_iter_ends_line (&iter))
+                                       gtk_text_iter_forward_char (&iter);
+                       }
+               }
+
+               gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &iter);
+       }
+
        if (self->suffix)
        {
                gsize len = g_utf8_strlen (self->suffix, -1);
@@ -398,6 +421,7 @@ gtk_source_vim_insert_class_init (GtkSourceVimInsertClass *klass)
 static void
 gtk_source_vim_insert_init (GtkSourceVimInsert *self)
 {
+       self->at = GTK_SOURCE_VIM_INSERT_HERE;
 }
 
 void
@@ -442,3 +466,22 @@ gtk_source_vim_insert_set_indent (GtkSourceVimInsert *self,
                g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INDENT]);
        }
 }
+
+void
+gtk_source_vim_insert_set_motion (GtkSourceVimInsert *self,
+                                  GtkSourceVimMotion *motion)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_INSERT (self));
+       g_return_if_fail (GTK_SOURCE_IS_VIM_MOTION (motion));
+
+       g_set_object (&self->motion, motion);
+}
+
+void
+gtk_source_vim_insert_set_at (GtkSourceVimInsert   *self,
+                              GtkSourceVimInsertAt  at)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_INSERT (self));
+
+       self->at = at;
+}
diff --git a/gtksourceview/vim/gtk-source-vim-insert.h b/gtksourceview/vim/gtk-source-vim-insert.h
index ff1d5744..e745db99 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.h
+++ b/gtksourceview/vim/gtk-source-vim-insert.h
@@ -21,21 +21,35 @@
 
 #pragma once
 
+#include "gtk-source-vim-motion.h"
 #include "gtk-source-vim-state.h"
 
 G_BEGIN_DECLS
 
+typedef enum
+{
+
+       GTK_SOURCE_VIM_INSERT_HERE,
+       GTK_SOURCE_VIM_INSERT_AFTER_CHAR,
+       GTK_SOURCE_VIM_INSERT_AFTER_CHAR_UNLESS_BOF,
+       GTK_SOURCE_VIM_INSERT_AFTER_CHAR_UNLESS_SOL,
+} GtkSourceVimInsertAt;
+
 #define GTK_SOURCE_TYPE_VIM_INSERT (gtk_source_vim_insert_get_type())
 
 G_DECLARE_FINAL_TYPE (GtkSourceVimInsert, gtk_source_vim_insert, GTK_SOURCE, VIM_INSERT, GtkSourceVimState)
 
 GtkSourceVimState *gtk_source_vim_insert_new        (void);
-void               gtk_source_vim_insert_set_indent (GtkSourceVimInsert *self,
-                                                     gboolean            indent);
-void               gtk_source_vim_insert_set_prefix (GtkSourceVimInsert *self,
-                                                     const char         *prefix);
-void               gtk_source_vim_insert_set_suffix (GtkSourceVimInsert *self,
-                                                     const char         *suffix);
+void               gtk_source_vim_insert_set_at     (GtkSourceVimInsert   *self,
+                                                     GtkSourceVimInsertAt  at);
+void               gtk_source_vim_insert_set_motion (GtkSourceVimInsert   *self,
+                                                     GtkSourceVimMotion   *motion);
+void               gtk_source_vim_insert_set_indent (GtkSourceVimInsert   *self,
+                                                     gboolean              indent);
+void               gtk_source_vim_insert_set_prefix (GtkSourceVimInsert   *self,
+                                                     const char           *prefix);
+void               gtk_source_vim_insert_set_suffix (GtkSourceVimInsert   *self,
+                                                     const char           *suffix);
 
 
 G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index c3227167..bea3909b 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -237,19 +237,11 @@ gtk_source_vim_normal_begin_change (GtkSourceVimNormal *self,
        return ret;
 }
 
-typedef enum
-{
-       INSERT_HERE,
-       INSERT_AFTER_CHAR,
-       INSERT_AFTER_CHAR_UNLESS_BOF,
-       INSERT_AFTER_CHAR_UNLESS_SOL,
-} InsertAt;
-
 G_GNUC_NULL_TERMINATED
 static GtkSourceVimState *
-gtk_source_vim_normal_begin_insert (GtkSourceVimNormal *self,
-                                    GtkSourceVimState  *motion,
-                                    InsertAt            at,
+gtk_source_vim_normal_begin_insert (GtkSourceVimNormal   *self,
+                                    GtkSourceVimState    *motion,
+                                    GtkSourceVimInsertAt  at,
                                     ...)
 {
        GtkSourceVimState *ret;
@@ -262,34 +254,19 @@ gtk_source_vim_normal_begin_insert (GtkSourceVimNormal *self,
 
        count = self->count;
 
-       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)))
-               {
-                       if (!gtk_text_iter_ends_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);
 
+       if (motion != NULL)
+       {
+               gtk_source_vim_motion_set_apply_on_leave (GTK_SOURCE_VIM_MOTION (motion), FALSE);
+               gtk_source_vim_insert_set_at (GTK_SOURCE_VIM_INSERT (ret), at);
+               gtk_source_vim_insert_set_motion (GTK_SOURCE_VIM_INSERT (ret),
+                                                 GTK_SOURCE_VIM_MOTION (motion));
+       }
+
        gtk_source_vim_state_set_count (ret, count);
        gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), ret);
 
@@ -318,35 +295,35 @@ key_handler_command (GtkSourceVimNormal *self,
                case GDK_KEY_i:
                        gtk_source_vim_normal_begin_insert (self,
                                                            gtk_source_vim_motion_new_none (),
-                                                           INSERT_HERE,
+                                                           GTK_SOURCE_VIM_INSERT_HERE,
                                                            NULL);
                        return TRUE;
 
                case GDK_KEY_I:
                        gtk_source_vim_normal_begin_insert (self,
                                                            gtk_source_vim_motion_new_first_char (),
-                                                           INSERT_HERE,
+                                                           GTK_SOURCE_VIM_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,
+                                                           GTK_SOURCE_VIM_INSERT_AFTER_CHAR,
                                                            NULL);
                        return TRUE;
 
                case GDK_KEY_A:
                        gtk_source_vim_normal_begin_insert (self,
                                                            gtk_source_vim_motion_new_line_end (),
-                                                           INSERT_AFTER_CHAR,
+                                                           GTK_SOURCE_VIM_INSERT_AFTER_CHAR,
                                                            NULL);
                        return TRUE;
 
                case GDK_KEY_o:
                        gtk_source_vim_normal_begin_insert (self,
                                                            gtk_source_vim_motion_new_line_end (),
-                                                           INSERT_AFTER_CHAR,
+                                                           GTK_SOURCE_VIM_INSERT_AFTER_CHAR,
                                                            "prefix", "\n",
                                                            "indent", TRUE,
                                                            NULL);
@@ -355,7 +332,7 @@ key_handler_command (GtkSourceVimNormal *self,
                case GDK_KEY_O:
                        gtk_source_vim_normal_begin_insert (self,
                                                            gtk_source_vim_motion_new_line_start (),
-                                                           INSERT_HERE,
+                                                           GTK_SOURCE_VIM_INSERT_HERE,
                                                            "suffix", "\n",
                                                            "indent", TRUE,
                                                            NULL);
@@ -366,7 +343,7 @@ key_handler_command (GtkSourceVimNormal *self,
                                return gtk_source_vim_normal_bail (self);
                        gtk_source_vim_normal_begin_change (self,
                                                            gtk_source_vim_motion_new_line_end (),
-                                                           INSERT_HERE,
+                                                           GTK_SOURCE_VIM_INSERT_HERE,
                                                            NULL);
                        return TRUE;
 


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