mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 04:01:13 +01:00
Fix: Yank notifications all over copy to clipboard notifcations
This commit is contained in:
@@ -1,17 +1,22 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
function M.trim_clipboard()
|
function M.trim_clipboard()
|
||||||
-- Get the content of the '+' register
|
-- Get content from default register
|
||||||
local content = vim.fn.getreg '+'
|
local content = vim.fn.getreg '"'
|
||||||
-- Remove single trailing newline if present
|
if not content or content == '' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
-- Remove trailing newline
|
||||||
content = content:gsub('\n$', '')
|
content = content:gsub('\n$', '')
|
||||||
-- Update '+' and '*' registers
|
-- Copy to system clipboard
|
||||||
vim.fn.setreg('+', content)
|
vim.fn.setreg('+', content)
|
||||||
vim.fn.setreg('*', content)
|
vim.fn.setreg('*', content)
|
||||||
-- Count the number of lines copied
|
-- Count lines
|
||||||
local line_count = select(2, content:gsub('\n', '')) + 1 -- Number of newlines + 1
|
local line_count = select(2, content:gsub('\n', '')) + 1
|
||||||
-- Notification with line count
|
|
||||||
local plural = line_count == 1 and '' or 's'
|
local plural = line_count == 1 and '' or 's'
|
||||||
vim.notify(string.format('Copied %d line%s to clipboard', line_count, plural), vim.log.levels.INFO)
|
-- Delay clipboard notification slightly to appear after yank notification
|
||||||
|
vim.defer_fn(function()
|
||||||
|
vim.notify(string.format('Copied %d line%s to clipboard', line_count, plural), vim.log.levels.INFO, { title = 'Clipboard' })
|
||||||
|
end, 50) -- 50ms delay
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -161,7 +161,10 @@ vim.keymap.set('n', 'Y', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
-- Visual mode: copy selection, trim trailing newline if needed
|
-- Visual mode: copy selection, trim trailing newline if needed
|
||||||
vim.keymap.set('x', 'Y', [["+y<esc>:lua require("copy_to_clipboard_fix").trim_clipboard()<CR>]])
|
vim.keymap.set('x', 'Y', function()
|
||||||
|
vim.cmd 'normal! y'
|
||||||
|
require('copy_to_clipboard_fix').trim_clipboard()
|
||||||
|
end, { noremap = true, silent = true })
|
||||||
|
|
||||||
-- [[ Paste from clipboard with line count ]]
|
-- [[ Paste from clipboard with line count ]]
|
||||||
local function paste_from_clipboard()
|
local function paste_from_clipboard()
|
||||||
|
|||||||
@@ -1,21 +1,24 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
vim.api.nvim_create_autocmd('TextYankPost', {
|
vim.api.nvim_create_autocmd('TextYankPost', {
|
||||||
pattern = '*',
|
pattern = '*',
|
||||||
callback = function()
|
callback = function(ev)
|
||||||
local content = vim.fn.getreg '"'
|
local regname = ev.regname or '"'
|
||||||
|
-- Ignore clipboard yanks
|
||||||
|
if regname == '+' or regname == '*' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local content = vim.fn.getreg(regname)
|
||||||
if not content or content == '' then
|
if not content or content == '' then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- Split by newline
|
|
||||||
local lines = vim.split(content, '\n', true)
|
local lines = vim.split(content, '\n', true)
|
||||||
-- Remove trailing empty line if present
|
|
||||||
if lines[#lines] == '' then
|
if lines[#lines] == '' then
|
||||||
table.remove(lines, #lines)
|
table.remove(lines, #lines)
|
||||||
end
|
end
|
||||||
local line_count = #lines
|
local line_count = #lines
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
local plural = line_count == 1 and '' or 's'
|
local plural = line_count == 1 and '' or 's'
|
||||||
vim.notify(string.format('Yanked %d line%s', line_count, plural), vim.log.levels.INFO)
|
vim.notify(string.format('Yanked %d line%s', line_count, plural), vim.log.levels.INFO, { title = 'Yank' })
|
||||||
end)
|
end)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user