4 #pragma implementation "trans.h"
9 #include "support/filetools.h"
10 #include "tex-strings.h"
13 #include "trans_mgr.h"
26 bool DefaultTrans::init_ = false;
29 DefaultTrans::DefaultTrans()
38 string const DefaultTrans::process(char c, TransManager & k)
43 return k.normalkey(c, dummy);
51 for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
62 void Trans::InsertException(Trans::keyexc & exclist, char c,
63 string const & data, bool flag, tex_accent accent)
65 keyexc p = new Keyexc;
77 void Trans::FreeException(Trans::keyexc & exclist)
79 Trans::keyexc p = exclist;
88 void Trans::FreeKeymap()
90 for (int i = 0; i < 256; ++i)
91 if (!keymap_[i].empty()) {
94 for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
96 FreeException(kmod_list_[i]->exception_list);
103 bool Trans::IsDefined() const
105 return !name_.empty();
109 string const & Trans::GetName() const
124 struct keyword_item kmapTags[K_LAST - 1] = {
132 tex_accent getkeymod(string const &);
135 void Trans::AddDeadkey(tex_accent accent, string const & keys,
136 string const & allowed)
138 if (kmod_list_[accent]) {
139 FreeException(kmod_list_[accent]->exception_list);
141 delete kmod_list_[accent];
144 kmod_list_[accent] = new kmod_list_decl;
145 kmod_list_[accent]->data = keys;
146 kmod_list_[accent]->accent = accent;
148 if (allowed == "native") {
149 kmod_list_[accent]->allowed= lyx_accent_table[accent].native;
152 kmod_list_[accent]->allowed = allowed;
157 for (string::size_type i = 0; i < keys.length(); ++i) {
159 keymap_[static_cast<unsigned char>(keys[i])];
163 // But the question remains: "Should we be allowed
164 // to change bindings, without unbinding first?"
167 temp += char(accent);
169 kmod_list_[accent]->exception_list = 0;
173 int Trans::Load(LyXLex & lex)
177 while (lex.IsOK() && !error) {
181 if (lyxerr.debugging(Debug::KBMAP))
182 lyxerr << "KMOD:\t" << lex.text() << endl;
183 if (lex.next(true)) {
184 if (lyxerr.debugging(Debug::KBMAP))
185 lyxerr << "key\t`" << lex.text()
190 string keys = lex.GetString();
192 if (lex.next(true)) {
193 if (lyxerr.debugging(Debug::KBMAP))
194 lyxerr << "accent\t`" << lex.text()
199 tex_accent accent = getkeymod(lex.GetString());
201 if (accent == TEX_NOACCENT)
204 if (lex.next(true)) {
205 if (lyxerr.debugging(Debug::KBMAP))
206 lyxerr << "allowed\t`" << lex.text()
211 string allowed = lex.GetString();
213 AddDeadkey(accent, keys, allowed);
219 lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
220 if (lex.next(true)) {
222 lyxerr[Debug::KBMAP] << str << endl;
226 tex_accent accent_1 = getkeymod(str);
227 if (accent_1 == TEX_NOACCENT) return -1;
229 if (lex.next(true)) {
231 lyxerr[Debug::KBMAP] << str << endl;
235 tex_accent accent_2= getkeymod(str);
236 if (accent_2 == TEX_NOACCENT) return -1;
238 if (kmod_list_[accent_1] == 0
239 || kmod_list_[accent_2] == 0)
242 // Find what key accent_2 is on - should
243 // check about accent_1 also
245 for (; key < 256; ++key) {
246 if (!keymap_[key].empty()
247 && keymap_[key][0] == 0
248 && keymap_[key][1] == accent_2)
254 allowed = lex.GetString();
255 lyxerr[Debug::KBMAP] << "allowed: "
260 InsertException(kmod_list_[accent_1]->exception_list,
261 static_cast<char>(key), allowed,
266 unsigned char key_from;
268 if (lyxerr.debugging(Debug::KBMAP))
269 lyxerr << "KMAP:\t" << lex.text() << endl;
270 if (lex.next(true)) {
271 key_from = lex.text()[0];
272 if (lyxerr.debugging(Debug::KBMAP))
273 lyxerr << "\t`" << lex.text() << "'"
278 if (lex.next(true)) {
279 string string_to = lex.text();
281 // strcpy(new char[strlen(t)+1], t);
282 keymap_[key_from] = string_to;
283 if (lyxerr.debugging(Debug::KBMAP))
284 lyxerr << "\t`" << string_to << "'"
296 if (lyxerr.debugging(Debug::KBMAP))
297 lyxerr << "KXMOD:\t" << lex.text() << endl;
298 if (lex.next(true)) {
299 if (lyxerr.debugging(Debug::KBMAP))
300 lyxerr << "\t`" << lex.text() << "'"
302 accent = getkeymod(lex.GetString());
306 if (lex.next(true)) {
307 if (lyxerr.debugging(Debug::KBMAP))
308 lyxerr << "\t`" << lex.text() << "'"
314 if (lex.next(true)) {
315 if (lyxerr.debugging(Debug::KBMAP))
316 lyxerr << "\t`" << lex.text() << "'"
322 InsertException(kmod_list_[accent]->exception_list,
326 case LyXLex::LEX_FEOF:
327 lyxerr[Debug::PARSER] << "End of parsing" << endl;
330 lex.printError("ParseKeymapFile: "
331 "Unknown tag: `$$Token'");
339 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
341 if (kmod_list_[accent] != 0) {
342 i = *kmod_list_[accent];
349 string const Trans::process(char c, TransManager & k)
354 string const t = Match(static_cast<unsigned char>(c));
356 if (t.empty() && c != 0) {
358 return k.normalkey(c, dt);
359 } else if (!t.empty() && t[0] != char(0)) {
361 return k.normalkey(c, dt);
364 *kmod_list_[static_cast<tex_accent>(t[1])]);
369 int Trans::Load(string const & language)
371 string const filename = LibFileSearch("kbd", language, "kmap");
372 if (filename.empty())
376 LyXLex lex(kmapTags, K_LAST-1);
377 lex.setFile(filename);
379 int const res = Load(lex);
390 tex_accent getkeymod(string const & p)
391 /* return modifier - decoded from p and update p */
393 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
394 if (lyxerr.debugging(Debug::KBMAP))
395 lyxerr << "p = " << p
396 << ", lyx_accent_table[" << i
397 << "].name = `" << lyx_accent_table[i].name
400 if (lyx_accent_table[i].name
401 && contains(p, lyx_accent_table[i].name)) {
402 lyxerr[Debug::KBMAP] << "Found it!" << endl;
403 return static_cast<tex_accent>(i);