[PATCH] *sourceview rediffed and integrated feedback



Hello here is the rediffed gtksourceview patch

I want to push that sooner rather than later, so
I'd like a bit of testing, at least to know that it
does not break existing working setups.

Any comments ?

-- 
Vincent Legoll
Index: filediff.py
===================================================================
--- filediff.py	(révision 1311)
+++ filediff.py	(copie de travail)
@@ -33,30 +33,8 @@
 import paths
 import cairo
 
-sourceview_available = 0
+from sourceviewer import srcviewer
 
-for sourceview in "gtksourceview sourceview".split():
-    try:
-        gsv = __import__(sourceview)
-        sourceview_available = 1
-        break
-    except ImportError:
-        pass
-
-if sourceview_available:
-    def set_highlighting_enabled(buf, fname, enabled):
-        if enabled:
-            import gnomevfs
-            mime_type = gnomevfs.get_mime_type( 
-                    gnomevfs.make_uri_from_input(os.path.abspath(fname)) )
-            man = gsv.SourceLanguagesManager()
-            gsl = man.get_language_from_mime_type( mime_type )
-            if gsl:
-                buf.set_language(gsl)
-            else:
-                enabled = False
-        buf.set_highlight(enabled)
-
 gdk = gtk.gdk
 
 ################################################################################
@@ -80,21 +58,17 @@
         """Start up an filediff with num_panes empty contents.
         """
         melddoc.MeldDoc.__init__(self, prefs)
-        override = {}
-        if sourceview_available:
-            override["GtkTextView"] = gsv.SourceView
-            override["GtkTextBuffer"] = gsv.SourceBuffer
-        gnomeglade.Component.__init__(self, paths.share_dir("glade2/filediff.glade"), "filediff", override)
+        gnomeglade.Component.__init__(self, paths.share_dir("glade2/filediff.glade"), "filediff", srcviewer.override)
         self.map_widgets_into_lists( ["textview", "fileentry", "diffmap", "scrolledwindow", "linkmap", "statusimage"] )
         for d in self.diffmap: d.hide()
         self._update_regexes()
         self.warned_bad_comparison = False
-        if sourceview_available:
+        if srcviewer:
             for v in self.textview:
-                v.set_buffer( gsv.SourceBuffer() )
+                v.set_buffer(srcviewer.GtkTextBuffer())
                 v.set_show_line_numbers(self.prefs.show_line_numbers)
                 v.set_insert_spaces_instead_of_tabs(self.prefs.spaces_instead_of_tabs)
-                v.set_tabs_width(self.prefs.tab_size)
+                srcviewer.set_tab_width(v, self.prefs.tab_size)
         self.keymask = 0
         self.load_font()
         self.deleted_lines_pending = -1
@@ -296,19 +270,19 @@
                 tabs.set_tab(i, pango.TAB_LEFT, i*value*self.pango_char_width)
             for i in range(3):
                 self.textview[i].set_tabs(tabs)
-            if sourceview_available:
+            if srcviewer:
                 for t in self.textview:
-                    t.set_tabs_width(value)
+                    srcviewer.set_tab_width(t, value)
         elif key == "use_custom_font" or key == "custom_font":
             self.load_font()
         elif key == "show_line_numbers":
-            if sourceview_available:
+            if srcviewer:
                 for t in self.textview:
                     t.set_show_line_numbers( value )
         elif key == "use_syntax_highlighting":
-            if sourceview_available:
+            if srcviewer:
                 for i in range(self.num_panes):
