]> git.gir.st - VimFx.git/blob - extension/lib/api.coffee
Merge branch 'master' into develop
[VimFx.git] / extension / lib / api.coffee
1 ###
2 # Copyright Simon Lydell 2015, 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 defines VimFx’s config file API.
21
22 defaults = require('./defaults')
23 prefs = require('./prefs')
24 utils = require('./utils')
25 Vim = require('./vim')
26
27 counter = new utils.Counter({start: 10000, step: 100})
28
29 createConfigAPI = (vimfx) -> {
30 get: (pref) -> switch
31 when pref of defaults.parsed_options
32 vimfx.options[pref]
33 when pref of defaults.all_prefs or pref?.startsWith('custom.')
34 prefs.get(pref)
35 else
36 throw new Error("VimFx: Unknown pref: #{pref}")
37
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]
43 else
44 throw new Error("VimFx: Unknown pref: #{pref}")
45
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))
55 else
56 throw new Error("VimFx: Unknown pref: #{pref}")
57
58 addCommand: ({name, description, mode, category, order} = {}, fn) ->
59 mode ?= 'normal'
60 category ?= if mode == 'normal' then 'misc' else ''
61 order ?= counter.tick()
62
63 unless typeof name == 'string'
64 throw new Error("VimFx: A command name as a string is required.
65 Got: #{name}")
66 unless /^[a-z_]+$/.test(name)
67 throw new Error("VimFx: Command names should only consist of a-z
68 (lowercase) and underscores. Got: #{name}")
69 unless typeof description == 'string' and description != ''
70 throw new Error("VimFx: Commands must have a non-empty description.
71 Got: #{description}")
72 unless utils.has(vimfx.modes, mode)
73 modes = Object.keys(vimfx.modes).join(', ')
74 throw new Error("VimFx: Unknown mode. Available modes are: #{modes}.
75 Got: #{mode}")
76 unless utils.has(vimfx.options.categories, category)
77 categories = Object.keys(vimfx.options.categories).join(', ')
78 throw new Error("VimFx: Unknown category. Available categories are:
79 #{categories}. Got: #{category}")
80 unless typeof order == 'number'
81 throw new Error("VimFx: Command order must be a number. Got: #{order}")
82 unless typeof fn == 'function'
83 throw new Error("VimFx: Commands need a function to run. Got: #{fn}")
84
85 pref = "#{defaults.BRANCH}custom.mode.#{mode}.#{name}"
86 prefs.root.default.set(pref, '')
87 vimfx.modes[mode].commands[name] = {
88 pref, category, order, run: fn, description
89 }
90 onShutdown(vimfx, -> delete vimfx.modes[mode].commands[name])
91
92 addOptionOverrides: (rules...) ->
93 unless vimfx.optionOverrides
94 vimfx.optionOverrides = []
95 vimfx.options = new Proxy(vimfx.options, {
96 get: (options, pref) ->
97 location = utils.getCurrentLocation()
98 overrides = getOverrides(vimfx.optionOverrides, location)
99 return overrides?[pref] ? options[pref]
100 })
101 onShutdown(vimfx, -> vimfx.optionOverrides = [])
102 vimfx.optionOverrides.push(rules...)
103
104 addKeyOverrides: (rules...) ->
105 unless vimfx.keyOverrides
106 vimfx.keyOverrides = []
107 vimfx.options.keyValidator = (keyStr, mode) ->
108 location = utils.getCurrentLocation()
109 overrides = getOverrides(vimfx.keyOverrides, location, mode)
110 return keyStr not in (overrides ? [])
111 onShutdown(vimfx, -> vimfx.keyOverrides = [])
112 vimfx.keyOverrides.push(rules...)
113
114 send: (vim, message, data = null, callback = null) ->
115 unless vim instanceof Vim
116 throw new Error("VimFx: The first argument must be a vim object.
117 Got: #{vim}")
118 unless typeof message == 'string'
119 throw new Error("VimFx: The second argument must be a message string.
120 Got: #{message}")
121 if typeof data == 'function'
122 throw new Error("VimFx: The third argument must not be a function.
123 Got: #{data}")
124
125 unless typeof callback == 'function' or callback == null
126 throw Error("VimFx: If provided, `callback` must be a function.
127 Got: #{callback}")
128 vim._send(message, data, callback, {prefix: 'config:'})
129
130 on: (event, listener) ->
131 vimfx.on(event, listener)
132 onShutdown(vimfx, -> vimfx.off(event, listener))
133
134 off: vimfx.off.bind(vimfx)
135 modes: vimfx.modes
136 }
137
138 getOverrides = (rules, args...) ->
139 for [match, overrides] in rules
140 return overrides if match(args...)
141 return null
142
143 onShutdown = (vimfx, handler) ->
144 fn = ->
145 handler()
146 vimfx.off('shutdown', fn)
147 vimfx.on('shutdown', fn)
148
149 module.exports = createConfigAPI
Imprint / Impressum