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