[gtksourceview/wip/chergert/vim: 7/363] some more ideas for normal handling




commit 12b26cacdc87e79d3dc2308902cfd42a044a4a00
Author: Christian Hergert <chergert redhat com>
Date:   Tue Oct 19 17:46:34 2021 -0700

    some more ideas for normal handling

 gtksourceview/gtksourcevimstate.c | 101 ++++++++++++++++++++++++++++++++++++--
 gtksourceview/gtksourcevimstate.h |  21 +++++---
 2 files changed, 110 insertions(+), 12 deletions(-)
---
diff --git a/gtksourceview/gtksourcevimstate.c b/gtksourceview/gtksourcevimstate.c
index ab6218b2..cb7a0f70 100644
--- a/gtksourceview/gtksourcevimstate.c
+++ b/gtksourceview/gtksourcevimstate.c
@@ -33,6 +33,7 @@ typedef struct
 struct _GtkSourceVimNormal
 {
   GtkSourceVimState parent_instance;
+  GString *command;
 };
 
 struct _GtkSourceVimInsert
@@ -53,6 +54,55 @@ enum {
 
 static GParamSpec *state_properties [STATE_N_PROPS];
 
+static inline char *
+get_key_string (guint keyval,
+                char  str[8])
+{
+  if (keyval == GDK_KEY_Escape)
+    {
+      str[0] = '^';
+      str[1] = '[';
+      str[2] = 0;
+    }
+  else if (keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter)
+    {
+      str[0] = '^';
+      str[1] = 'M';
+      str[2] = 0;
+    }
+  else
+    {
+      gunichar ch = gdk_keyval_to_unicode (keyval);
+      int len = g_unichar_to_utf8 (ch, str);
+      str[len] = 0;
+    }
+
+  return str;
+}
+
+static gboolean
+gtk_source_vim_state_real_handle (GtkSourceVimState *self,
+                                  GdkEvent          *event)
+{
+  char string[8];
+  GdkModifierType mods;
+  guint keyval;
+  guint keycode;
+
+  if (gdk_event_get_event_type (event) != GDK_KEY_PRESS)
+    return FALSE;
+
+  if (GTK_SOURCE_VIM_STATE_GET_CLASS (self)->handle_keypress == NULL)
+    return FALSE;
+
+  keyval = gdk_key_event_get_keyval (event);
+  keycode = gdk_key_event_get_keycode (event);
+  mods = gdk_key_event_get_consumed_modifiers (event);
+  get_key_string (keyval, string);
+
+  return GTK_SOURCE_VIM_STATE_GET_CLASS (self)->handle_keypress (self, keyval, keycode, mods, string);
+}
+
 static void
 gtk_source_vim_state_dispose (GObject *object)
 {
@@ -120,6 +170,8 @@ gtk_source_vim_state_class_init (GtkSourceVimStateClass *klass)
   object_class->get_property = gtk_source_vim_state_get_property;
   object_class->set_property = gtk_source_vim_state_set_property;
 
+  klass->handle = gtk_source_vim_state_real_handle;
+
   state_properties [STATE_PROP_PARENT] =
     g_param_spec_object ("parent",
                          "Parent",
@@ -282,13 +334,39 @@ gtk_source_vim_state_handle (GtkSourceVimState *self,
 }
 
 static gboolean
-gtk_source_vim_normal_handle (GtkSourceVimState *state,
-                              GdkEvent          *event)
+gtk_source_vim_normal_is_command (GtkSourceVimNormal *self,
+                                  const char         *command)
+{
+  return FALSE;
+}
+
+static gboolean
+gtk_source_vim_normal_handle_keypress (GtkSourceVimState *state,
+                                       guint              keyval,
+                                       guint              keycode,
+                                       GdkModifierType    mods,
+                                       const char        *str)
 {
   GtkSourceVimNormal *self = (GtkSourceVimNormal *)state;
 
   g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
-  g_assert (event != NULL);
+
+  /* Fail, bail, etc? */
+  if (str == NULL)
+    return FALSE;
+
+  /* TODO:
+   *
+   * if string is a number, then we can only have trailing
+   * numbers in the command. we also need to check if the str
+   * will terminate the command (cCaAiI etc).
+   */
+
+  g_string_append (self->command, str);
+
+  if (gtk_source_vim_normal_is_command (self, self->command->str))
+    {
+    }
 
   return FALSE;
 }
@@ -321,18 +399,33 @@ gtk_source_vim_normal_restore (GtkSourceVimState *state,
     }
 }
 
+static void
+gtk_source_vim_normal_finalize (GObject *object)
+{
+  GtkSourceVimNormal *self = (GtkSourceVimNormal *)object;
+
+  g_string_free (self->command, TRUE);
+  self->command = NULL;
+
+  G_OBJECT_CLASS (gtk_source_vim_normal_parent_class)->finalize (object);
+}
+
 static void
 gtk_source_vim_normal_class_init (GtkSourceVimNormalClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
 
-  state_class->handle = gtk_source_vim_normal_handle;
+  object_class->finalize = gtk_source_vim_normal_finalize;
+
+  state_class->handle_keypress = gtk_source_vim_normal_handle_keypress;
   state_class->restore = gtk_source_vim_normal_restore;
 }
 
 static void
 gtk_source_vim_normal_init (GtkSourceVimNormal *self)
 {
+  self->command = g_string_new (NULL);
 }
 
 static void
diff --git a/gtksourceview/gtksourcevimstate.h b/gtksourceview/gtksourcevimstate.h
index c00a5553..abcd6917 100644
--- a/gtksourceview/gtksourcevimstate.h
+++ b/gtksourceview/gtksourcevimstate.h
@@ -36,14 +36,19 @@ struct _GtkSourceVimStateClass
 {
   GObject parent_class;
 
-  void     (*enter)   (GtkSourceVimState *state);
-  void     (*leave)   (GtkSourceVimState *state);
-  void     (*suspend) (GtkSourceVimState *state,
-                       GtkSourceVimState *to);
-  void     (*restore) (GtkSourceVimState *state,
-                       GtkSourceVimState *from);
-  gboolean (*handle)  (GtkSourceVimState *state,
-                       GdkEvent          *event);
+  void     (*enter)           (GtkSourceVimState *state);
+  void     (*leave)           (GtkSourceVimState *state);
+  void     (*suspend)         (GtkSourceVimState *state,
+                               GtkSourceVimState *to);
+  void     (*restore)         (GtkSourceVimState *state,
+                               GtkSourceVimState *from);
+  gboolean (*handle)          (GtkSourceVimState *state,
+                               GdkEvent          *event);
+  gboolean (*handle_keypress) (GtkSourceVimState *state,
+                               guint              keyval,
+                               guint              keycode,
+                               GdkModifierType    mods,
+                               const char        *string);
 };
 
 GtkSourceVimState *gtk_source_vim_state_get_parent (GtkSourceVimState *self);


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