/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.1.1 by Dan Loda
First working version of xannotate.
1
# Copyright (C) 2005 Dan Loda <danloda@gmail.com>
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
17
import time
18
0.1.1 by Dan Loda
First working version of xannotate.
19
import pygtk
20
pygtk.require("2.0")
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
21
import gobject
0.1.1 by Dan Loda
First working version of xannotate.
22
import gtk
23
import pango
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
24
import re
0.1.1 by Dan Loda
First working version of xannotate.
25
66.6.6 by Aaron Bentley
Support scrolling based on an offset
26
from bzrlib import patiencediff, tsort
0.1.1 by Dan Loda
First working version of xannotate.
27
from bzrlib.errors import NoSuchRevision
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
28
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
0.1.1 by Dan Loda
First working version of xannotate.
29
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
30
from colormap import AnnotateColorMap, AnnotateColorSaturation
146 by Jelmer Vernooij
Move more code to top-level directory.
31
from bzrlib.plugins.gtk.logview import LogView
0.1.1 by Dan Loda
First working version of xannotate.
32
33
34
(
35
    REVISION_ID_COL,
36
    LINE_NUM_COL,
37
    COMMITTER_COL,
38
    REVNO_COL,
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
39
    HIGHLIGHT_COLOR_COL,
0.1.1 by Dan Loda
First working version of xannotate.
40
    TEXT_LINE_COL
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
41
) = range(6)
0.1.1 by Dan Loda
First working version of xannotate.
42
43
0.1.2 by Dan Loda
Rename plugin: xannotate => gannotate
44
class GAnnotateWindow(gtk.Window):
0.1.1 by Dan Loda
First working version of xannotate.
45
    """Annotate window."""
46
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
47
    def __init__(self, all=False, plain=False):
48
        self.all = all
49
        self.plain = plain
50
        
0.1.1 by Dan Loda
First working version of xannotate.
51
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
52
        
0.1.1 by Dan Loda
First working version of xannotate.
53
        self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
54
        self.annotate_colormap = AnnotateColorSaturation()
0.1.1 by Dan Loda
First working version of xannotate.
55
56
        self._create()
57
        self.revisions = {}
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
58
        self.history = []
173.1.1 by Aaron Bentley
Better behavior when unable to go back
59
        self._no_back = set()
0.1.17 by Dan Loda
A little refactoring.
60
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
61
    def annotate(self, tree, branch, file_id):
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
62
        self.annotations = []
63
        self.branch = branch
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
64
        self.tree = tree
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
65
        self.file_id = file_id
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
66
        self.revision_id = getattr(tree, 'get_revision_id', 
67
                                   lambda: CURRENT_REVISION)()
0.1.1 by Dan Loda
First working version of xannotate.
68
        
259 by Aaron Bentley
Add author support to gannotate and log viewer
69
        # [revision id, line number, author, revno, highlight color, line]
0.1.17 by Dan Loda
A little refactoring.
70
        self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
71
                                       gobject.TYPE_STRING,
72
                                       gobject.TYPE_STRING,
73
                                       gobject.TYPE_STRING,
74
                                       gobject.TYPE_STRING,
75
                                       gobject.TYPE_STRING)
0.1.1 by Dan Loda
First working version of xannotate.
76
        
77
        last_seen = None
0.4.1 by Aaron Bentley
Updated performance, API use
78
        try:
79
            branch.lock_read()
80
            branch.repository.lock_read()
81
            for line_no, (revision, revno, line)\
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
82
                    in enumerate(self._annotate(tree, file_id)):
0.4.1 by Aaron Bentley
Updated performance, API use
83
                if revision.revision_id == last_seen and not self.all:
273 by Aaron Bentley
Use get_apparent_author, rename variables to 'author'
84
                    revno = author = ""
0.4.1 by Aaron Bentley
Updated performance, API use
85
                else:
86
                    last_seen = revision.revision_id
273 by Aaron Bentley
Use get_apparent_author, rename variables to 'author'
87
                    author = revision.get_apparent_author()
0.4.1 by Aaron Bentley
Updated performance, API use
88
89
                if revision.revision_id not in self.revisions:
90
                    self.revisions[revision.revision_id] = revision
91
92
                self.annomodel.append([revision.revision_id,
93
                                       line_no + 1,
273 by Aaron Bentley
Use get_apparent_author, rename variables to 'author'
94
                                       author,
0.4.1 by Aaron Bentley
Updated performance, API use
95
                                       revno,
96
                                       None,
97
                                       line.rstrip("\r\n")
98
                                      ])
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
99
                self.annotations.append(revision)
