2014-04-28 22:48:23 +04:00
|
|
|
#include "buffer_utils.hh"
|
|
|
|
|
2014-08-15 16:21:54 +04:00
|
|
|
#include "buffer_manager.hh"
|
2014-04-30 00:37:11 +04:00
|
|
|
#include "event_manager.hh"
|
2015-10-16 03:33:17 +03:00
|
|
|
#include "file.hh"
|
2014-04-30 00:37:11 +04:00
|
|
|
|
2014-10-13 16:12:33 +04:00
|
|
|
#include <unistd.h>
|
2014-05-15 22:11:59 +04:00
|
|
|
#include <sys/select.h>
|
|
|
|
|
2014-04-28 22:48:23 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
CharCount get_column(const Buffer& buffer,
|
2014-05-07 22:51:01 +04:00
|
|
|
CharCount tabstop, ByteCoord coord)
|
2014-04-28 22:48:23 +04:00
|
|
|
{
|
2014-10-03 16:39:13 +04:00
|
|
|
auto line = buffer[coord.line];
|
2014-04-28 22:48:23 +04:00
|
|
|
auto col = 0_char;
|
|
|
|
for (auto it = line.begin();
|
|
|
|
it != line.end() and coord.column > (int)(it - line.begin());
|
2014-07-03 00:14:01 +04:00
|
|
|
it = utf8::next(it, line.end()))
|
2014-04-28 22:48:23 +04:00
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
col = (col / tabstop + 1) * tabstop;
|
|
|
|
else
|
|
|
|
++col;
|
|
|
|
}
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
2015-02-25 16:40:19 +03:00
|
|
|
ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop, CharCoord coord)
|
|
|
|
{
|
|
|
|
auto line = buffer[coord.line];
|
|
|
|
auto col = 0_char;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++col;
|
|
|
|
it = utf8::next(it, line.end());
|
|
|
|
}
|
|
|
|
return (int)(it - line.begin());
|
|
|
|
}
|
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
struct BufferData
|
2014-08-15 16:21:54 +04:00
|
|
|
{
|
2015-10-16 03:33:17 +03:00
|
|
|
BufferLines lines;
|
|
|
|
bool bom = false;
|
|
|
|
bool crlf = false;
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
BufferData(StringView data)
|
2014-08-15 16:21:54 +04:00
|
|
|
{
|
2015-10-16 03:33:17 +03:00
|
|
|
const char* pos = data.begin();
|
|
|
|
if (data.length() >= 3 and
|
|
|
|
data[0_byte] == '\xEF' and data[1_byte] == '\xBB' and data[2_byte] == '\xBF')
|
|
|
|
{
|
|
|
|
bom = true;
|
|
|
|
pos = data.begin() + 3;
|
|
|
|
}
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
while (pos < data.end())
|
|
|
|
{
|
|
|
|
const char* line_end = pos;
|
|
|
|
while (line_end < data.end() and *line_end != '\r' and *line_end != '\n')
|
|
|
|
++line_end;
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
lines.emplace_back(StringData::create({pos, line_end}, '\n'));
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n')
|
|
|
|
{
|
|
|
|
crlf = true;
|
|
|
|
pos = line_end + 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pos = line_end + 1;
|
2014-08-15 16:21:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
void apply_options(Buffer& buffer) const
|
|
|
|
{
|
|
|
|
OptionManager& options = buffer.options();
|
|
|
|
options.get_local_option("eolformat").set<String>(crlf ? "crlf" : "lf");
|
|
|
|
options.get_local_option("BOM").set<String>(bom ? "utf-8" : "no");
|
|
|
|
}
|
|
|
|
};
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
Buffer* create_file_buffer(StringView filename)
|
|
|
|
{
|
|
|
|
if (MappedFile file_data{filename})
|
|
|
|
return create_buffer({ file_data.data, (int)file_data.st.st_size }, filename,
|
|
|
|
Buffer::Flags::File, file_data.st.st_mtim);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
bool reload_file_buffer(Buffer& buffer)
|
|
|
|
{
|
|
|
|
kak_assert(buffer.flags() & Buffer::Flags::File);
|
|
|
|
if (MappedFile file_data{buffer.name()})
|
|
|
|
{
|
|
|
|
reload_buffer(buffer, { file_data.data, (int)file_data.st.st_size }, file_data.st.st_mtim);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer* create_buffer(StringView data, StringView name, Buffer::Flags flags,
|
|
|
|
timespec fs_timestamp)
|
|
|
|
{
|
|
|
|
BufferData buf_data(data);
|
|
|
|
Buffer* buffer = new Buffer{name.str(), flags, std::move(buf_data.lines), fs_timestamp};
|
|
|
|
buf_data.apply_options(*buffer);
|
2014-08-15 16:21:54 +04:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-10-16 03:33:17 +03:00
|
|
|
void reload_buffer(Buffer& buffer, StringView data, timespec fs_timestamp)
|
|
|
|
{
|
|
|
|
BufferData buf_data(data);
|
|
|
|
buffer.reload(std::move(buf_data.lines), fs_timestamp);
|
|
|
|
buf_data.apply_options(buffer);
|
|
|
|
}
|
|
|
|
|
2014-05-02 21:58:04 +04:00
|
|
|
Buffer* create_fifo_buffer(String name, int fd, bool scroll)
|
2014-04-30 00:37:11 +04:00
|
|
|
{
|
2014-11-04 16:55:56 +03:00
|
|
|
static ValueId s_fifo_watcher_id = ValueId::get_free_id();
|
2014-04-30 00:37:11 +04:00
|
|
|
|
2014-11-04 16:55:56 +03:00
|
|
|
Buffer* buffer = BufferManager::instance().get_buffer_ifp(name);
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer->flags() |= Buffer::Flags::NoUndo;
|
2015-09-27 13:55:34 +03:00
|
|
|
buffer->reload({"\n"_ss}, InvalidTime);
|
2014-11-04 16:55:56 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
|
|
|
|
|
|
|
|
auto watcher_deleter = [buffer](FDWatcher* watcher) {
|
|
|
|
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
|
2014-12-03 16:56:02 +03:00
|
|
|
watcher->close_fd();
|
2014-11-04 16:55:56 +03:00
|
|
|
buffer->run_hook_in_own_context("BufCloseFifo", "");
|
|
|
|
buffer->flags() &= ~Buffer::Flags::Fifo;
|
2015-06-03 22:22:07 +03:00
|
|
|
delete watcher;
|
2014-11-04 16:55:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// capture a non static one to silence a warning.
|
|
|
|
ValueId fifo_watcher_id = s_fifo_watcher_id;
|
|
|
|
|
|
|
|
std::unique_ptr<FDWatcher, decltype(watcher_deleter)> watcher(
|
2014-11-25 04:00:18 +03:00
|
|
|
new FDWatcher(fd, [buffer, scroll, fifo_watcher_id](FDWatcher& watcher, EventMode mode) {
|
|
|
|
if (mode != EventMode::Normal)
|
|
|
|
return;
|
|
|
|
|
2015-08-05 01:02:40 +03:00
|
|
|
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
|
|
|
|
|
2014-05-15 22:11:59 +04:00
|
|
|
constexpr size_t buffer_size = 2048;
|
2014-08-27 01:11:23 +04:00
|
|
|
// 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)
|
|
|
|
size_t loops = 16;
|
2014-04-30 00:37:11 +04:00
|
|
|
char data[buffer_size];
|
2014-05-15 22:11:59 +04:00
|
|
|
const int fifo = watcher.fd();
|
|
|
|
timeval tv{ 0, 0 };
|
|
|
|
fd_set rfds;
|
|
|
|
ssize_t count = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
count = read(fifo, data, buffer_size);
|
|
|
|
auto pos = buffer->end()-1;
|
2014-05-02 21:58:04 +04:00
|
|
|
|
2014-05-15 22:11:59 +04:00
|
|
|
bool prevent_scrolling = pos == buffer->begin() and not scroll;
|
|
|
|
if (prevent_scrolling)
|
|
|
|
++pos;
|
2014-05-02 21:58:04 +04:00
|
|
|
|
2015-04-27 18:39:51 +03:00
|
|
|
buffer->insert(pos, StringView(data, data+count));
|
2014-05-02 21:58:04 +04:00
|
|
|
|
2014-06-30 21:56:18 +04:00
|
|
|
if (count > 0 and prevent_scrolling)
|
2014-05-15 22:11:59 +04:00
|
|
|
{
|
|
|
|
buffer->erase(buffer->begin(), buffer->begin()+1);
|
2014-06-30 21:56:18 +04:00
|
|
|
// in the other case, the buffer will have automatically
|
|
|
|
// inserted a \n to guarantee its invariant.
|
|
|
|
if (data[count-1] == '\n')
|
|
|
|
buffer->insert(buffer->end(), "\n");
|
2014-05-15 22:11:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fifo, &rfds);
|
2014-05-02 21:58:04 +04:00
|
|
|
}
|
2014-08-27 01:11:23 +04:00
|
|
|
while (--loops and count > 0 and
|
|
|
|
select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1);
|
2014-05-02 21:58:04 +04:00
|
|
|
|
2015-06-28 13:06:19 +03:00
|
|
|
buffer->run_hook_in_own_context("BufReadFifo", buffer->name());
|
|
|
|
|
2014-04-30 00:37:11 +04:00
|
|
|
if (count <= 0)
|
2014-11-04 16:55:56 +03:00
|
|
|
buffer->values().erase(fifo_watcher_id); // will delete this
|
|
|
|
}), std::move(watcher_deleter));
|
2014-04-30 00:37:11 +04:00
|
|
|
|
2014-11-04 16:55:56 +03:00
|
|
|
buffer->values()[fifo_watcher_id] = Value(std::move(watcher));
|
|
|
|
buffer->flags() = Buffer::Flags::Fifo | Buffer::Flags::NoUndo;
|
2015-05-05 22:23:15 +03:00
|
|
|
buffer->run_hook_in_own_context("BufOpenFifo", buffer->name());
|
2014-04-30 00:37:11 +04:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-06-06 13:54:48 +03:00
|
|
|
void write_to_debug_buffer(StringView str)
|
|
|
|
{
|
|
|
|
if (not BufferManager::has_instance())
|
|
|
|
{
|
|
|
|
write(2, str.data(), (int)str.length());
|
|
|
|
write(2, "\n", 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringView debug_buffer_name = "*debug*";
|
|
|
|
if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name))
|
|
|
|
buffer->insert(buffer->end(), str);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String line = str + ((str.empty() or str.back() != '\n') ? "\n" : "");
|
2015-10-16 03:33:17 +03:00
|
|
|
create_buffer(line, debug_buffer_name, Buffer::Flags::NoUndo, InvalidTime);
|
2015-06-06 13:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 22:48:23 +04:00
|
|
|
}
|