Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active June 1, 2017 22:57
Show Gist options
  • Save louisswarren/21ff57fad7fb7e2a4f1a71814c375a34 to your computer and use it in GitHub Desktop.
Save louisswarren/21ff57fad7fb7e2a4f1a71814c375a34 to your computer and use it in GitHub Desktop.
A clarinet is a pipe with a reed; clarinet.py reads from a named pipe.
#!/usr/bin/env python3
import os
import sys
import textwrap
import time
def usage():
print(textwrap.dedent("""
Usage: {} <pipepath>
Create a named pipe, send its contents to stdout forever.
Supplied pipe path must not exist.
Warning: supplied pipe path will be deleted after a keyboard interrupt.
""".format(sys.argv[0])).strip())
def read_loop(f):
while True:
data = f.read()
if data:
sys.stdout.write(data)
sys.stdout.flush()
else:
time.sleep(0.1)
def make_pipe(pipe_fname):
try:
os.mkfifo(pipe_fname)
except OSError as e:
print("Failed to create named pipe {}".format(pipe_fname))
print("Error:", e)
sys.exit(1)
def main(pipe_fname):
make_pipe(pipe_fname)
try:
with open(pipe_fname) as fifo:
read_loop(fifo)
except KeyboardInterrupt:
print()
finally:
os.unlink(pipe_fname)
if __name__ == '__main__':
if len(sys.argv) == 2:
main(sys.argv[1])
else:
usage()
@louisswarren
Copy link
Author

louisswarren commented Jun 1, 2017

Use case:

In ~/.vim/ftplugin/scheme.vim:

" Execute the file (or the file up to the current line)
nnoremap <f10> :w! ~/minlog/.clarinet<cr>
nnoremap <s-f10> :1,.w! ~/minlog/.clarinet<cr>
" Execute the current line(s)
nnoremap <f9> :.w! ~/minlog/.clarinet<cr>
inoremap <f9> <esc>:.w! ~/minlog/.clarinet<cr>a
vnoremap <f9> :'<,'>w! ~/minlog/.clarinet<cr>

Then start minlog with (assuming your scheme implementation is guile)

clarinet ~/minlog/.clarinet | guile -l ~/minlog/init.scm

Vim will write the commands to execute into the named pipe, and clarinet will pipe them into guile. Minlog without emacs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment