enalbe Vim cmd line for searching in read mode

This commit is contained in:
Le Tan 2018-01-19 19:29:40 +08:00
parent b927a525e2
commit 333be910cc
3 changed files with 75 additions and 22 deletions

View File

@ -155,6 +155,26 @@ public:
Invalid Invalid
}; };
// Search item including the searched text and options.
struct SearchItem
{
SearchItem() : m_options(0), m_forward(true) {}
bool isEmpty() const
{
return m_text.isEmpty();
}
// The user raw input.
QString m_rawStr;
// The string used to search.
QString m_text;
uint m_options;
bool m_forward;
};
// Handle key press event. // Handle key press event.
// @p_autoIndentPos: the cursor position of last auto indent. // @p_autoIndentPos: the cursor position of last auto indent.
// Returns true if the event is consumed and need no more handling. // Returns true if the event is consumed and need no more handling.
@ -205,6 +225,12 @@ public:
// Returns empty string if it is not a valid register. // Returns empty string if it is not a valid register.
QString readRegister(int p_key, int p_modifiers); QString readRegister(int p_key, int p_modifiers);
// Fetch the searched string and options from @p_type and @p_cmd.
// \C for case-sensitive;
// Case-insensitive by default.
// Regular-expression by default.
static VVim::SearchItem fetchSearchItem(VVim::CommandLineType p_type, const QString &p_cmd);
signals: signals:
// Emit when current mode has been changed. // Emit when current mode has been changed.
void modeChanged(VimMode p_mode); void modeChanged(VimMode p_mode);
@ -284,21 +310,6 @@ private:
} }
}; };
// Search item including the searched text and options.
struct SearchItem
{
SearchItem() : m_options(0), m_forward(true) {}
// The user raw input.
QString m_rawStr;
// The string used to search.
QString m_text;
uint m_options;
bool m_forward;
};
class SearchHistory class SearchHistory
{ {
public: public:
@ -800,12 +811,6 @@ private:
// [[, ]], [], ][, [{, ]}. // [[, ]], [], ][, [{, ]}.
void processTitleJump(const QList<Token> &p_tokens, bool p_forward, int p_relativeLevel); void processTitleJump(const QList<Token> &p_tokens, bool p_forward, int p_relativeLevel);
// Fetch the searched string and options from @p_type and @p_cmd.
// \C for case-sensitive;
// Case-insensitive by default.
// Regular-expression by default.
VVim::SearchItem fetchSearchItem(VVim::CommandLineType p_type, const QString &p_cmd);
// Clear search highlight. // Clear search highlight.
void clearSearchHighlight(); void clearSearchHighlight();

View File

@ -645,7 +645,7 @@ void VMdTab::clearSearchedWordHighlight()
} }
} }
void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool /* p_shift */) void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift)
{ {
V_ASSERT(m_webViewer); V_ASSERT(m_webViewer);
@ -661,6 +661,7 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool /* p_shift */)
// Zoom out. // Zoom out.
zoomWebPage(false); zoomWebPage(false);
} }
break; break;
// Equal // Equal
@ -669,6 +670,7 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool /* p_shift */)
// Zoom in. // Zoom in.
zoomWebPage(true); zoomWebPage(true);
} }
break; break;
// 0 // 0
@ -677,6 +679,35 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool /* p_shift */)
// Recover zoom. // Recover zoom.
m_webViewer->setZoomFactor(1); m_webViewer->setZoomFactor(1);
} }
break;
// / or ?
case 191:
if (!p_ctrl) {
VVim::CommandLineType type = VVim::CommandLineType::SearchForward;
if (p_shift) {
// ?, search backward.
type = VVim::CommandLineType::SearchBackward;
}
emit triggerVimCmd(type);
}
break;
// n or N
case 78:
if (!p_ctrl) {
if (!m_lastSearchItem.isEmpty()) {
bool forward = !p_shift;
findTextInWebView(m_lastSearchItem.m_text,
m_lastSearchItem.m_options,
false,
forward ? m_lastSearchItem.m_forward : !m_lastSearchItem.m_forward);
}
}
break; break;
default: default:
@ -1047,6 +1078,8 @@ void VMdTab::handleVimCmdCommandCancelled()
if (vim) { if (vim) {
vim->processCommandLineCancelled(); vim->processCommandLineCancelled();
} }
} else {
m_webViewer->findText("");
} }
} }
@ -1057,6 +1090,14 @@ void VMdTab::handleVimCmdCommandFinished(VVim::CommandLineType p_type, const QSt
if (vim) { if (vim) {
vim->processCommandLine(p_type, p_cmd); vim->processCommandLine(p_type, p_cmd);
} }
} else {
Q_ASSERT(p_type == VVim::CommandLineType::SearchForward
|| p_type == VVim::CommandLineType::SearchBackward);
m_lastSearchItem = VVim::fetchSearchItem(p_type, p_cmd);
findTextInWebView(m_lastSearchItem.m_text,
m_lastSearchItem.m_options,
false,
m_lastSearchItem.m_forward);
} }
} }
@ -1069,6 +1110,11 @@ void VMdTab::handleVimCmdCommandChanged(VVim::CommandLineType p_type, const QStr
if (vim) { if (vim) {
vim->processCommandLineChanged(p_type, p_cmd); vim->processCommandLineChanged(p_type, p_cmd);
} }
} else {
Q_ASSERT(p_type == VVim::CommandLineType::SearchForward
|| p_type == VVim::CommandLineType::SearchBackward);
VVim::SearchItem item = VVim::fetchSearchItem(p_type, p_cmd);
findTextInWebView(item.m_text, item.m_options, true, item.m_forward);
} }
} }

View File

@ -215,6 +215,8 @@ private:
// Used to scroll to the header of edit mode in read mode. // Used to scroll to the header of edit mode in read mode.
VHeaderPointer m_headerFromEditMode; VHeaderPointer m_headerFromEditMode;
VVim::SearchItem m_lastSearchItem;
}; };
inline VMdEditor *VMdTab::getEditor() inline VMdEditor *VMdTab::getEditor()