84
83
self.sourceview.show()
87
def apply_gedit_colors(buf):
88
"""Set style to that specified in gedit configuration.
86
def apply_gedit_colors(lang):
87
"""Set style for lang to that specified in gedit configuration.
90
89
This method needs the gconf module.
92
:param buf: a gtksourceview2.Buffer object.
91
:param lang: a gtksourceview.SourceLanguage object.
94
GEDIT_SCHEME_PATH = '/apps/gedit-2/preferences/editor/colors/scheme'
93
GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
94
GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
96
96
client = gconf.client_get_default()
97
style_scheme_name = client.get_string(GEDIT_SCHEME_PATH)
98
if style_scheme_name is not None:
99
style_scheme = gtksourceview2.StyleSchemeManager().get_scheme(style_scheme_name)
101
buf.set_style_scheme(style_scheme)
97
client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
99
for tag in lang.get_tags():
100
tag_id = tag.get_id()
101
gconf_key = GEDIT_LANG_PATH + '/' + tag_id
102
style_string = client.get_string(gconf_key)
104
if style_string is None:
107
# function to get a bool from a string that's either '0' or '1'
108
string_bool = lambda x: bool(int(x))
110
# style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
111
# values are: mask, fg, bg, italic, bold, underline, strike
112
# this packs them into (str_value, attr_name, conv_func) tuples
113
items = zip(style_string.split('/'), ['mask', 'foreground',
114
'background', 'italic', 'bold', 'underline', 'strikethrough' ],
115
[ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
116
string_bool, string_bool, string_bool ]
119
style = gtksourceview.SourceTagStyle()
121
# XXX The mask attribute controls whether the present values of
122
# foreground and background color should in fact be used. Ideally
123
# (and that's what gedit does), one could set all three attributes,
124
# and let the TagStyle object figure out which colors to use.
125
# However, in the GtkSourceview python bindings, the mask attribute
126
# is read-only, and it's derived instead from the colors being
127
# set or not. This means that we have to sometimes refrain from
128
# setting fg or bg colors, depending on the value of the mask.
129
# This code could go away if mask were writable.
130
mask = int(items[0][0])
131
if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
133
if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
135
items[0:1] = [] # skip the mask unconditionally
137
for value, attr, func in items:
141
warning('gconf key %s contains an invalid value: %s'
144
setattr(style, attr, value)
146
lang.set_tag_style(tag_id, style)
104
def apply_colordiff_colors(klass, buf):
149
def apply_colordiff_colors(klass, lang):
105
150
"""Set style colors for lang using the colordiff configuration file.
107
152
Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
109
:param buf: a "Diff" gtksourceview2.Buffer object.
154
:param lang: a "Diff" gtksourceview.SourceLanguage object.
111
scheme_manager = gtksourceview2.StyleSchemeManager()
112
style_scheme = scheme_manager.get_scheme('colordiff')
114
# if style scheme not found, we'll generate it from colordiffrc
115
# TODO: reload if colordiffrc has changed.
116
if style_scheme is None:
119
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
120
f = os.path.expanduser(f)
121
if os.path.exists(f):
125
warning('could not open file %s: %s' % (f, str(e)))
127
colors.update(klass.parse_colordiffrc(f))
131
# ~/.colordiffrc does not exist
135
# map GtkSourceView2 scheme styles to colordiff names
158
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
159
f = os.path.expanduser(f)
160
if os.path.exists(f):
164
warning('could not open file %s: %s' % (f, str(e)))
166
colors.update(klass.parse_colordiffrc(f))
170
# ~/.colordiffrc does not exist
174
# map GtkSourceView tags to colordiff names
136
175
# since GSV is richer, accept new names for extra bits,
137
176
# defaulting to old names if they're not present
138
'diff:added-line': ['newtext'],
139
'diff:removed-line': ['oldtext'],
140
'diff:location': ['location', 'diffstuff'],
141
'diff:file': ['file', 'diffstuff'],
142
'diff:special-case': ['specialcase', 'diffstuff'],
145
converted_colors = {}
146
for name, values in mapping.items():
149
color = colors.get(value, None)
150
if color is not None:
154
converted_colors[name] = color
156
# some xml magic to produce needed style scheme description
157
e_style_scheme = Element('style-scheme')
158
e_style_scheme.set('id', 'colordiff')
159
e_style_scheme.set('_name', 'ColorDiff')
160
e_style_scheme.set('version', '1.0')
161
for name, color in converted_colors.items():
162
style = SubElement(e_style_scheme, 'style')
163
style.set('name', name)
164
style.set('foreground', '#%s' % color)
166
scheme_xml = tostring(e_style_scheme, 'UTF-8')
167
if not os.path.exists(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles')):
168
os.makedirs(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles'))
169
file(os.path.expanduser('~/.local/share/gtksourceview-2.0/styles/colordiff.xml'), 'w').write(scheme_xml)
171
scheme_manager.force_rescan()
172
style_scheme = scheme_manager.get_scheme('colordiff')
174
buf.set_style_scheme(style_scheme)
177
'Added@32@line': ['newtext'],
178
'Removed@32@line': ['oldtext'],
179
'Location': ['location', 'diffstuff'],
180
'Diff@32@file': ['file', 'diffstuff'],
181
'Special@32@case': ['specialcase', 'diffstuff'],
184
for tag in lang.get_tags():
185
tag_id = tag.get_id()
186
keys = mapping.get(tag_id, [])
190
color = colors.get(key, None)
191
if color is not None:
197
style = gtksourceview.SourceTagStyle()
199
style.foreground = gtk.gdk.color_parse(color)
201
warning('not a valid color: %s' % color)
203
lang.set_tag_style(tag_id, style)
177
206
def parse_colordiffrc(fileobj):