-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnvim-inner
executable file
·98 lines (84 loc) · 2.83 KB
/
nvim-inner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
"""Edit a file in the host nvim instance."""
import os
import sys
try:
from pynvim import attach
except ImportError:
try:
from neovim import attach
except ImportError:
print("WARNING: don't have nvim python module, launching without it")
attach = None
args = sys.argv[1:]
if not args or len(args) > 1:
print "Usage: {} <filename>".format(sys.argv[0])
sys.exit(1)
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file) and os.path.abspath(exe_file) != os.path.abspath(__file__):
return exe_file
return None
addr = os.environ.get("NVIM_LISTEN_ADDRESS", None)
if not addr or not args:
if addr:
print("Launching from inside nvim, but no pynvim available - cancelling")
sys.exit()
try:
os.execvp('nvim', ["nvim", "-p"] + args)
except OSError:
print("Missing nvim, trying vim")
os.execvp(which('vim'), ["vim", "-p"] + args)
nvim = attach("socket", path=addr)
def fullpath(*args):
_p = os.path
return _p.abspath(_p.normpath(_p.expanduser(_p.join(*args))))
del _p
def setup():
global termbuf
nvim.input('<c-\\><c-n>') # exit terminal mode
nvim.command
chid = nvim.channel_id
termbuf = nvim.eval('bufnr("%")')
nvim.vars['file_to_edit'] = fullpath(args[0])
nvim.command('exe "below new ".g:file_to_edit')
# nvim.command('noremap <buffer> :wqa :wq')
# nvim.command('noremap <buffer> :qa :q')
# nvim.command('noremap <buffer> :qa! :q!')
# nvim.command('noremap <buffer> :wqa! :wq!')
nvim.command('setlocal bufhidden=wipe')
nvim.command('augroup TERMEDIT')
nvim.command('highlight StatusLine ctermfg=red')
nvim.command('au BufUnload <buffer> call rpcnotify({0}, "n")'.format(chid))
nvim.command('au BufEnter <buffer> highlight StatusLine ctermfg=red')
nvim.command('au BufLeave <buffer> highlight StatusLine ctermfg=blue')
nvim.command('augroup END')
def exit(*a, **kw):
try:
nvim.vars['file_to_edit'] = None
swb = nvim.eval("&swb")
nvim.command('set swb=useopen,usetab')
nvim.command('augroup TERMEDIT')
nvim.command('au!')
nvim.command('augroup END')
nvim.command('sb %s' % termbuf)
nvim.command('set swb=' + (swb or ''))
nvim.input('i')
nvim.stop_loop()
except:
import traceback
traceback.print_exc()
sys.stdout.flush()
sys.stderr.flush()
sys.exit(1)
nvim.run_loop(exit, exit, setup)