[gtksourceview/wip/chergert/vim: 144/363] add registers helper




commit 9889a3e4dfc5940d39e022ff5408652182c29c1d
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 29 15:49:24 2021 -0700

    add registers helper

 gtksourceview/vim/gtk-source-vim-registers.c | 107 +++++++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-registers.h |  44 +++++++++++
 gtksourceview/vim/gtk-source-vim-state.c     |  23 ++++++
 gtksourceview/vim/gtk-source-vim-state.h     |   1 +
 gtksourceview/vim/meson.build                |   1 +
 5 files changed, 176 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-registers.c b/gtksourceview/vim/gtk-source-vim-registers.c
new file mode 100644
index 00000000..f901e9e6
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-registers.c
@@ -0,0 +1,107 @@
+/*
+ * 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-registers.h"
+
+struct _GtkSourceVimRegisters
+{
+       GtkSourceVimState parent_instance;
+       GHashTable *values;
+};
+
+G_DEFINE_TYPE (GtkSourceVimRegisters, gtk_source_vim_registers, GTK_SOURCE_TYPE_VIM_STATE)
+
+static void
+gtk_source_vim_registers_finalize (GObject *object)
+{
+       GtkSourceVimRegisters *self = (GtkSourceVimRegisters *)object;
+
+       g_clear_pointer (&self->values, g_hash_table_unref);
+
+       G_OBJECT_CLASS (gtk_source_vim_registers_parent_class)->finalize (object);
+}
+
+static void
+gtk_source_vim_registers_class_init (GtkSourceVimRegistersClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->finalize = gtk_source_vim_registers_finalize;
+}
+
+static void
+gtk_source_vim_registers_init (GtkSourceVimRegisters *self)
+{
+       self->values = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
+}
+
+GtkSourceVimState *
+gtk_source_vim_registers_new (void)
+{
+       return g_object_new (GTK_SOURCE_TYPE_VIM_REGISTERS, NULL);
+}
+
+const char *
+gtk_source_vim_registers_get (GtkSourceVimRegisters *self,
+                              const char            *name)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_VIM_REGISTERS (self), NULL);
+
+       if (name == NULL)
+       {
+               name = "";
+       }
+
+       return g_hash_table_lookup (self->values, name);
+}
+
+void
+gtk_source_vim_registers_set (GtkSourceVimRegisters *self,
+                              const char            *name,
+                              const char            *value)
+{
+       gtk_source_vim_registers_take (self, name, g_strdup (value));
+}
+
+void
+gtk_source_vim_registers_take (GtkSourceVimRegisters *self,
+                               const char            *name,
+                               char                  *value)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_REGISTERS (self));
+
+       if (name == NULL)
+               name = "";
+
+       if (value == NULL)
+               g_hash_table_remove (self->values, name);
+       else
+               g_hash_table_insert (self->values, (char *)g_intern_string (name), value);
+}
+
+void
+gtk_source_vim_registers_clear (GtkSourceVimRegisters *self,
+                                const char            *name)
+{
+       gtk_source_vim_registers_set (self, name, NULL);
+}
diff --git a/gtksourceview/vim/gtk-source-vim-registers.h b/gtksourceview/vim/gtk-source-vim-registers.h
new file mode 100644
index 00000000..736ff9cc
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-registers.h
@@ -0,0 +1,44 @@
+/*
+ * 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_REGISTERS (gtk_source_vim_registers_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimRegisters, gtk_source_vim_registers, GTK_SOURCE, VIM_REGISTERS, 
GtkSourceVimState)
+
+GtkSourceVimState *gtk_source_vim_registers_new   (void);
+const char        *gtk_source_vim_registers_get   (GtkSourceVimRegisters *self,
+                                                   const char            *name);
+void               gtk_source_vim_registers_set   (GtkSourceVimRegisters *self,
+                                                   const char            *name,
+                                                   const char            *string);
+void               gtk_source_vim_registers_take  (GtkSourceVimRegisters *self,
+                                                   const char            *name,
+                                                   char                  *string);
+void               gtk_source_vim_registers_clear (GtkSourceVimRegisters *self,
+                                                   const char            *name);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-state.c b/gtksourceview/vim/gtk-source-vim-state.c
index 4819444a..0c395468 100644
--- a/gtksourceview/vim/gtk-source-vim-state.c
+++ b/gtksourceview/vim/gtk-source-vim-state.c
@@ -25,10 +25,12 @@
 #include "gtksourceutils-private.h"
 #include "gtksourceview.h"
 
+#include "gtk-source-vim-registers.h"
 #include "gtk-source-vim-state.h"
 
 typedef struct
 {
+       GtkSourceVimState *registers;
        GtkSourceVimState *parent;
        GtkSourceVimState *child;
        GtkSourceView     *view;
@@ -162,6 +164,7 @@ gtk_source_vim_state_dispose (GObject *object)
 
        g_clear_weak_pointer (&priv->view);
        g_clear_object (&priv->parent);
+       g_clear_object (&priv->registers);
 
        G_OBJECT_CLASS (gtk_source_vim_state_parent_class)->dispose (object);
 }
@@ -849,3 +852,23 @@ gtk_source_vim_state_set_can_repeat (GtkSourceVimState *self,
 
        priv->can_repeat = !!can_repeat;
 }
+
+GtkSourceVimState *
+gtk_source_vim_state_get_registers (GtkSourceVimState *self)
+{
+       GtkSourceVimStatePrivate *priv;
+       GtkSourceVimState *root;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_VIM_STATE (self), NULL);
+
+       root = gtk_source_vim_state_get_root (self);
+       priv = gtk_source_vim_state_get_instance_private (root);
+
+       if (priv->registers == NULL)
+       {
+               priv->registers = gtk_source_vim_registers_new ();
+               gtk_source_vim_state_set_parent (priv->registers, GTK_SOURCE_VIM_STATE (root));
+       }
+
+       return priv->registers;
+}
diff --git a/gtksourceview/vim/gtk-source-vim-state.h b/gtksourceview/vim/gtk-source-vim-state.h
index 2e0aa12b..e2de544c 100644
--- a/gtksourceview/vim/gtk-source-vim-state.h
+++ b/gtksourceview/vim/gtk-source-vim-state.h
@@ -72,6 +72,7 @@ GtkSourceBuffer   *gtk_source_vim_state_get_buffer            (GtkSourceVimState
                                                                GtkTextIter       *selection_bound);
 GtkSourceVimState *gtk_source_vim_state_get_root              (GtkSourceVimState *self);
 GtkSourceVimState *gtk_source_vim_state_get_parent            (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_registers         (GtkSourceVimState *self);
 int                gtk_source_vim_state_get_count             (GtkSourceVimState *self);
 gboolean           gtk_source_vim_state_get_count_set         (GtkSourceVimState *self);
 void               gtk_source_vim_state_set_count             (GtkSourceVimState *self,
diff --git a/gtksourceview/vim/meson.build b/gtksourceview/vim/meson.build
index 0c2e3118..55f9be5f 100644
--- a/gtksourceview/vim/meson.build
+++ b/gtksourceview/vim/meson.build
@@ -7,6 +7,7 @@ vim_sources = files([
   'gtk-source-vim-normal.c',
   'gtk-source-vim-insert.c',
   'gtk-source-vim-insert-literal.c',
+  'gtk-source-vim-registers.c',
   'gtk-source-vim-replace.c',
   'gtk-source-vim-state.c',
   'gtk-source-vim-text-history.c',


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