From 333e87dedd453673485faf8d0cbedb02502453fe Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 7 Feb 2012 23:01:02 +0000 Subject: [PATCH] add a Register class, registers can now contain a list of string That way, registers can be used to store per selection data --- src/main.cc | 18 +++++++++--------- src/register.cc | 35 +++++++++++++++++++++++++++++++++++ src/register.hh | 29 +++++++++++++++++++++++++++++ src/register_manager.hh | 5 +++-- 4 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 src/register.cc create mode 100644 src/register.hh diff --git a/src/main.cc b/src/main.cc index a1d51e0c..c9066243 100644 --- a/src/main.cc +++ b/src/main.cc @@ -413,7 +413,7 @@ bool insert_char(IncrementalInserter& inserter, const Key& key) inserter.insert(inserter.buffer().name()); break; default: - inserter.insert(RegisterManager::instance()[next_key.key]); + inserter.insert(RegisterManager::instance()[next_key.key].get()); } } break; @@ -834,9 +834,9 @@ void do_search(Editor& editor) { std::string ex = prompt("/"); if (ex.empty()) - ex = RegisterManager::instance()['/']; + ex = RegisterManager::instance()['/'].get(); else - RegisterManager::instance()['/'] = ex; + RegisterManager::instance()['/'].set(ex); editor.select(std::bind(select_next_match, _1, ex)); } @@ -845,7 +845,7 @@ void do_search(Editor& editor) void do_search_next(Editor& editor) { - std::string& ex = RegisterManager::instance()['/']; + const std::string& ex = RegisterManager::instance()['/'].get(); if (not ex.empty()) editor.select(std::bind(select_next_match, _1, ex)); else @@ -854,18 +854,18 @@ void do_search_next(Editor& editor) void do_yank(Editor& editor, int count) { - RegisterManager::instance()['"'] = editor.selection_content(); + RegisterManager::instance()['"'].set(editor.selection_content()); } void do_erase(Editor& editor, int count) { - RegisterManager::instance()['"'] = editor.selection_content(); + RegisterManager::instance()['"'].set(editor.selection_content()); editor.erase(); } void do_change(Editor& editor, int count) { - RegisterManager::instance()['"'] = editor.selection_content(); + RegisterManager::instance()['"'].set(editor.selection_content()); do_insert(editor, IncrementalInserter::Mode::Change); } @@ -873,9 +873,9 @@ template void do_paste(Editor& editor, int count) { if (append) - editor.append(RegisterManager::instance()['"']); + editor.append(RegisterManager::instance()['"'].get()); else - editor.insert(RegisterManager::instance()['"']); + editor.insert(RegisterManager::instance()['"'].get()); } void do_select_regex(Editor& editor, int count) diff --git a/src/register.cc b/src/register.cc new file mode 100644 index 00000000..e5a1bbb2 --- /dev/null +++ b/src/register.cc @@ -0,0 +1,35 @@ +#include "register.hh" + +namespace Kakoune +{ + +const std::string Register::ms_empty; + +void Register::set(const std::string& value) +{ + m_content.clear(); + m_content.push_back(value); +} + +void Register::set(const memoryview& values) +{ + m_content = std::vector(values.begin(), values.end()); +} + +const std::string& Register::get() const +{ + if (m_content.size() != 0) + return m_content.front(); + else + return ms_empty; +} + +const std::string& Register::get(size_t index) const +{ + if (m_content.size() > index) + return m_content[index]; + else + return ms_empty; +} + +} diff --git a/src/register.hh b/src/register.hh new file mode 100644 index 00000000..2b8872d5 --- /dev/null +++ b/src/register.hh @@ -0,0 +1,29 @@ +#ifndef register_hh_INCLUDED +#define register_hh_INCLUDED + +#include +#include + +#include "memoryview.hh" + +namespace Kakoune +{ + +class Register +{ +public: + void set(const std::string& value); + void set(const memoryview& values); + const std::string& get() const; + const std::string& get(size_t index) const; + +private: + std::vector m_content; + + static const std::string ms_empty; +}; + +} + +#endif // register_hh_INCLUDED + diff --git a/src/register_manager.hh b/src/register_manager.hh index 46bcfc06..c6e65a79 100644 --- a/src/register_manager.hh +++ b/src/register_manager.hh @@ -1,6 +1,7 @@ #ifndef register_manager_hh_INCLUDED #define register_manager_hh_INCLUDED +#include "register.hh" #include "utils.hh" namespace Kakoune @@ -9,10 +10,10 @@ namespace Kakoune class RegisterManager : public Singleton { public: - std::string& operator[](char reg) { return m_registers[reg]; } + Register& operator[](char reg) { return m_registers[reg]; } protected: - std::unordered_map m_registers; + std::unordered_map m_registers; }; }