2013-10-25 01:01:17 +02:00
|
|
|
#include "keymap_manager.hh"
|
|
|
|
|
2015-01-06 14:40:56 +01:00
|
|
|
#include "array_view.hh"
|
2014-12-16 19:57:19 +01:00
|
|
|
#include "assert.hh"
|
2018-01-18 09:57:14 +01:00
|
|
|
#include "exception.hh"
|
|
|
|
#include "string_utils.hh"
|
2013-10-25 01:01:17 +02:00
|
|
|
|
2017-03-03 17:51:29 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-10-25 01:01:17 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-03-03 17:51:29 +01:00
|
|
|
void KeymapManager::map_key(Key key, KeymapMode mode,
|
|
|
|
KeyList mapping, String docstring)
|
2013-10-25 01:01:17 +02:00
|
|
|
{
|
2017-03-07 01:30:54 +01:00
|
|
|
m_mapping[KeyAndMode{key, mode}] = {std::move(mapping), std::move(docstring)};
|
2013-10-25 01:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeymapManager::unmap_key(Key key, KeymapMode mode)
|
|
|
|
{
|
2017-07-11 02:49:35 +02:00
|
|
|
m_mapping.remove(KeyAndMode{key, mode});
|
2013-10-25 01:01:17 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 19:40:38 +02:00
|
|
|
void KeymapManager::unmap_keys(KeymapMode mode)
|
|
|
|
{
|
|
|
|
auto it = m_mapping.begin();
|
|
|
|
while (it != m_mapping.end())
|
|
|
|
{
|
|
|
|
auto& map = *it;
|
|
|
|
if (map.key.second == mode)
|
|
|
|
unmap_key(map.key.first, map.key.second);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 01:01:17 +02:00
|
|
|
|
|
|
|
bool KeymapManager::is_mapped(Key key, KeymapMode mode) const
|
|
|
|
{
|
2017-03-07 01:30:54 +01:00
|
|
|
return m_mapping.find(KeyAndMode{key, mode}) != m_mapping.end() or
|
2013-10-25 01:01:17 +02:00
|
|
|
(m_parent and m_parent->is_mapped(key, mode));
|
|
|
|
}
|
|
|
|
|
2017-11-24 10:25:36 +01:00
|
|
|
const KeymapManager::KeymapInfo&
|
2017-03-03 17:51:29 +01:00
|
|
|
KeymapManager::get_mapping(Key key, KeymapMode mode) const
|
2013-10-25 01:01:17 +02:00
|
|
|
{
|
2017-03-07 01:30:54 +01:00
|
|
|
auto it = m_mapping.find(KeyAndMode{key, mode});
|
2013-10-25 01:01:17 +02:00
|
|
|
if (it != m_mapping.end())
|
2017-03-07 01:30:54 +01:00
|
|
|
return it->value;
|
2013-10-25 01:01:17 +02:00
|
|
|
kak_assert(m_parent);
|
|
|
|
return m_parent->get_mapping(key, mode);
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:51:29 +01:00
|
|
|
KeymapManager::KeyList KeymapManager::get_mapped_keys(KeymapMode mode) const
|
|
|
|
{
|
|
|
|
KeyList res;
|
|
|
|
if (m_parent)
|
|
|
|
res = m_parent->get_mapped_keys(mode);
|
|
|
|
for (auto& map : m_mapping)
|
|
|
|
{
|
2017-07-11 02:49:35 +02:00
|
|
|
if (map.key.second == mode and not contains(res, map.key.first))
|
2017-03-07 01:30:54 +01:00
|
|
|
res.emplace_back(map.key.first);
|
2017-03-03 17:51:29 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-18 10:24:34 +01:00
|
|
|
void KeymapManager::add_user_mode(String user_mode_name)
|
2018-01-18 09:57:14 +01:00
|
|
|
{
|
|
|
|
auto modes = {"normal", "insert", "prompt", "menu", "goto", "view", "user", "object"};
|
2018-03-02 07:45:04 +01:00
|
|
|
|
2018-01-18 09:57:14 +01:00
|
|
|
if (contains(modes, user_mode_name))
|
|
|
|
throw runtime_error(format("'{}' is already a regular mode", user_mode_name));
|
|
|
|
|
2018-03-02 07:45:04 +01:00
|
|
|
if (contains(user_modes(), user_mode_name))
|
2018-01-18 09:57:14 +01:00
|
|
|
throw runtime_error(format("user mode '{}' already defined", user_mode_name));
|
|
|
|
|
2018-03-25 07:35:33 +02:00
|
|
|
if (not all_of(user_mode_name, is_identifier))
|
2018-01-18 09:57:14 +01:00
|
|
|
throw runtime_error(format("invalid mode name: '{}'", user_mode_name));
|
|
|
|
|
2018-03-02 07:45:04 +01:00
|
|
|
user_modes().push_back(std::move(user_mode_name));
|
2018-01-18 09:57:14 +01:00
|
|
|
}
|
|
|
|
|
2013-10-25 01:01:17 +02:00
|
|
|
}
|