]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Make hints mode more robust
[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 @_setBrowser(browser)
33 @_storage = {}
34
35 @_resetState()
36
37 Object.defineProperty(this, 'options', {
38 get: => @_parent.options
39 enumerable: true
40 })
41
42 # Since this is done in the constructor, defer location change handling to
43 # the next tick so that this `vim` instance is saved in `vimfx.vims` first.
44 # This allows 'locationChange' listeners to use `@vimfx.getCurrentVim()`.
45 utils.nextTick(@window, =>
46 @_onLocationChange(@browser.currentURI.spec)
47 )
48
49 # Require the subset of the options needed to be listed explicitly (as
50 # opposed to sending _all_ options) for performance. Each option access
51 # might trigger an optionOverride.
52 @_listen('options', ({ prefs }) =>
53 options = {}
54 for pref in prefs
55 options[pref] = @options[pref]
56 return options
57 )
58
59 @_listen('vimMethod', ({ method, args = [] }, { callback = null }) =>
60 result = @[method](args...)
61 @_send(callback, result) if callback
62 )
63
64 @_listen('vimMethodSync', ({ method, args = [] }) =>
65 return @[method](args...)
66 )
67
68 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
69
70 _setBrowser: (@browser) ->
71 @window = @browser.ownerGlobal
72 @_messageManager = @browser.messageManager
73
74 _resetState: ->
75 @_state =
76 frameCanReceiveEvents: false
77 lastUrl: null
78
79 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
80
81 isFrameEvent: (event) ->
82 target = event.originalTarget
83 return @_state.frameCanReceiveEvents and
84 if MULTI_PROCESS_ENABLED
85 (target == @window.gBrowser.selectedBrowser)
86 else
87 not (target.ownerGlobal == @window)
88
89 # `args...` is passed to the mode's `onEnter` method.
90 enterMode: (mode, args...) ->
91 return if @mode == mode
92
93 unless utils.has(@_parent.modes, mode)
94 modes = Object.keys(@_parent.modes).join(', ')
95 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
96 Got: #{ mode }")
97
98 @_call('onLeave') if @mode?
99 @mode = mode
100 result = @_call('onEnter', null, args...)
101 @_parent.emit('modeChange', this)
102 @_send('modeChange', {mode})
103 return result
104
105 _consumeKeyEvent: (event, focusType) ->
106 return @_parent.consumeKeyEvent(event, this, focusType)
107
108 _onInput: (match, { isFrameEvent = false } = {}) ->
109 suppress = @_call('onInput', {isFrameEvent, count: match.count}, match)
110 return suppress
111
112 _onLocationChange: (url, { refresh = false } = {}) ->
113 return if url == @_state.lastUrl and not refresh
114 @_state.lastUrl = url
115 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
116 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
117 @_send('locationChange')
118
119 _call: (method, data = {}, extraArgs...) ->
120 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
121 currentMode = @_parent.modes[@mode]
122 return currentMode[method].call(currentMode, args, extraArgs...)
123
124 _run: (name, data = {}, callback = null) ->
125 @_send('runCommand', {name, data}, callback)
126
127 _listen: (name, listener) ->
128 messageManager.listen(name, listener, @_messageManager)
129
130 _listenOnce: (name, listener) ->
131 messageManager.listenOnce(name, listener, @_messageManager)
132
133 _send: (name, data, callback = null) ->
134 messageManager.send(name, data, @_messageManager, callback)
135
136 notify: (title, options = {}) ->
137 new @window.Notification(title, Object.assign({
138 icon: 'chrome://vimfx/skin/icon128.png'
139 tag: 'VimFx-notification'
140 }, options))
141
142 markPageInteraction: ->
143 @_send('markPageInteraction')
144
145 _focusMarkerElement: (elementIndex, options = {}) ->
146 # If you, for example, focus the location bar, unfocus it by pressing
147 # `<esc>` and then try to focus a link or text input in a web page the focus
148 # won’t work unless `@browser` is focused first.
149 @browser.focus()
150 @_run('focus_marker_element', {elementIndex, options})
151
152 module.exports = Vim
Imprint / Impressum