[gtksourceview/wip/chergert/vim: 210/293] add numbered registers




commit 60a45599952ec7f58c55289d6d67cd02ba52149b
Author: Christian Hergert <chergert redhat com>
Date:   Tue Nov 2 12:47:39 2021 -0700

    add numbered registers
    
    and push into 0 position when yanking to default

 gtksourceview/vim/gtk-source-vim-delete.c       |   7 +-
 gtksourceview/vim/gtk-source-vim-registers.c    |  89 ++++++++++++++++--
 gtksourceview/vim/gtk-source-vim-registers.h    |  21 ++---
 gtksourceview/vim/gtk-source-vim-state.c        |  15 ---
 gtksourceview/vim/gtk-source-vim-state.h        | 118 ++++++++++++------------
 gtksourceview/vim/gtk-source-vim-text-history.c |   5 +-
 6 files changed, 153 insertions(+), 102 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-delete.c b/gtksourceview/vim/gtk-source-vim-delete.c
index c3b2d69e..7f2ba456 100644
--- a/gtksourceview/vim/gtk-source-vim-delete.c
+++ b/gtksourceview/vim/gtk-source-vim-delete.c
@@ -133,6 +133,7 @@ gtk_source_vim_delete_repeat (GtkSourceVimState *state)
        GtkSourceVimDelete *self = (GtkSourceVimDelete *)state;
        GtkSourceBuffer *buffer;
        GtkTextIter insert, selection;
+       char *text;
 
        g_assert (GTK_SOURCE_IS_VIM_DELETE (self));
 
@@ -150,9 +151,9 @@ gtk_source_vim_delete_repeat (GtkSourceVimState *state)
        }
 
        /* Copy the deleted text to the unnamed register */
-       gtk_source_vim_registers_take (gtk_source_vim_state_get_registers (state),
-                                      NULL,
-                                      gtk_text_iter_get_slice (&insert, &selection));
+       text = gtk_text_iter_get_slice (&insert, &selection);
+       gtk_source_vim_state_set_current_register_value (state, text);
+       g_free (text);
 
        gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &insert, &selection);
 
diff --git a/gtksourceview/vim/gtk-source-vim-registers.c b/gtksourceview/vim/gtk-source-vim-registers.c
index 38b667a0..81d15f09 100644
--- a/gtksourceview/vim/gtk-source-vim-registers.c
+++ b/gtksourceview/vim/gtk-source-vim-registers.c
@@ -29,7 +29,11 @@
 struct _GtkSourceVimRegisters
 {
        GtkSourceVimState parent_instance;
+
        GHashTable *values;
+
+       char *numbered[10];
+       int numbered_pos;
 };
 
 G_DEFINE_TYPE (GtkSourceVimRegisters, gtk_source_vim_registers, GTK_SOURCE_TYPE_VIM_STATE)
@@ -41,6 +45,11 @@ gtk_source_vim_registers_finalize (GObject *object)
 
        g_clear_pointer (&self->values, g_hash_table_unref);
 
+       for (guint i = 0; i < G_N_ELEMENTS (self->numbered); i++)
+       {
+               g_clear_pointer (&self->numbered[i], g_ref_string_release);
+       }
+
        G_OBJECT_CLASS (gtk_source_vim_registers_parent_class)->finalize (object);
 }
 
@@ -55,7 +64,10 @@ gtk_source_vim_registers_class_init (GtkSourceVimRegistersClass *klass)
 static void
 gtk_source_vim_registers_init (GtkSourceVimRegisters *self)
 {
-       self->values = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
+       self->values = g_hash_table_new_full (g_str_hash,
+                                             g_str_equal,
+                                             NULL,
+                                             (GDestroyNotify)g_ref_string_release);
 }
 
 GtkSourceVimState *
@@ -75,22 +87,65 @@ gtk_source_vim_registers_get (GtkSourceVimRegisters *self,
                name = DEFAULT_REGISTER;
        }
 
+       if (g_ascii_isdigit (*name))
+       {
+               return gtk_source_vim_registers_get_numbered (self, *name - '0');
+       }
+
        return g_hash_table_lookup (self->values, name);
 }
 
