]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
closes #41. All help commands can be disabled now.
[VimFx.git] / extension / packages / vim.coffee
1 utils = require 'utils'
2
3 { commands
4 , hintCharHandler
5 , findCharHandler
6 } = require 'commands'
7
8 { getPref
9 , isCommandDisabled
10 } = require 'prefs'
11
12
13 MODE_NORMAL = 1
14 MODE_HINTS = 2
15 MODE_FIND = 3
16
17 class Vim
18 constructor: (@window) ->
19 @mode = MODE_NORMAL
20 @keys = []
21 @markers = undefined
22 @cb = undefined
23 @findStr = ""
24
25 enterFindMode: ->
26 @mode = MODE_FIND
27
28 enterHintsMode: (@markers, @cb) ->
29 @mode = MODE_HINTS
30
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()
41 result = hintChars.search(regexpEscape(keyStr)) > -1
42 else if @mode == MODE_FIND
43 result = true
44
45 if result then @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 else if @mode == MODE_FIND
62 findCharHandler @, lastKeyStr, keyboardEvent.charCode
63 result = true
64
65 return result
66
67 findCommand = (keys) ->
68 for i in [0...keys.length]
69 seq = keys.slice(i).join(',')
70 if com = commands[seq]
71 if not isCommandDisabled(seq)
72 return com
73
74 return undefined
75
76 maybeCommand = (keys) ->
77 for i in [0...keys.length]
78 sequence = keys.slice(i).join(',')
79 for seq, com of commands
80 if seq.indexOf(sequence) == 0
81 return not isCommandDisabled(seq)
82
83 return false
84
85 exports.Vim = Vim
Imprint / Impressum