[gtksourceview/wip/custom-word-boundaries: 2/2] Buffer: custom word boundaries for cursor movements
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/custom-word-boundaries: 2/2] Buffer: custom word boundaries for cursor movements
- Date: Sat, 18 Oct 2014 12:08:00 +0000 (UTC)
commit ce3de06414068d4bbb98c2cf1ddd7248d5356437
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon Apr 28 23:40:11 2014 +0200
Buffer: 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 | 301 +++++++++++++++++++++++++++++++++++++++
1 files changed, 301 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 61b0b54..6b5248f 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,302 @@ 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 "full word". A full 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_full_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_full_word_end(). */
+static void
+iter_backward_full_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_full_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_full_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 Pango. The
+ * underscore is added to the possible characters of a natural-language word.
+ */
+static void
+iter_forward_extra_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_extra_natural_word_end(). */
+static void
+iter_backward_extra_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".
+ *
+ * It is normally the same word boundaries as in Vim. This function is the same
+ * as the 'e' command.
+ *
+ * With the custom word definition, a word can be:
+ * - a natural-language word as definied by Pango, plus the underscore. The
+ * underscore is added because it is often used in programming languages.
+ * - a group of contiguous non-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 position that this function can return. Example:
+ * "|---- aaaa" -> "----| aaaa"
+ */
+ iter_forward_full_word_end (&farthest);
+
+ /* Go to the next extra-natural word end. It can be farther than 'farthest'.
+ * Example:
+ * "|---- aaaa" -> "---- aaaa|"
+ */
+ iter_forward_extra_natural_word_end (&next_word_end);
+
+ if (gtk_text_iter_compare (&farthest, &next_word_end) < 0)
+ {
+ *iter = farthest;
+ return;
+ }
+
+ /* From 'next_word_end', go to the previous extra-natural word start.
+ *
+ * Example 1:
+ * iter: "ab|cd"
+ * next_word_end: "abcd|" -> the good one
+ * word_start: "|abcd"
+ *
+ * Example 2:
+ * iter: "| abcd()"
+ * next_word_end: " abcd|()" -> the good one
+ * word_start: " |abcd()"
+ *
+ * Example 3:
+ * iter: "abcd|()efgk"
+ * next_word_end: "abcd()efgk|"
+ * word_start: "abcd()|efgk" -> the good one, at the end of the word "()".
+ */
+ word_start = next_word_end;
+ iter_backward_extra_natural_word_start (&word_start);
+
+ /* Example 1 */
+ if (gtk_text_iter_compare (&word_start, iter) <= 0)
+ {
+ *iter = next_word_end;
+ }
+
+ /* Example 2 */
+ else if (iter_at_full_word_start (&word_start))
+ {
+ *iter = next_word_end;
+ }
+
+ /* Example 3 */
+ else
+ {
+ *iter = word_start;
+ }
+}
+
+/* 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_full_word_start (&farthest);
+ iter_backward_extra_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_extra_natural_word_end (&word_end);
+
+ if (gtk_text_iter_compare (iter, &word_end) <= 0)
+ {
+ *iter = prev_word_start;
+ }
+ else if (iter_at_full_word_end (&word_end))
+ {
+ *iter = prev_word_start;
+ }
+ else
+ {
+ *iter = word_end;
+ }
+}
+
+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]