diff --git a/src/inputmode/kateabstractinputmode.h b/src/inputmode/kateabstractinputmode.h --- a/src/inputmode/kateabstractinputmode.h +++ b/src/inputmode/kateabstractinputmode.h @@ -53,7 +53,7 @@ virtual bool overwrite() const = 0; virtual void overwrittenChar(const QChar &) = 0; virtual void clearSelection() = 0; - virtual bool stealKey(const QKeyEvent *) const = 0; + virtual bool stealKey(QKeyEvent* k) = 0; virtual void gotFocus() = 0; virtual void lostFocus() = 0; diff --git a/src/inputmode/katenormalinputmode.h b/src/inputmode/katenormalinputmode.h --- a/src/inputmode/katenormalinputmode.h +++ b/src/inputmode/katenormalinputmode.h @@ -46,7 +46,7 @@ void overwrittenChar(const QChar &) Q_DECL_OVERRIDE; void clearSelection() Q_DECL_OVERRIDE; - bool stealKey(const QKeyEvent *) const Q_DECL_OVERRIDE; + bool stealKey(QKeyEvent *) Q_DECL_OVERRIDE; void gotFocus() Q_DECL_OVERRIDE; void lostFocus() Q_DECL_OVERRIDE; diff --git a/src/inputmode/katenormalinputmode.cpp b/src/inputmode/katenormalinputmode.cpp --- a/src/inputmode/katenormalinputmode.cpp +++ b/src/inputmode/katenormalinputmode.cpp @@ -67,7 +67,7 @@ view()->clearSelection(); } -bool KateNormalInputMode::stealKey(const QKeyEvent *) const +bool KateNormalInputMode::stealKey( QKeyEvent *) { return false; } diff --git a/src/inputmode/kateviinputmode.h b/src/inputmode/kateviinputmode.h --- a/src/inputmode/kateviinputmode.h +++ b/src/inputmode/kateviinputmode.h @@ -49,7 +49,7 @@ void overwrittenChar(const QChar &) Q_DECL_OVERRIDE; void clearSelection() Q_DECL_OVERRIDE; - bool stealKey(const QKeyEvent *) const Q_DECL_OVERRIDE; + bool stealKey(QKeyEvent *) Q_DECL_OVERRIDE; void gotFocus() Q_DECL_OVERRIDE; void lostFocus() Q_DECL_OVERRIDE; @@ -92,6 +92,8 @@ KateVi::GlobalState *m_viGlobal; KateRenderer::caretStyles m_caret; + bool m_nextKeypressIsOverriddenShortCut; + // configs bool m_relLineNumbers; bool m_activated; diff --git a/src/inputmode/kateviinputmode.cpp b/src/inputmode/kateviinputmode.cpp --- a/src/inputmode/kateviinputmode.cpp +++ b/src/inputmode/kateviinputmode.cpp @@ -65,6 +65,7 @@ , m_viModeEmulatedCommandBar(0) , m_viGlobal(global) , m_caret(KateRenderer::Block) + , m_nextKeypressIsOverriddenShortCut(false) , m_activated(false) { m_relLineNumbers = KateViewConfig::global()->viRelativeLineNumbers(); @@ -131,11 +132,22 @@ // do nothing, handled elsewhere } -bool KateViInputMode::stealKey(const QKeyEvent *k) const +bool KateViInputMode::stealKey(QKeyEvent *k) { - const bool steal = KateViewConfig::global()->viInputModeStealKeys(); - return steal && (m_viModeManager->getCurrentViMode() != KateVi::ViMode::InsertMode || - k->modifiers() == Qt::ControlModifier || k->key() == Qt::Key_Insert); + if (!KateViewConfig::global()->viInputModeStealKeys()) + { + return false; + } + + // Actually see if we can make use of this key - if so, we've stolen it; if not, + // let Qt's shortcut handling system deal with it. + const bool stolen = keyPress(k); + if (stolen) + { + // Qt will replay this QKeyEvent, next time as an ordinary KeyPress. + m_nextKeypressIsOverriddenShortCut = true; + } + return stolen; } KTextEditor::View::InputMode KateViInputMode::viewInputMode() const @@ -255,7 +267,7 @@ KateVi::EmulatedCommandBar *KateViInputMode::viModeEmulatedCommandBar() { if (!m_viModeEmulatedCommandBar) { - m_viModeEmulatedCommandBar = new KateVi::EmulatedCommandBar(m_viModeManager, view()); + m_viModeEmulatedCommandBar = new KateVi::EmulatedCommandBar(this, m_viModeManager, view()); m_viModeEmulatedCommandBar->hide(); } @@ -269,24 +281,15 @@ bool KateViInputMode::keyPress(QKeyEvent *e) { - if (m_viModeManager->getCurrentViMode() == KateVi::InsertMode || - m_viModeManager->getCurrentViMode() == KateVi::ReplaceMode) { + if (m_nextKeypressIsOverriddenShortCut) + { + // This is just the replay of a shortcut that we stole, this time as a QKeyEvent. + // Ignore it, as we'll have already handled it via stealKey()! + m_nextKeypressIsOverriddenShortCut = false; + return true; + } - if (m_viModeManager->handleKeypress(e)) { - return true; - } else if (e->modifiers() != Qt::NoModifier && e->modifiers() != Qt::ShiftModifier) { - // re-post key events not handled if they have a modifier other than shift - QEvent *copy = new QKeyEvent(e->type(), e->key(), e->modifiers(), e->text(), - e->isAutoRepeat(), e->count()); - QCoreApplication::postEvent(viewInternal()->parent(), copy); - } - } else { // !InsertMode - if (!m_viModeManager->handleKeypress(e)) { - // we didn't need that keypress, un-steal it :-) - QEvent *copy = new QKeyEvent(e->type(), e->key(), e->modifiers(), e->text(), - e->isAutoRepeat(), e->count()); - QCoreApplication::postEvent(viewInternal()->parent(), copy); - } + if (m_viModeManager->handleKeypress(e)) { emit view()->viewModeChanged(view(), viewMode()); return true; } diff --git a/src/vimode/emulatedcommandbar/emulatedcommandbar.h b/src/vimode/emulatedcommandbar/emulatedcommandbar.h --- a/src/vimode/emulatedcommandbar/emulatedcommandbar.h +++ b/src/vimode/emulatedcommandbar/emulatedcommandbar.h @@ -56,7 +56,7 @@ public: enum Mode { NoMode, SearchForward, SearchBackward, Command }; - explicit EmulatedCommandBar(InputModeManager *viInputModeManager, QWidget *parent = 0); + explicit EmulatedCommandBar(KateViInputMode* viInputMode, InputModeManager *viInputModeManager, QWidget *parent = 0); virtual ~EmulatedCommandBar(); void init(Mode mode, const QString &initialText = QString()); bool isActive(); @@ -71,6 +71,7 @@ private: + KateViInputMode *m_viInputMode; InputModeManager *m_viInputModeManager; bool m_isActive = false; bool m_wasAborted = true; diff --git a/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp b/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp --- a/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp +++ b/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp @@ -27,6 +27,7 @@ #include "../globalstate.h" #include #include +#include #include #include "matchhighlighter.h" #include "interactivesedreplacemode.h" @@ -69,8 +70,9 @@ } } -EmulatedCommandBar::EmulatedCommandBar(InputModeManager *viInputModeManager, QWidget *parent) +EmulatedCommandBar::EmulatedCommandBar(KateViInputMode* viInputMode, InputModeManager *viInputModeManager, QWidget *parent) : KateViewBarWidget(false, parent), + m_viInputMode(viInputMode), m_viInputModeManager(viInputModeManager), m_view(viInputModeManager->view()) { @@ -230,7 +232,7 @@ if (event->type() == QEvent::KeyPress) { // Re-route this keypress through Vim's central keypress handling area, so that we can use the keypress in e.g. // mappings and macros. - return m_viInputModeManager->handleKeypress(static_cast(event)); + return m_viInputMode->keyPress(static_cast(event)); } return false; } diff --git a/src/vimode/inputmodemanager.cpp b/src/vimode/inputmodemanager.cpp --- a/src/vimode/inputmodemanager.cpp +++ b/src/vimode/inputmodemanager.cpp @@ -118,7 +118,7 @@ // of a mapping, we don't want to record them when they are played back by m_keyMapper, hence // the "!isPlayingBackRejectedKeys()". And obviously, since we're recording keys before they are mapped, we don't // want to also record the executed mapping, as when we replayed the macro, we'd get duplication! - if (m_macroRecorder->isRecording() && !m_macroRecorder->isReplaying() && !isSyntheticSearchCompletedKeyPress && !keyMapper()->isExecutingMapping() && !keyMapper()->isPlayingBackRejectedKeys()) { + if (m_macroRecorder->isRecording() && !m_macroRecorder->isReplaying() && !isSyntheticSearchCompletedKeyPress && !keyMapper()->isExecutingMapping() && !keyMapper()->isPlayingBackRejectedKeys() && !lastChangeRecorder()->isReplaying()) { m_macroRecorder->record(*e); } diff --git a/src/vimode/keymapper.cpp b/src/vimode/keymapper.cpp --- a/src/vimode/keymapper.cpp +++ b/src/vimode/keymapper.cpp @@ -26,6 +26,7 @@ #include #include #include +#include "macrorecorder.h" #include @@ -119,8 +120,33 @@ // We've been swallowing all the keypresses meant for m_keys for our mapping keys; now that we know // this cannot be a mapping, restore them. Q_ASSERT(!isPartialMapping && !isFullMapping); - playBackRejectedKeys(); - return true; + const bool isUserKeypress = !m_viInputModeManager->macroRecorder()->isReplaying() && !isExecutingMapping(); + if (isUserKeypress && m_mappingKeys.size() == 1) + { + // Ugh - unpleasant complication starting with Qt 5.5-ish - it is no + // longer possible to replay QKeyEvents in such a way that shortcuts + // are triggered, so if we want to correctly handle a shortcut (e.g. + // ctrl+s for e.g. Save), we can no longer pop it into m_mappingKeys + // then immediately playBackRejectedKeys() (as this will not trigger + // the e.g. Save shortcut) - the best we can do is, if e.g. ctrl+s is + // not part of any mapping, immediately return false, *not* calling + // playBackRejectedKeys() and clearing m_mappingKeys ourselves. + // If e.g. ctrl+s *is* part of a mapping, then if the mapping is + // rejected, the played back e.g. ctrl+s does not trigger the e.g. + // Save shortcut. Likewise, we can no longer have e.g. ctrl+s inside + // mappings or macros - the e.g. Save shortcut will not be triggered! + // Altogether, a pretty disastrous change from Qt's old behaviour - + // either they "fix" it (although it could be argued that being able + // to trigger shortcuts from QKeyEvents was never the desired behaviour) + // or we try to emulate Shortcut-handling ourselves :( + m_mappingKeys.clear(); + return false; + } + else + { + playBackRejectedKeys(); + return true; + } } m_doNotMapNextKeypress = false; return false; diff --git a/src/vimode/lastchangerecorder.h b/src/vimode/lastchangerecorder.h --- a/src/vimode/lastchangerecorder.h +++ b/src/vimode/lastchangerecorder.h @@ -31,6 +31,13 @@ { class InputModeManager; +/** + * In e.g. Insert mode, Qt seems to feed each keypress through twice; once as a ShortcutOverride (even if the key + * doesn't actually appear to be a ShortcutOverride) and then, whether the "ShortcutOverride" was accepted or not, + * again as a KeyPress. We don't want to store both, so this helper helps to decide what to do. + */ +bool isRepeatOfShortcutOverrideAsKeyPress(const QKeyEvent& currentKeyPress, const QKeyEvent& lastKeyPress); + class LastChangeRecorder { public: diff --git a/src/vimode/lastchangerecorder.cpp b/src/vimode/lastchangerecorder.cpp --- a/src/vimode/lastchangerecorder.cpp +++ b/src/vimode/lastchangerecorder.cpp @@ -25,6 +25,17 @@ using namespace KateVi; +bool KateVi::isRepeatOfShortcutOverrideAsKeyPress(const QKeyEvent& currentKeyPress, const QKeyEvent& lastKeyPress) +{ + if (lastKeyPress.type() == QEvent::ShortcutOverride && currentKeyPress.type() == QEvent::KeyPress && + lastKeyPress.key() == currentKeyPress.key() && + lastKeyPress.modifiers() == currentKeyPress.modifiers()) + { + return true; + } + return false; +} + LastChangeRecorder::LastChangeRecorder(InputModeManager *viInputModeManager) : m_viInputModeManager(viInputModeManager) , m_isReplaying(false) @@ -37,6 +48,10 @@ void LastChangeRecorder::record(const QKeyEvent &e) { + if (!m_changeLog.isEmpty()) { + if (isRepeatOfShortcutOverrideAsKeyPress(e, m_changeLog.last())) + return; + } if (e.key() != Qt::Key_Shift && e.key() != Qt::Key_Control && e.key() != Qt::Key_Meta && e.key() != Qt::Key_Alt) { m_changeLog.append(e); } diff --git a/src/vimode/macrorecorder.cpp b/src/vimode/macrorecorder.cpp --- a/src/vimode/macrorecorder.cpp +++ b/src/vimode/macrorecorder.cpp @@ -26,6 +26,7 @@ #include "globalstate.h" #include "macros.h" #include "completionreplayer.h" +#include "lastchangerecorder.h" namespace { const QChar LastPlayedRegister = QLatin1Char('@'); @@ -70,6 +71,10 @@ void MacroRecorder::record(const QKeyEvent &event) { + if (!m_eventsLog.isEmpty()) { + if (isRepeatOfShortcutOverrideAsKeyPress(event, m_eventsLog.last())) + return; + } m_eventsLog.append(event); } diff --git a/src/vimode/modes/normalvimode.cpp b/src/vimode/modes/normalvimode.cpp --- a/src/vimode/modes/normalvimode.cpp +++ b/src/vimode/modes/normalvimode.cpp @@ -426,11 +426,16 @@ } } else if (m_matchingCommands.size() == 0 && m_matchingMotions.size() == 0) { resetParser(); - return false; + // A bit ugly: we haven't made use of the key event, + // but don't want "typeable" keypresses (e.g. a, b, 3, etc) to be marked + // as unused as they will then be added to the document, but we don't + // want to swallow all keys in case this was a shortcut. + // So say we made use of it if and only if it was *not* a shortcut. + return e->type() != QEvent::ShortcutOverride; } m_matchingMotions.clear(); - return false; + return true; // TODO - need to check this - it's currently required for making tests pass, but seems odd. } /**