2013-04-09 21:39:03 +04:00
|
|
|
#include "context.hh"
|
|
|
|
|
2014-10-30 02:22:54 +03:00
|
|
|
#include "alias_registry.hh"
|
2013-09-13 01:47:23 +04:00
|
|
|
#include "client.hh"
|
2013-04-09 21:39:03 +04:00
|
|
|
#include "user_interface.hh"
|
2014-06-21 14:31:08 +04:00
|
|
|
#include "register_manager.hh"
|
2013-04-09 21:39:03 +04:00
|
|
|
#include "window.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-12-21 00:10:08 +04:00
|
|
|
Context::~Context() = default;
|
2013-04-09 21:39:03 +04:00
|
|
|
|
2014-05-13 02:25:15 +04:00
|
|
|
Context::Context(InputHandler& input_handler, SelectionList selections,
|
2014-12-19 02:12:58 +03:00
|
|
|
Flags flags, String name)
|
2014-04-04 03:00:06 +04:00
|
|
|
: m_input_handler{&input_handler},
|
2014-05-13 02:25:15 +04:00
|
|
|
m_selections{std::move(selections)},
|
2014-12-19 02:12:58 +03:00
|
|
|
m_flags(flags),
|
2014-04-04 03:00:06 +04:00
|
|
|
m_name(std::move(name))
|
|
|
|
{}
|
2013-12-17 03:24:08 +04:00
|
|
|
|
2015-04-19 20:47:52 +03:00
|
|
|
Context::Context(EmptyContextFlag) {}
|
|
|
|
|
2013-04-09 21:39:03 +04:00
|
|
|
Buffer& Context::buffer() const
|
|
|
|
{
|
|
|
|
if (not has_buffer())
|
|
|
|
throw runtime_error("no buffer in context");
|
2014-05-14 03:59:36 +04:00
|
|
|
return const_cast<Buffer&>((*m_selections).buffer());
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Window& Context::window() const
|
|
|
|
{
|
|
|
|
if (not has_window())
|
|
|
|
throw runtime_error("no window in context");
|
2013-12-21 00:10:08 +04:00
|
|
|
return *m_window;
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 22:09:15 +04:00
|
|
|
InputHandler& Context::input_handler() const
|
|
|
|
{
|
|
|
|
if (not has_input_handler())
|
|
|
|
throw runtime_error("no input handler in context");
|
|
|
|
return *m_input_handler;
|
|
|
|
}
|
|
|
|
|
2013-09-13 01:47:23 +04:00
|
|
|
Client& Context::client() const
|
2013-04-09 21:39:03 +04:00
|
|
|
{
|
2013-09-13 01:47:23 +04:00
|
|
|
if (not has_client())
|
2013-11-14 22:09:15 +04:00
|
|
|
throw runtime_error("no client in context");
|
2013-09-13 01:47:23 +04:00
|
|
|
return *m_client;
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
UserInterface& Context::ui() const
|
|
|
|
{
|
|
|
|
if (not has_ui())
|
|
|
|
throw runtime_error("no user interface in context");
|
2013-11-14 22:09:15 +04:00
|
|
|
return client().ui();
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
2014-10-30 17:00:42 +03:00
|
|
|
Scope& Context::scope() const
|
2013-04-09 21:39:03 +04:00
|
|
|
{
|
|
|
|
if (has_window())
|
2014-10-30 17:00:42 +03:00
|
|
|
return window();
|
2013-04-09 21:39:03 +04:00
|
|
|
if (has_buffer())
|
2014-10-30 17:00:42 +03:00
|
|
|
return buffer();
|
|
|
|
return GlobalScope::instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionManager& Context::options() const
|
|
|
|
{
|
|
|
|
return scope().options();
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HookManager& Context::hooks() const
|
|
|
|
{
|
2014-10-30 17:00:42 +03:00
|
|
|
return scope().hooks();
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
2013-10-25 03:01:17 +04:00
|
|
|
KeymapManager& Context::keymaps() const
|
|
|
|
{
|
2014-10-30 17:00:42 +03:00
|
|
|
return scope().keymaps();
|
2013-10-25 03:01:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-30 02:22:54 +03:00
|
|
|
AliasRegistry& Context::aliases() const
|
|
|
|
{
|
2014-10-30 17:00:42 +03:00
|
|
|
return scope().aliases();
|
2014-10-30 02:22:54 +03:00
|
|
|
}
|
|
|
|
|
2013-11-14 22:09:15 +04:00
|
|
|
void Context::set_client(Client& client)
|
|
|
|
{
|
|
|
|
kak_assert(not has_client());
|
|
|
|
m_client.reset(&client);
|
|
|
|
}
|
|
|
|
|
2013-12-21 00:10:08 +04:00
|
|
|
void Context::set_window(Window& window)
|
|
|
|
{
|
|
|
|
kak_assert(&window.buffer() == &buffer());
|
|
|
|
m_window.reset(&window);
|
|
|
|
}
|
|
|
|
|
2013-09-16 22:15:13 +04:00
|
|
|
void Context::print_status(DisplayLine status) const
|
2013-04-09 21:39:03 +04:00
|
|
|
{
|
2013-09-16 22:15:13 +04:00
|
|
|
if (has_client())
|
|
|
|
client().print_status(std::move(status));
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Context::push_jump()
|
|
|
|
{
|
2013-12-15 18:25:23 +04:00
|
|
|
const SelectionList& jump = selections();
|
2013-04-09 21:39:03 +04:00
|
|
|
if (m_current_jump != m_jump_list.end())
|
|
|
|
{
|
|
|
|
auto begin = m_current_jump;
|
2014-05-13 23:09:37 +04:00
|
|
|
if (&buffer() != &begin->buffer() or *begin != jump)
|
2013-04-09 21:39:03 +04:00
|
|
|
++begin;
|
|
|
|
m_jump_list.erase(begin, m_jump_list.end());
|
|
|
|
}
|
|
|
|
m_jump_list.erase(std::remove(begin(m_jump_list), end(m_jump_list), jump),
|
|
|
|
end(m_jump_list));
|
2014-05-13 02:25:15 +04:00
|
|
|
m_jump_list.push_back(jump);
|
2013-04-09 21:39:03 +04:00
|
|
|
m_current_jump = m_jump_list.end();
|
|
|
|
}
|
|
|
|
|
2014-05-13 23:09:37 +04:00
|
|
|
const SelectionList& Context::jump_forward()
|
2013-04-09 21:39:03 +04:00
|
|
|
{
|
|
|
|
if (m_current_jump != m_jump_list.end() and
|
|
|
|
m_current_jump + 1 != m_jump_list.end())
|
2014-05-13 23:09:37 +04:00
|
|
|
{
|
|
|
|
SelectionList& res = *++m_current_jump;
|
|
|
|
res.update();
|
|
|
|
return res;
|
|
|
|
}
|
2013-04-09 21:39:03 +04:00
|
|
|
throw runtime_error("no next jump");
|
|
|
|
}
|
|
|
|
|
2014-05-13 23:09:37 +04:00
|
|
|
const SelectionList& Context::jump_backward()
|
2013-04-09 21:39:03 +04:00
|
|
|
{
|
2013-10-02 21:48:50 +04:00
|
|
|
if (m_current_jump != m_jump_list.end() and
|
2013-12-15 18:25:23 +04:00
|
|
|
*m_current_jump != selections())
|
2013-10-02 21:48:50 +04:00
|
|
|
{
|
|
|
|
push_jump();
|
2014-05-13 23:09:37 +04:00
|
|
|
SelectionList& res = *--m_current_jump;
|
|
|
|
res.update();
|
|
|
|
return res;
|
2013-10-02 21:48:50 +04:00
|
|
|
}
|
2013-04-09 21:39:03 +04:00
|
|
|
if (m_current_jump != m_jump_list.begin())
|
|
|
|
{
|
|
|
|
if (m_current_jump == m_jump_list.end())
|
|
|
|
{
|
|
|
|
push_jump();
|
|
|
|
--m_current_jump;
|
|
|
|
}
|
2014-05-13 23:09:37 +04:00
|
|
|
SelectionList& res = *--m_current_jump;
|
|
|
|
res.update();
|
|
|
|
return res;
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
throw runtime_error("no previous jump");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Context::forget_jumps_to_buffer(Buffer& buffer)
|
|
|
|
{
|
|
|
|
for (auto it = m_jump_list.begin(); it != m_jump_list.end();)
|
|
|
|
{
|
|
|
|
if (&it->buffer() == &buffer)
|
|
|
|
{
|
|
|
|
if (it < m_current_jump)
|
|
|
|
--m_current_jump;
|
|
|
|
else if (it == m_current_jump)
|
|
|
|
m_current_jump = m_jump_list.end()-1;
|
|
|
|
|
|
|
|
it = m_jump_list.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 00:10:08 +04:00
|
|
|
void Context::change_buffer(Buffer& buffer)
|
2013-04-09 21:39:03 +04:00
|
|
|
{
|
2014-10-10 17:00:24 +04:00
|
|
|
if (&buffer == &this->buffer())
|
|
|
|
return;
|
|
|
|
|
2014-01-07 00:07:08 +04:00
|
|
|
if (m_edition_level > 0)
|
2014-10-10 17:00:24 +04:00
|
|
|
this->buffer().commit_undo_group();
|
2014-11-21 21:56:39 +03:00
|
|
|
|
2013-12-21 00:10:08 +04:00
|
|
|
m_window.reset();
|
|
|
|
if (has_client())
|
|
|
|
client().change_buffer(buffer);
|
|
|
|
else
|
2014-05-14 03:59:36 +04:00
|
|
|
m_selections = SelectionList{buffer, Selection{}};
|
2013-11-14 22:09:15 +04:00
|
|
|
if (has_input_handler())
|
|
|
|
input_handler().reset_normal_mode();
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
2013-12-15 18:25:23 +04:00
|
|
|
SelectionList& Context::selections()
|
|
|
|
{
|
2013-12-21 00:10:08 +04:00
|
|
|
if (not m_selections)
|
|
|
|
throw runtime_error("no selections in context");
|
2014-05-14 03:59:36 +04:00
|
|
|
(*m_selections).update();
|
2013-12-21 00:10:08 +04:00
|
|
|
return *m_selections;
|
2013-12-15 18:25:23 +04:00
|
|
|
}
|
|
|
|
|
2015-04-19 17:12:16 +03:00
|
|
|
SelectionList& Context::selections_write_only()
|
|
|
|
{
|
|
|
|
if (not m_selections)
|
|
|
|
throw runtime_error("no selections in context");
|
|
|
|
return *m_selections;
|
|
|
|
}
|
|
|
|
|
2013-12-15 18:25:23 +04:00
|
|
|
const SelectionList& Context::selections() const
|
|
|
|
{
|
2014-05-14 03:59:36 +04:00
|
|
|
return const_cast<Context&>(*this).selections();
|
2013-12-15 18:25:23 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 16:58:41 +03:00
|
|
|
Vector<String> Context::selections_content() const
|
2013-12-16 00:37:07 +04:00
|
|
|
{
|
2014-04-04 03:00:06 +04:00
|
|
|
auto& buf = buffer();
|
2015-01-12 16:58:41 +03:00
|
|
|
Vector<String> contents;
|
2013-12-16 00:37:07 +04:00
|
|
|
for (auto& sel : selections())
|
2014-04-04 03:00:06 +04:00
|
|
|
contents.push_back(buf.string(sel.min(), buf.char_next(sel.max())));
|
2013-12-16 00:37:07 +04:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2013-12-15 22:07:51 +04:00
|
|
|
void Context::begin_edition()
|
|
|
|
{
|
2014-01-07 00:07:08 +04:00
|
|
|
if (m_edition_level >= 0)
|
|
|
|
++m_edition_level;
|
2013-12-15 22:07:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Context::end_edition()
|
|
|
|
{
|
2014-01-07 00:07:08 +04:00
|
|
|
if (m_edition_level < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
kak_assert(m_edition_level != 0);
|
2013-12-15 22:07:51 +04:00
|
|
|
if (m_edition_level == 1)
|
|
|
|
buffer().commit_undo_group();
|
|
|
|
|
|
|
|
--m_edition_level;
|
|
|
|
}
|
|
|
|
|
2014-06-21 14:31:08 +04:00
|
|
|
StringView Context::main_sel_register_value(StringView reg) const
|
|
|
|
{
|
|
|
|
auto strings = RegisterManager::instance()[reg].values(*this);
|
|
|
|
size_t index = m_selections ? (*m_selections).main_index() : 0;
|
2014-06-23 16:16:51 +04:00
|
|
|
if (strings.size() <= index)
|
2014-06-21 14:31:08 +04:00
|
|
|
index = strings.size() - 1;
|
|
|
|
return strings[index];
|
|
|
|
}
|
|
|
|
|
2013-04-09 21:39:03 +04:00
|
|
|
}
|