From 2734b8407c1f74461d6ceacae66c52a40201ba67 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 21 Jun 2017 22:00:11 +0800 Subject: [PATCH] vim-mode: support canceling autoindent after o/O --- src/utils/vvim.cpp | 20 +++++++++++++++----- src/utils/vvim.h | 4 ++-- src/vmdeditoperations.cpp | 11 +++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/utils/vvim.cpp b/src/utils/vvim.cpp index 7c95c910..bfbf608d 100644 --- a/src/utils/vvim.cpp +++ b/src/utils/vvim.cpp @@ -340,9 +340,9 @@ static int percentageToBlockNumber(const QTextDocument *p_doc, int p_percent) return num >= 0 ? num : 0; } -bool VVim::handleKeyPressEvent(QKeyEvent *p_event) +bool VVim::handleKeyPressEvent(QKeyEvent *p_event, bool *p_autoIndented) { - bool ret = handleKeyPressEvent(p_event->key(), p_event->modifiers()); + bool ret = handleKeyPressEvent(p_event->key(), p_event->modifiers(), p_autoIndented); if (ret) { p_event->accept(); } @@ -350,13 +350,17 @@ bool VVim::handleKeyPressEvent(QKeyEvent *p_event) return ret; } -bool VVim::handleKeyPressEvent(int key, int modifiers) +bool VVim::handleKeyPressEvent(int key, int modifiers, bool *p_autoIndented) { bool ret = false; bool resetPositionInBlock = true; Key keyInfo(key, modifiers); bool unindent = false; + if (p_autoIndented) { + *p_autoIndented = false; + } + // Handle Insert mode key press. if (VimMode::Insert == m_mode) { if (key == Qt::Key_Escape @@ -739,16 +743,22 @@ bool VVim::handleKeyPressEvent(int key, int modifiers) 1); } + bool textInserted = false; if (vconfig.getAutoIndent()) { - VEditUtils::indentBlockAsPreviousBlock(cursor); + textInserted = VEditUtils::indentBlockAsPreviousBlock(cursor); if (vconfig.getAutoList()) { - VEditUtils::insertListMarkAsPreviousBlock(cursor); + textInserted = VEditUtils::insertListMarkAsPreviousBlock(cursor) + || textInserted; } } cursor.endEditBlock(); m_editor->setTextCursor(cursor); + if (p_autoIndented && textInserted) { + *p_autoIndented = true; + } + setMode(VimMode::Insert); } diff --git a/src/utils/vvim.h b/src/utils/vvim.h index 6ddba224..79889aea 100644 --- a/src/utils/vvim.h +++ b/src/utils/vvim.h @@ -148,7 +148,7 @@ public: // Handle key press event. // Returns true if the event is consumed and need no more handling. - bool handleKeyPressEvent(QKeyEvent *p_event); + bool handleKeyPressEvent(QKeyEvent *p_event, bool *p_autoIndented = NULL); // Return current mode. VimMode getMode() const; @@ -449,7 +449,7 @@ private: }; // Returns true if the event is consumed and need no more handling. - bool handleKeyPressEvent(int key, int modifiers); + bool handleKeyPressEvent(int key, int modifiers, bool *p_autoIndented = NULL); // Reset all key state info. void resetState(); diff --git a/src/vmdeditoperations.cpp b/src/vmdeditoperations.cpp index 9a7b6a67..e9dbbd10 100644 --- a/src/vmdeditoperations.cpp +++ b/src/vmdeditoperations.cpp @@ -193,8 +193,15 @@ bool VMdEditOperations::insertImage() bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event) { - if (m_editConfig->m_enableVimMode && m_vim->handleKeyPressEvent(p_event)) { - m_autoIndentPos = -1; + bool autoIndentedVim = false; + if (m_editConfig->m_enableVimMode + && m_vim->handleKeyPressEvent(p_event, &autoIndentedVim)) { + if (autoIndentedVim) { + m_autoIndentPos = m_editor->textCursor().position(); + } else { + m_autoIndentPos = -1; + } + return true; }