From 73c91769b26b67d6ef93af419be732abacfb1e2f Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Mon, 27 Apr 2020 22:03:11 +0200 Subject: [PATCH] searchbar: Implement modern/glib regexp support fixes #4 refs #28 --- terminatorlib/regex.py | 16 ++++++++++++++++ terminatorlib/searchbar.py | 20 ++++++++++++++++++-- terminatorlib/terminal.py | 16 ++++------------ 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 terminatorlib/regex.py diff --git a/terminatorlib/regex.py b/terminatorlib/regex.py new file mode 100644 index 00000000..e88c1189 --- /dev/null +++ b/terminatorlib/regex.py @@ -0,0 +1,16 @@ +""" +Utilities for Regexp in VTE +""" + +import gi +gi.require_version('Vte', '2.91') # vte-0.38 (gnome-3.14) +from gi.repository import GLib, Vte + +# constants for vte regex matching +# TODO: Please replace with a proper reference to VTE, I found none! +PCRE2_MULTILINE = 0x00000400 +FLAGS_GLIB = (GLib.RegexCompileFlags.OPTIMIZE | GLib.RegexCompileFlags.MULTILINE) +if hasattr(Vte, 'REGEX_FLAGS_DEFAULT'): + FLAGS_PCRE2 = (Vte.REGEX_FLAGS_DEFAULT | PCRE2_MULTILINE) +else: + FLAGS_PCRE2 = None diff --git a/terminatorlib/searchbar.py b/terminatorlib/searchbar.py index 95132cb2..83c0dbde 100644 --- a/terminatorlib/searchbar.py +++ b/terminatorlib/searchbar.py @@ -2,12 +2,16 @@ # GPL v2 only """searchbar.py - classes necessary to provide a terminal search bar""" +import gi from gi.repository import Gtk, Gdk +gi.require_version('Vte', '2.91') # vte-0.38 (gnome-3.14) +from gi.repository import Vte from gi.repository import GObject from gi.repository import GLib from .translation import _ from .config import Config +from . import regex # pylint: disable-msg=R0904 class Searchbar(Gtk.HBox): @@ -127,8 +131,20 @@ class Searchbar(Gtk.HBox): if searchtext != self.searchstring: self.searchstring = searchtext - self.searchre = GLib.Regex(searchtext, 0, 0) - self.vte.search_set_gregex(self.searchre, 0) + self.searchre = None + + if regex.FLAGS_PCRE2: + try: + self.searchre = Vte.Regex.new_for_search(searchtext, len(searchtext), regex.FLAGS_PCRE2) + 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, regex.FLAGS_GLIB, 0) + self.vte.search_set_gregex(self.searchre, 0) self.next.set_sensitive(True) self.prev.set_sensitive(True) diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index 64a14bdd..2f22dfb9 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -28,15 +28,7 @@ from .translation import _ from .signalman import Signalman from . import plugin from terminatorlib.layoutlauncher import LayoutLauncher - -# constants for vte regex matching -# TODO: Please replace with a proper reference to VTE, I found none! -PCRE2_MULTILINE = 0x00000400 -REGEX_FLAGS_GLIB = (GLib.RegexCompileFlags.OPTIMIZE | GLib.RegexCompileFlags.MULTILINE) -if hasattr(Vte, 'REGEX_FLAGS_DEFAULT'): - REGEX_FLAGS_PCRE2 = (Vte.REGEX_FLAGS_DEFAULT | PCRE2_MULTILINE) -else: - REGEX_FLAGS_PCRE2 = None +from . import regex # pylint: disable-msg=R0904 class Terminal(Gtk.VBox): @@ -266,9 +258,9 @@ class Terminal(Gtk.VBox): def _add_regex(self, name, re): match = -1 - if REGEX_FLAGS_PCRE2: + if regex.FLAGS_PCRE2: try: - reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags or REGEX_FLAGS_PCRE2) + reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags or regex.FLAGS_PCRE2) match = self.vte.match_add_regex(reg, 0) except GLib.Error: # happens when PCRE2 support is not builtin (Ubuntu < 19.10) @@ -276,7 +268,7 @@ class Terminal(Gtk.VBox): # try the "old" glib regex if match < 0: - reg = GLib.Regex.new(re, self.regex_flags or REGEX_FLAGS_GLIB, 0) + reg = GLib.Regex.new(re, self.regex_flags or regex.FLAGS_GLIB, 0) match = self.vte.match_add_gregex(reg, 0) self.matches[name] = match