-
Notifications
You must be signed in to change notification settings - Fork 148
/
clippy.lua
135 lines (118 loc) · 3.61 KB
/
clippy.lua
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
local id = ID("clippy.clippy")
local menuid
local stack
local kStackLimit = 10
local function SaveStack(self)
local settings = self:GetSettings()
settings.stack = stack
self:SetSettings( settings )
end
local function SaveClip()
local tdo = wx.wxTextDataObject("None")
if wx.wxClipboard:Get():Open() then
wx.wxClipboard:Get():GetData(tdo)
wx.wxClipboard:Get():Close()
local newclip = tdo:GetText()
if newclip ~= "" then
for i,oldclip in ipairs( stack ) do
if newclip == oldclip then
table.remove( stack, i )
table.insert( stack, 1, newclip )
stack[kStackLimit] = nil
return
end
end
table.insert( stack, 1, newclip )
stack[kStackLimit] = nil
end
end
end
local function OpenClipList( editor )
if not editor then
return
end
if editor:AutoCompActive() then
editor:AutoCompCancel()
end
editor:AutoCompSetSeparator(string.byte('\n'))
editor:AutoCompSetTypeSeparator(0)
local list, firstline, rem = {}
for i,clip in ipairs(stack) do
firstline, rem = string.match(clip,'([^\r\n]+)(.*)')
if firstline~=nil then
if rem ~= "" then firstline = firstline .. "..." end
list[#list+1] = i.."\t "..firstline
end
end
editor:UserListShow(2,table.concat(list,'\n'))
editor:AutoCompSelect( list[2] or "" )
editor:AutoCompSetSeparator(string.byte(' '))
editor:AutoCompSetTypeSeparator(string.byte('?'))
end
function PasteClip(i)
local newclip = stack[i]
local tdo = wx.wxTextDataObject(newclip)
if wx.wxClipboard:Get():Open() then
wx.wxClipboard:Get():SetData(tdo)
wx.wxClipboard:Get():Close()
if i ~= 1 then
table.remove( stack, i )
table.insert( stack, 1, newclip )
stack[kStackLimit] = nil
end
ide.frame:AddPendingEvent(wx.wxCommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, ID.PASTE))
return true
end
return false
end
local function OnRegister(self)
local menu = ide:GetMenuBar():GetMenu(ide:GetMenuBar():FindMenu(TR("&Edit")))
menuid = menu:Append(id, "Open Clip Stack"..KSC(id, "Ctrl-Shift-V"))
ide:GetMainFrame():Connect(id, wx.wxEVT_COMMAND_MENU_SELECTED, function (event)
OpenClipList(ide:GetEditorWithFocus(ide:GetEditor()))
end)
ide:GetMainFrame():Connect(id, wx.wxEVT_UPDATE_UI, function (event)
event:Check(ide:GetEditorWithFocus(ide:GetEditor()) ~= nil)
end)
local settings = self:GetSettings()
stack = settings.stack or {}
settings.stack = stack
self:SetSettings(settings)
end
local function OnUnRegister(self)
local menu = ide:GetMenuBar():GetMenu(ide:GetMenuBar():FindMenu(TR("&Edit")))
ide:GetMainFrame():Disconnect(id, wx.wxID_ANY, wx.wxID_ANY)
if menuid then menu:Destroy(menuid) end
end
local function OnEditorAction( self, editor, event )
local eid = event:GetId()
if eid == ID.COPY or eid == ID.CUT then
-- call the original handler first to process Copy/Cut event
self.onEditorAction = nil
ide.frame:ProcessEvent(wx.wxCommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, eid))
self.onEditorAction = OnEditorAction
SaveClip()
SaveStack(self)
return false
end
end
local function OnEditorUserlistSelection( self, editor, event )
if event:GetListType() == 2 then
local i = tonumber( event:GetText():sub(1,1) );
PasteClip(i)
SaveStack(self)
return false
end
end
return
{
name = "Clippy",
description = "Enables a stack-based clipboard which saves the last 10 entries.",
author = "sclark39",
dependencies = "1.3",
version = 0.24,
onRegister = OnRegister,
onUnRegister = OnUnRegister,
onEditorAction = OnEditorAction,
onEditorUserlistSelection = OnEditorUserlistSelection,
}