]> git.gir.st - VimFx.git/blob - extension/lib/legacy.coffee
Use destructuring defaults from CoffeeScript 1.10.0
[VimFx.git] / extension / lib / legacy.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 notation = require('vim-like-key-notation')
21 prefs = require('./prefs')
22 utils = require('./utils')
23
24 applyMigrations = (migrations) ->
25 for migration, index in migrations
26 pref = "migration.#{ index }.applied"
27 # This allows to manually choose migrations to apply. Be careful, though,
28 # since some migrations might have to run in order!
29 unless prefs.has(pref) and prefs.get(pref)
30 migration()
31 prefs.set(pref, true)
32 return
33
34 commaSeparatedList = /.[^,]*,?/g
35
36 commaSeparatedListItem = ///^
37 (?:
38 (?: (Shift) | ([acm])+ )
39 -
40 )?
41 (.+?)
42 ,?
43 $///
44
45 convertKey = (keyStr) ->
46 return keyStr.match(commaSeparatedList).map((part) ->
47 [match, shift, modifiers, key] = part.match(commaSeparatedListItem)
48 modifiers ?= ''
49 return notation.stringify({
50 key
51 shiftKey: Boolean(shift)
52 altKey: 'a' in modifiers
53 ctrlKey: 'c' in modifiers
54 metaKey: 'm' in modifiers
55 })
56 )
57
58 convertPattern = (pattern) ->
59 return utils.regexpEscape(pattern)
60 .replace(/\\\*/g, '.*')
61 .replace(/!/g, '.')
62 .replace(/\s+/g, '\\s+')
63
64 splitListString = (str) ->
65 return str.split(/\s*,[\s,]*/)
66
67 module.exports = {
68 applyMigrations
69 convertKey
70 convertPattern
71 splitListString
72 }
Imprint / Impressum