1
0
mirror of https://github.com/kepler155c/opus synced 2024-10-03 09:20:41 +00:00
opus/sys/autorun/clipboard.lua

46 lines
1.1 KiB
Lua
Raw Normal View History

2017-10-08 21:45:01 +00:00
_G.requireInjector()
2017-10-14 07:41:54 +00:00
local Util = require('util')
2017-10-14 07:41:54 +00:00
2017-10-15 06:36:54 +00:00
local keys = _G.keys
2017-10-14 07:41:54 +00:00
local multishell = _ENV.multishell
2017-10-15 06:36:54 +00:00
local textutils = _G.textutils
2017-10-08 21:45:01 +00:00
local clipboard = { }
function clipboard.getText()
if clipboard.data then
2017-10-15 06:36:54 +00:00
if type(clipboard.data) == 'table' then
local s, m = pcall(textutils.serialize, clipboard.data)
clipboard.data = (s and m) or Util.tostring(clipboard.data)
end
return Util.tostring(clipboard.data)
end
end
function clipboard.useInternal(mode)
2017-05-14 00:46:26 +00:00
if mode ~= clipboard.internal then
clipboard.internal = mode
2017-10-14 07:41:54 +00:00
local text = 'Clipboard (^m): ' .. ((mode and 'internal') or 'normal')
multishell.showMessage(text)
end
end
2017-10-14 07:41:54 +00:00
multishell.hook('clipboard_copy', function(_, args)
2017-10-15 06:36:54 +00:00
clipboard.data = args[1]
if clipboard.data then
clipboard.useInternal(true)
end
2017-10-14 07:41:54 +00:00
end)
multishell.hook('paste', function(_, args)
2017-10-15 06:36:54 +00:00
if clipboard.internal then
2017-10-14 07:41:54 +00:00
args[1] = clipboard.getText() or ''
end
end)
2017-10-15 06:36:54 +00:00
-- control-m - toggle clipboard mode
multishell.addHotkey(keys.m, function()
clipboard.useInternal(not clipboard.internal)
2017-10-14 07:41:54 +00:00
end)