(trunk-1552) Merge search bar wrap toggle from Christophe Bourez

This commit is contained in:
Stephen Boddy 2015-06-21 17:57:14 +02:00
parent b549f43834
commit 899916c0fb
1 changed files with 48 additions and 21 deletions

View File

@ -22,6 +22,7 @@ class Searchbar(Gtk.HBox):
reslabel = None reslabel = None
next = None next = None
prev = None prev = None
wrap = None
vte = None vte = None
config = None config = None
@ -78,16 +79,31 @@ class Searchbar(Gtk.HBox):
self.prev.set_sensitive(False) self.prev.set_sensitive(False)
self.prev.connect('clicked', self.prev_search) self.prev.connect('clicked', self.prev_search)
# Wrap checkbox
self.wrap = Gtk.CheckButton(_('Wrap'))
self.wrap.show()
self.wrap.set_sensitive(True)
self.wrap.connect('toggled', self.wrap_toggled)
self.pack_start(label, False, True, 0) self.pack_start(label, False, True, 0)
self.pack_start(self.entry, True, True, 0) self.pack_start(self.entry, True, True, 0)
self.pack_start(self.reslabel, False, True, 0) self.pack_start(self.reslabel, False, True, 0)
self.pack_start(self.prev, False, False, 0) self.pack_start(self.prev, False, False, 0)
self.pack_start(self.next, False, False, 0) self.pack_start(self.next, False, False, 0)
self.pack_start(self.wrap, False, False, 0)
self.pack_end(close, False, False, 0) self.pack_end(close, False, False, 0)
self.hide() self.hide()
self.set_no_show_all(True) self.set_no_show_all(True)
def wrap_toggled(self, toggled):
if self.searchrow is None:
self.prev.set_sensitive(False)
self.next.set_sensitive(False)
elif toggled:
self.prev.set_sensitive(True)
self.next.set_sensitive(True)
def get_vte(self): def get_vte(self):
"""Find our parent widget""" """Find our parent widget"""
parent = self.get_parent() parent = self.get_parent()
@ -100,6 +116,9 @@ class Searchbar(Gtk.HBox):
key = Gdk.keyval_name(event.keyval) key = Gdk.keyval_name(event.keyval)
if key == 'Escape': if key == 'Escape':
self.end_search() self.end_search()
else:
self.prev.set_sensitive(False)
self.next.set_sensitive(False)
def start_search(self): def start_search(self):
"""Show ourselves""" """Show ourselves"""
@ -116,7 +135,7 @@ class Searchbar(Gtk.HBox):
return return
if searchtext != self.searchstring: if searchtext != self.searchstring:
self.searchrow = self.get_vte_buffer_range()[0] self.searchrow = self.get_vte_buffer_range()[0] - 1
self.searchstring = searchtext self.searchstring = searchtext
self.searchre = re.compile(searchtext) self.searchre = re.compile(searchtext)
@ -128,46 +147,54 @@ class Searchbar(Gtk.HBox):
def next_search(self, widget): def next_search(self, widget):
"""Search forwards and jump to the next result, if any""" """Search forwards and jump to the next result, if any"""
startrow,endrow = self.get_vte_buffer_range() startrow,endrow = self.get_vte_buffer_range()
found = startrow <= self.searchrow and self.searchrow < endrow
row = self.searchrow
while True: while True:
if self.searchrow >= endrow: row += 1
self.searchrow = startrow if row >= endrow:
self.reslabel.set_text(_('No more results')) if found and self.wrap.get_active():
return row = startrow - 1
buffer = self.vte.get_text_range(self.searchrow, 0, else:
self.searchrow+1, 0, self.prev.set_sensitive(found)
self.search_character) self.next.set_sensitive(False)
self.reslabel.set_text(_('No more results'))
return
buffer = self.vte.get_text_range(row, 0, row + 1, 0, self.search_character)
buffer = buffer[0] buffer = buffer[0]
buffer = buffer[:buffer.find('\n')] buffer = buffer[:buffer.find('\n')]
matches = self.searchre.search(buffer) matches = self.searchre.search(buffer)
if matches: if matches:
self.searchrow = row
self.prev.set_sensitive(True)
self.search_hit(self.searchrow) self.search_hit(self.searchrow)
self.searchrow += 1
return return
self.searchrow += 1
# FIXME: There is an issue in switching search direction, probably because
# we increment/decrement self.searchrow after each search iteration
def prev_search(self, widget): def prev_search(self, widget):
"""Jump back to the previous search""" """Jump back to the previous search"""
startrow,endrow = self.get_vte_buffer_range() startrow,endrow = self.get_vte_buffer_range()
found = startrow <= self.searchrow and self.searchrow < endrow
row = self.searchrow
while True: while True:
if self.searchrow <= startrow: row -= 1
self.searchrow = endrow if row <= startrow:
self.reslabel.set_text(_('No more results')) if found and self.wrap.get_active():
return row = endrow
buffer = self.vte.get_text_range(self.searchrow, 0, else:
self.searchrow+1, 0, self.next.set_sensitive(found)
self.search_character) self.prev.set_sensitive(False)
self.reslabel.set_text(_('No more results'))
return
buffer = self.vte.get_text_range(row, 0, row + 1, 0, self.search_character)
buffer = buffer[0] buffer = buffer[0]
buffer = buffer[:buffer.find('\n')] buffer = buffer[:buffer.find('\n')]
matches = self.searchre.search(buffer) matches = self.searchre.search(buffer)
if matches: if matches:
self.searchrow = row
self.next.set_sensitive(True)
self.search_hit(self.searchrow) self.search_hit(self.searchrow)
self.searchrow -= 1
return return
self.searchrow -= 1
def search_hit(self, row): def search_hit(self, row):
"""Update the UI for a search hit""" """Update the UI for a search hit"""