Try getting hints to work

This commit is contained in:
Florian Bruhin 2018-02-26 10:14:30 +01:00
parent d077f38ac4
commit be4cd94207
3 changed files with 39 additions and 23 deletions

View File

@ -118,7 +118,7 @@ class BaseKeyParser(QObject):
e: the KeyPressEvent from Qt. e: the KeyPressEvent from Qt.
Return: Return:
A self.Match member. A QKeySequence match or None.
""" """
key = e.key() key = e.key()
txt = keyutils.keyevent_to_string(e) txt = keyutils.keyevent_to_string(e)
@ -186,8 +186,10 @@ class BaseKeyParser(QObject):
- The found binding with Match.definitive. - The found binding with Match.definitive.
""" """
assert sequence assert sequence
assert not isinstance(sequence, str)
for seq, cmd in self.bindings.items(): for seq, cmd in self.bindings.items():
assert not isinstance(seq, str), seq
match = sequence.matches(seq) match = sequence.matches(seq)
if match != QKeySequence.NoMatch: if match != QKeySequence.NoMatch:
return (match, cmd) return (match, cmd)
@ -238,6 +240,7 @@ class BaseKeyParser(QObject):
self.bindings = {} self.bindings = {}
for key, cmd in config.key_instance.get_bindings_for(modename).items(): for key, cmd in config.key_instance.get_bindings_for(modename).items():
assert not isinstance(key, str), key
assert cmd assert cmd
self.bindings[key] = cmd self.bindings[key] = cmd

View File

@ -352,6 +352,7 @@ class KeySequence:
modifiers = ev.modifiers() modifiers = ev.modifiers()
if (modifiers == Qt.ShiftModifier and if (modifiers == Qt.ShiftModifier and
ev.text() and
unicodedata.category(ev.text()) != 'Lu'): unicodedata.category(ev.text()) != 'Lu'):
modifiers = Qt.KeyboardModifiers() modifiers = Qt.KeyboardModifiers()
@ -364,6 +365,17 @@ class KeySequence:
new._validate() new._validate()
return new return new
def remove_last(self):
"""Create a new KeySequence with the last key removed."""
new = self.__class__()
new._sequences = self._sequeces[:]
if len(new._sequences[-1]) == 1:
del new._sequences[-1]
else:
new._sequences[-1] = QKeySequence(*new._sequences[-1][:-1])
new._validate()
return new
@classmethod @classmethod
def parse(cls, keystr): def parse(cls, keystr):
"""Parse a keystring like <Ctrl-x> or xyz and return a KeySequence.""" """Parse a keystring like <Ctrl-x> or xyz and return a KeySequence."""

View File

