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



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

    Custom word boundaries
    
    Try to implement the same word boundaries as in Vim.
    Just a test, not finished.

 gtksourceview/gtksourcebuffer.c |   95 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index d9b5886..422e5c3 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -219,6 +219,15 @@ 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 gboolean         gtk_source_buffer_real_at_boundary     (GtkTextBuffer           *buffer,
+                                                        const GtkTextIter       *iter,
+                                                        GtkTextBoundaryType      boundary_type);
+
+static void     gtk_source_buffer_real_move_iter       (GtkTextBuffer           *buffer,
+                                                        GtkTextIter             *iter,
+                                                        GtkTextBoundaryType      boundary_type,
+                                                        gboolean                 forward);
+
 static void
 gtk_source_buffer_constructed (GObject *object)
 {
@@ -256,6 +265,8 @@ 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->at_boundary         = gtk_source_buffer_real_at_boundary;
+       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 +1816,90 @@ 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);
+       }
+}
+
+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 void
+iter_forward_word_boundary (GtkTextIter *iter)
+{
+       GtkTextIter farthest = *iter;
+       GtkTextIter next_word_end = *iter;
+       GtkTextIter word_start;
+
+       iter_forward_big_word_end (&farthest);
+       gtk_text_iter_forward_word_end (&next_word_end);
+
+       if (gtk_text_iter_compare (&farthest, &next_word_end) < 0)
+       {
+               *iter = farthest;
+               return;
+       }
+
+       word_start = next_word_end;
+       gtk_text_iter_backward_word_start (&word_start);
+
+       if (gtk_text_iter_compare (iter, &word_start) < 0 &&
+           !iter_at_big_word_start (&word_start))
+       {
+               *iter = word_start;
+       }
+       else
+       {
+               *iter = next_word_end;
+       }
+}
+
+static gboolean
+gtk_source_buffer_real_at_boundary (GtkTextBuffer       *buffer,
+                                   const GtkTextIter   *iter,
+                                   GtkTextBoundaryType  boundary_type)
+{
+}
+
+static void
+gtk_source_buffer_real_move_iter (GtkTextBuffer       *buffer,
+                                 GtkTextIter         *iter,
+                                 GtkTextBoundaryType  boundary_type,
+                                 gboolean             forward)
+{
+}
+
 /**
  * gtk_source_buffer_create_source_mark:
  * @buffer: a #GtkSourceBuffer.


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