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')
32 class VimFx extends utils.EventEmitter
33 constructor: (@modes, @options) ->
38 @on('modeChange', ({mode}) => @reset(mode))
40 SPECIAL_KEYS: ['<force>', '<late>']
43 @vims.set(browser, new Vim(browser, this))
45 # NOTE: This method is often called in event handlers. Many events may fire
46 # before a `vim` object has been created for the current tab yet (such as when
47 # the browser is starting up). Therefore always check if anything was
50 # return unless vim = @vimfx.getCurrentVim(@window)
51 getCurrentVim: (window) -> @vims.get(window.gBrowser.selectedBrowser)
53 reset: (mode = null) ->
54 @currentKeyTree = if mode then @keyTrees[mode] else {}
59 {@keyTrees, @errors} = createKeyTrees(@getGroupedCommands(), @SPECIAL_KEYS)
61 stringifyKeyEvent: (event) ->
62 return notation.stringify(event, {
63 ignoreKeyboardLayout: @options.ignore_keyboard_layout
64 translations: @options.translations
67 consumeKeyEvent: (event, vim, focusType) ->
69 return unless keyStr = @stringifyKeyEvent(event)
72 @reset(mode) if now - @lastInputTime >= @options.timeout
75 toplevel = (@currentKeyTree == @keyTrees[mode])
77 if toplevel and @options.keyValidator
78 unless @options.keyValidator(keyStr, mode)
87 when keyStr of @currentKeyTree and
88 not (toplevel and keyStr == '0' and @count != '')
89 next = @currentKeyTree[keyStr]
90 if next instanceof Leaf
92 {command, specialKeys} = next
94 @currentKeyTree = next
97 when toplevel and DIGIT.test(keyStr) and
98 not (keyStr == '0' and @count == '')
105 count = if @count == '' then undefined else Number(@count)
106 focus = @adjustFocusType(event, vim, focusType, keyStr)
107 unmodifiedKey = notation.parse(keyStr).key
108 @reset(mode) if type == 'full'
110 type, focus, command, count, specialKeys, keyStr, unmodifiedKey, toplevel
113 adjustFocusType: (event, vim, focusType, keyStr) ->
114 # Frame scripts and the tests don’t pass in `originalTarget`.
115 document = event.originalTarget?.ownerDocument
116 if focusType == null and document and
117 # TODO: Remove when Tab Groups have been removed.
118 (vim.window.TabView?.isVisible() or
119 document.fullscreenElement or document.mozFullScreenElement)
122 keys = @options["#{focusType}_element_keys"]
123 return null if keys and keyStr not in keys
127 getGroupedCommands: (options = {}) ->
129 for modeName, mode of @modes
130 if options.enabledOnly
131 usedSequences = getUsedSequences(@keyTrees[modeName])
132 for commandName, command of mode.commands
133 enabledSequences = null
134 if options.enabledOnly
135 enabledSequences = utils.removeDuplicates(
136 command._sequences.filter((sequence) ->
137 return (usedSequences[sequence] == command.pref)
140 continue if enabledSequences.length == 0
141 categories = modes[modeName] ?= {}
142 category = categories[command.category] ?= []
144 {command, enabledSequences, order: command.order, name: commandName}
148 for modeName, categories of modes
149 categoriesSorted = []
150 for categoryName, commands of categories
151 category = @options.categories[categoryName]
152 categoriesSorted.push({
153 name: category.name()
155 order: category.order
156 commands: commands.sort(byOrder)
158 mode = @modes[modeName]
163 categories: categoriesSorted.sort(byOrder)
165 return modesSorted.sort(byOrder)
167 byOrder = (a, b) -> a.order - b.order
170 constructor: (@command, @originalSequence, @specialKeys) ->
172 createKeyTrees = (groupedCommands, specialKeyStrings) ->
176 pushError = (error, command) ->
177 (errors[command.pref] ?= []).push(error)
179 pushOverrideErrors = (command, tree) ->
180 {command: overridingCommand, originalSequence} = getFirstLeaf(tree)
183 subject: overridingCommand.description()
184 context: originalSequence
185 pushError(error, command)
187 pushSpecialKeyError = (command, originalSequence, key) ->
189 id: 'illegal_special_key'
191 context: originalSequence
192 pushError(error, command)
194 for mode in groupedCommands
195 keyTrees[mode._name] = {}
196 for category in mode.categories then for {command} in category.commands
197 {shortcuts, errors: parseErrors} = parseShortcutPref(command.pref)
198 pushError(error, command) for error in parseErrors
199 command._sequences = []
201 for shortcut in shortcuts
202 [prefixKeys..., lastKey] = shortcut.normalized
203 tree = keyTrees[mode._name]
204 command._sequences.push(shortcut.original)
205 seenNonSpecialKey = false
209 for prefixKey, index in prefixKeys
210 if prefixKey in specialKeyStrings
212 pushSpecialKeyError(command, shortcut.original, prefixKey)
216 specialKeys[prefixKey] = true
219 seenNonSpecialKey = true
222 next = tree[prefixKey]
223 if next instanceof Leaf
224 pushOverrideErrors(command, next)
230 tree = tree[prefixKey] = {}
233 if lastKey in specialKeyStrings
234 subject = if seenNonSpecialKey then lastKey else shortcut.original
235 pushSpecialKeyError(command, shortcut.original, subject)
238 pushOverrideErrors(command, tree[lastKey])
240 tree[lastKey] = new Leaf(command, shortcut.original, specialKeys)
242 return {keyTrees, errors}
244 parseShortcutPref = (pref) ->
248 # The shorcut prefs are read from root in order to support other extensions to
249 # extend VimFx with custom commands.
250 prefValue = prefs.root.get(pref).trim()
252 unless prefValue == ''
253 for sequence in prefValue.split(/\s+/)
256 for key in notation.parseSequence(sequence)
258 shortcut.push(notation.normalize(key))
260 throw error unless error.id?
264 shortcuts.push({normalized: shortcut, original: sequence}) unless errored
266 return {shortcuts, errors}
268 getFirstLeaf = (node) ->
269 if node instanceof Leaf
271 for key, value of node
272 return getFirstLeaf(value)
274 getLeaves = (node) ->
275 if node instanceof Leaf
278 for key, value of node
279 leaves.push(getLeaves(value)...)
282 getUsedSequences = (tree) ->
284 for leaf in getLeaves(tree)
285 usedSequences[leaf.originalSequence] = leaf.command.pref
288 module.exports = VimFx