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"
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:25:58 +02:00
|
|
|
void Context::print_status(DisplayLine status, bool immediate) const
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2013-09-16 20:15:13 +02:00
|
|
|
if (has_client())
|
2016-10-29 12:25:58 +02:00
|
|
|
client().print_status(std::move(status), immediate);
|
2013-04-09 19:39:03 +02:00
|
|
|
}
|
|
|
|
|
2015-12-23 02:56:54 +01:00
|
|
|
void JumpList::push(SelectionList jump)
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
if (m_current != m_jumps.size())
|
|
|
|
m_jumps.erase(m_jumps.begin()+m_current+1, m_jumps.end());
|
2015-12-23 02:56:54 +01:00
|
|
|
m_jumps.erase(std::remove(begin(m_jumps), end(m_jumps), jump),
|
|
|
|
end(m_jumps));
|
|
|
|
m_jumps.push_back(jump);
|
2015-12-23 03:31:03 +01:00
|
|
|
m_current = m_jumps.size();
|
2013-04-09 19:39:03 +02:00
|
|
|
}
|
|
|
|
|
2015-12-23 02:56:54 +01:00
|
|
|
const SelectionList& JumpList::forward()
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
if (m_current != m_jumps.size() and
|
|
|
|
m_current + 1 != m_jumps.size())
|
2014-05-13 21:09:37 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
SelectionList& res = m_jumps[++m_current];
|
2014-05-13 21:09:37 +02:00
|
|
|
res.update();
|
|
|
|
return res;
|
|
|
|
}
|
2013-04-09 19:39:03 +02:00
|
|
|
throw runtime_error("no next jump");
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:56:54 +01:00
|
|
|
const SelectionList& JumpList::backward(const SelectionList& current)
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
if (m_current != m_jumps.size() and
|
|
|
|
m_jumps[m_current] != current)
|
2013-10-02 19:48:50 +02:00
|
|
|
{
|
2015-12-23 02:56:54 +01:00
|
|
|
push(current);
|
2015-12-23 03:31:03 +01:00
|
|
|
SelectionList& res = m_jumps[--m_current];
|
2014-05-13 21:09:37 +02:00
|
|
|
res.update();
|
|
|
|
return res;
|
2013-10-02 19:48:50 +02:00
|
|
|
}
|
2015-12-23 03:31:03 +01:00
|
|
|
if (m_current != 0)
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
if (m_current == m_jumps.size())
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 02:56:54 +01:00
|
|
|
push(current);
|
2015-12-23 03:31:03 +01:00
|
|
|
if (--m_current == 0)
|
2015-08-26 11:54:51 +02:00
|
|
|
throw runtime_error("no previous jump");
|
2013-04-09 19:39:03 +02:00
|
|
|
}
|
2015-12-23 03:31:03 +01:00
|
|
|
SelectionList& res = m_jumps[--m_current];
|
2014-05-13 21:09:37 +02:00
|
|
|
res.update();
|
|
|
|
return res;
|
2013-04-09 19:39:03 +02:00
|
|
|
}
|
|
|
|
throw runtime_error("no previous jump");
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:56:54 +01:00
|
|
|
void JumpList::forget_buffer(Buffer& buffer)
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
for (size_t i = 0; i < m_jumps.size();)
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
if (&m_jumps[i].buffer() == &buffer)
|
2013-04-09 19:39:03 +02:00
|
|
|
{
|
2015-12-23 03:31:03 +01:00
|
|
|
if (i < m_current)
|
2015-12-23 02:56:54 +01:00
|
|
|
--m_current;
|
2015-12-23 03:31:03 +01:00
|
|
|
else if (i == m_current)
|
|
|
|
m_current = m_jumps.size()-1;
|
2013-04-09 19:39:03 +02:00
|
|
|
|
2015-12-23 03:31:03 +01:00
|
|
|
m_jumps.erase(m_jumps.begin() + i);
|
2013-04-09 19:39:03 +02:00
|
|
|
}
|
|
|
|
else
|
2015-12-23 03:31:03 +01:00
|
|
|
++i;
|
2013-04-09 19:39:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-05-13 12:18:21 +02:00
|
|
|
|
|
|
|
if (has_input_handler())
|
|
|
|
input_handler().reset_normal_mode();
|
|
|
|
|
2013-12-20 21:10:08 +01:00
|
|
|
m_window.reset();
|
|
|
|
if (has_client())
|
2016-06-16 20:35:43 +02:00
|
|
|
{
|
|
|
|
client().info_hide();
|
|
|
|
client().menu_hide();
|
2017-05-13 12:18:21 +02:00
|
|
|
client().change_buffer(buffer);
|
2016-06-16 20:35:43 +02:00
|
|
|
}
|
2013-12-20 21:10:08 +01:00
|
|
|
else
|
2014-05-14 01:59:36 +02:00
|
|
|
m_selections = SelectionList{buffer, Selection{}};
|
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
|
|
|
|
{
|
2017-02-14 01:02:01 +01:00
|
|
|
auto strings = RegisterManager::instance()[reg].get(*this);
|
2014-06-21 12:31:08 +02:00
|
|
|
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
|
|
|
}
|