kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

register_manager.cc (3169B)


      1 #include "register_manager.hh"
      2 
      3 #include "assert.hh"
      4 #include "context.hh"
      5 #include "hash_map.hh"
      6 #include "format.hh"
      7 #include "ranges.hh"
      8 #include "hook_manager.hh"
      9 
     10 namespace Kakoune
     11 {
     12 
     13 Register::RestoreInfo Register::save(const Context& context)
     14 {
     15     return get(context) | gather<RestoreInfo>();
     16 }
     17 
     18 void Register::restore(Context& context, const RestoreInfo& info)
     19 {
     20     set(context, info, true);
     21 }
     22 
     23 void StaticRegister::set(Context& context, ConstArrayView<String> values, bool)
     24 {
     25     m_content.assign(values.begin(), values.end());
     26     if (not m_disable_modified_hook)
     27         context.hooks().run_hook(Hook::RegisterModified, m_name, context);
     28 }
     29 
     30 ConstArrayView<String> StaticRegister::get(const Context&)
     31 {
     32     if (m_content.empty())
     33         return ConstArrayView<String>(String::ms_empty);
     34     else
     35         return ConstArrayView<String>(m_content);
     36 }
     37 
     38 const String& StaticRegister::get_main(const Context& context, size_t main_index)
     39 {
     40     auto content = get(context);
     41     return content[std::min(main_index, content.size() - 1)];
     42 }
     43 
     44 void HistoryRegister::set(Context& context, ConstArrayView<String> values, bool restoring)
     45 {
     46     constexpr size_t size_limit = 1000;
     47 
     48     if (restoring)
     49     {
     50         StaticRegister::set(context, values, true);
     51         m_content.erase(remove_if(m_content, [](auto&& s) { return s.empty(); }), m_content.end());
     52         return;
     53     }
     54 
     55     for (auto&& entry : values | reverse())
     56     {
     57         m_content.erase(std::remove(m_content.begin(), m_content.end(), entry), m_content.end());
     58         m_content.insert(m_content.begin(), entry);
     59     }
     60 
     61     if (m_content.size() > size_limit)
     62         m_content.erase(m_content.end() - (m_content.size() - size_limit), m_content.end());
     63 
     64     if (not m_disable_modified_hook)
     65         context.hooks().run_hook(Hook::RegisterModified, m_name, context);
     66 }
     67 
     68 const String& HistoryRegister::get_main(const Context&, size_t)
     69 {
     70     return m_content.empty() ? String::ms_empty : m_content.front();
     71 }
     72 
     73 static const HashMap<StringView, Codepoint> reg_names {
     74     { "slash", '/' },
     75     { "dquote", '"' },
     76     { "pipe", '|' },
     77     { "caret", '^' },
     78     { "arobase", '@' },
     79     { "percent", '%' },
     80     { "dot", '.' },
     81     { "hash", '#' },
     82     { "underscore", '_' },
     83     { "colon", ':' }
     84 };
     85 
     86 Register& RegisterManager::operator[](StringView reg) const
     87 {
     88     if (reg.length() == 1)
     89         return (*this)[reg[0_byte]];
     90 
     91     auto it = reg_names.find(reg);
     92     if (it == reg_names.end())
     93         throw runtime_error(format("no such register: '{}'", reg));
     94     return (*this)[it->value];
     95 }
     96 
     97 Register& RegisterManager::operator[](Codepoint c) const
     98 {
     99     c = to_lower(c);
    100     auto it = m_registers.find(c);
    101     if (it == m_registers.end())
    102         throw runtime_error(format("no such register: '{}'", c));
    103 
    104     return *(it->value);
    105 }
    106 
    107 void RegisterManager::add_register(Codepoint c, UniquePtr<Register> reg)
    108 {
    109     auto& reg_ptr = m_registers[c];
    110     kak_assert(not reg_ptr);
    111     reg_ptr = std::move(reg);
    112 }
    113 
    114 CandidateList RegisterManager::complete_register_name(StringView prefix, ByteCount cursor_pos) const
    115 {
    116     return complete(prefix, cursor_pos, reg_names | transform([](auto& i) { return i.key; }));
    117 }
    118 
    119 }