2009-08-18 12:43:18 +00:00
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""searchbar.py - classes necessary to provide a terminal search bar"""
|
|
|
|
|
2020-04-27 20:03:11 +00:00
|
|
|
import gi
|
2015-06-21 02:23:52 +00:00
|
|
|
from gi.repository import Gtk, Gdk
|
2020-04-27 20:03:11 +00:00
|
|
|
gi.require_version('Vte', '2.91') # vte-0.38 (gnome-3.14)
|
|
|
|
from gi.repository import Vte
|
2014-09-19 14:08:08 +00:00
|
|
|
from gi.repository import GObject
|
2017-07-25 16:34:31 +00:00
|
|
|
from gi.repository import GLib
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2018-04-24 18:22:10 +00:00
|
|
|
from .translation import _
|
|
|
|
from .config import Config
|
2020-04-27 20:03:11 +00:00
|
|
|
from . import regex
|
2020-06-02 01:00:13 +00:00
|
|
|
from .util import dbg
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2009-08-18 12:51:09 +00:00
|
|
|
# pylint: disable-msg=R0904
|
2014-09-19 14:08:08 +00:00
|
|
|
class Searchbar(Gtk.HBox):
|
2009-08-18 12:43:18 +00:00
|
|
|
"""Class implementing the Searchbar widget"""
|
|
|
|
|
|
|
|
__gsignals__ = {
|
2014-09-19 14:08:08 +00:00
|
|
|
'end-search': (GObject.SignalFlags.RUN_LAST, None, ()),
|
2009-08-18 12:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry = None
|
|
|
|
next = None
|
2009-11-04 18:06:34 +00:00
|
|
|
prev = None
|
2015-06-21 15:57:14 +00:00
|
|
|
wrap = None
|
2009-11-04 18:06:34 +00:00
|
|
|
|
|
|
|
vte = None
|
|
|
|
config = None
|
|
|
|
|
|
|
|
searchstring = None
|
2011-03-22 07:05:38 +00:00
|
|
|
searchre = None
|
2009-08-18 12:43:18 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
2014-09-19 14:08:08 +00:00
|
|
|
GObject.GObject.__init__(self)
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2020-06-22 19:24:41 +00:00
|
|
|
# default regex flags are not CASELESS
|
|
|
|
self.regex_flags_pcre2 = regex.FLAGS_PCRE2
|
|
|
|
self.regex_flags_glib = regex.FLAGS_GLIB
|
|
|
|
|
2009-11-04 18:06:34 +00:00
|
|
|
self.config = Config()
|
|
|
|
|
2016-12-08 02:22:59 +00:00
|
|
|
self.get_style_context().add_class("terminator-terminal-searchbar")
|
|
|
|
|
2009-08-18 12:43:18 +00:00
|
|
|
# Search text
|
2014-09-19 14:08:08 +00:00
|
|
|
self.entry = Gtk.Entry()
|
2009-08-18 12:43:18 +00:00
|
|
|
self.entry.set_activates_default(True)
|
|
|
|
self.entry.show()
|
|
|
|
self.entry.connect('activate', self.do_search)
|
|
|
|
self.entry.connect('key-press-event', self.search_keypress)
|
|
|
|
|
|
|
|
# Label
|
2014-09-19 14:08:08 +00:00
|
|
|
label = Gtk.Label(label=_('Search:'))
|
2009-08-18 12:43:18 +00:00
|
|
|
label.show()
|
|
|
|
|
|
|
|
# Close Button
|
2014-09-19 14:08:08 +00:00
|
|
|
close = Gtk.Button()
|
|
|
|
close.set_relief(Gtk.ReliefStyle.NONE)
|
2009-08-18 12:43:18 +00:00
|
|
|
close.set_focus_on_click(False)
|
2014-09-19 14:08:08 +00:00
|
|
|
icon = Gtk.Image()
|
|
|
|
icon.set_from_stock(Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)
|
2009-08-18 12:43:18 +00:00
|
|
|
close.add(icon)
|
|
|
|
close.set_name('terminator-search-close-button')
|
|
|
|
if hasattr(close, 'set_tooltip_text'):
|
|
|
|
close.set_tooltip_text(_('Close Search bar'))
|
|
|
|
close.connect('clicked', self.end_search)
|
|
|
|
close.show_all()
|
|
|
|
|
|
|
|
# Next Button
|
2020-06-22 19:24:41 +00:00
|
|
|
self.next = Gtk.Button.new_with_label('Next')
|
2009-11-04 20:41:01 +00:00
|
|
|
self.next.show()
|
|
|
|
self.next.set_sensitive(False)
|
2009-08-18 12:43:18 +00:00
|
|
|
self.next.connect('clicked', self.next_search)
|
|
|
|
|
2009-11-04 18:06:34 +00:00
|
|
|
# Previous Button
|
2020-06-22 19:24:41 +00:00
|
|
|
self.prev = Gtk.Button.new_with_label('Prev')
|
2009-11-04 20:41:01 +00:00
|
|
|
self.prev.show()
|
|
|
|
self.prev.set_sensitive(False)
|
2009-11-04 18:06:34 +00:00
|
|
|
self.prev.connect('clicked', self.prev_search)
|
|
|
|
|
2020-06-22 19:24:41 +00:00
|
|
|
# Match Case checkbox
|
|
|
|
self.match_case = Gtk.CheckButton.new_with_label('Match Case')
|
|
|
|
self.match_case.show()
|
|
|
|
self.match_case.set_sensitive(True)
|
2020-10-18 02:04:00 +00:00
|
|
|
self.match_case.set_active(self.config.base.get_item('case_sensitive'))
|
2020-06-22 19:24:41 +00:00
|
|
|
self.match_case.connect('toggled', self.match_case_toggled)
|
|
|
|
|
2015-06-21 15:57:14 +00:00
|
|
|
# Wrap checkbox
|
2020-06-22 19:24:41 +00:00
|
|
|
self.wrap = Gtk.CheckButton.new_with_label('Wrap')
|
2015-06-21 15:57:14 +00:00
|
|
|
self.wrap.show()
|
|
|
|
self.wrap.set_sensitive(True)
|
2020-06-22 19:24:41 +00:00
|
|
|
self.wrap.set_active(True)
|
2015-06-21 15:57:14 +00:00
|
|
|
self.wrap.connect('toggled', self.wrap_toggled)
|
|
|
|
|
2020-10-20 13:21:12 +00:00
|
|
|
# Invert Search checkbox
|
|
|
|
self.invert_search = Gtk.CheckButton.new_with_label('Invert Search')
|
|
|
|
self.invert_search.show()
|
|
|
|
self.search_is_inverted = self.config.base.get_item('invert_search')
|
|
|
|
self.invert_search.set_active(self.search_is_inverted)
|
|
|
|
self.invert_search.connect('toggled', self.wrap_invert_search)
|
|
|
|
|
2014-09-19 14:10:43 +00:00
|
|
|
self.pack_start(label, False, True, 0)
|
2014-09-19 14:08:08 +00:00
|
|
|
self.pack_start(self.entry, True, True, 0)
|
2014-09-19 14:10:43 +00:00
|
|
|
self.pack_start(self.prev, False, False, 0)
|
|
|
|
self.pack_start(self.next, False, False, 0)
|
2015-06-21 15:57:14 +00:00
|
|
|
self.pack_start(self.wrap, False, False, 0)
|
2020-06-22 19:24:41 +00:00
|
|
|
self.pack_start(self.match_case, False, False, 0)
|
2020-10-20 13:21:12 +00:00
|
|
|
self.pack_start(self.invert_search, False, False, 0)
|
2014-09-19 14:10:43 +00:00
|
|
|
self.pack_end(close, False, False, 0)
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2009-08-19 18:00:33 +00:00
|
|
|
self.hide()
|
2009-11-07 01:40:43 +00:00
|
|
|
self.set_no_show_all(True)
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2020-10-20 13:21:12 +00:00
|
|
|
def wrap_invert_search(self, toggled):
|
|
|
|
self.search_is_inverted = toggled.get_active()
|
|
|
|
self.invert_search.set_active(toggled.get_active())
|
|
|
|
self.config.base.set_item('invert_search', toggled.get_active())
|
|
|
|
self.config.save()
|
|
|
|
|
2015-06-21 15:57:14 +00:00
|
|
|
def wrap_toggled(self, toggled):
|
2017-07-25 16:34:31 +00:00
|
|
|
toggled_state = toggled.get_active()
|
|
|
|
self.vte.search_set_wrap_around(toggled_state)
|
|
|
|
if toggled_state:
|
2015-06-21 15:57:14 +00:00
|
|
|
self.prev.set_sensitive(True)
|
|
|
|
self.next.set_sensitive(True)
|
|
|
|
|
2020-06-22 19:24:41 +00:00
|
|
|
def match_case_toggled(self, toggled):
|
|
|
|
"""Handles Match Case checkbox toggles"""
|
|
|
|
|
|
|
|
toggled_state = toggled.get_active()
|
|
|
|
if not toggled_state:
|
|
|
|
# Add the CASELESS regex flags when the checkbox is not checked.
|
2020-06-24 14:16:58 +00:00
|
|
|
try:
|
|
|
|
self.regex_flags_pcre2 = (regex.FLAGS_PCRE2 | regex.PCRE2_CASELESS)
|
|
|
|
except TypeError:
|
|
|
|
# if PCRE2 support is not available
|
|
|
|
pass
|
|
|
|
|
|
|
|
# The code will fall back to use this GLib regex when PCRE2 is not available
|
2020-06-22 19:24:41 +00:00
|
|
|
self.regex_flags_glib = (regex.FLAGS_GLIB | regex.GLIB_CASELESS)
|
|
|
|
else:
|
|
|
|
# Default state of the check box is unchecked. CASELESS regex flags are not added.
|
|
|
|
self.regex_flags_pcre2 = regex.FLAGS_PCRE2
|
|
|
|
self.regex_flags_glib = regex.FLAGS_GLIB
|
|
|
|
|
2020-10-17 21:51:19 +00:00
|
|
|
self.config.base.set_item('case_sensitive', toggled_state)
|
|
|
|
self.config.save()
|
2022-10-14 13:35:55 +00:00
|
|
|
self.do_search(self.entry) # Start a new search every time the check box is toggled.
|
2020-06-22 19:24:41 +00:00
|
|
|
|
2009-11-04 18:06:34 +00:00
|
|
|
def get_vte(self):
|
|
|
|
"""Find our parent widget"""
|
|
|
|
parent = self.get_parent()
|
|
|
|
if parent:
|
|
|
|
self.vte = parent.vte
|
2020-06-22 19:24:41 +00:00
|
|
|
#turn on wrap by default
|
|
|
|
self.vte.search_set_wrap_around(True)
|
2009-11-04 18:06:34 +00:00
|
|
|
|
2009-08-18 12:51:09 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-08-18 12:43:18 +00:00
|
|
|
def search_keypress(self, widget, event):
|
2009-08-18 12:48:04 +00:00
|
|
|
"""Handle keypress events"""
|
2014-09-19 14:08:08 +00:00
|
|
|
key = Gdk.keyval_name(event.keyval)
|
2009-08-18 12:48:04 +00:00
|
|
|
if key == 'Escape':
|
|
|
|
self.end_search()
|
2015-06-21 15:57:14 +00:00
|
|
|
else:
|
|
|
|
self.prev.set_sensitive(False)
|
|
|
|
self.next.set_sensitive(False)
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2009-11-04 18:06:34 +00:00
|
|
|
def start_search(self):
|
|
|
|
"""Show ourselves"""
|
|
|
|
if not self.vte:
|
|
|
|
self.get_vte()
|
|
|
|
|
|
|
|
self.show()
|
|
|
|
self.entry.grab_focus()
|
|
|
|
|
2009-08-18 12:43:18 +00:00
|
|
|
def do_search(self, widget):
|
|
|
|
"""Trap and re-emit the clicked signal"""
|
2020-06-02 01:00:13 +00:00
|
|
|
dbg('entered do_search')
|
2009-11-04 18:06:34 +00:00
|
|
|
searchtext = self.entry.get_text()
|
2020-06-02 01:00:13 +00:00
|
|
|
dbg('searchtext: %s' % searchtext)
|
2009-11-04 18:06:34 +00:00
|
|
|
if searchtext == '':
|
|
|
|
return
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2020-06-22 19:24:41 +00:00
|
|
|
self.searchre = None
|
|
|
|
if regex.FLAGS_PCRE2:
|
|
|
|
try:
|
|
|
|
self.searchre = Vte.Regex.new_for_search(searchtext, len(searchtext), self.regex_flags_pcre2)
|
2020-06-02 01:00:13 +00:00
|
|
|
dbg('search RE: %s' % self.searchre)
|
2020-06-22 19:24:41 +00:00
|
|
|
self.vte.search_set_regex(self.searchre, 0)
|
|
|
|
except GLib.Error:
|
|
|
|
# happens when PCRE2 support is not builtin (Ubuntu < 19.10)
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not self.searchre:
|
|
|
|
# fall back to old GLib regex
|
|
|
|
self.searchre = GLib.Regex(searchtext, self.regex_flags_glib, 0)
|
|
|
|
dbg('search RE: %s' % self.searchre)
|
|
|
|
self.vte.search_set_gregex(self.searchre, 0)
|
2009-08-18 12:43:18 +00:00
|
|
|
|
2009-11-04 20:41:01 +00:00
|
|
|
self.next.set_sensitive(True)
|
|
|
|
self.prev.set_sensitive(True)
|
2020-10-20 13:21:12 +00:00
|
|
|
# switch search direction based on the inversion checkbox
|
|
|
|
if not self.search_is_inverted:
|
|
|
|
self.next_search(None)
|
|
|
|
else:
|
|
|
|
self.prev_search(None)
|
2009-11-04 18:06:34 +00:00
|
|
|
|
|
|
|
def next_search(self, widget):
|
|
|
|
"""Search forwards and jump to the next result, if any"""
|
2017-07-25 16:34:31 +00:00
|
|
|
found_result = self.vte.search_find_next()
|
|
|
|
if not self.wrap.get_active():
|
|
|
|
self.next.set_sensitive(found_result)
|
|
|
|
else:
|
|
|
|
self.next.set_sensitive(True)
|
|
|
|
self.prev.set_sensitive(True)
|
|
|
|
return
|
2009-11-04 18:06:34 +00:00
|
|
|
|
|
|
|
def prev_search(self, widget):
|
|
|
|
"""Jump back to the previous search"""
|
2017-07-25 16:34:31 +00:00
|
|
|
found_result = self.vte.search_find_previous()
|
|
|
|
if not self.wrap.get_active():
|
|
|
|
self.prev.set_sensitive(found_result)
|
2011-04-08 22:32:52 +00:00
|
|
|
else:
|
2017-07-25 16:34:31 +00:00
|
|
|
self.prev.set_sensitive(True)
|
|
|
|
self.next.set_sensitive(True)
|
|
|
|
return
|
2009-11-04 18:06:34 +00:00
|
|
|
|
|
|
|
def end_search(self, widget=None):
|
2009-08-18 12:43:18 +00:00
|
|
|
"""Trap and re-emit the end-search signal"""
|
2009-11-04 18:06:34 +00:00
|
|
|
self.searchstring = None
|
2011-03-22 07:05:38 +00:00
|
|
|
self.searchre = None
|
2009-11-04 18:06:34 +00:00
|
|
|
self.emit('end-search')
|
2009-08-18 12:43:18 +00:00
|
|
|
|
|
|
|
def get_search_term(self):
|
|
|
|
"""Return the currently set search term"""
|
|
|
|
return(self.entry.get_text())
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
GObject.type_register(Searchbar)
|