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