rework fifo handling, use real fifos in {make,grep}.kak, update README

This commit is contained in:
Maxime Coste 2012-09-12 19:54:46 +02:00
parent a467d73115
commit f6d2533cae
5 changed files with 23 additions and 12 deletions

View File

@ -343,11 +343,16 @@ the +edit+ command can take a -fifo parameter:
:edit -fifo <filename> <buffername> :edit -fifo <filename> <buffername>
----------------------------------- -----------------------------------
in this case, a buffer named +<buffername>+ is created which reads its content in this case, a buffer named +<buffername>+ is created which reads its content
from +<filename>+. When filename is appended to, the buffer is automatically from fifo +<filename>+. When the fifo is written to, the buffer is automatically
updated. updated.
This is very useful for running some commands asynchronously while displaying 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. 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 Kakrc
----- -----

View File

@ -247,11 +247,15 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
EventManager::instance().watch(fd, [buffer, &context](int fd) { EventManager::instance().watch(fd, [buffer, &context](int fd) {
char data[512]; char data[512];
ssize_t count = read(fd, 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)); close(fd);
buffer->reset_undo_data(); EventManager::instance().unwatch(fd);
context.draw_ifn();
} }
}); });

View File

@ -36,7 +36,7 @@ void EventManager::handle_next_events()
{ {
for (size_t i = 0; i < m_events.size(); ++i) 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); m_handlers[i](m_events[i].fd);
} }
} }

View File

@ -1,12 +1,13 @@
def -shell-params -file-completion \ def -shell-params -file-completion \
grep %{ echo grep in progress, please wait...; %sh{ grep %{ echo grep in progress, please wait...; %sh{
output=$(mktemp -t kak-grep.XXXXXXXX) output=$(mktemp -t -d kak-grep.XXXXXXXX)/fifo
grep -PHn $@ >& ${output} < /dev/null & mkfifo ${output}
( grep -PHn "$@" >& ${output} ) >& /dev/null < /dev/null &
echo "echo echo "echo
try %{ db *grep* } catch %{ } try %{ db *grep* } catch %{ }
edit -fifo ${output} *grep* edit -fifo ${output} *grep*
setb filetype grep setb filetype grep
hook buffer BufClose .* %{ %sh{ rm ${output} } }" hook buffer BufClose .* %{ %sh{ rm -r $(dirname ${output}) } }"
}} }}
hook global WinSetOption filetype=grep %{ hook global WinSetOption filetype=grep %{

View File

@ -1,11 +1,12 @@
def -shell-params make %{ echo make in progress, please wait...; %sh{ def -shell-params make %{ echo make in progress, please wait...; %sh{
output=$(mktemp -t kak-make.XXXXXXXX) output=$(mktemp -t -d kak-make.XXXXXXXX)/fifo
make $@ >& ${output} < /dev/null & mkfifo ${output}
( make $@ >& ${output} ) >& /dev/null < /dev/null &
echo "echo echo "echo
try %{ db *make* } catch %{ } try %{ db *make* } catch %{ }
edit -fifo ${output} *make* edit -fifo ${output} *make*
setb filetype make setb filetype make
hook buffer BufClose .* %{ %sh{ rm ${output} } }" hook buffer BufClose .* %{ %sh{ rm -r $(dirname ${output}) } }"
}} }}
hook global WinSetOption filetype=make %{ hook global WinSetOption filetype=make %{