gedit r6796 - in trunk: . plugins/modelines



Author: pborelli
Date: Fri Jan  2 20:15:55 2009
New Revision: 6796
URL: http://svn.gnome.org/viewvc/gedit?rev=6796&view=rev

Log:
2009-01-02  Paolo Borelli  <pborelli katamail com>

	* plugins/modelines/gedit-modeline-plugin.c:
	* plugins/modelines/modeline-parser.c:
	Add support for parsing the highlighting mode.
	Bug #379663, patch by Sergej Chodarev.



Added:
   trunk/plugins/modelines/language-mappings
Modified:
   trunk/ChangeLog
   trunk/plugins/modelines/Makefile.am
   trunk/plugins/modelines/gedit-modeline-plugin.c
   trunk/plugins/modelines/modeline-parser.c
   trunk/plugins/modelines/modeline-parser.h

Modified: trunk/plugins/modelines/Makefile.am
==============================================================================
--- trunk/plugins/modelines/Makefile.am	(original)
+++ trunk/plugins/modelines/Makefile.am	Fri Jan  2 20:15:55 2009
@@ -7,6 +7,10 @@
 	$(WARN_CFLAGS)					\
 	$(DISABLE_DEPRECATED_CFLAGS)
 
+modelinesdir = $(datadir)/gedit-2/plugins/modelines
+modelines_DATA = \
+	language-mappings
+
 plugin_LTLIBRARIES = libmodelines.la
 
 libmodelines_la_SOURCES = \

Modified: trunk/plugins/modelines/gedit-modeline-plugin.c
==============================================================================
--- trunk/plugins/modelines/gedit-modeline-plugin.c	(original)
+++ trunk/plugins/modelines/gedit-modeline-plugin.c	Fri Jan  2 20:15:55 2009
@@ -45,9 +45,10 @@
 	gulong document_saved_handler_id;
 } DocumentData;
 
-static void gedit_modeline_plugin_activate (GeditPlugin *plugin, GeditWindow *window);
-static void gedit_modeline_plugin_deactivate (GeditPlugin *plugin, GeditWindow *window);
-static void gedit_modeline_plugin_finalize (GObject *object);
+static void	gedit_modeline_plugin_activate (GeditPlugin *plugin, GeditWindow *window);
+static void	gedit_modeline_plugin_deactivate (GeditPlugin *plugin, GeditWindow *window);
+static GObject	*gedit_modeline_plugin_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_param);
+static void	gedit_modeline_plugin_finalize (GObject *object);
 
 GEDIT_PLUGIN_REGISTER_TYPE(GeditModelinePlugin, gedit_modeline_plugin)
 
@@ -69,12 +70,34 @@
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 	GeditPluginClass *plugin_class = GEDIT_PLUGIN_CLASS (klass);
 
+	object_class->constructor = gedit_modeline_plugin_constructor;
 	object_class->finalize = gedit_modeline_plugin_finalize;
 
 	plugin_class->activate = gedit_modeline_plugin_activate;
 	plugin_class->deactivate = gedit_modeline_plugin_deactivate;
 }
 
+static GObject *
+gedit_modeline_plugin_constructor (GType                  type,
+				   guint                  n_construct_properties,
+				   GObjectConstructParam *construct_param)
+{
+	GObject *object;
+	gchar *data_dir;
+
+	object = G_OBJECT_CLASS (gedit_modeline_plugin_parent_class)->constructor (type,
+										   n_construct_properties,
+										   construct_param);
+
+	data_dir = gedit_plugin_get_data_dir (GEDIT_PLUGIN (object));
+
+	modeline_parser_init (data_dir);
+
+	g_free (data_dir);
+
+	return object;
+}
+
 static void
 gedit_modeline_plugin_init (GeditModelinePlugin *plugin)
 {
@@ -86,6 +109,8 @@
 {
 	gedit_debug_message (DEBUG_PLUGINS, "GeditModelinePlugin finalizing");
 
+	modeline_parser_shutdown ();
+
 	G_OBJECT_CLASS (gedit_modeline_plugin_parent_class)->finalize (object);
 }
 
@@ -94,7 +119,7 @@
 			     const GError  *error,
 			     GtkSourceView *view)
 {
-	apply_modeline (view);
+	modeline_parser_apply_modeline (view);
 }
 
 static void
@@ -153,12 +178,12 @@
 	GList *l;
 
 	gedit_debug (DEBUG_PLUGINS);
