gtksourceview r2010 - in trunk: . docs/reference docs/reference/tmpl gtksourceview tests



Author: muntyan
Date: Wed Aug  6 00:57:00 2008
New Revision: 2010
URL: http://svn.gnome.org/viewvc/gtksourceview?rev=2010&view=rev

Log:
2008-08-05  Yevgen Muntyan  <muntyan tamu edu>

	New API: gtk_source_language_manager_guess_language(), bug #546060.

	* gtksourceview/gtksourcelanguagemanager.h:
	* gtksourceview/gtksourcelanguagemanager.c: (pick_lang_for_filename),
	(pick_lang_for_mime_type_pass), (pick_lang_for_mime_type),
	(grok_win32_content_type), (gtk_source_language_manager_guess_language):
	new functions.

	* tests/test-widget.c: use it here.

	* tests/Makefile.am:
	* configure.ac: removed gnome-vfs and added gio dependency.

	* docs/reference/gtksourceview-2.0-sections.txt:
	* docs/reference/tmpl/languagemanager.sgml:
	* docs/reference/tmpl/language.sgml:
	* docs/reference/tmpl/view.sgml: updated.



Modified:
   trunk/ChangeLog
   trunk/configure.ac
   trunk/docs/reference/gtksourceview-2.0-sections.txt
   trunk/docs/reference/tmpl/language.sgml
   trunk/docs/reference/tmpl/languagemanager.sgml
   trunk/docs/reference/tmpl/view.sgml
   trunk/gtksourceview/gtksourcelanguagemanager.c
   trunk/gtksourceview/gtksourcelanguagemanager.h
   trunk/tests/Makefile.am
   trunk/tests/test-widget.c

Modified: trunk/configure.ac
==============================================================================
--- trunk/configure.ac	(original)
+++ trunk/configure.ac	Wed Aug  6 00:57:00 2008
@@ -63,9 +63,8 @@
 PKG_CHECK_MODULES(DEP, [
 	gtk+-2.0 >= $GTK_REQUIRED_VERSION
 	libxml-2.0 >= $LIBXML_REQUIRED_VERSION
+	gio-2.0
 ])
