]> git.gir.st - VimFx.git/blob - extension/lib/config.coffee
Improve `Cu.import`s
[VimFx.git] / extension / lib / config.coffee
1 ###
2 # Copyright Simon Lydell 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file loads the user config file: config.js and frame.js.
21
22 createConfigAPI = require('./api')
23 messageManager = require('./message-manager')
24 utils = require('./utils')
25
26 {OS} = Components.utils.import('resource://gre/modules/osfile.jsm', {})
27
28 load = (vimfx, callback = ->) ->
29 configDir = vimfx.options.config_file_directory
30
31 unless configDir
32 callback(null)
33 return
34
35 scope = {vimfx: createConfigAPI(vimfx)}
36
37 # Calling `vimfx.createKeyTrees()` after each `vimfx.set()` that modifies a
38 # shortcut is absolutely redundant and may make Firefox start slower. Do it
39 # once instead.
40 vimfx.skipCreateKeyTrees = true
41 error = loadFile(configDir, 'config.js', scope)
42 vimfx.skipCreateKeyTrees = false
43 vimfx.createKeyTrees()
44
45 if error
46 callback(false)
47 return
48
49 messageManager.send('loadConfig', configDir, callback)
50
51 loadFile = (dir, file, scope) ->
52 uri = OS.Path.toFileURI(OS.Path.join(utils.expandPath(dir), file))
53 try
54 Services.scriptloader.loadSubScriptWithOptions(uri, {
55 target: scope
56 charset: 'UTF-8'
57 ignoreCache: true
58 })
59 return null
60 catch error
61 console.error("VimFx: Error loading #{file}", uri, error)
62 return error
63
64 module.exports = {
65 load
66 loadFile
67 }
Imprint / Impressum