/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: Adeodato Simó
  • Date: 2007-07-05 19:50:44 UTC
  • mto: This revision was merged to the branch mainline in revision 234.
  • Revision ID: dato@net.com.org.es-20070705195044-rcrj8f27i386cvrv
Support setting diff colors from gedit's syntax highlighting config too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    have_gtksourceview = True
24
24
except ImportError:
25
25
    have_gtksourceview = False
 
26
try:
 
27
    import gconf
 
28
    have_gconf = True
 
29
except ImportError:
 
30
    have_gconf = False
26
31
 
27
32
import bzrlib
28
33
 
95
100
            self.buffer = gtksourceview.SourceBuffer()
96
101
            slm = gtksourceview.SourceLanguagesManager()
97
102
            gsl = slm.get_language_from_mime_type("text/x-patch")
 
103
            if have_gconf:
 
104
                self.apply_gedit_colors(gsl)
98
105
            self.apply_colordiff_colors(gsl)
99
106
            self.buffer.set_language(gsl)
100
107
            self.buffer.set_highlight(True)
173
180
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
174
181
 
175
182
    @staticmethod
 
183
    def apply_gedit_colors(lang):
 
184
        """Set style for lang to that specified in gedit configuration.
 
185
 
 
186
        This method needs the gconf module.
 
187
        
 
188
        :param lang: a gtksourceview.SourceLanguage object.
 
189
        """
 
190
        GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
 
191
        GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
 
192
 
 
193
        client = gconf.client_get_default()
 
194
        client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
 
195
 
 
196
        for tag in lang.get_tags():
 
197
            tag_id = tag.get_id()
 
198
            gconf_key = GEDIT_LANG_PATH + '/' + tag_id
 
199
            style_string = client.get_string(gconf_key)
 
200
 
 
201
            if style_string is None:
 
202
                continue
 
203
 
 
204
            # function to get a bool from a string that's either '0' or '1'
 
205
            string_bool = lambda x: bool(int(x))
 
206
 
 
207
            # style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
 
208
            # values are: mask, fg, bg, italic, bold, underline, strike
 
209
            # this packs them into (str_value, attr_name, conv_func) tuples
 
210
            items = zip(style_string.split('/'), ['mask', 'foreground',
 
211
                'background', 'italic', 'bold', 'underline', 'strikethrough' ],
 
212
                [ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
 
213
                    string_bool, string_bool, string_bool ]
 
214
            )
 
215
 
 
216
            style = gtksourceview.SourceTagStyle()
 
217
 
 
218
            # XXX The mask attribute controls whether the present values of
 
219
            # foreground and background color should in fact be used. Ideally
 
220
            # (and that's what gedit does), one could set all three attributes,
 
221
            # and let the TagStyle object figure out which colors to use.
 
222
            # However, in the GtkSourceview python bindings, the mask attribute
 
223
            # is read-only, and it's derived instead from the colors being
 
224
            # set or not. This means that we have to sometimes refrain from
 
225
            # setting fg or bg colors, depending on the value of the mask.
 
226
            # This code could go away if mask were writable.
 
227
            mask = int(items[0][0])
 
228
            if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
 
229
                items[2:3] = []
 
230
            if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
 
231
                items[1:2] = []
 
232
            items[0:1] = [] # skip the mask unconditionally
 
233
 
 
234
            for value, attr, func in items:
 
235
                try:
 
236
                    value = func(value)
 
237
                except ValueError:
 
238
                    warning('gconf key %s contains an invalid value: %s'
 
239
                            % gconf_key, value)
 
240
                else:
 
241
                    setattr(style, attr, value)
 
242
 
 
243
            lang.set_tag_style(tag_id, style)
 
244
 
 
245
    @staticmethod
176
246
    def apply_colordiff_colors(lang):
177
247
        """Set style colors for lang using the colordiff configuration file.
178
248