terminal: Refactor regex handling and add pointer cursor

This commit is contained in:
Markus Frosch 2020-04-05 19:52:25 +02:00
parent 07d7dd56b2
commit da4f59ff74
1 changed files with 18 additions and 14 deletions

View File

@ -258,6 +258,11 @@ class Terminal(Gtk.VBox):
return(terminalbox) 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)
self.vte.match_set_cursor_name(self.matches[name], 'pointer')
def update_url_matches(self): def update_url_matches(self):
"""Update the regexps used to match URLs""" """Update the regexps used to match URLs"""
userchars = "-A-Za-z0-9" userchars = "-A-Za-z0-9"
@ -274,8 +279,7 @@ class Terminal(Gtk.VBox):
re = (lboundry + schemes + re = (lboundry + schemes +
"//(" + user + "@)?[" + hostchars +".]+(:[0-9]+)?(" + "//(" + user + "@)?[" + hostchars +".]+(:[0-9]+)?(" +
urlpath + ")?" + rboundry + "/?") urlpath + ")?" + rboundry + "/?")
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags) self._add_regex('full_uri', re)
self.matches['full_uri'] = self.vte.match_add_regex(reg, 0)
if self.matches['full_uri'] == -1: if self.matches['full_uri'] == -1:
err ('Terminal::update_url_matches: Failed adding URL matches') err ('Terminal::update_url_matches: Failed adding URL matches')
@ -284,24 +288,23 @@ class Terminal(Gtk.VBox):
'(callto:|h323:|sip:)' + "[" + userchars + "+][" + '(callto:|h323:|sip:)' + "[" + userchars + "+][" +
userchars + ".]*(:[0-9]+)?@?[" + pathchars + "]+" + userchars + ".]*(:[0-9]+)?@?[" + pathchars + "]+" +
rboundry) rboundry)
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags) self._add_regex('voip', re)
self.matches['voip'] = self.vte.match_add_regex(reg, 0)
re = (lboundry + re = (lboundry +
"(www|ftp)[" + hostchars + "]*\.[" + hostchars + "(www|ftp)[" + hostchars + "]*\.[" + hostchars +
".]+(:[0-9]+)?(" + urlpath + ")?" + rboundry + "/?") ".]+(:[0-9]+)?(" + urlpath + ")?" + rboundry + "/?")
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags) self._add_regex('addr_only', re)
self.matches['addr_only'] = self.vte.match_add_regex(reg, 0)
re = (lboundry + re = (lboundry +
"(mailto:)?[a-zA-Z0-9][a-zA-Z0-9.+-]*@[a-zA-Z0-9]" + "(mailto:)?[a-zA-Z0-9][a-zA-Z0-9.+-]*@[a-zA-Z0-9]" +
"[a-zA-Z0-9-]*\.[a-zA-Z0-9][a-zA-Z0-9-]+" + "[a-zA-Z0-9-]*\.[a-zA-Z0-9][a-zA-Z0-9-]+" +
"[.a-zA-Z0-9-]*" + rboundry) "[.a-zA-Z0-9-]*" + rboundry)
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags) self._add_regex('email', re)
self.matches['email'] = self.vte.match_add_regex(reg, 0)
re = (lboundry + re = (lboundry +
"""news:[-A-Z\^_a-z{|}~!"#$%&'()*+,./0-9;:=?`]+@""" + """news:[-A-Z\^_a-z{|}~!"#$%&'()*+,./0-9;:=?`]+@""" +
"[-A-Za-z0-9.]+(:[0-9]+)?" + rboundry) "[-A-Za-z0-9.]+(:[0-9]+)?" + rboundry)
reg = Vte.Regex.new_for_match(re, len(re), self.regex_flags) self._add_regex('nntp', re)
self.matches['nntp'] = self.vte.match_add_regex(reg, 0)
# Now add any matches from plugins # Now add any matches from plugins
try: try:
@ -315,8 +318,9 @@ class Terminal(Gtk.VBox):
if name in self.matches: if name in self.matches:
dbg('refusing to add duplicate match %s' % name) dbg('refusing to add duplicate match %s' % name)
continue continue
reg = Vte.Regex.new_for_match(match, len(match), self.regex_flags)
self.matches[name] = self.vte.match_add_regex(reg, 0) self._add_regex(name, match)
dbg('added plugin URL handler for %s (%s) as %d' % dbg('added plugin URL handler for %s (%s) as %d' %
(name, urlplugin.__class__.__name__, (name, urlplugin.__class__.__name__,
self.matches[name])) self.matches[name]))
@ -328,8 +332,8 @@ class Terminal(Gtk.VBox):
if name in self.matches: if name in self.matches:
err('Terminal::match_add: Refusing to create duplicate match %s' % name) err('Terminal::match_add: Refusing to create duplicate match %s' % name)
return return
reg = Vte.Regex.new_for_match(match, len(match), self.regex_flags)
self.matches[name] = self.vte.match_add_regex(reg, 0) self._add_regex(name, match)
def match_remove(self, name): def match_remove(self, name):
"""Remove a previously registered URL match""" """Remove a previously registered URL match"""