-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAutoSpell.py
96 lines (73 loc) · 2.82 KB
/
AutoSpell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sublime, sublime_plugin, sys, json, inspect, os
# http://sublimetexttips.com/execute-a-command-every-time-sublime-launches/
def plugin_loaded():
# settings = sublime.load_settings("AutoSpell.sublime-settings")
# settings.add_on_change('default_replacements', build)
# settings.add_on_change('default_triggers', build)
# settings.add_on_change('custom_replacements', build)
# settings.add_on_change('custom_triggers', build)
build()
# def plugin_unloaded():
# settings = sublime.load_settings("AutoSpell.sublime-settings")
# settings.clear_on_change('default_replacements')
# settings.clear_on_change('default_triggers')
# settings.clear_on_change('custom_replacements')
# settings.clear_on_change('custom_triggers')
def build():
sys.stdout.write('Building AutoSpell Index: Started\n')
settings = sublime.load_settings("AutoSpell.sublime-settings")
key_map = []
default_replacements = settings.get('default_replacements', {})
if(default_replacements == False):
default_replacements = {}
elif(type(default_replacements) is not dict):
print('invalid default_replacements in settings')
return
default_triggers = settings.get('default_triggers', [])
if(default_triggers == False):
default_triggers = []
elif(type(default_triggers) is not list):
print('invalid default_triggers in settings')
return
custom_replacements = settings.get('custom_replacements', {})
if(type(custom_replacements) is not dict):
print('invalid custom_replacements in settings')
return
custom_triggers = settings.get('custom_triggers', [])
if(type(custom_triggers) is not list):
print('invalid custom_triggers in settings')
return
# Combine them
default_replacements.update(custom_replacements)
replacements = default_replacements
triggers = list(set(default_triggers + custom_triggers))
for mispelled in replacements:
replacement = replacements[mispelled]
for key in triggers:
char = key
if char == 'enter':
char = '\n'
# lowercase version
entry = {'command': 'insert', 'args': {}}
entry['args']['characters'] = replacement + char
entry['keys'] = list(mispelled)
entry['keys'].append(key)
key_map.append(entry)
# TODO make it DRY
# capitalize first letter version
entry = {'command': 'insert', 'args': {}}
entry['args']['characters'] = (replacement + char).capitalize()
entry['keys'] = list(mispelled.capitalize())
entry['keys'].append(key)
key_map.append(entry)
if(not key_map):
print('no key maps')
return
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
key_map_data = open('%s/Default.sublime-keymap' % current_dir, 'w')
key_map_data.write(json.dumps(key_map))
key_map_data.close()
sys.stdout.write('Building AutoSpell Index: Finished\n')
class AutoSpell(sublime_plugin.ApplicationCommand):
def run(self):
print('run AutoSpell')