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