2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014.
5 # This file is part of VimFx.
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
21 utils = require('./utils')
24 constructor: (@window, @parent) ->
25 @rootWindow = utils.getRootWindow(@window) # For convenience.
29 resetState: (force = false) ->
32 lastAutofocusPrevention: null
33 scrollableElements: new WeakMap()
34 lastFocusedTextInput: null
39 @enterMode('normal') if force or @mode == 'ignore'
41 @parent.emit('load', {vim: this, location: @window.location})
44 url = @rootWindow.gBrowser.currentURI.spec
45 return @parent.options.black_list.some((regex) -> regex.test(url))
47 # `args` is an array of arguments to be passed to the mode's `onEnter` method.
48 enterMode: (mode, args...) ->
49 return if @mode == mode
51 unless utils.has(@parent.modes, mode)
52 modes = Object.keys(@parent.modes).join(', ')
53 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
58 @call('onEnter', {args})
59 @parent.emit('modeChange', this) if @parent.currentVim == this
62 match = @parent.consumeKeyEvent(event, this)
63 return null unless match
64 suppress = @call('onInput', {event, count: match.count}, match)
67 call: (method, data = {}, extraArgs...) ->
68 currentMode = @parent.modes[@mode]
69 args = Object.assign({vim: this, storage: @storage[@mode] ?= {}}, data)
70 currentMode?[method].call(currentMode, args, extraArgs...)