-	
+
 	views = gedit_window_get_views (window);
 	for (l = views; l != NULL; l = l->next)
 	{
 		connect_handlers (GEDIT_VIEW (l->data));
-		apply_modeline (GTK_SOURCE_VIEW (l->data));
+		modeline_parser_apply_modeline (GTK_SOURCE_VIEW (l->data));
 	}
 	g_list_free (views);
 

Added: trunk/plugins/modelines/language-mappings
==============================================================================
--- (empty file)
+++ trunk/plugins/modelines/language-mappings	Fri Jan  2 20:15:55 2009
@@ -0,0 +1,14 @@
+[vim]
+cs=c-sharp
+docbk=docbook
+javascript=js
+lhaskell=haskell-literate
+spec=rpmspec
+tex=latex
+xhtml=html
+
+[emacs]
+c++=cpp
+
+[kate]
+

Modified: trunk/plugins/modelines/modeline-parser.c
==============================================================================
--- trunk/plugins/modelines/modeline-parser.c	(original)
+++ trunk/plugins/modelines/modeline-parser.c	Fri Jan  2 20:15:55 2009
@@ -22,12 +22,25 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <gedit/gedit-language-manager.h>
 #include <gedit/gedit-prefs-manager.h>
 #include <gedit/gedit-debug.h>
 #include "modeline-parser.h"
 
+#define MODELINES_LANGUAGE_MAPPINGS_FILE "language-mappings"
+
+/* base dir to lookup configuration files */
+static gchar *modelines_data_dir;
+
+/* Mappings: language name -> Gedit language ID */
+static GHashTable *vim_languages;
+static GHashTable *emacs_languages;
+static GHashTable *kate_languages;
+
 typedef struct _ModelineOptions
 {
+	gchar		*language_id;
+
 	/* these options are similar to the GtkSourceView properties of the
 	 * same names.
 	 */
@@ -39,6 +52,135 @@
 	guint		right_margin_position;
 } ModelineOptions;
 
+void
+modeline_parser_init (const gchar *data_dir)
+{
+	modelines_data_dir = g_strdup (data_dir);
+}
+
+void
+modeline_parser_shutdown ()
+{
+	g_hash_table_destroy (vim_languages);
+	g_hash_table_destroy (emacs_languages);
+	g_hash_table_destroy (kate_languages);
+
+	g_free (modelines_data_dir);
+}
+
+static GHashTable *
+load_language_mappings_group (GKeyFile *key_file, const gchar *group)
+{
+	GHashTable *table;
+	gchar **keys;
+	gsize length = 0;
+	int i;
+
+	table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+	keys = g_key_file_get_keys (key_file, group, &length, NULL);
+
+	gedit_debug_message (DEBUG_PLUGINS,
+			     "%" G_GSIZE_FORMAT " mappings in group %s",
+			     length, group);
+
+	for (i = 0; i < length; i++)
+	{
+		gchar *name = keys[i];
+		gchar *id = g_key_file_get_string (key_file, group, name, NULL);
+		g_hash_table_insert (table, name, id);
+	}
+	g_free (keys);
+
+	return table;
+}
+
+/* lazy loading of language mappings */
+static void
+load_language_mappings (void)
+{
+	gchar *fname;
+	GKeyFile *mappings;
+	GError *error = NULL;
+
+	fname = g_build_filename (modelines_data_dir,
+				  MODELINES_LANGUAGE_MAPPINGS_FILE,
+				  NULL);
+
+	mappings = g_key_file_new ();
+
+	if (g_key_file_load_from_file (mappings, fname, 0, &error))
+	{
+		gedit_debug_message (DEBUG_PLUGINS,
+				     "Loaded language mappings from %s",
+				     fname);
+
+		vim_languages = load_language_mappings_group (mappings, "vim");
+		emacs_languages	= load_language_mappings_group (mappings, "emacs");
+		kate_languages = load_language_mappings_group (mappings, "kate");
+	}
+	else
+	{
+		gedit_debug_message (DEBUG_PLUGINS,
+				     "Failed to loaded language mappings from %s: %s",
+				     fname, error->message);
+
+		g_error_free (error);
+	}
+
+	g_key_file_free (mappings);
+	g_free (fname);
+}
+
+static gchar *
+get_language_id (const gchar *language_name, GHashTable *mapping)
+{
+	gchar *name;
+	gchar *language_id;
+
+	name = g_ascii_strdown (language_name, -1);
+
+	language_id = g_hash_table_lookup (mapping, name);
+
+	if (language_id != NULL)
+	{
+		g_free (name);
+		return g_strdup (language_id);
+	}
+	else
+	{
+		/* by default assume that the gtksourcevuew id is the same */
+		return name;
+	}
+}
+
+static gchar *
+get_language_id_vim (const gchar *language_name)
+{
+	if (vim_languages == NULL)
+		load_language_mappings ();
+
+	return get_language_id (language_name, vim_languages);
+}
+
+static gchar *
+get_language_id_emacs (const gchar *language_name)
+{
+	if (emacs_languages == NULL)
+		load_language_mappings ();
+
+	return get_language_id (language_name, emacs_languages);
+}
+
+static gchar *
+get_language_id_kate (const gchar *language_name)
+{
+	if (kate_languages == NULL)
+		load_language_mappings ();
+
+	return get_language_id (language_name, kate_languages);
+}
+
 static gboolean
 skip_whitespaces (gchar **s)
 {
@@ -108,7 +250,12 @@
 			}
 		}
 
