-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.lua
195 lines (152 loc) · 3.8 KB
/
mapper.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
-- local log = function(msg) reaper.ShowConsoleMsg(msg .. '\n') end
local log = function(msg) end
local msg = function(msg) reaper.ShowConsoleMsg("[Vimper] ".. msg .. '\n') end
local clearLog = reaper.ClearConsole
local bindingsPath = reaper.GetResourcePath() .. '/vimper_bindings.lua'
local timeOut = 2
function scriptPath()
local info = debug.getinfo(1,'S');
return info.source:match[[^@?(.*[\/])[^\/]-$]]
end
function include(path)
return dofile(scriptPath() .. path .. '.lua')
end
include('state_io')
include('action_codes')
function mergeInclude(...)
local tmp = scriptPath() .. "tmp_actions"
local args = table.pack(...)
local fail = false
withFile(tmp, 'w', function(f)
for _,v in ipairs(args) do
local content = readFile(v)
if content == nil then
msg("File missing: " .. v)
fail = true
else
f:write(content)
end
end
end)
if fail then
return {}
else
return dofile(tmp)
end
end
function tryTriggerAction(actions, curInput, count)
actionFun = actions[curInput]
if actionFun then
local ret = actionFun(count)
return ret or true
else
return false
end
end
function timeoutState()
local curTime = os.time()
local lastTime = getLastTime() or 0
updateLastTime(curTime)
if curTime - lastTime >= timeOut then
clearQuery()
end
end
function longestTableKeyLen(t)
local longestLen = 0
for k,_ in pairs(t) do
local l = string.len(k)
if l > longestLen then
longestLen = l
end
end
return longestLen
end
-- Replaces all instances of special keys, e.g <space>, with a single character
function shortenSpecial(str)
return string.gsub(str, '<.->', '_')
end
function mapTableKeys(t, f)
local out = {}
for k, v in pairs(t) do
local newK = f(k, v)
out[newK] = v
end
return out
end
function shortenKeys(t)
return mapTableKeys(t, function(k)
return shortenSpecial(k)
end)
end
function stripNumbers(str)
-- return string.gsub(str, '%d', '')
return str:gsub("[0-9]*(.-)$", "%1")
end
function getCount(str)
local count = 0
local firstNonNumInd = str:find("[^%d]")
if firstNonNumInd ~= nil and firstNonNumInd > 1 then
for d in str:sub(0, firstNonNumInd-1):gmatch '%d' do
count = count*10 + tonumber(d)
end
end
return count
end
function tooLong(actions, query)
return longestTableKeyLen(shortenKeys(actions)) <= string.len(shortenSpecial(query))
end
function doInput(ch, context)
timeoutState()
local st = getQuery() or ''
local allActions = mergeInclude(scriptPath()..'actions.lua', bindingsPath)
local actions = allActions.global
for k,v in pairs(allActions[context]) do actions[k] = v end
local originalQuery = st .. ch
local curCount = getCount(shortenSpecial(originalQuery))
local query = stripNumbers(originalQuery)
log(query .. " | " .. ch)
local actionRet = tryTriggerAction(actions, query, curCount)
log(context)
if actionRet and actionRet ~= DO_NOT_STORE_LAST then
setLastAction(originalQuery)
setLastContext(context)
end
if ch == "<esc>" or actionRet or tooLong(actions, query) then
log 'clearQuery'
clearQuery()
else
updateQuery(ch)
end
end
--[[
function filterTable(t, filterIter)
local out = {}
for k, v in pairs(t) do
if filterIter(v, k, t) then out[k] = v end
end
return out
end
function tableLen(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function firstTableValue(t)
local retK, retV
for k,v in pairs(t) do
retK, retV = k,v
end
return retK, retV
end
function strAt(str, ind)
return string.sub(str, ind, ind)
end
function filterActions(acts, char)
local newActs = filterTable(acts, function(v, k)
return strAt(k, 1) == char
end)
return mapTableKeys(newActs, function(k)
return string.sub(k, 2, -1)
end)
end
--]]