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