[gtksourceview/wip/chergert/vim: 306/363] add some history to command bar




commit 95fb504b0336e71f06c7a7a2ea958fa1cb5ea52b
Author: Christian Hergert <chergert redhat com>
Date:   Fri Nov 5 08:54:53 2021 -0700

    add some history to command bar

 gtksourceview/vim/gtk-source-vim-command-bar.c | 56 ++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 4 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-command-bar.c b/gtksourceview/vim/gtk-source-vim-command-bar.c
index f0c6bc99..57abedb9 100644
--- a/gtksourceview/vim/gtk-source-vim-command-bar.c
+++ b/gtksourceview/vim/gtk-source-vim-command-bar.c
@@ -24,14 +24,20 @@
 #include "gtk-source-vim.h"
 #include "gtk-source-vim-command-bar.h"
 
+#define MAX_HISTORY 25
+
 struct _GtkSourceVimCommandBar
 {
        GtkSourceVimState parent_instance;
+       GPtrArray *history;
        GString *buffer;
+       int history_pos;
 };
 
 G_DEFINE_TYPE (GtkSourceVimCommandBar, gtk_source_vim_command_bar, GTK_SOURCE_TYPE_VIM_STATE)
 
+static GPtrArray *history;
+
 GtkSourceVimState *
 gtk_source_vim_command_bar_new (void)
 {
@@ -67,6 +73,29 @@ do_notify (GtkSourceVimCommandBar *self)
        }
 }
 
+static void
+move_history (GtkSourceVimCommandBar *self,
+              int                     direction)
+{
+       g_assert (GTK_SOURCE_IS_VIM_COMMAND_BAR (self));
+
+       if (history->len == 0)
+               return;
+
+       if (direction < 0)
+               self->history_pos--;
+       else
+               self->history_pos++;
+
+       if (self->history_pos < 0)
+               self->history_pos = history->len - 1;
+       else if (self->history_pos >= history->len)
+               self->history_pos = 0;
+
+       g_string_truncate (self->buffer, 0);
+       g_string_append (self->buffer, g_ptr_array_index (history, self->history_pos));
+}
+
 static void
 do_execute (GtkSourceVimCommandBar *self,
             const char             *command)
@@ -76,6 +105,13 @@ do_execute (GtkSourceVimCommandBar *self,
        g_assert (GTK_SOURCE_IS_VIM_COMMAND_BAR (self));
        g_assert (command != NULL);
 
+       if (history->len > MAX_HISTORY)
+       {
+               g_ptr_array_set_size (history, MAX_HISTORY);
+       }
+
+       g_ptr_array_add (history, g_strdup (command));
+
        root = gtk_source_vim_state_get_root (GTK_SOURCE_VIM_STATE (self));
        if (GTK_SOURCE_IS_VIM (root))
                gtk_source_vim_emit_execute_command (GTK_SOURCE_VIM (root), command);
@@ -92,10 +128,6 @@ gtk_source_vim_command_bar_handle_keypress (GtkSourceVimState *state,
 
        g_assert (GTK_SOURCE_IS_VIM_COMMAND_BAR (self));
 
-       /* TODO: It would be nice to reuse the insert-literal stuff for
-        * this if we can (which might mean using a temporary text view).
-        */
-
        if (gtk_source_vim_state_is_escape (keyval, mods))
        {
                g_string_truncate (self->buffer, 0);
@@ -118,6 +150,18 @@ gtk_source_vim_command_bar_handle_keypress (GtkSourceVimState *state,
 
                        return TRUE;
 
+               case GDK_KEY_Tab:
+               case GDK_KEY_KP_Tab:
+                       break;
+
+               case GDK_KEY_Up:
+                       move_history (self, -1);
+                       break;
+
+               case GDK_KEY_Down:
+                       move_history (self, 1);
+                       break;
+
                case GDK_KEY_Return:
                case GDK_KEY_KP_Enter:
                case GDK_KEY_ISO_Enter:
@@ -148,6 +192,8 @@ gtk_source_vim_command_bar_enter (GtkSourceVimState *state)
 
        g_assert (GTK_SOURCE_VIM_STATE (self));
 
+       self->history_pos = 0;
+
        if (self->buffer->len == 0)
        {
                g_string_append_c (self->buffer, ':');
@@ -184,6 +230,8 @@ gtk_source_vim_command_bar_class_init (GtkSourceVimCommandBarClass *klass)
        state_class->enter = gtk_source_vim_command_bar_enter;
        state_class->leave = gtk_source_vim_command_bar_leave;
        state_class->handle_keypress = gtk_source_vim_command_bar_handle_keypress;
+
+       history = g_ptr_array_new_with_free_func (g_free);
 }
 
 static void


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