Return Match instead bool in _handle_single_key.

This commit is contained in:
Florian Bruhin 2015-02-27 10:49:52 +01:00
parent d66997610b
commit 42e2438efb
2 changed files with 26 additions and 20 deletions

View File

@ -71,7 +71,7 @@ class BaseKeyParser(QObject):
do_log = True
Match = usertypes.enum('Match', ['partial', 'definitive', 'ambiguous',
'none'])
'other', 'none'])
Type = usertypes.enum('Type', ['chain', 'special'])
def __init__(self, win_id, parent=None, supports_count=None,
@ -153,7 +153,7 @@ class BaseKeyParser(QObject):
e: the KeyPressEvent from Qt.
Return:
True if event has been handled, False otherwise.
A self.Match member.
"""
txt = e.text()
key = e.key()
@ -163,7 +163,7 @@ class BaseKeyParser(QObject):
self._debug_log("Escape pressed, discarding '{}'.".format(
self._keystring))
self._keystring = ''
return
return self.Match.none
if len(txt) == 1:
category = unicodedata.category(txt) # pylint: disable=no-member
@ -173,7 +173,7 @@ class BaseKeyParser(QObject):
if (not txt) or is_control_char:
self._debug_log("Ignoring, no text char")
return False
return self.Match.none
self._stop_delayed_exec()
self._keystring += txt
@ -182,7 +182,7 @@ class BaseKeyParser(QObject):
if not cmd_input:
# Only a count, no command yet, but we handled it
return True
return self.Match.other
match, binding = self._match_key(cmd_input)
@ -205,8 +205,7 @@ class BaseKeyParser(QObject):
self._debug_log("Giving up with '{}', no matches".format(
self._keystring))
self._keystring = ''
return False
return True
return match
def _match_key(self, cmd_input):
"""Try to match a given keystring with any bound keychain.
@ -296,13 +295,16 @@ class BaseKeyParser(QObject):
Args:
e: the KeyPressEvent from Qt
Return:
True if the event was handled, False otherwise.
"""
handled = self._handle_special_key(e)
if handled or not self._supports_chains:
return handled
handled = self._handle_single_key(e)
match = self._handle_single_key(e)
self.keystring_updated.emit(self._keystring)
return handled
return match != self.Match.none
def read_config(self, modename=None):
"""Read the configuration.

View File

@ -54,12 +54,12 @@ class NormalKeyParser(keyparser.CommandKeyParser):
e: the KeyPressEvent from Qt.
Return:
True if event has been handled, False otherwise.
A self.Match member.
"""
txt = e.text().strip()
if not self._keystring and any(txt == c for c in STARTCHARS):
message.set_cmd_text(self._win_id, txt)
return True
return self.Match.definitive
return super()._handle_single_key(e)
@ -140,21 +140,25 @@ class HintKeyParser(keyparser.CommandKeyParser):
Args:
e: the KeyPressEvent from Qt
Returns:
True if the match has been handled, False otherwise.
"""
handled = self._handle_single_key(e)
if handled and self._keystring:
# A key has been added to the keystring (Match.partial)
match = self._handle_single_key(e)
if match == self.Match.partial:
self.keystring_updated.emit(self._keystring)
self._last_press = LastPress.keystring
return handled
elif handled:
# We handled the key but the keystring is empty. This happens when
# match is Match.definitive, so a keychain has been completed.
return True
elif match == self.Match.definitive:
self._last_press = LastPress.none
return handled
else:
return True
elif match == self.Match.other:
pass
elif match == self.Match.none:
# We couldn't find a keychain so we check if it's a special key.
return self._handle_special_key(e)
else:
raise ValueError("Got invalid match type {}!".format(match))
def execute(self, cmdstr, keytype, count=None):
"""Handle a completed keychain."""