From cd5c4c105628d37128e4a69572c2c50497ee54a2 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Sat, 14 Jan 2012 20:09:25 +0000 Subject: [PATCH] Allow plugins to override the open/copy menu items in the terminal context menu. Also set these values for all of the default plugins. When copying a URL, run it through the URL preparer - this means wusers will get the resulting URI on their clipboard, rather than the original text sometimes and the URI some other times, depending on the type of URI --- ChangeLog | 4 ++++ terminatorlib/plugin.py | 2 ++ terminatorlib/plugins/url_handlers.py | 6 +++++ terminatorlib/terminal.py | 12 +++++----- terminatorlib/terminal_popup_menu.py | 33 ++++++++++++++++++++++++--- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 24899dd8..6a11c2e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ terminator 0.97: * Allow font dimming in inactive terminals + * Allow URL handler plugins to override label text for URL context + menus + * When copying a URL, run it through the URL handler first so the + resulting URL is copied, rather than the original text terminator 0.96: * Unity support for opening new windows (Lucian Adrian Grijincu) diff --git a/terminatorlib/plugin.py b/terminatorlib/plugin.py index 2dd907b4..639b6542 100755 --- a/terminatorlib/plugin.py +++ b/terminatorlib/plugin.py @@ -155,6 +155,8 @@ class URLHandler(Plugin): capabilities = ['url_handler'] handler_name = None match = None + nameopen = None + namecopy = None def __init__(self): """Class initialiser""" diff --git a/terminatorlib/plugins/url_handlers.py b/terminatorlib/plugins/url_handlers.py index 04524749..6c1f75eb 100644 --- a/terminatorlib/plugins/url_handlers.py +++ b/terminatorlib/plugins/url_handlers.py @@ -14,6 +14,8 @@ class LaunchpadBugURLHandler(plugin.URLHandler): capabilities = ['url_handler'] handler_name = 'launchpad_bug' match = '\\b(lp|LP):?\s?#?[0-9]+(,\s*#?[0-9]+)*\\b' + nameopen = "Open Launchpad bug" + namecopy = "Copy bug URL" def callback(self, url): """Look for the number in the supplied string and return it as a URL""" @@ -26,6 +28,8 @@ class LaunchpadCodeURLHandler(plugin.URLHandler): branch entry then it should be transformed into a code.launchpad.net URL""" capabilities = ['url_handler'] handler_name = 'launchpad_code' + nameopen = "Open Launchpad branch" + namecopy = "Copy branch URL" lpfilters = {} lpfilters['project'] = '[a-z0-9]{1}[a-z0-9\.\-\+]+' lpfilters['group'] = '~%s' % lpfilters['project'] @@ -45,6 +49,8 @@ class APTURLHandler(plugin.URLHandler): it appropriately""" capabilities = ['url_handler'] handler_name = 'apturl' + nameopen = "Open software manager" + namecopy = "Copy package URI" match = '\\bapt:.*\\b' def callback(self, url): diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index f94daca9..1860b9b3 100755 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -284,14 +284,14 @@ class Terminal(gtk.VBox): name = urlplugin.handler_name match = urlplugin.match if name in self.matches: - dbg('Terminal::update_matches: refusing to add \ -duplicate match %s' % name) + dbg('refusing to add duplicate match %s' % name) continue self.matches[name] = self.vte.match_add(match) - dbg('Terminal::update_matches: added plugin URL handler \ -for %s (%s)' % (name, urlplugin.__class__.__name__)) + dbg('added plugin URL handler for %s (%s) as %d' % + (name, urlplugin.__class__.__name__, + self.matches[name])) except Exception, ex: - err('Terminal::update_url_matches: %s' % ex) + err('Exception occurred adding plugin URL match: %s' % ex) def match_add(self, name, match): """Register a URL match""" @@ -1293,7 +1293,7 @@ for %s (%s)' % (name, urlplugin.__class__.__name__)) url = newurl break except Exception, ex: - err('Terminal::prepare_url: %s' % ex) + err('Exception occurred preparing URL: %s' % ex) return(url) diff --git a/terminatorlib/terminal_popup_menu.py b/terminatorlib/terminal_popup_menu.py index 6017a574..887badc6 100755 --- a/terminatorlib/terminal_popup_menu.py +++ b/terminatorlib/terminal_popup_menu.py @@ -10,7 +10,7 @@ from version import APP_NAME from translation import _ from encoding import TerminatorEncoding from terminator import Terminator -from util import err +from util import err, dbg from config import Config from prefseditor import PrefsEditor import plugin @@ -47,14 +47,41 @@ class TerminalPopupMenu(object): button = 3 if url: + dbg("URL matches id: %d" % url[1]) + if not url[1] in terminal.matches.values(): + err("Unknown URL match id: %d" % url[1]) + dbg("Available matches: %s" % terminal.matches) + + nameopen = None + namecopy = None if url[1] == terminal.matches['email']: nameopen = _('_Send email to...') namecopy = _('_Copy email address') elif url[1] == terminal.matches['voip']: nameopen = _('Ca_ll VoIP address') namecopy = _('_Copy VoIP address') - else: + elif url[1] in terminal.matches.values(): + # This is a plugin match + for pluginname in terminal.matches: + if terminal.matches[pluginname] == url[1]: + break + + dbg("Found match ID (%d) in terminal.matches plugin %s" % + (url[1], pluginname)) + registry = plugin.PluginRegistry() + registry.load_plugins() + plugins = registry.get_plugins_by_capability('url_handler') + for urlplugin in plugins: + if urlplugin.handler_name == pluginname: + dbg("Identified matching plugin: %s" % + urlplugin.handler_name) + nameopen = _(urlplugin.nameopen) + namecopy = _(urlplugin.namecopy) + break + + if not nameopen: nameopen = _('_Open link') + if not namecopy: namecopy = _('_Copy address') icon = gtk.image_new_from_stock(gtk.STOCK_JUMP_TO, @@ -66,7 +93,7 @@ class TerminalPopupMenu(object): item = gtk.MenuItem(namecopy) item.connect('activate', - lambda x: terminal.clipboard.set_text(url[0])) + lambda x: terminal.clipboard.set_text(terminal.prepare_url(url))) menu.append(item) menu.append(gtk.MenuItem())