[gtksourceview/wip/chergert/vim: 29/73] add insert literal state




commit 1259bd7ef11300a13b50453dea66e0944d3036b4
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 22 10:49:57 2021 -0700

    add insert literal state
    
    this will make tracking things easier and can share with replace mode

 gtksourceview/vim/gtk-source-vim-insert-literal.c | 117 ++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-insert-literal.h |  34 +++++++
 gtksourceview/vim/gtk-source-vim-insert.c         |  70 ++-----------
 gtksourceview/vim/meson.build                     |   1 +
 4 files changed, 158 insertions(+), 64 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-insert-literal.c 
b/gtksourceview/vim/gtk-source-vim-insert-literal.c
new file mode 100644
index 00000000..893c80eb
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-insert-literal.c
@@ -0,0 +1,117 @@
+/*
+ * 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-insert-literal.h"
+
+struct _GtkSourceVimInsertLiteral
+{
+       GtkSourceVimState parent_instance;
+};
+
+G_DEFINE_TYPE (GtkSourceVimInsertLiteral, gtk_source_vim_insert_literal, GTK_SOURCE_TYPE_VIM_STATE)
+
+GtkSourceVimState *
+gtk_source_vim_insert_literal_new (void)
+{
+       return g_object_new (GTK_SOURCE_TYPE_VIM_INSERT_LITERAL, NULL);
+}
+
+static gboolean
+do_literal (GtkSourceVimInsertLiteral *self,
+            const char                *string)
+{
+       g_assert (GTK_SOURCE_IS_VIM_INSERT_LITERAL (self));
+       g_assert (string != NULL);
+
+       if (string[0] != 0)
+       {
+               GtkSourceBuffer *buffer;
+               GtkTextIter insert;
+
+               buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &insert, NULL);
+               gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &insert, string, -1);
+       }
+
+       gtk_source_vim_state_pop (GTK_SOURCE_VIM_STATE (self));
+
+       return TRUE;
+}
+
+static gboolean
+gtk_source_vim_insert_literal_handle_keypress (GtkSourceVimState *state,
+                                               guint              keyval,
+                                               guint              keycode,
+                                               GdkModifierType    mods,
+                                               const char        *string)
+{
+       GtkSourceVimInsertLiteral *self = (GtkSourceVimInsertLiteral *)state;
+       char outbuf[8] = {0};
+       gunichar ch;
+
+       g_assert (GTK_SOURCE_IS_VIM_INSERT_LITERAL (self));
+
+       switch (keyval)
+       {
+               case GDK_KEY_Return:
+               case GDK_KEY_KP_Enter:
+               case GDK_KEY_ISO_Enter:
+                       return do_literal (self, "\n");
+
+               case GDK_KEY_BackSpace:
+                       return do_literal (self, "\b");
+
+               case GDK_KEY_Tab:
+                       return do_literal (self, "\t");
+
+               case GDK_KEY_Escape:
+                       return do_literal (self, "\e");
+
+               case GDK_KEY_l:
+                       if ((mods & GDK_CONTROL_MASK) != 0)
+                               return do_literal (self, "\f");
+                       break;
+
+               default:
+                       break;
+       }
+
+       ch = gdk_keyval_to_unicode (keyval);
+
+       if (g_unichar_to_utf8 (ch, outbuf) <= 0)
+               outbuf[0] = 0;
+
+       return do_literal (self, outbuf);
+}
+
+static void
+gtk_source_vim_insert_literal_class_init (GtkSourceVimInsertLiteralClass *klass)
+{
+       GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
+
+       state_class->handle_keypress = gtk_source_vim_insert_literal_handle_keypress;
+}
+
+static void
+gtk_source_vim_insert_literal_init (GtkSourceVimInsertLiteral *self)
+{
+}
diff --git a/gtksourceview/vim/gtk-source-vim-insert-literal.h 
b/gtksourceview/vim/gtk-source-vim-insert-literal.h
new file mode 100644
index 00000000..b99c9147
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-insert-literal.h
@@ -0,0 +1,34 @@
+/*
+ * 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
+ */
+
+#pragma once
+
+#include "gtk-source-vim-state.h"
+
+G_BEGIN_DECLS
+
+#define GTK_SOURCE_TYPE_VIM_INSERT_LITERAL (gtk_source_vim_insert_literal_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimInsertLiteral, gtk_source_vim_insert_literal, GTK_SOURCE, 
VIM_INSERT_LITERAL, GtkSourceVimState)
+
+GtkSourceVimState *gtk_source_vim_insert_literal_new (void);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-insert.c b/gtksourceview/vim/gtk-source-vim-insert.c
index f10cfdf2..88bda230 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.c
+++ b/gtksourceview/vim/gtk-source-vim-insert.c
@@ -22,12 +22,12 @@
 #include "config.h"
 
 #include "gtk-source-vim-insert.h"
