diff --git a/README.asciidoc b/README.asciidoc index ff5dc073..32dd7f09 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -343,11 +343,16 @@ the +edit+ command can take a -fifo parameter: :edit -fifo ----------------------------------- in this case, a buffer named ++ is created which reads its content -from ++. When filename is appended to, the buffer is automatically +from fifo ++. When the fifo is written to, the buffer is automatically updated. + This is very useful for running some commands asynchronously while displaying their result in a buffer. See rc/make.kak and rc/grep.kak for examples. +When the buffer is deleted, the fifo will be closed, so any program writing +to it will receive SIGPIPE. This is usefull as it permits to stop the writing +program when the buffer is deleted. + Kakrc ----- diff --git a/src/commands.cc b/src/commands.cc index 2ef70a17..90dfa394 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -247,11 +247,15 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context) EventManager::instance().watch(fd, [buffer, &context](int fd) { char data[512]; ssize_t count = read(fd, data, 512); - if (count > 0) + buffer->insert(buffer->end()-1, + count > 0 ? String(data, data+count) + : "*** kak: fifo closed ***\n"); + buffer->reset_undo_data(); + context.draw_ifn(); + if (count <= 0) { - buffer->insert(buffer->end()-1, String(data, data + count)); - buffer->reset_undo_data(); - context.draw_ifn(); + close(fd); + EventManager::instance().unwatch(fd); } }); diff --git a/src/event_manager.cc b/src/event_manager.cc index 2a8df29c..3ac0ce3a 100644 --- a/src/event_manager.cc +++ b/src/event_manager.cc @@ -36,7 +36,7 @@ void EventManager::handle_next_events() { for (size_t i = 0; i < m_events.size(); ++i) { - if (m_events[i].revents & POLLIN) + if (m_events[i].revents) m_handlers[i](m_events[i].fd); } } diff --git a/src/rc/grep.kak b/src/rc/grep.kak index a1a25874..e5ef5230 100644 --- a/src/rc/grep.kak +++ b/src/rc/grep.kak @@ -1,12 +1,13 @@ def -shell-params -file-completion \ grep %{ echo grep in progress, please wait...; %sh{ - output=$(mktemp -t kak-grep.XXXXXXXX) - grep -PHn $@ >& ${output} < /dev/null & + output=$(mktemp -t -d kak-grep.XXXXXXXX)/fifo + mkfifo ${output} + ( grep -PHn "$@" >& ${output} ) >& /dev/null < /dev/null & echo "echo try %{ db *grep* } catch %{ } edit -fifo ${output} *grep* setb filetype grep - hook buffer BufClose .* %{ %sh{ rm ${output} } }" + hook buffer BufClose .* %{ %sh{ rm -r $(dirname ${output}) } }" }} hook global WinSetOption filetype=grep %{ diff --git a/src/rc/make.kak b/src/rc/make.kak index 346c70f0..f64cea01 100644 --- a/src/rc/make.kak +++ b/src/rc/make.kak @@ -1,11 +1,12 @@ def -shell-params make %{ echo make in progress, please wait...; %sh{ - output=$(mktemp -t kak-make.XXXXXXXX) - make $@ >& ${output} < /dev/null & + output=$(mktemp -t -d kak-make.XXXXXXXX)/fifo + mkfifo ${output} + ( make $@ >& ${output} ) >& /dev/null < /dev/null & echo "echo try %{ db *make* } catch %{ } edit -fifo ${output} *make* setb filetype make - hook buffer BufClose .* %{ %sh{ rm ${output} } }" + hook buffer BufClose .* %{ %sh{ rm -r $(dirname ${output}) } }" }} hook global WinSetOption filetype=make %{