Hi,

the Ubuntu folks requested a migration to GtkSourceÍView2. This patch
just does that. I also refactored the gedit style scheme support a
bit, and you can see a hackish approach to support colordiffrc (thanks
to GtkSourceView API changes).

Cheers,
Szilveszter
# Bazaar merge directive format 2 (Bazaar 0.90)
# revision_id: [email protected]\
#   fwvt03uqtl2p6vk7
# target_branch: file:///home/szilveszter/Development/Bazaar/bzr-\
#   gtk/trunk/
# testament_sha1: 0b2d6843d1a677cfb102015318120e0b1f742977
# timestamp: 2009-05-26 15:59:34 +0200
# base_revision_id: [email protected]\
#   zhtopap2bn9vjtpc
# 
# Begin patch
=== modified file 'README'
--- README	2008-03-09 13:47:52 +0000
+++ README	2009-05-26 13:58:24 +0000
@@ -23,8 +23,8 @@
 
 In order to see syntax highlighted diffs:
 
-  * gtksourceview python bindings (on Debian and Ubuntu systems, these
-    are in the python-gnome2-desktop package)
+  * GtkSourceView2 Python bindings (on Debian and Ubuntu systems, these
+    are in the python-gtksourceview2 package)
 
 In order to use the nautilus integration, you will need:
 

=== modified file 'diff.py'
--- diff.py	2008-10-22 07:56:53 +0000
+++ diff.py	2009-05-26 13:53:30 +0000
@@ -17,9 +17,10 @@
 import os
 import re
 import sys
+from xml.etree.ElementTree import Element, SubElement, tostring
 
 try:
-    import gtksourceview
+    import gtksourceview2
     have_gtksourceview = True
 except ImportError:
     have_gtksourceview = False
@@ -63,16 +64,16 @@
         self.set_shadow_type(gtk.SHADOW_IN)
 
         if have_gtksourceview:
-            self.buffer = gtksourceview.SourceBuffer()
-            slm = gtksourceview.SourceLanguagesManager()
-            gsl = slm.get_language_from_mime_type("text/x-patch")
+            self.buffer = gtksourceview2.Buffer()
+            slm = gtksourceview2.LanguageManager()
+            gsl = slm.guess_language(content_type="text/x-patch")
             if have_gconf:
-                self.apply_gedit_colors(gsl)
-            self.apply_colordiff_colors(gsl)
+                self.apply_gedit_colors(self.buffer)
+            self.apply_colordiff_colors(self.buffer)
             self.buffer.set_language(gsl)
-            self.buffer.set_highlight(True)
+            self.buffer.set_highlight_syntax(True)
 
-            self.sourceview = gtksourceview.SourceView(self.buffer)
+            self.sourceview = gtksourceview2.View(self.buffer)
         else:
             self.buffer = gtk.TextBuffer()
             self.sourceview = gtk.TextView(self.buffer)
@@ -83,124 +84,93 @@
         self.sourceview.show()
 
     @staticmethod
-    def apply_gedit_colors(lang):
-        """Set style for lang to that specified in gedit configuration.
+    def apply_gedit_colors(buf):
+        """Set style to that specified in gedit configuration.
 
         This method needs the gconf module.
 
