/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: Adeodato Simó
  • Date: 2007-07-05 19:50:44 UTC
  • mto: This revision was merged to the branch mainline in revision 234.
  • Revision ID: dato@net.com.org.es-20070705195044-rcrj8f27i386cvrv
Support setting diff colors from gedit's syntax highlighting config too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: UTF-8 -*-
 
3
"""Difference window.
 
4
 
 
5
This module contains the code to manage the diff window which shows
 
6
the changes made between two revisions on a branch.
 
7
"""
 
8
 
 
9
__copyright__ = "Copyright © 2005 Canonical Ltd."
 
10
__author__    = "Scott James Remnant <scott@ubuntu.com>"
 
11
 
 
12
 
 
13
from cStringIO import StringIO
 
14
 
 
15
import gtk
 
16
import pango
 
17
import os
 
18
import re
 
19
import sys
 
20
 
 
21
try:
 
22
    import gtksourceview
 
23
    have_gtksourceview = True
 
24
except ImportError:
 
25
    have_gtksourceview = False
 
26
try:
 
27
    import gconf
 
28
    have_gconf = True
 
29
except ImportError:
 
30
    have_gconf = False
 
31
 
 
32
import bzrlib
 
33
 
 
34
from bzrlib.diff import show_diff_trees
 
35
from bzrlib.errors import NoSuchFile
 
36
from bzrlib.trace import warning
 
37
 
 
38
 
 
39
class DiffWindow(gtk.Window):
 
40
    """Diff window.
 
41
 
 
42
    This object represents and manages a single window containing the
 
43
    differences between two revisions on a branch.
 
44
    """
 
45
 
 
46
    def __init__(self):
 
47
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
 
48
        self.set_border_width(0)
 
49
        self.set_title("bzrk diff")
 
50
 
 
51
        # Use two thirds of the screen by default
 
52
        screen = self.get_screen()
 
53
        monitor = screen.get_monitor_geometry(0)
 
54
        width = int(monitor.width * 0.66)
 
55
        height = int(monitor.height * 0.66)
 
56
        self.set_default_size(width, height)
 
57
 
 
58
        self.construct()
 
59
 
 
60
    def construct(self):
 
61
        """Construct the window contents."""
 
62
        # The   window  consists  of   a  pane   containing:  the
 
63
        # hierarchical list  of files on  the left, and  the diff
 
64
        # for the currently selected file on the right.
 
65
        pane = gtk.HPaned()
 
66
        self.add(pane)
 
67
        pane.show()
 
68
 
 
69
        # The file hierarchy: a scrollable treeview
 
70
        scrollwin = gtk.ScrolledWindow()
 
71
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
72
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
 
73
        pane.pack1(scrollwin)
 
74
        scrollwin.show()
 
75
 
 
76
        self.model = gtk.TreeStore(str, str)
 
77
        self.treeview = gtk.TreeView(self.model)
 
78
        self.treeview.set_headers_visible(False)
 
79
        self.treeview.set_search_column(1)
 
80
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
 
81
        scrollwin.add(self.treeview)
 
82
        self.treeview.show()
 
83
 
 
84
        cell = gtk.CellRendererText()
 
85
        cell.set_property("width-chars", 20)
 
86
        column = gtk.TreeViewColumn()
 
87
        column.pack_start(cell, expand=True)
 
88
        column.add_attribute(cell, "text", 0)
 
89
        self.treeview.append_column(column)
 
90
 
 
91
        # The diffs of the  selected file: a scrollable source or
 
92
        # text view
 
93
        scrollwin = gtk.ScrolledWindow()
 
94
        scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 
95
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
 
96
        pane.pack2(scrollwin)
 
97
        scrollwin.show()
 
98
 
 
99
        if have_gtksourceview:
 
100
            self.buffer = gtksourceview.SourceBuffer()
 
101
            slm = gtksourceview.SourceLanguagesManager()
 
102
            gsl = slm.get_language_from_mime_type("text/x-patch")
 
103
            if have_gconf:
 
104
                self.apply_gedit_colors(gsl)
 
105
            self.apply_colordiff_colors(gsl)
 
106
            self.buffer.set_language(gsl)
 
107
            self.buffer.set_highlight(True)
 
108
 
 
109
            sourceview = gtksourceview.SourceView(self.buffer)
 
110
        else:
 
111
            self.buffer = gtk.TextBuffer()
 
112
            sourceview = gtk.TextView(self.buffer)
 
113
 
 
114
        sourceview.set_editable(False)
 
115
        sourceview.modify_font(pango.FontDescription("Monospace"))
 
116
        scrollwin.add(sourceview)
 
117
        sourceview.show()
 
118
 
 
119
    def set_diff(self, description, rev_tree, parent_tree):
 
120
        """Set the differences showed by this window.
 
121
 
 
122
        Compares the two trees and populates the window with the
 
123
        differences.
 
124
        """
 
125
        self.rev_tree = rev_tree
 
126
        self.parent_tree = parent_tree
 
127
 
 
128
        self.model.clear()
 
129
        delta = self.rev_tree.changes_from(self.parent_tree)
 
130
 
 
131
        self.model.append(None, [ "Complete Diff", "" ])
 
132
 
 
133
        if len(delta.added):
 
134
            titer = self.model.append(None, [ "Added", None ])
 
135
            for path, id, kind in delta.added:
 
136
                self.model.append(titer, [ path, path ])
 
137
 
 
138
        if len(delta.removed):
 
139
            titer = self.model.append(None, [ "Removed", None ])
 
140
            for path, id, kind in delta.removed:
 
