Skip to content

Commit

Permalink
feat: add permament_wrapper option to manage wsl (#203)
Browse files Browse the repository at this point in the history
Ref: #196
  • Loading branch information
gbprod authored Nov 12, 2024
1 parent 73215b7 commit 0c5d77a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Yanky comes with the following defaults:
cancel_event = "update",
ignore_registers = { "_" },
update_register_on_cycle = false,
permanent_wrapper = nil,
},
picker = {
select = {
Expand Down Expand Up @@ -199,6 +200,7 @@ require("yanky").setup({
cancel_event = "update",
ignore_registers = { "_" },
update_register_on_cycle = false,
permanent_wrapper = nil,
},
system_clipboard = {
sync_with_ring = true,
Expand Down Expand Up @@ -300,6 +302,14 @@ Using the `update_register_on_cycle` option, when you cycle through the ring,
the contents of the register used to update will be updated with the last
content cycled.

### `ring.permanent_wrapper`

Default: `nil`

Using the `permanent_wrapper` option, you can set a wrapper that will be used
for every put action. This is useful if you want to add a treatment to every
put actions (ex: remove `\r` for wsl support).

### Commands

You can clear yank history using `YankyClearHistory` command.
Expand Down Expand Up @@ -706,7 +716,9 @@ require("substitute").setup({
on_substitute = require("yanky.integration").substitute(),
})
```

or

```lua
opts = {
on_substitute = function() require("yanky.integration").substitute() end,
Expand Down Expand Up @@ -784,6 +796,22 @@ end

</details>

<details>
<summary><b>windows wsl</b></summary>
When pasting text from windows system clipboard, there's always a ^M at the end
of each line. Adding a permeant wrapper to remove these ^M characters when pasting
will allows you to paste text from windows system clipboard without any issues.

```lua
require("yanky").setup({
ring = {
permanent_wrapper = require("yanky.wrappers").remove_carriage_return,
},
})
```

</details>

## 🎉 Credits

This plugin is mostly a lua version of [svermeulen/vim-yoink](https://github.com/svermeulen/vim-yoink)
Expand Down
3 changes: 3 additions & 0 deletions lua/yanky.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ function yanky.put(type, is_visual, callback)
yanky.ring.state = nil
yanky.ring.is_cycling = false
yanky.ring.callback = callback or do_put
if yanky.config.options.ring.permament_wrapper ~= nil then
yanky.ring.callback = yanky.config.options.ring.permament_wrapper(yanky.ring.callback)
end

-- On Yank event is not triggered when put from expression register,
-- To allows cycling, we must store value here
Expand Down
19 changes: 19 additions & 0 deletions lua/yanky/wrappers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,23 @@ function wrappers.set_cursor_pos(pos, next)
end
end

function wrappers.remove_carriage_return(next)
return function(state, callback)
local body = vim.fn.getreg(state.register)
local type = vim.fn.getregtype(state.register)

local reformated_body = body:gsub("\r", "")

vim.fn.setreg(state.register, reformated_body, type)

if nil == next then
callback(state)
else
next(state, callback)
end

vim.fn.setreg(state.register, body, type)
end
end

return wrappers

0 comments on commit 0c5d77a

Please sign in to comment.