2012-04-03 17:39:20 +04:00
|
|
|
#include "option_manager.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
#include "assert.hh"
|
2017-03-15 20:55:34 +03:00
|
|
|
#include "flags.hh"
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-04-12 23:03:26 +04: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 20:25:40 +04:00
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
OptionManager::OptionManager(OptionManager& parent)
|
|
|
|
: m_parent(&parent)
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
2012-06-14 17:16:44 +04:00
|
|
|
parent.register_watcher(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionManager::~OptionManager()
|
|
|
|
{
|
|
|
|
if (m_parent)
|
|
|
|
m_parent->unregister_watcher(*this);
|
|
|
|
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_watchers.empty());
|
2012-06-14 17:16:44 +04:00
|
|
|
}
|
|
|
|
|
2016-08-21 22:24:18 +03:00
|
|
|
void OptionManager::register_watcher(OptionManagerWatcher& watcher) const
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(not contains(m_watchers, &watcher));
|
2012-06-14 17:16:44 +04:00
|
|
|
m_watchers.push_back(&watcher);
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:24:18 +03:00
|
|
|
void OptionManager::unregister_watcher(OptionManagerWatcher& watcher) const
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
|
|
|
auto it = find(m_watchers.begin(), m_watchers.end(), &watcher);
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(it != m_watchers.end());
|
2012-06-14 17:16:44 +04:00
|
|
|
m_watchers.erase(it);
|
|
|
|
}
|
|
|
|
|
2015-04-26 22:40:32 +03:00
|
|
|
struct option_not_found : public runtime_error
|
|
|
|
{
|
|
|
|
option_not_found(StringView name)
|
|
|
|
: runtime_error("option not found: " + name) {}
|
|
|
|
};
|
|
|
|
|
2014-11-16 23:55:36 +03:00
|
|
|
Option& OptionManager::get_local_option(StringView name)
|
2013-03-03 20:25:40 +04:00
|
|
|
{
|
2017-06-23 11:54:21 +03:00
|
|
|
auto it = m_options.find(name);
|
2013-03-03 20:25:40 +04:00
|
|
|
if (it != m_options.end())
|
2017-06-23 11:54:21 +03:00
|
|
|
return *(it->value);
|
2013-03-03 20:25:40 +04:00
|
|
|
else if (m_parent)
|
2012-06-12 22:28:25 +04:00
|
|
|
{
|
2017-06-23 11:54:21 +03:00
|
|
|
auto* clone = (*m_parent)[name].clone(*this);
|
|
|
|
return *m_options.insert({clone->name(), std::unique_ptr<Option>{clone}});
|
2012-06-12 22:28:25 +04:00
|
|
|
}
|
2013-03-03 20:25:40 +04:00
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
|
2012-04-03 22:25:27 +04:00
|
|
|
}
|
|
|
|
|
2014-11-16 23:55:36 +03:00
|
|
|
Option& OptionManager::operator[](StringView name)
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
2017-06-23 11:54:21 +03:00
|
|
|
auto it = m_options.find(name);
|
2012-04-03 22:25:27 +04:00
|
|
|
if (it != m_options.end())
|
2017-06-23 11:54:21 +03:00
|
|
|
return *it->value;
|
2012-04-03 22:25:27 +04:00
|
|
|
else if (m_parent)
|
|
|
|
return (*m_parent)[name];
|
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
}
|
|
|
|
|
2014-11-16 23:55:36 +03:00
|
|
|
const Option& OptionManager::operator[](StringView name) const
|
2014-05-25 20:36:12 +04:00
|
|
|
{
|
|
|
|
return const_cast<OptionManager&>(*this)[name];
|
|
|
|
}
|
|
|
|
|
2015-08-10 15:54:52 +03:00
|
|
|
void OptionManager::unset_option(StringView name)
|
|
|
|
{
|
|
|
|
kak_assert(m_parent); // cannot unset option on global manager
|
2017-06-23 11:54:21 +03:00
|
|
|
if (m_options.contains(name))
|
2015-08-10 15:54:52 +03:00
|
|
|
{
|
2017-06-23 11:54:21 +03:00
|
|
|
m_options.erase(name);
|
2015-08-10 15:54:52 +03:00
|
|
|
on_option_changed((*m_parent)[name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
OptionManager::OptionList OptionManager::flatten_options() const
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
OptionList res = m_parent ? m_parent->flatten_options() : OptionList{};
|
2012-06-14 17:16:44 +04:00
|
|
|
for (auto& option : m_options)
|
2013-03-03 20:25:40 +04:00
|
|
|
{
|
2017-06-23 11:54:21 +03:00
|
|
|
auto it = find_if(res, [&](const Option* opt) { return opt->name() == option.key; });
|
2013-03-03 20:25:40 +04:00
|
|
|
if (it != res.end())
|
2017-06-23 11:54:21 +03:00
|
|
|
*it = option.value.get();
|
2013-03-03 20:25:40 +04:00
|
|
|
else
|
2017-06-23 11:54:21 +03:00
|
|
|
res.emplace_back(option.value.get());
|
2013-03-03 20:25:40 +04:00
|
|
|
}
|
2012-06-14 17:16:44 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
void OptionManager::on_option_changed(const Option& option)
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
|
|
|
// if parent option changed, but we overrided it, it's like nothing happened
|
2017-06-23 11:54:21 +03:00
|
|
|
if (&option.manager() != this and m_options.contains(option.name()))
|
2012-06-14 17:16:44 +04:00
|
|
|
return;
|
|
|
|
|
2017-02-20 16:44:08 +03:00
|
|
|
// The watcher list might get mutated during calls to on_option_changed
|
|
|
|
auto watchers = m_watchers;
|
|
|
|
for (auto* watcher : watchers)
|
|
|
|
{
|
|
|
|
if (contains(m_watchers, watcher)) // make sure this watcher is still alive
|
|
|
|
watcher->on_option_changed(option);
|
|
|
|
}
|
2012-06-14 17:16:44 +04:00
|
|
|
}
|
|
|
|
|
2015-08-10 15:38:06 +03:00
|
|
|
CandidateList OptionsRegistry::complete_option_name(StringView prefix,
|
|
|
|
ByteCount cursor_pos) const
|
|
|
|
{
|
2016-09-27 00:59:02 +03:00
|
|
|
using OptionPtr = std::unique_ptr<const OptionDesc>;
|
2016-04-17 21:21:08 +03:00
|
|
|
return complete(prefix, cursor_pos, m_descs |
|
|
|
|
filter([](const OptionPtr& desc)
|
|
|
|
{ return not (desc->flags() & OptionFlags::Hidden); }) |
|
2016-10-11 02:32:40 +03:00
|
|
|
transform(std::mem_fn(&OptionDesc::name)));
|
2015-08-10 15:38:06 +03:00
|
|
|
}
|
|
|
|
|
2012-04-03 17:39:20 +04:00
|
|
|
}
|