141
                self.model.append(titer, [ path, path ])
 
142
 
 
143
        if len(delta.renamed):
 
144
            titer = self.model.append(None, [ "Renamed", None ])
 
145
            for oldpath, newpath, id, kind, text_modified, meta_modified \
 
146
                    in delta.renamed:
 
147
                self.model.append(titer, [ oldpath, newpath ])
 
148
 
 
149
        if len(delta.modified):
 
150
            titer = self.model.append(None, [ "Modified", None ])
 
151
            for path, id, kind, text_modified, meta_modified in delta.modified:
 
152
                self.model.append(titer, [ path, path ])
 
153
 
 
154
        self.treeview.expand_all()
 
155
        self.set_title(description + " - bzrk diff")
 
156
 
 
157
    def set_file(self, file_path):
 
158
        tv_path = None
 
159
        for data in self.model:
 
160
            for child in data.iterchildren():
 
161
                if child[0] == file_path or child[1] == file_path:
 
162
                    tv_path = child.path
 
163
                    break
 
164
        if tv_path is None:
 
165
            raise NoSuchFile(file_path)
 
166
        self.treeview.set_cursor(tv_path)
 
167
        self.treeview.scroll_to_cell(tv_path)
 
168
 
 
169
    def _treeview_cursor_cb(self, *args):
 
170
        """Callback for when the treeview cursor changes."""
 
171
        (path, col) = self.treeview.get_cursor()
 
172
        specific_files = [ self.model[path][1] ]
 
173
        if specific_files == [ None ]:
 
174
            return
 
175
        elif specific_files == [ "" ]:
 
176
            specific_files = []
 
177
 
 
178
        s = StringIO()
 
179
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
 
180
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
 
181
 
 
182
    @staticmethod
 
183
    def apply_gedit_colors(lang):
 
184
        """Set style for lang to that specified in gedit configuration.
 
185
 
 
186
        This method needs the gconf module.
 
187
        
 
188
        :param lang: a gtksourceview.SourceLanguage object.
 
189
        """
 
190
        GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
 
191
        GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
 
192
 
 
193
        client = gconf.client_get_default()
 
194
        client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
 
195
 
 
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)
 
200
 
 
201
            if style_string is None:
 
202
                continue
 
203
 
 
204
            # function to get a bool from a string that's either '0' or '1'
 
205
            string_bool = lambda x: bool(int(x))
 
206
 
 
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 ]
 
214
            )
 
215
 
 
216
            style = gtksourceview.SourceTagStyle()
 
217
 
 
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
 
229
                items[2:3] = []
 
230
            if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
 
231
                items[1:2] = []
 
232
            items[0:1] = [] # skip the mask unconditionally
 
233
 
 
234
            for value, attr, func in items:
 
235
                try:
 
236
                    value = func(value)
 
237
                except ValueError:
 
238
                    warning('gconf key %s contains an invalid value: %s'
 
239
                            % gconf_key, value)
 
240
                else:
 
241
                    setattr(style, attr, value)
 
242
 
 
243
            lang.set_tag_style(tag_id, style)
 
244
 
 
245
    @staticmethod
 
246
    def apply_colordiff_colors(lang):
 
247
        """Set style colors for lang using the colordiff configuration file.
 
248
 
 
249
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
 
250
 
 
251
        :param lang: a "Diff" gtksourceview.SourceLanguage object.
 
252
        """
 
253
        def parse_colordiffrc(fileobj):
 
254
            """Parse fileobj as a colordiff configuration file.
 
255
            
 
256
            :return: A dict with the key -> value pairs.
 
257
            """
 
258
            colors = {}
 
259
            for line in fileobj:
 
260
                if re.match(r'^\s*#', line):
 
261
                    continue
 
262
                key, val = line.split('=')
 
263
                colors[key.strip()] = val.strip()
 
264
            return colors
 
265
 
 
266
        colors = {}
 
267
 
 
268
        for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
 
269
            f = os.path.expanduser(f)
 
270
            if os.path.exists(f):
 
271
                try:
 
272
                    f = file(f)
 
273
                except IOError, e:
 
274
                    warning('could not open file %s: %s' % (f, str(e)))
 
275
                else:
 
276
                    colors.update(parse_colordiffrc(f))
 
277
                    f.close()
 
278
 
 
279
        if not colors:
 
280
            # ~/.colordiffrc does not exist
 
281
            return
 
282
 
 
283
        mapping = {
 
284
                # map GtkSourceView tags to colordiff names
 
285
                # since GSV is richer, accept new names for extra bits,
 
286
                # defaulting to old names if they're not present
 
287
                'Added@32@line': ['newtext'],
 
288
                'Removed@32@line': ['oldtext'],
 
289
                'Location': ['location', 'diffstuff'],
 
290
                'Diff@32@file': ['file', 'diffstuff'],
 
291
                'Special@32@case': ['specialcase', 'diffstuff'],
 
292
        }
 
293
 
 
294
        for tag in lang.get_tags():
 
295
            tag_id = tag.get_id()
 
296
            keys = mapping.get(tag_id, [])
 
297
            color = None
 
298
 
 
299
            for key in keys:
 
300
                color = colors.get(key, None)
 
301
                if color is not None:
 
302
                    break
 
303
 
 
304
            if color is None:
 
305
                continue
 
306
 
 
307
            style = gtksourceview.SourceTagStyle()
 
308
            try:
 
309
                style.foreground = gtk.gdk.color_parse(color)
 
310
            except ValueError:
 
311
                warning('not a valid color: %s' % color)
 
312
            else:
 
313
                lang.set_tag_style(tag_id, style)