2 # Copyright Simon Lydell 2015, 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 file defines VimFx’s config file API.
22 defaults = require('./defaults')
23 prefs = require('./prefs')
24 utils = require('./utils')
25 Vim = require('./vim')
27 counter = new utils.Counter({start: 10000, step: 100})
29 createConfigAPI = (vimfx) -> {
31 when pref of defaults.parsed_options
33 when pref of defaults.all_prefs or pref?.startsWith('custom.')
36 throw new Error("VimFx: Unknown pref: #{pref}")
38 getDefault: (pref) -> switch
39 when pref of defaults.parsed_options or pref?.startsWith('custom.')
40 throw new Error("VimFx: No default for pref: #{pref}")
41 when pref of defaults.all_prefs
42 defaults.all_prefs[pref]
44 throw new Error("VimFx: Unknown pref: #{pref}")
46 set: (pref, value) -> switch
47 when pref of defaults.parsed_options
48 previousValue = vimfx.options[pref]
49 vimfx.options[pref] = value
50 onShutdown(vimfx, -> vimfx.options[pref] = previousValue)
51 when pref of defaults.all_prefs or pref?.startsWith('custom.')
52 previousValue = if prefs.has(pref) then prefs.get(pref) else null
53 prefs.set(pref, value)
54 onShutdown(vimfx, -> prefs.set(pref, previousValue))
56 throw new Error("VimFx: Unknown pref: #{pref}")
58 addCommand: ({name, description, mode, category, order} = {}, fn) ->
60 category ?= if mode == 'normal' then 'misc' else ''
61 order ?= counter.tick()
63 unless typeof name == 'string'
65 "VimFx: A command name as a string is required. Got: #{name}"
67 unless /^[a-z_]+$/.test(name)
69 "VimFx: Command names should only consist of a-z (lowercase) and
70 underscores. Got: #{name}"
72 unless typeof description == 'string' and description != ''
74 "VimFx: Commands must have a non-empty description. Got: #{description}"
76 unless utils.has(vimfx.modes, mode)
77 modes = Object.keys(vimfx.modes).join(', ')
79 "VimFx: Unknown mode. Available modes are: #{modes}. Got: #{mode}"
81 unless utils.has(vimfx.options.categories, category)
82 categories = Object.keys(vimfx.options.categories).join(', ')
84 "VimFx: Unknown category. Available categories are: #{categories}.
87 unless typeof order == 'number'
88 throw new Error("VimFx: Command order must be a number. Got: #{order}")
89 unless typeof fn == 'function'
90 throw new Error("VimFx: Commands need a function to run. Got: #{fn}")
92 pref = "#{defaults.BRANCH}custom.mode.#{mode}.#{name}"
93 prefs.root.default.set(pref, '')
94 vimfx.modes[mode].commands[name] = {
95 pref, category, order, run: fn, description
97 onShutdown(vimfx, -> delete vimfx.modes[mode].commands[name])
99 addOptionOverrides: (rules...) ->
100 unless vimfx.optionOverrides
101 vimfx.optionOverrides = []
102 vimfx.options = new Proxy(vimfx.options, {
103 get: (options, pref) ->
104 location = utils.getCurrentLocation()
105 overrides = getOverrides(vimfx.optionOverrides, location)
106 return overrides?[pref] ? options[pref]
108 onShutdown(vimfx, -> vimfx.optionOverrides = [])
109 vimfx.optionOverrides.push(rules...)
111 addKeyOverrides: (rules...) ->
112 unless vimfx.keyOverrides
113 vimfx.keyOverrides = []
114 vimfx.options.keyValidator = (keyStr, mode) ->
115 location = utils.getCurrentLocation()
116 overrides = getOverrides(vimfx.keyOverrides, location, mode)
117 return keyStr not in (overrides ? [])
118 onShutdown(vimfx, -> vimfx.keyOverrides = [])
119 vimfx.keyOverrides.push(rules...)
121 send: (vim, message, data = null, callback = null) ->
122 unless vim instanceof Vim
124 "VimFx: The first argument must be a vim object. Got: #{vim}"
126 unless typeof message == 'string'
128 "VimFx: The second argument must be a message string. Got: #{message}"
130 if typeof data == 'function'
132 "VimFx: The third argument must not be a function. Got: #{data}"
135 unless typeof callback == 'function' or callback == null
137 "VimFx: If provided, `callback` must be a function. Got: #{callback}"
139 vim._send(message, data, callback, {prefix: 'config:'})
141 on: (event, listener) ->
142 vimfx.on(event, listener)
143 onShutdown(vimfx, -> vimfx.off(event, listener))
145 off: vimfx.off.bind(vimfx)
149 getOverrides = (rules, args...) ->
150 for [match, overrides] in rules
151 return overrides if match(args...)
154 onShutdown = (vimfx, handler) ->
157 vimfx.off('shutdown', fn)
158 vimfx.on('shutdown', fn)
160 module.exports = createConfigAPI