0
votes

I am currently trying to make a server client architecture for any program using pipes as its stdin and stdout.

I do have the whole server-thing working, and it's communicating through named pipes(fifo). What I am trying to do is to read the content of the stdout pipe into a vim buffer, constantly (asynchronously), and I am looking for the most elegant solution to this problem.

Say I have a program, python, communicating through 2(3 if you account stderr but it's not relevant to finding the solution) pipes, namely PIPE_IN as its stdin, and PIPE_OUT as its stdout.

I can write to the pipe easily from vim (with :w), how would I however read from PIPE_OUT into a vim or neovim buffer?

This is not supposed to be portable to non-UNIX systems, however I would like to keep compatibility with both vim and neovim as much as possible. What would be your recommendations to do so? Should I use the vimscript language for this? Is using vim's command-line mode better in that case? What other, probably more elegant solutions, am I missing?

Thanks in advance!

NB: The server project's code can be found at Soulthym/pyper on GitHub for testing purposes.

1
Tried :r /path/to/fifo? Doesn't work? Maybe :r! cat /path/to/fifo? - phd
:r! cat /path/to/fifo seems to somewhat function under neovim, although it is synchronous and freezes vim until I Ctrl+C the command - Soulthym

1 Answers

2
votes

NeoVim and modern Vim support channels which can be used for asynchronous communication with external components.

In particular, NeoVim supports a sockconnect() which supports named pipes:

Connect a socket to an address. If {mode} is "pipe" then {address} should be the path of a named pipe. [...] Returns a channel ID.

When creating a channel, you can attach a callback to get notifications when there's data to read. You also have functions to poll a channel to see if there's data available.

(An aside, but consider using Unix-domain sockets rather than pipes, they're much more featureful. They're bidirectional, so you don't need multiple pairs of them. Also you can use them to implement servers that can listen and accept multiple connections at the same address.)