0.4.1 by Aaron Bentley
Updated performance, API use
100
101
            if not self.plain:
66.6.1 by Aaron Bentley
Remove usused span selector
102
                now = time.time()
103
                self.annomodel.foreach(self._highlight_annotation, now)
0.4.1 by Aaron Bentley
Updated performance, API use
104
        finally:
105
            branch.repository.unlock()
106
            branch.unlock()
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
107
0.1.17 by Dan Loda
A little refactoring.
108
        self.annoview.set_model(self.annomodel)
109
        self.annoview.grab_focus()
0.1.1 by Dan Loda
First working version of xannotate.
110
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
111
    def jump_to_line(self, lineno):
0.1.17 by Dan Loda
A little refactoring.
112
        if lineno > len(self.annomodel) or lineno < 1:
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
113
            row = 0
114
            # FIXME:should really deal with this in the gui. Perhaps a status
115
            # bar?
116
            print("gannotate: Line number %d does't exist. Defaulting to "
117
                  "line 1." % lineno)
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
118
	    return
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
119
        else:
120
            row = lineno - 1
121
0.1.17 by Dan Loda
A little refactoring.
122
        self.annoview.set_cursor(row)
59.2.1 by Aaron Bentley
Gannotate takes a line number
123
        self.annoview.scroll_to_cell(row, use_align=True)
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
124
66.2.4 by Aaron Bentley
Use dotted revnos instead of 'merge' where possible
125
    def _dotted_revnos(self, repository, revision_id):
126
        """Return a dict of revision_id -> dotted revno
127
        
128
        :param repository: The repository to get the graph from
129
        :param revision_id: The last revision for which this info is needed
130
        """
131
        graph = repository.get_revision_graph(revision_id)
132
        dotted = {}
133
        for n, revision_id, d, revno, e in tsort.merge_sort(graph, 
134
            revision_id, generate_revno=True):
135
            dotted[revision_id] = '.'.join(str(num) for num in revno)
136
        return dotted
137
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
138
    def _annotate(self, tree, file_id):
139
        current_revision = FakeRevision(CURRENT_REVISION)
140
        current_revision.committer = self.branch.get_config().username()
141
        current_revision.timestamp = time.time()
142
        current_revision.message = '[Not yet committed]'
66.6.6 by Aaron Bentley
Support scrolling based on an offset
143
        current_revision.parent_ids = tree.get_parent_ids()
157.1.7 by Aaron Bentley
Fix branch-nick handling
144
        current_revision.properties['branch-nick'] = self.branch.nick
66.2.15 by Aaron Bentley
fix future revno
145
        current_revno = '%d?' % (self.branch.revno() + 1)
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
146
        repository = self.branch.repository
147
        if self.revision_id == CURRENT_REVISION:
148
            revision_id = self.branch.last_revision()
149
        else:
150
            revision_id = self.revision_id
66.2.4 by Aaron Bentley
Use dotted revnos instead of 'merge' where possible
151
        dotted = self._dotted_revnos(repository, revision_id)
66.6.5 by Aaron Bentley
Speed up the 'back' operation
152
        revision_cache = RevisionCache(repository, self.revisions)
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
153
        for origin, text in tree.annotate_iter(file_id):
0.4.1 by Aaron Bentley
Updated performance, API use
154
            rev_id = origin
66.6.7 by Aaron Bentley
Handle current revision better
155
            if rev_id == CURRENT_REVISION:
156
                revision = current_revision
157
                revno = current_revno
158
            else:
159
                try:
160
                    revision = revision_cache.get_revision(rev_id)
161
                    revno = dotted.get(rev_id, 'merge')
162
                    if len(revno) > 15:
163
                        revno = 'merge'
164
                except NoSuchRevision:
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
165
                    revision = FakeRevision(rev_id)
166
                    revno = "?"
0.1.1 by Dan Loda
First working version of xannotate.
167
168
            yield revision, revno, text
169
0.2.2 by Dan Loda
Add file's age as default span
170
    def _highlight_annotation(self, model, path, iter, now):
171
        revision_id, = model.get(iter, REVISION_ID_COL)
172
        revision = self.revisions[revision_id]
0.2.5 by Dan Loda
Use granny-like colors as default and rename ColorMap => AnnotateColorMap.
173
        model.set(iter, HIGHLIGHT_COLOR_COL,
0.1.19 by Aaron Bentley
Different colours for different committers
174
                  self.annotate_colormap.get_color(revision, now))
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
175
66.6.4 by Aaron Bentley
Add back button to see older versions
176
    def _selected_revision(self):
