/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: Szilveszter Farkas
  • Date: 2009-05-26 13:53:30 UTC
  • mto: (635.2.6 trunk)
  • mto: This revision was merged to the branch mainline in revision 639.
  • Revision ID: szilveszter.farkas@gmail.com-20090526135330-ewlmnokv5qn1tu5y
Made apply_colordiff_colors() work with gtksourceview2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import os
18
18
import re
19
19
import sys
 
20
from xml.etree.ElementTree import Element, SubElement, tostring
20
21
 
21
22
try:
22
23
    import gtksourceview2
68
69
            gsl = slm.guess_language(content_type="text/x-patch")
69
70
            if have_gconf:
70
71
                self.apply_gedit_colors(self.buffer)
71
 
            self.apply_colordiff_colors(gsl)
 
72
            self.apply_colordiff_colors(self.buffer)
72
73
            self.buffer.set_language(gsl)
73
74
            self.buffer.set_highlight_syntax(True)
74
75
 
99
100
        buf.set_style_scheme(style_scheme)
100
101
 
101
102
    @classmethod
102
 
    def apply_colordiff_colors(klass, lang):
 
103
    def apply_colordiff_colors(klass, buf):
103
104
        """Set style colors for lang using the colordiff configuration file.
104
105
 
105
106
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
106
107
 
107
 
        :param lang: a "Diff" gtksourceview2.Language object.
 
108
        :param buf: a "Diff" gtksourceview2.Buffer object.
108
109
        """
109
 
        colors = {}
110
 
 
111
 
        for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
112
 
            f = os.path.expanduser(f)
113
 
            if os.path.exists(f):
114
 
                try:
115
 
                    f = file(f)
116
 
                except IOError, e:
117
 
                    warning('could not open file %s: %s' % (f, str(e)))
118
 
                else:
119
 
                    colors.update(klass.parse_colordiffrc(f))
120
 
                    f.close()
121
 
 
122
 
        if not colors:
123
 
            # ~/.colordiffrc does not exist
124
 
            return
125
 
 
126
 
        mapping = {
127
 
                # map GtkSourceView tags to colordiff names
 
110
        scheme_manager = gtksourceview2.StyleSchemeManager()
 
111
        style_scheme = scheme_manager.get_scheme('colordiff')
 
112
        
 
113
        # if style scheme not found, we'll generate it from colordiffrc
 
114
        # TODO: reload if colordiffrc has changed.
 
115
        if style_scheme is None:
 
116
            colors = {}
 
117
 
 
118
            for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
 
119
                f = os.path.expanduser(f)
 
120
                if os.path.exists(f):
 
121
                    try:
 
122
                        f = file(f)
 
123
                    except IOError, e:
 
124
                        warning('could not open file %s: %s' % (f, str(e)))
 
125
                    else:
 
126
                        colors.update(klass.parse_colordiffrc(f))
 
127
                        f.close()
 
128
 
 
129
            if not colors:
 
130
                # ~/.colordiffrc does not exist
 
131
                return
 
132
            
 
133
            mapping = {
 
134
                # map GtkSourceView2 scheme styles to colordiff names
128
135
                # since GSV is richer, accept new names for extra bits,
129
136
                # defaulting to old names if they're not present
130
 
                'Added@32@line': ['newtext'],
131
 
                'Removed@32@line': ['oldtext'],
132
 
                'Location': ['location', 'diffstuff'],
133
 
                'Diff@32@file': ['file', 'diffstuff'],
134
 
                'Special@32@case': ['specialcase', 'diffstuff'],
135
 
        }
136
 
 
137
 
        for tag in lang.get_tags():
138
 
            tag_id = tag.get_id()
139
 
            keys = mapping.get(tag_id, [])
140
 
            color = None
141
 
 
142
 
            for key in keys:
143
 
                color = colors.get(key, None)
144
 
                if color is not None:
145
 
                    break
146
 
 
147
 
            if color is None:
148
 
                continue
149
 
 
150
 
            style = gtksourceview2.Style()
151
 
            try:
152
 
                style.foreground = gtk.gdk.color_parse(color)
153
 
            except ValueError:
154
 
                warning('not a valid color: %s' % color)
155
 
            else:
156
 
                lang.set_tag_style(tag_id, style)
 
137
                'diff:added-line': ['newtext'],
 
138
                'diff:removed-line': ['oldtext'],
 
139
                'diff:location': ['location', 'diffstuff'],
 
140
                'diff:file': ['file', 'diffstuff'],
 
141
                'diff:special-case': ['specialcase', 'diffstuff'],
 
142
            }
 
143
            
 
144
            converted_colors = {}
 
145
            for name, values in mapping.items():
 
146
                color = None
 
147
                for value in values:
 
148
                    color = colors.get(value, None)
 
149
                    if color is not None:
 
150
                        break
 
151
                if color is None:
 
152
                    continue
 
153
                converted_colors[name] = color
 
154
            
 
155
            # some xml magic to produce needed style scheme description
 
156
            e_style_scheme = Element('style-scheme')
 
157
            e_style_scheme.set('id', 'colordiff')
 
158
            e_style_scheme.set('_name', 'ColorDiff')
 
159
            e_style_scheme.set('version', '1.0')
 
160
            for name, color in converted_colors.items():
 
161
                style = SubElement(e_style_scheme, 'style')
 
162
                style.set('name', name)
 
163
                style.set('foreground', '#%s' % color)
 
164
            
 
165
            scheme_xml = tostring(e_style_scheme, 'UTF-8')
 
166
            if not os.path.exists(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles')):
 
167
                os.makedirs(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles'))
 
168
            file(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles/colordiff.xml'), 'w').write(scheme_xml)
 
169
            
 
170
            scheme_manager.force_rescan()
 
171
            style_scheme = scheme_manager.get_scheme('colordiff')
 
172
        
 
173
        buf.set_style_scheme(style_scheme)
157
174
 
158
175
    @staticmethod
159
176
    def parse_colordiffrc(fileobj):