]> git.gir.st - VimFx.git/blob - extension/lib/main.coffee
Allow to temporarily enter Ignore mode based on focusType
[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 button = require('./button')
25 config = require('./config')
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("#{ADDON_PATH}/skin/style.css")
55
56 options.observe(vimfx)
57
58 prefs.observe('', (pref) ->
59 if pref.startsWith('mode.') or pref.startsWith('custom.')
60 vimfx.createKeyTrees()
61 else if pref of defaults.all_options
62 value = parsePref(pref)
63 vimfx.options[pref] = value
64 )
65
66 button.injectButton(vimfx)
67
68 setWindowAttribute = (window, name, value = 'none') ->
69 window.document.documentElement.setAttribute("vimfx-#{name}", value)
70
71 onModeDisplayChange = (vimOrEvent) ->
72 window = vimOrEvent.window ? vimOrEvent.originalTarget.ownerGlobal
73
74 # The 'modeChange' event provides the `vim` object that changed mode, but
75 # it might not be the current `vim` anymore so always get the current one.
76 return unless vim = vimfx.getCurrentVim(window)
77
78 setWindowAttribute(window, 'mode', vim.mode)
79 vimfx.emit('modeDisplayChange', vim)
80
81 vimfx.on('modeChange', onModeDisplayChange)
82 vimfx.on('TabSelect', onModeDisplayChange)
83
84 vimfx.on('focusTypeChange', ({vim, focusType}) ->
85 setWindowAttribute(vim.window, 'focus-type', focusType)
86 )
87
88 windows = new WeakSet()
89 messageManager.listen('tabCreated', (data, callback, browser) ->
90 # Frame scripts are run in more places than we need. Tell those not to do
91 # anything.
92 unless browser.getAttribute('messagemanagergroup') == 'browsers'
93 callback(false)
94 return
95
96 window = browser.ownerGlobal
97 vimfx.addVim(browser)
98
99 unless windows.has(window)
100 windows.add(window)
101 eventManager = new UIEventManager(vimfx, window)
102 eventManager.addListeners(vimfx, window)
103 setWindowAttribute(window, 'mode', 'normal')
104 setWindowAttribute(window, 'focus-type', null)
105
106 callback(true)
107 )
108
109 messageManager.load("#{ADDON_PATH}/content/bootstrap.js")
110
111 config.load(vimfx)
112 vimfx.on('shutdown', -> messageManager.send('unloadConfig'))
113 module.onShutdown(->
114 # Make sure to run the below lines in this order. The second line results in
115 # removing all message listeners in frame scripts, including the one for
116 # 'unloadConfig' (see above).
117 vimfx.emit('shutdown')
118 messageManager.send('shutdown')
119 )
120
121 if test
122 test(vimfx)
123 runFrameTests = true
124 messageManager.listen('runTests', (data, callback) ->
125 callback(runFrameTests)
126 runFrameTests = false
127 )
Imprint / Impressum