9 #include "support/filetools.h"
10 #include "tex-strings.h"
13 #include "trans_mgr.h"
29 bool DefaultTrans::init_ = false;
32 DefaultTrans::DefaultTrans()
42 string const DefaultTrans::process(char c, TransManager & k)
47 return k.normalkey(c, dummy);
50 string const DefaultTrans::process(char c, TransManager & k)
52 return k.normalkey(c);
62 for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
74 void Trans::InsertException(KmodException & exclist, char c,
75 string const & data, bool flag, tex_accent accent)
78 keyexc p = new Keyexc;
93 exclist.insert(exclist.begin(), p);
95 // exclist.push_back(p);
100 void Trans::FreeException(KmodException & exclist)
103 Trans::keyexc p = exclist;
115 void Trans::FreeKeymap()
118 for (int i = 0; i < 256; ++i)
119 if (!keymap_[i].empty()) {
122 for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
124 FreeException(kmod_list_[i]->exception_list);
125 delete kmod_list_[i];
135 bool Trans::IsDefined() const
137 return !name_.empty();
141 string const & Trans::GetName() const
156 struct keyword_item kmapTags[K_LAST - 1] = {
164 tex_accent getkeymod(string const &);
168 void Trans::AddDeadkey(tex_accent accent, string const & keys,
169 string const & allowed)
171 void Trans::AddDeadkey(tex_accent accent, string const & keys)
175 if (kmod_list_[accent]) {
176 FreeException(kmod_list_[accent]->exception_list);
178 delete kmod_list_[accent];
181 kmod_list_[accent] = new KmodInfo;
182 kmod_list_[accent]->data = keys;
183 kmod_list_[accent]->accent = accent;
188 kmod_list_[accent] = tmp;
191 if (allowed == "native") {
192 kmod_list_[accent]->allowed= lyx_accent_table[accent].native;
194 kmod_list_[accent]->allowed = allowed;
197 for (string::size_type i = 0; i < keys.length(); ++i) {
199 keymap_[static_cast<unsigned char>(keys[i])];
203 // But the question remains: "Should we be allowed
204 // to change bindings, without unbinding first?"
207 temp += char(accent);
210 for (string::size_type i = 0; i < keys.length(); ++i) {
214 keymap_[keys[i]] = tmp;
218 kmod_list_[accent]->exception_list = 0;
223 int Trans::Load(LyXLex & lex)
227 while (lex.IsOK() && !error) {
231 if (lyxerr.debugging(Debug::KBMAP))
232 lyxerr << "KMOD:\t" << lex.text() << endl;
233 if (lex.next(true)) {
234 if (lyxerr.debugging(Debug::KBMAP))
235 lyxerr << "key\t`" << lex.text()
240 string const keys = lex.GetString();
242 if (lex.next(true)) {
243 if (lyxerr.debugging(Debug::KBMAP))
244 lyxerr << "accent\t`" << lex.text()
249 tex_accent accent = getkeymod(lex.GetString());
251 if (accent == TEX_NOACCENT)
255 #warning This code should be removed...
256 // But we need to fix up all the kmap files first
257 // so that this field is not present anymore.
258 if (lex.next(true)) {
259 if (lyxerr.debugging(Debug::KBMAP))
260 lyxerr << "allowed\t`" << lex.text()
265 string const allowed = lex.GetString();
266 AddDeadkey(accent, keys /*, allowed*/);
268 AddDeadkey(accent, keys);
275 lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
276 if (lex.next(true)) {
278 lyxerr[Debug::KBMAP] << str << endl;
282 tex_accent accent_1 = getkeymod(str);
283 if (accent_1 == TEX_NOACCENT) return -1;
285 if (lex.next(true)) {
287 lyxerr[Debug::KBMAP] << str << endl;
291 tex_accent accent_2= getkeymod(str);
292 if (accent_2 == TEX_NOACCENT) return -1;
295 if (kmod_list_[accent_1] == 0
296 || kmod_list_[accent_2] == 0)
299 std::map<int, KmodInfo>::iterator it1 =
300 kmod_list_.find(accent_1);
301 std::map<int, KmodInfo>::iterator it2 =
302 kmod_list_.find(accent_2);
303 if (it1 == kmod_list_.end()
304 || it2 == kmod_list_.end()) {
309 // Find what key accent_2 is on - should
310 // check about accent_1 also
313 for (; key < 256; ++key) {
314 if (!keymap_[key].empty()
315 && keymap_[key][0] == 0
316 && keymap_[key][1] == accent_2)
322 allowed = lex.GetString();
323 lyxerr[Debug::KBMAP] << "allowed: "
328 InsertException(kmod_list_[accent_1].exception_list,
329 static_cast<char>(key), allowed,
332 std::map<int, string>::iterator it = keymap_.begin();
333 std::map<int, string>::iterator end = keymap_.end();
334 for (; it != end; ++it) {
335 if (!it->second.empty()
336 && it->second[0] == 0
337 && it->second[1] == accent_2)
342 allowed = lex.GetString();
343 lyxerr[Debug::KBMAP] << "allowed: "
349 InsertException(kmod_list_[accent_1].exception_list,
350 static_cast<char>(it->first), allowed,
356 unsigned char key_from;
358 if (lyxerr.debugging(Debug::KBMAP))
359 lyxerr << "KMAP:\t" << lex.text() << endl;
360 if (lex.next(true)) {
361 key_from = lex.text()[0];
362 if (lyxerr.debugging(Debug::KBMAP))
363 lyxerr << "\t`" << lex.text() << "'"
368 if (lex.next(true)) {
369 string string_to = lex.text();
371 keymap_[key_from] = string_to;
373 keymap_[key_from] = string_to;
375 if (lyxerr.debugging(Debug::KBMAP))
376 lyxerr << "\t`" << string_to << "'"
388 if (lyxerr.debugging(Debug::KBMAP))
389 lyxerr << "KXMOD:\t" << lex.text() << endl;
390 if (lex.next(true)) {
391 if (lyxerr.debugging(Debug::KBMAP))
392 lyxerr << "\t`" << lex.text() << "'"
394 accent = getkeymod(lex.GetString());
398 if (lex.next(true)) {
399 if (lyxerr.debugging(Debug::KBMAP))
400 lyxerr << "\t`" << lex.text() << "'"
406 if (lex.next(true)) {
407 if (lyxerr.debugging(Debug::KBMAP))
408 lyxerr << "\t`" << lex.text() << "'"
414 InsertException(kmod_list_[accent].exception_list,
418 case LyXLex::LEX_FEOF:
419 lyxerr[Debug::PARSER] << "End of parsing" << endl;
422 lex.printError("ParseKeymapFile: "
423 "Unknown tag: `$$Token'");
431 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
434 if (kmod_list_[accent] != 0) {
435 i = *kmod_list_[accent];
440 std::map<int, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
441 if (cit != kmod_list_.end()) {
451 string const Trans::process(char c, TransManager & k)
454 string const t = Match(static_cast<unsigned char>(c));
456 if (t.empty() && c != 0) {
458 return k.normalkey(c, dt);
459 } else if (!t.empty() && t[0] != char(0)) {
461 return k.normalkey(c, dt);
464 *kmod_list_[static_cast<tex_accent>(t[1])]);
468 string const Trans::process(char c, TransManager & k)
470 string const t = Match(static_cast<unsigned char>(c));
472 if (t.empty() && c != 0) {
473 return k.normalkey(c);
474 } else if (!t.empty() && t[0] != char(0)) {
475 //return k.normalkey(c);
480 *kmod_list_[static_cast<tex_accent>(t[1])]);
483 kmod_list_[static_cast<tex_accent>(t[1])]);
490 int Trans::Load(string const & language)
492 string const filename = LibFileSearch("kbd", language, "kmap");
493 if (filename.empty())
497 LyXLex lex(kmapTags, K_LAST-1);
498 lex.setFile(filename);
500 int const res = Load(lex);
511 tex_accent getkeymod(string const & p)
512 /* return modifier - decoded from p and update p */
514 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
515 if (lyxerr.debugging(Debug::KBMAP))
516 lyxerr << "p = " << p
517 << ", lyx_accent_table[" << i
518 << "].name = `" << lyx_accent_table[i].name
521 if (lyx_accent_table[i].name
522 && contains(p, lyx_accent_table[i].name)) {
523 lyxerr[Debug::KBMAP] << "Found it!" << endl;
524 return static_cast<tex_accent>(i);