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