[gtksourceview/wip/custom-word-boundaries-2] GtkSourceIter, the come back
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/custom-word-boundaries-2] GtkSourceIter, the come back
- Date: Sat, 20 Dec 2014 14:16:03 +0000 (UTC)
commit d103543362e16f22a851500c3179cb77691d5d67
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Dec 20 15:01:07 2014 +0100
GtkSourceIter, the come back
Add GtkTextIter functions to implement custom word boundaries. It'll be
used in gtksourceview.c. It's better to have a separate file with those
functions so the file can easily be copied in applications, for example
gnome-builder (to implement word movements for the Vim mode). It maybe
makes sense to publish those functions as public, but as a first step
let's keep that private.
docs/reference/Makefile.am | 1 +
gtksourceview/Makefile.am | 2 +
gtksourceview/gtksourceiter.c | 301 +++++++++++++++++++++++++++++++++++++++++
gtksourceview/gtksourceiter.h | 35 +++++
4 files changed, 339 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 6af4fd9..0bdc48a 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -38,6 +38,7 @@ IGNORE_HFILES = \
gtksourcegutterrendererlines.h \
gtksourcegutterrenderermarks.h \
gtksourcegutterrenderer-private.h \
+ gtksourceiter.h \
gtksourcelanguage-private.h \
gtksourcemarkssequence.h \
gtksourcepixbufhelper.h \
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 5dd626f..8df429f 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -65,6 +65,7 @@ libgtksourceview_private_headers = \
gtksourcegutterrendererlines.h \
gtksourcegutterrenderermarks.h \
gtksourcegutterrenderer-private.h \
+ gtksourceiter.h \
gtksourcelanguage-private.h \
gtksourcemarkssequence.h \
gtksourcepixbufhelper.h \
@@ -85,6 +86,7 @@ libgtksourceview_private_c_files = \
gtksourceengine.c \
gtksourcegutterrendererlines.c \
gtksourcegutterrenderermarks.c \
+ gtksourceiter.c \
gtksourcelanguage-parser-1.c \
gtksourcelanguage-parser-2.c \
gtksourcemarkssequence.c \
diff --git a/gtksourceview/gtksourceiter.c b/gtksourceview/gtksourceiter.c
new file mode 100644
index 0000000..49c641a
--- /dev/null
+++ b/gtksourceview/gtksourceiter.c
@@ -0,0 +1,301 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* gtksourceiter.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/* Private GtkTextIter functions. Contains forward/backward functions for word
+ * movements, with custom word boundaries that are used for word selection
+ * (double-click) and cursor movements (Ctrl+left, Ctrl+right, etc).
+ * The initial idea was to use those word boundaries directly in GTK+, for all
+ * text widgets. But in the end only the GtkTextView::extend-selection signal
+ * has been added to be able to customize the boundaries for double- and
+ * triple-click (the ::move-cursor and ::delete-from-cursor signals were already
+ * present to customize boundaries for cursor movements). The GTK+ developers
+ * didn't want to change the word boundaries for text widgets. More information:
+ * https://mail.gnome.org/archives/gtk-devel-list/2014-September/msg00019.html
+ * https://bugzilla.gnome.org/show_bug.cgi?id=111503
+ */
+
+#include "gtksourceiter.h"
+
+/* 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.
+ */
+void
+_gtk_source_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(). */
+void
+_gtk_source_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);
+
+ /* Case 1 */
+ if (gtk_text_iter_compare (iter, &word_end) <= 0)
+ {
+ *iter = prev_word_start;
+ }
+
+ /* Case 2 */
+ else if (iter_at_full_word_end (&word_end))
+ {
+ *iter = prev_word_start;
+ }
+
+ /* Case 3 */
+ else
+ {
+ *iter = word_end;
+ }
+}
diff --git a/gtksourceview/gtksourceiter.h b/gtksourceview/gtksourceiter.h
new file mode 100644
index 0000000..e975925
--- /dev/null
+++ b/gtksourceview/gtksourceiter.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* gtksourceiter.h
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __GTK_SOURCE_ITER_H__
+#define __GTK_SOURCE_ITER_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void _gtk_source_iter_forward_word_end (GtkTextIter *iter);
+
+void _gtk_source_iter_backward_word_start (GtkTextIter *iter);
+
+G_END_DECLS
+
+#endif /* __GTK_SOURCE_ITER_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]