]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
Merge branch 'develop' into proper-modes
[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
8 @storage =
9 commands: {}
10 modes: {}
11
12 for { name } in @commands
13 @storage.commands[name] = {}
14
15 for name of @modes
16 @storage.modes[name] = {}
17
18 enterMode: (mode, args) ->
19 # Note: `args` is an array of arguments to be passed to the mode's `onEnter` method. We cannot
20 # use `args...`, since that destroys the `this` context for the mode's `onEnter` method.
21 @mode = mode
22 @modes[mode].onEnter?(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 onInput: (keyStr, event) ->
30 @keys.push(keyStr)
31
32 if @mode == MODE_NORMAL
33 { match, exact, command, index } = @searchForCommand(@commands)
34
35 if match
36 if exact then command.func(this, @storage.commands[command.name], event)
37 return true
38 else
39 return false
40
41 else
42 if keyStr == @esc
43 @enterNormalMode()
44 return true
45 else
46 return @modes[@mode].onInput?(this, @storage.modes[@mode], keyStr, event)
47
48 # Intentionally taking `commands` as a parameter (instead of simply using `@commands`), so that
49 # the method can be reused by custom modes.
50 searchForCommand: (commands) ->
51 for index in [0...@keys.length] by 1
52 str = @keys[index..].join(',')
53 for command in commands
54 for key in command.keys()
55 if key.startsWith(str) and command.enabled()
56 @keys = @keys[index..]
57 return {match: true, exact: (key == str), command}
58
59 @keys.length = 0
60 return {match: false}
61
62 Vim.MODE_NORMAL = MODE_NORMAL
63
64 # What is minimally required for a command
65 class Vim.Command
66 constructor: (@name) ->
67 keys: -> return ['key1', 'key2', 'keyN']
68 enabled: -> return true
69 func: (vim, storage, event) ->
70
71 exports.Vim = Vim
Imprint / Impressum