3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * Full author contact details are available in file CREDITS.
15 #include "support/filetools.h"
16 #include "support/lstrings.h"
19 #include "trans_mgr.h"
24 using support::contains;
25 using support::libFileSearch;
51 void Trans::insertException(KmodException & exclist, char c,
52 string const & data, bool flag, tex_accent accent)
59 exclist.insert(exclist.begin(), p);
61 // exclist.push_back(p);
65 void Trans::freeException(KmodException & exclist)
71 void Trans::freeKeymap()
78 bool Trans::isDefined() const
80 return !name_.empty();
84 string const & Trans::getName() const
99 struct keyword_item kmapTags[K_LAST - 1] = {
107 tex_accent getkeymod(string const &);
110 void Trans::addDeadkey(tex_accent accent, string const & keys)
115 kmod_list_[accent] = tmp;
117 for (string::size_type i = 0; i < keys.length(); ++i) {
121 keymap_[keys[i]] = tmp;
126 int Trans::load(LyXLex & lex)
130 while (lex.isOK() && !error) {
134 if (lyxerr.debugging(Debug::KBMAP))
135 lyxerr << "KMOD:\t" << lex.getString() << endl;
136 if (lex.next(true)) {
137 if (lyxerr.debugging(Debug::KBMAP))
138 lyxerr << "key\t`" << lex.getString()
143 string const keys = lex.getString();
145 if (lex.next(true)) {
146 if (lyxerr.debugging(Debug::KBMAP))
147 lyxerr << "accent\t`" << lex.getString()
152 tex_accent accent = getkeymod(lex.getString());
154 if (accent == TEX_NOACCENT)
158 //#warning This code should be removed...
159 // But we need to fix up all the kmap files first
160 // so that this field is not present anymore.
161 if (lex.next(true)) {
162 if (lyxerr.debugging(Debug::KBMAP))
163 lyxerr << "allowed\t`" << lex.getString()
168 /* string const allowed = lex.getString(); */
169 addDeadkey(accent, keys /*, allowed*/);
171 addDeadkey(accent, keys);
178 lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
179 if (lex.next(true)) {
180 str = lex.getString();
181 lyxerr[Debug::KBMAP] << str << endl;
185 tex_accent accent_1 = getkeymod(str);
186 if (accent_1 == TEX_NOACCENT) return -1;
188 if (lex.next(true)) {
189 str = lex.getString();
190 lyxerr[Debug::KBMAP] << str << endl;
194 tex_accent accent_2= getkeymod(str);
195 if (accent_2 == TEX_NOACCENT) return -1;
197 map<int, KmodInfo>::iterator it1 =
198 kmod_list_.find(accent_1);
199 map<int, KmodInfo>::iterator it2 =
200 kmod_list_.find(accent_2);
201 if (it1 == kmod_list_.end()
202 || it2 == kmod_list_.end()) {
206 // Find what key accent_2 is on - should
207 // check about accent_1 also
208 map<int, string>::iterator it = keymap_.begin();
209 map<int, string>::iterator end = keymap_.end();
210 for (; it != end; ++it) {
211 if (!it->second.empty()
212 && it->second[0] == 0
213 && it->second[1] == accent_2)
218 allowed = lex.getString();
219 lyxerr[Debug::KBMAP] << "allowed: "
225 insertException(kmod_list_[accent_1].exception_list,
226 static_cast<char>(it->first), allowed,
231 unsigned char key_from;
233 if (lyxerr.debugging(Debug::KBMAP))
234 lyxerr << "KMAP:\t" << lex.getString() << endl;
235 if (lex.next(true)) {
236 key_from = lex.getString()[0];
237 if (lyxerr.debugging(Debug::KBMAP))
238 lyxerr << "\t`" << lex.getString() << '\''
243 if (lex.next(true)) {
244 string const string_to = lex.getString();
245 keymap_[key_from] = string_to;
246 if (lyxerr.debugging(Debug::KBMAP))
247 lyxerr << "\t`" << string_to << '\''
259 if (lyxerr.debugging(Debug::KBMAP))
260 lyxerr << "KXMOD:\t" << lex.getString() << endl;
261 if (lex.next(true)) {
262 if (lyxerr.debugging(Debug::KBMAP))
263 lyxerr << "\t`" << lex.getString() << '\''
265 accent = getkeymod(lex.getString());
269 if (lex.next(true)) {
270 if (lyxerr.debugging(Debug::KBMAP))
271 lyxerr << "\t`" << lex.getString() << '\''
273 key = lex.getString()[0];
277 if (lex.next(true)) {
278 if (lyxerr.debugging(Debug::KBMAP))
279 lyxerr << "\t`" << lex.getString() << '\''
281 str = lex.getString();
285 insertException(kmod_list_[accent].exception_list,
289 case LyXLex::LEX_FEOF:
290 lyxerr[Debug::PARSER] << "End of parsing" << endl;
293 lex.printError("ParseKeymapFile: "
294 "Unknown tag: `$$Token'");
302 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
304 map<int, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
305 if (cit != kmod_list_.end()) {
313 string const Trans::process(char c, TransManager & k)
315 string const t = match(static_cast<unsigned char>(c));
317 if (t.empty() && c != 0) {
318 return k.normalkey(c);
319 } else if (!t.empty() && t[0] != char(0)) {
320 //return k.normalkey(c);
324 kmod_list_[static_cast<tex_accent>(t[1])]);
329 int Trans::load(string const & language)
331 string const filename = libFileSearch("kbd", language, "kmap");
332 if (filename.empty())
336 LyXLex lex(kmapTags, K_LAST - 1);
337 lex.setFile(filename);
339 int const res = load(lex);
350 tex_accent getkeymod(string const & p)
351 /* return modifier - decoded from p and update p */
353 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
354 if (lyxerr.debugging(Debug::KBMAP))
355 lyxerr << "p = " << p
356 << ", lyx_accent_table[" << i
357 << "].name = `" << lyx_accent_table[i].name
360 if (lyx_accent_table[i].name
361 && contains(p, lyx_accent_table[i].name)) {
362 lyxerr[Debug::KBMAP] << "Found it!" << endl;
363 return static_cast<tex_accent>(i);