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 # This file defines a top-level object to hold global state for VimFx. It keeps
21 # track of all `Vim` instances (vim.coffee), all options and all keyboard
22 # shortcuts. It can consume key presses according to its commands, and return
23 # the commands for UI presentation. There is only one `VimFx` instance.
25 notation = require('vim-like-key-notation')
26 prefs = require('./prefs')
27 utils = require('./utils')
28 Vim = require('./vim')
33 class VimFx extends utils.EventEmitter
34 constructor: (@modes, @options) ->
39 @on('modechange', ({mode}) => @reset(mode))
42 @vims.set(browser, new Vim(browser, this))
44 getCurrentVim: (window) -> @vims.get(window.gBrowser.selectedBrowser)
46 reset: (mode = null) ->
47 @currentKeyTree = if mode then @keyTrees[mode] else {}
52 {@keyTrees, @forceCommands, @errors} = createKeyTrees(@getGroupedCommands())
54 stringifyKeyEvent: (event) ->
55 return notation.stringify(event, {
56 ignoreKeyboardLayout: @options.ignore_keyboard_layout
57 translations: @options.translations
60 consumeKeyEvent: (event, vim, focusType) ->
62 return unless keyStr = @stringifyKeyEvent(event)
65 @reset(mode) if now - @lastInputTime >= @options.timeout
68 toplevel = (@currentKeyTree == @keyTrees[mode])
70 if toplevel and @options.keyValidator
71 unless @options.keyValidator(keyStr, mode)
79 when toplevel and DIGIT.test(keyStr) and
80 not (keyStr == '0' and @count == '')
84 when keyStr of @currentKeyTree
85 next = @currentKeyTree[keyStr]
86 if next instanceof Leaf
88 command = next.command
90 @currentKeyTree = next
96 count = if @count == '' then undefined else Number(@count)
97 force = if command then (command.pref of @forceCommands) else false
98 focus = @adjustFocusType(event, vim, focusType, keyStr)
99 unmodifiedKey = notation.parse(keyStr).key
100 @reset(mode) if type == 'full'
101 return {type, focus, command, count, force, keyStr, unmodifiedKey, toplevel}
103 adjustFocusType: (event, vim, focusType, keyStr) ->
104 # Frame scripts and the tests don’t pass in `originalTarget`.
105 document = event.originalTarget?.ownerDocument
106 if focusType == null and document and
107 (vim.window.TabView.isVisible() or
108 document.fullscreenElement or document.mozFullScreenElement)
111 keys = @options["#{ focusType }_element_keys"]
112 return null if keys and keyStr not in keys
116 getGroupedCommands: (options = {}) ->
118 for modeName, mode of @modes
119 if options.enabledOnly
120 usedSequences = getUsedSequences(@keyTrees[modeName])
121 for commandName, command of mode.commands
122 enabledSequences = null
123 if options.enabledOnly
124 enabledSequences = utils.removeDuplicates(
125 command._sequences.filter((sequence) ->
126 return (usedSequences[sequence] == command.pref)
129 continue if enabledSequences.length == 0
130 categories = modes[modeName] ?= {}
131 category = categories[command.category] ?= []
132 category.push({command, enabledSequences, order: command.order})
135 for modeName, categories of modes
136 categoriesSorted = []
137 for categoryName, commands of categories
138 category = @options.categories[categoryName]
139 categoriesSorted.push({
140 name: category.name()
142 order: category.order
143 commands: commands.sort(byOrder)
145 mode = @modes[modeName]
150 categories: categoriesSorted.sort(byOrder)
152 return modesSorted.sort(byOrder)
154 byOrder = (a, b) -> a.order - b.order
157 constructor: (@command, @originalSequence) ->
159 createKeyTrees = (groupedCommands) ->
164 pushError = (error, command) ->
165 (errors[command.pref] ?= []).push(error)
167 pushOverrideErrors = (command, tree) ->
168 { command: overridingCommand, originalSequence } = getFirstLeaf(tree)
171 subject: overridingCommand.description()
172 context: originalSequence
173 pushError(error, command)
175 pushForceKeyError = (command, originalSequence) ->
177 id: 'illegal_force_key'
179 context: originalSequence
180 pushError(error, command)
182 for mode in groupedCommands
183 keyTrees[mode._name] = {}
184 for category in mode.categories then for { command } in category.commands
185 { shortcuts, errors: parseErrors } = parseShortcutPref(command.pref)
186 pushError(error, command) for error in parseErrors
187 command._sequences = []
189 for shortcut in shortcuts
190 [ prefixKeys..., lastKey ] = shortcut.normalized
191 tree = keyTrees[mode._name]
192 command._sequences.push(shortcut.original)
195 for prefixKey, index in prefixKeys
196 if prefixKey == FORCE_KEY
198 forceCommands[command.pref] = true
201 pushForceKeyError(command, shortcut.original)
206 next = tree[prefixKey]
207 if next instanceof Leaf
208 pushOverrideErrors(command, next)
214 tree = tree[prefixKey] = {}
217 if lastKey == FORCE_KEY
218 pushForceKeyError(command, shortcut.original)
221 pushOverrideErrors(command, tree[lastKey])
223 tree[lastKey] = new Leaf(command, shortcut.original)
225 return {keyTrees, forceCommands, errors}
227 parseShortcutPref = (pref) ->
231 # The shorcut prefs are read from root in order to support other extensions to
232 # extend VimFx with custom commands.
233 prefValue = prefs.root.get(pref).trim()
235 unless prefValue == ''
236 for sequence in prefValue.split(/\s+/)
239 for key in notation.parseSequence(sequence)
241 shortcut.push(notation.normalize(key))
243 throw error unless error.id?
247 shortcuts.push({normalized: shortcut, original: sequence}) unless errored
249 return {shortcuts, errors}
251 getFirstLeaf = (node) ->
252 if node instanceof Leaf
254 for key, value of node
255 return getFirstLeaf(value)
257 getLeaves = (node) ->
258 if node instanceof Leaf
261 for key, value of node
262 leaves.push(getLeaves(value)...)
265 getUsedSequences = (tree) ->
267 for leaf in getLeaves(tree)
268 usedSequences[leaf.originalSequence] = leaf.command.pref
271 module.exports = VimFx