4 constructor: ({ @window, @commands, @modes, @escapeCommand }) ->
12 for { name } in @commands
13 @storage.commands[name] = {}
16 @storage.modes[name] = {}
18 enterMode: (mode, args) ->
19 # `args` is an array of arguments to be passed to the mode's `onEnter` method
21 @modes[mode].onEnter?(this, @storage.modes[mode], args)
24 return if @mode == MODE_NORMAL
25 @modes[@mode].onEnterNormalMode?(this, @storage.modes[@mode])
29 onInput: (keyStr, event, options = {}) ->
32 esc = @searchForMatchingCommand([@escapeCommand]).exact
34 if options.autoInsertMode and not esc
37 if @mode == MODE_NORMAL
39 return @runCommand(@escapeCommand, event)
41 { match, exact, command, index } = @searchForMatchingCommand(@commands)
44 @keys = @keys[index..]
45 if exact then @runCommand(command, event)
56 return @modes[@mode].onInput?(this, @storage.modes[@mode], keyStr, event)
58 # Intentionally taking `commands` as a parameter (instead of simply using `@commands`), so that
59 # the method can be reused by custom modes (and by escape handling).
60 searchForMatchingCommand: (commands) ->
61 for index in [0...@keys.length] by 1
62 str = @keys[index..].join(',')
63 for command in commands
64 for key in command.keys()
65 if key.startsWith(str) and command.enabled()
66 return {match: true, exact: (key == str), command, index}
70 runCommand: (command, event) ->
71 command.func(this, @storage.commands[command.name], event)
73 Vim.MODE_NORMAL = MODE_NORMAL
75 # What is minimally required for a command
77 constructor: (@name) ->
78 keys: -> return ['key1', 'key2', 'keyN']
79 enabled: -> return true
80 func: (vim, storage, event) ->