]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Enable the `braces_spacing` coffeelint rule
[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 @_addListeners()
52
53 _addListeners: ->
54 # Require the subset of the options needed to be listed explicitly (as
55 # opposed to sending _all_ options) for performance. Each option access
56 # might trigger an optionOverride.
57 @_listen('options', ({prefs}) =>
58 options = {}
59 for pref in prefs
60 options[pref] = @options[pref]
61 return options
62 )
63
64 @_listen('vimMethod', ({method, args = []}, {callback = null}) =>
65 result = @[method](args...)
66 @_send(callback, result) if callback
67 )
68
69 @_listen('vimMethodSync', ({method, args = []}) =>
70 return @[method](args...)
71 )
72
73 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
74
75 @_listen('locationChange', @_onLocationChange.bind(this))
76
77 _setBrowser: (browser) ->
78 refresh = @browser?
79 @browser = browser
80 @window = @browser.ownerGlobal
81 @_messageManager = @browser.messageManager
82 @_addListeners() if refresh
83
84 _resetState: ->
85 @_state =
86 frameCanReceiveEvents: false
87
88 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
89
90 isUIEvent: (event) ->
91 target = event.originalTarget
92 return not @_state.frameCanReceiveEvents or
93 if MULTI_PROCESS_ENABLED
94 (target != @window.gBrowser.selectedBrowser)
95 else
96 (target.ownerGlobal instanceof ChromeWindow)
97
98 # `args...` is passed to the mode's `onEnter` method.
99 enterMode: (mode, args...) ->
100 return if @mode == mode
101
102 unless utils.has(@_parent.modes, mode)
103 modes = Object.keys(@_parent.modes).join(', ')
104 throw new Error("VimFx: Unknown mode. Available modes are: #{modes}.
105 Got: #{mode}")
106
107 @_call('onLeave') if @mode?
108 @mode = mode
109 result = @_call('onEnter', null, args...)
110 @_parent.emit('modeChange', this)
111 @_send('modeChange', {mode})
112 return result
113
114 _consumeKeyEvent: (event, focusType) ->
115 return @_parent.consumeKeyEvent(event, this, focusType)
116
117 _onInput: (match, uiEvent = false) ->
118 # In the location bar, `<tab>` is used to cycle between autocomplete
119 # results. In the dev tools, `<tab>` autocompletes what you’re typing. The
120 # only way to preverve this important behavior seems to be special casing.
121 {focus_previous, focus_next} = @_parent.modes.normal.commands
122 if uiEvent and
123 ((match.command == focus_previous and match.keyStr == '<s-tab>') or
124 (match.command == focus_next and match.keyStr == '<tab>'))
125 return false
126
127 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
128 return suppress
129
130 _onLocationChange: (url) ->
131 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
132 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
133
134 _call: (method, data = {}, extraArgs...) ->
135 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
136 currentMode = @_parent.modes[@mode]
137 return currentMode[method].call(currentMode, args, extraArgs...)
138
139 _run: (name, data = {}, callback = null) ->
140 @_send('runCommand', {name, data}, callback)
141
142 _listen: (name, listener) ->
143 messageManager.listen(name, listener, @_messageManager)
144
145 _listenOnce: (name, listener) ->
146 messageManager.listenOnce(name, listener, @_messageManager)
147
148 _send: (name, data, callback = null) ->
149 messageManager.send(name, data, @_messageManager, callback)
150
151 notify: (title, options = {}) ->
152 return unless @options.notifications_enabled
153 new @window.Notification(title, Object.assign({
154 icon: 'chrome://vimfx/skin/icon128.png'
155 tag: 'VimFx-notification'
156 }, options))
157
158 markPageInteraction: ->
159 @_send('markPageInteraction')
160
161 _focusMarkerElement: (elementIndex, options = {}) ->
162 # If you, for example, focus the location bar, unfocus it by pressing
163 # `<esc>` and then try to focus a link or text input in a web page the focus
164 # won’t work unless `@browser` is focused first.
165 @browser.focus()
166 @_run('focus_marker_element', {elementIndex, options})
167
168 module.exports = Vim
Imprint / Impressum