]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
Make document_language non-mutable
[lyx.git] / src / Encoding.cpp
1 /**
2  * \file Encoding.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Dekel Tsur
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Encoding.h"
16
17 #include "Lexer.h"
18
19 #include "support/debug.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/textutils.h"
23 #include "support/unicode.h"
24
25 #include <boost/cstdint.hpp>
26
27 #include <sstream>
28 #include <algorithm>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 int const Encoding::any = -1;
36
37 Encodings encodings;
38
39 Encodings::MathCommandSet Encodings::mathcmd;
40 Encodings::TextCommandSet Encodings::textcmd;
41 Encodings::MathSymbolSet  Encodings::mathsym;
42
43 namespace {
44
45 typedef map<char_type, CharInfo> CharInfoMap;
46 CharInfoMap unicodesymbols;
47
48 typedef set<char_type> CharSet;
49 typedef map<string, CharSet> CharSetMap;
50 CharSet forced;
51 CharSetMap forcedselected;
52
53 typedef set<char_type> MathAlphaSet;
54 MathAlphaSet mathalpha;
55
56
57 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
58 char_type const max_ucs4 = 0x110000;
59
60 } // namespace anon
61
62
63 EncodingException::EncodingException(char_type c)
64         : failed_char(c), par_id(0), pos(0)
65 {
66 }
67
68
69 const char * EncodingException::what() const throw()
70 {
71         return "Could not find LaTeX command for a character";
72 }
73
74
75 CharInfo::CharInfo(
76         docstring const & textcommand, docstring const & mathcommand,
77         std::string const & textpreamble, std::string const & mathpreamble,
78         std::string const & tipashortcut, unsigned int flags)
79         : textcommand_(textcommand), mathcommand_(mathcommand),
80           textpreamble_(textpreamble), mathpreamble_(mathpreamble),
81           tipashortcut_(tipashortcut), flags_(flags)
82 {
83 }
84
85 Encoding::Encoding(string const & n, string const & l, string const & g,
86                    string const & i, bool f, bool u, Encoding::Package p)
87         : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f),
88           unsafe_(u), forced_(&forcedselected[n]), package_(p)
89 {
90         if (n == "ascii") {
91                 // ASCII can encode 128 code points and nothing else
92                 start_encodable_ = 128;
93                 complete_ = true;
94         } else if (i == "UTF-8") {
95                 // UTF8 can encode all UCS4 code points
96                 start_encodable_ = max_ucs4;
97                 complete_ = true;
98         } else {
99                 start_encodable_ = 0;
100                 complete_ = false;
101         }
102 }
103
104
105 void Encoding::init() const
106 {
107         if (complete_)
108                 return;
109
110         start_encodable_ = 0;
111         // temporarily switch off lyxerr, since we will generate iconv errors
112         lyxerr.disable();
113         if (fixedwidth_) {
114                 // We do not need to check all UCS4 code points, it is enough
115                 // if we check all 256 code points of this encoding.
116                 for (unsigned short j = 0; j < 256; ++j) {
117                         char const c = char(j);
118                         vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
119                         if (ucs4.size() != 1)
120                                 continue;
121                         char_type const uc = ucs4[0];
122                         CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
123                         if (it == unicodesymbols.end())
124                                 encodable_.insert(uc);
125                         else if (!it->second.force()) {
126                                 if (forced_->empty() || forced_->find(uc) == forced_->end())
127                                         encodable_.insert(uc);
128                         }
129                 }
130         } else {
131                 // We do not know how many code points this encoding has, and
132                 // they do not have a direct representation as a single byte,
133                 // therefore we need to check all UCS4 code points.
134                 // This is expensive!
135                 for (char_type c = 0; c < max_ucs4; ++c) {
136                         vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
137                         if (!eightbit.empty()) {
138                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
139                                 if (it == unicodesymbols.end())
140                                         encodable_.insert(c);
141                                 else if (!it->second.force()) {
142                                         if (forced_->empty() || forced_->find(c) == forced_->end())
143                                                 encodable_.insert(c);
144                                 }
145                         }
146                 }
147         }
148         lyxerr.enable();
149         CharSet::iterator it = encodable_.find(start_encodable_);
150         while (it != encodable_.end()) {
151                 encodable_.erase(it);
152                 ++start_encodable_;
153                 it = encodable_.find(start_encodable_);
154         }
155         complete_ = true;
156 }
157
158
159 bool Encoding::isForced(char_type c) const
160 {
161         if (!forced.empty() && forced.find(c) != forced.end())
162                 return true;
163         return !forced_->empty() && forced_->find(c) != forced_->end();
164 }
165
166
167 bool Encoding::encodable(char_type c) const
168 {
169         // assure the used encoding is properly initialized
170         init();
171
172         if (iconvName_ == "UTF-8" && package_ == none)
173                 return true;
174         if (c < start_encodable_ && !isForced(c))
175                 return true;
176         if (encodable_.find(c) != encodable_.end())
177                 return true;
178         return false;
179 }
180
181
182 pair<docstring, bool> Encoding::latexChar(char_type c) const
183 {
184         if (encodable(c))
185                 return make_pair(docstring(1, c), false);
186
187         // c cannot (or should not) be encoded in this encoding
188         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
189         if (it == unicodesymbols.end())
190                 throw EncodingException(c);
191         // at least one of mathcommand and textcommand is nonempty
192         if (it->second.textcommand().empty())
193                 return make_pair(
194                         "\\ensuremath{" + it->second.mathcommand() + '}', false);
195         return make_pair(it->second.textcommand(), !it->second.textnotermination());
196 }
197
198
199 pair<docstring, docstring> Encoding::latexString(docstring const & input, bool dryrun) const
200 {
201         docstring result;
202         docstring uncodable;
203         bool terminate = false;
204         for (size_t n = 0; n < input.size(); ++n) {
205                 try {
206                         char_type const c = input[n];
207                         pair<docstring, bool> latex_char = latexChar(c);
208                         docstring const latex = latex_char.first;
209                         if (terminate && !prefixIs(latex, '\\')
210                             && !prefixIs(latex, '{')
211                             && !prefixIs(latex, '}')) {
212                                         // Prevent eating of a following
213                                         // space or command corruption by
214                                         // following characters
215                                         if (latex == " ")
216                                                 result += "{}";
217                                         else
218                                                 result += " ";
219                                 }
220                         result += latex;
221                         terminate = latex_char.second;
222                 } catch (EncodingException & /* e */) {
223                         LYXERR0("Uncodable character in latexString!");
224                         if (dryrun) {
225                                 result += "<" + _("LyX Warning: ")
226                                            + _("uncodable character") + " '";
227                                 result += docstring(1, input[n]);
228                                 result += "'>";
229                         } else
230                                 uncodable += input[n];
231                 }
232         }
233         return make_pair(result, uncodable);
234 }
235
236
237 vector<char_type> Encoding::symbolsList() const
238 {
239         // assure the used encoding is properly initialized
240         init();
241
242         // first all encodable characters
243         vector<char_type> symbols(encodable_.begin(), encodable_.end());
244         // add those below start_encodable_
245         for (char_type c = 0; c < start_encodable_; ++c)
246                 symbols.push_back(c);
247         // now the ones from the unicodesymbols file
248         CharInfoMap::const_iterator const end = unicodesymbols.end();
249         CharInfoMap::const_iterator it = unicodesymbols.begin();
250         for (; it != end; ++it)
251                 symbols.push_back(it->first);
252         return symbols;
253 }
254
255
256 bool Encodings::latexMathChar(char_type c, bool mathmode,
257                         Encoding const * encoding, docstring & command,
258                         bool & needsTermination)
259 {
260         command = empty_docstring();
261         if (encoding)
262                 if (encoding->encodable(c))
263                         command = docstring(1, c);
264         needsTermination = false;
265
266         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
267         if (it == unicodesymbols.end()) {
268                 if (!encoding || command.empty())
269                         throw EncodingException(c);
270                 if (mathmode)
271                         addMathSym(c);
272                 return false;
273         }
274         // at least one of mathcommand and textcommand is nonempty
275         bool use_math = (mathmode && !it->second.mathcommand().empty()) ||
276                         (!mathmode && it->second.textcommand().empty());
277         if (use_math) {
278                 command = it->second.mathcommand();
279                 needsTermination = !it->second.mathnotermination();
280                 addMathCmd(c);
281         } else {
282                 if (!encoding || command.empty()) {
283                         command = it->second.textcommand();
284                         needsTermination = !it->second.textnotermination();
285                         addTextCmd(c);
286                 }
287                 if (mathmode)
288                         addMathSym(c);
289         }
290         return use_math;
291 }
292
293
294 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
295                 bool & combining, bool & needsTermination, set<string> * req)
296 {
297         CharInfoMap::const_iterator const end = unicodesymbols.end();
298         CharInfoMap::const_iterator it = unicodesymbols.begin();
299         for (combining = false; it != end; ++it) {
300                 docstring const math = it->second.mathcommand();
301                 docstring const text = it->second.textcommand();
302                 if ((cmdtype & MATH_CMD) && math == cmd) {
303                         combining = it->second.combining();
304                         needsTermination = !it->second.mathnotermination();
305                         if (req && it->second.mathfeature() &&
306                             !it->second.mathpreamble().empty())
307                                 req->insert(it->second.mathpreamble());
308                         return it->first;
309                 }
310                 if ((cmdtype & TEXT_CMD) && text == cmd) {
311                         combining = it->second.combining();
312                         needsTermination = !it->second.textnotermination();
313                         if (req && it->second.textfeature() &&
314                             !it->second.textpreamble().empty())
315                                 req->insert(it->second.textpreamble());
316                         return it->first;
317                 }
318         }
319         needsTermination = false;
320         return 0;
321 }
322
323
324 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
325                 bool & needsTermination, docstring & rem, set<string> * req)
326 {
327         needsTermination = false;
328         rem = empty_docstring();
329         bool const mathmode = cmdtype & MATH_CMD;
330         bool const textmode = cmdtype & TEXT_CMD;
331         docstring symbols;
332         size_t const cmdend = cmd.size();
333         size_t prefix = 0;
334         CharInfoMap::const_iterator const uniend = unicodesymbols.end();
335         for (size_t i = 0, j = 0; j < cmdend; ++j) {
336                 // Also get the char after a backslash
337                 if (j + 1 < cmdend && cmd[j] == '\\') {
338                         ++j;
339                         prefix = 1;
340                         // Detect things like \=*{e} as well
341                         if (j + 3 < cmdend && cmd[j+1] == '*' &&
342                             cmd[j+2] == '{') {
343                                 ++j;
344                                 prefix = 2;
345                         }
346                 }
347                 // position of the last character before a possible macro
348                 // argument
349                 size_t m = j;
350                 // If a macro argument follows, get it, too
351                 // Do it here only for single character commands. Other
352                 // combining commands need this too, but they are handled in
353                 // the loop below for performance reasons.
354                 if (j + 1 < cmdend && cmd[j + 1] == '{') {
355                         size_t k = j + 1;
356                         int count = 1;
357                         while (k < cmdend && count) {
358                                 k = cmd.find_first_of(from_ascii("{}"), k + 1);
359                                 // braces may not be balanced
360                                 if (k == docstring::npos)
361                                         break;
362                                 if (cmd[k] == '{')
363                                         ++count;
364                                 else
365                                         --count;
366                         }
367                         if (k != docstring::npos)
368                                 j = k;
369                 } else if (m + 1 < cmdend && isAlphaASCII(cmd[m])) {
370                         while (m + 2 < cmdend && isAlphaASCII(cmd[m+1]))
371                                 m++;
372                 }
373                 // Start with this substring and try augmenting it when it is
374                 // the prefix of some command in the unicodesymbols file
375                 docstring subcmd = cmd.substr(i, j - i + 1);
376
377                 CharInfoMap::const_iterator it = unicodesymbols.begin();
378                 // First part of subcmd which might be a combining character
379                 docstring combcmd = (m == j) ? docstring() : cmd.substr(i, m - i + 1);
380                 // The combining character of combcmd if it exists
381                 CharInfoMap::const_iterator combining = uniend;
382                 size_t unicmd_size = 0;
383                 char_type c = 0;
384                 for (; it != uniend; ++it) {
385                         docstring const math = mathmode ? it->second.mathcommand()
386                                                         : docstring();
387                         docstring const text = textmode ? it->second.textcommand()
388                                                         : docstring();
389                         if (!combcmd.empty() && it->second.combining() &&
390                             (math == combcmd || text == combcmd))
391                                 combining = it;
392                         size_t cur_size = max(math.size(), text.size());
393                         // The current math or text unicode command cannot
394                         // match, or we already matched a longer one
395                         if (cur_size < subcmd.size() || cur_size <= unicmd_size)
396                                 continue;
397
398                         docstring tmp = subcmd;
399                         size_t k = j;
400                         while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
401                                 ++k;
402                                 if (k >= cmdend || cur_size <= tmp.size())
403                                         break;
404                                 tmp += cmd[k];
405                         }
406                         // No match
407                         if (k == j)
408                                 continue;
409
410                         // The last added char caused a mismatch, because
411                         // we didn't exhaust the chars in cmd and didn't
412                         // exceed the maximum size of the current unicmd
413                         if (k < cmdend && cur_size > tmp.size())
414                                 tmp.resize(tmp.size() - 1);
415
416                         // If this is an exact match, we found a (longer)
417                         // matching entry in the unicodesymbols file.
418                         if (math != tmp && text != tmp)
419                                 continue;
420                         // If we found a combining command, we need to append
421                         // the macro argument if this has not been done above.
422                         if (tmp == combcmd && combining != uniend &&
423                             k < cmdend && cmd[k] == '{') {
424                                 size_t l = k;
425                                 int count = 1;
426                                 while (l < cmdend && count) {
427                                         l = cmd.find_first_of(from_ascii("{}"), l + 1);
428                                         // braces may not be balanced
429                                         if (l == docstring::npos)
430                                                 break;
431                                         if (cmd[l] == '{')
432                                                 ++count;
433                                         else
434                                                 --count;
435                                 }
436                                 if (l != docstring::npos) {
437                                         j = l;
438                                         subcmd = cmd.substr(i, j - i + 1);
439                                 }
440                         }
441                         // If the entry doesn't start with '\', we take note
442                         // of the match and continue (this is not a ultimate
443                         // acceptance, as some other entry may match a longer
444                         // portion of the cmd string). However, if the entry
445                         // does start with '\', we accept the match only if
446                         // this is a valid macro, i.e., either it is a single
447                         // (nonletter) char macro, or nothing else follows,
448                         // or what follows is a nonletter char, or the last
449                         // character is a }.
450                         else if (tmp[0] != '\\'
451                                    || (tmp.size() == prefix + 1 &&
452                                        !isAlphaASCII(tmp[1]) &&
453                                        (prefix == 1 || !isAlphaASCII(tmp[2])))
454                                    || k == cmdend 
455                                    || !isAlphaASCII(cmd[k])
456                                    || tmp[tmp.size() - 1] == '}'
457                                  ) {
458                                 c = it->first;
459                                 j = k - 1;
460                                 i = j + 1;
461                                 unicmd_size = cur_size;
462                                 if (math == tmp)
463                                         needsTermination = !it->second.mathnotermination();
464                                 else
465                                         needsTermination = !it->second.textnotermination();
466                                 if (req) {
467                                         if (math == tmp && it->second.mathfeature() &&
468                                             !it->second.mathpreamble().empty())
469                                                 req->insert(it->second.mathpreamble());
470                                         if (text == tmp && it->second.textfeature() &&
471                                             !it->second.textpreamble().empty())
472                                                 req->insert(it->second.textpreamble());
473                                 }
474                         }
475                 }
476                 if (unicmd_size)
477                         symbols += c;
478                 else if (combining != uniend &&
479                          prefixIs(subcmd, combcmd + '{')) {
480                         // We know that subcmd starts with combcmd and
481                         // contains an argument in braces.
482                         docstring const arg = subcmd.substr(
483                                 combcmd.length() + 1,
484                                 subcmd.length() - combcmd.length() - 2);
485                         // If arg is a single character we can construct a
486                         // combining sequence.
487                         char_type a;
488                         bool argcomb = false;
489                         if (arg.size() == 1 && isAlnumASCII(arg[0]))
490                                 a = arg[0];
491                         else {
492                                 // Use the version of fromLaTeXCommand() that
493                                 // parses only one command, since we cannot
494                                 // use more than one character.
495                                 bool dummy = false;
496                                 set<string> r;
497                                 a = fromLaTeXCommand(arg, cmdtype, argcomb,
498                                                      dummy, &r);
499                                 if (a && req && !argcomb)
500                                         req->insert(r.begin(), r.end());
501                         }
502                         if (a && !argcomb) {
503                                 // In unicode the combining character comes
504                                 // after its base
505                                 symbols += a;
506                                 symbols += combining->first;
507                                 i = j + 1;
508                                 unicmd_size = 2;
509                         }
510                 }
511                 if (j + 1 == cmdend && !unicmd_size) {
512                         // No luck. Return what remains
513                         rem = cmd.substr(i);
514                         if (needsTermination && !rem.empty()) {
515                                 if (rem.substr(0, 2) == "{}") {
516                                         rem = rem.substr(2);
517                                         needsTermination = false;
518                                 } else if (rem[0] == ' ') {
519                                         needsTermination = false;
520                                         // LaTeX would swallow all spaces
521                                         rem = ltrim(rem);
522                                 }
523                         }
524                 }
525         }
526         return symbols;
527 }
528
529
530 CharInfo const & Encodings::unicodeCharInfo(char_type c)
531 {
532         static CharInfo empty;
533         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
534         return it != unicodesymbols.end() ? it->second : empty;
535 }
536
537
538 bool Encodings::isCombiningChar(char_type c)
539 {
540         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
541         if (it != unicodesymbols.end())
542                 return it->second.combining();
543         return false;
544 }
545
546
547 string const Encodings::TIPAShortcut(char_type c)
548 {
549         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
550         if (it != unicodesymbols.end())
551                 return it->second.tipashortcut();
552         return string();
553 }
554
555
556 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
557 {
558         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
559
560         if (it == unicodesymbols.end())
561                 return false;
562
563         if (it->second.textpreamble() != "textgreek" && it->second.textpreamble() != "textcyr")
564                 return false;
565
566         if (preamble.empty()) {
567                 preamble = it->second.textpreamble();
568                 return true;
569         }
570         return it->second.textpreamble() == preamble;
571 }
572
573
574 bool Encodings::isMathAlpha(char_type c)
575 {
576         return mathalpha.count(c);
577 }
578
579
580 Encoding const *
581 Encodings::fromLyXName(string const & name, bool allowUnsafe) const
582 {
583         EncodingList::const_iterator const it = encodinglist.find(name);
584         if (!allowUnsafe && it->second.unsafe())
585                 return 0;
586         return it != encodinglist.end() ? &it->second : 0;
587 }
588
589
590 Encoding const *
591 Encodings::fromLaTeXName(string const & n, int const & p, bool allowUnsafe) const
592 {
593         string name = n;
594         // FIXME: if we have to test for too many of these synonyms,
595         // we should instead extend the format of lib/encodings
596         if (n == "ansinew")
597                 name = "cp1252";
598
599         // We don't use find_if because it makes copies of the pairs in
600         // the map.
601         // This linear search is OK since we don't have many encodings.
602         // Users could even optimize it by putting the encodings they use
603         // most at the top of lib/encodings.
604         EncodingList::const_iterator const end = encodinglist.end();
605         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
606                 if ((it->second.latexName() == name) && (it->second.package() & p)
607                                 && (!it->second.unsafe() || allowUnsafe))
608                         return &it->second;
609         return 0;
610 }
611
612
613 Encoding const *
614 Encodings::fromIconvName(string const & n, int const & p, bool allowUnsafe) const
615 {
616         EncodingList::const_iterator const end = encodinglist.end();
617         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
618                 if ((it->second.iconvName() == n) && (it->second.package() & p)
619                                 && (!it->second.unsafe() || allowUnsafe))
620                         return &it->second;
621         return 0;
622 }
623
624
625 Encodings::Encodings()
626 {}
627
628
629 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
630 {
631         // We must read the symbolsfile first, because the Encoding
632         // constructor depends on it.
633         CharSetMap forcednotselected;
634         Lexer symbolslex;
635         symbolslex.setFile(symbolsfile);
636         bool getNextToken = true;
637         while (symbolslex.isOK()) {
638                 char_type symbol;
639
640                 if (getNextToken) {
641                         if (!symbolslex.next(true))
642                                 break;
643                 } else
644                         getNextToken = true;
645
646                 istringstream is(symbolslex.getString());
647                 // reading symbol directly does not work if
648                 // char_type == wchar_t.
649                 boost::uint32_t tmp;
650                 if(!(is >> hex >> tmp))
651                         break;
652                 symbol = tmp;
653
654                 if (!symbolslex.next(true))
655                         break;
656                 docstring textcommand = symbolslex.getDocString();
657                 if (!symbolslex.next(true))
658                         break;
659                 string textpreamble = symbolslex.getString();
660                 if (!symbolslex.next(true))
661                         break;
662                 string sflags = symbolslex.getString();
663                 
664                 string tipashortcut;
665                 int flags = 0;
666
667                 if (suffixIs(textcommand, '}'))
668                         flags |= CharInfoTextNoTermination;
669                 while (!sflags.empty()) {
670                         string flag;
671                         sflags = split(sflags, flag, ',');
672                         if (flag == "combining") {
673                                 flags |= CharInfoCombining;
674                         } else if (flag == "force") {
675                                 flags |= CharInfoForce;
676                                 forced.insert(symbol);
677                         } else if (prefixIs(flag, "force=")) {
678                                 vector<string> encodings =
679                                         getVectorFromString(flag.substr(6), ";");
680                                 for (size_t i = 0; i < encodings.size(); ++i)
681                                         forcedselected[encodings[i]].insert(symbol);
682                                 flags |= CharInfoForceSelected;
683                         } else if (prefixIs(flag, "force!=")) {
684                                 vector<string> encodings =
685                                         getVectorFromString(flag.substr(7), ";");
686                                 for (size_t i = 0; i < encodings.size(); ++i)
687                                         forcednotselected[encodings[i]].insert(symbol);
688                                 flags |= CharInfoForceSelected;
689                         } else if (flag == "mathalpha") {
690                                 mathalpha.insert(symbol);
691                         } else if (flag == "notermination=text") {
692                                 flags |= CharInfoTextNoTermination;
693                         } else if (flag == "notermination=math") {
694                                 flags |= CharInfoMathNoTermination;
695                         } else if (flag == "notermination=both") {
696                                 flags |= CharInfoTextNoTermination;
697                                 flags |= CharInfoMathNoTermination;
698                         } else if (flag == "notermination=none") {
699                                 flags &= ~CharInfoTextNoTermination;
700                                 flags &= ~CharInfoMathNoTermination;
701                         } else if (contains(flag, "tipashortcut=")) {
702                                 tipashortcut = split(flag, '=');
703                         } else {
704                                 lyxerr << "Ignoring unknown flag `" << flag
705                                        << "' for symbol `0x"
706                                        << hex << symbol << dec
707                                        << "'." << endl;
708                         }
709                 }
710                 // mathcommand and mathpreamble have been added for 1.6.0.
711                 // make them optional so that old files still work.
712                 int const lineno = symbolslex.lineNumber();
713                 bool breakout = false;
714                 docstring mathcommand;
715                 string mathpreamble;
716                 if (symbolslex.next(true)) {
717                         if (symbolslex.lineNumber() != lineno) {
718                                 // line in old format without mathcommand and mathpreamble
719                                 getNextToken = false;
720                         } else {
721                                 mathcommand = symbolslex.getDocString();
722                                 if (suffixIs(mathcommand, '}'))
723                                         flags |= CharInfoMathNoTermination;
724                                 if (symbolslex.next(true)) {
725                                         if (symbolslex.lineNumber() != lineno) {
726                                                 // line in new format with mathcommand only
727                                                 getNextToken = false;
728                                         } else {
729                                                 // line in new format with mathcommand and mathpreamble
730                                                 mathpreamble = symbolslex.getString();
731                                         }
732                                 } else
733                                         breakout = true;
734                         }
735                 } else {
736                         breakout = true;
737                 }
738
739                 // backward compatibility
740                 if (mathpreamble == "esintoramsmath")
741                         mathpreamble = "esint|amsmath";
742
743                 if (!textpreamble.empty())
744                         if (textpreamble[0] != '\\')
745                                 flags |= CharInfoTextFeature;
746                 if (!mathpreamble.empty())
747                         if (mathpreamble[0] != '\\')
748                                 flags |= CharInfoMathFeature;
749
750                 CharInfo info = CharInfo(
751                         textcommand, mathcommand,
752                         textpreamble, mathpreamble,
753                         tipashortcut, flags);
754                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
755                            << to_utf8(info.textcommand()) << "' '" << info.textpreamble()
756                            << " '" << info.textfeature() << ' ' << info.textnotermination()
757                            << ' ' << to_utf8(info.mathcommand()) << "' '" << info.mathpreamble()
758                            << "' " << info.mathfeature() << ' ' << info.mathnotermination()
759                            << ' ' << info.combining() << ' ' << info.force()
760                            << ' ' << info.forceselected());
761
762                 // we assume that at least one command is nonempty when using unicodesymbols
763                 if (info.isUnicodeSymbol()) {
764                         unicodesymbols[symbol] = info;
765                 }
766
767                 if (breakout)
768                         break;
769         }
770
771         // Now read the encodings
772         enum {
773                 et_encoding = 1,
774                 et_end
775         };
776
777         LexerKeyword encodingtags[] = {
778                 { "encoding", et_encoding },
779                 { "end", et_end }
780         };
781
782         Lexer lex(encodingtags);
783         lex.setFile(encfile);
784         lex.setContext("Encodings::read");
785         while (lex.isOK()) {
786                 switch (lex.lex()) {
787                 case et_encoding:
788                 {
789                         lex.next();
790                         string const name = lex.getString();
791                         lex.next();
792                         string const latexname = lex.getString();
793                         lex.next();
794                         string const guiname = lex.getString();
795                         lex.next();
796                         string const iconvname = lex.getString();
797                         lex.next();
798                         string const width = lex.getString();
799                         bool fixedwidth = false;
800                         bool unsafe = false;
801                         if (width == "fixed")
802                                 fixedwidth = true;
803                         else if (width == "variable")
804                                 fixedwidth = false;
805                         else if (width == "variableunsafe") {
806                                 fixedwidth = false;
807                                 unsafe = true;
808                         }
809                         else
810                                 lex.printError("Unknown width");
811
812                         lex.next();
813                         string const p = lex.getString();
814                         Encoding::Package package = Encoding::none;
815                         if (p == "none")
816                                 package = Encoding::none;
817                         else if (p == "inputenc")
818                                 package = Encoding::inputenc;
819                         else if (p == "CJK")
820                                 package = Encoding::CJK;
821                         else if (p == "japanese")
822                                 package = Encoding::japanese;
823                         else
824                                 lex.printError("Unknown package");
825
826                         LYXERR(Debug::INFO, "Reading encoding " << name);
827                         encodinglist[name] = Encoding(name, latexname,
828                                 guiname, iconvname, fixedwidth, unsafe,
829                                 package);
830
831                         if (lex.lex() != et_end)
832                                 lex.printError("Missing end");
833                         break;
834                 }
835                 case et_end:
836                         lex.printError("Misplaced end");
837                         break;
838                 case Lexer::LEX_FEOF:
839                         break;
840                 default:
841                         lex.printError("Unknown tag");
842                         break;
843                 }
844         }
845
846         // Move all information from forcednotselected to forcedselected
847         for (CharSetMap::const_iterator it1 = forcednotselected.begin(); it1 != forcednotselected.end(); ++it1) {
848                 for (CharSetMap::iterator it2 = forcedselected.begin(); it2 != forcedselected.end(); ++it2) {
849                         if (it2->first != it1->first)
850                                 it2->second.insert(it1->second.begin(), it1->second.end());
851                 }
852         }
853
854 }
855
856
857 } // namespace lyx