[gtksourceview/wip/chergert/vim: 51/293] add b and B motion




commit 90ec8eb16e06e52e4aa1df621ad768719f74656f
Author: Christian Hergert <chergert redhat com>
Date:   Sat Oct 23 10:36:18 2021 -0700

    add b and B motion

 gtksourceview/vim/gtk-source-vim-motion.c | 121 ++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index 7d423d6a..6a294d29 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -130,6 +130,95 @@ forward_classified_start (GtkTextIter  *iter,
        return FALSE;
 }
 
+static gboolean
+backward_classified_end (GtkTextIter  *iter,
+                         gint        (*classify) (gunichar))
+{
+       gunichar ch;
+       gint begin_class;
+       gint cur_class;
+
+       g_assert (iter);
+
+       ch = gtk_text_iter_get_char (iter);
+       begin_class = classify (ch);
+
+       if (begin_class == CLASS_NEWLINE)
+       {
+               gtk_text_iter_forward_char (iter);
+               return TRUE;
+       }
+
+       for (;;)
+       {
+               if (!gtk_text_iter_backward_char (iter))
+                       return FALSE;
+
+               ch = gtk_text_iter_get_char (iter);
+               cur_class = classify (ch);
+
+               if (cur_class == CLASS_NEWLINE)
+               {
+                       gtk_text_iter_forward_char (iter);
+                       return TRUE;
+               }
+
+               /* reset begin_class if we hit space, we can take anything after that */
+               if (cur_class == CLASS_SPACE)
+                       begin_class = CLASS_SPACE;
+
+               if (cur_class != begin_class && cur_class != CLASS_SPACE)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
+static gboolean
+backward_classified_start (GtkTextIter  *iter,
+                           gint        (*classify) (gunichar))
+{
+       gunichar ch;
+       gint begin_class;
+       gint cur_class;
+
+       g_assert (iter);
+
+       if (!gtk_text_iter_backward_char (iter))
+               return FALSE;
+
+       /* If we are on space, walk to the end of the previous word. */
+       ch = gtk_text_iter_get_char (iter);
+       if (classify (ch) == CLASS_SPACE)
+               if (!backward_classified_end (iter, classify))
+                       return FALSE;
+
+       ch = gtk_text_iter_get_char (iter);
+       begin_class = classify (ch);
+       if (begin_class == CLASS_NEWLINE)
+       {
+               gtk_text_iter_forward_char (iter);
+               return TRUE;
+       }
+
+       for (;;)
+       {
+               if (!gtk_text_iter_backward_char (iter))
+                       return FALSE;
+
+               ch = gtk_text_iter_get_char (iter);
+               cur_class = classify (ch);
+
+               if (cur_class != begin_class || cur_class == CLASS_NEWLINE)
+               {
+                       gtk_text_iter_forward_char (iter);
+                       return TRUE;
+               }
+       }
+
+       return FALSE;
+}
+
 static void
 get_iter_at_visual_column (GtkSourceView *view,
                            GtkTextIter   *iter,
@@ -383,6 +472,34 @@ motion_forward_WORD_start (GtkTextIter   *iter,
        return forward_classified_start (iter, classify_WORD);
 }
 
+static gboolean
+motion_backward_word_start (GtkTextIter   *iter,
+                          GtkSourceView *view)
+{
+       return backward_classified_start (iter, classify_word);
+}
+
+static gboolean
+motion_backward_WORD_start (GtkTextIter   *iter,
+                          GtkSourceView *view)
+{
+       return backward_classified_start (iter, classify_WORD);
+}
+
+static gboolean
+motion_backward_word_end (GtkTextIter   *iter,
+                          GtkSourceView *view)
+{
+       return backward_classified_end (iter, classify_word);
+}
+
+static gboolean
+motion_backward_WORD_end (GtkTextIter   *iter,
+                          GtkSourceView *view)
+{
+       return backward_classified_end (iter, classify_WORD);
+}
+
 GtkSourceVimState *
 gtk_source_vim_motion_new (void)
 {
@@ -508,7 +625,11 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                        return gtk_source_vim_motion_complete (self, motion_forward_WORD_start);
 
                case GDK_KEY_b:
+                       return gtk_source_vim_motion_complete (self, motion_backward_word_start);
+
                case GDK_KEY_B:
+                       return gtk_source_vim_motion_complete (self, motion_backward_WORD_start);
+
                case GDK_KEY_e:
                case GDK_KEY_E:
                case GDK_KEY_f:


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