]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
Cosmetic changes to the needauth dialogs
[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 those below start_encodable_
263         vector<char_type> symbols;
264         for (char_type c = 0; c < start_encodable_; ++c)
265                 symbols.push_back(c);
266         //add all encodable characters
267         copy(encodable_.begin(), encodable_.end(), back_inserter(symbols));
268         // now the ones from the unicodesymbols file
269         for (pair<char_type, CharInfo> const & elem : unicodesymbols)
270                 symbols.push_back(elem.first);
271         return symbols;
272 }
273
274
275 bool Encodings::latexMathChar(char_type c, bool mathmode,
276                         Encoding const * encoding, docstring & command,
277                         bool & needsTermination)
278 {
279         command = empty_docstring();
280         if (encoding)
281                 if (encoding->encodable(c))
282                         command = docstring(1, c);
283         needsTermination = false;
284
285         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
286         if (it == unicodesymbols.end()) {
287                 if (!encoding || command.empty())
288                         throw EncodingException(c);
289                 if (mathmode)
290                         addMathSym(c);
291                 return false;
292         }
293         // at least one of mathcommand and textcommand is nonempty
294         bool use_math = (mathmode && !it->second.mathcommand().empty()) ||
295                         (!mathmode && it->second.textcommand().empty());
296         if (use_math) {
297                 command = it->second.mathcommand();
298                 needsTermination = !it->second.mathnotermination();
299                 addMathCmd(c);
300         } else {
301                 if (!encoding || command.empty()) {
302                         command = it->second.textcommand();
303                         needsTermination = !it->second.textnotermination();
304                         addTextCmd(c);
305                 }
306                 if (mathmode)
307                         addMathSym(c);
308         }
309         return use_math;
310 }
311
312
313 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
314                 bool & combining, bool & needsTermination, set<string> * req)
315 {
316         CharInfoMap::const_iterator const end = unicodesymbols.end();
317         CharInfoMap::const_iterator it = unicodesymbols.begin();
318         for (combining = false; it != end; ++it) {
319                 if (it->second.deprecated())
320                         continue;
321                 docstring const math = it->second.mathcommand();
322                 docstring const text = it->second.textcommand();
323                 if ((cmdtype & MATH_CMD) && math == cmd) {
324                         combining = it->second.combining();
325                         needsTermination = !it->second.mathnotermination();
326                         if (req && it->second.mathfeature() &&
327                             !it->second.mathpreamble().empty())
328                                 req->insert(it->second.mathpreamble());
329                         return it->first;
330                 }
331                 if ((cmdtype & TEXT_CMD) && text == cmd) {
332                         combining = it->second.combining();
333                         needsTermination = !it->second.textnotermination();
334                         if (req && it->second.textfeature() &&
335                             !it->second.textpreamble().empty())
336                                 req->insert(it->second.textpreamble());
337                         return it->first;
338                 }
339         }
340         needsTermination = false;
341         return 0;
342 }
343
344
345 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
346                 bool & needsTermination, docstring & rem, set<string> * req)
347 {
348         needsTermination = false;
349         rem = empty_docstring();
350         bool const mathmode = cmdtype & MATH_CMD;
351         bool const textmode = cmdtype & TEXT_CMD;
352         docstring symbols;
353         size_t const cmdend = cmd.size();
354         size_t prefix = 0;
355         CharInfoMap::const_iterator const uniend = unicodesymbols.end();
356         for (size_t i = 0, j = 0; j < cmdend; ++j) {
357                 // Also get the char after a backslash
358                 if (j + 1 < cmdend && cmd[j] == '\\') {
359                         ++j;
360                         prefix = 1;
361                         // Detect things like \=*{e} as well
362                         if (j + 3 < cmdend && cmd[j+1] == '*' &&
363                             cmd[j+2] == '{') {
364                                 ++j;
365                                 prefix = 2;
366                         }
367                 }
368                 // position of the last character before a possible macro
369                 // argument
370                 size_t m = j;
371                 // If a macro argument follows, get it, too
372                 // Do it here only for single character commands. Other
373                 // combining commands need this too, but they are handled in
374                 // the loop below for performance reasons.
375                 if (j + 1 < cmdend && cmd[j + 1] == '{') {
376                         size_t k = j + 1;
377                         int count = 1;
378                         while (k < cmdend && count) {
379                                 k = cmd.find_first_of(from_ascii("{}"), k + 1);
380                                 // braces may not be balanced
381                                 if (k == docstring::npos)
382                                         break;
383                                 if (cmd[k] == '{')
384                                         ++count;
385                                 else
386                                         --count;
387                         }
388                         if (k != docstring::npos)
389                                 j = k;
390                 } else if (m + 1 < cmdend && isAlphaASCII(cmd[m])) {
391                         while (m + 2 < cmdend && isAlphaASCII(cmd[m+1]))
392                                 m++;
393                 }
394                 // Start with this substring and try augmenting it when it is
395                 // the prefix of some command in the unicodesymbols file
396                 docstring subcmd = cmd.substr(i, j - i + 1);
397
398                 CharInfoMap::const_iterator it = unicodesymbols.begin();
399                 // First part of subcmd which might be a combining character
400                 docstring combcmd = (m == j) ? docstring() : cmd.substr(i, m - i + 1);
401                 // The combining character of combcmd if it exists
402                 CharInfoMap::const_iterator combining = uniend;
403                 size_t unicmd_size = 0;
404                 char_type c = 0;
405                 for (; it != uniend; ++it) {
406                         if (it->second.deprecated())
407                                 continue;
408                         docstring const math = mathmode ? it->second.mathcommand()
409                                                         : docstring();
410                         docstring const text = textmode ? it->second.textcommand()
411                                                         : docstring();
412                         if (!combcmd.empty() && it->second.combining() &&
413                             (math == combcmd || text == combcmd))
414                                 combining = it;
415                         size_t cur_size = max(math.size(), text.size());
416                         // The current math or text unicode command cannot
417                         // match, or we already matched a longer one
418                         if (cur_size < subcmd.size() || cur_size <= unicmd_size)
419                                 continue;
420
421                         docstring tmp = subcmd;
422                         size_t k = j;
423                         while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
424                                 ++k;
425                                 if (k >= cmdend || cur_size <= tmp.size())
426                                         break;
427                                 tmp += cmd[k];
428                         }
429                         // No match
430                         if (k == j)
431                                 continue;
432
433                         // The last added char caused a mismatch, because
434                         // we didn't exhaust the chars in cmd and didn't
435                         // exceed the maximum size of the current unicmd
436                         if (k < cmdend && cur_size > tmp.size())
437                                 tmp.resize(tmp.size() - 1);
438
439                         // If this is an exact match, we found a (longer)
440                         // matching entry in the unicodesymbols file.
441                         if (math != tmp && text != tmp)
442                                 continue;
443                         // If we found a combining command, we need to append
444                         // the macro argument if this has not been done above.
445                         if (tmp == combcmd && combining != uniend &&
446                             k < cmdend && cmd[k] == '{') {
447                                 size_t l = k;
448                                 int count = 1;
449                                 while (l < cmdend && count) {
450                                         l = cmd.find_first_of(from_ascii("{}"), l + 1);
451                                         // braces may not be balanced
452                                         if (l == docstring::npos)
453                                                 break;
454                                         if (cmd[l] == '{')
455                                                 ++count;
456                                         else
457                                                 --count;
458                                 }
459                                 if (l != docstring::npos) {
460                                         j = l;
461                                         subcmd = cmd.substr(i, j - i + 1);
462                                 }
463                         }
464                         // If the entry doesn't start with '\', we take note
465                         // of the match and continue (this is not a ultimate
466                         // acceptance, as some other entry may match a longer
467                         // portion of the cmd string). However, if the entry
468                         // does start with '\', we accept the match only if
469                         // this is a valid macro, i.e., either it is a single
470                         // (nonletter) char macro, or nothing else follows,
471                         // or what follows is a nonletter char, or the last
472                         // character is a }.
473                         else if (tmp[0] != '\\'
474                                    || (tmp.size() == prefix + 1 &&
475                                        !isAlphaASCII(tmp[1]) &&
476                                        (prefix == 1 || !isAlphaASCII(tmp[2])))
477                                    || k == cmdend
478                                    || !isAlphaASCII(cmd[k])
479                                    || tmp[tmp.size() - 1] == '}'
480                                  ) {
481                                 c = it->first;
482                                 j = k - 1;
483                                 i = j + 1;
484                                 unicmd_size = cur_size;
485                                 if (math == tmp)
486                                         needsTermination = !it->second.mathnotermination();
487                                 else
488                                         needsTermination = !it->second.textnotermination();
489                                 if (req) {
490                                         if (math == tmp && it->second.mathfeature() &&
491                                             !it->second.mathpreamble().empty())
492                                                 req->insert(it->second.mathpreamble());
493                                         if (text == tmp && it->second.textfeature() &&
494                                             !it->second.textpreamble().empty())
495                                                 req->insert(it->second.textpreamble());
496                                 }
497                         }
498                 }
499                 if (unicmd_size)
500                         symbols += c;
501                 else if (combining != uniend &&
502                          prefixIs(subcmd, combcmd + '{')) {
503                         // We know that subcmd starts with combcmd and
504                         // contains an argument in braces.
505                         docstring const arg = subcmd.substr(
506                                 combcmd.length() + 1,
507                                 subcmd.length() - combcmd.length() - 2);
508                         // If arg is a single character we can construct a
509                         // combining sequence.
510                         char_type a;
511                         bool argcomb = false;
512                         if (arg.size() == 1 && isAlnumASCII(arg[0]))
513                                 a = arg[0];
514                         else {
515                                 // Use the version of fromLaTeXCommand() that
516                                 // parses only one command, since we cannot
517                                 // use more than one character.
518                                 bool dummy = false;
519                                 set<string> r;
520                                 a = fromLaTeXCommand(arg, cmdtype, argcomb,
521                                                      dummy, &r);
522                                 if (a && req && !argcomb)
523                                         req->insert(r.begin(), r.end());
524                         }
525                         if (a && !argcomb) {
526                                 // In unicode the combining character comes
527                                 // after its base
528                                 symbols += a;
529                                 symbols += combining->first;
530                                 i = j + 1;
531                                 unicmd_size = 2;
532                         }
533                 }
534                 if (j + 1 == cmdend && !unicmd_size) {
535                         // No luck. Return what remains
536                         rem = cmd.substr(i);
537                         if (needsTermination && !rem.empty()) {
538                                 if (rem.substr(0, 2) == "{}") {
539                                         rem = rem.substr(2);
540                                         needsTermination = false;
541                                 } else if (rem[0] == ' ') {
542                                         needsTermination = false;
543                                         // LaTeX would swallow all spaces
544                                         rem = ltrim(rem);
545                                 }
546                         }
547                 }
548         }
549         return symbols;
550 }
551
552
553 CharInfo const & Encodings::unicodeCharInfo(char_type c)
554 {
555         static CharInfo empty;
556         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
557         return it != unicodesymbols.end() ? it->second : empty;
558 }
559
560
561 bool Encodings::isCombiningChar(char_type c)
562 {
563         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
564         if (it != unicodesymbols.end())
565                 return it->second.combining();
566         return false;
567 }
568
569
570 string const Encodings::TIPAShortcut(char_type c)
571 {
572         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
573         if (it != unicodesymbols.end())
574                 return it->second.tipashortcut();
575         return string();
576 }
577
578
579 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
580 {
581         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
582
583         if (it == unicodesymbols.end())
584                 return false;
585
586         if (it->second.textpreamble() != "textgreek" && it->second.textpreamble() != "textcyr")
587                 return false;
588
589         if (preamble.empty()) {
590                 preamble = it->second.textpreamble();
591                 return true;
592         }
593         return it->second.textpreamble() == preamble;
594 }
595
596
597 bool Encodings::isMathAlpha(char_type c)
598 {
599         return mathalpha.count(c);
600 }
601
602
603 bool Encodings::isUnicodeTextOnly(char_type c)
604 {
605         if (isASCII(c) || isMathAlpha(c))
606                 return false;
607
608         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
609         return it == unicodesymbols.end() || it->second.mathcommand().empty();
610 }
611
612
613 Encoding const *
614 Encodings::fromLyXName(string const & name, bool allowUnsafe) const
615 {
616         EncodingList::const_iterator const it = encodinglist.find(name);
617         if (it == encodinglist.end())
618                 return 0;
619         if (!allowUnsafe && it->second.unsafe())
620                 return 0;
621         return &it->second;
622 }
623
624
625 Encoding const *
626 Encodings::fromLaTeXName(string const & n, int const & p, bool allowUnsafe) const
627 {
628         string name = n;
629         // FIXME: if we have to test for too many of these synonyms,
630         // we should instead extend the format of lib/encodings
631         if (n == "ansinew")
632                 name = "cp1252";
633
634         // We don't use find_if because it makes copies of the pairs in
635         // the map.
636         // This linear search is OK since we don't have many encodings.
637         // Users could even optimize it by putting the encodings they use
638         // most at the top of lib/encodings.
639         EncodingList::const_iterator const end = encodinglist.end();
640         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
641                 if ((it->second.latexName() == name) && (it->second.package() & p)
642                                 && (!it->second.unsafe() || allowUnsafe))
643                         return &it->second;
644         return 0;
645 }
646
647
648 Encoding const *
649 Encodings::fromIconvName(string const & n, int const & p, bool allowUnsafe) const
650 {
651         EncodingList::const_iterator const end = encodinglist.end();
652         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
653                 if ((it->second.iconvName() == n) && (it->second.package() & p)
654                                 && (!it->second.unsafe() || allowUnsafe))
655                         return &it->second;
656         return 0;
657 }
658
659
660 Encodings::Encodings()
661 {}
662
663
664 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
665 {
666         // We must read the symbolsfile first, because the Encoding
667         // constructor depends on it.
668         CharSetMap forcednotselected;
669         Lexer symbolslex;
670         symbolslex.setFile(symbolsfile);
671         bool getNextToken = true;
672         while (symbolslex.isOK()) {
673                 char_type symbol;
674
675                 if (getNextToken) {
676                         if (!symbolslex.next(true))
677                                 break;
678                 } else
679                         getNextToken = true;
680
681                 istringstream is(symbolslex.getString());
682                 // reading symbol directly does not work if
683                 // char_type == wchar_t.
684                 boost::uint32_t tmp;
685                 if(!(is >> hex >> tmp))
686                         break;
687                 symbol = tmp;
688
689                 if (!symbolslex.next(true))
690                         break;
691                 docstring textcommand = symbolslex.getDocString();
692                 if (!symbolslex.next(true))
693                         break;
694                 string textpreamble = symbolslex.getString();
695                 if (!symbolslex.next(true))
696                         break;
697                 string sflags = symbolslex.getString();
698
699                 string tipashortcut;
700                 int flags = 0;
701
702                 if (suffixIs(textcommand, '}'))
703                         flags |= CharInfoTextNoTermination;
704                 while (!sflags.empty()) {
705                         string flag;
706                         sflags = split(sflags, flag, ',');
707                         if (flag == "combining") {
708                                 flags |= CharInfoCombining;
709                         } else if (flag == "force") {
710                                 flags |= CharInfoForce;
711                                 forced.insert(symbol);
712                         } else if (prefixIs(flag, "force=")) {
713                                 vector<string> encodings =
714                                         getVectorFromString(flag.substr(6), ";");
715                                 for (size_t i = 0; i < encodings.size(); ++i)
716                                         forcedselected[encodings[i]].insert(symbol);
717                                 flags |= CharInfoForceSelected;
718                         } else if (prefixIs(flag, "force!=")) {
719                                 vector<string> encodings =
720                                         getVectorFromString(flag.substr(7), ";");
721                                 for (size_t i = 0; i < encodings.size(); ++i)
722                                         forcednotselected[encodings[i]].insert(symbol);
723                                 flags |= CharInfoForceSelected;
724                         } else if (flag == "mathalpha") {
725                                 mathalpha.insert(symbol);
726                         } else if (flag == "notermination=text") {
727                                 flags |= CharInfoTextNoTermination;
728                         } else if (flag == "notermination=math") {
729                                 flags |= CharInfoMathNoTermination;
730                         } else if (flag == "notermination=both") {
731                                 flags |= CharInfoTextNoTermination;
732                                 flags |= CharInfoMathNoTermination;
733                         } else if (flag == "notermination=none") {
734                                 flags &= ~CharInfoTextNoTermination;
735                                 flags &= ~CharInfoMathNoTermination;
736                         } else if (contains(flag, "tipashortcut=")) {
737                                 tipashortcut = split(flag, '=');
738                         } else if (flag == "deprecated") {
739                                 flags |= CharInfoDeprecated;
740                         } else {
741                                 lyxerr << "Ignoring unknown flag `" << flag
742                                        << "' for symbol `0x"
743                                        << hex << symbol << dec
744                                        << "'." << endl;
745                         }
746                 }
747                 // mathcommand and mathpreamble have been added for 1.6.0.
748                 // make them optional so that old files still work.
749                 int const lineno = symbolslex.lineNumber();
750                 bool breakout = false;
751                 docstring mathcommand;
752                 string mathpreamble;
753                 if (symbolslex.next(true)) {
754                         if (symbolslex.lineNumber() != lineno) {
755                                 // line in old format without mathcommand and mathpreamble
756                                 getNextToken = false;
757                         } else {
758                                 mathcommand = symbolslex.getDocString();
759                                 if (suffixIs(mathcommand, '}'))
760                                         flags |= CharInfoMathNoTermination;
761                                 if (symbolslex.next(true)) {
762                                         if (symbolslex.lineNumber() != lineno) {
763                                                 // line in new format with mathcommand only
764                                                 getNextToken = false;
765                                         } else {
766                                                 // line in new format with mathcommand and mathpreamble
767                                                 mathpreamble = symbolslex.getString();
768                                         }
769                                 } else
770                                         breakout = true;
771                         }
772                 } else {
773                         breakout = true;
774                 }
775
776                 // backward compatibility
777                 if (mathpreamble == "esintoramsmath")
778                         mathpreamble = "esint|amsmath";
779
780                 if (!textpreamble.empty())
781                         if (textpreamble[0] != '\\')
782                                 flags |= CharInfoTextFeature;
783                 if (!mathpreamble.empty())
784                         if (mathpreamble[0] != '\\')
785                                 flags |= CharInfoMathFeature;
786
787                 CharInfo info = CharInfo(
788                         textcommand, mathcommand,
789                         textpreamble, mathpreamble,
790                         tipashortcut, flags);
791                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
792                            << to_utf8(info.textcommand()) << "' '" << info.textpreamble()
793                            << " '" << info.textfeature() << ' ' << info.textnotermination()
794                            << ' ' << to_utf8(info.mathcommand()) << "' '" << info.mathpreamble()
795                            << "' " << info.mathfeature() << ' ' << info.mathnotermination()
796                            << ' ' << info.combining() << ' ' << info.force()
797                            << ' ' << info.forceselected());
798
799                 // we assume that at least one command is nonempty when using unicodesymbols
800                 if (info.isUnicodeSymbol()) {
801                         unicodesymbols[symbol] = info;
802                 }
803
804                 if (breakout)
805                         break;
806         }
807
808         // Now read the encodings
809         enum {
810                 et_encoding = 1,
811                 et_end
812         };
813
814         LexerKeyword encodingtags[] = {
815                 { "encoding", et_encoding },
816                 { "end", et_end }
817         };
818
819         Lexer lex(encodingtags);
820         lex.setFile(encfile);
821         lex.setContext("Encodings::read");
822         while (lex.isOK()) {
823                 switch (lex.lex()) {
824                 case et_encoding:
825                 {
826                         lex.next();
827                         string const name = lex.getString();
828                         lex.next();
829                         string const latexname = lex.getString();
830                         lex.next();
831                         string const guiname = lex.getString();
832                         lex.next();
833                         string const iconvname = lex.getString();
834                         lex.next();
835                         string const width = lex.getString();
836                         bool fixedwidth = false;
837                         bool unsafe = false;
838                         if (width == "fixed")
839                                 fixedwidth = true;
840                         else if (width == "variable")
841                                 fixedwidth = false;
842                         else if (width == "variableunsafe") {
843                                 fixedwidth = false;
844                                 unsafe = true;
845                         }
846                         else
847                                 lex.printError("Unknown width");
848
849                         lex.next();
850                         string const p = lex.getString();
851                         Encoding::Package package = Encoding::none;
852                         if (p == "none")
853                                 package = Encoding::none;
854                         else if (p == "inputenc")
855                                 package = Encoding::inputenc;
856                         else if (p == "CJK")
857                                 package = Encoding::CJK;
858                         else if (p == "japanese")
859                                 package = Encoding::japanese;
860                         else
861                                 lex.printError("Unknown package");
862
863                         LYXERR(Debug::INFO, "Reading encoding " << name);
864                         encodinglist[name] = Encoding(name, latexname,
865                                 guiname, iconvname, fixedwidth, unsafe,
866                                 package);
867
868                         if (lex.lex() != et_end)
869                                 lex.printError("Missing end");
870                         break;
871                 }
872                 case et_end:
873                         lex.printError("Misplaced end");
874                         break;
875                 case Lexer::LEX_FEOF:
876                         break;
877                 default:
878                         lex.printError("Unknown tag");
879                         break;
880                 }
881         }
882
883         // Move all information from forcednotselected to forcedselected
884         for (CharSetMap::const_iterator it1 = forcednotselected.begin(); it1 != forcednotselected.end(); ++it1) {
885                 for (CharSetMap::iterator it2 = forcedselected.begin(); it2 != forcedselected.end(); ++it2) {
886                         if (it2->first != it1->first)
887                                 it2->second.insert(it1->second.begin(), it1->second.end());
888                 }
889         }
890
891 }
892
893
894 } // namespace lyx