[gtksourceview/wip/chergert/vim: 236/363] start on linewise selection and :delete command




commit 75895c9f57042f2f4ca8019cb7c49bf1daac0fe9
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 3 10:34:58 2021 -0700

    start on linewise selection and :delete command

 gtksourceview/vim/gtk-source-vim-command.c | 62 ++++++++++++++++++------------
 1 file changed, 37 insertions(+), 25 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-command.c b/gtksourceview/vim/gtk-source-vim-command.c
index a8650a57..d531a8cb 100644
--- a/gtksourceview/vim/gtk-source-vim-command.c
+++ b/gtksourceview/vim/gtk-source-vim-command.c
@@ -26,6 +26,7 @@
 #include "gtksourcebuffer.h"
 
 #include "gtk-source-vim-command.h"
+#include "gtk-source-vim-registers.h"
 
 typedef void (*Command) (GtkSourceVimCommand *self);
 
@@ -54,27 +55,33 @@ static GParamSpec *properties[N_PROPS];
 static GHashTable *commands;
 
 static void
-extend_lines (GtkTextIter *a,
-              GtkTextIter *b)
+gtk_source_vim_command_delete (GtkSourceVimCommand *self)
 {
-       if (gtk_text_iter_equal (a, b))
-       {
-               gtk_text_iter_set_line_offset (a, 0);
-               if (!gtk_text_iter_ends_line (b))
-                       gtk_text_iter_forward_to_line_end (b);
-       }
-       else if (gtk_text_iter_compare (a, b) < 0)
-       {
-               gtk_text_iter_set_line_offset (a, 0);
-               if (!gtk_text_iter_ends_line (b))
-                       gtk_text_iter_forward_to_line_end (b);
-       }
-       else
+       GtkSourceVimState *registers;
+       GtkSourceBuffer *buffer;
+       GtkTextIter iter, selection;
+       char *text;
+
+       gtk_source_vim_state_set_can_repeat (GTK_SOURCE_VIM_STATE (self), TRUE);
+
+       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
+       gtk_source_vim_state_select_linewise (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
+       text = gtk_text_iter_get_slice (&iter, &selection);
+
+       if (gtk_text_iter_is_end (&selection) || gtk_text_iter_is_end (&iter))
        {
-               gtk_text_iter_set_line_offset (b, 0);
-               if (!gtk_text_iter_ends_line (a))
-                       gtk_text_iter_forward_to_line_end (a);
+               char *tmp = text;
+               text = g_strdup_printf ("%s\n", tmp);
+               g_free (tmp);
        }
+
+       gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
+       gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &iter, &selection);
+       gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
+
+       registers = gtk_source_vim_state_get_registers (GTK_SOURCE_VIM_STATE (self));
+       gtk_source_vim_registers_set (GTK_SOURCE_VIM_REGISTERS (registers), NULL, text);
+       g_free (text);
 }
 
 static void
@@ -119,12 +126,6 @@ gtk_source_vim_command_yank (GtkSourceVimCommand *self)
 
        gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
 
-       /* if linewise, then extend to whole lines */
-       if (gtk_text_iter_get_line (&iter) != gtk_text_iter_get_line (&selection))
-       {
-               extend_lines (&iter, &selection);
-       }
-
        gtk_text_iter_order (&selection, &iter);
 
        if (gtk_text_iter_ends_line (&iter))
@@ -417,6 +418,7 @@ gtk_source_vim_command_repeat (GtkSourceVimState *state)
        GtkTextIter iter;
        GtkTextIter selection;
        GtkTextMark *mark;
+       gboolean linewise = FALSE;
 
        g_assert (GTK_SOURCE_IS_VIM_COMMAND (self));
 
@@ -432,14 +434,23 @@ gtk_source_vim_command_repeat (GtkSourceVimState *state)
        if (self->motion)
        {
                gtk_source_vim_motion_apply (self->motion, &iter, TRUE);
+               linewise |= gtk_source_vim_motion_is_linewise (self->motion);
        }
 
        if (self->selection_motion)
        {
                gtk_source_vim_motion_apply (self->selection_motion, &selection, TRUE);
+               linewise |= gtk_source_vim_motion_is_linewise (self->selection_motion);
        }
 
-       gtk_source_vim_state_select (state, &iter, &selection);
+       if (linewise)
+       {
+               gtk_source_vim_state_select_linewise (state, &iter, &selection);
+       }
+       else
+       {
+               gtk_source_vim_state_select (state, &iter, &selection);
+       }
 
        command (self);
 
@@ -593,6 +604,7 @@ gtk_source_vim_command_class_init (GtkSourceVimCommandClass *klass)
 
 #define ADD_COMMAND(name, func) \
        g_hash_table_insert(commands, (char*)name, (gpointer)func)
+       ADD_COMMAND (":delete",        gtk_source_vim_command_delete);
        ADD_COMMAND (":join",          gtk_source_vim_command_join);
        ADD_COMMAND (":j",             gtk_source_vim_command_join);
        ADD_COMMAND (":undo",          gtk_source_vim_command_undo);


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