Get rid of modeman.maybe_leave

This commit is contained in:
Florian Bruhin 2016-11-10 07:19:45 +01:00
parent 2ef85d6c35
commit bbd842bd82
6 changed files with 21 additions and 31 deletions

View File

@ -742,8 +742,8 @@ class CommandDispatcher:
message.info("{} {} yanked to {}".format( message.info("{} {} yanked to {}".format(
len(s), "char" if len(s) == 1 else "chars", target)) len(s), "char" if len(s) == 1 else "chars", target))
if not keep: if not keep:
modeman.maybe_leave(self._win_id, KeyMode.caret, modeman.leave(self._win_id, KeyMode.caret, "yank selected",
"yank selected") maybe=True)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True) @cmdutils.argument('count', count=True)

View File

@ -50,7 +50,8 @@ class HintingError(Exception):
def on_mode_entered(mode, win_id): def on_mode_entered(mode, win_id):
"""Stop hinting when insert mode was entered.""" """Stop hinting when insert mode was entered."""
if mode == usertypes.KeyMode.insert: if mode == usertypes.KeyMode.insert:
modeman.maybe_leave(win_id, usertypes.KeyMode.hint, 'insert mode') modeman.leave(win_id, usertypes.KeyMode.hint, 'insert mode',
maybe=True)
class HintLabel(QLabel): class HintLabel(QLabel):
@ -859,8 +860,8 @@ class HintManager(QObject):
raise ValueError("No suitable handler found!") raise ValueError("No suitable handler found!")
if not self._context.rapid: if not self._context.rapid:
modeman.maybe_leave(self._win_id, usertypes.KeyMode.hint, modeman.leave(self._win_id, usertypes.KeyMode.hint, 'followed',
'followed') maybe=True)
else: else:
# Reset filtering # Reset filtering
self.filter_hints(None) self.filter_hints(None)

View File

@ -151,9 +151,8 @@ class MouseEventFilter(QObject):
else: else:
log.mouse.debug("Clicked non-editable element!") log.mouse.debug("Clicked non-editable element!")
if config.get('input', 'auto-leave-insert-mode'): if config.get('input', 'auto-leave-insert-mode'):
modeman.maybe_leave(self._tab.win_id, modeman.leave(self._tab.win_id, usertypes.KeyMode.insert,
usertypes.KeyMode.insert, 'click', maybe=True)
'click')
def _mouserelease_insertmode(self): def _mouserelease_insertmode(self):
"""If we have an insertmode check scheduled, handle it.""" """If we have an insertmode check scheduled, handle it."""
@ -174,9 +173,8 @@ class MouseEventFilter(QObject):
else: else:
log.mouse.debug("Clicked non-editable element (delayed)!") log.mouse.debug("Clicked non-editable element (delayed)!")
if config.get('input', 'auto-leave-insert-mode'): if config.get('input', 'auto-leave-insert-mode'):
modeman.maybe_leave(self._tab.win_id, modeman.leave(self._tab.win_id, usertypes.KeyMode.insert,
usertypes.KeyMode.insert, 'click-delayed', maybe=True)
'click-delayed')
self._tab.elements.find_focused(mouserelease_insertmode_cb) self._tab.elements.find_focused(mouserelease_insertmode_cb)

View File

@ -106,18 +106,9 @@ def enter(win_id, mode, reason=None, only_if_normal=False):
instance(win_id).enter(mode, reason, only_if_normal) instance(win_id).enter(mode, reason, only_if_normal)
def leave(win_id, mode, reason=None): def leave(win_id, mode, reason=None, *, maybe=False):
"""Leave the mode 'mode'.""" """Leave the mode 'mode'."""
instance(win_id).leave(mode, reason) instance(win_id).leave(mode, reason, maybe=maybe)
def maybe_leave(win_id, mode, reason=None):
"""Convenience method to leave 'mode' without exceptions."""
try:
instance(win_id).leave(mode, reason)
except NotInModeError as e:
# This is rather likely to happen, so we only log to debug log.
log.modes.debug("{} (leave reason: {})".format(e, reason))
class ModeManager(QObject): class ModeManager(QObject):

