2012-04-03 15:39:20 +02:00
|
|
|
#include "option_manager.hh"
|
2012-06-14 15:16:44 +02:00
|
|
|
#include "assert.hh"
|
2012-04-03 15:39:20 +02:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert(m_watchers.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::register_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
assert(not contains(m_watchers, &watcher));
|
|
|
|
m_watchers.push_back(&watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::unregister_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
auto it = find(m_watchers.begin(), m_watchers.end(), &watcher);
|
|
|
|
assert(it != m_watchers.end());
|
|
|
|
m_watchers.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::set_option(const String& name, const Option& value)
|
|
|
|
{
|
|
|
|
Option old_value = m_options[name];
|
|
|
|
m_options[name] = value;
|
|
|
|
|
|
|
|
if (old_value != value)
|
2012-06-12 20:28:25 +02:00
|
|
|
{
|
2012-06-14 15:16:44 +02:00
|
|
|
for (auto watcher : m_watchers)
|
|
|
|
watcher->on_option_changed(name, value);
|
2012-06-12 20:28:25 +02:00
|
|
|
}
|
2012-04-03 20:25:27 +02:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:16:44 +02:00
|
|
|
const Option& OptionManager::operator[](const String& name) const
|
2012-04-03 20:25:27 +02:00
|
|
|
{
|
|
|
|
auto it = m_options.find(name);
|
|
|
|
if (it != m_options.end())
|
|
|
|
return it->second;
|
|
|
|
else if (m_parent)
|
|
|
|
return (*m_parent)[name];
|
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList OptionManager::complete_option_name(const String& prefix,
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount cursor_pos)
|
2012-04-03 20:25:27 +02:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
2012-04-03 20:25:27 +02:00
|
|
|
CandidateList result;
|
|
|
|
if (m_parent)
|
|
|
|
result = m_parent->complete_option_name(prefix, cursor_pos);
|
|
|
|
for (auto& option : m_options)
|
|
|
|
{
|
|
|
|
if (option.first.substr(0, real_prefix.length()) == real_prefix and
|
|
|
|
not contains(result, option.first))
|
|
|
|
result.push_back(option.first);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:16:44 +02:00
|
|
|
OptionManager::OptionMap OptionManager::flatten_options() const
|
|
|
|
{
|
|
|
|
OptionMap res = m_parent ? m_parent->flatten_options() : OptionMap();
|
|
|
|
for (auto& option : m_options)
|
|
|
|
res.insert(option);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::on_option_changed(const String& name, const Option& value)
|
|
|
|
{
|
|
|
|
// if parent option changed, but we overrided it, it's like nothing happened
|
|
|
|
if (m_options.find(name) != m_options.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto watcher : m_watchers)
|
|
|
|
watcher->on_option_changed(name, value);
|
|
|
|
}
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
GlobalOptionManager::GlobalOptionManager()
|
|
|
|
: OptionManager()
|
|
|
|
{
|
2012-06-14 15:16:44 +02:00
|
|
|
set_option("tabstop", Option(8));
|
2012-11-19 19:22:11 +01:00
|
|
|
set_option("indentwidth", Option(4));
|
2012-08-10 14:24:13 +02:00
|
|
|
set_option("eolformat", Option("lf"));
|
2012-08-10 18:48:21 +02:00
|
|
|
set_option("BOM", Option("no"));
|
2012-04-03 15:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|