vim-mode: share registers among all tabs

This commit is contained in:
Le Tan 2017-10-18 19:55:25 +08:00
parent ad082d3ba0
commit dcb6227d3b
2 changed files with 40 additions and 21 deletions

View File

@ -18,6 +18,7 @@ extern VConfigManager *g_config;
const QChar VVim::c_unnamedRegister = QChar('"'); const QChar VVim::c_unnamedRegister = QChar('"');
const QChar VVim::c_blackHoleRegister = QChar('_'); const QChar VVim::c_blackHoleRegister = QChar('_');
const QChar VVim::c_selectionRegister = QChar('+'); const QChar VVim::c_selectionRegister = QChar('+');
QMap<QChar, VVim::Register> VVim::s_registers;
const int VVim::SearchHistory::c_capacity = 50; const int VVim::SearchHistory::c_capacity = 50;
@ -556,7 +557,7 @@ bool VVim::handleKeyPressEvent(int key, int modifiers, int *p_autoIndentPos)
QChar reg = keyToRegisterName(keyInfo); QChar reg = keyToRegisterName(keyInfo);
if (!reg.isNull()) { if (!reg.isNull()) {
// Insert register content. // Insert register content.
m_editor->insertPlainText(m_registers[reg].read()); m_editor->insertPlainText(getRegister(reg).read());
} }
goto clear_accept; goto clear_accept;
@ -602,11 +603,12 @@ bool VVim::handleKeyPressEvent(int key, int modifiers, int *p_autoIndentPos)
QChar reg = keyToRegisterName(keyInfo); QChar reg = keyToRegisterName(keyInfo);
if (!reg.isNull()) { if (!reg.isNull()) {
m_keys.clear(); m_keys.clear();
setRegister(reg); setCurrentRegisterName(reg);
if (m_registers[reg].isNamedRegister()) { Register &r = getRegister(reg);
m_registers[reg].m_append = (modifiers == Qt::ShiftModifier); if (r.isNamedRegister()) {
r.m_append = (modifiers == Qt::ShiftModifier);
} else { } else {
Q_ASSERT(!m_registers[reg].m_append); Q_ASSERT(!r.m_append);
} }
goto accept; goto accept;
@ -2223,7 +2225,7 @@ void VVim::resetState()
m_keys.clear(); m_keys.clear();
m_tokens.clear(); m_tokens.clear();
m_pendingKeys.clear(); m_pendingKeys.clear();
setRegister(c_unnamedRegister); setCurrentRegisterName(c_unnamedRegister);
m_resetPositionInBlock = true; m_resetPositionInBlock = true;
m_registerPending = false; m_registerPending = false;
} }
@ -3881,7 +3883,7 @@ void VVim::processPasteAction(QList<Token> &p_tokens, bool p_pasteBefore)
repeat = to.m_repeat; repeat = to.m_repeat;
} }
Register &reg = m_registers[m_regName]; Register &reg = getRegister(m_regName);
QString value = reg.read(); QString value = reg.read();
bool isBlock = reg.isBlock(); bool isBlock = reg.isBlock();
if (value.isEmpty()) { if (value.isEmpty()) {
@ -4855,14 +4857,17 @@ void VVim::expandSelectionToWholeLines(QTextCursor &p_cursor)
void VVim::initRegisters() void VVim::initRegisters()
{ {
m_registers.clear(); if (!s_registers.isEmpty()) {
for (char ch = 'a'; ch <= 'z'; ++ch) { return;
m_registers[QChar(ch)] = Register(QChar(ch));
} }
m_registers[c_unnamedRegister] = Register(c_unnamedRegister); for (char ch = 'a'; ch <= 'z'; ++ch) {
m_registers[c_blackHoleRegister] = Register(c_blackHoleRegister); s_registers[QChar(ch)] = Register(QChar(ch));
m_registers[c_selectionRegister] = Register(c_selectionRegister); }
s_registers[c_unnamedRegister] = Register(c_unnamedRegister);
s_registers[c_blackHoleRegister] = Register(c_blackHoleRegister);
s_registers[c_selectionRegister] = Register(c_selectionRegister);
} }
bool VVim::expectingRegisterName() const bool VVim::expectingRegisterName() const
@ -5145,12 +5150,12 @@ void VVim::saveToRegister(const QString &p_text)
qDebug() << QString("save text(%1) to register(%2)").arg(text).arg(m_regName); qDebug() << QString("save text(%1) to register(%2)").arg(text).arg(m_regName);
Register &reg = m_registers[m_regName]; Register &reg = getRegister(m_regName);
reg.update(text); reg.update(text);
if (!reg.isBlackHoleRegister() && !reg.isUnnamedRegister()) { if (!reg.isBlackHoleRegister() && !reg.isUnnamedRegister()) {
// Save it to unnamed register. // Save it to unnamed register.
m_registers[c_unnamedRegister].update(reg.m_value); setRegister(c_unnamedRegister, reg.m_value);
} }
} }
@ -5257,7 +5262,7 @@ void VVim::message(const QString &p_msg)
const QMap<QChar, VVim::Register> &VVim::getRegisters() const const QMap<QChar, VVim::Register> &VVim::getRegisters() const
{ {
return m_registers; return s_registers;
} }
const VVim::Marks &VVim::getMarks() const const VVim::Marks &VVim::getMarks() const
@ -5280,7 +5285,7 @@ QString VVim::getPendingKeys() const
return str; return str;
} }
void VVim::setRegister(QChar p_reg) void VVim::setCurrentRegisterName(QChar p_reg)
{ {
m_regName = p_reg; m_regName = p_reg;
} }
@ -5793,7 +5798,7 @@ QString VVim::readRegister(int p_key, int p_modifiers)
Key keyInfo(p_key, p_modifiers); Key keyInfo(p_key, p_modifiers);
QChar reg = keyToRegisterName(keyInfo); QChar reg = keyToRegisterName(keyInfo);
if (!reg.isNull()) { if (!reg.isNull()) {
return m_registers[reg].read(); return getRegister(reg).read();
} }
return ""; return "";