-        :param lang: a gtksourceview.SourceLanguage object.
+        :param buf: a gtksourceview2.Buffer object.
         """
-        GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
-        GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
+        GEDIT_SCHEME_PATH = '/apps/gedit-2/preferences/editor/colors/scheme'
 
         client = gconf.client_get_default()
-        client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
-
-        for tag in lang.get_tags():
-            tag_id = tag.get_id()
-            gconf_key = GEDIT_LANG_PATH + '/' + tag_id
-            style_string = client.get_string(gconf_key)
-
-            if style_string is None:
-                continue
-
-            # function to get a bool from a string that's either '0' or '1'
-            string_bool = lambda x: bool(int(x))
-
-            # style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
-            # values are: mask, fg, bg, italic, bold, underline, strike
-            # this packs them into (str_value, attr_name, conv_func) tuples
-            items = zip(style_string.split('/'), ['mask', 'foreground',
-                'background', 'italic', 'bold', 'underline', 'strikethrough' ],
-                [ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
-                    string_bool, string_bool, string_bool ]
-            )
-
-            style = gtksourceview.SourceTagStyle()
-
-            # XXX The mask attribute controls whether the present values of
-            # foreground and background color should in fact be used. Ideally
-            # (and that's what gedit does), one could set all three attributes,
-            # and let the TagStyle object figure out which colors to use.
-            # However, in the GtkSourceview python bindings, the mask attribute
-            # is read-only, and it's derived instead from the colors being
-            # set or not. This means that we have to sometimes refrain from
-            # setting fg or bg colors, depending on the value of the mask.
-            # This code could go away if mask were writable.
-            mask = int(items[0][0])
-            if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
-                items[2:3] = []
-            if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
-                items[1:2] = []
-            items[0:1] = [] # skip the mask unconditionally
-
-            for value, attr, func in items:
-                try:
-                    value = func(value)
-                except ValueError:
-                    warning('gconf key %s contains an invalid value: %s'
-                            % gconf_key, value)
-                else:
-                    setattr(style, attr, value)
-
-            lang.set_tag_style(tag_id, style)
+        style_scheme_name = client.get_string(GEDIT_SCHEME_PATH)
+        style_scheme = gtksourceview2.StyleSchemeManager().get_scheme(style_scheme_name)
+        
+        buf.set_style_scheme(style_scheme)
 
     @classmethod
-    def apply_colordiff_colors(klass, lang):
+    def apply_colordiff_colors(klass, buf):
         """Set style colors for lang using the colordiff configuration file.
 
         Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
 
