2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2014-04-25 12:34:17 +02: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/>.
|
|
|
|
|
|
|
|
"""KeyChainParser for "hint" and "normal" modes.
|
|
|
|
|
|
|
|
Module attributes:
|
|
|
|
STARTCHARS: Possible chars for starting a commandline input.
|
|
|
|
"""
|
|
|
|
|
2014-05-02 17:53:16 +02:00
|
|
|
from PyQt5.QtCore import pyqtSignal, Qt
|
2014-04-25 12:34:17 +02:00
|
|
|
|
|
|
|
import qutebrowser.utils.message as message
|
2014-05-02 17:53:16 +02:00
|
|
|
import qutebrowser.config.config as config
|
2014-04-25 12:34:17 +02:00
|
|
|
from qutebrowser.keyinput.keyparser import CommandKeyParser
|
2014-05-06 17:04:06 +02:00
|
|
|
from qutebrowser.utils.usertypes import enum
|
2014-05-23 16:11:55 +02:00
|
|
|
from qutebrowser.utils.log import keyboard as logger
|
2014-04-25 12:34:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
STARTCHARS = ":/?"
|
2014-07-28 20:41:42 +02:00
|
|
|
LastPress = enum('LastPress', 'none', 'filtertext', 'keystring')
|
2014-04-25 12:34:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NormalKeyParser(CommandKeyParser):
|
|
|
|
|
|
|
|
"""KeyParser for normalmode with added STARTCHARS detection."""
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent, supports_count=True, supports_chains=True)
|
|
|
|
self.read_config('keybind')
|
|
|
|
|
2014-06-17 11:03:42 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '<{}>'.format(self.__class__.__name__)
|
|
|
|
|
2014-04-25 12:34:17 +02:00
|
|
|
def _handle_single_key(self, e):
|
|
|
|
"""Override _handle_single_key to abort if the key is a startchar.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
e: the KeyPressEvent from Qt.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if event has been handled, False otherwise.
|
|
|
|
"""
|
|
|
|
txt = e.text().strip()
|
|
|
|
if not self._keystring and any(txt == c for c in STARTCHARS):
|
|
|
|
message.set_cmd_text(txt)
|
|
|
|
return True
|
|
|
|
return super()._handle_single_key(e)
|
|
|
|
|
|
|
|
|
2014-05-20 12:05:14 +02:00
|
|
|
class PromptKeyParser(CommandKeyParser):
|
|
|
|
|
|
|
|
"""KeyParser for yes/no prompts."""
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent, supports_count=False, supports_chains=True)
|
|
|
|
# We don't want an extra section for this in the config, so we just
|
|
|
|
# abuse the keybind.prompt section.
|
|
|
|
self.read_config('keybind.prompt')
|
|
|
|
|
2014-06-17 11:03:42 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '<{}>'.format(self.__class__.__name__)
|
|
|
|
|
2014-05-20 12:05:14 +02:00
|
|
|
|
2014-04-25 12:34:17 +02:00
|
|
|
class HintKeyParser(CommandKeyParser):
|
|
|
|
|
|
|
|
"""KeyChainParser for hints.
|
|
|
|
|
|
|
|
Signals:
|
|
|
|
fire_hint: When a hint keybinding was completed.
|
|
|
|
Arg: the keystring/hint string pressed.
|
2014-05-02 17:53:16 +02:00
|
|
|
filter_hints: When the filter text changed.
|
|
|
|
Arg: the text to filter hints with.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
_filtertext: The text to filter with.
|
2014-05-06 17:04:06 +02:00
|
|
|
_last_press: The nature of the last keypress, a LastPress member.
|
2014-04-25 12:34:17 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
fire_hint = pyqtSignal(str)
|
2014-05-02 17:53:16 +02:00
|
|
|
filter_hints = pyqtSignal(str)
|
2014-04-25 12:34:17 +02:00
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent, supports_count=False, supports_chains=True)
|
2014-05-02 17:53:16 +02:00
|
|
|
self._filtertext = ''
|
2014-05-06 17:04:06 +02:00
|
|
|
self._last_press = LastPress.none
|
2014-04-25 12:34:17 +02:00
|
|
|
self.read_config('keybind.hint')
|
|
|
|
|
2014-05-02 17:53:16 +02:00
|
|
|
def _handle_special_key(self, e):
|
|
|
|
"""Override _handle_special_key to handle string filtering.
|
|
|
|
|
|
|
|
Return True if the keypress has been handled, and False if not.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
e: the KeyPressEvent from Qt.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if event has been handled, False otherwise.
|
|
|
|
|
|
|
|
Emit:
|
|
|
|
filter_hints: Emitted when filter string has changed.
|
2014-05-06 17:04:34 +02:00
|
|
|
keystring_updated: Emitted when keystring has been changed.
|
2014-05-02 17:53:16 +02:00
|
|
|
"""
|
2014-07-03 06:30:50 +02:00
|
|
|
logger.debug("Got special key 0x{:x} text {}".format(
|
|
|
|
e.key(), e.text()))
|
2014-05-06 17:04:34 +02:00
|
|
|
if e.key() == Qt.Key_Backspace:
|
2014-05-23 16:11:55 +02:00
|
|
|
logger.debug("Got backspace, mode {}, filtertext '{}', keystring "
|
2014-07-28 20:41:42 +02:00
|
|
|
"'{}'".format(self._last_press, self._filtertext,
|
|
|
|
self._keystring))
|
2014-05-06 17:04:34 +02:00
|
|
|
if self._last_press == LastPress.filtertext and self._filtertext:
|
2014-05-02 17:53:16 +02:00
|
|
|
self._filtertext = self._filtertext[:-1]
|
|
|
|
self.filter_hints.emit(self._filtertext)
|
2014-05-06 17:04:34 +02:00
|
|
|
return True
|
|
|
|
elif self._last_press == LastPress.keystring and self._keystring:
|
|
|
|
self._keystring = self._keystring[:-1]
|
|
|
|
self.keystring_updated.emit(self._keystring)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return super()._handle_special_key(e)
|
|
|
|
elif config.get('hints', 'mode') != 'number':
|
|
|
|
return super()._handle_special_key(e)
|
2014-05-02 17:53:16 +02:00
|
|
|
elif not e.text():
|
|
|
|
return super()._handle_special_key(e)
|
|
|
|
else:
|
|
|
|
self._filtertext += e.text()
|
|
|
|
self.filter_hints.emit(self._filtertext)
|
2014-05-06 17:04:06 +02:00
|
|
|
self._last_press = LastPress.filtertext
|
2014-05-02 17:53:16 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def handle(self, e):
|
|
|
|
"""Handle a new keypress and call the respective handlers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
e: the KeyPressEvent from Qt
|
|
|
|
|
|
|
|
Emit:
|
|
|
|
keystring_updated: If a new keystring should be set.
|
|
|
|
"""
|
|
|
|
handled = self._handle_single_key(e)
|
2014-06-13 14:52:54 +02:00
|
|
|
if handled and self._keystring:
|
|
|
|
# A key has been added to the keystring (Match.partial)
|
2014-05-02 17:53:16 +02:00
|
|
|
self.keystring_updated.emit(self._keystring)
|
2014-05-06 17:04:06 +02:00
|
|
|
self._last_press = LastPress.keystring
|
2014-05-02 17:53:16 +02:00
|
|
|
return handled
|
2014-06-13 14:52:54 +02:00
|
|
|
elif handled:
|
|
|
|
# We handled the key but the keystring is empty. This happens when
|
|
|
|
# match is Match.definitive, so a keychain has been completed.
|
|
|
|
self._last_press = LastPress.none
|
|
|
|
return handled
|
|
|
|
else:
|
|
|
|
# We couldn't find a keychain so we check if it's a special key.
|
|
|
|
return self._handle_special_key(e)
|
2014-05-02 17:53:16 +02:00
|
|
|
|
2014-04-25 12:34:17 +02:00
|
|
|
def execute(self, cmdstr, keytype, count=None):
|
|
|
|
"""Handle a completed keychain.
|
|
|
|
|
|
|
|
Emit:
|
2014-05-05 07:45:36 +02:00
|
|
|
fire_hint: Emitted if keytype is chain
|
2014-04-25 12:34:17 +02:00
|
|
|
"""
|
2014-05-05 07:45:36 +02:00
|
|
|
if keytype == self.Type.chain:
|
2014-04-25 12:34:17 +02:00
|
|
|
self.fire_hint.emit(cmdstr)
|
|
|
|
else:
|
|
|
|
# execute as command
|
|
|
|
super().execute(cmdstr, keytype, count)
|
|
|
|
|
|
|
|
def on_hint_strings_updated(self, strings):
|
|
|
|
"""Handler for HintManager's hint_strings_updated.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
strings: A list of hint strings.
|
|
|
|
"""
|
|
|
|
self.bindings = {s: s for s in strings}
|
2014-05-02 17:53:16 +02:00
|
|
|
self._filtertext = ''
|