2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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 # 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.
27 messageManager = require('./message-manager')
28 utils = require('./utils')
30 ChromeWindow = Ci.nsIDOMChromeWindow
33 constructor: (browser, @_parent) ->
39 Object.defineProperty(this, 'options', {
40 get: => @_parent.options
44 # Since this is done in the constructor, defer location change handling to
45 # the next tick so that this `vim` instance is saved in `vimfx.vims` first.
46 # This allows 'locationChange' listeners to use `@vimfx.getCurrentVim()`.
47 utils.nextTick(@window, =>
48 @_onLocationChange(@browser.currentURI.spec)
51 # Require the subset of the options needed to be listed explicitly (as
52 # opposed to sending _all_ options) for performance. Each option access
53 # might trigger an optionOverride.
54 @_listen('options', ({ prefs }) =>
57 options[pref] = @options[pref]
61 @_listen('vimMethod', ({ method, args = [] }, { callback = null }) =>
62 result = @[method](args...)
63 @_send(callback, result) if callback
66 @_listen('vimMethodSync', ({ method, args = [] }) =>
67 return @[method](args...)
70 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
72 _setBrowser: (@browser) ->
73 @window = @browser.ownerGlobal
74 @_messageManager = @browser.messageManager
78 frameCanReceiveEvents: false
81 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
84 target = event.originalTarget
85 return @_state.frameCanReceiveEvents and
86 if MULTI_PROCESS_ENABLED
87 (target != @window.gBrowser.selectedBrowser)
89 (target.ownerGlobal instanceof ChromeWindow)
91 # `args...` is passed to the mode's `onEnter` method.
92 enterMode: (mode, args...) ->
93 return if @mode == mode
95 unless utils.has(@_parent.modes, mode)
96 modes = Object.keys(@_parent.modes).join(', ')
97 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
100 @_call('onLeave') if @mode?
102 result = @_call('onEnter', null, args...)
103 @_parent.emit('modeChange', this)
104 @_send('modeChange', {mode})
107 _consumeKeyEvent: (event, focusType) ->
108 return @_parent.consumeKeyEvent(event, this, focusType)
110 _onInput: (match, uiEvent = false) ->
111 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
114 _onLocationChange: (url, { refresh = false } = {}) ->
115 return if url == @_state.lastUrl and not refresh
116 @_state.lastUrl = url
117 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
118 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
119 @_send('locationChange')
121 _call: (method, data = {}, extraArgs...) ->
122 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
123 currentMode = @_parent.modes[@mode]
124 return currentMode[method].call(currentMode, args, extraArgs...)
126 _run: (name, data = {}, callback = null) ->
127 @_send('runCommand', {name, data}, callback)
129 _listen: (name, listener) ->
130 messageManager.listen(name, listener, @_messageManager)
132 _listenOnce: (name, listener) ->
133 messageManager.listenOnce(name, listener, @_messageManager)
135 _send: (name, data, callback = null) ->
136 messageManager.send(name, data, @_messageManager, callback)
138 notify: (title, options = {}) ->
139 new @window.Notification(title, Object.assign({
140 icon: 'chrome://vimfx/skin/icon128.png'
141 tag: 'VimFx-notification'
144 markPageInteraction: ->
145 @_send('markPageInteraction')
147 _focusMarkerElement: (elementIndex, options = {}) ->
148 # If you, for example, focus the location bar, unfocus it by pressing
149 # `<esc>` and then try to focus a link or text input in a web page the focus
150 # won’t work unless `@browser` is focused first.
152 @_run('focus_marker_element', {elementIndex, options})