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 return options[pref] unless location
106 overrides = getOverrides(vimfx.optionOverrides, location)
107 return overrides?[pref] ? options[pref]
109 onShutdown(vimfx, -> vimfx.optionOverrides = [])
110 vimfx.optionOverrides.push(rules...)
112 addKeyOverrides: (rules...) ->
113 unless vimfx.keyOverrides
114 vimfx.keyOverrides = []
115 vimfx.options.keyValidator = (keyStr, mode) ->
116 location = utils.getCurrentLocation()
117 return true unless location
118 overrides = getOverrides(vimfx.keyOverrides, location, mode)
119 return keyStr not in (overrides ? [])
120 onShutdown(vimfx, -> vimfx.keyOverrides = [])
121 vimfx.keyOverrides.push(rules...)
123 send: (vim, message, data = null, callback = null) ->
124 unless vim instanceof Vim
126 "VimFx: The first argument must be a vim object. Got: #{vim}"
128 unless typeof message == 'string'
130 "VimFx: The second argument must be a message string. Got: #{message}"
132 if typeof data == 'function'
134 "VimFx: The third argument must not be a function. Got: #{data}"
137 unless typeof callback == 'function' or callback == null
139 "VimFx: If provided, `callback` must be a function. Got: #{callback}"
141 vim._send(message, data, callback, {prefix: 'config:'})
143 on: (event, listener) ->
144 vimfx.on(event, listener)
145 onShutdown(vimfx, -> vimfx.off(event, listener))
147 off: vimfx.off.bind(vimfx)
151 getOverrides = (rules, args...) ->
152 for [match, overrides] in rules
153 return overrides if match(args...)
156 onShutdown = (vimfx, handler) ->
159 vimfx.off('shutdown', fn)
160 vimfx.on('shutdown', fn)
162 module.exports = createConfigAPI