]> git.gir.st - VimFx.git/blob - extension/lib/legacy.coffee
Change license to MIT
[VimFx.git] / extension / lib / legacy.coffee
1 # This file contains functions helping to upgrade to a newer version of VimFx
2 # without breaking backwards-compatibility. These are used in migrations.coffee.
3
4 notation = require('vim-like-key-notation')
5 prefs = require('./prefs')
6 utils = require('./utils')
7
8 applyMigrations = (migrations) ->
9 for migration, index in migrations
10 pref = "migration.#{index}.applied"
11 # This allows to manually choose migrations to apply. Be careful, though,
12 # since some migrations might have to run in order!
13 unless prefs.has(pref) and prefs.get(pref)
14 migration()
15 prefs.set(pref, true)
16 return
17
18 commaSeparatedList = /.[^,]*,?/g
19
20 commaSeparatedListItem = ///^
21 (?:
22 (?: (Shift) | ([acm])+ )
23 -
24 )?
25 (.+?)
26 ,?
27 $///
28
29 convertKey = (keyStr) ->
30 return (keyStr.trim().match(commaSeparatedList) ? []).map((part) ->
31 [match, shift, modifiers, key] = part.match(commaSeparatedListItem)
32 modifiers ?= ''
33 return notation.stringify({
34 key
35 shiftKey: Boolean(shift)
36 altKey: 'a' in modifiers
37 ctrlKey: 'c' in modifiers
38 metaKey: 'm' in modifiers
39 })
40 )
41
42 convertPattern = (pattern) ->
43 return utils.regexEscape(pattern)
44 .replace(/\\\*/g, '.*')
45 .replace(/!/g, '.')
46 .replace(/\s+/g, '\\s+')
47
48 splitListString = (str) ->
49 return str.split(/\s*,[\s,]*/)
50
51 module.exports = {
52 applyMigrations
53 convertKey
54 convertPattern
55 splitListString
56 }
Imprint / Impressum