qutebrowser/qutebrowser/commands/managers.py

233 lines
6.9 KiB
Python
Raw Normal View History

2014-02-24 19:11:43 +01:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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/>.
"""Module containing command managers (SearchManager and CommandManager)."""
2014-02-24 19:11:43 +01:00
2014-05-01 00:23:53 +02:00
import logging
2014-02-24 19:11:43 +01:00
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject
2014-02-24 16:47:32 +01:00
from PyQt5.QtWebKitWidgets import QWebPage
2014-02-24 19:11:43 +01:00
import qutebrowser.config.config as config
import qutebrowser.commands.utils as cmdutils
2014-04-09 20:47:24 +02:00
import qutebrowser.utils.message as message
2014-05-14 18:00:40 +02:00
from qutebrowser.commands.exceptions import (NoSuchCommandError,
CommandMetaError, CommandError)
from qutebrowser.utils.misc import safe_shlex_split
2014-02-24 19:11:43 +01:00
2014-02-24 16:47:32 +01:00
2014-04-17 17:44:27 +02:00
def split_cmdline(text):
"""Convenience function to split a commandline into it's logical parts.
Args:
text: The string to split.
Return:
A list of strings.
"""
logging.debug("Splitting '{}'".format(text))
manager = CommandManager()
original_cmd = text.strip().split(maxsplit=1)
2014-04-17 17:44:27 +02:00
try:
parts = manager.parse(text)
2014-04-17 17:44:27 +02:00
except NoSuchCommandError:
parts = text.split(' ')
2014-05-01 00:23:53 +02:00
if text.endswith(' '):
parts.append('')
logging.debug("Split parts: {}".format(parts))
if len(parts) == 1 and parts[0]:
parts = original_cmd
2014-04-17 17:44:27 +02:00
return parts
class SearchManager(QObject):
2014-02-24 19:11:43 +01:00
"""Manage qutebrowser searches.
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
def _search(self, text, rev=False):
"""Search for a text on the current page.
Args:
text: The text to search for.
rev: Search direction, True if reverse, else False.
Emit:
do_search: If a search should be started.
"""
if self._text is not None and self._text != text:
self.do_search.emit('', 0)
self._text = text
self._flags = 0
2014-04-27 21:21:14 +02:00
if config.get('general', '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
if rev:
self._flags |= QWebPage.FindBackward
self.do_search.emit(self._text, self._flags)
@pyqtSlot(str)
def search(self, text):
"""Search for a text on a website.
Args:
text: The text to search for.
"""
self._search(text)
@pyqtSlot(str)
def search_rev(self, text):
"""Search for a text on a website in reverse direction.
Args:
text: The text to search for.
"""
self._search(text, rev=True)
@cmdutils.register(instance='searchmanager', hide=True)
2014-02-24 19:11:43 +01:00
def nextsearch(self, count=1):
"""Continue the search to the ([count]th) next term.
Args:
count: How many elements to ignore.
Emit:
do_search: If a search should be started.
"""
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)
class CommandManager:
2014-02-24 19:11:43 +01:00
"""Manage qutebrowser commandline commands.
2014-02-24 19:11:43 +01:00
Attributes:
_cmd: The command which was parsed.
_args: The arguments which were parsed.
"""
2014-04-10 07:42:21 +02:00
def __init__(self):
2014-02-24 19:11:43 +01:00
self._cmd = None
self._args = []
2014-04-09 06:58:17 +02:00
def parse(self, text, aliases=True):
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.
Raise:
NoSuchCommandError if a command wasn't found.
2014-04-09 06:58:17 +02:00
Return:
2014-04-10 06:58:58 +02:00
A split string commandline, e.g ['open', 'www.google.com']
2014-02-24 19:11:43 +01:00
"""
parts = text.strip().split(maxsplit=1)
if not parts:
raise NoSuchCommandError("No command given")
cmdstr = parts[0]
if aliases:
try:
alias = config.get('aliases', cmdstr)
except (config.NoOptionError, config.NoSectionError):
2014-02-24 19:11:43 +01:00
pass
else:
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
new_cmd = alias
if text.endswith(' '):
new_cmd += ' '
logging.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False)
2014-02-24 19:11:43 +01:00
try:
cmd = cmdutils.cmd_dict[cmdstr]
except KeyError:
2014-04-30 10:41:25 +02:00
raise NoSuchCommandError('{}: no such command'.format(cmdstr))
2014-02-24 19:11:43 +01:00
if len(parts) == 1:
args = []
elif cmd.split:
args = safe_shlex_split(parts[1])
2014-02-24 19:11:43 +01:00
else:
args = parts[1].split(maxsplit=cmd.nargs[0] - 1)
2014-02-24 19:11:43 +01:00
self._cmd = cmd
self._args = args
2014-05-01 00:23:53 +02:00
retargs = args[:]
if text.endswith(' ') and (cmd.split is True or
2014-05-02 11:28:11 +02:00
len(args) < cmd.nargs[0]):
2014-05-01 00:23:53 +02:00
retargs.append('')
return [cmdstr] + retargs
2014-02-24 19:11:43 +01:00
def _check(self):
"""Check if the argument count for the command is correct."""
self._cmd.check(self._args)
def _run(self, count=None):
"""Run a command with an optional count.
Args:
count: Count to pass to the command.
"""
if count is not None:
self._cmd.run(self._args, count=count)
else:
self._cmd.run(self._args)
2014-04-30 10:41:25 +02:00
def run(self, text, count=None):
2014-02-24 19:11:43 +01:00
"""Parse a command from a line of text.
Args:
text: The text to parse.
count: The count to pass to the command.
"""
if ';;' in text:
for sub in text.split(';;'):
2014-04-30 10:41:25 +02:00
self.run(sub, count)
2014-04-30 10:47:46 +02:00
return
2014-04-30 10:41:25 +02:00
self.parse(text)
self._check()
2014-02-24 19:11:43 +01:00
self._run(count=count)
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-05-14 18:00:40 +02:00
except (CommandMetaError, CommandError) as e:
message.error(e)