-        :param lang: a "Diff" gtksourceview.SourceLanguage object.
+        :param buf: a "Diff" gtksourceview2.Buffer object.
         """
-        colors = {}
-
-        for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
-            f = os.path.expanduser(f)
-            if os.path.exists(f):
-                try:
-                    f = file(f)
-                except IOError, e:
-                    warning('could not open file %s: %s' % (f, str(e)))
-                else:
-                    colors.update(klass.parse_colordiffrc(f))
-                    f.close()
-
-        if not colors:
-            # ~/.colordiffrc does not exist
-            return
-
-        mapping = {
-                # map GtkSourceView tags to colordiff names
+        scheme_manager = gtksourceview2.StyleSchemeManager()
+        style_scheme = scheme_manager.get_scheme('colordiff')
+        
+        # if style scheme not found, we'll generate it from colordiffrc
+        # TODO: reload if colordiffrc has changed.
+        if style_scheme is None:
+            colors = {}
+
+            for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
+                f = os.path.expanduser(f)
+                if os.path.exists(f):
+                    try:
+                        f = file(f)
+                    except IOError, e:
+                        warning('could not open file %s: %s' % (f, str(e)))
+                    else:
+                        colors.update(klass.parse_colordiffrc(f))
+                        f.close()
+
+            if not colors:
+                # ~/.colordiffrc does not exist
+                return
+            
+            mapping = {
+                # map GtkSourceView2 scheme styles to colordiff names
                 # since GSV is richer, accept new names for extra bits,
                 # defaulting to old names if they're not present
-                'ad...@32@line': ['newtext'],
-                'remo...@32@line': ['oldtext'],
-                'Location': ['location', 'diffstuff'],
-                'd...@32@file': ['file', 'diffstuff'],
-                'spec...@32@case': ['specialcase', 'diffstuff'],
-        }
-
-        for tag in lang.get_tags():
-            tag_id = tag.get_id()
-            keys = mapping.get(tag_id, [])
-            color = None
-
-            for key in keys:
-                color = colors.get(key, None)
-                if color is not None:
-                    break
-
-            if color is None:
-                continue
-
-            style = gtksourceview.SourceTagStyle()
-            try:
-                style.foreground = gtk.gdk.color_parse(color)
-            except ValueError:
-                warning('not a valid color: %s' % color)
-            else:
-                lang.set_tag_style(tag_id, style)
+                'diff:added-line': ['newtext'],
+                'diff:removed-line': ['oldtext'],
+                'diff:location': ['location', 'diffstuff'],
+                'diff:file': ['file', 'diffstuff'],
+                'diff:special-case': ['specialcase', 'diffstuff'],
+            }
+            
+            converted_colors = {}
+            for name, values in mapping.items():
+                color = None
+                for value in values:
+                    color = colors.get(value, None)
+                    if color is not None:
+                        break
+                if color is None:
+                    continue
+                converted_colors[name] = color
+            
+            # some xml magic to produce needed style scheme description
+            e_style_scheme = Element('style-scheme')
+            e_style_scheme.set('id', 'colordiff')
+            e_style_scheme.set('_name', 'ColorDiff')
+            e_style_scheme.set('version', '1.0')
+            for name, color in converted_colors.items():
+                style = SubElement(e_style_scheme, 'style')
+                style.set('name', name)
+                style.set('foreground', '#%s' % color)
+            
+            scheme_xml = tostring(e_style_scheme, 'UTF-8')
+            if not os.path.exists(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles')):
+                os.makedirs(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles'))
+            file(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles/colordiff.xml'), 'w').write(scheme_xml)
+            
+            scheme_manager.force_rescan()
+            style_scheme = scheme_manager.get_scheme('colordiff')
+        
+        buf.set_style_scheme(style_scheme)
 
     @staticmethod
     def parse_colordiffrc(fileobj):

# Begin bundle
IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWdDMOiUACB1/gERURABb////
f+ffCr////tgDg5323Xuu9x2ZLaVm0NChpssFJCtGQpQUUpEaIanqU9NAgg2gNNTI00YAjENDAIG
jBMAkimhpqnjQpvVPVTaaaj0PVPJ6pgNAj9JAMQGJk0YaTJpoTVAaDIaGgAA0aBoAAAAAkRBE00A
KaZT9FEeZNJqfqj1GgMAyAam0jygOMmTRoDRpiMjQxDAmjTEGI0GEABgVJITTQAICaaYhpqZNMgG
imyaAQBkbRGUBARwjTHyH5sBogUJrWkpOUrK0FAUkiVonu4xDCdwflGtnr8up6lt0cqIZrxsb/bx
/zQ8eTmQ6XVw6LLzMlj7f+475CNzfstP+sIkY0wb4dQKgDBZg0p2MU6xSwoMQIg1gRkjo4DooKoV
HrHIpgI9mkgSbFfhPRJEGciP6O1u49xzfGkdflpw0+wdxuKSGxttDaC/sEi+3psEg+DXJt8uMTMc
n0Ou4lqBvakXiUDnE0N6opOaAKMDRmoHiLicp1OgVlgENF2+9PI+U6DqUEncpI1ipLDz8lsA+GxK
oGRCluqu37WiA1zvGvwbYIpMIopmbDPgsQHOlalzLWgzAU1M4MgirdcKpe+3ovd3FPhpOVVzTgXA
0BtDCOOlhbkDWWOiHGU2lvRXZV6rcsQ4Y8pDBjY2Dkrcu53Y4RqaPxkBfZlGAGOkevXhi8ZPxd2N
IrPt822kzn628JIWAyUKRO/QNlKGah7zCiRG3GjQYYYXTPMzcgovABDjjjfA6j3sWVSQ0Pu+uvjm
nXXmKOz0rdkurXuuweXq3evyWhw+LyHLUGR3D/wpe1EL9ZLRDUwNtq1DwhFO9UTWOuGW/5FaUvE6
a4DmIIG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RxLthYkgwDa6QjjyX/dLPyI+Yu5IpwoSGhmHRKA==
-- 
bzr-gtk mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.canonical.com/mailman/listinfo/bzr-gtk

Reply via email to