[gtksourceview/wip/chergert/vim] more ideas for layered states



commit f929556f099833718f6423da790ca6e5529edc34
Author: Christian Hergert <chergert redhat com>
Date:   Thu Oct 21 12:46:08 2021 -0700

    more ideas for layered states

 gtksourceview/vim/gtk-source-vim-insert.h  |  34 ++++++
 gtksourceview/vim/gtk-source-vim-normal.c  | 186 +++++++++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-normal.h  |  34 ++++++
 gtksourceview/vim/gtk-source-vim-replace.h |  34 ++++++
 gtksourceview/vim/gtk-source-vim-state.h   |  68 +++++++++++
 gtksourceview/vim/gtk-source-vim-visual.h  |  42 +++++++
 gtksourceview/vim/gtk-source-vim.h         |  40 +++++++
 7 files changed, 438 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-insert.h b/gtksourceview/vim/gtk-source-vim-insert.h
new file mode 100644
index 00000000..6049bea0
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-insert.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 "gtksourcevimstate.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SOURCE_VIM_INSERT (gtk_source_vim_insert_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimInsert, gtk_source_vim_insert, GTK, SOURCE_VIM_INSERT, GObject)
+
+GtkSourceVimInsert *gtk_source_vim_insert_new (GtkSourceVimState *parent);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
new file mode 100644
index 00000000..b2e495aa
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -0,0 +1,186 @@
+/*
+ * 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 "gtksourcevimnormal.h"
+
+typedef enum
+{
+       PARSE_STATE_NUMBER  = 1 << 0,
+       PARSE_STATE_MOTION  = 1 << 1,
+       PARSE_STATE_OPER    = 1 << 2,
+       PARSE_STATE_INITIAL = (PARSE_STATE_NUMBER | PARSE_STATE_MOTION | PARSE_STATE_OPER),
+} ParseState;
+
+typedef enum
+{
+       OPER_0,
+       OPER_INSERT,
+       OPER_DELETE,
+       OPER_REPLACE,
+} Oper;
+
+struct _GtkSourceVimNormal
+{
+       GtkSourceVimState parent_instance;
+
+       /* What state of parsing input are we */
+       ParseState state;
+
+       int repeat;
+       Oper op;
+
+       struct {
+               char kind[8];
+               int number;
+       } motion;
+};
+
+G_DEFINE_TYPE (GtkSourceVimNormal, gtk_source_vim_normal, GTK_SOURCE_TYPE_VIM_STATE)
+
+static inline gboolean
+is_escape (guint           keyval,
+          GdkModifierType mods)
+{
+       return keyval == GDK_KEY_Escape ||
+               (keyval == GDK_KEY_bracketleft && (state & GDK_CONTROL_MASK) != 0);
+}
+
+static void
+gtk_source_vim_normal_reset (GtkSourceVimNormal *self)
+{
+       g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
+
+       self->state = PARSE_STATE_INITIAL;
+       self->repeat = 0;
+}
+
+static gboolean
+gtk_source_vim_normal_handle_keypress (GtkSourceVimState *state,
+                                       guint              keyval,
+                                       guint              keycode,
+                                       GdkModifierType    mods,
+                                       const char        *string)
+{
+       GtkSourceVimNormal *self = (GtkSourceVimNormal *)state;
+
+       g_assert (GTK_SOURCE_IS_VIM_STATE (self));
+
+       if (is_escape (keyval, mods))
+       {
+               gtk_source_vim_normal_reset (self);
+               return TRUE;
+       }
+
+       if (keyval >= GDK_KEY_0 && keyval <= GDK_KEY_9)
+       {
+       }
+
+       if ((self->state & PARSE_STATE_NUMBER) != 0)
+       {
+               int n;
+
+               switch (keyval) {
+               case GDK_KEY_0:
+                       /* cannot lead with 0 */
+                       n = self->number == 0 ? -1 : 0;
+                       break;
+               case GDK_KEY_1: n = 1; break;
+               case GDK_KEY_2: n = 2; break;
+               case GDK_KEY_3: n = 3; break;
+               case GDK_KEY_4: n = 4; break;
+               case GDK_KEY_5: n = 5; break;
+               case GDK_KEY_6: n = 6; break;
+               case GDK_KEY_7: n = 7; break;
+               case GDK_KEY_8: n = 8; break;
+               case GDK_KEY_9: n = 9; break;
+               default: n = -1; break;
+               }
+
+               if (n >= 0)
+               {
+                       self->number = self->number * 10 + n;
+                       return TRUE;
+               }
+       }
+
+       /* 3cip and 3c2ip result in 3cip */
+
+       switch (keyval) {
+       case GDK_KEY_0:
+       case GDK_KEY_1:
+       case GDK_KEY_2:
+       case GDK_KEY_3:
+       case GDK_KEY_4;
+       case GDK_KEY_5:
+       case GDK_KEY_6:
+       case GDK_KEY_7:
+       case GDK_KEY_8:
+       case GDK_KEY_9:
+               break;
+       }
+
+       return FALSE;
+}
+
+static void
+gtk_source_vim_normal_restore (GtkSourceVimState *state,
+                               GtkSourceVimState *from)
+{
+       g_assert (GTK_SOURCE_IS_VIM_STATE (state));
+       g_assert (GTK_SOURCE_IS_VIM_STATE (from));
+
+       gtk_source_vim_normal_reset (GTK_SOURCE_VIM_NORMAL (state));
+}
+
+static void
+gtk_source_vim_normal_finalize (GObject *object)
+{
+       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);
+
+       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)
+{
+}
+
+GtkSourceVimNormal *
+gtk_source_vim_normal_new (GtkSourceVimState *parent)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_VIM_STATE (parent), NULL);
+
+       return g_object_new (GTK_SOURCE_TYPE_VIM_STATE,
+                            "parent", parent,
+                            NULL);
+}
diff --git a/gtksourceview/vim/gtk-source-vim-normal.h b/gtksourceview/vim/gtk-source-vim-normal.h
new file mode 100644
index 00000000..6e3ab0bb
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-normal.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 "gtksourcevimstate.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SOURCE_VIM_NORMAL (gtk_source_vim_normal_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimNormal, gtk_source_vim_normal, GTK, SOURCE_VIM_NORMAL, GtkSourceVimState)
+
+GtkSourceVimNormal *gtk_source_vim_normal_new (GtkSourceVimState *parent);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-replace.h b/gtksourceview/vim/gtk-source-vim-replace.h
new file mode 100644
index 00000000..798f85f3
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-replace.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 "gtksourcevimstate.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SOURCE_VIM_REPLACE (gtk_source_vim_replace_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimReplace, gtk_source_vim_replace, GTK, SOURCE_VIM_REPLACE, 
GtkSourceVimState)
+
+GtkSourceVimReplace *gtk_source_vim_replace_new (GtkSourceVimState *parent);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-state.h b/gtksourceview/vim/gtk-source-vim-state.h
new file mode 100644
index 00000000..f65fa364
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-state.h
@@ -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
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "gtksourcetypes.h"
+#include "gtksourcetypes-private.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SOURCE_VIM_STATE (gtk_source_vim_state_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (GtkSourceVimState, gtk_source_vim_state, GTK, SOURCE_VIM_STATE, GObject)
+
+struct _GtkSourceVimStateClass
+{
+       GObjectClass parent_class;
+
+       void     (*enter)           (GtkSourceVimState *state);
+       void     (*suspend)         (GtkSourceVimState *state,
+                                    GtkSourceVimState *to);
+       void     (*restore)         (GtkSourceVimState *state,
+                                    GtkSourceVimState *from);
+       void     (*leave)           (GtkSourceVimState *state);
+       gboolean (*handle_event)    (GtkSourceVimState *state,
+                                    GdkEvent          *event);
+       gboolean (*handle_keypress) (GtkSourceVimState *state,
+                                    guint              keyval,
+                                    guint              keycode,
+                                    GdkModifierType    mods,
+                                    const char        *string);
+       gboolean (*get_can_repeat)  (GtkSourceVimState *state);
+       void     (*repeat)          (GtkSourceVimState *state,
+                                    int                repeat);
+};
+
+void               gtk_source_vim_state_beep           (GtkSourceVimState *self);
+GtkSourceView     *gtk_source_vim_state_get_view       (GtkSourceVimState *self);
+GtkSourceBuffer   *gtk_source_vim_state_get_buffer     (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_root       (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_parent     (GtkSourceVimState *self);
+gboolean           gtk_source_vim_state_handle_event   (GtkSourceVimState *self,
+                                                        GdkEvent          *event);
+gboolean           gtk_source_vim_state_get_can_repeat (GtkSourceVimState *self,
+void               gtk_source_vim_state_repeat         (GtkSourceVimState *self,
+                                                        int                repeat);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-visual.h b/gtksourceview/vim/gtk-source-vim-visual.h
new file mode 100644
index 00000000..13aa233a
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim-visual.h
@@ -0,0 +1,42 @@
+/*
+ * 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 "gtksourcevimstate.h"
+
+G_BEGIN_DECLS
+
+typedef enum
+{
+       GTK_SOURCE_VIM_VISUAL_CHAR,
+       GTK_SOURCE_VIM_VISUAL_LINE,
+       GTK_SOURCE_VIM_VISUAL_BLOCK,
+} GtkSourceVimVisualMode;
+
+#define GTK_TYPE_SOURCE_VIM_VISUAL (gtk_source_vim_visual_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVimVisual, gtk_source_vim_visual, GTK, SOURCE_VIM_VISUAL, GObject)
+
+GtkSourceVimVisual *gtk_source_vim_visual_new (GtkSourceVimState      *parent,
+                                               GtkSourceVimVisualMode  mode);
+
+G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim.h b/gtksourceview/vim/gtk-source-vim.h
new file mode 100644
index 00000000..fac91c9b
--- /dev/null
+++ b/gtksourceview/vim/gtk-source-vim.h
@@ -0,0 +1,40 @@
+/*
+ * 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 "gtksourcevim.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SOURCE_VIM (gtk_source_vim_get_type())
+
+G_DECLARE_FINAL_TYPE (GtkSourceVim, gtk_source_vim, GTK, SOURCE_VIM, GtkSourceVimState)
+
+GtkSourceVim *gtk_source_vim_new             (GtkSourceView  *view);
+const char   *gtk_source_vim_get_command     (GtkSourceVim   *self);
+const char   *gtk_source_vim_get_command_bar (GtkSourceVim   *self);
+void          gtk_source_vim_emit_split      (GtkSourceVim   *self,
+                                              GtkOrientation  orientation,
+                                              gboolean        new_document,
+                                              gboolean        focus_split);
+
+G_END_DECLS


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