2012-04-03 15:39:20 +02:00
|
|
|
#include "option_manager.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2012-06-14 15:16:44 +02:00
|
|
|
#include "assert.hh"
|
2012-04-03 15:39:20 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-04-12 21:03:26 +02:00
|
|
|
OptionDesc::OptionDesc(String name, String docstring, OptionFlags flags)
|
|
|
|
: m_name(std::move(name)), m_docstring(std::move(docstring)),
|
|
|
|
m_flags(flags) {}
|
|
|
|
|
|
|
|
Option::Option(const OptionDesc& desc, OptionManager& manager)
|
|
|
|
: m_manager(manager), m_desc(desc) {}
|
2013-03-03 17:25:40 +01:00
|
|
|
|
2012-06-14 15:16:44 +02:00
|
|
|
OptionManager::OptionManager(OptionManager& parent)
|
|
|
|
: m_parent(&parent)
|
2012-04-03 20:25:27 +02:00
|
|
|
{
|
2012-06-14 15:16:44 +02:00
|
|
|
parent.register_watcher(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionManager::~OptionManager()
|
|
|
|
{
|
|
|
|
if (m_parent)
|
|
|
|
m_parent->unregister_watcher(*this);
|
|
|
|
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(m_watchers.empty());
|
2012-06-14 15:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::register_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(not contains(m_watchers, &watcher));
|
2012-06-14 15:16:44 +02:00
|
|
|
m_watchers.push_back(&watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::unregister_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
auto it = find(m_watchers.begin(), m_watchers.end(), &watcher);
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(it != m_watchers.end());
|
2012-06-14 15:16:44 +02:00
|
|
|
m_watchers.erase(it);
|
|
|
|
}
|
|
|
|
|
2015-04-26 21:40:32 +02:00
|
|
|
struct option_not_found : public runtime_error
|
|
|
|
{
|
|
|
|
option_not_found(StringView name)
|
|
|
|
: runtime_error("option not found: " + name) {}
|
|
|
|
};
|
|
|
|
|
2014-11-16 21:55:36 +01:00
|
|
|
Option& OptionManager::get_local_option(StringView name)
|
2013-03-03 17:25:40 +01:00
|
|
|
{
|
|
|
|
auto it = find_option(m_options, name);
|
|
|
|
if (it != m_options.end())
|
|
|
|
return **it;
|
|
|
|
else if (m_parent)
|
2012-06-12 20:28:25 +02:00
|
|
|
{
|
2013-03-03 17:25:40 +01:00
|
|
|
m_options.emplace_back((*m_parent)[name].clone(*this));
|
|
|
|
return *m_options.back();
|
2012-06-12 20:28:25 +02:00
|
|
|
}
|
2013-03-03 17:25:40 +01:00
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
|
2012-04-03 20:25:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:55:36 +01:00
|
|
|
Option& OptionManager::operator[](StringView name)
|
2012-04-03 20:25:27 +02:00
|
|
|
{
|
2013-03-03 17:25:40 +01:00
|
|
|
auto it = find_option(m_options, name);
|
2012-04-03 20:25:27 +02:00
|
|
|
if (it != m_options.end())
|
2013-03-03 17:25:40 +01:00
|
|
|
return **it;
|
2012-04-03 20:25:27 +02:00
|
|
|
else if (m_parent)
|
|
|
|
return (*m_parent)[name];
|
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
}
|
|
|
|
|
2014-11-16 21:55:36 +01:00
|
|
|
const Option& OptionManager::operator[](StringView name) const
|
2014-05-25 18:36:12 +02:00
|
|
|
{
|
|
|
|
return const_cast<OptionManager&>(*this)[name];
|
|
|
|
}
|
|
|
|
|
2015-08-10 14:54:52 +02:00
|
|
|
void OptionManager::unset_option(StringView name)
|
|
|
|
{
|
|
|
|
kak_assert(m_parent); // cannot unset option on global manager
|
|
|
|
auto it = find_option(m_options, name);
|
|
|
|
if (it != m_options.end())
|
|
|
|
{
|
|
|
|
m_options.erase(it);
|
|
|
|
on_option_changed((*m_parent)[name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
OptionManager::OptionList OptionManager::flatten_options() const
|
2012-06-14 15:16:44 +02:00
|
|
|
{
|
2013-03-03 17:25:40 +01:00
|
|
|
OptionList res = m_parent ? m_parent->flatten_options() : OptionList{};
|
2012-06-14 15:16:44 +02:00
|
|
|
for (auto& option : m_options)
|
2013-03-03 17:25:40 +01:00
|
|
|
{
|
|
|
|
auto it = find_option(res, option->name());
|
|
|
|
if (it != res.end())
|
|
|
|
*it = option.get();
|
|
|
|
else
|
|
|
|
res.emplace_back(option.get());
|
|
|
|
}
|
2012-06-14 15:16:44 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
void OptionManager::on_option_changed(const Option& option)
|
2012-06-14 15:16:44 +02:00
|
|
|
{
|
|
|
|
// if parent option changed, but we overrided it, it's like nothing happened
|
2013-03-03 17:25:40 +01:00
|
|
|
if (&option.manager() != this and
|
|
|
|
find_option(m_options, option.name()) != m_options.end())
|
2012-06-14 15:16:44 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto watcher : m_watchers)
|
2013-03-03 17:25:40 +01:00
|
|
|
watcher->on_option_changed(option);
|
2012-06-14 15:16:44 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 14:38:06 +02:00
|
|
|
CandidateList OptionsRegistry::complete_option_name(StringView prefix,
|
|
|
|
ByteCount cursor_pos) const
|
|
|
|
{
|
2016-04-17 20:21:08 +02:00
|
|
|
using OptionPtr = std::unique_ptr<OptionDesc>;
|
|
|
|
return complete(prefix, cursor_pos, m_descs |
|
|
|
|
filter([](const OptionPtr& desc)
|
|
|
|
{ return not (desc->flags() & OptionFlags::Hidden); }) |
|
|
|
|
transform([](const OptionPtr& desc) -> const String&
|
|
|
|
{ return desc->name(); }));
|
2015-08-10 14:38:06 +02:00
|
|
|
}
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
}
|