From 84a034d7e94eecf02a0ad372980867a7d3277b55 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 8 Sep 2014 07:35:18 +0200 Subject: [PATCH] More liberal quoting. --- qutebrowser/utils/completer.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/qutebrowser/utils/completer.py b/qutebrowser/utils/completer.py index 357f7985c..a70bbe5f2 100644 --- a/qutebrowser/utils/completer.py +++ b/qutebrowser/utils/completer.py @@ -19,8 +19,6 @@ """Completer attached to a CompletionView.""" -import shlex - from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject from qutebrowser.config import config, configdata @@ -131,6 +129,21 @@ class Completer(QObject): model = self._models.get(completion) return model + def _quote(self, s): + """Quote s if it needs quoting for the commandline. + + Note we don't use shlex.quote because that quotes a lot of shell + metachars we don't need to have quoted. + """ + if not s: + return "''" + elif any(c in s for c in ' \'\t\n\\'): + # use single quotes, and put single quotes into double quotes + # the string $'b is then quoted as '$'"'"'b' + return "'" + s.replace("'", "'\"'\"'") + "'" + else: + return s + def selection_changed(self, selected, _deselected): """Emit change_completed_part if a new item was selected. @@ -150,7 +163,7 @@ class Completer(QObject): data = model.data(indexes[0]) if data is None: return - data = shlex.quote(data) + data = self._quote(data) if model.count() == 1 and config.get('completion', 'quick-complete'): # If we only have one item, we want to apply it immediately # and go on to the next part.