[gtksourceview] utils: merge gtksourceview-utils into gtksourceutils



commit 8fbb537ed4baddc4245e6181c179002e63d4a042
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Sep 24 12:54:05 2017 +0200

    utils: merge gtksourceview-utils into gtksourceutils
    
    gtksourceview-utils contained the private utils functions, while
    gtksourceutils contained only the public utils functions.
    
    Merge the two *.c files together, with gtksourceutils-private.h for the
    private functions.
    
    Now all utils functions have the gtk_source_utils or _gtk_source_utils
    prefix (see the previous commits), for better consistency.
    
    Also, the gtksourceview-utils filename was not suitable for the
    GtkSource -> Gsv namespace change. gtksourceview-utils was not part of
    the GtkSourceView class.

 docs/reference/Makefile.am                         |    2 +-
 gtksourceview/Makefile.am                          |    5 +-
 gtksourceview/gtksourcecontextengine.c             |    2 +-
 gtksourceview/gtksourcelanguage.c                  |    2 +-
 gtksourceview/gtksourcelanguagemanager.c           |    2 +-
 gtksourceview/gtksourcemap.c                       |    2 +-
 gtksourceview/gtksourceregex.c                     |    2 +-
 gtksourceview/gtksourcestyleschememanager.c        |    2 +-
 ...sourceview-utils.h => gtksourceutils-private.h} |   10 +-
 gtksourceview/gtksourceutils.c                     |  318 ++++++++++++++++++-
 gtksourceview/gtksourceview-utils.c                |  338 --------------------
 11 files changed, 332 insertions(+), 353 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 7fcf9fd..8e17d6e 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -62,7 +62,7 @@ IGNORE_HFILES =                                       \
        gtksourcestyle-private.h                \
        gtksourcetypes-private.h                \
        gtksourceundomanagerdefault.h           \
-       gtksourceview-utils.h
+       gtksourceutils-private.h
 
 # Extra options to supply to gtkdoc-mkdb
 MKDB_OPTIONS = --xml-mode --output-format=xml
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 61317de..dd9a990 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -122,7 +122,7 @@ libgtksourceview_private_headers =          \
        gtksourcestyle-private.h                \
        gtksourcetypes-private.h                \
        gtksourceundomanagerdefault.h           \
-       gtksourceview-utils.h
+       gtksourceutils-private.h
 
 libgtksourceview_private_c_files =     \
        gtksourcebufferinputstream.c    \
@@ -140,8 +140,7 @@ libgtksourceview_private_c_files =  \
        gtksourcepixbufhelper.c         \
        gtksourceregex.c                \
        gtksourceundomanagerdefault.c   \
-       gtksourceview-init.c            \
-       gtksourceview-utils.c
+       gtksourceview-init.c
 
 # Split in a helper library, so the private functions can be used in unit tests.
 noinst_LTLIBRARIES = libgtksourceview-core.la
diff --git a/gtksourceview/gtksourcecontextengine.c b/gtksourceview/gtksourcecontextengine.c
index 91574d4..05b9b46 100644
--- a/gtksourceview/gtksourcecontextengine.c
+++ b/gtksourceview/gtksourcecontextengine.c
@@ -34,7 +34,7 @@
 #include "gtksourceregex.h"
 #include "gtksourcestyle.h"
 #include "gtksourcestylescheme.h"
-#include "gtksourceview-utils.h"
+#include "gtksourceutils-private.h"
 
 #undef ENABLE_DEBUG
 #undef ENABLE_PROFILE
diff --git a/gtksourceview/gtksourcelanguage.c b/gtksourceview/gtksourcelanguage.c
index 823a12c..4310545 100644
--- a/gtksourceview/gtksourcelanguage.c
+++ b/gtksourceview/gtksourcelanguage.c
@@ -34,7 +34,7 @@
 #include <glib/gi18n-lib.h>
 #include "gtksourcelanguage-private.h"
 #include "gtksourcelanguage.h"
-#include "gtksourceview-utils.h"
+#include "gtksourceutils-private.h"
 
 #ifdef G_OS_WIN32
 #include <io.h>
diff --git a/gtksourceview/gtksourcelanguagemanager.c b/gtksourceview/gtksourcelanguagemanager.c
index 6d05822..4a1eb10 100644
--- a/gtksourceview/gtksourcelanguagemanager.c
+++ b/gtksourceview/gtksourcelanguagemanager.c
@@ -29,7 +29,7 @@
 
 #include "gtksourcelanguage.h"
 #include "gtksourcelanguage-private.h"
