Merge pull request #43 from gnome-terminator/fix/regex-search
This commit is contained in:
commit
827b926df8
16
terminatorlib/regex.py
Normal file
16
terminatorlib/regex.py
Normal 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
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user