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