Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipboard action picking up items from previous selection in visual mode (v) #66

Open
dhruvmanila opened this issue Apr 20, 2022 · 4 comments

Comments

@dhruvmanila
Copy link
Contributor

Description

I've a custom function which marks the items and calls the respective clipboard action all in one. This was working perfectly well, but at some point in the past it stopped working. Today, I got the time to debug it and seems to be a problem in the plugin, but I'm not 100% sure.

Reproduction

  1. Save the below code in minimal.lua
-- Load the 'runtime/' files
vim.cmd [[set runtimepath=$VIMRUNTIME]]

-- Originally, `packpath` contains a lot of path to search into which also
-- includes the `~/.config/nvim` directory. Now, if we open Neovim, the files
-- in the `plugin/`, `ftplugin/`, etc. directories will be loaded automatically.
--
-- We will set the value of `packpath` to contain only our testing directory to
-- avoid loading files from our config directory.
--
--     $ nvim -nu minimal.lua
vim.cmd [[set packpath=/tmp/nvim/site]]

local package_root = '/tmp/nvim/site/pack'
local packer_install_path = package_root .. '/packer/start/packer.nvim'

local function load_plugins()
  require('packer').startup {
    {
      'wbthomason/packer.nvim',
      -- Add plugins to test...
      'tamago324/lir.nvim',
      'nvim-lua/plenary.nvim',
    },
    config = {
      package_root = package_root,
      compile_path = packer_install_path .. '/plugin/packer_compiled.lua',
      display = { non_interactive = true },
    },
  }
end

_G.load_config = function()
  -- Add the necessary `init.lua` settings which could include the setup
  -- functions for the plugins...
  vim.g.loaded_netrw = 1
  vim.g.loaded_netrwPlugin = 1

  local lir = require 'lir'
  local actions = require 'lir.actions'
  local mark_actions = require 'lir.mark.actions'

  local clipboard_actions = setmetatable({}, {
    __index = function(_, action)
      return function(mode)
        mark_actions.mark(mode)
        require('lir.clipboard.actions')[action]()
      end
    end,
  })

  lir.setup {
    mappings = {
      ['q'] = actions.quit,
      ['h'] = actions.up,
      ['l'] = actions.edit,
    },
    on_init = function()
      vim.keymap.set('x', 'C', function()
        clipboard_actions.copy 'v'
      end, {
        buffer = true,
        desc = 'Lir: Copy visual selection',
      })

      vim.keymap.set('x', 'X', function()
        clipboard_actions.cut 'v'
      end, {
        buffer = true,
        desc = 'Lir: Cut visual selection',
      })
    end,
  }

  vim.keymap.set('n', '-', require('lir.float').toggle)
end

if vim.fn.isdirectory(packer_install_path) == 0 then
  print 'Installing plugins and dependencies...'
  vim.fn.system {
    'git',
    'clone',
    '--depth=1',
    'https://github.com/wbthomason/packer.nvim',
    packer_install_path,
  }
end

load_plugins()
require('packer').sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]]
  1. Open Neovim with nvim -n -u path/to/minimal.lua
  2. Open the Lir floating window with the key -
  3. Select any number of items and press C (that's uppercase C)
  4. Now, select items other than the ones from step 4 and repeat

You'll notice that in step 4, nothing got marked as copy while in step 5, the previous selection got marked as copy.

Video

lir_bug_repro

@tamago324
Copy link
Owner

Thanks for the report.
You are right, it does not work properly.

It worked correctly when I did the following
But the cause is unknown.

	on_init = function()
		vim.api.nvim_buf_set_keymap(
			0,
			"x",
			"C",
			':<C-u>lua require"lir.mark.actions".toggle_mark("v")<CR> | :lua require"lir.clipboard.actions".copy()<CR>',
			{ noremap = true, silent = true }
		)
    end

@tamago324
Copy link
Owner

Using vim.api.nvim_buf_set_keymap() as follows seems to work well.
Possibly a bug in vim.keymap.set.

vim.api.nvim_buf_set_keymap(0, "x", "C", "", {
  noremap = true,
  callback = function()
    m.toggle_mark("v")
    c.copy()
  end,
})

@dhruvmanila
Copy link
Contributor Author

vim.api.nvim_buf_set_keymap(0, "x", "C", "", {
  noremap = true,
  callback = function()
    m.toggle_mark("v")
    c.copy()
  end,
})

This is not working for me. Which Neovim version are you on? I'm on stable v0.7.0.

NVIM v0.7.0
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by dhruv@mbp

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/Users/dhruv/neovim/share/nvim"

Run :checkhealth for more info

@dhruvmanila
Copy link
Contributor Author

Hi @tamago324 any update here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants