Created
December 28, 2025 21:56
-
-
Save TyOverby/76b964b06ad97dd5736717c739690a6e to your computer and use it in GitHub Desktop.
`nvim` launcher python script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import neovim | |
| import os | |
| import sys | |
| args = sys.argv[1:] | |
| wait = 0 | |
| exitcode = 0 | |
| vimexe = "/usr/bin/nvim" | |
| addr_env = 'NVIM' | |
| if addr_env in os.environ: | |
| addr = os.environ[addr_env] | |
| if ":" in addr: | |
| split = addr.split(":") | |
| nvim = neovim.attach('tcp', address=split[0], port=split[1]) | |
| else: | |
| nvim = neovim.attach('socket', path=os.environ[addr_env]) | |
| def open_files(inbetween, on_every): | |
| global wait | |
| first = True | |
| for file in args: | |
| if not first: | |
| inbetween() | |
| first = False | |
| file = os.path.abspath(file) | |
| nvim.command('edit {}'.format(file)) | |
| bvars = nvim.current.buffer.vars | |
| chanid = nvim.channel_id | |
| nvim.command('augroup nvr') | |
| nvim.command('autocmd BufDelete <buffer> silent! call rpcnotify({}, "BufDelete")'.format(chanid)) | |
| nvim.command('autocmd VimLeave * if exists("v:exiting") && v:exiting > 0 | silent! call rpcnotify({}, "Exit", v:exiting) | endif'.format(chanid)) | |
| nvim.command('augroup END') | |
| if 'nvr' in bvars: | |
| if chanid not in bvars['nvr']: | |
| bvars['nvr'] = [chanid] + bvars['nvr'] | |
| else: | |
| bvars['nvr'] = [chanid] | |
| on_every(file) | |
| onclose = lambda: None | |
| if len(args) >= 1 and args[0] == "-d": | |
| args = args[1:] | |
| nvim.command('TabooOpen review') | |
| def inbetween(): | |
| nvim.command('vsplit') | |
| def on_every(filename): | |
| nvim.command('diffthis') | |
| if filename.endswith(".ml.new-tip") or filename.endswith(".ml.old-tip") or filename.endswith(".mli.old-tip") or filename.endswith(".mli.new-tip"): | |
| nvim.command('set filetype=ocaml') | |
| open_files(inbetween, on_every) | |
| onclose = lambda: nvim.command('tabclose') | |
| else: | |
| def inbetween(): | |
| return None | |
| def on_every(filename): | |
| global wait | |
| wait += 1 | |
| return None | |
| open_files(inbetween, on_every) | |
| def notification_cb(msg, args): | |
| global wait | |
| global exitcode | |
| if msg == 'BufDelete': | |
| wait -= 1 | |
| if wait <= 0: | |
| onclose() | |
| nvim.stop_loop() | |
| if len(args) == 0: | |
| exitcode = 0 | |
| else: | |
| exitcode = args[0] | |
| def err_cb(error): | |
| global exitcode | |
| nvim.stop_loop() | |
| exitcode = 1 | |
| nvim.run_loop(None, notification_cb, None, err_cb) | |
| nvim.close() | |
| sys.exit(exitcode) | |
| else: | |
| os.execvp(vimexe, [vimexe, '-u', '~/.config/nvim/init.lua'] + args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment