2014-04-28 20:48:23 +02:00
|
|
|
#include "buffer_utils.hh"
|
|
|
|
|
2014-08-15 14:21:54 +02:00
|
|
|
#include "buffer_manager.hh"
|
2014-04-29 22:37:11 +02:00
|
|
|
#include "event_manager.hh"
|
2015-10-16 02:33:17 +02:00
|
|
|
#include "file.hh"
|
2018-11-14 07:52:57 +01:00
|
|
|
#include "selection.hh"
|
2020-06-27 05:33:50 +02:00
|
|
|
#include "changes.hh"
|
2014-04-29 22:37:11 +02:00
|
|
|
|
2016-11-29 00:53:50 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-18 13:45:27 +02:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define st_mtim st_mtimespec
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2020-06-27 05:33:50 +02:00
|
|
|
void replace(Buffer& buffer, ArrayView<BufferRange> ranges, ConstArrayView<String> strings)
|
|
|
|
{
|
|
|
|
ForwardChangesTracker changes_tracker;
|
|
|
|
size_t timestamp = buffer.timestamp();
|
|
|
|
for (size_t index = 0; index < ranges.size(); ++index)
|
|
|
|
{
|
|
|
|
auto& range = ranges[index];
|
|
|
|
range.begin = changes_tracker.get_new_coord_tolerant(range.begin);
|
|
|
|
range.end = changes_tracker.get_new_coord_tolerant(range.end);
|
|
|
|
kak_assert(buffer.is_valid(range.begin) and buffer.is_valid(range.end));
|
|
|
|
|
|
|
|
range = buffer.replace(range.begin, range.end, strings.empty() ? StringView{} : strings[std::min(index, strings.size()-1)]);
|
|
|
|
kak_assert(buffer.is_valid(range.begin) and buffer.is_valid(range.end));
|
|
|
|
changes_tracker.update(buffer, timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.check_invariant();
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount get_column(const Buffer& buffer,
|
|
|
|
ColumnCount tabstop, BufferCoord coord)
|
2014-04-28 20:48:23 +02:00
|
|
|
{
|
2014-10-03 14:39:13 +02:00
|
|
|
auto line = buffer[coord.line];
|
2016-09-22 21:36:26 +02:00
|
|
|
auto col = 0_col;
|
2014-04-28 20:48:23 +02:00
|
|
|
for (auto it = line.begin();
|
2016-09-24 19:52:54 +02:00
|
|
|
it != line.end() and coord.column > (int)(it - line.begin()); )
|
2014-04-28 20:48:23 +02:00
|
|
|
{
|
|
|
|
if (*it == '\t')
|
2016-09-24 19:52:54 +02:00
|
|
|
{
|
2014-04-28 20:48:23 +02:00
|
|
|
col = (col / tabstop + 1) * tabstop;
|
2016-09-24 19:52:54 +02:00
|
|
|
++it;
|
|
|
|
}
|
2014-04-28 20:48:23 +02:00
|
|
|
else
|
2016-09-25 11:55:57 +02:00
|
|
|
col += codepoint_width(utf8::read_codepoint(it, line.end()));
|
2014-04-28 20:48:23 +02:00
|
|
|
}
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:45:48 +01:00
|
|
|
ColumnCount column_length(const Buffer& buffer, ColumnCount tabstop, LineCount line)
|
|
|
|
{
|
|
|
|
return get_column(buffer, tabstop, BufferCoord{line, ByteCount{INT_MAX}});
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
ByteCount get_byte_to_column(const Buffer& buffer, ColumnCount tabstop, DisplayCoord coord)
|
2015-02-25 14:40:19 +01:00
|
|
|
{
|
|
|
|
auto line = buffer[coord.line];
|
2016-09-22 21:36:26 +02:00
|
|
|
auto col = 0_col;
|
2015-02-25 14:40:19 +01:00
|
|
|
auto it = line.begin();
|
|
|
|
while (it != line.end() and coord.column > col)
|
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
{
|
|
|
|
col = (col / tabstop + 1) * tabstop;
|
|
|
|
if (col > coord.column) // the target column was in the tab
|
|
|
|
break;
|
2016-09-24 19:52:54 +02:00
|
|
|
++it;
|
2015-02-25 14:40:19 +01:00
|
|
|
}
|
|
|
|
else
|
2016-09-24 19:52:54 +02:00
|
|
|
{
|
|
|
|
auto next = it;
|
2016-09-25 11:55:57 +02:00
|
|
|
col += codepoint_width(utf8::read_codepoint(next, line.end()));
|
2016-09-24 19:52:54 +02:00
|
|
|
if (col > coord.column) // the target column was in the char
|
|
|
|
break;
|
|
|
|
it = next;
|
|
|
|
}
|
2015-02-25 14:40:19 +01:00
|
|
|
}
|
|
|
|
return (int)(it - line.begin());
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:59:33 +01:00
|
|
|
Buffer* open_file_buffer(StringView filename, Buffer::Flags flags)
|
2015-10-16 02:33:17 +02:00
|
|
|
{
|
2017-08-29 04:42:04 +02:00
|
|
|
MappedFile file_data{parse_filename(filename)};
|
2016-05-14 09:33:50 +02:00
|
|
|
return BufferManager::instance().create_buffer(
|
2016-11-14 14:59:33 +01:00
|
|
|
filename.str(), Buffer::Flags::File | flags, file_data, file_data.st.st_mtim);
|
2015-10-16 02:33:17 +02:00
|
|
|
}
|
2014-08-15 14:21:54 +02:00
|
|
|
|
2016-11-14 14:59:33 +01:00
|
|
|
Buffer* open_or_create_file_buffer(StringView filename, Buffer::Flags flags)
|
2015-10-16 02:33:17 +02:00
|
|
|
{
|
2016-05-14 09:33:50 +02:00
|
|
|
auto& buffer_manager = BufferManager::instance();
|
2017-08-29 04:42:04 +02:00
|
|
|
auto path = parse_filename(filename);
|
|
|
|
if (file_exists(path))
|
2015-10-16 02:33:17 +02:00
|
|
|
{
|
2017-08-29 04:42:04 +02:00
|
|
|
MappedFile file_data{path};
|
2016-11-14 14:59:33 +01:00
|
|
|
return buffer_manager.create_buffer(filename.str(), Buffer::Flags::File | flags,
|
2016-05-14 09:33:50 +02:00
|
|
|
file_data, file_data.st.st_mtim);
|
2015-10-16 02:33:17 +02:00
|
|
|
}
|
2016-05-14 09:33:50 +02:00
|
|
|
return buffer_manager.create_buffer(
|
|
|
|
filename.str(), Buffer::Flags::File | Buffer::Flags::New,
|
|
|
|
{}, InvalidTime);
|
2015-10-16 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void reload_file_buffer(Buffer& buffer)
|
|
|
|
{
|
|
|
|
kak_assert(buffer.flags() & Buffer::Flags::File);
|
|
|
|
MappedFile file_data{buffer.name()};
|
|
|
|
buffer.reload(file_data, file_data.st.st_mtim);
|
2018-02-05 10:18:51 +01:00
|
|
|
buffer.flags() &= ~Buffer::Flags::New;
|
2015-10-16 02:33:17 +02:00
|
|
|
}
|
|
|
|
|
2017-03-08 20:33:25 +01:00
|
|
|
Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll)
|
2014-04-29 22:37:11 +02:00
|
|
|
{
|
2019-12-28 00:32:15 +01:00
|
|
|
static ValueId fifo_watcher_id = get_free_value_id();
|
2014-04-29 22:37:11 +02:00
|
|
|
|
2016-05-14 09:33:50 +02:00
|
|
|
auto& buffer_manager = BufferManager::instance();
|
|
|
|
Buffer* buffer = buffer_manager.get_buffer_ifp(name);
|
2014-11-04 14:55:56 +01:00
|
|
|
if (buffer)
|
|
|
|
{
|
2017-03-08 20:33:25 +01:00
|
|
|
buffer->flags() |= Buffer::Flags::NoUndo | flags;
|
2015-10-16 14:52:14 +02:00
|
|
|
buffer->reload({}, InvalidTime);
|
2014-11-04 14:55:56 +01:00
|
|
|
}
|
|
|
|
else
|
2016-05-14 09:33:50 +02:00
|
|
|
buffer = buffer_manager.create_buffer(
|
2017-03-08 20:33:25 +01:00
|
|
|
std::move(name), flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
|
2014-11-04 14:55:56 +01:00
|
|
|
|
2019-12-28 00:32:15 +01:00
|
|
|
struct FifoWatcher : FDWatcher
|
|
|
|
{
|
|
|
|
FifoWatcher(int fd, Buffer& buffer, bool scroll)
|
2021-03-10 23:02:02 +01:00
|
|
|
: FDWatcher(fd, FdEvents::Read, EventMode::Normal,
|
2019-12-28 00:32:15 +01:00
|
|
|
[](FDWatcher& watcher, FdEvents, EventMode mode) {
|
|
|
|
if (mode == EventMode::Normal)
|
|
|
|
static_cast<FifoWatcher&>(watcher).read_fifo();
|
|
|
|
}),
|
|
|
|
m_buffer(buffer), m_scroll(scroll)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~FifoWatcher()
|
|
|
|
{
|
|
|
|
kak_assert(m_buffer.flags() & Buffer::Flags::Fifo);
|
|
|
|
close_fd();
|
|
|
|
m_buffer.run_hook_in_own_context(Hook::BufCloseFifo, "");
|
|
|
|
m_buffer.flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
|
|
|
|
}
|
2014-11-04 14:55:56 +01:00
|
|
|
|
2019-12-28 00:32:15 +01:00
|
|
|
void read_fifo() const
|
2014-05-15 20:11:59 +02:00
|
|
|
{
|
2019-12-28 00:32:15 +01:00
|
|
|
kak_assert(m_buffer.flags() & Buffer::Flags::Fifo);
|
|
|
|
|
|
|
|
constexpr size_t buffer_size = 2048;
|
|
|
|
// if we read data slower than it arrives in the fifo, limiting the
|
|
|
|
// iteration number allows us to go back go back to the event loop and
|
|
|
|
// handle other events sources (such as input)
|
2021-03-31 08:19:20 +02:00
|
|
|
constexpr size_t max_loop = 1024;
|
2019-12-28 00:32:15 +01:00
|
|
|
bool closed = false;
|
|
|
|
size_t loop = 0;
|
|
|
|
char data[buffer_size];
|
|
|
|
BufferCoord insert_coord = m_buffer.back_coord();
|
|
|
|
const int fifo = fd();
|
2020-03-14 02:58:22 +01:00
|
|
|
|
2017-01-25 14:55:20 +01:00
|
|
|
{
|
2020-03-14 02:58:22 +01:00
|
|
|
auto restore_flags = on_scope_end([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; });
|
|
|
|
m_buffer.flags() &= ~Buffer::Flags::ReadOnly;
|
|
|
|
do
|
2019-12-28 00:32:15 +01:00
|
|
|
{
|
|
|
|
|
2020-03-14 02:58:22 +01:00
|
|
|
const ssize_t count = ::read(fifo, data, buffer_size);
|
|
|
|
if (count <= 0)
|
|
|
|
{
|
|
|
|
closed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pos = m_buffer.back_coord();
|
|
|
|
const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll;
|
|
|
|
if (prevent_scrolling)
|
|
|
|
pos = m_buffer.next(pos);
|
|
|
|
|
|
|
|
m_buffer.insert(pos, StringView(data, data+count));
|
|
|
|
|
|
|
|
if (prevent_scrolling)
|
|
|
|
{
|
|
|
|
m_buffer.erase({0,0}, m_buffer.next({0,0}));
|
|
|
|
// in the other case, the buffer will have automatically
|
|
|
|
// inserted a \n to guarantee its invariant.
|
|
|
|
if (data[count-1] == '\n')
|
|
|
|
m_buffer.insert(m_buffer.end_coord(), "\n");
|
|
|
|
}
|
2019-12-28 00:32:15 +01:00
|
|
|
}
|
2020-03-14 02:58:22 +01:00
|
|
|
while (++loop < max_loop and fd_readable(fifo));
|
2017-01-25 14:55:20 +01:00
|
|
|
}
|
2014-05-02 19:58:04 +02:00
|
|
|
|
2019-12-28 00:32:15 +01:00
|
|
|
if (insert_coord != m_buffer.back_coord())
|
|
|
|
m_buffer.run_hook_in_own_context(
|
|
|
|
Hook::BufReadFifo,
|
|
|
|
selection_to_string(ColumnType::Byte, m_buffer, {insert_coord, m_buffer.back_coord()}));
|
2014-05-02 19:58:04 +02:00
|
|
|
|
2019-12-28 00:32:15 +01:00
|
|
|
if (closed)
|
|
|
|
m_buffer.values().erase(fifo_watcher_id); // will delete this
|
2019-06-06 23:21:30 +02:00
|
|
|
}
|
|
|
|
|
2019-12-28 00:32:15 +01:00
|
|
|
Buffer& m_buffer;
|
|
|
|
bool m_scroll;
|
|
|
|
};
|
2014-04-29 22:37:11 +02:00
|
|
|
|
2019-12-28 00:32:15 +01:00
|
|
|
buffer->values()[fifo_watcher_id] = Value(std::make_unique<FifoWatcher>(fd, *buffer, scroll));
|
2017-03-08 20:33:25 +01:00
|
|
|
buffer->flags() = flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo;
|
2018-10-22 23:15:53 +02:00
|
|
|
buffer->run_hook_in_own_context(Hook::BufOpenFifo, buffer->name());
|
2014-04-29 22:37:11 +02:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-06-06 12:54:48 +02:00
|
|
|
void write_to_debug_buffer(StringView str)
|
|
|
|
{
|
|
|
|
if (not BufferManager::has_instance())
|
|
|
|
{
|
2016-11-29 00:53:50 +01:00
|
|
|
write(2, str);
|
|
|
|
write(2, "\n");
|
2015-06-06 12:54:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:52:14 +02:00
|
|
|
constexpr StringView debug_buffer_name = "*debug*";
|
2015-11-19 22:33:50 +01:00
|
|
|
// Try to ensure we keep an empty line at the end of the debug buffer
|
|
|
|
// where the user can put its cursor to scroll with new messages
|
|
|
|
const bool eol_back = not str.empty() and str.back() == '\n';
|
2015-06-06 12:54:48 +02:00
|
|
|
if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name))
|
2018-02-11 03:06:19 +01:00
|
|
|
{
|
|
|
|
buffer->flags() &= ~Buffer::Flags::ReadOnly;
|
|
|
|
auto restore = on_scope_end([buffer] { buffer->flags() |= Buffer::Flags::ReadOnly; });
|
|
|
|
|
2016-03-16 14:59:30 +01:00
|
|
|
buffer->insert(buffer->back_coord(), eol_back ? str : str + "\n");
|
2018-02-11 03:06:19 +01:00
|
|
|
}
|
2015-06-06 12:54:48 +02:00
|
|
|
else
|
|
|
|
{
|
2015-11-19 22:33:50 +01:00
|
|
|
String line = str + (eol_back ? "\n" : "\n\n");
|
2016-05-14 09:33:50 +02:00
|
|
|
BufferManager::instance().create_buffer(
|
2018-02-11 03:06:19 +01:00
|
|
|
debug_buffer_name.str(), Buffer::Flags::NoUndo | Buffer::Flags::Debug | Buffer::Flags::ReadOnly,
|
2016-05-14 09:33:50 +02:00
|
|
|
line, InvalidTime);
|
2015-06-06 12:54:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 10:12:52 +01:00
|
|
|
|
|
|
|
auto to_string(Buffer::HistoryId id)
|
2018-12-31 18:40:49 +01:00
|
|
|
{
|
2020-01-09 10:12:52 +01:00
|
|
|
using Result = decltype(to_string(size_t{}));
|
2020-01-02 11:27:16 +01:00
|
|
|
if (id == Buffer::HistoryId::Invalid)
|
2020-01-09 10:12:52 +01:00
|
|
|
return Result{1, "-"};
|
2020-01-02 11:27:16 +01:00
|
|
|
return to_string(static_cast<size_t>(id));
|
2018-12-31 18:40:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
static String modification_as_string(const Buffer::Modification& modification)
|
2018-12-31 18:40:49 +01:00
|
|
|
{
|
2020-03-01 03:28:06 +01:00
|
|
|
return format("{}{}.{}|{}",
|
|
|
|
modification.type == Buffer::Modification::Type::Insert ? '+' : '-',
|
|
|
|
modification.coord.line, modification.coord.column,
|
|
|
|
modification.content->strview());
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> history_as_strings(const Vector<Buffer::HistoryNode>& history)
|
|
|
|
{
|
|
|
|
Vector<String> res;
|
|
|
|
for (auto& node : history)
|
|
|
|
{
|
2018-12-31 18:40:49 +01:00
|
|
|
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(node.committed.time_since_epoch());
|
2020-03-01 03:28:06 +01:00
|
|
|
res.push_back(to_string(node.parent));
|
|
|
|
res.push_back(to_string(seconds.count()));
|
|
|
|
res.push_back(to_string(node.redo_child));
|
|
|
|
for (auto& modification : node.undo_group)
|
|
|
|
res.push_back(modification_as_string(modification));
|
2018-12-31 18:40:49 +01:00
|
|
|
};
|
2020-03-01 03:28:06 +01:00
|
|
|
return res;
|
2018-12-31 18:40:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
Vector<String> undo_group_as_strings(const Buffer::UndoGroup& undo_group)
|
2018-12-31 18:40:49 +01:00
|
|
|
{
|
2020-03-01 03:28:06 +01:00
|
|
|
Vector<String> res;
|
|
|
|
for (auto& modification : undo_group)
|
|
|
|
res.push_back(modification_as_string(modification));
|
|
|
|
return res;
|
2018-12-31 18:40:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 07:21:49 +02:00
|
|
|
String generate_buffer_name(StringView pattern)
|
|
|
|
{
|
|
|
|
auto& buffer_manager = BufferManager::instance();
|
|
|
|
for (int i = 0; true; ++i)
|
|
|
|
{
|
|
|
|
String name = format(pattern, i);
|
|
|
|
if (buffer_manager.get_buffer_ifp(name) == nullptr)
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
}
|