From 59a11c178f0a80afc67e41afee25773f23972402 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 1 Feb 2015 23:55:37 +0100 Subject: [PATCH] Don't open relative files in fuzzy_url with :open With most actions which use fuzzy_url (:open/quickmarks/etc.) it's rather confusing when relative files are opened - the only place where they should be opened is when we're processing a commandline argument. --- qutebrowser/app.py | 2 +- qutebrowser/utils/urlutils.py | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 7cf14480a..d03479854 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -330,7 +330,7 @@ class Application(QApplication): window=win_id) log.init.debug("Startup URL {}".format(cmd)) try: - url = urlutils.fuzzy_url(cmd, cwd) + url = urlutils.fuzzy_url(cmd, cwd, relative=True) except urlutils.FuzzyUrlError as e: message.error(0, "Error in startup argument '{}': " "{}".format(cmd, e)) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index d6a932bfa..65fc41396 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -123,23 +123,30 @@ def _is_url_dns(url): return not info.error() -def fuzzy_url(urlstr, cwd=None): +def fuzzy_url(urlstr, cwd=None, relative=False): """Get a QUrl based on an user input which is URL or search term. Args: urlstr: URL to load as a string. cwd: The current working directory, or None. + relative: Whether to resolve relative files. Return: A target QUrl to a searchpage or the original URL. """ - if cwd: - path = os.path.join(cwd, os.path.expanduser(urlstr)) - else: + expanded = os.path.expanduser(urlstr) + if relative and cwd: + path = os.path.join(cwd, expanded) + elif relative: try: - path = os.path.abspath(os.path.expanduser(urlstr)) + path = os.path.abspath(expanded) except OSError: path = None + elif os.path.isabs(expanded): + path = expanded + else: + path = None + stripped = urlstr.strip() if path is not None and os.path.exists(path): log.url.debug("URL is a local file")