diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index f0ed6d67a..6dde5d40f 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -818,7 +818,10 @@ class CommandDispatcher: force_search = False if not utils.supports_selection(): sel = False - text = utils.get_clipboard(selection=sel) + try: + text = utils.get_clipboard(selection=sel) + except utils.ClipboardEmptyError as e: + raise cmdexc.CommandError(e) text_urls = [u for u in text.split('\n') if u.strip()] if (len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) and urlutils.get_path_if_valid( @@ -1462,9 +1465,12 @@ class CommandDispatcher: raise cmdexc.CommandError("Focused element is not editable!") try: - sel = utils.get_clipboard(selection=True) - except utils.SelectionUnsupportedError: - sel = utils.get_clipboard() + try: + sel = utils.get_clipboard(selection=True) + except utils.SelectionUnsupportedError: + sel = utils.get_clipboard() + except utils.ClipboardEmptyError: + return log.misc.debug("Pasting primary selection into element {}".format( elem.debug_text())) diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 91c8bf3c9..201fc4bbe 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -57,10 +57,13 @@ def replace_variables(win_id, arglist): QUrl.RemovePassword) if '{url:pretty}' in arglist: pretty_url = _current_url(tabbed_browser).toString(QUrl.RemovePassword) - if '{clipboard}' in arglist: - clipboard = utils.get_clipboard() - if '{primary}' in arglist: - primary = utils.get_clipboard(selection=True) + try: + if '{clipboard}' in arglist: + clipboard = utils.get_clipboard() + if '{primary}' in arglist: + primary = utils.get_clipboard(selection=True) + except utils.ClipboardEmptyError as e: + raise cmdexc.CommandError(e) for arg in arglist: if arg == '{url}': args.append(url) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 9cc275502..f770f5c05 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -1677,4 +1677,9 @@ CHANGED_KEY_COMMANDS = [ (re.compile(r'^download-remove --all$'), r'download-clear'), (re.compile(r'^hint links fill "([^"]*)"$'), r'hint links fill \1'), + + (re.compile(r'^paste$'), r'open {clipboard}'), + (re.compile(r'^paste -([twb])$'), r'open -\1 {clipboard}'), + (re.compile(r'^paste -([twb])s$'), r'open -\1 {primary}'), + (re.compile(r'^paste -s([twb])$'), r'open -\1 {primary}'), ] diff --git a/qutebrowser/misc/miscwidgets.py b/qutebrowser/misc/miscwidgets.py index 63a5718c6..7e84300d2 100644 --- a/qutebrowser/misc/miscwidgets.py +++ b/qutebrowser/misc/miscwidgets.py @@ -47,7 +47,8 @@ class MinimalLineEditMixin: if e.key() == Qt.Key_Insert and e.modifiers() == Qt.ShiftModifier: try: text = utils.get_clipboard(selection=True) - except utils.SelectionUnsupportedError: + except (utils.SelectionUnsupportedError, + utils.ClipboardEmptyError): pass else: e.accept() diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 983827d46..32696aacf 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -37,7 +37,6 @@ import pkg_resources import qutebrowser from qutebrowser.utils import qtutils, log -from qutebrowser.commands import cmdexc fake_clipboard = None @@ -49,6 +48,11 @@ class SelectionUnsupportedError(Exception): """Raised if [gs]et_clipboard is used and selection=True is unsupported.""" +class ClipboardEmptyError(Exception): + + """Raised if get_clipboard is used and the clipboard is empty.""" + + def elide(text, length): """Elide text so it uses a maximum of length chars.""" if length < 1: @@ -813,7 +817,7 @@ def get_clipboard(selection=False): target = "Primary selection" if selection else "Clipboard" if not data.strip(): - raise cmdexc.CommandError("{} is empty.".format(target)) + raise ClipboardEmptyError("{} is empty.".format(target)) log.misc.debug("{} contained: {!r}".format(target, data)) return data diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index a6ad3d119..53664b718 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -286,6 +286,10 @@ class TestKeyConfigParser: 'hint links fill :open {hint-url}'), ('hint links fill ":open -t {hint-url}"', 'hint links fill :open -t {hint-url}'), + + ('paste', 'open {clipboard}'), + ('paste -t', 'open -t {clipboard}'), + ('paste -ws', 'open -w {primary}'), ] ) def test_migrations(self, old, new_expected):