0.1.17 by Dan Loda
A little refactoring.
177
        (path, col) = self.annoview.get_cursor()
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
178
        if path is None:
66.6.4 by Aaron Bentley
Add back button to see older versions
179
            return None
180
        return self.annomodel[path][REVISION_ID_COL]
181
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
182
    def _activate_selected_revision(self, w):
66.6.4 by Aaron Bentley
Add back button to see older versions
183
        rev_id = self._selected_revision()
184
        if rev_id is None:
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
185
            return
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
186
        selected = self.revisions[rev_id]
187
        self.logview.set_revision(selected)
173.1.1 by Aaron Bentley
Better behavior when unable to go back
188
        if (len(selected.parent_ids) != 0 and selected.parent_ids[0] not in
189
            self._no_back):
190
            enable_back = True
191
        else:
192
            enable_back = False
193
        self.back_button.set_sensitive(enable_back)
0.1.1 by Dan Loda
First working version of xannotate.
194
195
    def _create(self):
0.1.17 by Dan Loda
A little refactoring.
196
        self.logview = self._create_log_view()
197
        self.annoview = self._create_annotate_view()
198
170.1.6 by Aaron Bentley
Move buttons to top, tweak layout
199
        vbox = gtk.VBox(False)
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
200
        vbox.show()
0.1.17 by Dan Loda
A little refactoring.
201
202
        sw = gtk.ScrolledWindow()
203
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
204
        sw.set_shadow_type(gtk.SHADOW_IN)
205
        sw.add(self.annoview)
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
206
        self.annoview.gwindow = self
0.1.17 by Dan Loda
A little refactoring.
207
        sw.show()
170.1.4 by Aaron Bentley
Move search fields directly below source window
208
209
        swbox = gtk.VBox()
210
        swbox.pack_start(sw)
211
        swbox.show()
170.1.6 by Aaron Bentley
Move buttons to top, tweak layout
212
213
        hbox = gtk.HBox(False, 6)
214
        self.back_button = self._create_back_button()
215
        hbox.pack_start(self.back_button, expand=False, fill=True)
216
        self.forward_button = self._create_forward_button()
217
        hbox.pack_start(self.forward_button, expand=False, fill=True)
218
        hbox.show()
219
        vbox.pack_start(hbox, expand=False, fill=True)
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
220
        
0.1.8 by Dan Loda
Remember window state. This introduces the gannotate.conf configuration file,
221
        self.pane = pane = gtk.VPaned()
170.1.4 by Aaron Bentley
Move search fields directly below source window
222
        pane.add1(swbox)
0.1.17 by Dan Loda
A little refactoring.
223
        pane.add2(self.logview)
0.1.1 by Dan Loda
First working version of xannotate.
224
        pane.show()
225
        vbox.pack_start(pane, expand=True, fill=True)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
226
227
        self._search = SearchBox()
170.1.4 by Aaron Bentley
Move search fields directly below source window
228
        swbox.pack_start(self._search, expand=False, fill=True)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
229
        accels = gtk.AccelGroup()
230
        accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
231
                             gtk.ACCEL_LOCKED,
232
                             self._search_by_text)
233
        accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
234
                             gtk.ACCEL_LOCKED,
235
                             self._search_by_line)
236
        self.add_accel_group(accels)
237
0.1.1 by Dan Loda
First working version of xannotate.
238
        self.add(vbox)
239
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
240
    def _search_by_text(self, accel_group, window, key, modifiers):
241
        self._search.show_for('text')
66.5.3 by v.ladeuil+lp at free
Realbetter fix for bug #73965.
242
        self._search.set_target(self.annoview, TEXT_LINE_COL)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
243
244
    def _search_by_line(self, accel_group, window, key, modifiers):
245
        self._search.show_for('line')
66.5.3 by v.ladeuil+lp at free
Realbetter fix for bug #73965.
246
        self._search.set_target(self.annoview, LINE_NUM_COL)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
247
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
248
    def row_diff(self, tv, path, tvc):
249
        row = path[0]
250
        revision = self.annotations[row]
65 by Aaron Bentley
Handle first revision properly
251
        repository = self.branch.repository
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
252
        if revision.revision_id == CURRENT_REVISION:
253
            tree1 = self.tree
254
            tree2 = self.tree.basis_tree()
65 by Aaron Bentley
Handle first revision properly
255
        else:
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
256
            tree1 = repository.revision_tree(revision.revision_id)
