]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Merge pull request #561 from cbertoldi/develop
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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 ChromeWindow = Ci.nsIDOMChromeWindow
31
32 class Vim
33 constructor: (browser, @_parent) ->
34 @_setBrowser(browser)
35 @_storage = {}
36
37 @_resetState()
38
39 Object.defineProperty(this, 'options', {
40 get: => @_parent.options
41 enumerable: true
42 })
43
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)
49 )
50
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 }) =>
55 options = {}
56 for pref in prefs
57 options[pref] = @options[pref]
58 return options
59 )
60
61 @_listen('vimMethod', ({ method, args = [] }, { callback = null }) =>
62 result = @[method](args...)
63 @_send(callback, result) if callback
64 )
65
66 @_listen('vimMethodSync', ({ method, args = [] }) =>
67 return @[method](args...)
68 )
69
70 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
71
72 _setBrowser: (@browser) ->
73 @window = @browser.ownerGlobal
74 @_messageManager = @browser.messageManager
75
76 _resetState: ->
77 @_state =
78 frameCanReceiveEvents: false
79 lastUrl: null
80
81 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
82
83 isUIEvent: (event) ->
84 target = event.originalTarget
85 return @_state.frameCanReceiveEvents and
86 if MULTI_PROCESS_ENABLED
87 (target != @window.gBrowser.selectedBrowser)
88 else
89 (target.ownerGlobal instanceof ChromeWindow)
90
91 # `args...` is passed to the mode's `onEnter` method.
92 enterMode: (mode, args...) ->
93 return if @mode == mode
94
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 }.
98 Got: #{ mode }")
99
100 @_call('onLeave') if @mode?
101 @mode = mode
102 result = @_call('onEnter', null, args...)
103 @_parent.emit('modeChange', this)
104 @_send('modeChange', {mode})
105 return result
106
107 _consumeKeyEvent: (event, focusType) ->
108 return @_parent.consumeKeyEvent(event, this, focusType)
109
110 _onInput: (match, uiEvent = false) ->
111 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
112 return suppress
113
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')
120
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...)
125
126 _run: (name, data = {}, callback = null) ->
127 @_send('runCommand', {name, data}, callback)
128
129 _listen: (name, listener) ->
130 messageManager.listen(name, listener, @_messageManager)
131
132 _listenOnce: (name, listener) ->
133 messageManager.listenOnce(name, listener, @_messageManager)
134
135 _send: (name, data, callback = null) ->
136 messageManager.send(name, data, @_messageManager, callback)
137
138 notify: (title, options = {}) ->
139 new @window.Notification(title, Object.assign({
140 icon: 'chrome://vimfx/skin/icon128.png'
141 tag: 'VimFx-notification'
142 }, options))
143
144 markPageInteraction: ->
145 @_send('markPageInteraction')
146
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.
151 @browser.focus()
152 @_run('focus_marker_element', {elementIndex, options})
153
154 module.exports = Vim
Imprint / Impressum