Skip to content

Commit

Permalink
searchbar: Implement modern/glib regexp support
Browse files Browse the repository at this point in the history
fixes #4
refs #28
  • Loading branch information
lazyfrosch committed Apr 27, 2020
1 parent 11a6a57 commit 73c9176
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
16 changes: 16 additions & 0 deletions terminatorlib/regex.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 18 additions & 2 deletions terminatorlib/searchbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -127,8 +131,20 @@ def do_search(self, widget):

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)
Expand Down
16 changes: 4 additions & 12 deletions terminatorlib/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@
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):
Expand Down Expand Up @@ -266,17 +258,17 @@ def create_terminalbox(self):

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)
pass

# 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
Expand Down

0 comments on commit 73c9176

Please sign in to comment.