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