AckslD / nvim-neoclip.lua

Clipboard manager neovim plugin with telescope integration
944 stars 20 forks source link

Ignore vim-surround #46

Open rockyzhang24 opened 2 years ago

rockyzhang24 commented 2 years ago

Was looking for such a plugin for managing clipboard and finally I found this. Thank you.

I am using vim-surround as well and I noticed that the deleted surroundings such as " ", ' ', etc are caught by this plugin. I believe this is meaningless. Take "Hello World" as an example, I place the cursor anywhere inside the double quotes and then I press cs"' to change the surroundings from double quotes to single quotes. Then the double quotes " " are recorded by this plugin. Is it possible to ignore such a behavior of other plugins?

Screen Shot 2022-02-05 at 11 28 33

Thank you very much.

AckslD commented 2 years ago

Hi @yanzhang0219, happy you like it :)

Maybe a filter could be used for this. Although I'm not sure exactly how vim-surround performs the change. Could you add a filter just to print what the yank action is:

require('neoclip').setup{
  ...
  filter = function(data)
    print(vim.inspect(data))
    return true
  end,
  ...
}

Then perform the action and see what :messages says.

Btw, I'm using vim-sandwich and if I do sr'" I don't get any additional entries in the neoclip history.

rockyzhang24 commented 2 years ago

Okay. Thanks. I also noticed that not only the surroundings are stored, but the surrounded text is stored as well. That means, once I execute cs"', two entries are stored. One is Hello World and the other is " ". I think the filter may be not applicable because text surrounded by some characters is very general. If I rule out all the text with surroundings to avoid it being stored by vim-surround, in the future when I want to copy it by my own, it won't be stored.

Here is what :messages output after adding the filter above.

image

I didn't dig into the source code of vim-surround. But I guess it must yank the text and the surroundings to implement its functions, and this triggers neoclip to record them.

AckslD commented 2 years ago

@yanzhang0219 Another option would be to execute the vim-surround commands using :noautocmd such that TextYankPost is not triggered and the entry it not stored.

Alternatively you could wrap the call with a require('neoclip').stop()/require('neoclip').start().

Unfortunately, the data passed to the filter is all the information we can know in the autocmd of TextYankPost, so there is no real way to filter this from neoclips side.

AckslD commented 2 years ago

Btw, as for you question on the vim-surround issue above, from the :messages you have above you can see that the operator is actually d. So vim-surround is deleting things during it's command and not actually yanking (y). Which to be fair I wouldn't say is nothing wrong in itself :)

Anyway, I've been pretty happy with vim-sandwich for a while and can recommend it.

rockyzhang24 commented 2 years ago

Thank you so much for the information.

you can see that the operator is actually d

Exactly. Sorry I overlooked that. Just now I went through the source code of vim-surround and I saw that it uses d operator with the default register. I've already changed the vim-surround issue I opened to see whether the blackhole register could be used instead of the default register.

Anyway, I've been pretty happy with vim-sandwich for a while and can recommend it.

I will definitely give it a shot. Thank you.

rockyzhang24 commented 2 years ago

Hello @AckslD, after using this plugin for a while, I have some comments and I will write it down here instead of opening a new issue because they are related. Thank you.

I found many plugins internally use d, c or y together with the default register to implement its features. This causes many useless texts are listed. For example, LuaSnip removes the placeholder by c, so if a snippet has a placeholder and after we replace it, the placeholder text is stored and will be listed by neoclip.

Using :noautocmd or wrapping stop()/start() is not applicable for some situation like LuaSnip example above. So the history list will contain many many unnecessary and useless texts in it.

AckslD commented 2 years ago

@yanzhang0219 Hmm I see, I will try to think of there is a good solution here. Let me know if you have some ideas.

AckslD commented 2 years ago

Btw, have you found any plugins using y or would restricting to only such yanks work for you? Ie using #47.

AckslD commented 2 years ago

Actually, I just realised that another plugin of mine for example uses y :)

https://github.com/AckslD/nvim-revJ.lua

rockyzhang24 commented 2 years ago

No idea so far. 😮‍💨 If only they use the blackhole register instead of the default one.

Is it possible to distinguish whether the operators are directly from the keystroke (by an event)? Actually we only care about the contents yanked directly by our own keystroke. Plugins internally use these operators by exec or something like that, and they are not from the actual keystrokes.

AckslD commented 2 years ago

@yanzhang0219, I'm not sure if that's possible unfortunately :/