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
|
|
|
|
2016-11-29 02:53:50 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-18 14:45:27 +03:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define st_mtim st_mtimespec
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-04-28 22:48:23 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2016-09-22 22:36:26 +03:00
|
|
|
ColumnCount get_column(const Buffer& buffer,
|
|
|
|
ColumnCount tabstop, BufferCoord coord)
|
2014-04-28 22:48:23 +04:00
|
|
|
{
|
2014-10-03 16:39:13 +04:00
|
|
|
auto line = buffer[coord.line];
|
2016-09-22 22:36:26 +03:00
|
|
|
auto col = 0_col;
|
2014-04-28 22:48:23 +04:00
|
|
|
for (auto it = line.begin();
|
2016-09-24 20:52:54 +03:00
|
|
|
it != line.end() and coord.column > (int)(it - line.begin()); )
|
2014-04-28 22:48:23 +04:00
|
|
|
{
|
|
|
|
if (*it == '\t')
|
2016-09-24 20:52:54 +03:00
|
|
|
{
|
2014-04-28 22:48:23 +04:00
|
|
|
col = (col / tabstop + 1) * tabstop;
|
2016-09-24 20:52:54 +03:00
|
|
|
++it;
|
|
|
|
}
|
2014-04-28 22:48:23 +04:00
|
|
|
else
|
2016-09-25 12:55:57 +03:00
|
|
|
col += codepoint_width(utf8::read_codepoint(it, line.end()));
|
2014-04-28 22:48:23 +04:00
|
|
|
}
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
2016-09-22 22:36:26 +03:00
|
|
|
ByteCount get_byte_to_column(const Buffer& buffer, ColumnCount tabstop, DisplayCoord coord)
|
2015-02-25 16:40:19 +03:00
|
|
|
{
|
|
|
|
auto line = buffer[coord.line];
|
2016-09-22 22:36:26 +03:00
|
|
|
auto col = 0_col;
|
2015-02-25 16:40:19 +03: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 20:52:54 +03:00
|
|
|
++it;
|
2015-02-25 16:40:19 +03:00
|
|
|
}
|
|
|
|
else
|
2016-09-24 20:52:54 +03:00
|
|
|
{
|
|
|
|
auto next = it;
|
2016-09-25 12:55:57 +03:00
|
|
|
col += codepoint_width(utf8::read_codepoint(next, line.end()));
|
2016-09-24 20:52:54 +03:00
|
|
|
if (col > coord.column) // the target column was in the char
|
|
|
|
break;
|
|
|
|
it = next;
|
|
|
|
}
|
2015-02-25 16:40:19 +03:00
|
|
|
}
|
|
|
|
return (int)(it - line.begin());
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:59:33 +03:00
|
|
|
Buffer* open_file_buffer(StringView filename, Buffer::Flags flags)
|
2015-10-16 03:33:17 +03:00
|
|
|
{
|
2015-10-16 15:58:56 +03:00
|
|
|
MappedFile file_data{filename};
|
2016-05-14 10:33:50 +03:00
|
|
|
return BufferManager::instance().create_buffer(
|
2016-11-14 16:59:33 +03:00
|
|
|
filename.str(), Buffer::Flags::File | flags, file_data, file_data.st.st_mtim);
|
2015-10-16 03:33:17 +03:00
|
|
|
}
|
2014-08-15 16:21:54 +04:00
|
|
|
|
2016-11-14 16:59:33 +03:00
|
|
|
Buffer* open_or_create_file_buffer(StringView filename, Buffer::Flags flags)
|
2015-10-16 03:33:17 +03:00
|
|
|
{
|
2016-05-14 10:33:50 +03:00
|
|
|
auto& buffer_manager = BufferManager::instance();
|
2015-10-16 15:58:56 +03:00
|
|
|
if (file_exists(filename))
|
2015-10-16 03:33:17 +03:00
|
|
|
{
|
2015-10-16 15:58:56 +03:00
|
|
|
MappedFile file_data{filename};
|
2016-11-14 16:59:33 +03:00
|
|
|
return buffer_manager.create_buffer(filename.str(), Buffer::Flags::File | flags,
|
2016-05-14 10:33:50 +03:00
|
|
|
file_data, file_data.st.st_mtim);
|
2015-10-16 03:33:17 +03:00
|
|
|
}
|
2016-05-14 10:33:50 +03:00
|
|
|
return buffer_manager.create_buffer(
|
|
|
|
filename.str(), Buffer::Flags::File | Buffer::Flags::New,
|
|
|
|
{}, InvalidTime);
|
2015-10-16 15:58:56 +03: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);
|
2015-10-16 03:33:17 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-12-03 16:17:42 +03:00
|
|
|
static ValueId s_fifo_watcher_id = get_free_value_id();
|
2014-04-30 00:37:11 +04:00
|
|
|
|
2016-05-14 10:33:50 +03:00
|
|
|
auto& buffer_manager = BufferManager::instance();
|
|
|
|
Buffer* buffer = buffer_manager.get_buffer_ifp(name);
|
2014-11-04 16:55:56 +03:00
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer->flags() |= Buffer::Flags::NoUndo;
|
2015-10-16 15:52:14 +03:00
|
|
|
buffer->reload({}, InvalidTime);
|
2014-11-04 16:55:56 +03:00
|
|
|
}
|
|
|
|
else
|
2016-05-14 10:33:50 +03:00
|
|
|
buffer = buffer_manager.create_buffer(
|
|
|
|
std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
|
2014-11-04 16:55:56 +03:00
|
|
|
|
|
|
|
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", "");
|
2016-10-23 20:22:42 +03:00
|
|
|
buffer->flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
|
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(
|
2016-11-30 16:59:08 +03:00
|
|
|
new FDWatcher(fd, FdEvents::Read,
|
|
|
|
[buffer, scroll, fifo_watcher_id](FDWatcher& watcher, FdEvents, EventMode mode) {
|
2014-11-25 04:00:18 +03:00
|
|
|
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();
|
|
|
|
do
|
|
|
|
{
|
2017-01-25 16:55:20 +03:00
|
|
|
const ssize_t count = ::read(fifo, data, buffer_size);
|
|
|
|
if (count <= 0)
|
|
|
|
{
|
|
|
|
buffer->values().erase(fifo_watcher_id); // will delete this
|
|
|
|
return;
|
|
|
|
}
|
2014-05-02 21:58:04 +04:00
|
|
|
|
2017-01-25 16:55:20 +03:00
|
|
|
auto pos = buffer->back_coord();
|
2016-09-22 22:36:26 +03:00
|
|
|
const bool prevent_scrolling = pos == BufferCoord{0,0} and not scroll;
|
2014-05-15 22:11:59 +04:00
|
|
|
if (prevent_scrolling)
|
2016-03-16 16:59:30 +03:00
|
|
|
pos = buffer->next(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
|
|
|
|
2017-01-25 16:55:20 +03:00
|
|
|
if (prevent_scrolling)
|
2014-05-15 22:11:59 +04:00
|
|
|
{
|
2016-03-16 16:59:30 +03:00
|
|
|
buffer->erase({0,0}, buffer->next({0,0}));
|
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')
|
2016-03-16 16:59:30 +03:00
|
|
|
buffer->insert(buffer->end_coord(), "\n");
|
2014-05-15 22:11:59 +04:00
|
|
|
}
|
2014-05-02 21:58:04 +04:00
|
|
|
}
|
2017-01-25 16:55:20 +03:00
|
|
|
while (--loops and fd_readable(fifo));
|
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-11-04 16:55:56 +03:00
|
|
|
}), 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())
|
|
|
|
{
|
2016-11-29 02:53:50 +03:00
|
|
|
write(2, str);
|
|
|
|
write(2, "\n");
|
2015-06-06 13:54:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-16 15:52:14 +03:00
|
|
|
constexpr StringView debug_buffer_name = "*debug*";
|
2015-11-20 00:33:50 +03: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 13:54:48 +03:00
|
|
|
if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name))
|
2016-03-16 16:59:30 +03:00
|
|
|
buffer->insert(buffer->back_coord(), eol_back ? str : str + "\n");
|
2015-06-06 13:54:48 +03:00
|
|
|
else
|
|
|
|
{
|
2015-11-20 00:33:50 +03:00
|
|
|
String line = str + (eol_back ? "\n" : "\n\n");
|
2016-05-14 10:33:50 +03:00
|
|
|
BufferManager::instance().create_buffer(
|
|
|
|
debug_buffer_name.str(), Buffer::Flags::NoUndo | Buffer::Flags::Debug,
|
|
|
|
line, InvalidTime);
|
2015-06-06 13:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 22:48:23 +04:00
|
|
|
}
|