Allow {url} and {url:pretty} as parts of arguments

This makes commands like `:open web.archive.org/web/{url}` possible.
This commit also adds a no_replace_variables command register argument
that stops the replacement from happening, which is important for
commands like `:bind` and `:spawn` that take a command as an argument.
This commit is contained in:
Jan Verbeek 2016-08-04 02:43:02 +02:00
parent 2f50d100e9
commit c7c5a98506
6 changed files with 19 additions and 14 deletions

View File

@ -992,7 +992,7 @@ class CommandDispatcher:
self._tabbed_browser.setUpdatesEnabled(True)
@cmdutils.register(instance='command-dispatcher', scope='window',
maxsplit=0)
maxsplit=0, no_replace_variables=True)
def spawn(self, cmdline, userscript=False, verbose=False, detach=False):
"""Spawn a command in a shell.

View File

@ -82,6 +82,7 @@ class Command:
no_cmd_split: If true, ';;' to split sub-commands is ignored.
backend: Which backend the command works with (or None if it works with
both)
no_replace_variables: Whether or not to replace variables like {url}
_qute_args: The saved data from @cmdutils.argument
_needs_js: Whether the command needs javascript enabled
_modes: The modes the command can be executed in.
@ -95,7 +96,7 @@ class Command:
hide=False, modes=None, not_modes=None, needs_js=False,
debug=False, ignore_args=False, deprecated=False,
no_cmd_split=False, star_args_optional=False, scope='global',
backend=None):
backend=None, no_replace_variables=False):
# I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-locals
if modes is not None and not_modes is not None:
@ -127,6 +128,7 @@ class Command:
self.handler = handler
self.no_cmd_split = no_cmd_split
self.backend = backend
self.no_replace_variables = no_replace_variables
self.docparser = docutils.DocstringParser(handler)
self.parser = argparser.ArgumentParser(

View File

@ -52,16 +52,16 @@ def replace_variables(win_id, arglist):
args = []
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
if '{url}' in arglist:
if any('{url}' in arg for arg in arglist):
url = _current_url(tabbed_browser).toString(QUrl.FullyEncoded |
QUrl.RemovePassword)
if '{url:pretty}' in arglist:
if any('{url:pretty}' in arg for arg in arglist):
pretty_url = _current_url(tabbed_browser).toString(QUrl.RemovePassword)
for arg in arglist:
if arg == '{url}':
args.append(url)
elif arg == '{url:pretty}':
args.append(pretty_url)
if '{url}' in arg:
args.append(arg.replace('{url}', url))
elif '{url:pretty}' in arg:
args.append(arg.replace('{url:pretty}', pretty_url))
else:
args.append(arg)
return args
@ -279,7 +279,10 @@ class CommandRunner(QObject):
window=self._win_id)
cur_mode = mode_manager.mode
args = replace_variables(self._win_id, result.args)
if result.cmd.no_replace_variables:
args = result.args
else:
args = replace_variables(self._win_id, result.args)
if count is not None:
if result.count is not None:
raise cmdexc.CommandMetaError("Got count via command and "

View File

@ -150,7 +150,8 @@ class KeyConfigParser(QObject):
data = str(self)
f.write(data)
@cmdutils.register(instance='key-config', maxsplit=1, no_cmd_split=True)
@cmdutils.register(instance='key-config', maxsplit=1, no_cmd_split=True,
no_replace_variables=True)
@cmdutils.argument('win_id', win_id=True)
@cmdutils.argument('key', completion=usertypes.Completion.empty)
@cmdutils.argument('command', completion=usertypes.Completion.command)

View File

@ -23,7 +23,7 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QSize
from PyQt5.QtWidgets import QSizePolicy
from qutebrowser.keyinput import modeman, modeparsers
from qutebrowser.commands import cmdexc, cmdutils, runners
from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.misc import cmdhistory, split
from qutebrowser.misc import miscwidgets as misc
from qutebrowser.utils import usertypes, log, objreg
@ -109,7 +109,6 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
append: If given, the text is appended to the current text.
"""
args = split.simple_split(text)
args = runners.replace_variables(self._win_id, args)
text = ' '.join(args)
if space:

View File

@ -39,7 +39,7 @@ from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication # pylint: disable=unused-import
@cmdutils.register(maxsplit=1, no_cmd_split=True)
@cmdutils.register(maxsplit=1, no_cmd_split=True, no_replace_variables=True)
@cmdutils.argument('win_id', win_id=True)
def later(ms: int, command, win_id):
"""Execute a command after some time.
@ -69,7 +69,7 @@ def later(ms: int, command, win_id):
raise
@cmdutils.register(maxsplit=1, no_cmd_split=True)
@cmdutils.register(maxsplit=1, no_cmd_split=True, no_replace_variables=True)
@cmdutils.argument('win_id', win_id=True)
def repeat(times: int, command, win_id):
"""Repeat a given command.