2 # Copyright Simon Lydell 2015.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 notation = require('vim-like-key-notation')
21 prefs = require('./prefs')
22 utils = require('./utils')
23 Vim = require('./vim')
28 class VimFx extends utils.EventEmitter
29 constructor: (@modes, @options) ->
31 @vimBucket = new utils.Bucket(((window) => new Vim(window, this)), this)
34 @on('modechange', ({mode}) => @reset(mode))
36 @on('bucket.get', (vim) =>
37 return if @currentVim == vim
39 @emit('currentVimChange', vim)
41 @customCommandCounter = 0
43 reset: (mode = null) ->
44 @currentKeyTree = if mode then @keyTrees[mode] else {}
48 getCurrentLocation: -> @currentVim?.window.location
51 {@keyTrees, @forceCommands, @errors} = createKeyTrees(@modes)
53 stringifyKeyEvent: (event) ->
54 return notation.stringify(event, {
55 ignoreKeyboardLayout: @options.ignore_keyboard_layout
56 translations: @options.translations
59 consumeKeyEvent: (event, mode) ->
60 return unless keyStr = @stringifyKeyEvent(event)
63 @reset(mode) if now - @lastInputTime >= @options.timeout
66 toplevel = (@currentKeyTree == @keyTrees[mode])
68 if toplevel and @options.keyValidator
69 unless @options.keyValidator(keyStr, mode)
77 when keyStr of @currentKeyTree
78 next = @currentKeyTree[keyStr]
79 if next instanceof Leaf
81 command = next.command
83 @currentKeyTree = next
86 when toplevel and DIGIT.test(keyStr) and
87 not (keyStr == '0' and @count == '')
94 count = if @count == '' then undefined else Number(@count)
95 force = if command then (command.pref of @forceCommands) else false
96 @reset(mode) if type == 'full'
97 return {type, command, count, force, keyStr}
99 getGroupedCommands: (options = {}) ->
101 for modeName, mode of @modes
102 if options.enabledOnly
103 usedSequences = getUsedSequences(@keyTrees[modeName])
104 for commandName, command of mode.commands
105 enabledSequences = null
106 if options.enabledOnly
107 enabledSequences = utils.removeDuplicates(
108 command._sequences.filter((sequence) ->
109 return (usedSequences[sequence] == command.pref)
112 continue if enabledSequences.length == 0
113 categories = modes[modeName] ?= {}
114 category = categories[command.category] ?= []
115 category.push({command, enabledSequences, order: command.order})
118 for modeName, categories of modes
119 categoriesSorted = []
120 for categoryName, commands of categories
121 category = @options.categories[categoryName]
122 categoriesSorted.push({
123 name: category.name()
125 order: category.order
126 commands: commands.sort(byOrder)
128 mode = @modes[modeName]
133 categories: categoriesSorted.sort(byOrder)
135 return modesSorted.sort(byOrder)
137 byOrder = (a, b) -> a.order - b.order
140 constructor: (@command, @originalSequence) ->
142 createKeyTrees = (modes) ->
147 pushError = (error, command) ->
148 (errors[command.pref] ?= []).push(error)
150 pushOverrideErrors = (command, tree) ->
151 for { command: overriddenCommand, originalSequence } in getLeaves(tree)
154 subject: command.description()
155 context: originalSequence
156 pushError(error, overriddenCommand)
159 pushForceKeyError = (command, originalSequence) ->
161 id: 'illegal_force_key'
163 context: originalSequence
164 pushError(error, command)
166 for modeName, { commands } of modes
167 keyTrees[modeName] = {}
168 for commandName, command of commands
169 { shortcuts, errors: parseErrors } = parseShortcutPref(command.pref)
170 pushError(error, command) for error in parseErrors
171 command._sequences = []
173 for shortcut in shortcuts
174 [ prefixKeys..., lastKey ] = shortcut.normalized
175 tree = keyTrees[modeName]
176 command._sequences.push(shortcut.original)
178 for prefixKey, index in prefixKeys
179 if prefixKey == FORCE_KEY
181 forceCommands[command.pref] = true
183 pushForceKeyError(command, shortcut.original)
187 next = tree[prefixKey]
188 if next instanceof Leaf
189 pushOverrideErrors(command, next)
190 tree = tree[prefixKey] = {}
194 tree = tree[prefixKey] = {}
196 if lastKey == FORCE_KEY
197 pushForceKeyError(command, shortcut.original)
200 pushOverrideErrors(command, tree[lastKey])
201 tree[lastKey] = new Leaf(command, shortcut.original)
203 return {keyTrees, forceCommands, errors}
205 parseShortcutPref = (pref) ->
209 # The shorcut prefs are read from root in order to support other extensions to
210 # extend VimFx with custom commands.
211 prefValue = prefs.root.get(pref).trim()
213 unless prefValue == ''
214 for sequence in prefValue.split(/\s+/)
217 for key in notation.parseSequence(sequence)
219 shortcut.push(notation.normalize(key))
221 throw error unless error.id?
225 shortcuts.push({normalized: shortcut, original: sequence}) unless errored
227 return {shortcuts, errors}
229 getLeaves = (node) ->
230 if node instanceof Leaf
233 for key, value of node
234 leaves.push(getLeaves(value)...)
237 getUsedSequences = (tree) ->
239 for leaf in getLeaves(tree)
240 usedSequences[leaf.originalSequence] = leaf.command.pref
243 module.exports = VimFx