forked from dbalatero/VimMode.spoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
388 lines (304 loc) · 8.98 KB
/
init.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
VimMode = {
afterExitHooks = nil,
commandState = nil,
entered = false,
enabled = true,
mode = nil,
sequence = nil
}
VimMode.name = "VimMode"
VimMode.version = "0.0.1"
VimMode.author = "David Balatero <dbalatero@gmail.com>"
VimMode.license = "ISC"
local function getSpoonPath()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
VimMode.spoonPath = getSpoonPath()
VimMode.requireFile = function(path)
return dofile(VimMode.spoonPath..path..".lua")
end
local extendedModes = VimMode.requireFile('lib/extended-modes')
local motions = VimMode.requireFile('lib/motions')
local operators = VimMode.requireFile('lib/operators')
local utils = VimMode.requireFile('lib/utils')
local function compose(...)
local fns = {...}
return function()
for _, fn in ipairs(fns) do
fn()
end
end
end
VimMode.buildCommandState = function()
return {
selection = false,
visualMode = false,
operatorFn = nil,
motionDirection = 'forward'
}
end
VimMode.spoonPath = getSpoonPath()
VimMode.dimScreen = function()
-- Stole these shifts from flux-like plugin
-- https://github.com/calvinwyoung/.dotfiles/blob/master/darwin/hammerspoon/flux.lua
whiteShift = {
alpha = 1.0,
red = 1.0,
green = 0.95201559,
blue = 0.90658983,
}
blackShift = {
alpha = 1.0,
red = 0,
green = 0,
blue = 0,
}
hs.screen.primaryScreen():setGamma(whiteShift, blackShift)
end
VimMode.restoreDim = function()
hs.screen.restoreGamma()
end
VimMode.new = function()
local self = utils.deepcopy(VimMode)
self.afterExitHooks = {}
self.commandState = VimMode.buildCommandState()
self.entered = false
self.enabled = true
self.mode = hs.hotkey.modal.new()
self.sequence = {
tap = nil,
waitingForPress = false
}
self.watchers = {}
return self
end
function VimMode:init()
self:bindModeKeys()
end
---------- toggling
function VimMode:enter()
if self.enabled then
self.mode:enter()
self.entered = true
self:resetState()
VimMode.dimScreen()
end
end
function VimMode:exit()
self.mode:exit()
VimMode.restoreDim()
self.entered = false
self:runAfterExitHooks()
end
function VimMode:disableForApp(disabledApp)
local vim = self
local watcher =
hs.application.watcher.new(function(applicationName, eventType)
if disabledApp ~= applicationName then return end
if eventType == hs.application.watcher.activated then
vim:exit()
vim:disable()
elseif eventType == hs.application.watcher.deactivated then
vim:enable()
end
end)
watcher:start()
-- If we are currently in this disabled application, exit vim mode
-- and disable
local currentApplication = hs.application.frontmostApplication()
if currentApplication:name() == disabledApp then
vim:exit()
vim:disable()
end
self.watchers[disabledApp] = watcher
end
---------- state
function VimMode:isSelection()
return not not self.commandState.selection
end
function VimMode:isVisualMode()
return not not self.commandState.visualMode
end
function VimMode:toggleVisualMode()
self.commandState.visualMode = true
self.commandState.selection = not self.commandState.selection
end
function VimMode:disable()
self.enabled = false
end
function VimMode:enable()
self:resetState()
self.enabled = true
end
function VimMode:resetState()
self.commandState = VimMode.buildCommandState()
end
function VimMode:hasOperator()
return not not self.commandState.operatorFn
end
---------- hooks
function VimMode:registerAfterExit(fn)
table.insert(self.afterExitHooks, fn)
end
function VimMode:runAfterExitHooks()
for _, fn in ipairs(self.afterExitHooks) do
fn()
end
end
---------- actions
function VimMode:runOperator()
if self.commandState.operatorFn then
self.commandState.operatorFn(self)
self:resetState()
end
end
function VimMode:restoreCursor()
if self.commandState.motionDirection == 'forward' then
utils.sendKeys({}, 'left')
else
utils.sendKeys({}, 'right')
end
end
---------- key bindings
function VimMode:enableKeySequence(key1, key2, modifiers)
modifiers = modifiers or {}
local waitingForPress = false
local maxDelay = 200
self.sequence.tap = hs.eventtap.new(
{hs.eventtap.event.types.keyDown},
function(event)
if not self.enabled or self.entered then
return false
end
local hasModifiers = event:getFlags():containExactly(modifiers)
local keyPressed = hs.keycodes.map[event:getKeyCode()]
if hasModifiers and keyPressed == key1 then
self.sequence.waitingForPress = true
hs.timer.doAfter(maxDelay / 1000, function()
if not self.sequence.waitingForPress then return end
self.sequence.waitingForPress = false
self.sequence.tap:stop()
utils.sendKeys(modifiers, key1)
self.sequence.tap:start()
end)
return true
end
if self.sequence.waitingForPress then
self.sequence.waitingForPress = false
if hasModifiers and keyPressed == key2 then
self.sequence.tap:stop()
self:enter()
return true
else
-- Pass thru the first key as well as the second one if we aren't
-- typing the sequence.
local currentModifiers = event:getFlags()
local currentKey = event:getKeyCode()
return true, {
hs.eventtap.event.newKeyEvent(modifiers, key1, true),
hs.eventtap.event.newKeyEvent(modifiers, key1, false),
hs.eventtap.event.newKeyEvent(currentModifiers, currentKey, true),
hs.eventtap.event.newKeyEvent(currentModifiers, currentKey, false)
}
end
end
return false
end
)
self:registerAfterExit(function()
self.sequence.tap:start()
end)
self.sequence.tap:start()
end
function VimMode:bindModeKeys()
local exit = function() self:exit() end
local isNormalMode = function(fn)
return function()
if not self.commandState.visualMode then fn() end
end
end
local function operatorOrMotion(operatorFn, motionFn)
return function()
if self:hasOperator() then
motionFn()
else
operatorFn()
end
end
end
------------ exiting
self.mode:bind({}, 'i', exit)
------------ motions
self.mode:bind({}, 'b', motions.backWord(self), nil, motions.backWord(self))
self.mode:bind({}, 'w', motions.word(self), nil, motions.word(self))
self.mode:bind({}, 'h', motions.left(self), nil, motions.left(self))
self.mode:bind({}, 'j', motions.down(self), nil, motions.down(self))
self.mode:bind({}, 'k', motions.up(self), nil, motions.up(self))
self.mode:bind({}, 'l', motions.right(self), nil, motions.right(self))
self.mode:bind({}, '0', motions.beginningOfLine(self), nil, motions.beginningOfLine(self))
self.mode:bind({'shift'}, '4', motions.endOfLine(self), nil, motions.endOfLine(self))
self.mode:bind({'shift'}, 'l', motions.endOfLine(self), nil, motions.endOfLine(self))
self.mode:bind({'shift'}, 'g', motions.endOfText(self), nil, motions.endOfText(self))
------------ operators
self.mode:bind({}, 'c', operators.change(self))
endOfLine = motions.endOfLine(self)
self.mode:bind({}, 'd',
operatorOrMotion(
operators.delete(self),
function()
utils.sendKeys({'command'}, 'left')
endOfLine()
utils.sendKeys({}, 'down')
utils.sendKeys({}, 'delete')
end
)
)
self.mode:bind({}, 'y', operators.yank(self))
------------ shortcuts
local paste = function()
utils.sendKeys({'cmd'}, 'v')
end
local undo = function()
utils.sendKeys({'cmd'}, 'z')
self:restoreCursor()
end
local redo = function()
utils.sendKeys({'cmd', 'shift'}, 'z')
self:restoreCursor()
end
local deleteUnderCursor = compose(
operators.delete(self),
isNormalMode(motions.right(self))
)
local searchAhead = function()
utils.sendKeys({'command'}, 'f')
end
local newLineBelow = function()
utils.sendKeys({'command'}, 'right')
self:exit()
utils.sendKeys({}, 'Return')
end
local newLineAbove = function()
utils.sendKeys({'command'}, 'left')
self:exit()
utils.sendKeys({}, 'Return')
utils.sendKeys({}, 'up')
end
self.mode:bind({'shift'}, 'a', compose(motions.endOfLine(self), exit))
self.mode:bind({'shift'}, 'c', compose(operators.change(self), motions.endOfLine(self)))
self.mode:bind({'shift'}, 'i', compose(motions.beginningOfLine(self), exit))
self.mode:bind({'shift'}, 'd', compose(operators.delete(self), motions.endOfLine(self)))
self.mode:bind({}, 's', compose(deleteUnderCursor, exit))
self.mode:bind({}, 'x', deleteUnderCursor)
self.mode:bind({}, 'o', newLineBelow)
self.mode:bind({'shift'}, 'o', newLineAbove)
self.mode:bind({}, 'p', paste)
self.mode:bind({}, 'u', undo)
self.mode:bind({'ctrl'}, 'r', redo)
---------- commands
self.mode:bind({}, '/', searchAhead)
self.mode:bind({}, 'v', function() self:toggleVisualMode() end)
self.mode:bind({}, 'r', extendedModes.replace(self))
end
return VimMode.new()