]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Re-implement "switch back to tab" logic
[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 @_resetState()
37
38 Object.defineProperty(this, 'options', {
39 get: => @_parent.options
40 enumerable: true
41 })
42
43 @_onLocationChange(@browser.currentURI.spec)
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 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
65
66 _resetState: ->
67 @_state =
68 frameCanReceiveEvents: false
69 lastUrl: null
70
71 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
72
73 isFrameEvent: (event) ->
74 target = event.originalTarget
75 return @_state.frameCanReceiveEvents and
76 if MULTI_PROCESS_ENABLED
77 (target == @window.gBrowser.selectedBrowser)
78 else
79 not (target.ownerGlobal == @window)
80
81 # `args` is an array of arguments to be passed to the mode's `onEnter` method.
82 enterMode: (mode, args...) ->
83 return false if @mode == mode
84
85 unless utils.has(@_parent.modes, mode)
86 modes = Object.keys(@_parent.modes).join(', ')
87 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
88 Got: #{ mode }")
89
90 @_call('onLeave') if @mode?
91 @mode = mode
92 @_call('onEnter', null, args...)
93 @_parent.emit('modeChange', this)
94 @_send('modeChange', {mode})
95 return true
96
97 _consumeKeyEvent: (event, focusType) ->
98 return @_parent.consumeKeyEvent(event, this, focusType)
99
100 _onInput: (match, { isFrameEvent = false } = {}) ->
101 suppress = @_call('onInput', {isFrameEvent, count: match.count}, match)
102 return suppress
103
104 _onLocationChange: (url, { refresh = false } = {}) ->
105 return if url == @_state.lastUrl and not refresh
106 @_state.lastUrl = url
107 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
108 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
109 @_send('locationChange')
110
111 _call: (method, data = {}, extraArgs...) ->
112 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
113 currentMode = @_parent.modes[@mode]
114 currentMode[method].call(currentMode, args, extraArgs...)
115
116 _run: (name, data = {}, callback = null) ->
117 @_send('runCommand', {name, data}, callback)
118
119 _listen: (name, listener) ->
120 messageManager.listen(name, listener, @_messageManager)
121
122 _listenOnce: (name, listener) ->
123 messageManager.listenOnce(name, listener, @_messageManager)
124
125 _send: (name, data, callback = null) ->
126 messageManager.send(name, data, @_messageManager, callback)
127
128 notify: (title, options = {}) ->
129 new @window.Notification(title, Object.assign({
130 icon: 'chrome://vimfx/skin/icon128.png'
131 tag: 'VimFx-notification'
132 }, options))
133
134 markPageInteraction: ->
135 @_send('markPageInteraction')
136
137 _focusMarkerElement: (elementIndex, options = {}) ->
138 # If you, for example, focus the location bar, unfocus it by pressing
139 # `<esc>` and then try to focus a link or text input in a web page the focus
140 # won’t work unless `@browser` is focused first.
141 @browser.focus()
142 @_run('focus_marker_element', {elementIndex, options})
143
144 module.exports = Vim
Imprint / Impressum