]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Add the `<s-f1>` Ignore mode "unquote" command
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
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.
11 #
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.
16 #
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/>.
19 ###
20
21 utils = require('./utils')
22
23 class Vim
24 constructor: (@window, @parent) ->
25 @rootWindow = utils.getRootWindow(@window) # For convenience.
26 @storage = {}
27 @resetState(true)
28
29 resetState: (force = false) ->
30 @state =
31 lastInteraction: null
32 lastAutofocusPrevention: null
33 scrollableElements: new WeakMap()
34 lastFocusedTextInput: null
35
36 if @isBlacklisted()
37 @enterMode('ignore')
38 else
39 @enterMode('normal') if force or @mode == 'ignore'
40
41 @parent.emit('load', {vim: this, location: @window.location})
42
43 isBlacklisted: ->
44 url = @rootWindow.gBrowser.currentURI.spec
45 return @parent.options.black_list.some((regex) -> regex.test(url))
46
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
50
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 }.
54 Got: #{ mode }")
55
56 @call('onLeave')
57 @mode = mode
58 @call('onEnter', {args})
59 @parent.emit('modeChange', this) if @parent.currentVim == this
60
61 onInput: (event) ->
62 match = @parent.consumeKeyEvent(event, this)
63 return null unless match
64 suppress = @call('onInput', {event, count: match.count}, match)
65 return suppress
66
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...)
71
72 module.exports = Vim
Imprint / Impressum