[gtksourceview/wip/custom-word-boundaries: 10/10] Custom word boundaries for cursor movements



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

    Custom word boundaries for cursor movements
    
    Implement the same word boundaries as in Vim.
    Ctrl+Left is like the 'b' Vim command. And Ctrl+Right like 'e'.
    Selection with double click works with these custom word boundaries too.
    
    Overrides only word boundaries for cursor movements.

 gtksourceview/gtksourcebuffer.c |  312 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 312 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 61b0b54..374de7a 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -222,6 +222,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)
 {
@@ -259,6 +263,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;
@@ -1827,6 +1832,313 @@ 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)));
+}
+
+/* Extends the definition of a natural-language word used by GtkTextIter/pango.
+ * The underscore is added to the possible characters of a natural-language
+ * word.
+ * A possible improvement is to specify the characters to add to the
+ * natural-language word definition, instead of hardcoding the underscore.
+ */
+static void
+iter_forward_natural_word_end (GtkTextIter *iter)
+{
+       GtkTextBoundaryType type;
+
+       type = gtk_text_iter_get_boundary_type (iter);
+       gtk_text_iter_set_boundary_type (iter, GTK_TEXT_BOUNDARY_TYPE_NATURAL_LANGUAGE);
+
+       gtk_text_iter_forward_word_end (iter);
+
+       while (TRUE)
+       {
+               if (gtk_text_iter_get_char (iter) == '_')
+               {
+                       gtk_text_iter_forward_char (iter);
+               }
+               else if (gtk_text_iter_starts_word (iter))
+               {
+                       gtk_text_iter_forward_word_end (iter);
+               }
+               else
+               {
+                       break;
+               }
+       }
+
+       gtk_text_iter_set_boundary_type (iter, type);
+}
+
+/* Symmetric of iter_forward_natural_word_end(). */
+static void
+iter_backward_natural_word_start (GtkTextIter *iter)
+{
+       GtkTextBoundaryType type;
+
+       type = gtk_text_iter_get_boundary_type (iter);
+       gtk_text_iter_set_boundary_type (iter, GTK_TEXT_BOUNDARY_TYPE_NATURAL_LANGUAGE);
+
+       if (!gtk_text_iter_backward_word_start (iter))
+       {
+               gtk_text_iter_set_offset (iter, 0);
+       }
+
+       while (!gtk_text_iter_is_start (iter))
+       {
+               GtkTextIter prev = *iter;
+               gtk_text_iter_backward_char (&prev);
+
+               if (gtk_text_iter_get_char (&prev) == '_')
+               {
+                       *iter = prev;
+               }
+               else if (gtk_text_iter_ends_word (iter))
+               {
+                       gtk_text_iter_backward_word_start (iter);
+               }
+               else
+               {
+                       break;
+               }
+       }
+
+       gtk_text_iter_set_boundary_type (iter, type);
+}
+
+/* Go to the next word end. With a custom definition of "word".
+ * Do not take into account invisible characters, it is handled by GtkTextView.
+ *
+ * It is normally the same word boundaries as in Vim. Ctrl+Left is like the 'b'
+ * Vim command (go to the previous word start), and Ctrl+Right is like 'e' (go
+ * to the next word end).
+ * With the custom word definition, a word can be:
+ * - a natural-language word as definied by GtkTextIter/pango (the is_word_start
+ *   and is_word_end PangoLogAttr attributes), plus the underscore.
+ * - a group of contiguous non-blank characters.
+ *
+ * With ctrl+arrow, the blank characters are skipped. For selecting a word with
+ * double click, the implementation in GtkTextView works fine for selecting
+ * blank characters too, if the double click is in blank characters.
+ */
+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|"
+        */
+       iter_forward_natural_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;
+       iter_backward_natural_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);
+       iter_backward_natural_word_start (&prev_word_start);
+
+       if (gtk_text_iter_compare (&prev_word_start, &farthest) < 0)
+       {
+               *iter = farthest;
+               return;
+       }
+
+       word_end = prev_word_start;
+       iter_forward_natural_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 (gtk_text_iter_get_boundary_type (iter))
+       {
+               case GTK_TEXT_BOUNDARY_TYPE_NATURAL_LANGUAGE:
+                       GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->move_iter (buffer,
+                                                                                          iter,
+                                                                                          move_type);
+                       return;
+
+               case GTK_TEXT_BOUNDARY_TYPE_FOR_CURSOR_MOVEMENTS:
+                       break;
+
+               default:
+                       g_return_if_reached ();
+       }
+
+       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]