[gtksourceview/wip/chergert/vim: 331/363] test commands via vim object




commit c84a25b1d71f5382a91fbf0c02ef5bdf544ab7c3
Author: Christian Hergert <chergert redhat com>
Date:   Fri Nov 5 17:09:17 2021 -0700

    test commands via vim object

 gtksourceview/vim/gtk-source-vim-command.c | 39 ++++++++++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim.c         | 17 +++++++++++++
 2 files changed, 56 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-command.c b/gtksourceview/vim/gtk-source-vim-command.c
index b9388913..16b530f5 100644
--- a/gtksourceview/vim/gtk-source-vim-command.c
+++ b/gtksourceview/vim/gtk-source-vim-command.c
@@ -24,6 +24,8 @@
 #include <string.h>
 
 #include "gtksourcebuffer.h"
+#include "gtksourcestyleschememanager.h"
+#include "gtksourcestylescheme.h"
 #include "gtksourceview.h"
 
 #include "gtk-source-vim.h"
@@ -41,6 +43,7 @@ struct _GtkSourceVimCommand
        GtkSourceVimMotion     *selection_motion;
        GtkSourceVimTextObject *text_object;
        char                   *command;
+       char                   *options;
        char                    char_pending[16];
 
        guint                   ignore_mark : 1;
@@ -579,6 +582,32 @@ gtk_source_vim_command_redo (GtkSourceVimCommand *self)
        } while (--count > 0);
 }
 
+static void
+gtk_source_vim_command_colorscheme (GtkSourceVimCommand *self)
+{
+       GtkSourceStyleSchemeManager *manager;
+       GtkSourceStyleScheme *scheme;
+       GtkSourceBuffer *buffer;
+       char *stripped;
+
+       g_assert (GTK_SOURCE_IS_VIM_COMMAND (self));
+
+       if (self->options == NULL)
+               return;
+
+       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), NULL, NULL);
+       manager = gtk_source_style_scheme_manager_get_default ();
+       stripped = g_strstrip (g_strdup (self->options));
+       scheme = gtk_source_style_scheme_manager_get_scheme (manager, stripped);
+
+       if (scheme != NULL)
+       {
+               gtk_source_buffer_set_style_scheme (buffer, scheme);
+       }
+
+       g_free (stripped);
+}
+
 static void
 gtk_source_vim_command_append_command (GtkSourceVimState *state,
                                        GString           *string)
@@ -723,6 +752,7 @@ gtk_source_vim_command_dispose (GObject *object)
        gtk_source_vim_state_release (&self->text_object);
 
        g_clear_pointer (&self->command, g_free);
+       g_clear_pointer (&self->options, g_free);
 
        G_OBJECT_CLASS (gtk_source_vim_command_parent_class)->dispose (object);
 }
@@ -828,6 +858,7 @@ gtk_source_vim_command_class_init (GtkSourceVimCommandClass *klass)
                g_hash_table_insert(commands, (char*)name, (gpointer)func); \
                g_ptr_array_add (commands_sorted, (char *)name); \
        } G_STMT_END
+       ADD_COMMAND (":colorscheme",   gtk_source_vim_command_colorscheme);
        ADD_COMMAND (":delete",        gtk_source_vim_command_delete);
        ADD_COMMAND (":join",          gtk_source_vim_command_join);
        ADD_COMMAND (":j",             gtk_source_vim_command_join);
@@ -912,5 +943,13 @@ gtk_source_vim_command_new_parsed (const char *command_line)
 
        g_return_val_if_fail (command_line != NULL, NULL);
 
+       /* TODO: create a real parser for this stuff */
+
+       if (g_str_has_prefix (command_line, ":colorscheme "))
+       {
+               ret = GTK_SOURCE_VIM_COMMAND (gtk_source_vim_command_new (":colorscheme"));
+               ret->options = g_strdup (command_line + strlen (":colorscheme"));
+       }
+
        return GTK_SOURCE_VIM_STATE (ret);
 }
diff --git a/gtksourceview/vim/gtk-source-vim.c b/gtksourceview/vim/gtk-source-vim.c
index c78d2b00..0e794834 100644
--- a/gtksourceview/vim/gtk-source-vim.c
+++ b/gtksourceview/vim/gtk-source-vim.c
@@ -24,6 +24,7 @@
 #include "gtksourceview.h"
 
 #include "gtk-source-vim.h"
+#include "gtk-source-vim-command.h"
 #include "gtk-source-vim-command-bar.h"
 #include "gtk-source-vim-normal.h"
 
@@ -67,6 +68,22 @@ static gboolean
 gtk_source_vim_real_execute_command (GtkSourceVim *self,
                                      const char   *command)
 {
+       GtkSourceVimState *new_state;
+
+       g_assert (GTK_SOURCE_IS_VIM (self));
+       g_assert (command != NULL);
+
+       new_state = gtk_source_vim_command_new_parsed (command);
+
+       if (new_state != NULL)
+       {
+               GtkSourceVimState *current = gtk_source_vim_state_get_current (GTK_SOURCE_VIM_STATE (self));
+               gtk_source_vim_state_set_parent (new_state, current);
+               gtk_source_vim_state_repeat (new_state);
+               gtk_source_vim_state_unparent (new_state);
+               return TRUE;
+       }
+
        return FALSE;
 }
 


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