[gtksourceview/wip/chergert/vim: 96/363] start on delete state




commit 7cd8d4a864b911b429c4fc1038f15d2686d46e49
Author: Christian Hergert <chergert redhat com>
Date:   Wed Oct 27 15:30:28 2021 -0700

    start on delete state

 gtksourceview/vim/gtk-source-vim-delete.c | 193 ++++++++++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-delete.h |  35 ++++++
 gtksourceview/vim/meson.build             |   1 +
 3 files changed, 229 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-delete.c b/gtksourceview/vim/gtk-source-vim-delete.c
new file mode 100644
index 00000000..482c557a
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-delete.c
@@ -0,0 +1,193 @@
+/*
+ * 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 <glib/gi18n.h>
+
+#include "gtksourceindenter.h"
+#include "gtksourceview.h"
+
+#include "gtk-source-vim-delete.h"
+
+struct _GtkSourceVimDelete
+{
+       GtkSourceVimState   parent_instance;
+       GtkSourceVimMotion *motion;
+};
+
+G_DEFINE_TYPE (GtkSourceVimDelete, gtk_source_vim_delete, GTK_SOURCE_TYPE_VIM_STATE)
+
+enum {
+       PROP_0,
+       PROP_MOTION,
+       N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+
+GtkSourceVimState *
+gtk_source_vim_delete_new (GtkSourceVimMotion *motion)
+{
+       g_return_val_if_fail (!motion || GTK_SOURCE_IS_VIM_MOTION (motion), NULL);
+
+       return g_object_new (GTK_SOURCE_TYPE_VIM_DELETE,
+                            "motion", motion,
+                            NULL);
+}
+
+static void
+gtk_source_vim_delete_set_motion (GtkSourceVimDelete *self,
+                                 GtkSourceVimMotion *motion)
+{
+       g_assert (GTK_SOURCE_IS_VIM_DELETE (self));
+       g_assert (!motion || GTK_SOURCE_IS_VIM_MOTION (motion));
+
+       if (g_set_object (&self->motion, motion))
+       {
+               if (motion != NULL)
+               {
+                       gtk_source_vim_motion_set_apply_on_leave (motion, FALSE);
+                       g_object_set (motion, "parent", self, NULL);
+               }
+       }
+}
+
+static void
+gtk_source_vim_delete_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+       GtkSourceVimDelete *self = GTK_SOURCE_VIM_DELETE (object);
+
+       switch (prop_id)
+       {
+               case PROP_MOTION:
+                       g_value_set_object (value, self->motion);
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       }
+}
+
+static void
+gtk_source_vim_delete_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+       GtkSourceVimDelete *self = GTK_SOURCE_VIM_DELETE (object);
+
+       switch (prop_id)
+       {
+               case PROP_MOTION:
+                       gtk_source_vim_delete_set_motion (self, g_value_get_object (value));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       }
+}
+
+static void
+gtk_source_vim_delete_resume (GtkSourceVimState *state,
+                              GtkSourceVimState *from)
+{
+       GtkSourceVimDelete *self = (GtkSourceVimDelete *)state;
+
+       g_assert (GTK_SOURCE_IS_VIM_DELETE (self));
+       g_assert (GTK_SOURCE_IS_VIM_STATE (from));
+
+       if (GTK_SOURCE_IS_VIM_MOTION (from))
+       {
+               gtk_source_vim_delete_set_motion (self, GTK_SOURCE_VIM_MOTION (from));
+               gtk_source_vim_state_pop (GTK_SOURCE_VIM_STATE (self));
+       }
+}
+
+static void
+gtk_source_vim_delete_leave (GtkSourceVimState *state)
+{
+       GtkSourceVimDelete *self = (GtkSourceVimDelete *)state;
+       GtkSourceBuffer *buffer;
+       GtkTextIter insert, selection;
+
+       g_assert (GTK_SOURCE_IS_VIM_DELETE (self));
+
+       if (self->motion != NULL)
+       {
+               buffer = gtk_source_vim_state_get_buffer (state, &insert, &selection);
+               gtk_source_vim_motion_apply (self->motion, &insert, TRUE);
+               gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &insert, &selection);
+       }
+}
+
+static void
+gtk_source_vim_delete_enter (GtkSourceVimState *state)
+{
+       GtkSourceVimDelete *self = (GtkSourceVimDelete *)state;
+
+       g_assert (GTK_SOURCE_IS_VIM_DELETE (self));
+
+       if (self->motion == NULL)
+               gtk_source_vim_state_push (state, gtk_source_vim_motion_new ());
+}
+
+static void
+gtk_source_vim_delete_dispose (GObject *object)
+{
+       GtkSourceVimDelete *self = (GtkSourceVimDelete *)object;
+
+       g_clear_object (&self->motion);
+
+       G_OBJECT_CLASS (gtk_source_vim_delete_parent_class)->dispose (object);
+}
+
+static void
+gtk_source_vim_delete_class_init (GtkSourceVimDeleteClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
+
+       object_class->dispose = gtk_source_vim_delete_dispose;
+       object_class->get_property = gtk_source_vim_delete_get_property;
+       object_class->set_property = gtk_source_vim_delete_set_property;
+
+       state_class->resume = gtk_source_vim_delete_resume;
+       state_class->enter = gtk_source_vim_delete_enter;
+       state_class->leave = gtk_source_vim_delete_leave;
+
+       properties [PROP_MOTION] =
+               g_param_spec_object ("motion",
+                                    "Motion",
+                                    "The motion to apply for delete",
+                                    GTK_SOURCE_TYPE_VIM_MOTION,
+                                    (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+gtk_source_vim_delete_init (GtkSourceVimDelete *self)
+{
+}
diff --git a/gtksourceview/vim/gtk-source-vim-delete.h b/gtksourceview/vim/gtk-source-vim-delete.h
new file mode 100644
index 00000000..613905ec
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-delete.h
@@ -0,0 +1,35 @@
+/*
+ * 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-motion.h"
+#include "gtk-source-vim-state.h"
+
+G_BEGIN_DECLS
+
+#define GTK_SOURCE_TYPE_VIM_DELETE (gtk_source_vim_delete_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimDelete, gtk_source_vim_delete, GTK_SOURCE, VIM_DELETE, GtkSourceVimState)
+
+GtkSourceVimState *gtk_source_vim_delete_new (GtkSourceVimMotion *motion);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/meson.build b/gtksourceview/vim/meson.build
index 48486881..a9985840 100644
--- a/gtksourceview/vim/meson.build
+++ b/gtksourceview/vim/meson.build
@@ -1,6 +1,7 @@
 vim_sources = files([
   'gtk-source-vim.c',
   'gtk-source-vim-command-bar.c',
+  'gtk-source-vim-delete.c',
   'gtk-source-vim-motion.c',
   'gtk-source-vim-normal.c',
   'gtk-source-vim-insert.c',


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