+#include "gtk-source-vim-insert-literal.h"
 #include "gtk-source-vim-replace.h"
 
 struct _GtkSourceVimInsert
 {
        GtkSourceVimState parent_instance;
-       guint insert_literal : 1;
 };
 
 G_DEFINE_TYPE (GtkSourceVimInsert, gtk_source_vim_insert, GTK_SOURCE_TYPE_VIM_STATE)
@@ -62,22 +62,6 @@ clear_to_first_char (GtkSourceVimInsert *self)
        return TRUE;
 }
 
-static gboolean
-gtk_source_vim_insert_literal (GtkSourceVimInsert *self,
-                               const char         *string)
-{
-       GtkSourceBuffer *buffer;
-       GtkTextIter insert;
-
-       g_assert (GTK_SOURCE_IS_VIM_INSERT (self));
-       g_assert (string != NULL);
-
-       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &insert, NULL);
-       gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &insert, string, -1);
-
-       return TRUE;
-}
-
 static gboolean
 gtk_source_vim_insert_handle_event (GtkSourceVimState *state,
                                     GdkEvent          *event)
@@ -96,46 +80,8 @@ gtk_source_vim_insert_handle_event (GtkSourceVimState *state,
                return FALSE;
 
        keyval = gdk_key_event_get_keyval (event);
-       mods = gdk_key_event_get_consumed_modifiers (event);
-
-       if (self->insert_literal)
-       {
-               self->insert_literal = FALSE;
-
-               switch (keyval)
-               {
-                       case GDK_KEY_Return:
-                       case GDK_KEY_KP_Enter:
-                       case GDK_KEY_ISO_Enter:
-                               return gtk_source_vim_insert_literal (self, "\n");
-
-                       case GDK_KEY_BackSpace:
-                               return gtk_source_vim_insert_literal (self, "\b");
-
-                       case GDK_KEY_Tab:
-                               return gtk_source_vim_insert_literal (self, "\t");
-
-                       case GDK_KEY_Escape:
-                               return gtk_source_vim_insert_literal (self, "\e");
-
-                       case GDK_KEY_l:
-                               if ((mods & GDK_CONTROL_MASK) != 0)
-                                       return gtk_source_vim_insert_literal (self, "\f");
-                               G_GNUC_FALLTHROUGH;
-
-                       default:
-                       {
-                               char outbuf[8] = {0};
-                               gunichar ch = gdk_keyval_to_unicode (keyval);
-                               int len = g_unichar_to_utf8 (ch, outbuf);
-
-                               if (len > 0)
-                                       gtk_source_vim_insert_literal (self, outbuf);
-
-                               return TRUE;
-                       }
-               }
-       }
+       mods = gdk_event_get_modifier_state (event)
+            & gtk_accelerator_get_default_mod_mask ();
 
        /* Allow input methods to complete */
        if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
@@ -148,14 +94,10 @@ gtk_source_vim_insert_handle_event (GtkSourceVimState *state,
                return TRUE;
        }
 
-       /* ctrl+v to begin insert literal */
-       if (self->insert_literal == FALSE)
+       if (keyval == GDK_KEY_v && (mods & GDK_CONTROL_MASK) != 0)
        {
-               if (keyval == GDK_KEY_v && (mods & GDK_CONTROL_MASK) != 0)
-               {
-                       self->insert_literal = TRUE;
-                       return TRUE;
-               }
+               gtk_source_vim_state_push (state, gtk_source_vim_insert_literal_new ());
+               return TRUE;
        }
 
        /* Now handle our commands */
diff --git a/gtksourceview/vim/meson.build b/gtksourceview/vim/meson.build
index 50c9fe26..0ea488d5 100644
--- a/gtksourceview/vim/meson.build
+++ b/gtksourceview/vim/meson.build
@@ -3,6 +3,7 @@ vim_sources = files([
   'gtk-source-vim-command-bar.c',
   'gtk-source-vim-normal.c',
   'gtk-source-vim-insert.c',
+  'gtk-source-vim-insert-literal.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]