[gnome-builder/wip/csoriano/operators] source-vim: improve w modifier for operators



commit 943ec3a206d6d3bb7275e2c71d563f80ace92e5a
Author: Carlos Soriano <csoriano gnome org>
Date:   Tue Feb 3 09:30:29 2015 +0100

    source-vim: improve w modifier for operators

 src/vim/gb-source-vim.c |  107 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 74 insertions(+), 33 deletions(-)
---
diff --git a/src/vim/gb-source-vim.c b/src/vim/gb-source-vim.c
index e2e554a..4b65740 100644
--- a/src/vim/gb-source-vim.c
+++ b/src/vim/gb-source-vim.c
@@ -207,6 +207,7 @@ enum
 {
   CLASS_0,
   CLASS_SPACE,
+  CLASS_RETURN,
   CLASS_SPECIAL,
   CLASS_WORD,
 };
@@ -344,9 +345,11 @@ gb_source_vim_classify (gunichar ch)
     {
     case ' ':
     case '\t':
-    case '\n':
       return CLASS_SPACE;
 
+    case '\n':
+      return CLASS_RETURN;
+
     case '"': case '\'':
     case '(': case ')':
     case '{': case '}':
@@ -767,6 +770,9 @@ gb_source_vim_get_next_char_iter (GbSourceVim      *vim,
     }
 }
 
