]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
Fix: Proper modes support
[VimFx.git] / extension / packages / vim.coffee
1 MODE_NORMAL = {}
2
3 class Vim
4 constructor: ({ @window, @commands, @modes, @esc }) ->
5 @mode = MODE_NORMAL
6 @keys = []
7 @lastKeyStr = null
8 @suppress = false
9
10 @storage =
11 commands: {}
12 modes: {}
13
14 for { name } in @commands
15 @storage.commands[name] = {}
16
17 for name of @modes
18 @storage.modes[name] = {}
19
20 enterMode: (mode, args...) ->
21 @mode = mode
22 @modes[mode].enter(this, @storage.modes[mode], args...)
23
24 enterNormalMode: ->
25 for name, mode of @modes
26 mode.onEnterNormalMode?(this, @storage.modes[name])
27 @mode = MODE_NORMAL
28
29 handleKeyDown: (event, keyStr) ->
30 @suppress = true
31 if @mode == MODE_NORMAL or keyStr == @esc
32 @keys.push(keyStr)
33 @lastKeyStr = keyStr
34 if command = @findCommand(@keys)
35 command.func(this, @storage.commands[command.name])
36 return command.name != @esc
37 else if @maybeCommand(@keys)
38 return true
39 else
40 @keys.length = 0
41 else if not (event.ctrlKey or event.metaKey)
42 @modes[@mode].handleKeyDown(this, @storage.modes[@mode], event, keyStr)
43
44 @suppress = false
45 @keys.length = 0
46 return false
47
48 handleKeyPress: (event) ->
49 return @lastKeyStr != @esc and @suppress
50
51 handleKeyUp: (event) ->
52 suppress = @suppress
53 @suppress = false
54 return @lastKeyStr != @esc and suppress
55
56 findCommand: (keys) ->
57 for i in [0...keys.length]
58 str = keys[i..].join(',')
59 for command in @commands
60 for key in command.keys()
61 if key == str and command.enabled()
62 return command
63
64 maybeCommand: (keys) ->
65 for i in [0...keys.length]
66 str = keys[i..].join(',')
67 for command in @commands
68 for key in command.keys()
69 if key.indexOf(str) == 0 and command.enabled()
70 return true
71
72 # What is minimally required for a command
73 class Vim.Command
74 constructor: (@keyValues, @name) ->
75 keys: -> return @keyValues
76 enabled: -> return true
77
78 exports.Vim = Vim
Imprint / Impressum