4 constructor: ({ @window, @commands, @modes, @esc }) ->
12 for { name } in @commands
13 @storage.commands[name] = {}
16 @storage.modes[name] = {}
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.
22 @modes[mode].onEnter?(this, @storage.modes[mode], args)
25 return if @mode == MODE_NORMAL
26 @modes[@mode].onEnterNormalMode?(this, @storage.modes[@mode])
30 onInput: (keyStr, event) ->
33 if @mode == MODE_NORMAL
34 { match, exact, command } = @searchForCommand(@commands)
37 if exact then command.func(this, @storage.commands[command.name], event)
47 return @modes[@mode].onInput?(this, @storage.modes[@mode], keyStr, event)
49 # Intentionally taking `commands` as a parameter (instead of simply using `@commands`), so that
50 # the method can be reused by custom modes.
51 searchForCommand: (commands) ->
52 for index in [0...@keys.length] by 1
53 str = @keys[index..].join(',')
54 for command in commands
55 for key in command.keys()
56 if key.startsWith(str) and command.enabled()
57 @keys = @keys[index..]
58 return {match: true, exact: (key == str), command}
63 Vim.MODE_NORMAL = MODE_NORMAL
65 # What is minimally required for a command
67 constructor: (@name) ->
68 keys: -> return ['key1', 'key2', 'keyN']
69 enabled: -> return true
70 func: (vim, storage, event) ->