[gtksourceview/wip/custom-word-boundaries] Custom word boundaries



commit a0f44115f8c690ed1e94988ecaa6a782171c1b0c
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Apr 28 23:40:11 2014 +0200

    Custom word boundaries
    
    Implement the same word boundaries as in Vim.
    Almost finished.
    TODO: take the underscore as part of a natural-language word.

 gtksourceview/gtksourcebuffer.c |  218 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 218 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index d9b5886..99acf91 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -219,6 +219,10 @@ static gboolean     gtk_source_buffer_find_bracket_match_with_limit (GtkSourceBuffe
 static void     gtk_source_buffer_real_undo            (GtkSourceBuffer         *buffer);
 static void     gtk_source_buffer_real_redo            (GtkSourceBuffer         *buffer);
 
+static void     gtk_source_buffer_real_move_iter       (GtkTextBuffer           *buffer,
+                                                        GtkTextIter             *iter,
+                                                        GtkTextMoveType          move_type);
+
 static void
 gtk_source_buffer_constructed (GObject *object)
 {
@@ -256,6 +260,7 @@ gtk_source_buffer_class_init (GtkSourceBufferClass *klass)
        tb_class->insert_pixbuf       = gtk_source_buffer_real_insert_pixbuf;
        tb_class->insert_child_anchor = gtk_source_buffer_real_insert_anchor;
        tb_class->apply_tag           = gtk_source_buffer_real_apply_tag;
+       tb_class->move_iter           = gtk_source_buffer_real_move_iter;
 
        tb_class->mark_set      = gtk_source_buffer_real_mark_set;
        tb_class->mark_deleted  = gtk_source_buffer_real_mark_deleted;
@@ -1805,6 +1810,219 @@ gtk_source_buffer_real_redo (GtkSourceBuffer *buffer)
        gtk_source_undo_manager_redo (buffer->priv->undo_manager);
 }
 
+/* Go to the end of the next or current "big word". A big word is a group of
+ * non-blank chars.
+ * In other words, this function is the same as the 'E' Vim command.
+ *
+ * Examples ('|' is the iter position):
+ * "|---- abcd"   -> "----| abcd"
+ * "|  ---- abcd" -> "  ----| abcd"
+ * "--|-- abcd"   -> "----| abcd"
+ * "---- a|bcd"   -> "---- abcd|"
+ */
+static void
+iter_forward_big_word_end (GtkTextIter *iter)
+{
+       while (g_unichar_isspace (gtk_text_iter_get_char (iter)))
+       {
+               gtk_text_iter_forward_char (iter);
+       }
+
+       while (!gtk_text_iter_is_end (iter) &&
+              !g_unichar_isspace (gtk_text_iter_get_char (iter)))
+       {
+               gtk_text_iter_forward_char (iter);
+       }
+}
+
+/* Symmetric of iter_forward_big_word_end(). */
+static void
+iter_backward_big_word_start (GtkTextIter *iter)
+{
+       GtkTextIter prev;
+
+       while (!gtk_text_iter_is_start (iter))
+       {
+               prev = *iter;
+               gtk_text_iter_backward_char (&prev);
+
+               if (!g_unichar_isspace (gtk_text_iter_get_char (&prev)))
+               {
+                       break;
+               }
+
+               *iter = prev;
+       }
+
+       while (!gtk_text_iter_is_start (iter))
+       {
+               prev = *iter;
+               gtk_text_iter_backward_char (&prev);
+
+               if (g_unichar_isspace (gtk_text_iter_get_char (&prev)))
+               {
+                       break;
+               }
+
+               *iter = prev;
+       }
+}
+
+static gboolean
+iter_at_big_word_start (const GtkTextIter *iter)
+{
+       GtkTextIter prev = *iter;
+
+       if (!gtk_text_iter_backward_char (&prev))
+       {
+               return TRUE;
+       }
+
+       return (g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
+               !g_unichar_isspace (gtk_text_iter_get_char (iter)));
+}
+
+static gboolean
+iter_at_big_word_end (const GtkTextIter *iter)
+{
+       GtkTextIter prev = *iter;
+
+       if (!gtk_text_iter_backward_char (&prev))
+       {
+               return FALSE;
+       }
+
+       return (!g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
+               g_unichar_isspace (gtk_text_iter_get_char (iter)));
+}
+
+/* Go to the next word end. With a custom definition of "word".
+ * Do not take into account invisible characters, it is handled by GtkTextView.
+ */
+static void
+iter_forward_word_end (GtkTextIter *iter)
+{
+       GtkTextIter farthest = *iter;
+       GtkTextIter next_word_end = *iter;
+       GtkTextIter word_start;
+
+       /* 'farthest' is the farthest where we can go. Example:
+        * "|---- aaaa"  ->  "----| aaaa"
+        */
+       iter_forward_big_word_end (&farthest);
+
+       /* Go to the next natural-language word end. It can be farther than
+        * 'farthest'. Example:
+        * "|---- aaaa"  ->  "---- aaaa|"
+        */
+       gtk_text_iter_forward_word_end (&next_word_end);
+
+       if (gtk_text_iter_compare (&farthest, &next_word_end) < 0)
+       {
+               *iter = farthest;
+               return;
+       }
+
+       /* From the next natural-language word end, go to the previous
+        * natural-language word start.
+        *
+        * Example 1:
+        * iter:          "| abcd()"
+        * next_word_end: " abcd|()" -> the good one
+        * word_start:    " |abcd()"
+        *
+        * Example 2:
+        * iter:          "abcd|()efgk"
+        * next_word_end: "abcd()efgk|"
+        * word_start:    "abcd()|efgk" -> the good one
+        *
+        * Example 3:
+        * iter:          "ab|cd"
+        * next_word_end: "abcd|" -> the good one
+        * word_start:    "|abcd"
+        */
+       word_start = next_word_end;
+       gtk_text_iter_backward_word_start (&word_start);
+
+       /* Example 1 and 2 match the first line of the "if".
+        *
+        * Example 2 also matches the second line of the "if".
+        * In example 2, 'word_start' is at the start of a natural-language
+        * word, but what is important is that 'word_start' is also at the _end_
+        * of the word "()". With our custom word definition, "()" is also a
+        * word. If 'word_start' is at the beginning of a big word, like in
+        * example 1, 'word_start' is not at the _end_ of a (custom) word, since
+        * the previous character is a space. And a group of spaces is not taken
+        * as a word.
+        *
+        * Example 3 doesn't match the first line of the "if".
+        */
+       if (gtk_text_iter_compare (iter, &word_start) < 0 &&
+           !iter_at_big_word_start (&word_start))
+       {
+               *iter = word_start;
+       }
+       else
+       {
+               *iter = next_word_end;
+       }
+}
+
+/* Symmetric of iter_forward_word_end(). */
+static void
+iter_backward_word_start (GtkTextIter *iter)
+{
+       GtkTextIter farthest = *iter;
+       GtkTextIter prev_word_start = *iter;
+       GtkTextIter word_end;
+
+       iter_backward_big_word_start (&farthest);
+
+       if (!gtk_text_iter_backward_word_start (&prev_word_start))
+       {
+               gtk_text_iter_set_offset (&prev_word_start, 0);
+       }
+
+       if (gtk_text_iter_compare (&prev_word_start, &farthest) < 0)
+       {
+               *iter = farthest;
+               return;
+       }
+
+       word_end = prev_word_start;
+       gtk_text_iter_forward_word_end (&word_end);
+
+       if (gtk_text_iter_compare (&word_end, iter) < 0 &&
+           !iter_at_big_word_end (&word_end))
+       {
+               *iter = word_end;
+       }
+       else
+       {
+               *iter = prev_word_start;
+       }
+}
+
+static void
+gtk_source_buffer_real_move_iter (GtkTextBuffer   *buffer,
+                                 GtkTextIter     *iter,
+                                 GtkTextMoveType  move_type)
+{
+       switch (move_type)
+       {
+               case GTK_TEXT_MOVE_TYPE_FORWARD_WORD_END:
+                       iter_forward_word_end (iter);
+                       break;
+
+               case GTK_TEXT_MOVE_TYPE_BACKWARD_WORD_START:
+                       iter_backward_word_start (iter);
+                       break;
+
+               default:
+                       g_return_if_reached ();
+       }
+}
+
 /**
  * gtk_source_buffer_create_source_mark:
  * @buffer: a #GtkSourceBuffer.


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