[gtksourceview/wip/chergert/vim: 137/293] abstract commands a bit




commit 145bf3c6f7198e36ed0767f177f693f149bd9cb5
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 29 12:11:43 2021 -0700

    abstract commands a bit
    
    and add undo/redo

 gtksourceview/vim/gtk-source-vim-command.c | 68 +++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 10 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-command.c b/gtksourceview/vim/gtk-source-vim-command.c
index c1e7e988..01bfa9e6 100644
--- a/gtksourceview/vim/gtk-source-vim-command.c
+++ b/gtksourceview/vim/gtk-source-vim-command.c
@@ -25,6 +25,8 @@
 
 #include "gtk-source-vim-command.h"
 
+typedef void (*Command) (GtkSourceVimCommand *self);
+
 struct _GtkSourceVimCommand
 {
        GtkSourceVimState parent_instance;
@@ -44,6 +46,7 @@ enum {
 };
 
 static GParamSpec *properties[N_PROPS];
+static GHashTable *commands;
 
 static void
 gtk_source_vim_command_join (GtkSourceVimCommand *self)
@@ -69,6 +72,42 @@ gtk_source_vim_command_join (GtkSourceVimCommand *self)
        gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer), &iter, &iter);
 }
 
+static void
+gtk_source_vim_command_undo (GtkSourceVimCommand *self)
+{
+       GtkSourceBuffer *buffer;
+       int count;
+
+       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), NULL, NULL);
+       count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+
+       for (int i = 0; i < count; i++)
+       {
+               if (gtk_text_buffer_get_can_undo (GTK_TEXT_BUFFER (buffer)))
+               {
+                       gtk_text_buffer_undo (GTK_TEXT_BUFFER (buffer));
+               }
+       }
+}
+
+static void
+gtk_source_vim_command_redo (GtkSourceVimCommand *self)
+{
+       GtkSourceBuffer *buffer;
+       int count;
+
+       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), NULL, NULL);
+       count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+
+       for (int i = 0; i < count; i++)
+       {
+               if (gtk_text_buffer_get_can_redo (GTK_TEXT_BUFFER (buffer)))
+               {
+                       gtk_text_buffer_redo (GTK_TEXT_BUFFER (buffer));
+               }
+       }
+}
+
 static void
 gtk_source_vim_command_append_command (GtkSourceVimState *state,
                                        GString           *string)
@@ -82,11 +121,18 @@ gtk_source_vim_command_repeat (GtkSourceVimState *state)
 {
        GtkSourceVimCommand *self = (GtkSourceVimCommand *)state;
        GtkSourceBuffer *buffer;
+       Command command;
        GtkTextIter iter;
        GtkTextIter selection;
 
        g_assert (GTK_SOURCE_IS_VIM_COMMAND (self));
 
+       if (self->command == NULL)
+               return;
+
+       if (!(command = g_hash_table_lookup (commands, self->command)))
+               return;
+
        buffer = gtk_source_vim_state_get_buffer (state, &iter, &selection);
 
        if (self->motion)
@@ -102,16 +148,7 @@ gtk_source_vim_command_repeat (GtkSourceVimState *state)
        gtk_source_vim_state_select (state, &iter, &selection);
 
        gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
-
-       /* XXX: obviously need to add an abstraction here for
-        * registered commands (and possible options on them).
-        */
-
-       if (g_strcmp0 (self->command, ":join") == 0)
-       {
-               gtk_source_vim_command_join (self);
-       }
-
+       command (self);
        gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
 }
 
@@ -223,6 +260,17 @@ gtk_source_vim_command_class_init (GtkSourceVimCommandClass *klass)
                                     (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
 
        g_object_class_install_properties (object_class, N_PROPS, properties);
+
+       commands = g_hash_table_new (g_str_hash, g_str_equal);
+
+#define ADD_COMMAND(name, func) \
+       g_hash_table_insert(commands, (char*)name, (gpointer)func)
+       ADD_COMMAND (":join", gtk_source_vim_command_join);
+       ADD_COMMAND (":j",    gtk_source_vim_command_join);
+       ADD_COMMAND (":undo", gtk_source_vim_command_undo);
+       ADD_COMMAND (":u",    gtk_source_vim_command_undo);
+       ADD_COMMAND (":redo", gtk_source_vim_command_redo);
+#undef ADD_COMMAND
 }
 
 static void


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