[gtksourceview/wip/chergert/vim] add selection motion



commit dcafe21a8ef27aac1a4740808ac2d7e2254624e2
Author: Christian Hergert <chergert redhat com>
Date:   Thu Oct 28 14:17:14 2021 -0700

    add selection motion

 gtksourceview/vim/gtk-source-vim-insert.c | 47 ++++++++++++++++++++++++++++---
 gtksourceview/vim/gtk-source-vim-insert.h | 24 ++++++++--------
 2 files changed, 56 insertions(+), 15 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-insert.c b/gtksourceview/vim/gtk-source-vim-insert.c
index 6da3a420..51378069 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.c
+++ b/gtksourceview/vim/gtk-source-vim-insert.c
@@ -36,6 +36,7 @@ struct _GtkSourceVimInsert
        GtkSourceVimState        parent_instance;
        GtkSourceVimTextHistory *history;
        GtkSourceVimMotion      *motion;
+       GtkSourceVimMotion      *selection_motion;
        char                    *prefix;
        char                    *suffix;
        GtkSourceVimInsertAt     at;
@@ -190,16 +191,15 @@ gtk_source_vim_insert_prepare (GtkSourceVimInsert *self)
        GtkSourceBuffer *buffer;
        GtkSourceView *view;
        GtkTextIter iter;
+       GtkTextIter selection;
 
        g_assert (GTK_SOURCE_IS_VIM_INSERT (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);
+       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
 
        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 ||
@@ -214,7 +214,34 @@ gtk_source_vim_insert_prepare (GtkSourceVimInsert *self)
                        }
                }
 
-               gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &iter);
+               if (self->selection_motion == NULL)
+               {
+                       selection = iter;
+               }
+       }
+
+       if (self->selection_motion)
+       {
+               gtk_source_vim_motion_apply (self->selection_motion, &selection, 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 (&selection))
+                                       gtk_text_iter_forward_char (&selection);
+                       }
+               }
+       }
+
+       gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
+
+       if (!gtk_text_iter_equal (&iter, &selection))
+       {
+               gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &iter, &selection);
        }
 
        if (self->suffix)
@@ -372,6 +399,8 @@ gtk_source_vim_insert_dispose (GObject *object)
 
        g_clear_pointer (&self->prefix, g_free);
        g_clear_object (&self->history);
+       g_clear_object (&self->motion);
+       g_clear_object (&self->selection_motion);
 
        G_OBJECT_CLASS (gtk_source_vim_insert_parent_class)->dispose (object);
 }
@@ -477,6 +506,16 @@ gtk_source_vim_insert_set_motion (GtkSourceVimInsert *self,
        g_set_object (&self->motion, motion);
 }
 
+void
+gtk_source_vim_insert_set_selection_motion (GtkSourceVimInsert *self,
+                                            GtkSourceVimMotion *selection_motion)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_INSERT (self));
+       g_return_if_fail (GTK_SOURCE_IS_VIM_MOTION (selection_motion));
+
+       g_set_object (&self->selection_motion, selection_motion);
+}
+
 void
 gtk_source_vim_insert_set_at (GtkSourceVimInsert   *self,
                               GtkSourceVimInsertAt  at)
diff --git a/gtksourceview/vim/gtk-source-vim-insert.h b/gtksourceview/vim/gtk-source-vim-insert.h
index e745db99..6582b54c 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.h
+++ b/gtksourceview/vim/gtk-source-vim-insert.h
@@ -39,17 +39,19 @@ typedef enum
 
 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_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);
+GtkSourceVimState *gtk_source_vim_insert_new                  (void);
+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_selection_motion (GtkSourceVimInsert   *self,
+                                                               GtkSourceVimMotion   *selection_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


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