-		if (strcmp (key->str, "et") == 0 ||
+		if (strcmp (key->str, "ft") == 0 ||
+		    strcmp (key->str, "filetype") == 0)
+		{
+			options->language_id = get_language_id_vim (value->str);
+		}
+		else if (strcmp (key->str, "et") == 0 ||
 		    strcmp (key->str, "expandtab") == 0)
 		{
 			options->insert_spaces = !neg;
@@ -151,7 +298,7 @@
  * See http://www.delorie.com/gnu/docs/emacs/emacs_486.html
  */
 static gchar *
-parse_emacs_modeline (gchar         *s,
+parse_emacs_modeline (gchar           *s,
 		      ModelineOptions *options)
 {
 	guint intval;
@@ -197,7 +344,11 @@
 				     "Emacs modeline bit: %s = %s",
 				     key->str, value->str);
 
-		if (strcmp (key->str, "tab-width") == 0)
+		if (strcmp (key->str, "Mode") == 0)
+		{
+			options->language_id = get_language_id_emacs (value->str);
+		}
+		else if (strcmp (key->str, "tab-width") == 0)
 		{
 			intval = atoi (value->str);
 			if (intval) options->tab_width = intval;
@@ -233,7 +384,7 @@
  * See http://wiki.kate-editor.org/index.php/Modelines
  */
 static gchar *
-parse_kate_modeline (gchar *s,
+parse_kate_modeline (gchar           *s,
 		     ModelineOptions *options)
 {
 	guint intval;
@@ -274,7 +425,12 @@
 				     "Kate modeline bit: %s = %s",
 				     key->str, value->str);
 
-		if (strcmp (key->str, "tab-width") == 0)
+		if (strcmp (key->str, "hl") == 0 ||
+		    strcmp (key->str, "syntax") == 0)
+		{
+			options->language_id = get_language_id_kate (value->str);
+		}
+		else if (strcmp (key->str, "tab-width") == 0)
 		{
 			intval = atoi (value->str);
 			if (intval) options->tab_width = intval;
@@ -354,7 +510,7 @@
 }
 
 void
-apply_modeline (GtkSourceView *view)
+modeline_parser_apply_modeline (GtkSourceView *view)
 {
 	ModelineOptions options;
 	GtkTextBuffer *buffer;
@@ -363,6 +519,7 @@
 	gint line_count;
 
 	/* Default values for modeline options */
+	options.language_id = NULL;
 	options.insert_spaces = gedit_prefs_manager_get_insert_spaces ();
 	options.tab_width = gedit_prefs_manager_get_tabs_size ();
 	options.indent_width = -1; /* not set by default */
@@ -416,6 +573,25 @@
 		line_number ++;
 	}
 
+	/* Try to set language */
+	if (options.language_id != NULL)
+	{
+		GtkSourceLanguageManager *manager;
+		GtkSourceLanguage *language;
+
+		manager = gedit_get_language_manager ();
+		language = gtk_source_language_manager_get_language
+				(manager, options.language_id);
+
+		if (language != NULL)
+		{
+			gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (buffer),
+							language);
+		}
+
+		g_free (options.language_id);
+	}
+
 	/* Apply the options we got from modelines */
 	gtk_source_view_set_insert_spaces_instead_of_tabs
 						(view, options.insert_spaces);

Modified: trunk/plugins/modelines/modeline-parser.h
==============================================================================
--- trunk/plugins/modelines/modeline-parser.h	(original)
+++ trunk/plugins/modelines/modeline-parser.h	Fri Jan  2 20:15:55 2009
@@ -27,7 +27,9 @@
 
 G_BEGIN_DECLS
 
-void	apply_modeline		(GtkSourceView *view);
+void	modeline_parser_init		(const gchar *data_dir);
+void	modeline_parser_shutdown	(void);
+void	modeline_parser_apply_modeline	(GtkSourceView *view);
 
 G_END_DECLS
 



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