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;
147 if (allowed == "native") {
148 kmod_list_[accent]->allowed= lyx_accent_table[accent].native;
150 kmod_list_[accent]->allowed = allowed;
153 for (string::size_type i = 0; i < keys.length(); ++i) {
155 keymap_[static_cast<unsigned char>(keys[i])];
159 // But the question remains: "Should we be allowed
160 // to change bindings, without unbinding first?"
163 temp += char(accent);
165 kmod_list_[accent]->exception_list = 0;
169 int Trans::Load(LyXLex & lex)
173 while (lex.IsOK() && !error) {
177 if (lyxerr.debugging(Debug::KBMAP))
178 lyxerr << "KMOD:\t" << lex.text() << endl;
179 if (lex.next(true)) {
180 if (lyxerr.debugging(Debug::KBMAP))
181 lyxerr << "key\t`" << lex.text()
186 string keys = lex.GetString();
188 if (lex.next(true)) {
189 if (lyxerr.debugging(Debug::KBMAP))
190 lyxerr << "accent\t`" << lex.text()
195 tex_accent accent = getkeymod(lex.GetString());
197 if (accent == TEX_NOACCENT)
200 if (lex.next(true)) {
201 if (lyxerr.debugging(Debug::KBMAP))
202 lyxerr << "allowed\t`" << lex.text()
207 string allowed = lex.GetString();
209 AddDeadkey(accent, keys, allowed);
215 lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
216 if (lex.next(true)) {
218 lyxerr[Debug::KBMAP] << str << endl;
222 tex_accent accent_1 = getkeymod(str);
223 if (accent_1 == TEX_NOACCENT) return -1;
225 if (lex.next(true)) {
227 lyxerr[Debug::KBMAP] << str << endl;
231 tex_accent accent_2= getkeymod(str);
232 if (accent_2 == TEX_NOACCENT) return -1;
234 if (kmod_list_[accent_1] == 0
235 || kmod_list_[accent_2] == 0)
238 // Find what key accent_2 is on - should
239 // check about accent_1 also
241 for (; key < 256; ++key) {
242 if (!keymap_[key].empty()
243 && keymap_[key][0] == 0
244 && keymap_[key][1] == accent_2)
250 allowed = lex.GetString();
251 lyxerr[Debug::KBMAP] << "allowed: "
256 InsertException(kmod_list_[accent_1]->exception_list,
257 static_cast<char>(key), allowed,
262 unsigned char key_from;
264 if (lyxerr.debugging(Debug::KBMAP))
265 lyxerr << "KMAP:\t" << lex.text() << endl;
266 if (lex.next(true)) {
267 key_from = lex.text()[0];
268 if (lyxerr.debugging(Debug::KBMAP))
269 lyxerr << "\t`" << lex.text() << "'"
274 if (lex.next(true)) {
275 string string_to = lex.text();
277 // strcpy(new char[strlen(t)+1], t);
278 keymap_[key_from] = string_to;
279 if (lyxerr.debugging(Debug::KBMAP))
280 lyxerr << "\t`" << string_to << "'"
292 if (lyxerr.debugging(Debug::KBMAP))
293 lyxerr << "KXMOD:\t" << lex.text() << endl;
294 if (lex.next(true)) {
295 if (lyxerr.debugging(Debug::KBMAP))
296 lyxerr << "\t`" << lex.text() << "'"
298 accent = getkeymod(lex.GetString());
302 if (lex.next(true)) {
303 if (lyxerr.debugging(Debug::KBMAP))
304 lyxerr << "\t`" << lex.text() << "'"
310 if (lex.next(true)) {
311 if (lyxerr.debugging(Debug::KBMAP))
312 lyxerr << "\t`" << lex.text() << "'"
318 InsertException(kmod_list_[accent]->exception_list,
322 case LyXLex::LEX_FEOF:
323 lyxerr[Debug::PARSER] << "End of parsing" << endl;
326 lex.printError("ParseKeymapFile: "
327 "Unknown tag: `$$Token'");
335 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
337 if (kmod_list_[accent] != 0) {
338 i = *kmod_list_[accent];
345 string const Trans::process(char c, TransManager & k)
350 string const t = Match(static_cast<unsigned char>(c));
352 if (t.empty() && c != 0) {
354 return k.normalkey(c, dt);
355 } else if (!t.empty() && t[0] != char(0)) {
357 return k.normalkey(c, dt);
360 *kmod_list_[static_cast<tex_accent>(t[1])]);
365 int Trans::Load(string const & language)
367 string const filename = LibFileSearch("kbd", language, "kmap");
368 if (filename.empty())
372 LyXLex lex(kmapTags, K_LAST-1);
373 lex.setFile(filename);
375 int const res = Load(lex);
386 tex_accent getkeymod(string const & p)
387 /* return modifier - decoded from p and update p */
389 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
390 if (lyxerr.debugging(Debug::KBMAP))
391 lyxerr << "p = " << p
392 << ", lyx_accent_table[" << i
393 << "].name = `" << lyx_accent_table[i].name
396 if (lyx_accent_table[i].name
397 && contains(p, lyx_accent_table[i].name)) {
398 lyxerr[Debug::KBMAP] << "Found it!" << endl;
399 return static_cast<tex_accent>(i);