+static inline char **
+get_numbered_pos (GtkSourceVimRegisters *self,
+                  guint                  n)
+{
+       return &self->numbered[(self->numbered_pos + n) % 10];
+}
+
+const char *
+gtk_source_vim_registers_get_numbered (GtkSourceVimRegisters *self,
+                                       guint                  n)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_VIM_REGISTERS (self), NULL);
+       g_return_val_if_fail (n <= 9, NULL);
+
+       return *get_numbered_pos (self, n);
+}
+
+static void
+gtk_source_vim_registers_push (GtkSourceVimRegisters *self,
+                               char                  *str)
+{
+       char **pos;
+
+       g_return_if_fail (GTK_SOURCE_IS_VIM_REGISTERS (self));
+
+       if (self->numbered_pos == 0)
+       {
+               self->numbered_pos = G_N_ELEMENTS (self->numbered) - 1;
+       }
+       else
+       {
+               self->numbered_pos--;
+       }
+
+       pos = get_numbered_pos (self, 0);
+
+       if (*pos != NULL)
+       {
+               g_ref_string_release (*pos);
+       }
+
+       *pos = str ? g_ref_string_acquire (str) : NULL;
+}
+
 void
 gtk_source_vim_registers_set (GtkSourceVimRegisters *self,
                               const char            *name,
                               const char            *value)
 {
-       gtk_source_vim_registers_take (self, name, g_strdup (value));
-}
+       char *str;
 
-void
-gtk_source_vim_registers_take (GtkSourceVimRegisters *self,
-                               const char            *name,
-                               char                  *value)
-{
        g_return_if_fail (GTK_SOURCE_IS_VIM_REGISTERS (self));
 
        if (name == NULL)
@@ -105,9 +160,23 @@ gtk_source_vim_registers_take (GtkSourceVimRegisters *self,
        }
 
        if (value == NULL)
+       {
                g_hash_table_remove (self->values, name);
-       else
-               g_hash_table_insert (self->values, (char *)g_intern_string (name), value);
+               return;
+       }
+
+       str = g_ref_string_new (value);
+       g_hash_table_insert (self->values,
+                            (char *)g_intern_string (name),
+                            str);
+
+       /* Push into the 0 numbered register and each 1..8 to
+        * the next numbered register position.
+        */
+       if (g_strcmp0 (name, DEFAULT_REGISTER) == 0)
+       {
+               gtk_source_vim_registers_push (self, str);
+       }
 }
 
 void
diff --git a/gtksourceview/vim/gtk-source-vim-registers.h b/gtksourceview/vim/gtk-source-vim-registers.h
index 736ff9cc..5b27b592 100644
--- a/gtksourceview/vim/gtk-source-vim-registers.h
+++ b/gtksourceview/vim/gtk-source-vim-registers.h
@@ -29,16 +29,15 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtkSourceVimRegisters, gtk_source_vim_registers, GTK_SOURCE, VIM_REGISTERS, 
GtkSourceVimState)
 
-GtkSourceVimState *gtk_source_vim_registers_new   (void);
-const char        *gtk_source_vim_registers_get   (GtkSourceVimRegisters *self,
-                                                   const char            *name);
-void               gtk_source_vim_registers_set   (GtkSourceVimRegisters *self,
-                                                   const char            *name,
-                                                   const char            *string);
-void               gtk_source_vim_registers_take  (GtkSourceVimRegisters *self,
-                                                   const char            *name,
-                                                   char                  *string);
-void               gtk_source_vim_registers_clear (GtkSourceVimRegisters *self,
-                                                   const char            *name);
+GtkSourceVimState *gtk_source_vim_registers_new          (void);
+const char        *gtk_source_vim_registers_get          (GtkSourceVimRegisters *self,
+                                                          const char            *name);
+const char        *gtk_source_vim_registers_get_numbered (GtkSourceVimRegisters *self,
+                                                          guint                  n);
+void               gtk_source_vim_registers_set          (GtkSourceVimRegisters *self,
+                                                          const char            *name,
+                                                          const char            *string);
+void               gtk_source_vim_registers_clear        (GtkSourceVimRegisters *self,
+                                                          const char            *name);
 
 G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-state.c b/gtksourceview/vim/gtk-source-vim-state.c
index 93eff621..f44c27e1 100644
--- a/gtksourceview/vim/gtk-source-vim-state.c
+++ b/gtksourceview/vim/gtk-source-vim-state.c
@@ -1010,18 +1010,3 @@ gtk_source_vim_state_set_current_register_value (GtkSourceVimState *self,
 
        gtk_source_vim_registers_set (GTK_SOURCE_VIM_REGISTERS (registers), current_register, value);
 }
-
-void
-gtk_source_vim_state_take_current_register_value (GtkSourceVimState *self,
-                                                  char              *value)
-{
-       GtkSourceVimState *registers;
-       const char *current_register;
-
-       g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
-
-       current_register = gtk_source_vim_state_get_current_register (self);
-       registers = gtk_source_vim_state_get_registers (self);
-
-       gtk_source_vim_registers_take (GTK_SOURCE_VIM_REGISTERS (registers), current_register, value);
-}
diff --git a/gtksourceview/vim/gtk-source-vim-state.h b/gtksourceview/vim/gtk-source-vim-state.h
index 0a751512..654f91db 100644
--- a/gtksourceview/vim/gtk-source-vim-state.h
+++ b/gtksourceview/vim/gtk-source-vim-state.h
@@ -59,66 +59,64 @@ struct _GtkSourceVimStateClass
                                             GString           *string);
 };
 
