-
Notifications
You must be signed in to change notification settings - Fork 367
/
detect_spellcheck.py
177 lines (154 loc) · 5.45 KB
/
detect_spellcheck.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import sublime_plugin
from .latextools_utils import settings, tex_directives
from .getTeXRoot import get_tex_root
try: # check whether the dictionaries package is installed
import Dictionaries
_DICT_INSTALLED = True
except:
_DICT_INSTALLED = False
from .deprecated_command import deprecate
class DictMissing(Exception):
pass
def normalize_locale(loc):
"""normalizes the locale into the used format"""
return loc.lower().replace("_", "-")
if _DICT_INSTALLED:
# mapping from the locales to the names in the dictionary package
_dictionary_mappings = {
"eu": "Basque.dic",
"bg": "Bulgarian.dic",
"ca": "Catalan.dic",
"hr": "Croatian.dic",
"cs": "Czech.dic",
"da": "Danish.dic",
"nl": "Dutch.dic",
"nl-be": "Dutch.dic",
"nl-nl": "Dutch.dic",
"en": "English (American).dic",
"en-en": "English (American).dic",
"en-us": "English (American).dic",
"en-au": "English (Australian).dic",
"en-gb": "English (British).dic",
"en-ca": "English (Canadian).dic",
"en-za": "English (South African).dic",
"et": "Estonian.dic",
"fr": "French.dic",
"fr-be": "French.dic",
"fr-ca": "French.dic",
"fr-fr": "French.dic",
"de": "German.dic",
"de-at": "German_de_AT.dic",
"de-ch": "German_de_CH.dic",
"de-de": "German_de_DE.dic",
"el": "Greek.dic",
"hu": "Hungarian.dic",
"it": "Italian.dic",
"it-it": "Italian.dic",
"it-ch": "Italian.dic",
"lt": "Lithuanian.dic",
"mn": "Mongolian.dic",
"nb": "Norwegian (Bokmal).dic",
"nn": "Norwegian (Nynorsk).dic",
"no": "Norwegian (Nynorsk).dic",
"no-no": "Norwegian (Nynorsk).dic",
"pl": "Polish.dic",
"pt-br": "Portuguese (Brazilian).dic",
"pt": "Portuguese (European).dic",
"pt-pt": "Portuguese (European).dic",
"ro": "Romanian (Modern).dic",
"ro-mo": "Romanian (Modern).dic",
"ru": "Russian.dic",
"ru-mo": "Russian.dic",
"sr": "Serbian (Cyrillic).dic",
"sr-sp": "Serbian (Cyrillic).dic",
"sk": "Slovak_sk_SK.dic",
"sk-sk": "Slovak_sk_SK.dic",
"sl": "Slovenian.dic",
"es": "Spanish.dic",
"es-ar": "Spanish.dic",
"es-bo": "Spanish.dic",
"es-cl": "Spanish.dic",
"es-co": "Spanish.dic",
"es-cr": "Spanish.dic",
"es-do": "Spanish.dic",
"es-ec": "Spanish.dic",
"es-sv": "Spanish.dic",
"es-gt": "Spanish.dic",
"es-hn": "Spanish.dic",
"es-mx": "Spanish.dic",
"es-ni": "Spanish.dic",
"es-pa": "Spanish.dic",
"es-py": "Spanish.dic",
"es-pe": "Spanish.dic",
"es-pr": "Spanish.dic",
"es-es": "Spanish.dic",
"es-uy": "Spanish.dic",
"es-ve": "Spanish.dic",
"sv": "Swedish.dic",
"sv-sv": "Swedish.dic",
"sv-ar": "Swedish.dic",
"sv-fi": "Swedish.dic",
"tr": "Turkish.dic",
"uk": "Ukrainian_uk_UA.dic",
"uk-ua": "Ukrainian_uk_UA.dic",
"vi": "Vietnamese_vi_VN.dic",
"vi-vn": "Vietnamese_vi_VN.dic"
}
def get_dict_path(loc):
loc = normalize_locale(loc)
try:
dict_name = _dictionary_mappings[loc]
except:
raise DictMissing()
dict_path = "Packages/Dictionaries/" + dict_name
return dict_path
else:
def get_dict_path(loc):
loc = normalize_locale(loc)
if loc == "en-gb":
return "Packages/Language - English/en_GB.dic"
elif loc in ["en", "en-us", "en-en"]:
return "Packages/Language - English/en_US.dic"
else:
raise DictMissing()
def _get_locale(view):
sc_option = tex_directives.\
parse_tex_directives(view, only_for=["spellcheck"])
return sc_option.get("spellcheck")
def _get_locale_from_tex_root(view):
tex_root = get_tex_root(view)
if not tex_root or tex_root == view.file_name():
return
return _get_locale(tex_root)
def update_dict_language(view, extract_from_root):
loc = (_get_locale(view) or
extract_from_root and _get_locale_from_tex_root(view))
if not loc:
return # no spellcheck directive found
try:
user_sc = settings.get_setting("tex_spellcheck_paths", {})
dict_path = user_sc.get(loc) or get_dict_path(loc)
except DictMissing:
print("dict definition missing for locale '{0}'"
.format(loc))
return # no dict defined for locale
current_dict = view.settings().get("dictionary")
if current_dict == dict_path:
return # the locale is already set
view.settings().set("dictionary", dict_path)
print("Changed dictionary to '{0}'".format(dict_path))
class LatextoolsAutoDetectSpellcheckListener(sublime_plugin.EventListener):
def on_post_save(self, view):
if not view.score_selector(0, "text.tex.latex"):
return
update_dict_language(view, False)
def on_load_event(self, view):
if not view.score_selector(0, "text.tex.latex"):
return
update_dict_language(view, True)
on_load_async = on_load_event
class LatextoolsDetectSpellcheckCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
update_dict_language(view)
deprecate(globals(), 'LatexDetectSpellcheckCommand', LatextoolsDetectSpellcheckCommand)