]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Prevent autofocus after re-selecting a tab
[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 # This file defines the `vim` API, available to all modes and commands. There is
22 # one `Vim` instance for each tab. Most importantly, it provides access to the
23 # owning Firefox window and the current mode, and allows you to change mode.
24 # `vim` objects are exposed by the Public API. Underscored names are private and
25 # should not be used by API consumers.
26
27 messageManager = require('./message-manager')
28 utils = require('./utils')
29
30 class Vim
31 constructor: (@browser, @_parent) ->
32 @window = @browser.ownerGlobal
33 @_messageManager = @browser.messageManager
34 @_storage = {}
35
36 Object.defineProperty(this, 'options', {
37 get: => @_parent.options
38 enumerable: true
39 })
40
41 location = utils.getCurrentLocation(@browser)
42 @enterMode(if @isBlacklisted(location.href) then 'ignore' else 'normal')
43 @_parent.emit('load', {vim: this, location})
44
45 # Require the subset of the options needed to be listed explicitly (as
46 # opposed to sending _all_ options) for performance. Each option access
47 # might trigger an optionOverride.
48 @_listen('options', ({ prefs }) =>
49 options = {}
50 for pref in prefs
51 options[pref] = @options[pref]
52 return options
53 )
54
55 @_listen('vimMethod', ({ method, args = [] }, { callback = null }) =>
56 result = @[method](args...)
57 @_send(callback, result) if callback
58 )
59
60 @_listen('vimMethodSync', ({ method, args = [] }) =>
61 return @[method](args...)
62 )
63
64 isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
65
66 isFrameEvent: (event) ->
67 return (event.originalTarget == @window.gBrowser.selectedBrowser)
68
69 isCurrent: -> @_parent.getCurrentVim(utils.getCurrentWindow()) == this
70
71 # `args` is an array of arguments to be passed to the mode's `onEnter` method.
72 enterMode: (mode, args...) ->
73 return false if @mode == mode
74
75 unless utils.has(@_parent.modes, mode)
76 modes = Object.keys(@_parent.modes).join(', ')
77 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
78 Got: #{ mode }")
79
80 @_call('onLeave') if @mode?
81 @mode = mode
82 @_call('onEnter', null, args...)
83 @_parent.emit('modeChange', this) if utils.getCurrentWindow() == @window
84 @_send('modeChange', {mode})
85 return true
86
87 _onInput: (event, focusType, { isFrameEvent = false } = {}) ->
88 match = @_parent.consumeKeyEvent(event, this, focusType)
89 return null unless match
90 suppress = @_call('onInput', {isFrameEvent, count: match.count}, match)
91 return suppress
92
93 _call: (method, data = {}, extraArgs...) ->
94 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
95 currentMode = @_parent.modes[@mode]
96 currentMode[method].call(currentMode, args, extraArgs...)
97
98 _run: (name, data = {}, callback = null) ->
99 @_send('runCommand', {name, data}, callback)
100
101 _listen: (name, listener) ->
102 messageManager.listen(name, listener, @_messageManager)
103
104 _listenOnce: (name, listener) ->
105 messageManager.listenOnce(name, listener, @_messageManager)
106
107 _send: (name, data, callback = null) ->
108 messageManager.send(name, data, @_messageManager, callback)
109
110 notify: (title, options = {}) ->
111 new @window.Notification(title, Object.assign({
112 icon: 'chrome://vimfx/skin/icon128.png'
113 tag: 'VimFx-notification'
114 }, options))
115
116 markPageInteraction: ->
117 @_send('markPageInteraction')
118
119 _focusMarkerElement: (elementIndex, options = {}) ->
120 # If you, for example, focus the location bar, unfocus it by pressing
121 # `<esc>` and then try to focus a link or text input in a web page the focus
122 # won’t work unless `@browser` is focused first.
123 @browser.focus()
124 @_run('focus_marker_element', {elementIndex, options})
125
126 module.exports = Vim
Imprint / Impressum