-                    set_highlighting_enabled(
+                    srcviewer.set_highlighting_enabled(
                         self.textbuffer[i],
                         self.bufferdata[i].filename,
                         self.prefs.use_syntax_highlighting )
@@ -318,7 +292,7 @@
             for t in self.textview:
                 t.set_wrap_mode(self.prefs.edit_wrap_lines)
         elif key == "spaces_instead_of_tabs":
-            if sourceview_available:
+            if sourceview:
                 for t in self.textview:
                     t.set_insert_spaces_instead_of_tabs(value)
 
@@ -615,10 +589,10 @@
         lenseq = [len(d) for d in self.linediffer.diffs]
         self.scheduler.add_task( self._update_highlighting( (0,lenseq[0]), (0,lenseq[1]) ).next )
         self._connect_buffer_handlers()
-        if sourceview_available:
+        if srcviewer:
             for i in range(len(files)):
                 if files[i]:
-                    set_highlighting_enabled(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
+                    srcviewer.set_highlighting_enabled(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
         yield 0
 
     def _update_highlighting(self, range0, range1):
@@ -796,11 +770,7 @@
 
     def make_patch(self, *extra):
         fontdesc = pango.FontDescription(self.prefs.get_current_font())
-        override = {}
-        if sourceview_available:
-            override["GtkTextView"] = gsv.SourceView
-            override["GtkTextBuffer"] = gsv.SourceBuffer
-        dialog = gnomeglade.Component( paths.share_dir("glade2/filediff.glade"), "patchdialog", override)
+        dialog = gnomeglade.Component(paths.share_dir("glade2/filediff.glade"), "patchdialog", srcviewer.override)
         dialog.widget.set_transient_for( self.widget.get_toplevel() )
         texts = [b.get_text(*b.get_bounds()).split("\n") for b in self.textbuffer]
         texts[0] = [l+"\n" for l in texts[0]]
@@ -808,20 +778,19 @@
         names = [self._get_pane_label(i) for i in range(2)]
         prefix = os.path.commonprefix( names )
         names = [n[prefix.rfind("/") + 1:] for n in names]
-        if sourceview_available:
-            dialog.textview.set_buffer( gsv.SourceBuffer() )
+        if srcviewer:
+            dialog.textview.set_buffer(srcviewer.GtkTextBuffer())
         dialog.textview.modify_font(fontdesc)
         buf = dialog.textview.get_buffer()
         lines = []
         for line in difflib.unified_diff(texts[0], texts[1], names[0], names[1]):
             buf.insert( buf.get_end_iter(), line )
             lines.append(line)
-        if sourceview_available:
-            man = gsv.SourceLanguagesManager()
-            gsl = man.get_language_from_mime_type("text/x-diff")
+        if srcviewer:
+            gsl = srcviewer.get_language_from_mime_type("text/x-diff")
             if gsl:
                 buf.set_language(gsl)
-                buf.set_highlight(True)
+                srcviewer.set_highlight(buf, True)
         result = dialog.widget.run()
         dialog.widget.destroy()
         if result >= 0:
Index: meldapp.py
===================================================================
--- meldapp.py	(révision 1311)
+++ meldapp.py	(copie de travail)
@@ -37,17 +37,8 @@
 import dirdiff
 import task
 
-# optional
-sourceview_available = 0
+from sourceviewer import srcviewer
 
-for sourceview in "gtksourceview sourceview".split():
-    try:
-        __import__(sourceview)
-        sourceview_available = 1
-        break
-    except ImportError:
-        pass
-
 version = "1.2.1-svn"
 
 # magic developer switch, changes some behaviour
@@ -211,7 +202,7 @@
             self.radiobutton_gnome_font.set_active(1)
         self.fontpicker.set_font_name( self.prefs.custom_font )
         self.spinbutton_tabsize.set_value( self.prefs.tab_size )
-        if sourceview_available:
+        if srcviewer:
             self.checkbutton_spaces_instead_of_tabs.set_active( self.prefs.spaces_instead_of_tabs )
             self.checkbutton_show_line_numbers.set_active( self.prefs.show_line_numbers )
             self.checkbutton_use_syntax_highlighting.set_active( self.prefs.use_syntax_highlighting )
Index: sourceviewer.py
===================================================================
--- sourceviewer.py	(révision 0)
+++ sourceviewer.py	(révision 0)
@@ -0,0 +1,140 @@
+### Copyright (C) 2009 Vincent Legoll <vincent legoll gmail com>
+
+### This program is free software; you can redistribute it and/or modify
+### it under the terms of the GNU General Public License as published by
+### the Free Software Foundation; either version 2 of the License, or
+### (at your option) any later version.
+
+### This program 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 General Public License for more details.
+
+### You should have received a copy of the GNU General Public License
+### along with this program; if not, write to the Free Software
+### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+'''
+Created on Mar 4, 2009
+
+ author: Vincent Legoll <vincent legoll gmail com>
+
+Abstraction from sourceview version API incompatibilities
+'''
+
+import os
+
+class _srcviewer():
+    srcviewer_module = None
+    
+    def __init__(self):
+        self.gsv = __import__(self.srcviewer_module)
+        self.version_check()
+        self.glm = None
+        self.overrides()
+        self.override = {}
+        self.override["GtkTextView"] = self.GtkTextView
+        self.override["GtkTextBuffer"] = self.GtkTextBuffer
+        
+    def version_check(self):
+        pass
+    
+    def get_language_manager(self):
+        if self.glm is None:
+            self.glm = self.GtkLanguageManager()
+        return self.glm
+
+    def set_highlighting_enabled(self, buf, fname, enabled):
+        if enabled:
+            gsl = self.get_language_from_file(os.path.abspath(fname))
+            if gsl:
+                buf.set_language(gsl)
+            else:
+                enabled = False
+        self.set_highlight(buf, enabled)
+
+class sourceview(_srcviewer):
+    srcviewer_module = "sourceview"
+
+    def overrides(self):
+        self.GtkTextView = self.gsv.SourceView
+        self.GtkTextBuffer = self.gsv.SourceBuffer
+        self.GtkLanguageManager = self.gsv.SourceLanguagesManager
+        self.set_tab_width = self.gsv.SourceView.set_tabs_width
+        self.set_highlight = self.gsv.SourceBuffer.set_highlight
+
+    def get_language_from_mime_type(self, mime_type):
+        return self.get_language_manager().get_language_from_mime_type(mime_type)
+    
+    def get_language_from_file(self, filename):
+        import gnomevfs
+        mime_type = gnomevfs.get_mime_type(
+                gnomevfs.make_uri_from_input(os.path.abspath(filename)))
+        return self.get_language_from_mime_type(mime_type)
+
+class gtksourceview(sourceview):
+    srcviewer_module = "gtksourceview"
+
+class _gtksourceview2(_srcviewer):
+    srcviewer_module = "gtksourceview2"
+    
+    def version_check(self):
+        raise NotImplementedError
+
+    def overrides(self):
+        self.GtkTextView = self.gsv.View
+        self.GtkTextBuffer = self.gsv.Buffer
+        self.GtkLanguageManager = self.gsv.LanguageManager
+        self.set_tab_width = self.gsv.View.set_tab_width
+        self.set_highlight = self.gsv.Buffer.set_highlight_syntax
+
+    def get_language_from_mime_type(self, mime_type):
+        for id in self.get_language_manager().get_language_ids():
+            lang = self.get_language_manager().get_language(id)
+            for mimetype in lang.get_mime_types():
+                if mime_type == mimetype:
+                    return lang
+        return None
+
+class gtksourceview22(_gtksourceview2):
+
+    def version_check(self):
+        if self.gsv.pygtksourceview2_version[1] > 2:
+            raise ImportError
+
+    def get_language_from_file(self, filename):
+        from fnmatch import fnmatch
+        for id in self.get_language_manager().get_language_ids():
+            lang = self.get_language_manager().get_language(id)
+            for aglob in lang.get_globs():
+                if fnmatch(filename, aglob):
+                    return lang
+        return None
+
+class gtksourceview24(_gtksourceview2):
+
+    def version_check(self):
+        if self.gsv.pygtksourceview2_version[1] < 4:
+            raise ImportError
+
+    def get_language_from_file(self, filename):
+        return self.get_language_manager().guess_language(filename)
+
+class nullsourceview(_srcviewer):
+
+    def __init__(self):
+        # No super() call, and nothing else but that
+        self.override = {}
+        
+    def __nonzero__(self):
+        return False
+
+def get_srcviewer():
+    for srcv in (gtksourceview22, gtksourceview24, gtksourceview, sourceview):
+        try:
+            return srcv()
+        except ImportError:
+            pass
+    return nullsourceview()
+
+srcviewer = get_srcviewer()


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