-AC_SUBST(DEP_CFLAGS)
-AC_SUBST(DEP_LIBS)
 
 # Do we build optional tests?
 AC_ARG_ENABLE(build_tests,
@@ -80,15 +79,11 @@
 
 msg_tests=no
 AM_CONDITIONAL(BUILD_TESTS, false)
-AM_CONDITIONAL(USE_GNOME_VFS, false)
 
 if test x$build_tests = xtrue; then
 	AM_CONDITIONAL(BUILD_TESTS, true)
 	AC_DEFINE(BUILD_TESTS, [], [build test suite])
 	msg_tests=yes
-	PKG_CHECK_MODULES(TESTS, gnome-vfs-2.0,
-			  AM_CONDITIONAL(USE_GNOME_VFS, true),
-			  [PKG_CHECK_MODULES(TESTS, gthread-2.0)])
         # test-widget uses this to find lang files and gtksourcebuffer.c
         ABS_TOP_SRCDIR=`cd $srcdir && pwd`
         AC_SUBST(ABS_TOP_SRCDIR)

Modified: trunk/docs/reference/gtksourceview-2.0-sections.txt
==============================================================================
--- trunk/docs/reference/gtksourceview-2.0-sections.txt	(original)
+++ trunk/docs/reference/gtksourceview-2.0-sections.txt	Wed Aug  6 00:57:00 2008
@@ -59,10 +59,12 @@
 gtk_source_view_get_insert_spaces_instead_of_tabs
 gtk_source_view_set_smart_home_end
 gtk_source_view_get_smart_home_end
-gtk_source_view_set_mark_category_pixbuf
-gtk_source_view_get_mark_category_pixbuf
 gtk_source_view_set_mark_category_priority
 gtk_source_view_get_mark_category_priority
+gtk_source_view_set_mark_category_pixbuf
+gtk_source_view_get_mark_category_pixbuf
+gtk_source_view_get_mark_category_background
+gtk_source_view_set_mark_category_background
 gtk_source_view_set_highlight_current_line
 gtk_source_view_get_highlight_current_line
 gtk_source_view_set_show_line_marks
@@ -123,6 +125,7 @@
 gtk_source_language_manager_get_search_path
 gtk_source_language_manager_get_language_ids
 gtk_source_language_manager_get_language
+gtk_source_language_manager_guess_language
 <SUBSECTION Standard>
 GtkSourceLanguageManagerClass
 GTK_IS_SOURCE_LANGUAGE_MANAGER
@@ -146,6 +149,7 @@
 gtk_source_mark_prev
 <SUBSECTION Standard>
 GtkSourceMarkClass
+GtkSourceMarkPrivate
 GTK_IS_SOURCE_MARK
 GTK_IS_SOURCE_MARK_CLASS
 GTK_SOURCE_MARK
@@ -163,6 +167,11 @@
 gtk_source_style_copy
 <SUBSECTION Standard>
 GTK_TYPE_SOURCE_STYLE
+GTK_IS_SOURCE_STYLE
+GTK_IS_SOURCE_STYLE_CLASS
+GTK_SOURCE_STYLE
+GTK_SOURCE_STYLE_CLASS
+GTK_SOURCE_STYLE_GET_CLASS
 gtk_source_style_get_type
 </SECTION>
 
@@ -276,5 +285,6 @@
 GTK_IS_SOURCE_PRINT_COMPOSITOR_CLASS
 GTK_SOURCE_PRINT_COMPOSITOR_GET_CLASS
 GtkSourcePrintCompositorPrivate
+GtkSourcePrintCompositorClass
 </SECTION>
 

Modified: trunk/docs/reference/tmpl/language.sgml
==============================================================================
--- trunk/docs/reference/tmpl/language.sgml	(original)
+++ trunk/docs/reference/tmpl/language.sgml	Wed Aug  6 00:57:00 2008
@@ -2,11 +2,14 @@
 GtkSourceLanguage
 
 <!-- ##### SECTION Short_Description ##### -->
-
+Syntax highlighting language
 
 <!-- ##### SECTION Long_Description ##### -->
 <para>
-
+#GtkSourceLanguage encapsulates syntax and highlighting styles for a
+particular language. Use #GtkSourceLanguageManager to obtain a
+#GtkSourceLanguage instance, and gtk_source_buffer_set_language()
+to apply it to a #GtkSourceBuffer.
 </para>
 
 <!-- ##### SECTION See_Also ##### -->
@@ -49,8 +52,8 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_name ##### -->
@@ -58,8 +61,8 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_section ##### -->
@@ -67,8 +70,8 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_hidden ##### -->
@@ -76,8 +79,8 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_metadata ##### -->
@@ -85,9 +88,9 @@
 
 </para>
 
- language: 
- name: 
- Returns: 
+ language:
+ name:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_mime_types ##### -->
@@ -95,8 +98,8 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_globs ##### -->
@@ -104,8 +107,8 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_style_name ##### -->
@@ -113,9 +116,9 @@
 
 </para>
 
- language: 
- style_id: 
- Returns: 
+ language:
+ style_id:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_get_style_ids ##### -->
@@ -123,7 +126,7 @@
 
 </para>
 
- language: 
- Returns: 
+ language:
+ Returns:
 
 

Modified: trunk/docs/reference/tmpl/languagemanager.sgml
==============================================================================
--- trunk/docs/reference/tmpl/languagemanager.sgml	(original)
+++ trunk/docs/reference/tmpl/languagemanager.sgml	Wed Aug  6 00:57:00 2008
@@ -2,11 +2,16 @@
 GtkSourceLanguageManager
 
 <!-- ##### SECTION Short_Description ##### -->
-
+Object which provides access to #GtkSourceLanguage<!-- -->s
 
 <!-- ##### SECTION Long_Description ##### -->
 <para>
-
+#GtkSourceLanguageManager is an object which processes language description
+files and creates and stores #GtkSourceLanguage objects, and provides API to
+access them.
+</para><para>Use gtk_source_language_manager_get_default() to retrieve the default
+instance of #GtkSourceLanguageManager, and gtk_source_language_manager_guess_language()
+to get a #GtkSourceLanguage for given file name and content type.
 </para>
 
 <!-- ##### SECTION See_Also ##### -->
@@ -39,7 +44,7 @@
 
 </para>
 
- Returns: 
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_manager_get_default ##### -->
@@ -47,7 +52,7 @@
 
 </para>
 
- Returns: 
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_manager_set_search_path ##### -->
@@ -55,8 +60,8 @@
 
 </para>
 
- lm: 
- dirs: 
+ lm:
+ dirs:
 
 
 <!-- ##### FUNCTION gtk_source_language_manager_get_search_path ##### -->
@@ -64,8 +69,8 @@
 
 </para>
 
- lm: 
- Returns: 
+ lm:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_manager_get_language_ids ##### -->
@@ -73,8 +78,8 @@
 
 </para>
 
- lm: 
- Returns: 
+ lm:
+ Returns:
 
 
 <!-- ##### FUNCTION gtk_source_language_manager_get_language ##### -->
@@ -82,8 +87,19 @@
 
 </para>
 
- lm: 
- id: 
- Returns: 
+ lm:
+ id:
+ Returns:
+
+
+<!-- ##### FUNCTION gtk_source_language_manager_guess_language ##### -->
+<para>
+
+</para>
+
+ lm:
+ filename:
+ content_type:
+ Returns:
 
 

Modified: trunk/docs/reference/tmpl/view.sgml
==============================================================================
--- trunk/docs/reference/tmpl/view.sgml	(original)
+++ trunk/docs/reference/tmpl/view.sgml	Wed Aug  6 00:57:00 2008
@@ -224,6 +224,26 @@
 @Returns: 
 
 
+<!-- ##### FUNCTION gtk_source_view_set_mark_category_priority ##### -->
+<para>
+
+</para>
+
+ view: 
+ category: 
+ priority: 
+
+
+<!-- ##### FUNCTION gtk_source_view_get_mark_category_priority ##### -->
+<para>
+
+</para>
+
+ view: 
+ category: 
+ Returns: 
+
+
 <!-- ##### FUNCTION gtk_source_view_set_mark_category_pixbuf ##### -->
 <para>
 
@@ -244,24 +264,25 @@
 @Returns: 
 
 
-<!-- ##### FUNCTION gtk_source_view_set_mark_category_priority ##### -->
+<!-- ##### FUNCTION gtk_source_view_get_mark_category_background ##### -->
 <para>
 
 </para>
 
 @view: 
 @category: 
- priority: 
+ dest: 
+ Returns: 
 
 
-<!-- ##### FUNCTION gtk_source_view_get_mark_category_priority ##### -->
+<!-- ##### FUNCTION gtk_source_view_set_mark_category_background ##### -->
 <para>
 
 </para>
 
 @view: 
 @category: 
- Returns: 
+ color: 
 
 
 <!-- ##### FUNCTION gtk_source_view_set_highlight_current_line ##### -->

Modified: trunk/gtksourceview/gtksourcelanguagemanager.c
==============================================================================
--- trunk/gtksourceview/gtksourcelanguagemanager.c	(original)
+++ trunk/gtksourceview/gtksourcelanguagemanager.c	Wed Aug  6 00:57:00 2008
@@ -27,6 +27,7 @@
 #include "gtksourcelanguage-private.h"
 #include "gtksourcelanguage.h"
 #include "gtksourceview-utils.h"
+#include <gio/gio.h>
 
 #define RNG_SCHEMA_FILE		"language2.rng"
 #define LANGUAGE_DIR		"language-specs"
@@ -408,3 +409,210 @@
 	return g_hash_table_lookup (lm->priv->language_ids, id);
 }
 
