]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Make it possible to create custom hint commands
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
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 config file API. Underscored names are
25 # private and should not be used by API consumers.
26
27 messageManager = require('./message-manager')
28 ScrollableElements = require('./scrollable-elements')
29 statusPanel = require('./status-panel')
30 utils = require('./utils')
31
32 ChromeWindow = Ci.nsIDOMChromeWindow
33
34 class Vim
35 constructor: (browser, @_parent) ->
36 @mode = undefined
37 @focusType = 'none'
38 @_setBrowser(browser, {addListeners: false})
39 @_storage = {}
40
41 @_resetState()
42
43 Object.defineProperty(this, 'options', {
44 get: => @_parent.options
45 enumerable: true
46 })
47
48 _start: ->
49 @_onLocationChange(@browser.currentURI.spec)
50 @_addListeners()
51 focusType = utils.getFocusType(utils.getActiveElement(@window))
52 @_setFocusType(focusType)
53
54 _addListeners: ->
55 @_listen('vimMethod', ({method, args = []}, callback = null) =>
56 result = this[method](args...)
57 callback?(result)
58 )
59
60 @_listen('vimMethodSync', ({method, args = []}) =>
61 return this[method](args...)
62 )
63
64 @_listen('locationChange', @_onLocationChange.bind(this))
65
66 @_listen('frameCanReceiveEvents', (value) =>
67 @_state.frameCanReceiveEvents = value
68 )
69
70 @_listen('focusType', (focusType) =>
71 # If the focus moves from a web page element to a browser UI element, the
72 # focus and blur events happen in the expected order, but the message from
73 # the frame script arrives too late. Therefore, check that the currently
74 # active element isn’t a browser UI element first.
75 unless @_isUIElement(utils.getActiveElement(@window))
76 @_setFocusType(focusType)
77 )
78
79 _setBrowser: (@browser, {addListeners = true} = {}) ->
80 @window = @browser.ownerGlobal
81 @_messageManager = @browser.messageManager
82
83 @_statusPanel?.remove()
84 @_statusPanel = statusPanel.injectStatusPanel(@browser)
85
86 @_addListeners() if addListeners
87
88 _resetState: ->
89 @_state = {
90 frameCanReceiveEvents: false
91 scrollableElements: new ScrollableElements(@window)
92 }
93
94 _isBlacklisted: (url) -> @options.blacklist.some((regex) -> regex.test(url))
95
96 isUIEvent: (event) ->
97 return not @_state.frameCanReceiveEvents or
98 @_isUIElement(event.originalTarget)
99
100 _isUIElement: (element) ->
101 # TODO: The `element.ownerGlobal` check will be redundant when
102 # non-multi-process is removed from Firefox.
103 return element.ownerGlobal instanceof ChromeWindow and
104 element != @window.gBrowser.selectedBrowser
105
106 # `args...` is passed to the mode's `onEnter` method.
107 _enterMode: (mode, args...) ->
108 return if @mode == mode
109
110 unless utils.has(@_parent.modes, mode)
111 modes = Object.keys(@_parent.modes).join(', ')
112 throw new Error(
113 "VimFx: Unknown mode. Available modes are: #{modes}. Got: #{mode}"
114 )
115
116 @_call('onLeave') if @mode?
117 @mode = mode
118 @_call('onEnter', null, args...)
119 @_parent.emit('modeChange', {vim: this})
120 @_send('modeChange', {mode})
121
122 _consumeKeyEvent: (event) ->
123 return @_parent.consumeKeyEvent(event, this)
124
125 _onInput: (match, event) ->
126 uiEvent = if @isUIEvent(event) then event else false
127 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
128 return suppress
129
130 _onLocationChange: (url) ->
131 switch
132 when @_isBlacklisted(url)
133 @_enterMode('ignore', {type: 'blacklist'})
134 when (@mode == 'ignore' and @_storage.ignore.type == 'blacklist') or
135 not @mode
136 @_enterMode('normal')
137 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
138
139 _call: (method, data = {}, extraArgs...) ->
140 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
141 currentMode = @_parent.modes[@mode]
142 return currentMode[method].call(currentMode, args, extraArgs...)
143
144 _run: (name, data = {}, callback = null) ->
145 @_send('runCommand', {name, data}, callback)
146
147 _messageManagerOptions: (options) ->
148 return Object.assign({
149 messageManager: @_messageManager
150 }, options)
151
152 _listen: (name, listener, options = {}) ->
153 messageManager.listen(name, listener, @_messageManagerOptions(options))
154
155 _listenOnce: (name, listener, options = {}) ->
156 messageManager.listenOnce(name, listener, @_messageManagerOptions(options))
157
158 _send: (name, data, callback = null, options = {}) ->
159 messageManager.send(name, data, callback, @_messageManagerOptions(options))
160
161 notify: (message) ->
162 @_parent.emit('notification', {vim: this, message})
163 if @options.notifications_enabled
164 @_statusPanel.setAttribute('label', message)
165 @_statusPanel.removeAttribute('inactive')
166
167 hideNotification: ->
168 @_parent.emit('hideNotification', {vim: this})
169 @_statusPanel.setAttribute('inactive', 'true')
170
171 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
172
173 _focusMarkerElement: (elementIndex, options = {}) ->
174 # If you, for example, focus the location bar, unfocus it by pressing
175 # `<esc>` and then try to focus a link or text input in a web page the focus
176 # won’t work unless `@browser` is focused first.
177 @browser.focus()
178 @_run('focus_marker_element', {elementIndex, options})
179
180 _setFocusType: (focusType) ->
181 return if focusType == @focusType
182 @focusType = focusType
183 switch
184 when @focusType == 'ignore'
185 @_enterMode('ignore', {type: 'focusType'})
186 when @mode == 'ignore' and @_storage.ignore.type == 'focusType'
187 @_enterMode('normal')
188 when @mode == 'normal' and @focusType == 'findbar'
189 @_enterMode('find')
190 when @mode == 'find' and @focusType != 'findbar'
191 @_enterMode('normal')
192 @_parent.emit('focusTypeChange', {vim: this})
193
194 module.exports = Vim
Imprint / Impressum