257
            if len(revision.parent_ids) > 0:
258
                tree2 = repository.revision_tree(revision.parent_ids[0])
259
            else:
260
                tree2 = repository.revision_tree(NULL_REVISION)
150 by Jelmer Vernooij
Fix handling showing diffs of working tree changes.
261
        from bzrlib.plugins.gtk.diff import DiffWindow
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
262
        window = DiffWindow()
66 by Aaron Bentley
Fix annotate diff window title
263
        window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
264
        window.set_file(tree1.id2path(self.file_id))
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
265
        window.show()
266
267
0.1.1 by Dan Loda
First working version of xannotate.
268
    def _create_annotate_view(self):
0.1.17 by Dan Loda
A little refactoring.
269
        tv = gtk.TreeView()
270
        tv.set_rules_hint(False)
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
271
        tv.connect("cursor-changed", self._activate_selected_revision)
0.1.17 by Dan Loda
A little refactoring.
272
        tv.show()
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
273
        tv.connect("row-activated", self.row_diff)
0.1.1 by Dan Loda
First working version of xannotate.
274
275
        cell = gtk.CellRendererText()
276
        cell.set_property("xalign", 1.0)
277
        cell.set_property("ypad", 0)
278
        cell.set_property("family", "Monospace")
279
        cell.set_property("cell-background-gdk",
0.1.17 by Dan Loda
A little refactoring.
280
                          tv.get_style().bg[gtk.STATE_NORMAL])
0.1.1 by Dan Loda
First working version of xannotate.
281
        col = gtk.TreeViewColumn()
282
        col.set_resizable(False)
283
        col.pack_start(cell, expand=True)
284
        col.add_attribute(cell, "text", LINE_NUM_COL)
0.1.17 by Dan Loda
A little refactoring.
285
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
286
287
        cell = gtk.CellRendererText()
288
        cell.set_property("ypad", 0)
289
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
290
        cell.set_property("cell-background-gdk",
291
                          self.get_style().bg[gtk.STATE_NORMAL])
292
        col = gtk.TreeViewColumn("Committer")
293
        col.set_resizable(True)
294
        col.pack_start(cell, expand=True)
295
        col.add_attribute(cell, "text", COMMITTER_COL)
0.1.17 by Dan Loda
A little refactoring.
296
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
297
298
        cell = gtk.CellRendererText()
299
        cell.set_property("xalign", 1.0)
300
        cell.set_property("ypad", 0)
301
        cell.set_property("cell-background-gdk",
302
                          self.get_style().bg[gtk.STATE_NORMAL])
303
        col = gtk.TreeViewColumn("Revno")
304
        col.set_resizable(False)
305
        col.pack_start(cell, expand=True)
306
        col.add_attribute(cell, "markup", REVNO_COL)
0.1.17 by Dan Loda
A little refactoring.
307
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
308
309
        cell = gtk.CellRendererText()
310
        cell.set_property("ypad", 0)
311
        cell.set_property("family", "Monospace")
312
        col = gtk.TreeViewColumn()
313
        col.set_resizable(False)
314
        col.pack_start(cell, expand=True)
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
315
#        col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
316
        col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
0.1.1 by Dan Loda
First working version of xannotate.
317
        col.add_attribute(cell, "text", TEXT_LINE_COL)
0.1.17 by Dan Loda
A little refactoring.
318
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
319
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
320
        # FIXME: Now that C-f is now used for search by text we
321
        # may as well disable the auto search.
322
        tv.set_search_column(LINE_NUM_COL)
66.5.1 by v.ladeuil+lp at free
Minimal fix for bug #73965.
323
0.1.17 by Dan Loda
A little refactoring.
324
        return tv
0.1.1 by Dan Loda
First working version of xannotate.
325
326
    def _create_log_view(self):
0.1.17 by Dan Loda
A little refactoring.
327
        lv = LogView()
328
        lv.show()
329
        return lv
0.1.1 by Dan Loda
First working version of xannotate.
330
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
331
    def _create_back_button(self):
66.6.4 by Aaron Bentley
Add back button to see older versions
332
        button = gtk.Button()
333
        button.set_use_stock(True)
334
        button.set_label("gtk-go-back")
335
        button.connect("clicked", lambda w: self.go_back())
170.1.6 by Aaron Bentley
Move buttons to top, tweak layout
336
        button.set_relief(gtk.RELIEF_NONE)
66.6.4 by Aaron Bentley
Add back button to see older versions
337
        button.show()
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
338
        return button
