]> git.gir.st - VimFx.git/blob - packages/vim.coffee
Event handling changed to accumulate keys during keydown, and execute them during...
[VimFx.git] / packages / vim.coffee
1 { getWindowId, Bucket } = require 'utils'
2
3 { commands,
4 hintCharHandler
5 } = require 'commands'
6
7 MODE_NORMAL = 1
8 MODE_HINTS = 2
9
10 class Vim
11 constructor: (@window) ->
12 @mode = MODE_NORMAL
13 @keys = []
14 @markers = undefined
15 @cb = undefined
16
17 pushKey: (keyStr) ->
18 if _maybeCommand(@mode, @keys, keyStr)
19 @keys.push keyStr
20 return true
21
22 return false
23
24 execKeys: ->
25 if command = _getCommand(@mode, @keys)
26 lastKey = @keys[@keys.length - 1]
27 command @
28 @keys = []
29 return lastKey != 'Esc'
30
31 enterHintsMode: () ->
32 @mode = MODE_HINTS
33
34 enterNormalMode: () ->
35 @markers = @cb = undefined
36
37 @mode = MODE_NORMAL
38
39 _getCommand = (mode, keys) ->
40 lastKey = keys[keys.length - 1]
41
42 if mode == MODE_NORMAL or lastKey == 'Esc'
43 sequence = keys.join(',')
44 if command = commands[sequence]
45 return command
46 else if keys.length > 0
47 return _getCommand mode, keys.slice(1)
48
49 else if mode == MODE_HINTS and keys.length > 0
50 return (vim) =>
51 return hintCharHandler(vim, lastKey.toLowerCase())
52
53 return undefined
54
55 _maybeCommand = (mode, keys, keyStr) ->
56 if mode == MODE_NORMAL || keyStr == 'Esc'
57 sequence = keys.concat([keyStr]).join(',')
58 for commandSequence in Object.keys(commands)
59 if commandSequence.search(sequence) == 0
60 return true
61
62 if keys.length > 0
63 return _maybeCommand mode, keys.slice(1), keyStr
64
65 else if mode == MODE_HINTS
66 return true
67
68 return false
69
70 exports.Vim = Vim
Imprint / Impressum