Add :insert-text command

See #1790.
:paste-primary now calls :insert-text and can be deprecated by
:insert-text {primary} after #1791 is merged.
This commit is contained in:
Jan Verbeek 2016-08-07 02:43:08 +02:00
parent 778ccad39f
commit dc69a90e69
2 changed files with 30 additions and 10 deletions

View File

@ -918,6 +918,7 @@ How many steps to zoom out.
|<<enter-mode,enter-mode>>|Enter a key mode.
|<<follow-hint,follow-hint>>|Follow a hint.
|<<follow-selected,follow-selected>>|Follow the selected text.
|<<insert-text,insert-text>>|Insert text at cursor position.
|<<jump-mark,jump-mark>>|Jump to the mark named by `key`.
|<<leave-mode,leave-mode>>|Leave the mode we're currently in.
|<<message-error,message-error>>|Show an error message in the statusbar.
@ -1028,6 +1029,18 @@ Follow the selected text.
==== optional arguments
* +*-t*+, +*--tab*+: Load the selected link in a new tab.
[[insert-text]]
=== insert-text
Syntax: +:insert-text 'text'+
Insert text at cursor position.
==== positional arguments
* +'text'+: The text to insert.
==== note
* This command does not split arguments after the last argument and handles quotes literally.
[[jump-mark]]
=== jump-mark
Syntax: +:jump-mark 'key'+

View File

@ -1431,6 +1431,19 @@ class CommandDispatcher:
needs_js=True, backend=usertypes.Backend.QtWebKit)
def paste_primary(self):
"""Paste the primary selection at cursor position."""
try:
self.insert_text(utils.get_clipboard(selection=True))
except utils.SelectionUnsupportedError:
self.insert_text(utils.get_clipboard())
@cmdutils.register(instance='command-dispatcher', maxsplit=0,
modes=[KeyMode.insert], hide=True, scope='window',
needs_js=True, backend=usertypes.Backend.QtWebKit)
def insert_text(self, text):
"""Insert text at cursor position.
Args:
text: The text to insert."""
# FIXME:qtwebengine have a proper API for this
tab = self._current_widget()
page = tab._widget.page() # pylint: disable=protected-access
@ -1440,20 +1453,14 @@ class CommandDispatcher:
raise cmdexc.CommandError("No element focused!")
if not elem.is_editable(strict=True):
raise cmdexc.CommandError("Focused element is not editable!")
try:
sel = utils.get_clipboard(selection=True)
except utils.SelectionUnsupportedError:
sel = utils.get_clipboard()
log.misc.debug("Pasting primary selection into element {}".format(
log.misc.debug("Inserting text into element {}".format(
elem.debug_text()))
elem.run_js_async("""
var sel = '{}';
var text = '{}';
var event = document.createEvent('TextEvent');
event.initTextEvent('textInput', true, true, null, sel);
event.initTextEvent('textInput', true, true, null, text);
this.dispatchEvent(event);
""".format(javascript.string_escape(sel)))
""".format(javascript.string_escape(text)))
def _search_cb(self, found, *, tab, old_scroll_pos, options, text, prev):
"""Callback called from search/search_next/search_prev.