summaryrefslogtreecommitdiff
path: root/nvimctl.py
diff options
context:
space:
mode:
Diffstat (limited to 'nvimctl.py')
-rwxr-xr-xnvimctl.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/nvimctl.py b/nvimctl.py
new file mode 100755
index 0000000..42e083a
--- /dev/null
+++ b/nvimctl.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+import neovim
+
+try:
+ nvim_socket = os.environ["NVIM"]
+except KeyError:
+ sys.stderr.write("Not inside Neovim terminal")
+ sys.exit(1)
+
+nvim = neovim.attach('socket', path=nvim_socket)
+
+
+class GetReg:
+ def name(self):
+ return "getreg"
+
+ def usage(self):
+ return "[register]"
+
+ def descr(self):
+ return "Prints the contents of a Neovim register to stdout."
+
+ def run(self, args):
+ register = args[0]
+ print(nvim.funcs.getreg(register))
+
+
+class PutReg:
+ def name(self):
+ return "setreg"
+
+ def usage(self):
+ return "[register]"
+
+ def descr(self):
+ return "Puts the contents of stdin into a Neovim register."
+
+ def run(self, args):
+ register = args[0]
+ lines = sys.stdin.read().splitlines()
+
+ if len(lines) == 1:
+ lines = lines[0]
+
+ nvim.funcs.setreg(register, lines)
+
+
+def print_help():
+ sys.stderr.write("USAGE %s <command> [args...]\n" % sys.argv[0])
+ sys.stderr.write("commands:\n")
+ for c in commands:
+ sys.stderr.write("\t%s: %s\n" % (c.name(), c.descr()))
+
+
+commands = [GetReg(), PutReg()]
+
+if len(sys.argv) < 2:
+ print_help()
+ sys.exit(1)
+
+cmd = sys.argv[1]
+args = sys.argv[2:]
+for c in commands:
+ if cmd == c.name():
+ sys.exit(c.run(args) or 0)
+
+sys.stderr.write("Unknown command %s\n" % cmd)
+print_help()