[gtksourceview/wip/chergert/vim: 28/73] stub out a bit of replace mode




commit c15b3090f93561e777e0a469ef657bf995f75ac1
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 22 10:27:10 2021 -0700

    stub out a bit of replace mode

 gtksourceview/vim/gtk-source-vim-insert.c  | 33 ++++++++++-----
 gtksourceview/vim/gtk-source-vim-normal.c  | 25 ++++++++++-
 gtksourceview/vim/gtk-source-vim-replace.c | 68 ++++++++++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-replace.h |  2 +-
 gtksourceview/vim/meson.build              |  1 +
 5 files changed, 116 insertions(+), 13 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-insert.c b/gtksourceview/vim/gtk-source-vim-insert.c
index 4643b0d0..f10cfdf2 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.c
+++ b/gtksourceview/vim/gtk-source-vim-insert.c
@@ -22,6 +22,7 @@
 #include "config.h"
 
 #include "gtk-source-vim-insert.h"
+#include "gtk-source-vim-replace.h"
 
 struct _GtkSourceVimInsert
 {
@@ -170,6 +171,17 @@ gtk_source_vim_insert_handle_event (GtkSourceVimState *state,
                }
        }
 
+       switch (keyval)
+       {
+               case GDK_KEY_Insert:
+                       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self),
+                                                  gtk_source_vim_replace_new ());
+                       break;
+
+               default:
+                       break;
+       }
+
        /* Because we're in overwrite mode, we need to deliver the
         * event directly to the textview so that we can avoid
         * overwriting the content at the current character.
@@ -179,20 +191,19 @@ gtk_source_vim_insert_handle_event (GtkSourceVimState *state,
 }
 
 static void
-gtk_source_vim_insert_leave (GtkSourceVimState *state)
+gtk_source_vim_insert_restore (GtkSourceVimState *state,
+                               GtkSourceVimState *from)
 {
-       GtkSourceBuffer *buffer;
-       GtkTextIter insert;
-
        g_assert (GTK_SOURCE_IS_VIM_INSERT (state));
+       g_assert (GTK_SOURCE_IS_VIM_STATE (from));
 
-       buffer = gtk_source_vim_state_get_buffer (state, &insert, NULL);
-
-       /* Move the cursor back one character */
-       if (!gtk_text_iter_starts_line (&insert) &&
-           gtk_text_iter_backward_char (&insert))
+       if (GTK_SOURCE_IS_VIM_REPLACE (from))
        {
-               gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer), &insert, &insert);
+               /* If we are leaving replace mode back to insert then
+                * we need also exit insert mode so we end up back on
+                * Normal mode.
+                */
+               gtk_source_vim_state_pop (state);
        }
 }
 
@@ -211,7 +222,7 @@ gtk_source_vim_insert_class_init (GtkSourceVimInsertClass *klass)
        object_class->dispose = gtk_source_vim_insert_dispose;
 
        state_class->handle_event = gtk_source_vim_insert_handle_event;
-       state_class->leave = gtk_source_vim_insert_leave;
+       state_class->restore = gtk_source_vim_insert_restore;
 }
 
 static void
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index e3471fca..db1010af 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -23,6 +23,7 @@
 
 #include "gtk-source-vim-insert.h"
 #include "gtk-source-vim-normal.h"
+#include "gtk-source-vim-replace.h"
 
 typedef gboolean (*KeyHandler) (GtkSourceVimNormal *self,
                                 guint               keyval,
@@ -126,6 +127,11 @@ key_handler_command (GtkSourceVimNormal *self,
                        gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), new_state);
                        return TRUE;
 
+               case GDK_KEY_R:
+                       new_state = gtk_source_vim_replace_new ();
+                       gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self), new_state);
+                       return TRUE;
+
                case GDK_KEY_a:
                case GDK_KEY_asciitilde:
                case GDK_KEY_A:
@@ -138,7 +144,6 @@ key_handler_command (GtkSourceVimNormal *self,
                case GDK_KEY_p:
                case GDK_KEY_P:
                case GDK_KEY_period:
-               case GDK_KEY_R:
                case GDK_KEY_s:
                case GDK_KEY_S:
                case GDK_KEY_u:
@@ -508,6 +513,24 @@ gtk_source_vim_normal_restore (GtkSourceVimState *state,
        g_assert (GTK_SOURCE_IS_VIM_STATE (from));
 
        gtk_source_vim_normal_clear (GTK_SOURCE_VIM_NORMAL (state));
+
+       /* Go back one character if we exited replace/insert state */
+       if (GTK_SOURCE_IS_VIM_INSERT (from) ||
+           GTK_SOURCE_IS_VIM_REPLACE (from))
+       {
+               GtkSourceBuffer *buffer;
+               GtkTextIter insert;
+
+               buffer = gtk_source_vim_state_get_buffer (state, &insert, NULL);
+
+               if (!gtk_text_iter_starts_line (&insert) &&
+                   gtk_text_iter_backward_char (&insert))
+               {
+                       gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
+                                                     &insert, &insert);
+               }
+       }
+
 }
 
 static void
diff --git a/gtksourceview/vim/gtk-source-vim-replace.c b/gtksourceview/vim/gtk-source-vim-replace.c
new file mode 100644
index 00000000..d0046d42
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-replace.c
@@ -0,0 +1,68 @@
+/*
+ * This file is part of GtkSourceView
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "config.h"
+
+#include "gtk-source-vim-replace.h"
+
+struct _GtkSourceVimReplace
+{
+       GtkSourceVimState parent_instance;
+};
+
+G_DEFINE_TYPE (GtkSourceVimReplace, gtk_source_vim_replace, GTK_SOURCE_TYPE_VIM_STATE)
+
+GtkSourceVimState *
+gtk_source_vim_replace_new (void)
+{
+       return g_object_new (GTK_SOURCE_TYPE_VIM_REPLACE, NULL);
+}
+
+static gboolean
+gtk_source_vim_replace_handle_keypress (GtkSourceVimState *state,
+                                        guint              keyval,
+                                        guint              keycode,
+                                        GdkModifierType    mods,
+                                        const char        *string)
+{
+       g_assert (GTK_SOURCE_IS_VIM_REPLACE (state));
+
+       if (gtk_source_vim_state_is_escape (keyval, mods))
+       {
+               gtk_source_vim_state_pop (state);
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
+static void
+gtk_source_vim_replace_class_init (GtkSourceVimReplaceClass *klass)
+{
+       GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
+
+       state_class->handle_keypress = gtk_source_vim_replace_handle_keypress;
+}
+
+static void
+gtk_source_vim_replace_init (GtkSourceVimReplace *self)
+{
+}
diff --git a/gtksourceview/vim/gtk-source-vim-replace.h b/gtksourceview/vim/gtk-source-vim-replace.h
index 35282995..8fc7dd8c 100644
--- a/gtksourceview/vim/gtk-source-vim-replace.h
+++ b/gtksourceview/vim/gtk-source-vim-replace.h
@@ -29,6 +29,6 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtkSourceVimReplace, gtk_source_vim_replace, GTK_SOURCE, VIM_REPLACE, 
GtkSourceVimState)
 
-GtkSourceVimReplace *gtk_source_vim_replace_new (void);
+GtkSourceVimState *gtk_source_vim_replace_new (void);
 
 G_END_DECLS
diff --git a/gtksourceview/vim/meson.build b/gtksourceview/vim/meson.build
index 9a132b33..50c9fe26 100644
--- a/gtksourceview/vim/meson.build
+++ b/gtksourceview/vim/meson.build
@@ -3,5 +3,6 @@ vim_sources = files([
   'gtk-source-vim-command-bar.c',
   'gtk-source-vim-normal.c',
   'gtk-source-vim-insert.c',
+  'gtk-source-vim-replace.c',
   'gtk-source-vim-state.c',
 ])


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