+static GtkSourceLanguage *
+pick_lang_for_filename (GtkSourceLanguageManager *lm,
+			const gchar              *filename)
+{
+	char *filename_utf8;
+	const gchar* const * p;
+
+	/* Use g_filename_display_name() instead of g_filename_to_utf8() because
+	 * g_filename_display_name() doesn't fail and replaces non-convertible
+	 * characters to unicode substitution symbol. */
+	filename_utf8 = g_filename_display_name (filename);
+
+	for (p = gtk_source_language_manager_get_language_ids (lm);
+	     p != NULL && *p != NULL;
+	     p++)
+	{
+		GtkSourceLanguage *lang;
+		gchar **globs, **gptr;
+
+		lang = gtk_source_language_manager_get_language (lm, *p);
+		globs = gtk_source_language_get_globs (lang);
+
+		for (gptr = globs; gptr != NULL && *gptr != NULL; gptr++)
+		{
+			/* FIXME g_pattern_match is wrong: there are no '[...]'
+			 * character ranges and '*' and '?' can not be escaped
+			 * to include them literally in a pattern.  */
+			if (g_pattern_match_simple (*gptr, filename_utf8))
+			{
+				g_strfreev (globs);
+				g_free (filename_utf8);
+				return lang;
+			}
+		}
+
+		g_strfreev (globs);
+	}
+
+	g_free (filename_utf8);
+	return NULL;
+}
+
+static GtkSourceLanguage *
+pick_lang_for_mime_type_pass (GtkSourceLanguageManager *lm,
+			      const char               *mime_type,
+			      gboolean                  exact_match)
+{
+	const gchar* const * id_ptr;
+
+	for (id_ptr = gtk_source_language_manager_get_language_ids (lm);
+	     id_ptr != NULL && *id_ptr != NULL;
+	     id_ptr++)
+	{
+		GtkSourceLanguage *lang;
+		gchar **mime_types, **mptr;
+
+		lang = gtk_source_language_manager_get_language (lm, *id_ptr);
+		mime_types = gtk_source_language_get_mime_types (lang);
+
+		for (mptr = mime_types; mptr != NULL && *mptr != NULL; mptr++)
+		{
+			gboolean matches;
+
+			if (exact_match)
+				matches = strcmp (mime_type, *mptr) == 0;
+			else
+				matches = g_content_type_is_a (mime_type, *mptr);
+
+			if (matches)
+			{
+				g_strfreev (mime_types);
+				return lang;
+			}
+		}
+
+		g_strfreev (mime_types);
+	}
+
+	return NULL;
+}
+
+static GtkSourceLanguage *
+pick_lang_for_mime_type (GtkSourceLanguageManager *lm,
+			 const char               *mime_type)
+{
+	GtkSourceLanguage *lang;
+	lang = pick_lang_for_mime_type_pass (lm, mime_type, TRUE);
+	if (!lang)
+		lang = pick_lang_for_mime_type_pass (lm, mime_type, FALSE);
+	return lang;
+}
+
+#ifdef G_OS_WIN32
+static void
+grok_win32_content_type (const gchar  *content_type,
+			 gchar       **alt_filename,
+			 gchar       **mime_type)
+{
+	*alt_filename = NULL;
+	*mime_type = NULL;
+
+	/* If it contains slash, then it's probably a mime type.
+	 * Otherwise treat is an extension. */
+	if (strchr (content_type, '/') != NULL)
+		*mime_type = g_strdup (content_type);
+	else
+		*alt_filename = g_strjoin ("filename", content_type, NULL);
+}
+#endif
+
+/**
+ * gtk_source_language_manager_guess_language:
+ * @lm: a #GtkSourceLanguageManager.
+ * @filename: a filename in Glib filename encoding, or %NULL.
+ * @content_type: a content type (as in GIO API), or %NULL.
+ *
+ * Picks a #GtkSourceLanguage for given file name and content type,
+ * according to the information in lang files. Either @filename or
+ * @content_type may be %NULL. This function can be used as follows:
+ *
+ * <informalexample><programlisting>
+ *   GtkSourceLanguage *lang;
+ *   lang = gtk_source_language_manager_guess_language (filename, NULL);
+ *   gtk_source_buffer_set_language (buffer, lang);
+ * </programlisting></informalexample>
+ *
+ * or
+ *
+ * <informalexample><programlisting>
+ *   GtkSourceLanguage *lang = NULL;
+ *   gboolean result_uncertain;
+ *   gchar *content_type;
+ *
+ *   content_type = g_content_type_guess (filename, NULL, 0, &result_uncertain);
+ *   if (result_uncertain)
+ *     {
+ *       g_free (content_type);
+ *       content_type = NULL;
+ *     }
+ *
+ *   lang = gtk_source_language_manager_guess_language (manager, filename, content_type);
+ *   gtk_source_buffer_set_language (buffer, lang);
+ *
+ *   g_free (content_type);
+ * </programlisting></informalexample>
+ *
+ * etc. Use gtk_source_language_get_mime_types() and gtk_source_language_get_globs()
+ * if you need full control over file -> language mapping.
+ *
+ * Returns: a #GtkSourceLanguage, or %NULL if there is no suitable language
+ * for given @filename and/or @content_type. Return value is owned by @lm
+ * and should not be freed.
+ *
+ * Since: 2.4
+ */
+GtkSourceLanguage *
+gtk_source_language_manager_guess_language (GtkSourceLanguageManager *lm,
+					    const gchar		     *filename,
+					    const gchar		     *content_type)
+{
+	GtkSourceLanguage *lang = NULL;
+
+	g_return_val_if_fail (GTK_IS_SOURCE_LANGUAGE_MANAGER (lm), NULL);
+	g_return_val_if_fail (filename != NULL || content_type != NULL, NULL);
+	g_return_val_if_fail ((filename == NULL || *filename != 0) &&
+			      (content_type == NULL || *content_type != 0), NULL);
+
+	ensure_languages (lm);
+
+	/* TODO
+	> Maybe the logic should be:
+	>  - match mime
+	>  - match glob
+	>  - if just one matches use it
+	>  - if they both match and the corresponding mime inside the lang files are one
+	> the anchestor of the other pick the more strict lang
+	>  - if they both match and the corresponding mime inside the lang files are
+	> unrelated, pick the glob one
+	*/
+
+	if (filename != NULL)
+		lang = pick_lang_for_filename (lm, filename);
+
+	if (lang == NULL && content_type != NULL)
+	{
+#ifndef G_OS_WIN32
+		/* On Unix "content type" is mime type */
+		lang = pick_lang_for_mime_type (lm, content_type);
+#else
+		/* On Windows "content type" is an extension, but user may pass a mime type too */
+		gchar *mime_type;
+		gchar *alt_filename;
+
+		grok_win32_content_type (content_type, &alt_filename, &mime_type);
+
+		if (alt_filename != NULL)
+			lang = pick_lang_for_filename (lm, alt_filename);
+		if (lang == NULL && mime_type != NULL)
+			lang = pick_lang_for_mime_type (lm, mime_type);
+
+		g_free (mime_type);
+		g_free (alt_filename);
+#endif
+	}
+
+	return lang;
+}

