From 9a68a2d3afa5d44961e04d7c4e1e0a0b568f4dee Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 14 Nov 2018 17:52:57 +1100 Subject: [PATCH] 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. --- doc/pages/hooks.asciidoc | 5 +++-- src/buffer_utils.cc | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/doc/pages/hooks.asciidoc b/doc/pages/hooks.asciidoc index 3ddd5553..0fe6a566 100644 --- a/doc/pages/hooks.asciidoc +++ b/doc/pages/hooks.asciidoc @@ -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* `.,.`:: 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 diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index c2c75da0..bb0669ff 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -3,6 +3,7 @@ #include "buffer_manager.hh" #include "event_manager.hh" #include "file.hh" +#include "selection.hh" #include @@ -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));