339
340
    def _create_forward_button(self):
341
        button = gtk.Button()
342
        button.set_use_stock(True)
343
        button.set_label("gtk-go-forward")
344
        button.connect("clicked", lambda w: self.go_forward())
170.1.6 by Aaron Bentley
Move buttons to top, tweak layout
345
        button.set_relief(gtk.RELIEF_NONE)
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
346
        button.show()
347
        button.set_sensitive(False)
348
        return button
66.6.4 by Aaron Bentley
Add back button to see older versions
349
350
    def go_back(self):
173.1.1 by Aaron Bentley
Better behavior when unable to go back
351
        last_tree = self.tree
66.6.4 by Aaron Bentley
Add back button to see older versions
352
        rev_id = self._selected_revision()
353
        parent_id = self.revisions[rev_id].parent_ids[0]
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
354
        target_tree = self.branch.repository.revision_tree(parent_id)
173.1.1 by Aaron Bentley
Better behavior when unable to go back
355
        if self._go(target_tree):
356
            self.history.append(last_tree)
357
            self.forward_button.set_sensitive(True)
358
        else:
359
            self._no_back.add(parent_id)
360
            self.back_button.set_sensitive(False)
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
361
362
    def go_forward(self):
363
        if len(self.history) == 0:
364
            return
365
        target_tree = self.history.pop()
366
        if len(self.history) == 0:
367
            self.forward_button.set_sensitive(False)
368
        self._go(target_tree)
369
370
    def _go(self, target_tree):
371
        rev_id = self._selected_revision()
372
        if self.file_id in target_tree:
373
            offset = self.get_scroll_offset(target_tree)
66.6.6 by Aaron Bentley
Support scrolling based on an offset
374
            (row,), col = self.annoview.get_cursor()
170.1.5 by Aaron Bentley
Add 'forward' button, much button cleanup
375
            self.annotate(target_tree, self.branch, self.file_id)
376
            new_row = row+offset
377
            if new_row < 0:
378
                new_row = 0
379
            self.annoview.set_cursor(new_row)
173.1.1 by Aaron Bentley
Better behavior when unable to go back
380
            return True
381
        else:
382
            return False
66.6.6 by Aaron Bentley
Support scrolling based on an offset
383
384
    def get_scroll_offset(self, tree):
385
        old = self.tree.get_file(self.file_id)
386
        new = tree.get_file(self.file_id)
387
        (row,), col = self.annoview.get_cursor()
388
        matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
389
                                                       new.readlines())
390
        for i, j, n in matcher.get_matching_blocks():
391
            if i + n >= row:
392
                return j - i
393
66.6.4 by Aaron Bentley
Add back button to see older versions
394
0.1.1 by Dan Loda
First working version of xannotate.
395
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
396
class FakeRevision:
0.1.1 by Dan Loda
First working version of xannotate.
397
    """ A fake revision.
398
399
    For when a revision is referenced but not present.
400
    """
401
157.1.7 by Aaron Bentley
Fix branch-nick handling
402
    def __init__(self, revision_id, committer='?', nick=None):
0.1.1 by Dan Loda
First working version of xannotate.
403
        self.revision_id = revision_id
404
        self.parent_ids = []
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
405
        self.committer = committer
0.1.1 by Dan Loda
First working version of xannotate.
406
        self.message = "?"
407
        self.timestamp = 0.0
408
        self.timezone = 0
157.1.7 by Aaron Bentley
Fix branch-nick handling
409
        self.properties = {}
0.1.1 by Dan Loda
First working version of xannotate.
410
273 by Aaron Bentley
Use get_apparent_author, rename variables to 'author'
411
    def get_apparent_author(self):
412
        return self.committer
413
0.1.23 by Aaron Bentley
Added revision caching to initial annotation
414
415
class RevisionCache(object):
416
    """A caching revision source"""
66.6.5 by Aaron Bentley
Speed up the 'back' operation
417
    def __init__(self, real_source, seed_cache=None):
0.1.23 by Aaron Bentley
Added revision caching to initial annotation
418
        self.__real_source = real_source
66.6.5 by Aaron Bentley
Speed up the 'back' operation
419
        if seed_cache is None:
420
            self.__cache = {}
421
        else:
422
            self.__cache = dict(seed_cache)
0.1.23 by Aaron Bentley
Added revision caching to initial annotation
423
424
    def get_revision(self, revision_id):
425
        if revision_id not in self.__cache:
426
            revision = self.__real_source.get_revision(revision_id)
