173
180
self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
183
def apply_gedit_colors(lang):
184
"""Set style for lang to that specified in gedit configuration.
186
This method needs the gconf module.
188
:param lang: a gtksourceview.SourceLanguage object.
190
GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
191
GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
193
client = gconf.client_get_default()
194
client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
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)
201
if style_string is None:
204
# function to get a bool from a string that's either '0' or '1'
205
string_bool = lambda x: bool(int(x))
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 ]
216
style = gtksourceview.SourceTagStyle()
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
230
if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
232
items[0:1] = [] # skip the mask unconditionally
234
for value, attr, func in items:
238
warning('gconf key %s contains an invalid value: %s'
241
setattr(style, attr, value)
243
lang.set_tag_style(tag_id, style)
176
246
def apply_colordiff_colors(lang):
177
247
"""Set style colors for lang using the colordiff configuration file.