]> git.gir.st - VimFx.git/blob - extension/lib/parse-prefs.coffee
Major refactor: Rework all UI and related improvements
[VimFx.git] / extension / lib / parse-prefs.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 utils = require('./utils')
22
23 MIN_NUM_HINT_CHARS = 2
24
25 parsePref = (pref) ->
26 # Parsed options are not stored in Firefox’s prefs system, and are therefore
27 # always read from the defaults. The only way to override them is via the
28 # public API.
29 if pref of defaults.parsed_options
30 return defaults.parsed_options[pref]
31
32 value = prefs.get(pref)
33
34 if pref of parsers
35 { parsed, normalized } = parsers[pref](value, defaults.all_options[pref])
36 if normalized? and normalized != value and prefs.has(pref)
37 prefs.set(pref, normalized)
38 return parsed
39
40 return value
41
42 # Splits a whitespace delimited string into an array, with empty elements and
43 # duplicates filtered.
44 parseSpaceDelimitedString = (value) ->
45 parsed = utils.removeDuplicates(value.split(/\s+/)).filter(Boolean)
46 return {parsed, normalized: parsed.join(' ')}
47
48 parsePatterns = (value) ->
49 result = parseSpaceDelimitedString(value)
50 # The patterns are case insensitive regexes and must match either in the
51 # beginning or at the end of a string. They do not match in the middle of
52 # words, so "previous" does not match "previously" (`previous\S*` can be used
53 # for that case). Note: `\s` is used instead of `\b` since it works better
54 # with non-English characters.
55 result.parsed = result.parsed.map((pattern) ->
56 patternRegex =
57 try
58 RegExp(pattern).source
59 catch
60 utils.regexpEscape(pattern)
61 return ///
62 ^\s* (?:#{ patternRegex }) (?:\s|$)
63 |
64 (?:\s|^) (?:#{ patternRegex }) \s*$
65 ///i
66 )
67 return result
68
69 parsers =
70 hint_chars: (value, defaultValue) ->
71 parsed = utils.removeDuplicateCharacters(value).replace(/\s/g, '')
72 # Make sure that hint chars contain at least the required amount of chars.
73 if parsed.length < MIN_NUM_HINT_CHARS
74 parsed = defaultValue[...MIN_NUM_HINT_CHARS]
75 return {parsed, normalized: parsed}
76
77 prev_patterns: parsePatterns
78 next_patterns: parsePatterns
79
80 black_list: (value) ->
81 result = parseSpaceDelimitedString(value)
82 result.parsed = result.parsed.map((pattern) ->
83 return ///^#{ utils.regexpEscape(pattern).replace(/\\\*/g, '.*') }$///i
84 )
85 return result
86
87 adjustable_element_keys: parseSpaceDelimitedString
88 activatable_element_keys: parseSpaceDelimitedString
89
90 pattern_attrs: parseSpaceDelimitedString
91
92 module.exports = parsePref
Imprint / Impressum