Merge branch 'shift_ins' of https://github.com/lahwaacz/qutebrowser into lahwaacz-shift_ins

This commit is contained in:
Florian Bruhin 2016-01-31 19:46:41 +01:00
commit 44625b254c
4 changed files with 84 additions and 1 deletions

View File

@ -1307,6 +1307,31 @@ class CommandDispatcher:
except webelem.IsNullError:
raise cmdexc.CommandError("Element vanished while editing!")
@cmdutils.register(instance='command-dispatcher',
modes=[KeyMode.insert], hide=True, scope='window',
needs_js=True)
def paste_primary(self):
"""Paste the primary selection at cursor position."""
frame = self._current_widget().page().currentFrame()
try:
elem = webelem.focus_elem(frame)
except webelem.IsNullError:
raise cmdexc.CommandError("No element focused!")
if not elem.is_editable(strict=True):
raise cmdexc.CommandError("Focused element is not editable!")
clipboard = QApplication.clipboard()
if clipboard.supportsSelection():
sel = clipboard.text(QClipboard.Selection)
log.misc.debug("Pasting primary selection into element {}".format(
elem.debug_text()))
elem.evaluateJavaScript("""
var sel = '{}';
var event = document.createEvent('TextEvent');
event.initTextEvent('textInput', true, true, null, sel);
this.dispatchEvent(event);
""".format(webelem.javascript_escape(sel)))
def _clear_search(self, view, text):
"""Clear search string/highlights for the given view.

View File

@ -1324,7 +1324,8 @@ KEY_SECTION_DESC = {
"Since normal keypresses are passed through, only special keys are "
"supported in this mode.\n"
"Useful hidden commands to map in this section:\n\n"
" * `open-editor`: Open a texteditor with the focused field."),
" * `open-editor`: Open a texteditor with the focused field.\n"
" * `paste-primary`: Paste primary selection at cursor position."),
'hint': (
"Keybindings for hint mode.\n"
"Since normal keypresses are passed through, only special keys are "
@ -1495,6 +1496,7 @@ KEY_DATA = collections.OrderedDict([
('insert', collections.OrderedDict([
('open-editor', ['<Ctrl-E>']),
('paste-primary', ['<Shift-Ins>']),
])),
('hint', collections.OrderedDict([

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Paste primary selection</title>
</head>
<body>
<textarea id="qute-textarea"></textarea>
</body>
</html>

View File

@ -170,3 +170,49 @@ Feature: Yanking and pasting.
history:
- active: true
url: http://localhost:*/data/hello3.txt
Scenario: Pasting the primary selection into an empty text field
When selection is supported
And I open data/paste_primary.html
And I put "Hello world" into the primary selection
# Click the text field
And I run :hint all
And I run :follow-hint a
And I run :paste-primary
# Compare
And I run :jseval console.log(document.getElementById('qute-textarea').value);
Then the javascript message "Hello world" should be logged
Scenario: Pasting the primary selection into a text field at specific position
When selection is supported
And I open data/paste_primary.html
And I run :jseval document.getElementById('qute-textarea').value = 'one two three four';
And I put " Hello world" into the primary selection
# Click the text field
And I run :hint all
And I run :follow-hint a
# Move to the beginning and two words to the right
And I press the keys "<Home>"
And I press the key "<Ctrl+Right>"
And I press the key "<Ctrl+Right>"
And I run :paste-primary
# Compare
And I run :jseval console.log(document.getElementById('qute-textarea').value);
Then the javascript message "one two Hello world three four" should be logged
Scenario: Pasting the primary selection into a text field with undo
When selection is supported
And I open data/paste_primary.html
# Click the text field
And I run :hint all
And I run :follow-hint a
# Paste and undo
And I put "This text should be undone" into the primary selection
And I run :paste-primary
And I press the key "<Ctrl+z>"
# Paste final text
And I put "This text should stay" into the primary selection
And I run :paste-primary
# Compare
And I run :jseval console.log(document.getElementById('qute-textarea').value);
Then the javascript message "This text should stay" should be logged