View File

@ -314,8 +314,8 @@ class PromptContainer(QWidget):
if not question.interrupted: if not question.interrupted:
# If this question was interrupted, we already connected the signal # If this question was interrupted, we already connected the signal
question.aborted.connect( question.aborted.connect(
lambda: modeman.maybe_leave(self._win_id, prompt.KEY_MODE, lambda: modeman.leave(self._win_id, prompt.KEY_MODE, 'aborted',
'aborted')) maybe=True))
modeman.enter(self._win_id, prompt.KEY_MODE, 'question asked') modeman.enter(self._win_id, prompt.KEY_MODE, 'question asked')
self.setSizePolicy(prompt.sizePolicy()) self.setSizePolicy(prompt.sizePolicy())
@ -328,7 +328,7 @@ class PromptContainer(QWidget):
@pyqtSlot(usertypes.KeyMode) @pyqtSlot(usertypes.KeyMode)
def _on_prompt_done(self, key_mode): def _on_prompt_done(self, key_mode):
"""Leave the prompt mode in this window if a question was answered.""" """Leave the prompt mode in this window if a question was answered."""
modeman.maybe_leave(self._win_id, key_mode, ':prompt-accept') modeman.leave(self._win_id, key_mode, ':prompt-accept', maybe=True)
@pyqtSlot(usertypes.KeyMode) @pyqtSlot(usertypes.KeyMode)
def _on_global_mode_left(self, mode): def _on_global_mode_left(self, mode):
@ -339,7 +339,7 @@ class PromptContainer(QWidget):
""" """
if mode not in [usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]: if mode not in [usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]:
return return
modeman.maybe_leave(self._win_id, mode, 'left in other window') modeman.leave(self._win_id, mode, 'left in other window', maybe=True)
item = self._layout.takeAt(0) item = self._layout.takeAt(0)
if item is not None: if item is not None:
widget = item.widget() widget = item.widget()

View File

@ -473,10 +473,10 @@ class TabbedBrowser(tabwidget.TabWidget):
@pyqtSlot() @pyqtSlot()
def on_cur_load_started(self): def on_cur_load_started(self):
"""Leave insert/hint mode when loading started.""" """Leave insert/hint mode when loading started."""
modeman.maybe_leave(self._win_id, usertypes.KeyMode.insert, modeman.leave(self._win_id, usertypes.KeyMode.insert, 'load started',
'load started') maybe=True)
modeman.maybe_leave(self._win_id, usertypes.KeyMode.hint, modeman.leave(self._win_id, usertypes.KeyMode.hint, 'load started',
'load started') maybe=True)
@pyqtSlot(browsertab.AbstractTab, str) @pyqtSlot(browsertab.AbstractTab, str)
def on_title_changed(self, tab, text): def on_title_changed(self, tab, text):
@ -567,7 +567,7 @@ class TabbedBrowser(tabwidget.TabWidget):
tab.setFocus() tab.setFocus()
for mode in [usertypes.KeyMode.hint, usertypes.KeyMode.insert, for mode in [usertypes.KeyMode.hint, usertypes.KeyMode.insert,
usertypes.KeyMode.caret, usertypes.KeyMode.passthrough]: usertypes.KeyMode.caret, usertypes.KeyMode.passthrough]:
modeman.maybe_leave(self._win_id, mode, 'tab changed') modeman.leave(self._win_id, mode, 'tab changed', maybe=True)
if self._now_focused is not None: if self._now_focused is not None:
objreg.register('last-focused-tab', self._now_focused, update=True, objreg.register('last-focused-tab', self._now_focused, update=True,
scope='window', window=self._win_id) scope='window', window=self._win_id)