searchbar: Implement modern/glib regexp support

fixes #4
refs #28
This commit is contained in:
Markus Frosch 2020-04-27 22:03:11 +02:00
parent 11a6a57f51
commit 73c91769b2
3 changed files with 38 additions and 14 deletions

16
terminatorlib/regex.py Normal file
View File

@ -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

View File

@ -2,12 +2,16 @@
# GPL v2 only # GPL v2 only
"""searchbar.py - classes necessary to provide a terminal search bar""" """searchbar.py - classes necessary to provide a terminal search bar"""
import gi
from gi.repository import Gtk, Gdk 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 GObject
from gi.repository import GLib from gi.repository import GLib
from .translation import _ from .translation import _
from .config import Config from .config import Config
from . import regex
# pylint: disable-msg=R0904 # pylint: disable-msg=R0904
class Searchbar(Gtk.HBox): class Searchbar(Gtk.HBox):
@ -127,7 +131,19 @@ class Searchbar(Gtk.HBox):
if searchtext != self.searchstring: if searchtext != self.searchstring:
self.searchstring = searchtext self.searchstring = searchtext
self.searchre = GLib.Regex(searchtext, 0, 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.vte.search_set_gregex(self.searchre, 0)
self.next.set_sensitive(True) self.next.set_sensitive(True)

View File

@ -28,15 +28,7 @@ from .translation import _
from .signalman import Signalman from .signalman import Signalman
from . import plugin from . import plugin
from terminatorlib.layoutlauncher import LayoutLauncher from terminatorlib.layoutlauncher import LayoutLauncher
from . import regex
# 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
# pylint: disable-msg=R0904 # pylint: disable-msg=R0904
class Terminal(Gtk.VBox): class Terminal(Gtk.VBox):
@ -266,9 +258,9 @@ class Terminal(Gtk.VBox):
def _add_regex(self, name, re): def _add_regex(self, name, re):
match = -1 match = -1
if REGEX_FLAGS_PCRE2: if regex.FLAGS_PCRE2:
try: 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) match = self.vte.match_add_regex(reg, 0)
except GLib.Error: except GLib.Error:
# happens when PCRE2 support is not builtin (Ubuntu < 19.10) # happens when PCRE2 support is not builtin (Ubuntu < 19.10)
@ -276,7 +268,7 @@ class Terminal(Gtk.VBox):
# try the "old" glib regex # try the "old" glib regex
if match < 0: 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) match = self.vte.match_add_gregex(reg, 0)
self.matches[name] = match self.matches[name] = match