kevinhwang91 / rnvimr

Make Ranger running in a floating window to communicate with Neovim via RPC
BSD 3-Clause "New" or "Revised" License
800 stars 17 forks source link

Expose if ranger is currently opened #131

Closed bagohart closed 1 year ago

bagohart commented 1 year ago

Is your feature request related to a problem? Please describe. Hi, cool plugin! I'm currently trying to configure this to my liking, and I would like to add a mapping that invokes :RnvimrResize, but only when ranger is opened. Ideally, this would work like

vim.keymap.set('n', '<F11>', function() 
  if Rnvimr.active() then
    vim.cmd('RnvimrResize')
  else
    somethingElse()
  end
end)

Looking at the code, I see that there are several functions involved in detecting this:

function! rnvimr#toggle() abort
    let win_hd = rnvimr#context#winid()
    if rnvimr#context#bufnr() != -1

This seems rather involved, and it's probably not a good idea to depend on these vimscript internals directly, since they are internals and could change at any time.

Describe the solution you'd like Is it possible to add a simple function Rmvimr.active() that I can use in a mapping?

kevinhwang91 commented 1 year ago
local function active()
    local bufnr = vim.fn.bufnr('ranger')
    return bufnr > 0 and vim.bo[bufnr].ft == 'rnvimr'
end
bagohart commented 1 year ago

Thanks, that turned out to be easier than I expected! Your suggestion doesn't work though, I think it's because that active() function doesn't check if bufnr('ranger') corresponds to the current buffer. In case anyone wants to accomplish the same thing, the following seems to work:

            function RnvimrActive()
                local rnvimr_bufnr = vim.fn.bufnr('ranger')
                local cur_bufnr = vim.fn.bufnr()
                return rnvimr_bufnr > 0 and vim.bo[rnvimr_bufnr].ft == 'rnvimr' and cur_bufnr == rnvimr_bufnr
            end
-- ...
            vim.keymap.set('n', '<F11>', function()
                if RnvimrActive() then
                    vim.cmd('RnvimrResize')
                    vim.cmd('stopinsert')
                else
                    vim.cmd('ZoomWinTabToggle') -- or any other action
                end
            end)