2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2015-01-03 15:51:31 +01:00
|
|
|
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-02-24 19:11:43 +01:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-08-06 15:38:25 +02:00
|
|
|
"""Module containing command managers (SearchRunner and CommandRunner)."""
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2015-04-13 07:16:41 +02:00
|
|
|
import collections
|
|
|
|
|
2014-09-23 22:00:26 +02:00
|
|
|
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QUrl
|
2014-02-24 16:47:32 +01:00
|
|
|
from PyQt5.QtWebKitWidgets import QWebPage
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-12-17 11:17:18 +01:00
|
|
|
from qutebrowser.config import config, configexc
|
2014-08-26 20:48:39 +02:00
|
|
|
from qutebrowser.commands import cmdexc, cmdutils
|
2015-01-04 20:13:25 +01:00
|
|
|
from qutebrowser.utils import message, log, utils, objreg, qtutils
|
2014-12-13 17:28:50 +01:00
|
|
|
from qutebrowser.misc import split
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-02-24 16:47:32 +01:00
|
|
|
|
2015-04-13 07:16:41 +02:00
|
|
|
ParseResult = collections.namedtuple('ParseResult', 'cmd, args, cmdline')
|
|
|
|
|
|
|
|
|
2014-09-28 23:23:02 +02:00
|
|
|
def replace_variables(win_id, arglist):
|
2014-09-03 10:47:27 +02:00
|
|
|
"""Utility function to replace variables like {url} in a list of args."""
|
|
|
|
args = []
|
2014-09-28 23:23:02 +02:00
|
|
|
tabbed_browser = objreg.get('tabbed-browser', scope='window',
|
|
|
|
window=win_id)
|
2015-01-04 20:13:25 +01:00
|
|
|
if '{url}' in arglist:
|
|
|
|
try:
|
2014-09-29 19:56:37 +02:00
|
|
|
url = tabbed_browser.current_url().toString(QUrl.FullyEncoded |
|
|
|
|
QUrl.RemovePassword)
|
2015-01-04 20:13:25 +01:00
|
|
|
except qtutils.QtValueError as e:
|
|
|
|
msg = "Current URL is invalid"
|
|
|
|
if e.reason:
|
|
|
|
msg += " ({})".format(e.reason)
|
|
|
|
msg += "!"
|
|
|
|
raise cmdexc.CommandError(msg)
|
|
|
|
for arg in arglist:
|
|
|
|
if arg == '{url}':
|
2014-09-03 10:47:27 +02:00
|
|
|
args.append(url)
|
|
|
|
else:
|
|
|
|
args.append(arg)
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2014-08-06 15:38:25 +02:00
|
|
|
class SearchRunner(QObject):
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2015-03-31 20:49:29 +02:00
|
|
|
"""Run searches on web pages.
|
2014-02-24 19:11:43 +01:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
_text: The text from the last search.
|
|
|
|
_flags: The flags from the last search.
|
|
|
|
|
|
|
|
Signals:
|
|
|
|
do_search: Emitted when a search should be started.
|
|
|
|
arg 1: Search string.
|
|
|
|
arg 2: Flags to use.
|
|
|
|
"""
|
|
|
|
|
|
|
|
do_search = pyqtSignal(str, 'QWebPage::FindFlags')
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
2014-04-17 17:44:27 +02:00
|
|
|
super().__init__(parent)
|
2014-02-24 19:11:43 +01:00
|
|
|
self._text = None
|
|
|
|
self._flags = 0
|
|
|
|
|
2014-06-17 11:03:42 +02:00
|
|
|
def __repr__(self):
|
2014-09-26 15:48:24 +02:00
|
|
|
return utils.get_repr(self, text=self._text, flags=self._flags)
|
2014-06-17 11:03:42 +02:00
|
|
|
|
2014-12-29 22:45:26 +01:00
|
|
|
@pyqtSlot(str)
|
|
|
|
@cmdutils.register(instance='search-runner', scope='window', maxsplit=0)
|
2015-04-03 14:39:47 +02:00
|
|
|
def search(self, text="", reverse=False):
|
|
|
|
"""Search for a text on the current page. With no text, clear results.
|
2014-02-24 19:11:43 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The text to search for.
|
2014-12-29 22:45:26 +01:00
|
|
|
reverse: Reverse search direction.
|
2014-02-24 19:11:43 +01:00
|
|
|
"""
|
|
|
|
if self._text is not None and self._text != text:
|
2014-06-26 11:10:31 +02:00
|
|
|
# We first clear the marked text, then the highlights
|
2014-06-26 10:56:31 +02:00
|
|
|
self.do_search.emit('', 0)
|
2014-06-26 11:10:31 +02:00
|
|
|
self.do_search.emit('', QWebPage.HighlightAllOccurrences)
|
2014-02-24 19:11:43 +01:00
|
|
|
self._text = text
|
|
|
|
self._flags = 0
|
2014-08-12 17:00:18 +02:00
|
|
|
ignore_case = config.get('general', 'ignore-case')
|
|
|
|
if ignore_case == 'smart':
|
|
|
|
if not text.islower():
|
|
|
|
self._flags |= QWebPage.FindCaseSensitively
|
2014-08-25 06:56:14 +02:00
|
|
|
elif not ignore_case:
|
2014-02-24 19:11:43 +01:00
|
|
|
self._flags |= QWebPage.FindCaseSensitively
|
2014-04-27 21:21:14 +02:00
|
|
|
if config.get('general', 'wrap-search'):
|
2014-02-24 19:11:43 +01:00
|
|
|
self._flags |= QWebPage.FindWrapsAroundDocument
|
2014-12-29 22:45:26 +01:00
|
|
|
if reverse:
|
2014-02-24 19:11:43 +01:00
|
|
|
self._flags |= QWebPage.FindBackward
|
2014-06-26 11:10:31 +02:00
|
|
|
# We actually search *twice* - once to highlight everything, then again
|
|
|
|
# to get a mark so we can navigate.
|
2014-02-24 19:11:43 +01:00
|
|
|
self.do_search.emit(self._text, self._flags)
|
2014-06-26 11:10:31 +02:00
|
|
|
self.do_search.emit(self._text, self._flags |
|
|
|
|
QWebPage.HighlightAllOccurrences)
|
2014-02-24 19:11:43 +01:00
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
def search_rev(self, text):
|
|
|
|
"""Search for a text on a website in reverse direction.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The text to search for.
|
|
|
|
"""
|
2014-12-29 22:45:26 +01:00
|
|
|
self.search(text, reverse=True)
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-09-28 22:13:14 +02:00
|
|
|
@cmdutils.register(instance='search-runner', hide=True, scope='window')
|
2014-10-09 06:33:24 +02:00
|
|
|
def search_next(self, count: {'special': 'count'}=1):
|
2014-02-24 19:11:43 +01:00
|
|
|
"""Continue the search to the ([count]th) next term.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
count: How many elements to ignore.
|
|
|
|
"""
|
|
|
|
if self._text is not None:
|
2014-04-16 10:02:34 +02:00
|
|
|
for _ in range(count):
|
2014-02-24 19:11:43 +01:00
|
|
|
self.do_search.emit(self._text, self._flags)
|
|
|
|
|
2014-09-28 22:13:14 +02:00
|
|
|
@cmdutils.register(instance='search-runner', hide=True, scope='window')
|
2014-10-09 06:33:24 +02:00
|
|
|
def search_prev(self, count: {'special': 'count'}=1):
|
2014-05-19 05:05:54 +02:00
|
|
|
"""Continue the search to the ([count]th) previous term.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
count: How many elements to ignore.
|
|
|
|
"""
|
|
|
|
if self._text is None:
|
|
|
|
return
|
|
|
|
# The int() here serves as a QFlags constructor to create a copy of the
|
|
|
|
# QFlags instance rather as a reference. I don't know why it works this
|
|
|
|
# way, but it does.
|
|
|
|
flags = int(self._flags)
|
|
|
|
if flags & QWebPage.FindBackward:
|
|
|
|
flags &= ~QWebPage.FindBackward
|
|
|
|
else:
|
|
|
|
flags |= QWebPage.FindBackward
|
|
|
|
for _ in range(count):
|
|
|
|
self.do_search.emit(self._text, flags)
|
|
|
|
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-09-29 20:16:38 +02:00
|
|
|
class CommandRunner(QObject):
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-08-06 15:38:25 +02:00
|
|
|
"""Parse and run qutebrowser commandline commands.
|
2014-02-24 19:11:43 +01:00
|
|
|
|
|
|
|
Attributes:
|
2014-09-28 22:13:14 +02:00
|
|
|
_win_id: The window this CommandRunner is associated with.
|
2014-02-24 19:11:43 +01:00
|
|
|
"""
|
|
|
|
|
2014-09-29 20:16:38 +02:00
|
|
|
def __init__(self, win_id, parent=None):
|
|
|
|
super().__init__(parent)
|
2014-09-28 22:13:14 +02:00
|
|
|
self._win_id = win_id
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-12-28 22:08:38 +01:00
|
|
|
def _get_alias(self, text):
|
2014-06-03 06:46:13 +02:00
|
|
|
"""Get an alias from the config.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The text to parse.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
None if no alias was found.
|
|
|
|
The new command string if an alias was found.
|
|
|
|
"""
|
|
|
|
parts = text.strip().split(maxsplit=1)
|
|
|
|
try:
|
|
|
|
alias = config.get('aliases', parts[0])
|
2014-12-17 11:17:18 +01:00
|
|
|
except (configexc.NoOptionError, configexc.NoSectionError):
|
2014-06-03 06:46:13 +02:00
|
|
|
return None
|
|
|
|
try:
|
|
|
|
new_cmd = '{} {}'.format(alias, parts[1])
|
|
|
|
except IndexError:
|
2014-12-28 22:08:38 +01:00
|
|
|
new_cmd = alias
|
2014-06-03 06:46:13 +02:00
|
|
|
if text.endswith(' '):
|
|
|
|
new_cmd += ' '
|
|
|
|
return new_cmd
|
|
|
|
|
2015-04-13 07:16:41 +02:00
|
|
|
def parse_all(self, text, *args, **kwargs):
|
|
|
|
"""Split a command on ;; and parse all parts.
|
|
|
|
|
|
|
|
If the first command in the commandline is a non-split one, it only
|
|
|
|
returns that.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: Text to parse.
|
|
|
|
*args/**kwargs: Passed to parse().
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
ParseResult tuples.
|
|
|
|
"""
|
|
|
|
if ';;' in text:
|
|
|
|
# Get the first command and check if it doesn't want to have ;;
|
|
|
|
# split.
|
|
|
|
first = text.split(';;')[0]
|
|
|
|
result = self.parse(first, *args, **kwargs)
|
|
|
|
if result.cmd.no_cmd_split:
|
|
|
|
sub_texts = [text]
|
|
|
|
else:
|
|
|
|
sub_texts = [e.strip() for e in text.split(';;')]
|
|
|
|
else:
|
|
|
|
sub_texts = [text]
|
|
|
|
for sub in sub_texts:
|
|
|
|
yield self.parse(sub, *args, **kwargs)
|
|
|
|
|
|
|
|
def parse(self, text, *, aliases=True, fallback=False, keep=False):
|
2014-02-24 19:11:43 +01:00
|
|
|
"""Split the commandline text into command and arguments.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: Text to parse.
|
|
|
|
aliases: Whether to handle aliases.
|
2014-06-03 06:46:13 +02:00
|
|
|
fallback: Whether to do a fallback splitting when the command was
|
|
|
|
unknown.
|
2014-11-06 07:15:02 +01:00
|
|
|
keep: Whether to keep special chars and whitespace
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-04-09 06:58:17 +02:00
|
|
|
Return:
|
2015-04-13 07:16:41 +02:00
|
|
|
A (cmd, args, cmdline) ParseResult tuple.
|
2014-02-24 19:11:43 +01:00
|
|
|
"""
|
2014-11-06 07:15:02 +01:00
|
|
|
cmdstr, sep, argstr = text.partition(' ')
|
2014-11-10 07:59:39 +01:00
|
|
|
if not cmdstr and not fallback:
|
2014-08-26 19:10:14 +02:00
|
|
|
raise cmdexc.NoSuchCommandError("No command given")
|
2014-02-24 19:11:43 +01:00
|
|
|
if aliases:
|
2014-12-28 22:08:38 +01:00
|
|
|
new_cmd = self._get_alias(text)
|
2014-06-03 06:46:13 +02:00
|
|
|
if new_cmd is not None:
|
2014-08-26 20:15:41 +02:00
|
|
|
log.commands.debug("Re-parsing with '{}'.".format(new_cmd))
|
2014-12-15 23:06:42 +01:00
|
|
|
return self.parse(new_cmd, aliases=False, fallback=fallback,
|
|
|
|
keep=keep)
|
2014-02-24 19:11:43 +01:00
|
|
|
try:
|
2015-04-13 07:16:41 +02:00
|
|
|
cmd = cmdutils.cmd_dict[cmdstr]
|
2014-02-24 19:11:43 +01:00
|
|
|
except KeyError:
|
2015-04-13 07:16:41 +02:00
|
|
|
if fallback:
|
|
|
|
cmd = None
|
|
|
|
args = None
|
|
|
|
if keep:
|
|
|
|
cmdstr, sep, argstr = text.partition(' ')
|
|
|
|
cmdline = [cmdstr, sep] + argstr.split()
|
|
|
|
else:
|
|
|
|
cmdline = text.split()
|
2014-06-03 06:46:13 +02:00
|
|
|
else:
|
2015-04-13 07:16:41 +02:00
|
|
|
raise cmdexc.NoSuchCommandError('{}: no such command'.format(
|
|
|
|
cmdstr))
|
2014-11-06 07:15:02 +01:00
|
|
|
else:
|
2015-04-13 07:16:41 +02:00
|
|
|
args = self._split_args(cmd, argstr, keep)
|
|
|
|
if keep and args:
|
|
|
|
cmdline = [cmdstr, sep + args[0]] + args[1:]
|
|
|
|
elif keep:
|
|
|
|
cmdline = [cmdstr, sep]
|
|
|
|
else:
|
|
|
|
cmdline = [cmdstr] + args[:]
|
|
|
|
return ParseResult(cmd=cmd, args=args, cmdline=cmdline)
|
2014-11-06 07:15:02 +01:00
|
|
|
|
2015-04-13 07:16:41 +02:00
|
|
|
def _split_args(self, cmd, argstr, keep):
|
2014-11-06 07:15:02 +01:00
|
|
|
"""Split the arguments from an arg string.
|
2014-09-03 10:47:27 +02:00
|
|
|
|
2014-11-06 07:15:02 +01:00
|
|
|
Args:
|
2015-04-13 07:16:41 +02:00
|
|
|
cmd: The command we're currently handling.
|
2014-11-06 07:15:02 +01:00
|
|
|
argstr: An argument string.
|
|
|
|
keep: Whether to keep special chars and whitespace
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A list containing the splitted strings.
|
|
|
|
"""
|
|
|
|
if not argstr:
|
2015-04-13 07:16:41 +02:00
|
|
|
return []
|
|
|
|
elif cmd.maxsplit is None:
|
|
|
|
return split.split(argstr, keep=keep)
|
2014-02-24 19:11:43 +01:00
|
|
|
else:
|
2014-09-02 21:54:07 +02:00
|
|
|
# If split=False, we still want to split the flags, but not
|
|
|
|
# everything after that.
|
|
|
|
# We first split the arg string and check the index of the first
|
|
|
|
# non-flag args, then we re-split again properly.
|
|
|
|
# example:
|
|
|
|
#
|
|
|
|
# input: "--foo -v bar baz"
|
|
|
|
# first split: ['--foo', '-v', 'bar', 'baz']
|
|
|
|
# 0 1 2 3
|
|
|
|
# second split: ['--foo', '-v', 'bar baz']
|
|
|
|
# (maxsplit=2)
|
2014-11-10 08:14:45 +01:00
|
|
|
split_args = split.simple_split(argstr, keep=keep)
|
2014-12-11 21:17:43 +01:00
|
|
|
flag_arg_count = 0
|
2014-09-02 21:54:07 +02:00
|
|
|
for i, arg in enumerate(split_args):
|
2014-11-09 20:46:21 +01:00
|
|
|
arg = arg.strip()
|
2014-12-11 21:17:43 +01:00
|
|
|
if arg.startswith('-'):
|
2015-04-13 21:12:14 +02:00
|
|
|
if arg in cmd.flags_with_args:
|
2014-12-11 21:17:43 +01:00
|
|
|
flag_arg_count += 1
|
|
|
|
else:
|
2015-04-13 07:16:41 +02:00
|
|
|
maxsplit = i + cmd.maxsplit + flag_arg_count
|
|
|
|
return split.simple_split(argstr, keep=keep,
|
|
|
|
maxsplit=maxsplit)
|
|
|
|
else: # pylint: disable=useless-else-on-loop
|
2014-09-02 21:54:07 +02:00
|
|
|
# If there are only flags, we got it right on the first try
|
|
|
|
# already.
|
2015-04-13 07:16:41 +02:00
|
|
|
return split_args
|
2014-02-24 19:11:43 +01:00
|
|
|
|
2014-04-30 10:41:25 +02:00
|
|
|
def run(self, text, count=None):
|
2014-09-02 21:54:07 +02:00
|
|
|
"""Parse a command from a line of text and run it.
|
2014-02-24 19:11:43 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The text to parse.
|
|
|
|
count: The count to pass to the command.
|
|
|
|
"""
|
2015-04-13 07:16:41 +02:00
|
|
|
for result in self.parse_all(text):
|
|
|
|
args = replace_variables(self._win_id, result.args)
|
|
|
|
if count is not None:
|
|
|
|
result.cmd.run(self._win_id, args, count=count)
|
|
|
|
else:
|
|
|
|
result.cmd.run(self._win_id, args)
|
2014-04-30 10:41:25 +02:00
|
|
|
|
|
|
|
@pyqtSlot(str, int)
|
|
|
|
def run_safely(self, text, count=None):
|
|
|
|
"""Run a command and display exceptions in the statusbar."""
|
|
|
|
try:
|
|
|
|
self.run(text, count)
|
2014-08-26 19:10:14 +02:00
|
|
|
except (cmdexc.CommandMetaError, cmdexc.CommandError) as e:
|
2014-09-28 22:13:14 +02:00
|
|
|
message.error(self._win_id, e, immediately=True)
|
2014-05-19 03:40:10 +02:00
|
|
|
|
|
|
|
@pyqtSlot(str, int)
|
|
|
|
def run_safely_init(self, text, count=None):
|
|
|
|
"""Run a command and display exceptions in the statusbar.
|
|
|
|
|
|
|
|
Contrary to run_safely, error messages are queued so this is more
|
|
|
|
suitable to use while initializing."""
|
|
|
|
try:
|
|
|
|
self.run(text, count)
|
2014-08-26 19:10:14 +02:00
|
|
|
except (cmdexc.CommandMetaError, cmdexc.CommandError) as e:
|
2014-09-28 22:13:14 +02:00
|
|
|
message.error(self._win_id, e)
|