[gtksourceview/wip/chergert/vim] avoid cursor on line ends



commit c1b6739e4da286529b8e6d36ef738f9102c56f27
Author: Christian Hergert <chergert redhat com>
Date:   Wed Oct 27 12:55:09 2021 -0700

    avoid cursor on line ends

 gtksourceview/vim/gtk-source-vim-normal.c | 51 ++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 15 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index d95d434b..2fa97f28 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -67,6 +67,34 @@ gtk_source_vim_normal_bail (GtkSourceVimNormal *self)
        return TRUE;
 }
 
+static void
+go_backward_char (GtkSourceVimNormal *self)
+{
+       GtkTextIter iter;
+
+       g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
+
+       gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, NULL);
+       if (!gtk_text_iter_starts_line (&iter) && gtk_text_iter_backward_char (&iter))
+               gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &iter);
+}
+
+static void
+keep_on_last_char (GtkSourceVimNormal *self)
+{
+       GtkTextIter iter;
+
+       g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
+
+       gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, NULL);
+
+       if (!gtk_text_iter_starts_line (&iter) && gtk_text_iter_ends_line (&iter))
+       {
+               gtk_text_iter_backward_char (&iter);
+               gtk_source_vim_state_select (GTK_SOURCE_VIM_STATE (self), &iter, &iter);
+       }
+}
+
 static gboolean
 key_handler_repeat (GtkSourceVimNormal *self,
                     guint               keyval,
@@ -116,14 +144,20 @@ key_handler_undo (GtkSourceVimNormal *self,
 
        buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), NULL, NULL);
 
+       /* TODO: ideally we'd land on the position we were before the undo, but
+        * the undo stack in GTK does not do that yet.
+        */
+
        switch (keyval)
        {
                case GDK_KEY_u:
                        REPEAT (gtk_text_buffer_undo (GTK_TEXT_BUFFER (buffer)));
+                       keep_on_last_char (self);
                        break;
 
                case GDK_KEY_r:
                        REPEAT (gtk_text_buffer_redo (GTK_TEXT_BUFFER (buffer)));
+                       keep_on_last_char (self);
                        break;
 
                default:
@@ -844,21 +878,8 @@ gtk_source_vim_normal_resume (GtkSourceVimState *state,
        }
 
        /* Go back one character if we exited replace/insert state */
-       if (GTK_SOURCE_IS_VIM_INSERT (from) ||
-           GTK_SOURCE_IS_VIM_REPLACE (from))
-       {
-               GtkTextIter iter;
-
-               gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer),
-                                                 &iter, insert);
-
-               if (!gtk_text_iter_starts_line (&iter) &&
-                   gtk_text_iter_backward_char (&iter))
-               {
-                       gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
-                                                     &iter, &iter);
-               }
-       }
+       if (GTK_SOURCE_IS_VIM_INSERT (from) || GTK_SOURCE_IS_VIM_REPLACE (from))
+               go_backward_char (self);
 
        gtk_source_vim_normal_clear (GTK_SOURCE_VIM_NORMAL (state));
        gtk_source_vim_state_set_overwrite (state, TRUE);


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