Modified: trunk/gtksourceview/gtksourcelanguagemanager.h
==============================================================================
--- trunk/gtksourceview/gtksourcelanguagemanager.h	(original)
+++ trunk/gtksourceview/gtksourcelanguagemanager.h	Wed Aug  6 00:57:00 2008
@@ -74,6 +74,10 @@
 GtkSourceLanguage	 *gtk_source_language_manager_get_language		(GtkSourceLanguageManager *lm,
 										 const gchar              *id);
 
+GtkSourceLanguage	 *gtk_source_language_manager_guess_language		(GtkSourceLanguageManager *lm,
+										 const gchar		  *filename,
+										 const gchar		  *content_type);
+
 
 
 G_END_DECLS

Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am	(original)
+++ trunk/tests/Makefile.am	Wed Aug  6 00:57:00 2008
@@ -1,7 +1,3 @@
-if USE_GNOME_VFS
-GVFS_FLAG = -DUSE_GNOME_VFS
-endif
-
 INCLUDES = 				\
 	-DDATADIR=\""$(datadir)"\" 	\
 	-DG_LOG_DOMAIN=\"GtkSourceView\"\
@@ -10,7 +6,6 @@
 	$(DISABLE_DEPRECATED_CFLAGS)	\
 	$(WARN_CFLAGS) 			\
 	$(DEP_CFLAGS)			\
