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