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"
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::register_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::unregister_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04: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 22:28:25 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
m_options.emplace_back((*m_parent)[name].clone(*this));
|
|
|
|
return *m_options.back();
|
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
|
|
|
}
|
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
const Option& OptionManager::operator[](const String& name) const
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
auto it = find_option(m_options, name);
|
2012-04-03 22:25:27 +04:00
|
|
|
if (it != m_options.end())
|
2013-03-03 20:25:40 +04:00
|
|
|
return **it;
|
2012-04-03 22:25:27 +04:00
|
|
|
else if (m_parent)
|
|
|
|
return (*m_parent)[name];
|
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
}
|
|
|
|
|
2014-01-02 23:20:16 +04:00
|
|
|
template<typename MatchingFunc>
|
|
|
|
CandidateList OptionManager::get_matching_names(MatchingFunc func)
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
|
|
|
CandidateList result;
|
|
|
|
if (m_parent)
|
2014-01-02 23:20:16 +04:00
|
|
|
result = m_parent->get_matching_names(func);
|
2012-04-03 22:25:27 +04:00
|
|
|
for (auto& option : m_options)
|
|
|
|
{
|
2014-04-12 23:03:26 +04:00
|
|
|
if (option->flags() & OptionFlags::Hidden)
|
2013-11-12 23:21:07 +04:00
|
|
|
continue;
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
const auto& name = option->name();
|
2014-01-02 23:20:16 +04:00
|
|
|
if (func(name) and not contains(result, name))
|
2013-03-03 20:25:40 +04:00
|
|
|
result.push_back(name);
|
2012-04-03 22:25:27 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-04-18 17:02:14 +04:00
|
|
|
CandidateList OptionManager::complete_option_name(StringView prefix,
|
2014-01-02 23:20:16 +04:00
|
|
|
ByteCount cursor_pos)
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
2014-04-18 17:02:14 +04:00
|
|
|
auto real_prefix = prefix.substr(0, cursor_pos);
|
|
|
|
auto result = get_matching_names(std::bind(prefix_match, _1, real_prefix));
|
2014-01-02 23:20:16 +04:00
|
|
|
if (result.empty())
|
2014-04-18 17:02:14 +04:00
|
|
|
result = get_matching_names(std::bind(subsequence_match, _1, real_prefix));
|
2014-01-02 23:20:16 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
auto it = find_option(res, option->name());
|
|
|
|
if (it != res.end())
|
|
|
|
*it = option.get();
|
|
|
|
else
|
|
|
|
res.emplace_back(option.get());
|
|
|
|
}
|
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
|
2013-03-03 20:25:40 +04:00
|
|
|
if (&option.manager() != this and
|
|
|
|
find_option(m_options, option.name()) != m_options.end())
|
2012-06-14 17:16:44 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto watcher : m_watchers)
|
2013-03-03 20:25:40 +04:00
|
|
|
watcher->on_option_changed(option);
|
2012-06-14 17:16:44 +04:00
|
|
|
}
|
|
|
|
|
2012-11-22 16:50:29 +04:00
|
|
|
GlobalOptions::GlobalOptions()
|
2012-04-03 17:39:20 +04:00
|
|
|
: OptionManager()
|
|
|
|
{
|
2014-04-09 23:14:04 +04:00
|
|
|
declare_option("tabstop", "size of a tab character", 8);
|
|
|
|
declare_option("indentwidth", "indentation width", 4);
|
|
|
|
declare_option("scrolloff",
|
|
|
|
"number of lines to keep visible main cursor when scrolling",
|
|
|
|
0);
|
|
|
|
declare_option("eolformat", "end of line format: 'crlf' or 'lf'", "lf"_str);
|
|
|
|
declare_option("BOM", "insert a byte order mark when writing buffer",
|
|
|
|
"no"_str);
|
|
|
|
declare_option("complete_prefix",
|
|
|
|
"complete up to common prefix in tab completion",
|
|
|
|
true);
|
|
|
|
declare_option("incsearch",
|
|
|
|
"incrementaly apply search/select/split regex",
|
|
|
|
true);
|
|
|
|
declare_option("autoinfo",
|
|
|
|
"automatically display contextual help",
|
|
|
|
true);
|
|
|
|
declare_option("autoshowcompl",
|
|
|
|
"automatically display possible completions for prompts",
|
|
|
|
true);
|
|
|
|
declare_option("aligntab",
|
|
|
|
"use tab characters when possible for alignement",
|
|
|
|
false);
|
|
|
|
declare_option("ignored_files",
|
|
|
|
"patterns to ignore when completing filenames",
|
|
|
|
Regex{R"(^(\..*|.*\.(o|so|a))$)"});
|
|
|
|
declare_option("filetype", "buffer filetype", ""_str);
|
|
|
|
declare_option("path", "path to consider when trying to find a file",
|
|
|
|
std::vector<String>({ "./", "/usr/include" }));
|
|
|
|
declare_option("completers", "insert mode completers to execute.",
|
2014-05-11 15:53:08 +04:00
|
|
|
std::vector<String>({"filename", "word=all"}),
|
2014-04-12 23:03:26 +04:00
|
|
|
OptionFlags::None,
|
2014-04-09 23:14:04 +04:00
|
|
|
OptionChecker<std::vector<String>>([](const std::vector<String>& s) {
|
|
|
|
static const auto values = {"word=buffer", "word=all", "filename"};
|
|
|
|
for (auto& v : s)
|
|
|
|
{
|
|
|
|
if (v.substr(0_byte, 7_byte) == "option=")
|
|
|
|
continue;
|
|
|
|
if (not contains(values, v))
|
|
|
|
throw runtime_error(v + " is not a recognised value for completers");
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
declare_option("autoreload",
|
|
|
|
"autoreload buffer when a filesystem modification is detected",
|
|
|
|
Ask);
|
2013-03-03 20:25:40 +04:00
|
|
|
}
|
|
|
|
|
2012-04-03 17:39:20 +04:00
|
|
|
}
|