-	$(GVFS_FLAG)			\
 	$(TESTS_CFLAGS)
 
 # testing executables

Modified: trunk/tests/test-widget.c
==============================================================================
--- trunk/tests/test-widget.c	(original)
+++ trunk/tests/test-widget.c	Wed Aug  6 00:57:00 2008
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <gtk/gtk.h>
+#include <gio/gio.h>
 #include <gtksourceview/gtksourceview.h>
 #include <gtksourceview/gtksourcelanguage.h>
 #include <gtksourceview/gtksourcelanguagemanager.h>
@@ -38,11 +39,6 @@
 #ifdef TEST_XML_MEM
 #include <libxml/xmlreader.h>
 #endif
-#ifdef USE_GNOME_VFS
-#include <libgnomevfs/gnome-vfs-init.h>
-#include <libgnomevfs/gnome-vfs-mime-utils.h>
-#include <libgnomevfs/gnome-vfs-utils.h>
-#endif
 
 /* Global list of open windows */
 
@@ -311,129 +307,45 @@
 	gtk_source_buffer_remove_source_marks (buffer, &s, &e, NULL);
 }
 
-/* Note this is wrong for several reasons, e.g. g_pattern_match is broken
- * for glob matching. */
 static GtkSourceLanguage *
-get_language_for_filename (const gchar *filename)
+get_language_for_file (GtkTextBuffer *buffer, const gchar *filename)
 {
-	const gchar * const *languages;
-	gchar *filename_utf8;
 	GtkSourceLanguageManager *manager;
+	GtkSourceLanguage *language;
+	GtkTextIter start, end;
+	gchar *text;
+	gchar *content_type;
+	gboolean result_uncertain;
 
-	filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
-	g_return_val_if_fail (filename_utf8 != NULL, NULL);
-
-	manager = gtk_source_language_manager_get_default ();
-	languages = gtk_source_language_manager_get_language_ids (manager);
+	gtk_text_buffer_get_start_iter (buffer, &start);
+	if (gtk_text_buffer_get_char_count (buffer) < 1024)
+		gtk_text_buffer_get_end_iter (buffer, &end);
+	else
+		gtk_text_buffer_get_iter_at_offset (buffer, &end, 1024);
+	text = gtk_text_buffer_get_slice (buffer, &start, &end, TRUE);
 
-	while (*languages != NULL)
+	content_type = g_content_type_guess (filename,
+					     (guchar*) text,
+					     strlen (text),
+					     &result_uncertain);
+	if (result_uncertain)
 	{
-		GtkSourceLanguage *lang;
-		gchar **globs, **p;
-
-		lang = gtk_source_language_manager_get_language (manager,
-								 *languages);
-		g_return_val_if_fail (GTK_IS_SOURCE_LANGUAGE (lang), NULL);
-		++languages;
-
-		globs = gtk_source_language_get_globs (lang);
-		if (globs == NULL)
-			continue;
-
-		for (p = globs; *p != NULL; p++)
-		{
-			if (g_pattern_match_simple (*p, filename_utf8))
-			{
-				g_strfreev (globs);
-				g_free (filename_utf8);
-
-				return lang;
-			}
-		}
-
-		g_strfreev (globs);
+		g_free (content_type);
+		content_type = NULL;
 	}
 
-	g_free (filename_utf8);
-	return NULL;
-}
-
-/* Note this is wrong, because it ignores mime parent types and subtypes.
- * It's fine to use in a simplish program like this, but is unacceptable
- * in a serious text editor. */
-static GtkSourceLanguage *
-get_language_for_mime_type (const gchar *mime)
-{
-	const gchar * const *languages;
-	GtkSourceLanguageManager *manager;
-
 	manager = gtk_source_language_manager_get_default ();
-	languages = gtk_source_language_manager_get_language_ids (manager);
-
-	while (*languages != NULL)
-	{
-		GtkSourceLanguage *lang;
-		gchar **mimetypes, **p;
-
-		lang = gtk_source_language_manager_get_language (manager,
-								 *languages);
-		g_return_val_if_fail (GTK_IS_SOURCE_LANGUAGE (lang), NULL);
-		++languages;
-
-		mimetypes = gtk_source_language_get_mime_types (lang);
-
-		if (mimetypes == NULL)
-			continue;
-
-		for (p = mimetypes; *p != NULL; p++)
-		{
-			if (strcmp (*p, mime) == 0)
-			{
-				g_strfreev (mimetypes);
-				return lang;
-			}
-		}
-
-		g_strfreev (mimetypes);
-	}
-
-	return NULL;
-}
-
-static GtkSourceLanguage *
-get_language_for_file (const gchar *filename)
-{
-	GtkSourceLanguage *language = NULL;
-
-#if defined(USE_GNOME_VFS)
-	gchar *mime_type;
-	gchar *uri;
-
-	/* I hate this! */
-	if (g_path_is_absolute (filename))
-	{
-		uri = gnome_vfs_get_uri_from_local_path (filename);
-	}
-	else
-	{
-		gchar *curdir, *path;
-		curdir = g_get_current_dir ();
-		path = g_strconcat (curdir, "/", filename, NULL);
-		g_free (curdir);
-		uri = gnome_vfs_get_uri_from_local_path (path);
-		g_free (path);
-	}
-
-	if ((mime_type = gnome_vfs_get_mime_type (uri)))
-		language = get_language_for_mime_type (mime_type);
-
-	g_free (mime_type);
-	g_free (uri);
-#endif
-
-	if (!language)
-		language = get_language_for_filename (filename);
+	language = gtk_source_language_manager_guess_language (manager,
+							       filename,
+							       content_type);
+
+	g_message ("Detected '%s' mime type for file %s, chose language %s",
+		   content_type ? content_type : "(null)",
+		   filename,
+		   language ? gtk_source_language_get_id (language) : "(none)");
 
+	g_free (content_type);
+	g_free (text);
 	return language;
 }
 
@@ -476,7 +388,7 @@
 	}
 
 	if (!language)
-		language = get_language_for_file (filename);
+		language = get_language_for_file (buffer, filename);
 
 	g_free (text);
 	return language;
@@ -1533,8 +1445,6 @@
 	  { NULL }
 	};
 
-	g_thread_init (NULL);
-
 #ifdef TEST_XML_MEM
 	init_mem_stuff ();
 #endif
@@ -1546,10 +1456,6 @@
 
 // 	gdk_window_set_debug_updates (TRUE);
 
-#ifdef USE_GNOME_VFS
-	gnome_vfs_init ();
-#endif
-
 	/* we do not use defaults so we don't need to install the library */
 	lang_dirs = use_default_paths ? NULL : builtin_lang_dirs;
 	lm = gtk_source_language_manager_get_default ();
@@ -1619,9 +1525,5 @@
 
 	g_free (style_scheme_id);
 
-#ifdef USE_GNOME_VFS
-	gnome_vfs_shutdown ();
-#endif
-
 	return 0;
 }



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