2
0
mirror of https://github.com/mhinz/neovim-remote synced 2024-11-11 13:10:34 +00:00
neovim-remote/nvr

153 lines
5.5 KiB
Plaintext
Raw Normal View History

2015-12-03 11:11:28 +00:00
#!/usr/bin/env python3
"""
Copyright (c) 2015 Marco Hinz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import sys
import os
import subprocess
import argparse
from neovim import attach
2015-12-04 12:50:12 +00:00
class Neovim():
"""Thin wrapper around nvim.attach() for lazy attaching.
This helps handling the silent and non-silent arguments.
"""
def __init__(self, sockpath):
self.sockpath = sockpath
self.server = None
def attached(self, silent=False):
if self.server is not None:
return True
try:
self.server = attach('socket', path=self.sockpath)
return True
except FileNotFoundError:
if silent:
return False
else:
2015-12-04 14:45:59 +00:00
print("Can't find unix socket {}. Export $NVIM_LISTEN_ADDRESS or use --servername.".format(self.sockpath))
2015-12-04 12:50:12 +00:00
sys.exit(1)
2015-12-03 15:39:02 +00:00
def parse_args():
usage = '{} [arguments]'.format(sys.argv[0])
2015-12-16 14:43:05 +00:00
desc = 'Helper tool for nvim that provides --remote and friends.'
2015-12-03 15:39:02 +00:00
epilog = 'Happy hacking!'
parser = argparse.ArgumentParser(usage=usage, description=desc, epilog=epilog)
parser.add_argument('--remote',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<file>',
help='open file in new buffer [ASYNC]')
2015-12-04 12:50:12 +00:00
parser.add_argument('--remote-wait',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<file>',
help='as --remote [SYNC]')
2015-12-04 12:50:12 +00:00
parser.add_argument('--remote-silent',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<file>',
help="as --remote, but don't throw error if no server is found [ASYNC]")
2015-12-03 15:39:02 +00:00
parser.add_argument('--remote-wait-silent',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<file>',
help="as --remote, but don't throw error if no server is found [SYNC]")
2015-12-03 15:39:02 +00:00
parser.add_argument('--remote-tab',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<file>',
help='open file in new tab [SYNC]')
2015-12-03 15:39:02 +00:00
parser.add_argument('--remote-send',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<keys>',
help='send keys to server [SYNC]')
2015-12-03 15:39:02 +00:00
parser.add_argument('--remote-expr',
2015-12-04 14:45:59 +00:00
action='append',
metavar='<expr>',
help='evaluate expression and print result [SYNC]')
parser.add_argument('--servername',
metavar='<sock>',
help='path to unix socket (overrides $NVIM_LISTEN_ADDRESS)')
2015-12-03 15:39:02 +00:00
return parser.parse_known_args()
2015-12-04 12:50:12 +00:00
2015-12-04 16:39:08 +00:00
def run_nvim(args=[]):
try:
# Run interactive shell in case 'nvim' is an alias.
subprocess.run([os.environ.get('SHELL'), '-ic', 'nvim ' + ' '.join(args)])
except FileNotFoundError:
print("Not in $PATH: nvim")
sys.exit(1)
2015-12-03 11:11:28 +00:00
def main():
2015-12-03 15:39:02 +00:00
args, unused = parse_args()
2015-12-04 14:56:52 +00:00
sockpath = os.environ.get('NVIM_LISTEN_ADDRESS')
2015-12-04 14:45:59 +00:00
if args.servername:
sockpath = args.servername
2015-12-04 16:39:08 +00:00
if len(list(filter(lambda x: x is not None, vars(args).values()))) == 1:
os.environ['NVIM_LISTEN_ADDRESS'] = sockpath
2015-12-04 16:39:08 +00:00
run_nvim()
2015-12-04 14:45:59 +00:00
else:
if sockpath is None:
sockpath = '/tmp/nvimsocket'
2015-12-03 11:11:28 +00:00
2015-12-04 12:50:12 +00:00
n = Neovim(sockpath)
2015-12-03 11:11:28 +00:00
2015-12-04 12:50:12 +00:00
if args.remote_silent and n.attached(silent=True):
for fname in args.remote_silent:
n.server.command('edit {}'.format(fname.replace(" ", "\ ")), async=True)
if args.remote_wait_silent and n.attached(silent=True):
for fname in args.remote_wait_silent:
n.server.command('edit {}'.format(fname.replace(" ", "\ ")))
if args.remote and n.attached():
2015-12-03 11:11:28 +00:00
for fname in args.remote:
2015-12-04 12:50:12 +00:00
n.server.command('edit {}'.format(fname.replace(" ", "\ ")), async=True)
if args.remote_wait and n.attached():
2015-12-03 12:44:33 +00:00
for fname in args.remote_wait:
2015-12-04 12:50:12 +00:00
n.server.command('edit {}'.format(fname.replace(" ", "\ ")))
if args.remote_tab and n.attached():
2015-12-03 11:29:35 +00:00
for fname in args.remote_tab:
2015-12-04 12:50:12 +00:00
n.server.command('tabedit {}'.format(fname.replace(" ", "\ ")))
if args.remote_send and n.attached():
2015-12-03 12:47:03 +00:00
for keys in args.remote_send:
2015-12-04 12:50:12 +00:00
n.server.input(keys)
if args.remote_expr and n.attached():
2015-12-03 11:21:40 +00:00
for expr in args.remote_expr:
result = n.server.eval(expr)
if type(result) is bytes:
print(result.decode())
elif type(result) is list:
print(list(map(lambda x: x.decode() if type(x) is bytes else x, result)))
elif type(result) is dict:
print({ (k.decode() if type(k) is bytes else k): v for (k,v) in result.items() })
2015-12-03 11:21:40 +00:00
print(*map(lambda x: 'Unknown argument: '+x, unused), sep='\n')
2015-12-03 11:11:28 +00:00
if __name__ == '__main__':
main()