99
100
buf.set_style_scheme(style_scheme)
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.
105
106
Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
107
:param lang: a "Diff" gtksourceview2.Language object.
108
:param buf: a "Diff" gtksourceview2.Buffer object.
111
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
112
f = os.path.expanduser(f)
113
if os.path.exists(f):
117
warning('could not open file %s: %s' % (f, str(e)))
119
colors.update(klass.parse_colordiffrc(f))
123
# ~/.colordiffrc does not exist
127
# map GtkSourceView tags to colordiff names
110
scheme_manager = gtksourceview2.StyleSchemeManager()
111
style_scheme = scheme_manager.get_scheme('colordiff')
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:
118
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
119
f = os.path.expanduser(f)
120
if os.path.exists(f):
124
warning('could not open file %s: %s' % (f, str(e)))
126
colors.update(klass.parse_colordiffrc(f))
130
# ~/.colordiffrc does not exist
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'],
137
for tag in lang.get_tags():
138
tag_id = tag.get_id()
139
keys = mapping.get(tag_id, [])
143
color = colors.get(key, None)
144
if color is not None:
150
style = gtksourceview2.Style()
152
style.foreground = gtk.gdk.color_parse(color)
154
warning('not a valid color: %s' % color)
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'],
144
converted_colors = {}
145
for name, values in mapping.items():
148
color = colors.get(value, None)
149
if color is not None:
153
converted_colors[name] = color
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)
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)
170
scheme_manager.force_rescan()
171
style_scheme = scheme_manager.get_scheme('colordiff')
173
buf.set_style_scheme(style_scheme)
159
176
def parse_colordiffrc(fileobj):