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
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-11-12 20:21:07 +01:00
|
|
|
Option::Option(OptionManager& manager, String name, Flags flags)
|
|
|
|
: m_manager(manager), m_name(std::move(name)), m_flags(flags) {}
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
Option& OptionManager::get_local_option(const String& name)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-06-14 15:16:44 +02:00
|
|
|
const Option& OptionManager::operator[](const String& name) const
|
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-01-02 20:20:16 +01:00
|
|
|
template<typename MatchingFunc>
|
|
|
|
CandidateList OptionManager::get_matching_names(MatchingFunc func)
|
2012-04-03 20:25:27 +02:00
|
|
|
{
|
|
|
|
CandidateList result;
|
|
|
|
if (m_parent)
|
2014-01-02 20:20:16 +01:00
|
|
|
result = m_parent->get_matching_names(func);
|
2012-04-03 20:25:27 +02:00
|
|
|
for (auto& option : m_options)
|
|
|
|
{
|
2013-11-12 20:21:07 +01:00
|
|
|
if (option->flags() & Option::Flags::Hidden)
|
|
|
|
continue;
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
const auto& name = option->name();
|
2014-01-02 20:20:16 +01:00
|
|
|
if (func(name) and not contains(result, name))
|
2013-03-03 17:25:40 +01:00
|
|
|
result.push_back(name);
|
2012-04-03 20:25:27 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-01-02 20:20:16 +01:00
|
|
|
CandidateList OptionManager::complete_option_name(const String& prefix,
|
|
|
|
ByteCount cursor_pos)
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
|
|
|
auto result = get_matching_names(std::bind(prefix_match, _1, std::ref(real_prefix)));
|
|
|
|
if (result.empty())
|
|
|
|
result = get_matching_names(std::bind(subsequence_match, _1, std::ref(real_prefix)));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-22 13:50:29 +01:00
|
|
|
GlobalOptions::GlobalOptions()
|
2012-04-03 15:39:20 +02:00
|
|
|
: OptionManager()
|
|
|
|
{
|
2013-03-03 17:25:40 +01:00
|
|
|
declare_option<int>("tabstop", 8);
|
|
|
|
declare_option<int>("indentwidth", 4);
|
2013-05-16 19:22:44 +02:00
|
|
|
declare_option<int>("scrolloff", 0);
|
2013-03-03 17:25:40 +01:00
|
|
|
declare_option<String>("eolformat", "lf");
|
|
|
|
declare_option<String>("BOM", "no");
|
2013-03-31 22:33:06 +02:00
|
|
|
declare_option<String>("shell", "bash");
|
2013-03-05 19:03:42 +01:00
|
|
|
declare_option<bool>("complete_prefix", true);
|
|
|
|
declare_option<bool>("incsearch", true);
|
2013-07-26 19:52:25 +02:00
|
|
|
declare_option<bool>("autoinfo", true);
|
2013-11-04 22:53:10 +01:00
|
|
|
declare_option<bool>("autoshowcompl", true);
|
2013-11-06 20:11:46 +01:00
|
|
|
declare_option<bool>("aligntab", false);
|
2013-03-14 13:42:07 +01:00
|
|
|
declare_option<Regex>("ignored_files", Regex{R"(^(\..*|.*\.(o|so|a))$)"});
|
2013-03-03 17:25:40 +01:00
|
|
|
declare_option<String>("filetype", "");
|
2013-03-09 14:23:19 +01:00
|
|
|
declare_option<std::vector<String>>("completions", {});
|
2013-03-13 18:52:55 +01:00
|
|
|
declare_option<std::vector<String>>("path", { "./", "/usr/include" });
|
2013-08-02 18:58:37 +02:00
|
|
|
declare_option<std::vector<String>>("completers", {"option", "filename", "word=buffer"},
|
2013-11-12 20:21:07 +01:00
|
|
|
Option::Flags::None,
|
2013-08-02 18:58:37 +02:00
|
|
|
[](const std::vector<String>& s) {
|
|
|
|
static const auto values = {"option", "word=buffer", "word=all", "filename" };
|
2013-05-06 13:52:41 +02:00
|
|
|
for (auto& v : s)
|
2013-08-02 18:58:37 +02:00
|
|
|
if (not contains(values, v))
|
2013-05-06 13:52:41 +02:00
|
|
|
throw runtime_error(v + " is not a recognised value for completers");
|
|
|
|
});
|
2013-10-21 19:58:11 +02:00
|
|
|
declare_option<YesNoAsk>("autoreload", Ask);
|
2013-03-03 17:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
}
|