@ -27,6 +27,7 @@ import traceback
import enum import enum
from PyQt5.QtCore import pyqtSlot, Qt from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtGui import QKeySequence
from qutebrowser.commands import cmdexc from qutebrowser.commands import cmdexc
from qutebrowser.config import config from qutebrowser.config import config
@ -73,9 +74,9 @@ class NormalKeyParser(keyparser.CommandKeyParser):
if self._inhibited: if self._inhibited:
self._debug_log("Ignoring key '{}', because the normal mode is " self._debug_log("Ignoring key '{}', because the normal mode is "
"currently inhibited.".format(txt)) "currently inhibited.".format(txt))
return self.Match.none return QKeySequence.NoMatch
match = super()._handle_single_key(e) match = super()._handle_single_key(e)
if match == self.Match.partial: if match == QKeySequence.PartialMatch:
timeout = config.val.input.partial_timeout timeout = config.val.input.partial_timeout
if timeout != 0: if timeout != 0:
self._partial_timer.setInterval(timeout) self._partial_timer.setInterval(timeout)
@ -97,9 +98,9 @@ class NormalKeyParser(keyparser.CommandKeyParser):
def _clear_partial_match(self): def _clear_partial_match(self):
"""Clear a partial keystring after a timeout.""" """Clear a partial keystring after a timeout."""
self._debug_log("Clearing partial keystring {}".format( self._debug_log("Clearing partial keystring {}".format(
self._keystring)) self._sequence))
self._keystring = '' self._sequence = keyutils.KeySequence()
self.keystring_updated.emit(self._keystring) self.keystring_updated.emit(str(self._sequence))
@pyqtSlot() @pyqtSlot()
def _clear_inhibited(self): def _clear_inhibited(self):
@ -174,28 +175,28 @@ class HintKeyParser(keyparser.CommandKeyParser):
window=self._win_id, tab='current') window=self._win_id, tab='current')
if e.key() == Qt.Key_Backspace: if e.key() == Qt.Key_Backspace:
log.keyboard.debug("Got backspace, mode {}, filtertext '{}', " log.keyboard.debug("Got backspace, mode {}, filtertext '{}', "
"keystring '{}'".format(self._last_press, "sequence '{}'".format(self._last_press,
self._filtertext, self._filtertext,
self._keystring)) self._sequence))
if self._last_press == LastPress.filtertext and self._filtertext: if self._last_press == LastPress.filtertext and self._filtertext:
self._filtertext = self._filtertext[:-1] self._filtertext = self._filtertext[:-1]
hintmanager.filter_hints(self._filtertext) hintmanager.filter_hints(self._filtertext)
return True return True
elif self._last_press == LastPress.keystring and self._keystring: elif self._last_press == LastPress.keystring and self._sequence:
self._keystring = self._keystring[:-1] self._sequence = self._sequence.remove_last()
self.keystring_updated.emit(self._keystring) self.keystring_updated.emit(str(self._sequence))
if not self._keystring and self._filtertext: if not self._sequence and self._filtertext:
# Switch back to hint filtering mode (this can happen only # Switch back to hint filtering mode (this can happen only
# in numeric mode after the number has been deleted). # in numeric mode after the number has been deleted).
hintmanager.filter_hints(self._filtertext) hintmanager.filter_hints(self._filtertext)
self._last_press = LastPress.filtertext self._last_press = LastPress.filtertext
return True return True
else: else:
return super()._handle_special_key(e) return False
elif hintmanager.current_mode() != 'number': elif hintmanager.current_mode() != 'number':
return super()._handle_special_key(e) return False
elif not e.text(): elif not e.text():
return super()._handle_special_key(e) return False
else: else:
self._filtertext += e.text() self._filtertext += e.text()
hintmanager.filter_hints(self._filtertext) hintmanager.filter_hints(self._filtertext)
@ -212,17 +213,17 @@ class HintKeyParser(keyparser.CommandKeyParser):
True if the match has been handled, False otherwise. True if the match has been handled, False otherwise.
""" """
# FIXME rewrite this # FIXME rewrite this
match = self._handle_single_key(e) match = super().handle(e)
if match == self.Match.partial: if match == QKeySequence.PartialMatch:
self.keystring_updated.emit(self._keystring) self.keystring_updated.emit(str(self._sequence))
self._last_press = LastPress.keystring self._last_press = LastPress.keystring
return True return True
elif match == self.Match.definitive: elif match == QKeySequence.ExactMatch:
self._last_press = LastPress.none self._last_press = LastPress.none
return True return True
elif match == self.Match.other: elif match is None: # FIXME
return None return None
elif match == self.Match.none: elif match == QKeySequence.NoMatch:
# We couldn't find a keychain so we check if it's a special key. # We couldn't find a keychain so we check if it's a special key.
return self._handle_special_key(e) return self._handle_special_key(e)
else: else:
@ -248,7 +249,7 @@ class HintKeyParser(keyparser.CommandKeyParser):
preserve_filter: Whether to keep the current value of preserve_filter: Whether to keep the current value of
`self._filtertext`. `self._filtertext`.
""" """
self.bindings = {s: s for s in strings} self.bindings = {keyutils.KeySequence(s): s for s in strings}
if not preserve_filter: if not preserve_filter:
self._filtertext = '' self._filtertext = ''