]> 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.
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 public API.
21
22 defaults = require('./defaults')
23 prefs = require('./prefs')
24 utils = require('./utils')
25
26 counter = new utils.Counter({start: 10000, step: 100})
27
28 createAPI = (vimfx) ->
29 get: (pref) -> switch
30 when pref of defaults.parsed_options
31 defaults.parsed_options[pref]
32 when pref of defaults.all_prefs or pref?.startsWith('custom.')
33 prefs.get(pref)
34 else
35 throw new Error("VimFx: Unknown pref: #{pref}")
36
37 getDefault: (pref) -> switch
38 when pref of defaults.parsed_options or pref?.startsWith('custom.')
39 throw new Error("VimFx: No default for pref: #{pref}")
40 when pref of defaults.all_prefs
41 defaults.all_prefs[pref]
42 else
43 throw new Error("VimFx: Unknown pref: #{pref}")
44
45 set: (pref, value) -> switch
46 when pref of defaults.parsed_options
47 vimfx.options[pref] = value
48 when pref of defaults.all_prefs or pref?.startsWith('custom.')
49 prefs.set(pref, value)
50 else
51 throw new Error("VimFx: Unknown pref: #{pref}")
52
53 addCommand: ({name, description, mode, category, order} = {}, fn) ->
54 mode ?= 'normal'
55 category ?= if mode == 'normal' then 'misc' else ''
56 order ?= counter.tick()
57
58 unless typeof name == 'string'
59 throw new Error("VimFx: A command name as a string is required.
60 Got: #{name}")
61 unless /^[a-z_]+$/.test(name)
62 throw new Error("VimFx: Command names should only consist of a-z
63 (lowercase) and underscores. Got: #{name}")
64 unless typeof description == 'string' and description != ''
65 throw new Error("VimFx: Commands must have a non-empty description.
66 Got: #{description}")
67 unless utils.has(vimfx.modes, mode)
68 modes = Object.keys(vimfx.modes).join(', ')
69 throw new Error("VimFx: Unknown mode. Available modes are: #{modes}.
70 Got: #{mode}")
71 unless utils.has(vimfx.options.categories, category)
72 categories = Object.keys(vimfx.options.categories).join(', ')
73 throw new Error("VimFx: Unknown category. Available categories are:
74 #{categories}. Got: #{category}")
75 unless typeof order == 'number'
76 throw new Error("VimFx: Command order must be a number. Got: #{order}")
77 unless typeof fn == 'function'
78 throw new Error("VimFx: Commands need a function to run. Got: #{fn}")
79
80 pref = "#{defaults.BRANCH}custom.mode.#{mode}.#{name}"
81 prefs.root.default.set(pref, '')
82 vimfx.modes[mode].commands[name] = {
83 pref, category, order, run: fn, description
84 }
85
86 addOptionOverrides: (rules...) ->
87 unless vimfx.optionOverrides
88 vimfx.optionOverrides = []
89 vimfx.options = new Proxy(vimfx.options, {
90 get: (options, pref) ->
91 location = utils.getCurrentLocation()
92 overrides = getOverrides(vimfx.optionOverrides, location)
93 return overrides?[pref] ? options[pref]
94 })
95 vimfx.optionOverrides.push(rules...)
96
97 addKeyOverrides: (rules...) ->
98 unless vimfx.keyOverrides
99 vimfx.keyOverrides = []
100 vimfx.options.keyValidator = (keyStr, mode) ->
101 location = utils.getCurrentLocation()
102 overrides = getOverrides(vimfx.keyOverrides, location, mode)
103 return keyStr not in (overrides ? [])
104 vimfx.keyOverrides.push(rules...)
105
106 on: vimfx.on.bind(vimfx)
107 modes: vimfx.modes
108
109 getOverrides = (rules, args...) ->
110 for [match, overrides] in rules
111 return overrides if match(args...)
112 return null
113
114 module.exports = createAPI
Imprint / Impressum