]> git.gir.st - VimFx.git/blob - extension/lib/main.coffee
Use `chrome://` URIs for `require()` calls
[VimFx.git] / extension / lib / main.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
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 pulls in all the different parts of VimFx, initializes them, and
22 # stiches them together.
23
24 createAPI = require('./api')
25 button = require('./button')
26 defaults = require('./defaults')
27 UIEventManager = require('./events')
28 {applyMigrations} = require('./legacy')
29 messageManager = require('./message-manager')
30 migrations = require('./migrations')
31 modes = require('./modes')
32 options = require('./options')
33 parsePref = require('./parse-prefs')
34 prefs = require('./prefs')
35 utils = require('./utils')
36 VimFx = require('./vimfx')
37 test = try require('../test/index')
38
39 Cu.import('resource://gre/modules/AddonManager.jsm')
40
41 module.exports = (data, reason) ->
42 # Set default prefs and apply migrations as early as possible.
43 prefs.default.init()
44 applyMigrations(migrations)
45
46 parsedOptions = {}
47 for pref of defaults.all_options
48 parsedOptions[pref] = parsePref(pref)
49 vimfx = new VimFx(modes, parsedOptions)
50 vimfx.id = data.id
51 vimfx.version = data.version
52 AddonManager.getAddonByID(vimfx.id, (info) -> vimfx.info = info)
53
54 utils.loadCss('style')
55
56 options.observe(vimfx)
57
58 skipCreateKeyTrees = false
59 prefs.observe('', (pref) ->
60 if pref.startsWith('mode.') or pref.startsWith('custom.')
61 vimfx.createKeyTrees() unless skipCreateKeyTrees
62 else if pref of defaults.all_options
63 value = parsePref(pref)
64 vimfx.options[pref] = value
65 )
66
67 button.injectButton(vimfx)
68
69 setWindowAttribute = (window, name, value = 'none') ->
70 window.document.documentElement.setAttribute("vimfx-#{name}", value)
71
72 onModeDisplayChange = (vimOrEvent) ->
73 window = vimOrEvent.window ? vimOrEvent.originalTarget.ownerGlobal
74
75 # The 'modeChange' event provides the `vim` object that changed mode, but it
76 # might not be the current `vim` anymore, so always get the current one.
77 return unless vim = vimfx.getCurrentVim(window)
78
79 setWindowAttribute(window, 'mode', vim.mode)
80 vimfx.emit('modeDisplayChange', vim)
81
82 vimfx.on('modeChange', onModeDisplayChange)
83 vimfx.on('TabSelect', onModeDisplayChange)
84
85 vimfx.on('focusTypeChange', ({vim, focusType}) ->
86 setWindowAttribute(vim.window, 'focus-type', focusType)
87 )
88
89 # Setup the public API. See public.coffee for more information. This is done
90 # _after_ the prefs observing setup, so that option prefs get validated and
91 # used when calling `vimfx.set()`.
92 apiUrl = "#{data.resourceURI.spec}lib/public.js"
93 prefs.set('api_url', apiUrl)
94 publicScope = Cu.import(apiUrl, {})
95 api = createAPI(vimfx)
96 publicScope._invokeCallback = (callback) ->
97 # Calling `vimfx.createKeyTrees()` after each `vimfx.set()` that modifies a
98 # shortcut is absolutely redundant and may make Firefox start slower. Do it
99 # once instead.
100 skipCreateKeyTrees = true
101 callback(api)
102 skipCreateKeyTrees = false
103 vimfx.createKeyTrees()
104 module.onShutdown(-> publicScope._invokeCallback = null)
105
106 # Pass the API to add-ons that loaded before VimFx, either because they just
107 # happened to do so when Firefox started, or because VimFx was updated (or
108 # disabled and then enabled) in the middle of the session. Because of the
109 # latter case, `Cu.unload(apiUrl)` is not called on shutdown. Otherwise you’d
110 # have to either restart Firefox, or disable and enable every add-on using the
111 # API in order for them to take effect again. (`_callbacks` should always
112 # exist, but it’s better to be safe than sorry.)
113 if publicScope._callbacks?.length > 0
114 publicScope._invokeCallback((api) ->
115 callback(api) for callback in publicScope._callbacks
116 return
117 )
118
119 test?(vimfx)
120
121 windows = new WeakSet()
122 messageManager.listen('tabCreated', (data, {target: browser}) ->
123 # Frame scripts are run in more places than we need. Tell those not to do
124 # anything.
125 group = browser.getAttribute('messagemanagergroup')
126 return false unless group == 'browsers'
127
128 window = browser.ownerGlobal
129 vimfx.addVim(browser)
130
131 unless windows.has(window)
132 windows.add(window)
133 eventManager = new UIEventManager(vimfx, window)
134 eventManager.addListeners(vimfx, window)
135 setWindowAttribute(window, 'mode', 'normal')
136 setWindowAttribute(window, 'focus-type', null)
137
138 return true
139 )
140
141 messageManager.load('bootstrap')
Imprint / Impressum