-void               gtk_source_vim_state_set_parent                  (GtkSourceVimState *self,
-                                                                     GtkSourceVimState *parent);
-void               gtk_source_vim_state_push                        (GtkSourceVimState *self,
-                                                                     GtkSourceVimState *new_state);
-void               gtk_source_vim_state_pop                         (GtkSourceVimState *self);
-void               gtk_source_vim_state_append_command              (GtkSourceVimState *self,
-                                                                     GString           *string);
-void               gtk_source_vim_state_beep                        (GtkSourceVimState *self);
-GtkSourceVimState *gtk_source_vim_state_get_child                   (GtkSourceVimState *self);
-GtkSourceVimState *gtk_source_vim_state_get_current                 (GtkSourceVimState *self);
-GtkSourceView     *gtk_source_vim_state_get_view                    (GtkSourceVimState *self);
-GtkSourceBuffer   *gtk_source_vim_state_get_buffer                  (GtkSourceVimState *self,
-                                                                     GtkTextIter       *insert,
-                                                                     GtkTextIter       *selection_bound);
-GtkSourceVimState *gtk_source_vim_state_get_root                    (GtkSourceVimState *self);
-GtkSourceVimState *gtk_source_vim_state_get_parent                  (GtkSourceVimState *self);
-GtkSourceVimState *gtk_source_vim_state_get_registers               (GtkSourceVimState *self);
-int                gtk_source_vim_state_get_count                   (GtkSourceVimState *self);
-gboolean           gtk_source_vim_state_get_count_set               (GtkSourceVimState *self);
-void               gtk_source_vim_state_set_count                   (GtkSourceVimState *self,
-                                                                     int                count);
-gboolean           gtk_source_vim_state_get_can_repeat              (GtkSourceVimState *self);
-void               gtk_source_vim_state_set_can_repeat              (GtkSourceVimState *self,
-                                                                     gboolean           can_repeat);
-void               gtk_source_vim_state_begin_user_action           (GtkSourceVimState *self);
-void               gtk_source_vim_state_end_user_action             (GtkSourceVimState *self);
-gboolean           gtk_source_vim_state_capture                     (GtkSourceVimState *self,
-                                                                     GdkEvent          *event);
-gboolean           gtk_source_vim_state_handle_event                (GtkSourceVimState *self,
-                                                                     GdkEvent          *event);
-void               gtk_source_vim_state_set_overwrite               (GtkSourceVimState *self,
-                                                                     gboolean           overwrite);
-gboolean           gtk_source_vim_state_synthesize                  (GtkSourceVimState *self,
-                                                                     guint              keyval,
-                                                                     GdkModifierType    mods);
-void               gtk_source_vim_state_repeat                      (GtkSourceVimState *self);
-int                gtk_source_vim_state_get_visible_lines           (GtkSourceVimState *self);
-void               gtk_source_vim_state_scroll_page                 (GtkSourceVimState *self,
-                                                                     int                count);
-void               gtk_source_vim_state_scroll_half_page            (GtkSourceVimState *self,
-                                                                     int                count);
-void               gtk_source_vim_state_scroll_line                 (GtkSourceVimState *self,
-                                                                     int                count);
-void               gtk_source_vim_state_z_scroll                    (GtkSourceVimState *self,
-                                                                     double             yalign);
-void               gtk_source_vim_state_select                      (GtkSourceVimState *self,
-                                                                     const GtkTextIter *insert,
-                                                                     const GtkTextIter *selection);
-const char        *gtk_source_vim_state_get_current_register        (GtkSourceVimState *self);
-void               gtk_source_vim_state_set_current_register        (GtkSourceVimState *self,
-                                                                     const char        *current_register);
-const char        *gtk_source_vim_state_get_current_register_value  (GtkSourceVimState *self);
-void               gtk_source_vim_state_set_current_register_value  (GtkSourceVimState *self,
-                                                                     const char        *value);
-void               gtk_source_vim_state_take_current_register_value (GtkSourceVimState *self,
-                                                                     char              *value);
-void               gtk_source_vim_state_place_cursor_onscreen       (GtkSourceVimState *self);
-void               gtk_source_vim_state_keyval_to_string            (guint              keyval,
-                                                                     GdkModifierType    mods,
-                                                                     char               str[16]);
+void               gtk_source_vim_state_set_parent                 (GtkSourceVimState *self,
+                                                                    GtkSourceVimState *parent);
+void               gtk_source_vim_state_push                       (GtkSourceVimState *self,
+                                                                    GtkSourceVimState *new_state);
+void               gtk_source_vim_state_pop                        (GtkSourceVimState *self);
+void               gtk_source_vim_state_append_command             (GtkSourceVimState *self,
+                                                                    GString           *string);
+void               gtk_source_vim_state_beep                       (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_child                  (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_current                (GtkSourceVimState *self);
+GtkSourceView     *gtk_source_vim_state_get_view                   (GtkSourceVimState *self);
+GtkSourceBuffer   *gtk_source_vim_state_get_buffer                 (GtkSourceVimState *self,
+                                                                    GtkTextIter       *insert,
+                                                                    GtkTextIter       *selection_bound);
+GtkSourceVimState *gtk_source_vim_state_get_root                   (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_parent                 (GtkSourceVimState *self);
+GtkSourceVimState *gtk_source_vim_state_get_registers              (GtkSourceVimState *self);
+int                gtk_source_vim_state_get_count                  (GtkSourceVimState *self);
+gboolean           gtk_source_vim_state_get_count_set              (GtkSourceVimState *self);
+void               gtk_source_vim_state_set_count                  (GtkSourceVimState *self,
+                                                                    int                count);
+gboolean           gtk_source_vim_state_get_can_repeat             (GtkSourceVimState *self);
+void               gtk_source_vim_state_set_can_repeat             (GtkSourceVimState *self,
+                                                                    gboolean           can_repeat);
+void               gtk_source_vim_state_begin_user_action          (GtkSourceVimState *self);
+void               gtk_source_vim_state_end_user_action            (GtkSourceVimState *self);
+gboolean           gtk_source_vim_state_capture                    (GtkSourceVimState *self,
+                                                                    GdkEvent          *event);
+gboolean           gtk_source_vim_state_handle_event               (GtkSourceVimState *self,
+                                                                    GdkEvent          *event);
+void               gtk_source_vim_state_set_overwrite              (GtkSourceVimState *self,
+                                                                    gboolean           overwrite);
+gboolean           gtk_source_vim_state_synthesize                 (GtkSourceVimState *self,
+                                                                    guint              keyval,
+                                                                    GdkModifierType    mods);
+void               gtk_source_vim_state_repeat                     (GtkSourceVimState *self);
+int                gtk_source_vim_state_get_visible_lines          (GtkSourceVimState *self);
+void               gtk_source_vim_state_scroll_page                (GtkSourceVimState *self,
+                                                                    int                count);
+void               gtk_source_vim_state_scroll_half_page           (GtkSourceVimState *self,
+                                                                    int                count);
+void               gtk_source_vim_state_scroll_line                (GtkSourceVimState *self,
+                                                                    int                count);
+void               gtk_source_vim_state_z_scroll                   (GtkSourceVimState *self,
+                                                                    double             yalign);
+void               gtk_source_vim_state_select                     (GtkSourceVimState *self,
+                                                                    const GtkTextIter *insert,
+                                                                    const GtkTextIter *selection);
+const char        *gtk_source_vim_state_get_current_register       (GtkSourceVimState *self);
+void               gtk_source_vim_state_set_current_register       (GtkSourceVimState *self,
+                                                                    const char        *current_register);
+const char        *gtk_source_vim_state_get_current_register_value (GtkSourceVimState *self);
+void               gtk_source_vim_state_set_current_register_value (GtkSourceVimState *self,
+                                                                    const char        *value);
+void               gtk_source_vim_state_place_cursor_onscreen      (GtkSourceVimState *self);
+void               gtk_source_vim_state_keyval_to_string           (guint              keyval,
+                                                                    GdkModifierType    mods,
+                                                                    char               str[16]);
 
 static inline gboolean
 gtk_source_vim_state_is_escape (guint           keyval,
diff --git a/gtksourceview/vim/gtk-source-vim-text-history.c b/gtksourceview/vim/gtk-source-vim-text-history.c
index 9f99276c..42d5b567 100644
--- a/gtksourceview/vim/gtk-source-vim-text-history.c
+++ b/gtksourceview/vim/gtk-source-vim-text-history.c
@@ -288,9 +288,8 @@ gtk_source_vim_text_history_end (GtkSourceVimTextHistory *self)
        }
 
        registers = gtk_source_vim_state_get_registers (GTK_SOURCE_VIM_STATE (self));
-       gtk_source_vim_registers_take (GTK_SOURCE_VIM_REGISTERS (registers),
-                                      ".",
-                                      g_string_free (inserted, FALSE));
+       gtk_source_vim_registers_set (GTK_SOURCE_VIM_REGISTERS (registers), ".", inserted->str);
+       g_string_free (inserted, TRUE);
 }
 
 void


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