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"
21 using lyx::support::contains;
22 using lyx::support::libFileSearch;
48 void Trans::InsertException(KmodException & exclist, char c,
49 string const & data, bool flag, tex_accent accent)
56 exclist.insert(exclist.begin(), p);
58 // exclist.push_back(p);
62 void Trans::FreeException(KmodException & exclist)
68 void Trans::FreeKeymap()
75 bool Trans::IsDefined() const
77 return !name_.empty();
81 string const & Trans::GetName() const
96 struct keyword_item kmapTags[K_LAST - 1] = {
104 tex_accent getkeymod(string const &);
107 void Trans::AddDeadkey(tex_accent accent, string const & keys)
112 kmod_list_[accent] = tmp;
114 for (string::size_type i = 0; i < keys.length(); ++i) {
118 keymap_[keys[i]] = tmp;
123 int Trans::Load(LyXLex & lex)
127 while (lex.isOK() && !error) {
131 if (lyxerr.debugging(Debug::KBMAP))
132 lyxerr << "KMOD:\t" << lex.getString() << endl;
133 if (lex.next(true)) {
134 if (lyxerr.debugging(Debug::KBMAP))
135 lyxerr << "key\t`" << lex.getString()
140 string const keys = lex.getString();
142 if (lex.next(true)) {
143 if (lyxerr.debugging(Debug::KBMAP))
144 lyxerr << "accent\t`" << lex.getString()
149 tex_accent accent = getkeymod(lex.getString());
151 if (accent == TEX_NOACCENT)
155 //#warning This code should be removed...
156 // But we need to fix up all the kmap files first
157 // so that this field is not present anymore.
158 if (lex.next(true)) {
159 if (lyxerr.debugging(Debug::KBMAP))
160 lyxerr << "allowed\t`" << lex.getString()
165 /* string const allowed = lex.getString(); */
166 AddDeadkey(accent, keys /*, allowed*/);
168 AddDeadkey(accent, keys);
175 lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
176 if (lex.next(true)) {
177 str = lex.getString();
178 lyxerr[Debug::KBMAP] << str << endl;
182 tex_accent accent_1 = getkeymod(str);
183 if (accent_1 == TEX_NOACCENT) return -1;
185 if (lex.next(true)) {
186 str = lex.getString();
187 lyxerr[Debug::KBMAP] << str << endl;
191 tex_accent accent_2= getkeymod(str);
192 if (accent_2 == TEX_NOACCENT) return -1;
194 map<int, KmodInfo>::iterator it1 =
195 kmod_list_.find(accent_1);
196 map<int, KmodInfo>::iterator it2 =
197 kmod_list_.find(accent_2);
198 if (it1 == kmod_list_.end()
199 || it2 == kmod_list_.end()) {
203 // Find what key accent_2 is on - should
204 // check about accent_1 also
205 map<int, string>::iterator it = keymap_.begin();
206 map<int, string>::iterator end = keymap_.end();
207 for (; it != end; ++it) {
208 if (!it->second.empty()
209 && it->second[0] == 0
210 && it->second[1] == accent_2)
215 allowed = lex.getString();
216 lyxerr[Debug::KBMAP] << "allowed: "
222 InsertException(kmod_list_[accent_1].exception_list,
223 static_cast<char>(it->first), allowed,
228 unsigned char key_from;
230 if (lyxerr.debugging(Debug::KBMAP))
231 lyxerr << "KMAP:\t" << lex.getString() << endl;
232 if (lex.next(true)) {
233 key_from = lex.getString()[0];
234 if (lyxerr.debugging(Debug::KBMAP))
235 lyxerr << "\t`" << lex.getString() << '\''
240 if (lex.next(true)) {
241 string const string_to = lex.getString();
242 keymap_[key_from] = string_to;
243 if (lyxerr.debugging(Debug::KBMAP))
244 lyxerr << "\t`" << string_to << '\''
256 if (lyxerr.debugging(Debug::KBMAP))
257 lyxerr << "KXMOD:\t" << lex.getString() << endl;
258 if (lex.next(true)) {
259 if (lyxerr.debugging(Debug::KBMAP))
260 lyxerr << "\t`" << lex.getString() << '\''
262 accent = getkeymod(lex.getString());
266 if (lex.next(true)) {
267 if (lyxerr.debugging(Debug::KBMAP))
268 lyxerr << "\t`" << lex.getString() << '\''
270 key = lex.getString()[0];
274 if (lex.next(true)) {
275 if (lyxerr.debugging(Debug::KBMAP))
276 lyxerr << "\t`" << lex.getString() << '\''
278 str = lex.getString();
282 InsertException(kmod_list_[accent].exception_list,
286 case LyXLex::LEX_FEOF:
287 lyxerr[Debug::PARSER] << "End of parsing" << endl;
290 lex.printError("ParseKeymapFile: "
291 "Unknown tag: `$$Token'");
299 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
301 map<int, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
302 if (cit != kmod_list_.end()) {
310 string const Trans::process(char c, TransManager & k)
312 string const t = Match(static_cast<unsigned char>(c));
314 if (t.empty() && c != 0) {
315 return k.normalkey(c);
316 } else if (!t.empty() && t[0] != char(0)) {
317 //return k.normalkey(c);
321 kmod_list_[static_cast<tex_accent>(t[1])]);
326 int Trans::Load(string const & language)
328 string const filename = libFileSearch("kbd", language, "kmap");
329 if (filename.empty())
333 LyXLex lex(kmapTags, K_LAST - 1);
334 lex.setFile(filename);
336 int const res = Load(lex);
347 tex_accent getkeymod(string const & p)
348 /* return modifier - decoded from p and update p */
350 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
351 if (lyxerr.debugging(Debug::KBMAP))
352 lyxerr << "p = " << p
353 << ", lyx_accent_table[" << i
354 << "].name = `" << lyx_accent_table[i].name
357 if (lyx_accent_table[i].name
358 && contains(p, lyx_accent_table[i].name)) {
359 lyxerr[Debug::KBMAP] << "Found it!" << endl;
360 return static_cast<tex_accent>(i);