Improve clipboard exceptions, migrate bindings

This commit is contained in:
Jan Verbeek 2016-08-07 00:46:23 +02:00
parent 96146c55af
commit 38508274e0
6 changed files with 34 additions and 11 deletions

View File

@ -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()))

View File

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

View File

@ -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}'),
]

View File

@ -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()

View File

@ -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

View File

@ -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):