View File

@ -166,7 +166,7 @@ public:
void setMode(VimMode p_mode, bool p_clearSelection = true); void setMode(VimMode p_mode, bool p_clearSelection = true);
// Set current register. // Set current register.
void setRegister(QChar p_reg); void setCurrentRegisterName(QChar p_reg);
// Get m_registers. // Get m_registers.
const QMap<QChar, Register> &getRegisters() const; const QMap<QChar, Register> &getRegisters() const;
@ -797,6 +797,10 @@ private:
// Clear search highlight. // Clear search highlight.
void clearSearchHighlight(); void clearSearchHighlight();
// Function utils for register.
Register &getRegister(QChar p_regName) const;
void setRegister(QChar p_regName, const QString &p_val);
VEdit *m_editor; VEdit *m_editor;
const VEditConfig *m_editConfig; const VEditConfig *m_editConfig;
VimMode m_mode; VimMode m_mode;
@ -814,8 +818,6 @@ private:
// Whether reset the position in block when moving cursor. // Whether reset the position in block when moving cursor.
bool m_resetPositionInBlock; bool m_resetPositionInBlock;
QMap<QChar, Register> m_registers;
// Currently used register. // Currently used register.
QChar m_regName; QChar m_regName;
@ -843,6 +845,18 @@ private:
static const QChar c_unnamedRegister; static const QChar c_unnamedRegister;
static const QChar c_blackHoleRegister; static const QChar c_blackHoleRegister;
static const QChar c_selectionRegister; static const QChar c_selectionRegister;
static QMap<QChar, VVim::Register> s_registers;
}; };
inline VVim::Register &VVim::getRegister(QChar p_regName) const
{
return s_registers[p_regName];
}
inline void VVim::setRegister(QChar p_regName, const QString &p_val)
{
s_registers[p_regName].update(p_val);
}
#endif // VVIM_H #endif // VVIM_H