+
+
+
 static gboolean
 gb_source_vim_get_selection_bounds (GbSourceVim *vim,
                                     GtkTextIter *insert_iter,
@@ -1037,7 +1043,6 @@ text_iter_backward_vim_word (GtkTextIter *iter)
    * If we are on space, walk until we get to non-whitespace. Then work our way
    * back to the beginning of the word.
    */
-  ch = gtk_text_iter_get_char (iter);
   if (gb_source_vim_classify (ch) == CLASS_SPACE)
     {
       for (;;)
@@ -1163,46 +1168,59 @@ gb_source_vim_move_forward (GbSourceVim *vim)
 }
 
 static gboolean
-text_iter_forward_vim_word (GtkTextIter *iter)
+text_iter_forward_vim_word (GtkTextIter *iter,
+                            gboolean     eol)
 {
   gint begin_class;
   gint cur_class;
   gunichar ch;
 
   g_assert (iter);
-
   ch = gtk_text_iter_get_char (iter);
   begin_class = gb_source_vim_classify (ch);
+  g_print ("begin class %i\n", ch);
+
+  if (eol && begin_class == CLASS_RETURN) {
+    return TRUE;
+  }
 
   /* Move to the first non-whitespace character if necessary. */
-  if (begin_class == CLASS_SPACE)
+  if (begin_class == CLASS_SPACE || (!eol && begin_class == CLASS_RETURN))
     {
       for (;;)
         {
-          if (!gtk_text_iter_forward_char (iter))
+          if (!gtk_text_iter_forward_char (iter)) {
+            g_print ("false\n");
             return FALSE;
+          }
 
           ch = gtk_text_iter_get_char (iter);
           cur_class = gb_source_vim_classify (ch);
-          if (cur_class != CLASS_SPACE)
+          g_print ("text_iter_forward_vim_word space %i, %i\n", ch, gtk_text_iter_get_line (iter));
+          if (cur_class != CLASS_SPACE && (eol || cur_class != CLASS_RETURN)) {
+            g_print ("rtrue\n");
             return TRUE;
+          }
         }
     }
-
   /* move to first character not at same class level. */
   while (gtk_text_iter_forward_char (iter))
     {
       ch = gtk_text_iter_get_char (iter);
+      g_print ("text_iter_forward_vim_word no space %i %i\n", ch, gtk_text_iter_get_line (iter));
       cur_class = gb_source_vim_classify (ch);
 
-      if (cur_class == CLASS_SPACE)
+      if (cur_class == CLASS_SPACE || (!eol && cur_class == CLASS_RETURN))
         {
+          g_print ("continue %i\n", ch);
           begin_class = CLASS_0;
           continue;
         }
 
-      if (cur_class != begin_class)
+      if (cur_class != begin_class) {
+        g_print ("true no space %i %i\n", ch, gtk_text_iter_get_line_offset (iter));
         return TRUE;
+      }
     }
 
   return FALSE;
@@ -1223,7 +1241,7 @@ text_iter_forward_vim_word_end (GtkTextIter *iter)
   /* If we are on space, walk to the start of the next word. */
   ch = gtk_text_iter_get_char (iter);
   if (gb_source_vim_classify (ch) == CLASS_SPACE)
-    if (!text_iter_forward_vim_word (iter))
+    if (!text_iter_forward_vim_word (iter, TRUE))
       return FALSE;
 
   ch = gtk_text_iter_get_char (iter);
@@ -1248,12 +1266,14 @@ text_iter_forward_vim_word_end (GtkTextIter *iter)
 }
 
 static void
-gb_source_vim_move_forward_word (GbSourceVim *vim)
+gb_source_vim_move_forward_word (GbSourceVim *vim,
+                                 gboolean     eol)
 {
   GtkTextBuffer *buffer;
   GtkTextIter iter;
   GtkTextIter selection;
   gboolean has_selection;
+  gboolean forward_word;
 
   g_assert (GB_IS_SOURCE_VIM (vim));
 
@@ -1264,7 +1284,9 @@ gb_source_vim_move_forward_word (GbSourceVim *vim)
   buffer = gtk_text_view_get_buffer (vim->priv->text_view);
   has_selection = gb_source_vim_get_selection_bounds (vim, &iter, &selection);
 
-  if (!text_iter_forward_vim_word (&iter))
+  forward_word = text_iter_forward_vim_word (&iter, eol);
+  g_print ("operator %i, %i\n", eol, forward_word);
+  if (!forward_word)
     gtk_text_buffer_get_end_iter (buffer, &iter);
 
   if (has_selection)
@@ -1740,33 +1762,42 @@ gb_source_vim_delete_selection (GbSourceVim *vim)
 
   buffer = gtk_text_view_get_buffer (vim->priv->text_view);
   gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
-
-  /*
+ /*
    * If there is no selection to delete, try to remove the next character
    * in the line. If there is no next character, delete the last character
    * in the line. It might look like there is no selection if the line
    * was empty.
    */
+  g_print ("end and start iters %i %i\n", gtk_text_iter_get_offset (&begin), gtk_text_iter_get_offset 
(&end));
   if (gtk_text_iter_equal (&begin, &end))
     {
       if (gtk_text_iter_starts_line (&begin) &&
           gtk_text_iter_ends_line (&end) &&
-          (0 == gtk_text_iter_get_line_offset (&end)))
+          (0 == gtk_text_iter_get_line_offset (&end))) {
+            g_print ("loong\n");
         return;
+          }
       else if (!gtk_text_iter_ends_line (&end))
         {
-          if (!gtk_text_iter_forward_char (&end))
+          g_print ("!gtk_text_iter_ends_line (&end)\n");
+          if (!gtk_text_iter_forward_char (&end)) {
+            g_print ("!gtk_text_iter_forward_char (&end)\n");
             gtk_text_buffer_get_end_iter (buffer, &end);
+          }
         }
       else if (!gtk_text_iter_starts_line (&begin))
         {
-          if (!gtk_text_iter_backward_char (&begin))
+          g_print ("!gtk_text_iter_starts_line (&begin)\n");
+          if (!gtk_text_iter_backward_char (&begin)) {
+            g_print ("!gtk_text_iter_backward_char (&begin)\n");
             return;
+          }
         }
-      else
+      else {
+        g_print ("return\n");
         return;
+      }
     }
-
   /*
    * Yank the selection text and apply it to the clipboard.
    */
@@ -1775,8 +1806,8 @@ gb_source_vim_delete_selection (GbSourceVim *vim)
                                         GDK_SELECTION_CLIPBOARD);
   gtk_clipboard_set_text (clipboard, text, -1);
   g_free (text);
-
   gtk_text_buffer_begin_user_action (buffer);
+  g_print ("iters %i %i\n", gtk_text_iter_get_line_offset (&begin), gtk_text_iter_get_line_offset (&end));
   gtk_text_buffer_delete (buffer, &begin, &end);
   gtk_text_buffer_end_user_action (buffer);
 
