2014-04-28 20:48:23 +02:00
|
|
|
#include "buffer_utils.hh"
|
|
|
|
|
2014-04-29 22:37:11 +02:00
|
|
|
#include "event_manager.hh"
|
|
|
|
|
2014-05-15 20:11:59 +02:00
|
|
|
#include <sys/select.h>
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
CharCount get_column(const Buffer& buffer,
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCount tabstop, ByteCoord coord)
|
2014-04-28 20:48:23 +02:00
|
|
|
{
|
|
|
|
auto& line = buffer[coord.line];
|
|
|
|
auto col = 0_char;
|
|
|
|
for (auto it = line.begin();
|
|
|
|
it != line.end() and coord.column > (int)(it - line.begin());
|
|
|
|
it = utf8::next(it))
|
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
col = (col / tabstop + 1) * tabstop;
|
|
|
|
else
|
|
|
|
++col;
|
|
|
|
}
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:58:04 +02:00
|
|
|
Buffer* create_fifo_buffer(String name, int fd, bool scroll)
|
2014-04-29 22:37:11 +02:00
|
|
|
{
|
|
|
|
Buffer* buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
|
|
|
|
|
2014-05-02 19:58:04 +02:00
|
|
|
auto watcher = new FDWatcher(fd, [buffer, scroll](FDWatcher& watcher) {
|
2014-05-15 20:11:59 +02:00
|
|
|
constexpr size_t buffer_size = 2048;
|
2014-04-29 22:37:11 +02:00
|
|
|
char data[buffer_size];
|
2014-05-15 20:11:59 +02: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 19:58:04 +02:00
|
|
|
|
2014-05-15 20:11:59 +02:00
|
|
|
bool prevent_scrolling = pos == buffer->begin() and not scroll;
|
|
|
|
if (prevent_scrolling)
|
|
|
|
++pos;
|
2014-05-02 19:58:04 +02:00
|
|
|
|
2014-05-15 20:11:59 +02:00
|
|
|
buffer->insert(pos, count > 0 ? String(data, data+count)
|
|
|
|
: "*** kak: fifo closed ***\n");
|
2014-05-02 19:58:04 +02:00
|
|
|
|
2014-05-15 20:11:59 +02:00
|
|
|
if (prevent_scrolling)
|
|
|
|
{
|
|
|
|
buffer->erase(buffer->begin(), buffer->begin()+1);
|
|
|
|
buffer->insert(buffer->end(), "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fifo, &rfds);
|
2014-05-02 19:58:04 +02:00
|
|
|
}
|
2014-05-15 20:11:59 +02:00
|
|
|
while (count > 0 and select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1);
|
2014-05-02 19:58:04 +02:00
|
|
|
|
2014-04-29 22:37:11 +02:00
|
|
|
if (count <= 0)
|
|
|
|
{
|
|
|
|
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
|
|
|
|
buffer->flags() &= ~Buffer::Flags::Fifo;
|
|
|
|
buffer->flags() &= ~Buffer::Flags::NoUndo;
|
2014-05-15 20:11:59 +02:00
|
|
|
close(fifo);
|
2014-06-16 20:49:58 +02:00
|
|
|
buffer->run_hook_in_own_context("BufCloseFifo", "");
|
2014-04-29 22:37:11 +02:00
|
|
|
delete &watcher;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
buffer->hooks().add_hook("BufClose", "",
|
|
|
|
[buffer, watcher](const String&, const Context&) {
|
|
|
|
// Check if fifo is still alive, else watcher is already dead
|
|
|
|
if (buffer->flags() & Buffer::Flags::Fifo)
|
|
|
|
{
|
|
|
|
close(watcher->fd());
|
2014-06-16 20:49:58 +02:00
|
|
|
buffer->run_hook_in_own_context("BufCloseFifo", "");
|
2014-04-29 22:37:11 +02:00
|
|
|
delete watcher;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
}
|