]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Support multi-process Firefox
[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 # `args` is an array of arguments to be passed to the mode's `onEnter` method.
70 enterMode: (mode, args...) ->
71 return false if @mode == mode
72
73 unless utils.has(@_parent.modes, mode)
74 modes = Object.keys(@_parent.modes).join(', ')
75 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
76 Got: #{ mode }")
77
78 @_call('onLeave') if @mode?
79 @mode = mode
80 @_call('onEnter', null, args...)
81 @_parent.emit('modeChange', this) if utils.getCurrentWindow() == @window
82 @_send('modeChange', {mode})
83 return true
84
85 _onInput: (event, focusType, { isFrameEvent = false } = {}) ->
86 match = @_parent.consumeKeyEvent(event, this, focusType)
87 return null unless match
88 suppress = @_call('onInput', {isFrameEvent, count: match.count}, match)
89 return suppress
90
91 _call: (method, data = {}, extraArgs...) ->
92 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
93 currentMode = @_parent.modes[@mode]
94 currentMode[method].call(currentMode, args, extraArgs...)
95
96 _run: (name, data = {}, callback = null) ->
97 @_send('runCommand', {name, data}, callback)
98
99 _listen: (name, listener) ->
100 messageManager.listen(name, listener, @_messageManager)
101
102 _listenOnce: (name, listener) ->
103 messageManager.listenOnce(name, listener, @_messageManager)
104
105 _send: (name, data, callback = null) ->
106 messageManager.send(name, data, @_messageManager, callback)
107
108 notify: (title, options = {}) ->
109 new @window.Notification(title, Object.assign({
110 icon: 'chrome://vimfx/skin/icon128.png'
111 tag: 'VimFx-notification'
112 }, options))
113
114 _focusMarkerElement: (elementIndex, options = {}) ->
115 # If you, for example, focus the location bar, unfocus it by pressing
116 # `<esc>` and then try to focus a link or text input in a web page the focus
117 # won’t work unless `@browser` is focused first.
118 @browser.focus()
119 @_run('focus_marker_element', {elementIndex, options})
120
121 module.exports = Vim
Imprint / Impressum