Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terminal: Add compat detection for Vte regexp feature #22

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions terminatorlib/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
# TODO: Please replace with a proper reference to VTE, I found none!
PCRE2_MULTILINE = 0x00000400

if hasattr(Vte, 'REGEX_FLAGS_DEFAULT'):
REGEX_FLAGS = (Vte.REGEX_FLAGS_DEFAULT | PCRE2_MULTILINE)
REGEX_MODERN = True
else:
REGEX_FLAGS = (GLib.RegexCompileFlags.OPTIMIZE | GLib.RegexCompileFlags.MULTILINE)
REGEX_MODERN = False

# pylint: disable-msg=R0904
class Terminal(Gtk.VBox):
Expand Down Expand Up @@ -70,7 +76,7 @@ class Terminal(Gtk.VBox):
(GObject.TYPE_INT,)),
'group-all': (GObject.SignalFlags.RUN_LAST, None, ()),
'group-all-toggle': (GObject.SignalFlags.RUN_LAST, None, ()),
'move-tab': (GObject.SignalFlags.RUN_LAST, None,
'move-tab': (GObject.SignalFlags.RUN_LAST, None,
(GObject.TYPE_STRING,)),
}

Expand All @@ -79,7 +85,7 @@ class Terminal(Gtk.VBox):

MOUSEBUTTON_LEFT = 1
MOUSEBUTTON_MIDDLE = 2
MOUSEBUTTON_RIGHT = 3
MOUSEBUTTON_RIGHT = 3

terminator = None
vte = None
Expand Down Expand Up @@ -150,7 +156,7 @@ def __init__(self):
self.vte.show()

self.default_encoding = self.vte.get_encoding()
self.regex_flags = (Vte.REGEX_FLAGS_DEFAULT | PCRE2_MULTILINE)
self.regex_flags = REGEX_FLAGS
self.update_url_matches()

self.terminalbox = self.create_terminalbox()
Expand Down Expand Up @@ -262,8 +268,12 @@ def create_terminalbox(self):
return(terminalbox)

def _add_regex(self, name, re):
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags)
self.matches[name] = self.vte.match_add_regex(reg, 0)
if REGEX_MODERN:
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags)
self.matches[name] = self.vte.match_add_regex(reg, 0)
else:
reg = GLib.Regex.new(re, self.regex_flags, 0)
self.matches[name] = self.vte.match_add_gregex(reg, 0)
self.vte.match_set_cursor_name(self.matches[name], 'pointer')

def update_url_matches(self):
Expand All @@ -280,7 +290,7 @@ def update_url_matches(self):
rboundry = "\\b"

re = (lboundry + schemes +
"//(" + user + "@)?[" + hostchars +".]+(:[0-9]+)?(" +
"//(" + user + "@)?[" + hostchars +".]+(:[0-9]+)?(" +
urlpath + ")?" + rboundry + "/?")
self._add_regex('full_uri', re)

Expand Down Expand Up @@ -361,8 +371,8 @@ def connect_signals(self):
self.cnxids.new(self.vte, 'popup-menu', self.popup_menu)