@@ -1938,9 +1969,11 @@ gb_source_vim_select_char (GbSourceVim *vim)
 static void
 gb_source_vim_apply_motion (GbSourceVim *vim,
                             char         motion,
+                            char         operator,
                             guint        count)
 {
   GbSourceVimCommand *cmd;
+  gboolean has_selection;
 
   cmd = g_hash_table_lookup (gCommands, GINT_TO_POINTER (motion));
   if (!cmd || (cmd->type != GB_SOURCE_VIM_COMMAND_MOVEMENT))
@@ -1950,22 +1983,25 @@ gb_source_vim_apply_motion (GbSourceVim *vim,
     gb_source_vim_select_line (vim);
   else
     gb_source_vim_select_char (vim);
-
-  cmd->func (vim, count, '\0');
-
-  if ((cmd->flags & GB_SOURCE_VIM_COMMAND_FLAG_MOTION_EXCLUSIVE))
+  g_print ("apply motion %c\n", operator);
+  cmd->func (vim, count, operator);
+ if ((cmd->flags & GB_SOURCE_VIM_COMMAND_FLAG_MOTION_EXCLUSIVE))
     {
       GtkTextIter iter, selection;
-
-      gb_source_vim_get_selection_bounds (vim, &iter, &selection);
-      if (gtk_text_iter_compare (&iter, &selection) < 0)
+      has_selection = gb_source_vim_get_selection_bounds (vim, &iter, &selection);
+      if (!has_selection)
+        return;
+      g_print ("apply motion  inside %i %i %i, %i %i\n", gtk_text_iter_get_line_offset (&iter), 
gtk_text_iter_get_line_offset (&selection), gtk_text_iter_compare (&iter, &selection), gtk_text_iter_get_line 
(&iter), gtk_text_iter_get_line (&selection));
+      if (gtk_text_iter_compare (&iter, &selection) < 0) {
         text_iter_swap (&iter, &selection);
-
+      }
+      gtk_text_iter_backward_char (&iter);
       /* From the docs:
        * "If the motion is exclusive and the end of the motion is in column 1,
        *  the end of the motion is moved to the end of the previous line and
        *  the motion becomes inclusive."
        */
+      g_print ("apply motion %i %i\n", gtk_text_iter_get_line_offset (&iter), gtk_text_iter_get_line_offset 
(&selection));
       if (gtk_text_iter_get_line_offset (&iter) == 0)
         {
           GtkTextIter tmp;
@@ -1989,6 +2025,7 @@ gb_source_vim_apply_motion (GbSourceVim *vim,
                while (!gtk_text_iter_starts_line (&iter))
                  gtk_text_iter_forward_char (&iter);
              }
+        gtk_text_iter_forward_char (&iter);
         }
       else
         {
@@ -4881,9 +4918,10 @@ gb_source_vim_cmd_delete (GbSourceVim *vim,
             gb_source_vim_cmd_select_line (vim, count, '\0');
         }
       else
-        gb_source_vim_apply_motion (vim, modifier, count);
+        gb_source_vim_apply_motion (vim, modifier, 'd', count);
     }
 
+  g_print ("delete selection\n");
   gb_source_vim_delete_selection (vim);
 
   if (modifier == 'd')
@@ -5413,16 +5451,19 @@ gb_source_vim_cmd_select (GbSourceVim *vim,
 static void
 gb_source_vim_cmd_forward_word (GbSourceVim *vim,
                                 guint        count,
-                                gchar        modifier)
+                                gchar        operator)
 {
   guint i;
+  gboolean eol;
 
   g_assert (GB_IS_SOURCE_VIM (vim));
 
   count = MAX (1, count);
 
+  g_print ("forward word\n");
+  eol = operator == 'd' || operator == 'c';
   for (i = 0; i < count; i++)
-    gb_source_vim_move_forward_word (vim);
+    gb_source_vim_move_forward_word (vim, eol);
 }
 
 static void
@@ -5490,7 +5531,7 @@ gb_source_vim_cmd_yank (GbSourceVim *vim,
           gb_source_vim_cmd_select_line (vim, count, '\0');
         }
       else
-        gb_source_vim_apply_motion (vim, modifier, count);
+        gb_source_vim_apply_motion (vim, modifier, 'y', count);
     }
 
   gb_source_vim_yank (vim);


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