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 for name, mode of @modes
26 mode.onEnterNormalMode?(this, @storage.modes[name])
29 onInput: (keyStr, event) ->
32 if @mode == MODE_NORMAL
33 { match, exact, command, index } = @searchForCommand(@commands)
36 if exact then command.func(this, @storage.commands[command.name], event)
46 return @modes[@mode].onInput?(this, @storage.modes[@mode], keyStr, event)
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}
62 Vim.MODE_NORMAL = MODE_NORMAL
64 # What is minimally required for a command
66 constructor: (@name) ->
67 keys: -> return ['key1', 'key2', 'keyN']
68 enabled: -> return true
69 func: (vim, storage, event) ->