srcvtetargets = [("vte", Gtk.TargetFlags.SAME_APP, self.TARGET_TYPE_VTE)]
dsttargets = [("vte", Gtk.TargetFlags.SAME_APP, self.TARGET_TYPE_VTE),
('text/x-moz-url', 0, self.TARGET_TYPE_MOZ),
dsttargets = [("vte", Gtk.TargetFlags.SAME_APP, self.TARGET_TYPE_VTE),
('text/x-moz-url', 0, self.TARGET_TYPE_MOZ),
('_NETSCAPE_URL', 0, 0)]
'''
The following should work, but on my system it corrupts the returned
Expand Down Expand Up @@ -391,7 +401,7 @@ def connect_signals(self):
dbg('Finalised drag targets: %s' % dsttargets)

for (widget, mask) in [
(self.vte, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.BUTTON3_MASK),
(self.vte, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.BUTTON3_MASK),
(self.titlebar, Gdk.ModifierType.BUTTON1_MASK)]:
widget.drag_source_set(mask, srcvtetargets, Gdk.DragAction.MOVE)

Expand All @@ -408,7 +418,7 @@ def connect_signals(self):
self.cnxids.new(self.vte, 'drag-data-received',
self.on_drag_data_received, self)

self.cnxids.new(self.vte, 'selection-changed',
self.cnxids.new(self.vte, 'selection-changed',
lambda widget: self.maybe_copy_clipboard())

if self.composite_support:
Expand Down Expand Up @@ -505,7 +515,7 @@ def populate_group_menu(self):
groupitems = []
cnxs = []

for key, value in list({_('Broadcast _all'):'all',
for key, value in list({_('Broadcast _all'):'all',
_('Broadcast _group'):'group',
_('Broadcast _off'):'off'}.items()):
item = Gtk.RadioMenuItem.new_with_mnemonic(groupitems, key)
Expand Down Expand Up @@ -621,7 +631,7 @@ def reconfigure(self, _widget=None):
if self.config['exit_action'] == 'restart':
self.cnxids.new(self.vte, 'child-exited', self.spawn_child, True)
elif self.config['exit_action'] in ('close', 'left'):
self.cnxids.new(self.vte, 'child-exited',
self.cnxids.new(self.vte, 'child-exited',
lambda x, y: self.emit('close-term'))

if self.custom_encoding != True:
Expand Down Expand Up @@ -737,7 +747,7 @@ def reconfigure(self, _widget=None):
setattr(newcolor, "red", y / 255.0)
setattr(newcolor, "green", y / 255.0)
setattr(newcolor, "blue", y / 255.0)
self.palette_active.append(newcolor)
self.palette_active.append(newcolor)
self.palette_inactive = []
for color in self.palette_active:
newcolor = Gdk.RGBA()
Expand Down Expand Up @@ -809,7 +819,7 @@ def reconfigure(self, _widget=None):
def set_cursor_color(self):
"""Set the cursor color appropriately"""
if self.config['cursor_color_fg']:
self.vte.set_color_cursor(None)
self.vte.set_color_cursor(None)
else:
cursor_color = Gdk.RGBA()
cursor_color.parse(self.config['cursor_color'])
Expand Down Expand Up @@ -880,7 +890,7 @@ def on_keypress(self, widget, event):
if mapping == "hide_window":
return(False)

if mapping and mapping not in ['close_window',
if mapping and mapping not in ['close_window',
'full_screen']:
dbg('Terminal::on_keypress: lookup found: %r' % mapping)
# handle the case where user has re-bound copy to ctrl+<key>
Expand Down Expand Up @@ -961,7 +971,7 @@ def on_buttonpress(self, widget, event):
return(True)

return(False)

def on_mousewheel(self, widget, event):
"""Handler for modifier + mouse wheel scroll events"""
SMOOTH_SCROLL_UP = event.direction == Gdk.ScrollDirection.SMOOTH and event.delta_y <= 0.
Expand Down Expand Up @@ -1022,7 +1032,7 @@ def on_drag_begin(self, widget, drag_context, _data):
"""Handle the start of a drag event"""
Gtk.drag_set_icon_pixbuf(drag_context, util.widget_pixbuf(self, 512), 0, 0)

def on_drag_data_get(self, _widget, _drag_context, selection_data, info,
def on_drag_data_get(self, _widget, _drag_context, selection_data, info,
_time, data):
"""I have no idea what this does, drag and drop is a mystery. sorry."""
selection_data.set(Gdk.atom_intern('vte', False), info,
Expand All @@ -1036,7 +1046,7 @@ def on_drag_motion(self, widget, drag_context, x, y, _time, _data):
# copy text from another widget
return
srcwidget = Gtk.drag_get_source_widget(drag_context)
if(isinstance(srcwidget, Gtk.EventBox) and
if(isinstance(srcwidget, Gtk.EventBox) and
srcwidget == self.titlebar) or widget == srcwidget:
# on self
return
Expand Down Expand Up @@ -1067,7 +1077,7 @@ def on_drag_motion(self, widget, drag_context, x, y, _time, _data):
elif pos == "left":
coord = (topleft, topmiddle, bottommiddle, bottomleft)
elif pos == "bottom":
coord = (bottomleft, bottomright, middleright , middleleft)
coord = (bottomleft, bottomright, middleright , middleleft)

#here, we define some widget internal values
widget._draw_data = { 'color': color, 'coord' : coord }
Expand Down Expand Up @@ -1130,11 +1140,11 @@ def on_drag_data_received(self, widget, drag_context, x, y, selection_data,
for term in self.terminator.get_target_terms(self):
term.feed(txt)
return

widgetsrc = data.terminator.terminals[int(selection_data.get_data())]
srcvte = Gtk.drag_get_source_widget(drag_context)
#check if computation requireds
if (isinstance(srcvte, Gtk.EventBox) and
if (isinstance(srcvte, Gtk.EventBox) and
srcvte == self.titlebar) or srcvte == widget:
return

Expand Down Expand Up @@ -1864,7 +1874,7 @@ def key_broadcast_all(self):

def key_insert_number(self):
self.emit('enumerate', False)

def key_insert_padded(self):
self.emit('enumerate', True)

Expand All @@ -1877,13 +1887,13 @@ def key_edit_window_title(self):
dialog.set_default_response(Gtk.ResponseType.ACCEPT)
dialog.set_resizable(False)
dialog.set_border_width(8)

label = Gtk.Label(label=_('Enter a new title for the Terminator window...'))
name = Gtk.Entry()
name.set_activates_default(True)
if window.title.text != self.vte.get_window_title():
name.set_text(self.get_toplevel().title.text)

dialog.vbox.pack_start(label, False, False, 6)
dialog.vbox.pack_start(name, False, False, 6)

Expand Down