[Xfce4-commits] <mousepad:matt/gtksourceview> Add initial filetype/language support and guess on file open.

Matthew Brush noreply at xfce.org
Thu Oct 6 23:18:12 CEST 2011


Updating branch refs/heads/matt/gtksourceview
         to 82b6e834ddeea4be8bf901307e6e093bde24631c (commit)
       from cc401c48985b9f06c855cac39175ddcbe0efe091 (commit)

commit 82b6e834ddeea4be8bf901307e6e093bde24631c
Author: Matthew Brush <mbrush at codebrainz.ca>
Date:   Mon Oct 3 21:48:03 2011 -0700

    Add initial filetype/language support and guess on file open.
    
    Still need to add UI elements to let user select language to use.

 mousepad/mousepad-file.c    |   82 +++++++++++++++++++++++++++++++++++++++++++
 mousepad/mousepad-file.h    |   12 ++++++
 mousepad/mousepad-private.h |    2 +
 3 files changed, 96 insertions(+), 0 deletions(-)

diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c
index b45c980..2e4a86c 100644
--- a/mousepad/mousepad-file.c
+++ b/mousepad/mousepad-file.c
@@ -393,6 +393,85 @@ mousepad_file_get_line_ending (MousepadFile *file)
 
 
 
+void 
+mousepad_file_set_language (MousepadFile      *file,
+                            GtkSourceLanguage *language)
+{
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  mousepad_return_if_fail (GTK_IS_SOURCE_BUFFER (file->buffer));
+  
+  gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (file->buffer), language);
+}
+
+
+
+GtkSourceLanguage *
+mousepad_file_get_language (MousepadFile *file)
+{
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
+  mousepad_return_val_if_fail (GTK_IS_SOURCE_BUFFER (file->buffer), NULL);
+  
+  return gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (file->buffer));
+}
+
+
+
+void
+mousepad_file_set_language_id (MousepadFile *file,
+                               const gchar  *language_id)
+{
+  GtkSourceLanguage *lang;
+  
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  mousepad_return_if_fail (GTK_IS_SOURCE_BUFFER (file->buffer));
+  
+  if (G_UNLIKELY (language_id == NULL))
+    {
+      gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (file->buffer), NULL);
+      return;
+    }
+  
+  lang = gtk_source_language_manager_get_language (gtk_source_language_manager_get_default (), language_id);
+  mousepad_file_set_language (file, lang);
+}
+
+
+
+const gchar *
+mousepad_file_get_language_id (MousepadFile *file)
+{
+  GtkSourceLanguage *lang;
+  
+  lang = mousepad_file_get_language (file);
+  return (lang != NULL) ? gtk_source_language_get_id (lang) : NULL;
+}
+
+
+
+GtkSourceLanguage *
+mousepad_file_guess_language (MousepadFile *file)
+{
+  gchar             *content_type;
+  gboolean           result_uncertain;
+  GtkSourceLanguage *language = NULL;
+
+  content_type = g_content_type_guess (file->filename, NULL, 0, &result_uncertain);
+  if (result_uncertain)
+    {
+      g_free (content_type);
+      content_type = NULL;
+    }
+
+  language = gtk_source_language_manager_guess_language (gtk_source_language_manager_get_default (), 
+                                                         file->filename, 
+                                                         content_type);
+  g_free (content_type);
+  
+  return language;
+}
+
+
+
 gint
 mousepad_file_open (MousepadFile  *file,
                     const gchar   *template_filename,
@@ -604,6 +683,9 @@ mousepad_file_open (MousepadFile  *file,
       g_mapped_file_free (mapped_file);
 #endif
 
+      /* guess and set the file's filetype/language */
+      mousepad_file_set_language (file, mousepad_file_guess_language (file));
+
       /* this does not count as a modified buffer */
       gtk_text_buffer_set_modified (file->buffer, FALSE);
     }
diff --git a/mousepad/mousepad-file.h b/mousepad/mousepad-file.h
index 18c5b6e..e9a8be3 100644
--- a/mousepad/mousepad-file.h
+++ b/mousepad/mousepad-file.h
@@ -68,6 +68,18 @@ void                mousepad_file_set_line_ending          (MousepadFile
 
 MousepadLineEnding  mousepad_file_get_line_ending          (MousepadFile        *file);
 
+void                mousepad_file_set_language             (MousepadFile        *file,
+                                                            GtkSourceLanguage   *language);
+
+GtkSourceLanguage  *mousepad_file_get_language             (MousepadFile        *file);
+
+void                mousepad_file_set_language_id          (MousepadFile        *file,
+                                                            const gchar         *language_id);
+
+const gchar        *mousepad_file_get_language_id          (MousepadFile        *file);
+
+GtkSourceLanguage  *mousepad_file_guess_language           (MousepadFile        *file);
+
 gint                mousepad_file_open                     (MousepadFile        *file,
                                                             const gchar         *template_filename,
                                                             GError             **error);
diff --git a/mousepad/mousepad-private.h b/mousepad/mousepad-private.h
index 85fae3c..921767b 100644
--- a/mousepad/mousepad-private.h
+++ b/mousepad/mousepad-private.h
@@ -24,6 +24,8 @@
 #include <gtksourceview/gtksourceview.h>
 #include <gtksourceview/gtksourcestylescheme.h>
 #include <gtksourceview/gtksourcestyleschememanager.h>
+#include <gtksourceview/gtksourcelanguage.h>
+#include <gtksourceview/gtksourcelanguagemanager.h>
 
 G_BEGIN_DECLS
 


More information about the Xfce4-commits mailing list