aboutsummaryrefslogtreecommitdiff
path: root/.config/mpv/scripts/copy-subs.lua
blob: 973e1f2da92309e38f8125d1eb4f3a1ed9c7df10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
_G.auto = false

function copy()
	local sub = mp.get_property("sub-text")
	if sub then
		-- remove some specific unicode characters
		sub = sub:gsub('\u{27a1}', '')
		sub = sub:gsub('\u{fffd}', '')
		sub = sub:gsub('\u{3013}', '')
		sub = sub:gsub('\u{2015}', '')

		sub = sub:gsub('\n', ' ') -- replace newlines with space
		sub = sub:gsub('\'', '\'\\\'\'') -- escape single quotes
		os.execute("echo '" .. sub .. "' | xclip -selection clipboard -i")
	end
end

function autocopy()
	if _G.auto == false then return end
	copy()
end

function toggle_autocopy()
	_G.auto = not _G.auto
	mp.osd_message("autocopy " .. (_G.auto and "on" or "off"))
end

mp.add_key_binding(nil, "copy", copy)

mp.observe_property("sub-text", "string", autocopy)
mp.add_key_binding(nil, "auto", toggle_autocopy)