]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
Merge pull request #94 from LordJZ/develop-embed
[VimFx.git] / extension / packages / vim.coffee
1 utils = require 'utils'
2
3 { commands
4 , hintCharHandler
5 } = require 'commands'
6
7 { getPref
8 , isCommandDisabled
9 } = require 'prefs'
10
11
12 MODE_NORMAL = 1
13 MODE_HINTS = 2
14
15 class Vim
16 constructor: (@window) ->
17 @mode = MODE_NORMAL
18 @keys = []
19 @lastKeyStr = null
20 @markers = undefined
21 @cb = undefined
22 @findStr = ""
23 @findRng = null
24
25 enterHintsMode: (@markers, @cb) ->
26 @mode = MODE_HINTS
27
28 # TODO: This function should probably remove
29 # hint markers (if they are present) as well
30 enterNormalMode: ->
31 @mode = MODE_NORMAL
32 @markers = @cb = undefined
33
34 handleKeyDown: (keyboardEvent, keyStr) ->
35 if @mode == MODE_NORMAL || keyStr == 'Esc'
36 result = maybeCommand @keys.concat([keyStr])
37 else if !keyboardEvent.ctrlKey and !keyboardEvent.metaKey
38 if @mode == MODE_HINTS
39 hintChars = getPref('hint_chars').toLowerCase()
40 result = hintChars.search(regexpEscape(keyStr)) > -1
41
42 if result
43 @lastKeyStr = keyStr
44 @keys.push keyStr
45
46 return result
47
48 handleKeyPress: (keyboardEvent) ->
49 lastKeyStr = if @keys.length > 0 then @keys[@keys.length - 1] else undefined
50 if @mode == MODE_NORMAL or lastKeyStr == 'Esc'
51 if command = findCommand @keys
52 command @
53 @keys.length = 0
54 result = true
55 else if !keyboardEvent.ctrlKey and !keyboardEvent.metaKey
56 @keys.length = 0
57 if @mode == MODE_HINTS
58 hintCharHandler @, lastKeyStr, keyboardEvent.charCode
59 result = true
60
61 return result
62
63 findCommand = (keys) ->
64 for i in [0...keys.length]
65 seq = keys.slice(i).join(',')
66 if com = commands[seq]
67 if not isCommandDisabled(seq)
68 return com
69
70 return undefined
71
72 maybeCommand = (keys) ->
73 for i in [0...keys.length]
74 sequence = keys.slice(i).join(',')
75 for seq, com of commands
76 if seq.indexOf(sequence) == 0
77 return not isCommandDisabled(seq)
78
79 return false
80
81 exports.Vim = Vim
Imprint / Impressum