diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index e1d293781..aac584621 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1307,6 +1307,39 @@ class CommandDispatcher: except webelem.IsNullError: raise cmdexc.CommandError("Element vanished while editing!") + @cmdutils.register(instance='command-dispatcher', + modes=[KeyMode.insert], hide=True, scope='window') + def paste_primary(self): + """Paste the primary selection at cursor position into the curently + selected form field. + """ + 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 selection: '{}'".format(sel)) + elem.evaluateJavaScript(""" + var sel = '%s'; + if (this.selectionStart || this.selectionStart == '0') { + var startPos = this.selectionStart; + var endPos = this.selectionEnd; + this.value = this.value.substring(0, startPos) + + sel + + this.value.substring(endPos, this.value.length); + this.selectionStart = startPos + sel.length; + this.selectionEnd = startPos + sel.length; + } else { + this.value += sel; + } + """ % sel) + def _clear_search(self, view, text): """Clear search string/highlights for the given view. diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index a9fbb2669..d94ad572e 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -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', ['']), + ('paste-primary', ['']), ])), ('hint', collections.OrderedDict([