-#include "gtksourceview-utils.h"
+#include "gtksourceutils-private.h"
 
 /**
  * SECTION:languagemanager
diff --git a/gtksourceview/gtksourcemap.c b/gtksourceview/gtksourcemap.c
index 6afb66b..35362c3 100644
--- a/gtksourceview/gtksourcemap.c
+++ b/gtksourceview/gtksourcemap.c
@@ -27,7 +27,7 @@
 #include "gtksourcecompletion.h"
 #include "gtksourcestyle-private.h"
 #include "gtksourcestylescheme.h"
-#include "gtksourceview-utils.h"
+#include "gtksourceutils-private.h"
 
 /**
  * SECTION:map
diff --git a/gtksourceview/gtksourceregex.c b/gtksourceview/gtksourceregex.c
index ed8f349..c91b533 100644
--- a/gtksourceview/gtksourceregex.c
+++ b/gtksourceview/gtksourceregex.c
@@ -27,7 +27,7 @@
 #include <string.h>
 #include <glib.h>
 #include <glib/gi18n-lib.h>
-#include "gtksourceview-utils.h"
+#include "gtksourceutils-private.h"
 
 /*
  * GRegex wrapper which adds a few features needed for syntax highlighting,
diff --git a/gtksourceview/gtksourcestyleschememanager.c b/gtksourceview/gtksourcestyleschememanager.c
index c8bfdc5..c56d09b 100644
--- a/gtksourceview/gtksourcestyleschememanager.c
+++ b/gtksourceview/gtksourcestyleschememanager.c
@@ -25,7 +25,7 @@
 #include "gtksourcestyleschememanager.h"
 #include <string.h>
 #include "gtksourcestylescheme.h"
-#include "gtksourceview-utils.h"
+#include "gtksourceutils-private.h"
 
 /**
  * SECTION:styleschememanager
diff --git a/gtksourceview/gtksourceview-utils.h b/gtksourceview/gtksourceutils-private.h
similarity index 85%
rename from gtksourceview/gtksourceview-utils.h
rename to gtksourceview/gtksourceutils-private.h
index 2811e0d..a21f0f6 100644
--- a/gtksourceview/gtksourceview-utils.h
+++ b/gtksourceview/gtksourceutils-private.h
@@ -2,7 +2,9 @@
  *
  * This file is part of GtkSourceView
  *
- * Copyright (C) 2007 - Gustavo Giráldez and Paolo Maggi
+ * Copyright (C) 2007 - Gustavo Giráldez
+ * Copyright (C) 2007 - Paolo Maggi
+ * Copyright (C) 2017 - Sébastien Wilmet <swilmet gnome org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -18,8 +20,8 @@
  * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef GTK_SOURCE_VIEW_UTILS_H
-#define GTK_SOURCE_VIEW_UTILS_H
+#ifndef GTK_SOURCE_UTILS_PRIVATE_H
+#define GTK_SOURCE_UTILS_PRIVATE_H
 
 #include <glib.h>
 #include <pango/pango.h>
@@ -47,4 +49,4 @@ gchar *               _gtk_source_utils_dgettext                      (const gchar *domain,
 
 G_END_DECLS
 
-#endif /* GTK_SOURCE_VIEW_UTILS_H */
+#endif /* GTK_SOURCE_UTILS_PRIVATE_H */
diff --git a/gtksourceview/gtksourceutils.c b/gtksourceview/gtksourceutils.c
index 5c5a6f7..6ff4084 100644
--- a/gtksourceview/gtksourceutils.c
+++ b/gtksourceview/gtksourceutils.c
@@ -3,7 +3,9 @@
  * This file is part of GtkSourceView
  *
  * Copyright (C) 2005 - Paolo Borelli
- * Copyright (C) 2013 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2007 - Gustavo Giráldez
+ * Copyright (C) 2007 - Paolo Maggi
+ * Copyright (C) 2013, 2017 - 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
@@ -32,7 +34,11 @@
 #endif
 
 #include "gtksourceutils.h"
+#include "gtksourceutils-private.h"
 #include <string.h>
