]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
Update Esc-related locale text and documentation
[VimFx.git] / extension / packages / vim.coffee
1 MODE_NORMAL = {}
2
3 class Vim
4 constructor: ({ @window, @commands, @modes, @escapeCommand }) ->
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 # `args` is an array of arguments to be passed to the mode's `onEnter` method
20 @mode = mode
21 @modes[mode].onEnter?(this, @storage.modes[mode], args)
22
23 enterNormalMode: ->
24 return if @mode == MODE_NORMAL
25 @modes[@mode].onEnterNormalMode?(this, @storage.modes[@mode])
26 @mode = MODE_NORMAL
27 @keys.length = 0
28
29 onInput: (keyStr, event, options = {}) ->
30 @keys.push(keyStr)
31
32 esc = @searchForMatchingCommand([@escapeCommand]).exact
33
34 if options.autoInsertMode and not esc
35 return false
36
37 if @mode == MODE_NORMAL
38 if esc
39 return @runCommand(@escapeCommand, event)
40
41 { match, exact, command, index } = @searchForMatchingCommand(@commands)
42
43 if match
44 @keys = @keys[index..]
45 if exact then @runCommand(command, event)
46 return true
47 else
48 @keys.length = 0
49 return false
50
51 else
52 if esc
53 @enterNormalMode()
54 return true
55 else
56 return @modes[@mode].onInput?(this, @storage.modes[@mode], keyStr, event)
57
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}
67
68 return {match: false}
69
70 runCommand: (command, event) ->
71 command.func(this, @storage.commands[command.name], event)
72
73 Vim.MODE_NORMAL = MODE_NORMAL
74
75 # What is minimally required for a command
76 class Vim.Command
77 constructor: (@name) ->
78 keys: -> return ['key1', 'key2', 'keyN']
79 enabled: -> return true
80 func: (vim, storage, event) ->
81
82 exports.Vim = Vim
Imprint / Impressum