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()
39 string const DefaultTrans::process(char c, TransManager & k)
44 return k.normalkey(c, dummy);
47 string const DefaultTrans::process(char c, TransManager & k)
49 return k.normalkey(c);
58 for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
69 void Trans::InsertException(Trans::keyexc & exclist, char c,
70 string const & data, bool flag, tex_accent accent)
72 keyexc p = new Keyexc;
84 void Trans::FreeException(Trans::keyexc & exclist)
86 Trans::keyexc p = exclist;
95 void Trans::FreeKeymap()
97 for (int i = 0; i < 256; ++i)
98 if (!keymap_[i].empty()) {
101 for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
103 FreeException(kmod_list_[i]->exception_list);
104 delete kmod_list_[i];
110 bool Trans::IsDefined() const
112 return !name_.empty();
116 string const & Trans::GetName() const
131 struct keyword_item kmapTags[K_LAST - 1] = {
139 tex_accent getkeymod(string const &);
142 void Trans::AddDeadkey(tex_accent accent, string const & keys,
143 string const & allowed)
145 if (kmod_list_[accent]) {
146 FreeException(kmod_list_[accent]->exception_list);
148 delete kmod_list_[accent];
151 kmod_list_[accent] = new kmod_list_decl;
152 kmod_list_[accent]->data = keys;
153 kmod_list_[accent]->accent = accent;
155 if (allowed == "native") {
156 kmod_list_[accent]->allowed= lyx_accent_table[accent].native;
159 kmod_list_[accent]->allowed = allowed;
164 for (string::size_type i = 0; i < keys.length(); ++i) {
166 keymap_[static_cast<unsigned char>(keys[i])];
170 // But the question remains: "Should we be allowed
171 // to change bindings, without unbinding first?"
174 temp += char(accent);
176 kmod_list_[accent]->exception_list = 0;
180 int Trans::Load(LyXLex & lex)
184 while (lex.IsOK() && !error) {
188 if (lyxerr.debugging(Debug::KBMAP))
189 lyxerr << "KMOD:\t" << lex.text() << endl;
190 if (lex.next(true)) {
191 if (lyxerr.debugging(Debug::KBMAP))
192 lyxerr << "key\t`" << lex.text()
197 string keys = lex.GetString();
199 if (lex.next(true)) {
200 if (lyxerr.debugging(Debug::KBMAP))
201 lyxerr << "accent\t`" << lex.text()
206 tex_accent accent = getkeymod(lex.GetString());
208 if (accent == TEX_NOACCENT)
211 if (lex.next(true)) {
212 if (lyxerr.debugging(Debug::KBMAP))
213 lyxerr << "allowed\t`" << lex.text()
218 string allowed = lex.GetString();
220 AddDeadkey(accent, keys, allowed);
226 lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
227 if (lex.next(true)) {
229 lyxerr[Debug::KBMAP] << str << endl;
233 tex_accent accent_1 = getkeymod(str);
234 if (accent_1 == TEX_NOACCENT) return -1;
236 if (lex.next(true)) {
238 lyxerr[Debug::KBMAP] << str << endl;
242 tex_accent accent_2= getkeymod(str);
243 if (accent_2 == TEX_NOACCENT) return -1;
245 if (kmod_list_[accent_1] == 0
246 || kmod_list_[accent_2] == 0)
249 // Find what key accent_2 is on - should
250 // check about accent_1 also
252 for (; key < 256; ++key) {
253 if (!keymap_[key].empty()
254 && keymap_[key][0] == 0
255 && keymap_[key][1] == accent_2)
261 allowed = lex.GetString();
262 lyxerr[Debug::KBMAP] << "allowed: "
267 InsertException(kmod_list_[accent_1]->exception_list,
268 static_cast<char>(key), allowed,
273 unsigned char key_from;
275 if (lyxerr.debugging(Debug::KBMAP))
276 lyxerr << "KMAP:\t" << lex.text() << endl;
277 if (lex.next(true)) {
278 key_from = lex.text()[0];
279 if (lyxerr.debugging(Debug::KBMAP))
280 lyxerr << "\t`" << lex.text() << "'"
285 if (lex.next(true)) {
286 string string_to = lex.text();
288 // strcpy(new char[strlen(t)+1], t);
289 keymap_[key_from] = string_to;
290 if (lyxerr.debugging(Debug::KBMAP))
291 lyxerr << "\t`" << string_to << "'"
303 if (lyxerr.debugging(Debug::KBMAP))
304 lyxerr << "KXMOD:\t" << lex.text() << endl;
305 if (lex.next(true)) {
306 if (lyxerr.debugging(Debug::KBMAP))
307 lyxerr << "\t`" << lex.text() << "'"
309 accent = getkeymod(lex.GetString());
313 if (lex.next(true)) {
314 if (lyxerr.debugging(Debug::KBMAP))
315 lyxerr << "\t`" << lex.text() << "'"
321 if (lex.next(true)) {
322 if (lyxerr.debugging(Debug::KBMAP))
323 lyxerr << "\t`" << lex.text() << "'"
329 InsertException(kmod_list_[accent]->exception_list,
333 case LyXLex::LEX_FEOF:
334 lyxerr[Debug::PARSER] << "End of parsing" << endl;
337 lex.printError("ParseKeymapFile: "
338 "Unknown tag: `$$Token'");
346 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
348 if (kmod_list_[accent] != 0) {
349 i = *kmod_list_[accent];
357 string const Trans::process(char c, TransManager & k)
360 string const t = Match(static_cast<unsigned char>(c));
362 if (t.empty() && c != 0) {
364 return k.normalkey(c, dt);
365 } else if (!t.empty() && t[0] != char(0)) {
367 return k.normalkey(c, dt);
370 *kmod_list_[static_cast<tex_accent>(t[1])]);
374 string const Trans::process(char c, TransManager & k)
376 string const t = Match(static_cast<unsigned char>(c));
378 if (t.empty() && c != 0) {
379 return k.normalkey(c);
380 } else if (!t.empty() && t[0] != char(0)) {
381 return k.normalkey(c);
384 *kmod_list_[static_cast<tex_accent>(t[1])]);
390 int Trans::Load(string const & language)
392 string const filename = LibFileSearch("kbd", language, "kmap");
393 if (filename.empty())
397 LyXLex lex(kmapTags, K_LAST-1);
398 lex.setFile(filename);
400 int const res = Load(lex);
411 tex_accent getkeymod(string const & p)
412 /* return modifier - decoded from p and update p */
414 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
415 if (lyxerr.debugging(Debug::KBMAP))
416 lyxerr << "p = " << p
417 << ", lyx_accent_table[" << i
418 << "].name = `" << lyx_accent_table[i].name
421 if (lyx_accent_table[i].name
422 && contains(p, lyx_accent_table[i].name)) {
423 lyxerr[Debug::KBMAP] << "Found it!" << endl;
424 return static_cast<tex_accent>(i);