2 # Copyright Simon Lydell 2016.
4 # This file is part of VimFx.
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.
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.
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/>.
20 # This files deals with handling all, or many, of VimFx’s prefs in bulk.
22 defaults = require('./defaults')
23 prefs = require('./prefs')
26 prefs.set(pref, null) for pref of defaults.all_prefs
30 regular = (pref for pref of defaults.all_prefs)
31 custom = Services.prefs.getBranch(defaults.BRANCH).getChildList('custom.')
33 for pref in regular.concat(custom) when prefs.has(pref)
34 exported[pref] = prefs.get(pref)
37 importExported = (exportedString) ->
40 exported = JSON.parse(exportedString)
44 errors: [error.message]
47 unless typeof exported == 'object' and exported != null and
48 Object.keys(exported).length > 0
51 errors: ["The input must be a non-empty object. Got: #{exportedString}"]
57 for pref, value of exported
58 unless Object::hasOwnProperty.call(defaults.all_prefs, pref) or
59 pref.startsWith('custom.')
60 errors.push("#{pref}: Unknown pref.")
64 # `prefs.set` handles validation of `value`.
65 prefs.set(pref, value)
67 errors.push("#{pref}: #{error.message.replace(/^VimFx: /, '')}")
72 return {numSuccesses, errors}
74 createImportErrorReport = ({numSuccesses, errors}) ->
77 'The stuff you pasted is invalid:'
79 s1 = if numSuccesses == 1 then '' else 's'
80 s2 = if errors.length == 1 then '' else 's'
82 #{numSuccesses} preference#{s1} imported successfully.
84 #{errors.length} error#{s2} occurred:
97 createImportErrorReport