[gtksourceview/wip/chergert/vim] allow setting a mark to move



commit 09edfb7f6a6901dba2b3079da2f0b35fe69b7b9c
Author: Christian Hergert <chergert redhat com>
Date:   Sat Oct 30 14:34:57 2021 -0700

    allow setting a mark to move

 gtksourceview/vim/gtk-source-vim-motion.c | 55 ++++++++++++++++++++++++++-----
 gtksourceview/vim/gtk-source-vim-motion.h |  2 ++
 2 files changed, 49 insertions(+), 8 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index 4446bce2..c67424fe 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -40,6 +40,9 @@ struct _GtkSourceVimMotion
        /* Text as it's typed for append_command() */
        GString *command_text;
 
+       /* The mark to apply the motion to or NULL */
+       GtkTextMark *mark;
+
        /* A function to apply the motion */
        Motion motion;
 
@@ -442,19 +445,27 @@ motion_forward_char (GtkTextIter        *iter,
 
 static gboolean
 motion_forward_char_same_line (GtkTextIter        *iter,
-                               GtkSourceVimMotion *state)
+                               GtkSourceVimMotion *self)
 {
-       if (gtk_text_iter_ends_line (iter))
+       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+
+       if (self->apply_count != 1)
                return FALSE;
 
-       gtk_text_iter_forward_char (iter);
+       count = MAX (1, count);
 
-       if (gtk_text_iter_ends_line (iter) && !gtk_text_iter_starts_line (iter))
+       for (guint i = 0; i < count; i++)
        {
-               gtk_text_iter_backward_char (iter);
-               return FALSE;
+               if (gtk_text_iter_ends_line (iter))
+                       break;
+
+               if (!gtk_text_iter_forward_char (iter))
+                       break;
        }
 
+       if (gtk_text_iter_ends_line (iter) && !gtk_text_iter_starts_line (iter))
+               gtk_text_iter_backward_char (iter);
+
        return TRUE;
 }
 
@@ -1264,6 +1275,7 @@ static void
 gtk_source_vim_motion_repeat (GtkSourceVimState *state)
 {
        GtkSourceVimMotion *self = (GtkSourceVimMotion *)state;
+       GtkSourceBuffer *buffer;
        GtkTextIter iter;
        int count;
 
@@ -1274,16 +1286,32 @@ gtk_source_vim_motion_repeat (GtkSourceVimState *state)
                return;
        }
 
-       gtk_source_vim_state_get_buffer (state, &iter, NULL);
+       buffer = gtk_source_vim_state_get_buffer (state, &iter, NULL);
        count = gtk_source_vim_state_get_count (state);
 
+       if (self->mark != NULL)
+       {
+               gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer),
+                                                 &iter,
+                                                 self->mark);
+       }
+
        do
        {
                if (!gtk_source_vim_motion_apply (self, &iter, FALSE))
                        break;
        } while (--count > 0);
 
-       gtk_source_vim_state_select (state, &iter, &iter);
+       if (self->mark != NULL)
+       {
+               gtk_text_buffer_move_mark (GTK_TEXT_BUFFER (buffer),
+                                          self->mark,
+                                          &iter);
+       }
+       else
+       {
+               gtk_source_vim_state_select (state, &iter, &iter);
+       }
 }
 
 static void
@@ -1321,6 +1349,7 @@ gtk_source_vim_motion_finalize (GObject *object)
 {
        GtkSourceVimMotion *self = (GtkSourceVimMotion *)object;
 
+       g_clear_object (&self->mark);
        g_string_free (self->command_text, TRUE);
        self->command_text = NULL;
 
@@ -1406,6 +1435,16 @@ gtk_source_vim_motion_set_apply_on_leave (GtkSourceVimMotion *self,
        self->apply_on_leave = !!apply_on_leave;
 }
 
+void
+gtk_source_vim_motion_set_mark (GtkSourceVimMotion *self,
+                                GtkTextMark        *mark)
+{
+       g_assert (GTK_SOURCE_IS_VIM_MOTION (self));
+       g_assert (!mark || GTK_IS_TEXT_MARK (mark));
+
+       g_set_object (&self->mark, mark);
+}
+
 GtkSourceVimState *
 gtk_source_vim_motion_new_first_char (void)
 {
diff --git a/gtksourceview/vim/gtk-source-vim-motion.h b/gtksourceview/vim/gtk-source-vim-motion.h
index e20cb924..ae2f7632 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.h
+++ b/gtksourceview/vim/gtk-source-vim-motion.h
@@ -42,6 +42,8 @@ GtkSourceVimState *gtk_source_vim_motion_new_forward_char          (void);
 GtkSourceVimState *gtk_source_vim_motion_new_line_start            (void);
 void               gtk_source_vim_motion_set_apply_on_leave        (GtkSourceVimMotion *self,
                                                                     gboolean            apply_on_leave);
+void               gtk_source_vim_motion_set_mark                  (GtkSourceVimMotion *self,
+                                                                    GtkTextMark        *mark);
 gboolean           gtk_source_vim_motion_apply                     (GtkSourceVimMotion *self,
                                                                     GtkTextIter        *iter,
                                                                     gboolean            apply_inclusive);


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