+#include <errno.h>
+#include <math.h>
+#include <glib/gi18n-lib.h>
 
 /**
  * gtk_source_utils_unescape_search_text:
@@ -198,3 +204,313 @@ gtk_source_utils_escape_search_text (const gchar* text)
 
        return g_string_free (str, FALSE);
 }
+
+#define GSV_DATA_SUBDIR "gtksourceview-" GSV_API_VERSION
+
+gchar **
+_gtk_source_utils_get_default_dirs (const gchar *basename)
+{
+       const gchar * const *system_dirs;
+       GPtrArray *dirs;
+
+       dirs = g_ptr_array_new ();
+
+       /* User dir */
+       g_ptr_array_add (dirs, g_build_filename (g_get_user_data_dir (),
+                                                GSV_DATA_SUBDIR,
+                                                basename,
+                                                NULL));
+
+       /* System dirs */
+       for (system_dirs = g_get_system_data_dirs ();
+            system_dirs != NULL && *system_dirs != NULL;
+            system_dirs++)
+       {
+               g_ptr_array_add (dirs, g_build_filename (*system_dirs,
+                                                        GSV_DATA_SUBDIR,
+                                                        basename,
+                                                        NULL));
+       }
+
+       g_ptr_array_add (dirs, NULL);
+
+       return (gchar **) g_ptr_array_free (dirs, FALSE);
+}
+
+static GSList *
+build_file_listing (const gchar *item,
+                   GSList      *filenames,
+                   const gchar *suffix,
+                   gboolean     only_dirs)
+{
+       GDir *dir;
+       const gchar *name;
+
+       if (!only_dirs && g_file_test (item, G_FILE_TEST_IS_REGULAR))
+       {
+               filenames = g_slist_prepend (filenames, g_strdup(item));
+               return filenames;
+
+       }
+       dir = g_dir_open (item, 0, NULL);
+
+       if (dir == NULL)
+       {
+               return filenames;
+       }
+
+       while ((name = g_dir_read_name (dir)) != NULL)
+       {
+               gchar *full_path = g_build_filename (item, name, NULL);
+
+               if (!g_file_test (full_path, G_FILE_TEST_IS_DIR) &&
+                   g_str_has_suffix (name, suffix))
+               {
+                       filenames = g_slist_prepend (filenames, full_path);
+               }
+               else
+               {
+                       g_free (full_path);
+               }
+       }
+
+       g_dir_close (dir);
+
+       return filenames;
+}
+
+GSList *
+_gtk_source_utils_get_file_list (gchar       **path,
+                                const gchar  *suffix,
+                                gboolean      only_dirs)
+{
+       GSList *files = NULL;
+
+       for ( ; path && *path; ++path)
+       {
+               files = build_file_listing (*path, files, suffix, only_dirs);
+       }
+
+       return g_slist_reverse (files);
+}
+
+/* Wrapper around strtoull for easier use: tries
+ * to convert @str to a number and return -1 if it is not.
+ * Used to check if references in subpattern contexts
+ * (e.g. \%{1@start} or \%{blah@start}) are named or numbers.
+ */
+gint
+_gtk_source_utils_string_to_int (const gchar *str)
+{
+       guint64 number;
+       gchar *end_str;
+
+       if (str == NULL || *str == '\0')
+       {
+               return -1;
+       }
+
+       errno = 0;
+       number = g_ascii_strtoull (str, &end_str, 10);
+
+       if (errno != 0 || number > G_MAXINT || *end_str != '\0')
+       {
+               return -1;
+       }
+
+       return number;
+}
+
+#define FONT_FAMILY  "font-family"
+#define FONT_VARIANT "font-variant"
+#define FONT_STRETCH "font-stretch"
+#define FONT_WEIGHT  "font-weight"
+#define FONT_SIZE    "font-size"
+
+gchar *
+_gtk_source_utils_pango_font_description_to_css (const PangoFontDescription *font_desc)
+{
+       PangoFontMask mask;
+       GString *str;
+
+#define ADD_KEYVAL(key,fmt) \
+       g_string_append(str,key":"fmt";")
+#define ADD_KEYVAL_PRINTF(key,fmt,...) \
+       g_string_append_printf(str,key":"fmt";", __VA_ARGS__)
+
+       g_return_val_if_fail (font_desc, NULL);
+
+       str = g_string_new (NULL);
+
+       mask = pango_font_description_get_set_fields (font_desc);
+
+       if ((mask & PANGO_FONT_MASK_FAMILY) != 0)
+       {
+               const gchar *family;
+
+               family = pango_font_description_get_family (font_desc);
+               ADD_KEYVAL_PRINTF (FONT_FAMILY, "\"%s\"", family);
+       }
+
+       if ((mask & PANGO_FONT_MASK_STYLE) != 0)
+       {
+               PangoVariant variant;
+
+               variant = pango_font_description_get_variant (font_desc);
+
+               switch (variant)
+               {
+                       case PANGO_VARIANT_NORMAL:
+                               ADD_KEYVAL (FONT_VARIANT, "normal");
+                               break;
+
+                       case PANGO_VARIANT_SMALL_CAPS:
+                               ADD_KEYVAL (FONT_VARIANT, "small-caps");
+                               break;
+
+                       default:
+                               break;
+               }
+       }
+
+       if ((mask & PANGO_FONT_MASK_WEIGHT))
+       {
+               gint weight;
+
+               weight = pango_font_description_get_weight (font_desc);
+
+               /*
+                * WORKAROUND:
+                *
+                * font-weight with numbers does not appear to be working as expected
+                * right now. So for the common (bold/normal), let's just use the string
+                * and let gtk warn for the other values, which shouldn't really be
+                * used for this.
+                */
+
+               switch (weight)
+               {
+                       case PANGO_WEIGHT_SEMILIGHT:
+                       /*
+                        * 350 is not actually a valid css font-weight, so we will just round
+                        * up to 400.
+                        */
+                       case PANGO_WEIGHT_NORMAL:
+                               ADD_KEYVAL (FONT_WEIGHT, "normal");
+                               break;
+
+                       case PANGO_WEIGHT_BOLD:
+                               ADD_KEYVAL (FONT_WEIGHT, "bold");
+                               break;
+
+                       case PANGO_WEIGHT_THIN:
+                       case PANGO_WEIGHT_ULTRALIGHT:
+                       case PANGO_WEIGHT_LIGHT:
+                       case PANGO_WEIGHT_BOOK:
+                       case PANGO_WEIGHT_MEDIUM:
+                       case PANGO_WEIGHT_SEMIBOLD:
+                       case PANGO_WEIGHT_ULTRABOLD:
+                       case PANGO_WEIGHT_HEAVY:
+                       case PANGO_WEIGHT_ULTRAHEAVY:
+                       default:
+                               /* round to nearest hundred */
+                               weight = round (weight / 100.0) * 100;
+                               ADD_KEYVAL_PRINTF ("font-weight", "%d", weight);
+                               break;
+               }
+       }
+
+       if ((mask & PANGO_FONT_MASK_STRETCH))
+       {
+               switch (pango_font_description_get_stretch (font_desc))
+               {
+                       case PANGO_STRETCH_ULTRA_CONDENSED:
+                               ADD_KEYVAL (FONT_STRETCH, "untra-condensed");
+                               break;
+
+                       case PANGO_STRETCH_EXTRA_CONDENSED:
+                               ADD_KEYVAL (FONT_STRETCH, "extra-condensed");
+                               break;
+
+                       case PANGO_STRETCH_CONDENSED:
+                               ADD_KEYVAL (FONT_STRETCH, "condensed");
+                               break;
+
+                       case PANGO_STRETCH_SEMI_CONDENSED:
+                               ADD_KEYVAL (FONT_STRETCH, "semi-condensed");
+                               break;
+
+                       case PANGO_STRETCH_NORMAL:
+                               ADD_KEYVAL (FONT_STRETCH, "normal");
+                               break;
+
+                       case PANGO_STRETCH_SEMI_EXPANDED:
+                               ADD_KEYVAL (FONT_STRETCH, "semi-expanded");
+                               break;
+
+                       case PANGO_STRETCH_EXPANDED:
+                               ADD_KEYVAL (FONT_STRETCH, "expanded");
+                               break;
+
+                       case PANGO_STRETCH_EXTRA_EXPANDED:
+                               ADD_KEYVAL (FONT_STRETCH, "extra-expanded");
+                               break;
+
+                       case PANGO_STRETCH_ULTRA_EXPANDED:
+                               ADD_KEYVAL (FONT_STRETCH, "untra-expanded");
+                               break;
+
+                       default:
+                               break;
+               }
+       }
+
+       if ((mask & PANGO_FONT_MASK_SIZE))
+       {
+               gint font_size;
+
+               font_size = pango_font_description_get_size (font_desc) / PANGO_SCALE;
+               ADD_KEYVAL_PRINTF ("font-size", "%dpt", font_size);
+       }
+
+       return g_string_free (str, FALSE);
+
+#undef ADD_KEYVAL
+#undef ADD_KEYVAL_PRINTF
+}
+
+/*
+ * _gtk_source_utils_dgettext:
+ *
+ * Try to translate string from given domain. It returns
+ * duplicated string which must be freed with g_free().
+ */
+gchar *
+_gtk_source_utils_dgettext (const gchar *domain,
+                           const gchar *string)
+{
+       const gchar *translated;
+       gchar *tmp;
+
+       g_return_val_if_fail (string != NULL, NULL);
+
+       if (domain == NULL)
+       {
+               return g_strdup (_(string));
+       }
+
+       translated = dgettext (domain, string);
+
+       if (g_strcmp0 (translated, string) == 0)
+       {
+               return g_strdup (_(string));
+       }
+
+       if (g_utf8_validate (translated, -1, NULL))
+       {
+               return g_strdup (translated);
+       }
+
+       tmp = g_locale_to_utf8 (translated, -1, NULL, NULL, NULL);
+       return tmp != NULL ? tmp : g_strdup (string);
+}


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