24
21
have_gtksourceview = True
25
22
except ImportError:
26
23
have_gtksourceview = False
35
27
from bzrlib.diff import show_diff_trees
36
28
from bzrlib.errors import NoSuchFile
37
from bzrlib.trace import warning
40
class DiffDisplay(gtk.ScrolledWindow):
41
"""This is the soft and chewy filling for a DiffWindow."""
44
gtk.ScrolledWindow.__init__(self)
48
self.parent_tree = None
51
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
52
self.set_shadow_type(gtk.SHADOW_IN)
54
if have_gtksourceview:
55
self.buffer = gtksourceview.SourceBuffer()
56
slm = gtksourceview.SourceLanguagesManager()
57
gsl = slm.get_language_from_mime_type("text/x-patch")
59
self.apply_gedit_colors(gsl)
60
self.apply_colordiff_colors(gsl)
61
self.buffer.set_language(gsl)
62
self.buffer.set_highlight(True)
64
sourceview = gtksourceview.SourceView(self.buffer)
66
self.buffer = gtk.TextBuffer()
67
sourceview = gtk.TextView(self.buffer)
69
sourceview.set_editable(False)
70
sourceview.modify_font(pango.FontDescription("Monospace"))
75
def apply_gedit_colors(lang):
76
"""Set style for lang to that specified in gedit configuration.
78
This method needs the gconf module.
80
:param lang: a gtksourceview.SourceLanguage object.
82
GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
83
GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
85
client = gconf.client_get_default()
86
client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
88
for tag in lang.get_tags():
90
gconf_key = GEDIT_LANG_PATH + '/' + tag_id
91
style_string = client.get_string(gconf_key)
93
if style_string is None:
96
# function to get a bool from a string that's either '0' or '1'
97
string_bool = lambda x: bool(int(x))
99
# style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
100
# values are: mask, fg, bg, italic, bold, underline, strike
101
# this packs them into (str_value, attr_name, conv_func) tuples
102
items = zip(style_string.split('/'), ['mask', 'foreground',
103
'background', 'italic', 'bold', 'underline', 'strikethrough' ],
104
[ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
105
string_bool, string_bool, string_bool ]
108
style = gtksourceview.SourceTagStyle()
110
# XXX The mask attribute controls whether the present values of
111
# foreground and background color should in fact be used. Ideally
112
# (and that's what gedit does), one could set all three attributes,
113
# and let the TagStyle object figure out which colors to use.
114
# However, in the GtkSourceview python bindings, the mask attribute
115
# is read-only, and it's derived instead from the colors being
116
# set or not. This means that we have to sometimes refrain from
117
# setting fg or bg colors, depending on the value of the mask.
118
# This code could go away if mask were writable.
119
mask = int(items[0][0])
120
if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
122
if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
124
items[0:1] = [] # skip the mask unconditionally
126
for value, attr, func in items:
130
warning('gconf key %s contains an invalid value: %s'
133
setattr(style, attr, value)
135
lang.set_tag_style(tag_id, style)
138
def apply_colordiff_colors(lang):
139
"""Set style colors for lang using the colordiff configuration file.
141
Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
143
:param lang: a "Diff" gtksourceview.SourceLanguage object.
147
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
148
f = os.path.expanduser(f)
149
if os.path.exists(f):
153
warning('could not open file %s: %s' % (f, str(e)))
155
colors.update(DiffDisplay.parse_colordiffrc(f))
159
# ~/.colordiffrc does not exist
163
# map GtkSourceView tags to colordiff names
164
# since GSV is richer, accept new names for extra bits,
165
# defaulting to old names if they're not present
166
'Added@32@line': ['newtext'],
167
'Removed@32@line': ['oldtext'],
168
'Location': ['location', 'diffstuff'],
169
'Diff@32@file': ['file', 'diffstuff'],
170
'Special@32@case': ['specialcase', 'diffstuff'],
173
for tag in lang.get_tags():
174
tag_id = tag.get_id()
175
keys = mapping.get(tag_id, [])
179
color = colors.get(key, None)
180
if color is not None:
186
style = gtksourceview.SourceTagStyle()
188
style.foreground = gtk.gdk.color_parse(color)
190
warning('not a valid color: %s' % color)
192
lang.set_tag_style(tag_id, style)
195
def parse_colordiffrc(fileobj):
196
"""Parse fileobj as a colordiff configuration file.
198
:return: A dict with the key -> value pairs.
202
if re.match(r'^\s*#', line):
206
key, val = line.split('=', 1)
207
colors[key.strip()] = val.strip()
210
def set_trees(self, rev_tree, parent_tree):
211
self.rev_tree = rev_tree
212
self.parent_tree = parent_tree
214
def show_diff(self, specific_files):
216
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
217
self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
220
31
class DiffWindow(gtk.Window):