427
            self.__cache[revision_id] = revision
428
        return self.__cache[revision_id]
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
429
430
class SearchBox(gtk.HBox):
431
    """A button box for searching in text or lines of annotations"""
432
    def __init__(self):
433
        gtk.HBox.__init__(self, False, 6)
434
435
        # Close button
436
        button = gtk.Button()
437
        image = gtk.Image()
438
        image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
439
        button.set_image(image)
440
        button.set_relief(gtk.RELIEF_NONE)
441
        button.connect("clicked", lambda w: self.hide_all())
442
        self.pack_start(button, expand=False, fill=False)
443
444
        # Search entry
445
        label = gtk.Label()
446
        self._label = label
447
        self.pack_start(label, expand=False, fill=False)
448
449
        entry = gtk.Entry()
450
        self._entry = entry
451
        entry.connect("activate", lambda w, d: self._do_search(d),
452
                      'forward')
453
        self.pack_start(entry, expand=False, fill=False)
454
455
        # Next/previous buttons
456
        button = gtk.Button('_Next')
457
        image = gtk.Image()
458
        image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
459
        button.set_image(image)
460
        button.connect("clicked", lambda w, d: self._do_search(d),
461
                       'forward')
462
        self.pack_start(button, expand=False, fill=False)
463
464
        button = gtk.Button('_Previous')
465
        image = gtk.Image()
466
        image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
467
        button.set_image(image)
468
        button.connect("clicked", lambda w, d: self._do_search(d),
469
                       'backward')
470
        self.pack_start(button, expand=False, fill=False)
471
472
        # Search options
473
        check = gtk.CheckButton('Match case')
474
        self._match_case = check
475
        self.pack_start(check, expand=False, fill=False)
476
477
        check = gtk.CheckButton('Regexp')
478
        check.connect("toggled", lambda w: self._set_label())
479
        self._regexp = check
480
        self.pack_start(check, expand=False, fill=False)
481
482
        self._view = None
483
        self._column = None
484
        # Note that we stay hidden (we do not call self.show_all())
485
486
487
    def show_for(self, kind):
488
        self._kind = kind
489
        self.show_all()
490
        self._set_label()
491
        # Hide unrelated buttons
492
        if kind == 'line':
493
            self._match_case.hide()
494
            self._regexp.hide()
495
        # Be ready
496
        self._entry.grab_focus()
497
498
    def _set_label(self):
499
        if self._kind == 'line':
500
            self._label.set_text('Find Line: ')
501
        else:
502
            if self._regexp.get_active():
503
                self._label.set_text('Find Regexp: ')
504
            else:
505
                self._label.set_text('Find Text: ')
506
507
    def set_target(self, view,column):
508
        self._view = view
509
        self._column = column
510
511
    def _match(self, model, iterator, column):
512
        matching_case = self._match_case.get_active()
513
        string, = model.get(iterator, column)
514
        key = self._entry.get_text()
515
        if self._regexp.get_active():
516
            if matching_case:
517
                match = re.compile(key).search(string, 1)
518
            else:
519
                match = re.compile(key, re.I).search(string, 1)
520
        else:
521
            if not matching_case:
522
                string = string.lower()
523
                key = key.lower()
524
            match = string.find(key) != -1
525
526
        return match
527
528
    def _iterate_rows_forward(self, model, start):
529
        model_size = len(model)
530
        current = start + 1
531
        while model_size != 0:
532
            if current >= model_size: current =  0
533
            yield model.get_iter_from_string('%d' % current)
534
            if current == start: raise StopIteration
535
            current += 1
536
537
    def _iterate_rows_backward(self, model, start):
538
        model_size = len(model)
539
        current = start - 1
540
        while model_size != 0:
541
            if current < 0: current = model_size - 1
542
            yield model.get_iter_from_string('%d' % current)
543
            if current == start: raise StopIteration
544
            current -= 1
545
546
    def _do_search(self, direction):
547
        if direction == 'forward':
548
            iterate = self._iterate_rows_forward
549
        else:
550
            iterate = self._iterate_rows_backward
551
552
        model, sel = self._view.get_selection().get_selected()
553
        if sel is None:
554
            start = 0
555
        else:
556
            path = model.get_string_from_iter(sel)
557
            start = int(path)
558
559
        for row in iterate(model, start):
560
            if self._match(model, row, self._column):
561
                path = model.get_path(row)
562
                self._view.set_cursor(path)
563
                self._view.scroll_to_cell(path, use_align=True)
564
                break