Change BufReadFifo hook param to contain the inserted range

the buffer name was not a very interesting information, whereas
the buffer range allows a hook to run only on the appended text
instead of all the buffer.
This commit is contained in:
Maxime Coste 2018-11-14 17:52:57 +11:00
parent 021ba55b38
commit 9a68a2d3af
2 changed files with 12 additions and 5 deletions

View File

@ -131,9 +131,10 @@ name. Hooks with no description will always use an empty string.
*BufOpenFifo* `buffer name`::
executed when a buffer opens a fifo
*BufReadFifo* `buffer name`::
*BufReadFifo* `<start line>.<start column>,<end line>.<end column>`::
executed after some data has been read from a fifo and inserted in
the buffer
the buffer. The hook param contains the range of text that was just
inserted, in a format compatible with the `select` command.
*BufCloseFifo*::
executed when a fifo buffer closes its fifo file descriptor either

View File

@ -3,6 +3,7 @@
#include "buffer_manager.hh"
#include "event_manager.hh"
#include "file.hh"
#include "selection.hh"
#include <unistd.h>
@ -127,8 +128,10 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
// 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;
constexpr size_t max_loop = 16;
size_t loop = 0;
char data[buffer_size];
BufferCoord insert_coord;
const int fifo = watcher.fd();
do
{
@ -144,6 +147,8 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
if (prevent_scrolling)
pos = buffer->next(pos);
if (loop == 0)
insert_coord = pos;
buffer->insert(pos, StringView(data, data+count));
if (prevent_scrolling)
@ -155,9 +160,10 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
buffer->insert(buffer->end_coord(), "\n");
}
}
while (--loops and fd_readable(fifo));
while (++loop < max_loop and fd_readable(fifo));
buffer->run_hook_in_own_context(Hook::BufReadFifo, buffer->name());
buffer->run_hook_in_own_context(Hook::BufReadFifo,
selection_to_string({insert_coord, buffer->back_coord()}));
}), std::move(watcher_deleter));
buffer->values()[fifo_watcher_id] = Value(std::move(watcher));