]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
small fix for non-english keyboard layouts
[VimFx.git] / extension / packages / vim.coffee
1 utils = require 'utils'
2
3 { commands
4 , hintCharHandler
5 , findCharHandler
6 } = require 'commands'
7
8 { getPref } = require 'prefs'
9
10
11 MODE_NORMAL = 1
12 MODE_HINTS = 2
13 MODE_FIND = 3
14
15 class Vim
16 constructor: (@window) ->
17 @mode = MODE_NORMAL
18 @keys = []
19 @markers = undefined
20 @cb = undefined
21 @findStr = ""
22
23 enterFindMode: ->
24 @mode = MODE_FIND
25
26 enterHintsMode: (@markers, @cb) ->
27 @mode = MODE_HINTS
28
29 enterNormalMode: ->
30 @mode = MODE_NORMAL
31 @markers = @cb = undefined
32
33 pushKey: (keyStr, keyCode) ->
34 if _maybeCommand(@mode, @keys, keyStr, keyCode)
35 @keys.push keyStr
36 return true
37
38 return false
39
40 execKeys: (charCode) ->
41 if command = _getCommand @mode, @keys, charCode
42 lastKey = @keys[@keys.length - 1]
43 command @
44 @keys = []
45 return lastKey != 'Esc'
46
47 handleKeyDown: (keyboardEvent, keyStr) ->
48 if @mode == MODE_NORMAL || keyStr == 'Esc'
49 result = maybeCommand @keys.concat([keyStr])
50 else if !keyboardEvent.ctrlKey and !keyboardEvent.metaKey
51 if @mode == MODE_HINTS
52 hintChars = getPref('hint_chars').toLowerCase()
53 result = hintChars.search(utils.regexpEscape(keyStr)) > -1
54 else if @mode == MODE_FIND
55 result = true
56
57 if result then @keys.push keyStr
58
59 return result
60
61 handleKeyPress: (keyboardEvent) ->
62 lastKeyStr = if @keys.length > 0 then @keys[@keys.length - 1] else undefined
63 if @mode == MODE_NORMAL or lastKeyStr == 'Esc'
64 if command = findCommand @keys
65 command @
66 result = lastKeyStr != 'Esc'
67 else if !keyboardEvent.ctrlKey and !keyboardEvent.metaKey
68 @keys.length = 0
69 if @mode == MODE_HINTS
70 hintCharHandler @, lastKeyStr, keyboardEvent.charCode
71 result = true
72 else if @mode == MODE_FIND
73 findCharHandler @, lastKeyStr, keyboardEvent.charCode
74 result = true
75
76 if result then @keys.length = 0
77
78 return result
79
80 findCommand = (keys) ->
81 for i in [0...keys.length]
82 if com = commands[keys.slice(i).join(',')]
83 return com
84
85 return undefined
86
87 maybeCommand = (keys) ->
88 for i in [0...keys.length]
89 sequence = keys.slice(i).join(',')
90 for seq, com of commands
91 if seq.indexOf(sequence) == 0
92 return true
93
94 return false
95
96 exports.Vim = Vim
Imprint / Impressum