[gtksourceview/wip/chergert/vim] make signals closer to vim meanings



commit 044e57b39f2e04f3787af7adaa53ff3c14c845d0
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 10 17:40:47 2021 -0800

    make signals closer to vim meanings
    
    that way they are less likely to be confused.

 gtksourceview/gtksourcevimimcontext.c | 86 ++++++++++++++++++++++++++++-------
 1 file changed, 69 insertions(+), 17 deletions(-)
---
diff --git a/gtksourceview/gtksourcevimimcontext.c b/gtksourceview/gtksourcevimimcontext.c
index a5fd02c1..094b7e63 100644
--- a/gtksourceview/gtksourcevimimcontext.c
+++ b/gtksourceview/gtksourcevimimcontext.c
@@ -65,8 +65,8 @@ enum {
        FORMAT_TEXT,
        DISCOVER_URI,
        LOAD_URI,
-       OPEN,
-       SAVE,
+       EDIT,
+       WRITE,
        N_SIGNALS
 };
 
@@ -88,6 +88,52 @@ gtk_source_vim_im_context_new (void)
        return g_object_new (GTK_SOURCE_TYPE_VIM_IM_CONTEXT, NULL);
 }
 
+static gboolean
+gtk_source_vim_im_context_real_execute_command (GtkSourceVimIMContext *self,
+                                                const char            *command)
+{
+       g_auto(GStrv) parts = NULL;
+
+       g_assert (GTK_SOURCE_IS_VIM_IM_CONTEXT (self));
+       g_assert (command != NULL);
+
+       parts = g_strsplit (command, " ", 2);
+
+       if (parts[1] != NULL)
+       {
+               g_strstrip (parts[1]);
+       }
+
+       if (g_str_equal (command, ":w") ||
+           g_str_equal (command, ":write"))
+       {
+               g_signal_emit (self, signals[WRITE], 0, NULL);
+               return TRUE;
+       }
+       else if (g_str_equal (command, ":e") ||
+                g_str_equal (command, ":edit"))
+       {
+               g_signal_emit (self, signals[EDIT], 0, NULL);
+               return TRUE;
+       }
+       else if (g_str_has_prefix (command, ":w ") ||
+                g_str_has_prefix (command, ":write "))
+       {
+               g_signal_emit (self, signals[WRITE], 0, parts[1]);
+               g_print ("Save %s\n", parts[1]);
+               return TRUE;
+       }
+       else if (g_str_has_prefix (command, ":e ") ||
+                g_str_has_prefix (command, ":edit "))
+       {
+               g_signal_emit (self, signals[EDIT], 0, parts[1]);
+               g_print ("Open %s\n", parts[1]);
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
 static void
 on_vim_notify_cb (GtkSourceVimIMContext *self,
                   GParamSpec            *pspec,
@@ -331,15 +377,15 @@ gtk_source_vim_im_context_class_init (GtkSourceVimIMContextClass *klass)
         * Since: 5.4
         */
        signals[EXECUTE_COMMAND] =
-               g_signal_new ("execute-command",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_LAST,
-                             0,
-                             g_signal_accumulator_true_handled, NULL,
-                             NULL,
-                             G_TYPE_BOOLEAN,
-                             1,
-                             G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
+               g_signal_new_class_handler ("execute-command",
+                                           G_TYPE_FROM_CLASS (klass),
+                                           G_SIGNAL_RUN_LAST,
+                                           G_CALLBACK (gtk_source_vim_im_context_real_execute_command),
+                                           g_signal_accumulator_true_handled, NULL,
+                                           NULL,
+                                           G_TYPE_BOOLEAN,
+                                           1,
+                                           G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
 
        /**
         * GtkSourceVimIMContext::format-text:
@@ -413,7 +459,7 @@ gtk_source_vim_im_context_class_init (GtkSourceVimIMContextClass *klass)
                              GTK_SOURCE_TYPE_VIEW);
 
        /**
-        * GtkSourceVimIMContext::save:
+        * GtkSourceVimIMContext::write:
         * @self: a #GtkSourceVimIMContext
         * @view: the #GtkSourceView
         * @path: (nullable): the path if provided, otherwise %NULL
@@ -421,10 +467,13 @@ gtk_source_vim_im_context_class_init (GtkSourceVimIMContextClass *klass)
         * Requests the application save the file. If a filename was provided,
         * it will be available to the signal handler as @path.
         *
+        * This may be executed in relation to the user running the
+        * `:write` or `:w` commands.
+        *
         * Since: 5.4
         */
-       signals[SAVE] =
-               g_signal_new ("save",
+       signals[WRITE] =
+               g_signal_new ("write",
                              G_TYPE_FROM_CLASS (klass),
                              G_SIGNAL_RUN_LAST,
                              0,
@@ -436,7 +485,7 @@ gtk_source_vim_im_context_class_init (GtkSourceVimIMContextClass *klass)
                              G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
 
        /**
-        * GtkSourceVimIMContext::open:
+        * GtkSourceVimIMContext::edit:
         * @self: a #GtkSourceVimIMContext
         * @view: the #GtkSourceView
         * @path: (nullable): the path if provided, otherwise %NULL
@@ -444,10 +493,13 @@ gtk_source_vim_im_context_class_init (GtkSourceVimIMContextClass *klass)
         * Requests the application open the file found at @path. If @path is
         * %NULL, then the current file should be reloaded from storage.
         *
+        * This may be executed in relation to the user running the
+        * `:edit` or `:e` commands.
+        *
         * Since: 5.4
         */
-       signals[OPEN] =
-               g_signal_new ("open",
+       signals[EDIT] =
+               g_signal_new ("edit",
                              G_TYPE_FROM_CLASS (klass),
                              G_SIGNAL_RUN_LAST,
                              0,


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