Thanks to visit codestin.com
Credit goes to gist.github.com

Skip to content

Instantly share code, notes, and snippets.

@rf-
Last active December 9, 2024 06:07
Show Gist options
  • Save rf-/33fc88d3071f4254b80e to your computer and use it in GitHub Desktop.
Save rf-/33fc88d3071f4254b80e to your computer and use it in GitHub Desktop.

Revisions

  1. rf- revised this gist Dec 9, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def get_listed_buffers(nvim):
    # is good enough to recognize '-f' and `+11`, which is all this script really
    # needs right now.
    filenames = [
    re.sub(' ', '\ ', os.path.abspath(arg))
    re.sub(' ', '\\ ', os.path.abspath(arg))
    for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    ]

  2. rf- revised this gist Jul 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ filenames = [
    ]

    try:
    nvim_socket = os.environ["NVIM_LISTEN_ADDRESS"]
    nvim_socket = os.environ["NVIM"]
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
  3. rf- revised this gist Jan 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -70,4 +70,4 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
    nvim.command('bdelete! %d' % buffer)
  4. rf- revised this gist Jan 1, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions nvim-client
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env python

    import neovim, os, re, sys, time
    import pynvim, os, re, sys, time

    # Get a list of buffers that haven't been deleted. `nvim.buffers` includes
    # buffers that have had `:bdelete` called on them and aren't in the buffer
    @@ -23,7 +23,7 @@ except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)

    nvim = neovim.attach('socket', path=nvim_socket)
    nvim = pynvim.attach('socket', path=nvim_socket)

    existing_buffers = get_listed_buffers(nvim)

    @@ -70,4 +70,4 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
    nvim.command('bdelete! %d' % buffer)
  5. rf- revised this gist Sep 29, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,6 @@ try:
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
    sys.exit()

    nvim = neovim.attach('socket', path=nvim_socket)

  6. rf- revised this gist Sep 29, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -71,4 +71,4 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
    nvim.command('bdelete! %d' % buffer)
  7. rf- revised this gist Sep 29, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ try:
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
    sys.exit()

    nvim = neovim.attach('socket', path=nvim_socket)

    @@ -70,4 +71,4 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
    nvim.command('bdelete! %d' % buffer)
  8. rf- revised this gist May 23, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,6 @@ try:
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
    sys.exit()

    nvim = neovim.attach('socket', path=nvim_socket)

  9. rf- revised this gist May 23, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ try:
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
    sys.exit()

    nvim = neovim.attach('socket', path=nvim_socket)

    @@ -56,7 +57,7 @@ if '-f' in sys.argv and len(new_buffers) > 0:

    stay_open = True
    while stay_open:
    nvim.session.next_message() # block until `rpcnotify` is called
    nvim.next_message() # block until `rpcnotify` is called
    open_buffers = [window.buffer.number for window in nvim.windows]
    stay_open = any([buffer in open_buffers for buffer in new_buffers])

  10. rf- revised this gist Nov 18, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,6 @@ try:
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
    sys.exit()

    nvim = neovim.attach('socket', path=nvim_socket)

  11. rf- revised this gist Oct 13, 2015. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions nvim-client
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env python

    import os, sys, neovim, time
    import neovim, os, re, sys, time

    # Get a list of buffers that haven't been deleted. `nvim.buffers` includes
    # buffers that have had `:bdelete` called on them and aren't in the buffer
    @@ -13,7 +13,8 @@ def get_listed_buffers(nvim):
    # is good enough to recognize '-f' and `+11`, which is all this script really
    # needs right now.
    filenames = [
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    re.sub(' ', '\ ', os.path.abspath(arg))
    for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    ]

    try:
    @@ -49,10 +50,10 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    # hides one of the buffers.
    channel_id = nvim.channel_id
    for buffer in new_buffers:
    nvim.command(
    'autocmd BufDelete,BufHidden <buffer=%d> ' + \
    'call rpcnotify(%d, "check_buffers")' % (buffer, channel_id)
    )
    nvim.command((
    'autocmd BufDelete,BufHidden <buffer=%d> ' +
    'call rpcnotify(%d, "check_buffers")'
    ) % (buffer, channel_id))

    stay_open = True
    while stay_open:
    @@ -70,4 +71,4 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
    nvim.command('bdelete! %d' % buffer)
  12. rf- revised this gist Sep 13, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,9 @@ def get_listed_buffers(nvim):
    # For now, treat all arguments that don't start with - or + as filenames. This
    # is good enough to recognize '-f' and `+11`, which is all this script really
    # needs right now.
    filenames = [arg for arg in sys.argv[1:] if not arg[0] in ['-', '+']]
    filenames = [
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    ]

    try:
    nvim_socket = os.environ["NVIM_LISTEN_ADDRESS"]
  13. rf- revised this gist Sep 12, 2015. 1 changed file with 30 additions and 9 deletions.
    39 changes: 30 additions & 9 deletions nvim-client
    Original file line number Diff line number Diff line change
    @@ -2,17 +2,22 @@

    import os, sys, neovim, time

    # Get a list of buffers that haven't been deleted. `nvim.buffers` includes
    # buffers that have had `:bdelete` called on them and aren't in the buffer
    # list, so we have to filter those out.
    def get_listed_buffers(nvim):
    return set(buf.number for buf in nvim.buffers \
    if nvim.eval('buflisted(%d)' % buf.number))

    filenames = [
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    ]
    # For now, treat all arguments that don't start with - or + as filenames. This
    # is good enough to recognize '-f' and `+11`, which is all this script really
    # needs right now.
    filenames = [arg for arg in sys.argv[1:] if not arg[0] in ['-', '+']]

    try:
    nvim_socket = os.environ["NVIM_LISTEN_ADDRESS"]
    except KeyError:
    # If we aren't running inside a `:terminal`, just exec nvim.
    os.execvp('nvim', sys.argv)
    sys.exit()

    @@ -29,7 +34,17 @@ for arg in sys.argv:
    if arg[0] == '+':
    nvim.command(arg[1:])

    # The '-f' flag is a signal that we're in a situation like a `git commit`
    # invocation where we need to block until the user is done with the file(s).
    if '-f' in sys.argv and len(new_buffers) > 0:
    # The rule here is that the user is 'done' with the opened files when none
    # of them are visible onscreen. This allows for use cases like hitting `:q`
    # on a `git commit` tempfile. However, we can't just poll to see if they're
    # visible, because using `nvim.windows`, `nvim.eval()`, or `nvim.call()`
    # will interrupt any multi-key mappings the user may be inputting. The
    # solution is to set a buffer-local autocmd on each opened buffer so that
    # we only check for visibility immediately after the user either closes or
    # hides one of the buffers.
    channel_id = nvim.channel_id
    for buffer in new_buffers:
    nvim.command(
    @@ -39,12 +54,18 @@ if '-f' in sys.argv and len(new_buffers) > 0:

    stay_open = True
    while stay_open:
    nvim.session.next_message()
    nvim.session.next_message() # block until `rpcnotify` is called
    open_buffers = [window.buffer.number for window in nvim.windows]
    stay_open = any([buffer in open_buffers for buffer in new_buffers])

    if '-c' in sys.argv:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
    # Now that none of the opened files are visible anymore, we do a few
    # cleanup steps before ending the script:
    # * Clear the arg list, since otherwise `:next` would reopen the tempfile
    # or whatever.
    # * Clear the autocmds we added, since `bdelete` just hides the buffer and
    # the autocmds will still be active if the user reopens the file(s).
    # * Delete each of the buffers we created.
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
  14. rf- revised this gist Sep 12, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,6 @@ if '-f' in sys.argv and len(new_buffers) > 0:
    nvim.session.next_message()
    open_buffers = [window.buffer.number for window in nvim.windows]
    stay_open = any([buffer in open_buffers for buffer in new_buffers])
    time.sleep(0.25)

    if '-c' in sys.argv:
    nvim.command('argdel *')
  15. rf- revised this gist Sep 12, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion nvim-client
    Original file line number Diff line number Diff line change
    @@ -29,14 +29,23 @@ for arg in sys.argv:
    if arg[0] == '+':
    nvim.command(arg[1:])

    if '-f' in sys.argv:
    if '-f' in sys.argv and len(new_buffers) > 0:
    channel_id = nvim.channel_id
    for buffer in new_buffers:
    nvim.command(
    'autocmd BufDelete,BufHidden <buffer=%d> ' + \
    'call rpcnotify(%d, "check_buffers")' % (buffer, channel_id)
    )

    stay_open = True
    while stay_open:
    nvim.session.next_message()
    open_buffers = [window.buffer.number for window in nvim.windows]
    stay_open = any([buffer in open_buffers for buffer in new_buffers])
    time.sleep(0.25)

    if '-c' in sys.argv:
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('autocmd! BufDelete,BufHidden <buffer=%d>' % buffer)
    nvim.command('bdelete! %d' % buffer)
  16. rf- renamed this gist Sep 12, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. rf- revised this gist Sep 12, 2015. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,10 @@

    import os, sys, neovim, time

    def get_listed_buffers(nvim):
    return set(buf.number for buf in nvim.buffers \
    if nvim.eval('buflisted(%d)' % buf.number))

    filenames = [
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    ]
    @@ -13,26 +17,26 @@ except KeyError:
    sys.exit()

    nvim = neovim.attach('socket', path=nvim_socket)

    existing_buffers = get_listed_buffers(nvim)

    nvim.command('split')
    nvim.command('args %s' % ' '.join(filenames))

    new_buffers = get_listed_buffers(nvim).difference(existing_buffers)

    for arg in sys.argv:
    if arg[0] == '+':
    nvim.command(arg[1:])

    if '-f' in sys.argv:
    stay_open = True
    while stay_open:
    # FIXME: This is a dumb way of tracking this and breaks for stuff that
    # resolves to /private (e.g. Pry tempfiles)
    open_buffers = [window.buffer.name for window in nvim.windows]
    stay_open = any([
    (filename in open_buffers) for filename in filenames
    ])
    open_buffers = [window.buffer.number for window in nvim.windows]
    stay_open = any([buffer in open_buffers for buffer in new_buffers])
    time.sleep(0.25)

    if '-c' in sys.argv:
    for filename in filenames:
    for buffer in nvim.buffers:
    if buffer.name == filename:
    nvim.command('bdelete! %d' % buffer.number)
    nvim.command('argdel *')
    for buffer in new_buffers:
    nvim.command('bdelete! %d' % buffer)
  18. rf- revised this gist Sep 11, 2015. 1 changed file with 29 additions and 25 deletions.
    54 changes: 29 additions & 25 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -2,33 +2,37 @@

    import os, sys, neovim, time

    # TODO: search through all ancestors
    gppid = os.popen("ps -p %d -oppid=" % os.getppid()).read().strip()
    gp_command = os.popen("ps -p %s -ocommand=" % gppid).read().strip()

    filenames = [
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] == '-'
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] in ['-', '+']
    ]

    if gp_command == 'nvim':
    # TODO: be less horrible
    nvim_socket = (
    os.popen('lsof -p %s | grep " unix " | grep -v -- "->"' % gppid).
    read().
    split()[-1]
    )
    nvim = neovim.attach('socket', path=nvim_socket)
    try:
    nvim_socket = os.environ["NVIM_LISTEN_ADDRESS"]
    except KeyError:
    os.execvp('nvim', sys.argv)
    sys.exit()

    for filename in filenames:
    nvim.command('split %s' % filename)
    nvim = neovim.attach('socket', path=nvim_socket)
    nvim.command('split')
    nvim.command('args %s' % ' '.join(filenames))

    if '-f' in sys.argv:
    stay_open = True
    while stay_open:
    open_buffers = [window.buffer.name for window in nvim.windows]
    stay_open = any([
    (filename in open_buffers) for filename in filenames
    ])
    time.sleep(0.25)
    else:
    os.execvp('nvim', sys.argv)
    for arg in sys.argv:
    if arg[0] == '+':
    nvim.command(arg[1:])

    if '-f' in sys.argv:
    stay_open = True
    while stay_open:
    # FIXME: This is a dumb way of tracking this and breaks for stuff that
    # resolves to /private (e.g. Pry tempfiles)
    open_buffers = [window.buffer.name for window in nvim.windows]
    stay_open = any([
    (filename in open_buffers) for filename in filenames
    ])
    time.sleep(0.25)

    if '-c' in sys.argv:
    for filename in filenames:
    for buffer in nvim.buffers:
    if buffer.name == filename:
    nvim.command('bdelete! %d' % buffer.number)
  19. rf- created this gist Sep 11, 2015.
    34 changes: 34 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/env python

    import os, sys, neovim, time

    # TODO: search through all ancestors
    gppid = os.popen("ps -p %d -oppid=" % os.getppid()).read().strip()
    gp_command = os.popen("ps -p %s -ocommand=" % gppid).read().strip()

    filenames = [
    os.path.abspath(arg) for arg in sys.argv[1:] if not arg[0] == '-'
    ]

    if gp_command == 'nvim':
    # TODO: be less horrible
    nvim_socket = (
    os.popen('lsof -p %s | grep " unix " | grep -v -- "->"' % gppid).
    read().
    split()[-1]
    )
    nvim = neovim.attach('socket', path=nvim_socket)

    for filename in filenames:
    nvim.command('split %s' % filename)

    if '-f' in sys.argv:
    stay_open = True
    while stay_open:
    open_buffers = [window.buffer.name for window in nvim.windows]
    stay_open = any([
    (filename in open_buffers) for filename in filenames
    ])
    time.sleep(0.25)
    else:
    os.execvp('nvim', sys.argv)