178
168
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
179
169
self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
182
def apply_gedit_colors(lang):
183
"""Set style for lang to that specified in gedit configuration.
185
This method needs the gconf module.
187
:param lang: a gtksourceview.SourceLanguage object.
189
GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
190
GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
192
client = gconf.client_get_default()
193
client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
195
for tag in lang.get_tags():
196
tag_id = tag.get_id()
197
gconf_key = GEDIT_LANG_PATH + '/' + tag_id
198
style_string = client.get_string(gconf_key)
200
if style_string is None:
203
# function to get a bool from a string that's either '0' or '1'
204
string_bool = lambda x: bool(int(x))
206
# style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
207
# values are: mask, fg, bg, italic, bold, underline, strike
208
# this packs them into (str_value, attr_name, conv_func) tuples
209
items = zip(style_string.split('/'), ['mask', 'foreground',
210
'background', 'italic', 'bold', 'underline', 'strikethrough' ],
211
[ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
212
string_bool, string_bool, string_bool ]
215
style = gtksourceview.SourceTagStyle()
217
# XXX The mask attribute controls whether the present values of
218
# foreground and background color should in fact be used. Ideally
219
# (and that's what gedit does), one could set all three attributes,
220
# and let the TagStyle object figure out which colors to use.
221
# However, in the GtkSourceview python bindings, the mask attribute
222
# is read-only, and it's derived instead from the colors being
223
# set or not. This means that we have to sometimes refrain from
224
# setting fg or bg colors, depending on the value of the mask.
225
# This code could go away if mask were writable.
226
mask = int(items[0][0])
227
if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
229
if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
231
items[0:1] = [] # skip the mask unconditionally
233
for value, attr, func in items:
237
warning('gconf key %s contains an invalid value: %s'
240
setattr(style, attr, value)
242
lang.set_tag_style(tag_id, style)
245
def apply_colordiff_colors(lang):
246
"""Set style colors for lang using the colordiff configuration file.
248
Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
250
:param lang: a "Diff" gtksourceview.SourceLanguage object.
254
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
255
f = os.path.expanduser(f)
256
if os.path.exists(f):
260
warning('could not open file %s: %s' % (f, str(e)))
262
colors.update(DiffWindow.parse_colordiffrc(f))
266
# ~/.colordiffrc does not exist
270
# map GtkSourceView tags to colordiff names
271
# since GSV is richer, accept new names for extra bits,
272
# defaulting to old names if they're not present
273
'Added@32@line': ['newtext'],
274
'Removed@32@line': ['oldtext'],
275
'Location': ['location', 'diffstuff'],
276
'Diff@32@file': ['file', 'diffstuff'],
277
'Special@32@case': ['specialcase', 'diffstuff'],
280
for tag in lang.get_tags():
281
tag_id = tag.get_id()
282
keys = mapping.get(tag_id, [])
286
color = colors.get(key, None)
287
if color is not None:
293
style = gtksourceview.SourceTagStyle()
295
style.foreground = gtk.gdk.color_parse(color)
297
warning('not a valid color: %s' % color)
299
lang.set_tag_style(tag_id, style)
302
def parse_colordiffrc(fileobj):
303
"""Parse fileobj as a colordiff configuration file.
305
:return: A dict with the key -> value pairs.
309
if re.match(r'^\s*#', line):
313
key, val = line.split('=', 1)
314
colors[key.strip()] = val.strip()