[gtksourceview/wip/chergert/vim] add indent and unindent commands



commit 3e4790f4e0863bc7f8c858a798d7467ccf5362f2
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 3 16:41:20 2021 -0700

    add indent and unindent commands

 gtksourceview/vim/gtk-source-vim-command.c | 52 ++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-command.c b/gtksourceview/vim/gtk-source-vim-command.c
index 3d13e86d..232a9143 100644
--- a/gtksourceview/vim/gtk-source-vim-command.c
+++ b/gtksourceview/vim/gtk-source-vim-command.c
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include "gtksourcebuffer.h"
+#include "gtksourceview.h"
 
 #include "gtk-source-vim-char-pending.h"
 #include "gtk-source-vim-command.h"
@@ -56,6 +57,55 @@ enum {
 static GParamSpec *properties[N_PROPS];
 static GHashTable *commands;
 
+static void
+gtk_source_vim_command_shift (GtkSourceVimCommand *self,
+                              int                  direction)
+{
+       GtkSourceBuffer *buffer;
+       GtkSourceView *view;
+       GtkTextIter iter, selection;
+       int count;
+
+       gtk_source_vim_state_set_can_repeat (GTK_SOURCE_VIM_STATE (self), TRUE);
+
+       count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+
+       if (count == 0)
+       {
+               return;
+       }
+
+       buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
+       view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
+
+       gtk_text_iter_order (&iter, &selection);
+
+       gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
+
+       for (int i = 0; i < count; i++)
+       {
+
+               if (direction > 0)
+                       gtk_source_view_indent_lines (view, &iter, &selection);
+               else
+                       gtk_source_view_unindent_lines (view, &iter, &selection);
+       }
+
+       gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
+}
+
+static void
+gtk_source_vim_command_indent (GtkSourceVimCommand *self)
+{
+       return gtk_source_vim_command_shift (self, 1);
+}
+
+static void
+gtk_source_vim_command_unindent (GtkSourceVimCommand *self)
+{
+       return gtk_source_vim_command_shift (self, -1);
+}
+
 static void
 gtk_source_vim_command_delete (GtkSourceVimCommand *self)
 {
@@ -690,6 +740,8 @@ gtk_source_vim_command_class_init (GtkSourceVimCommandClass *klass)
        ADD_COMMAND ("downcase",       gtk_source_vim_command_downcase);
        ADD_COMMAND ("rot13",          gtk_source_vim_command_rot13);
        ADD_COMMAND ("replace-one",    gtk_source_vim_command_replace_one);
+       ADD_COMMAND ("indent",         gtk_source_vim_command_indent);
+       ADD_COMMAND ("unindent",       gtk_source_vim_command_unindent);
 #undef ADD_COMMAND
 }
 


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