From dc69a90e69642a39dcacd4764f1d29ee1357834f Mon Sep 17 00:00:00 2001 From: Jan Verbeek Date: Sun, 7 Aug 2016 02:43:08 +0200 Subject: [PATCH] Add :insert-text command See #1790. :paste-primary now calls :insert-text and can be deprecated by :insert-text {primary} after #1791 is merged. --- doc/help/commands.asciidoc | 13 +++++++++++++ qutebrowser/browser/commands.py | 27 +++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index f43be5822..13487a823 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -918,6 +918,7 @@ How many steps to zoom out. |<>|Enter a key mode. |<>|Follow a hint. |<>|Follow the selected text. +|<>|Insert text at cursor position. |<>|Jump to the mark named by `key`. |<>|Leave the mode we're currently in. |<>|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'+ diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 3728ebac4..cd60510dd 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -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.