]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
Merge branch 'release-0.4.8'
[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 result = utils.getHintChars().search(regexpEscape(keyStr)) > -1
40
41 if result
42 @lastKeyStr = keyStr
43 @keys.push keyStr
44
45 return result
46
47 handleKeyPress: (keyboardEvent) ->
48 lastKeyStr = if @keys.length > 0 then @keys[@keys.length - 1] else undefined
49 if @mode == MODE_NORMAL or lastKeyStr == 'Esc'
50 if command = findCommand @keys
51 command @
52 @keys.length = 0
53 result = true
54 else if !keyboardEvent.ctrlKey and !keyboardEvent.metaKey
55 @keys.length = 0
56 if @mode == MODE_HINTS
57 hintCharHandler @, lastKeyStr, keyboardEvent.charCode
58 result = true
59
60 return result
61
62 findCommand = (keys) ->
63 for i in [0...keys.length]
64 seq = keys.slice(i).join(',')
65 if com = commands[seq]
66 if not isCommandDisabled(seq)
67 return com
68
69 return undefined
70
71 maybeCommand = (keys) ->
72 for i in [0...keys.length]
73 sequence = keys.slice(i).join(',')
74 for seq, com of commands
75 if seq.indexOf(sequence) == 0
76 return not isCommandDisabled(seq)
77
78 return false
79
80 exports.Vim = Vim
Imprint / Impressum