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