]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/Preamble.cpp
484b00912b9d76f4ca47ec56c0b1fa775da9f2b3
[lyx.git] / src / tex2lyx / Preamble.cpp
1 /**
2  * \file Preamble.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 // {[(
13
14 #include <config.h>
15
16 #include "Preamble.h"
17 #include "tex2lyx.h"
18
19 #include "Encoding.h"
20 #include "LayoutFile.h"
21 #include "Layout.h"
22 #include "Lexer.h"
23 #include "TextClass.h"
24
25 #include "support/convert.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29
30 #include "support/regex.h"
31
32 #include <algorithm>
33 #include <iostream>
34
35 using namespace std;
36 using namespace lyx::support;
37
38
39 namespace lyx {
40
41 // special columntypes
42 extern map<char, int> special_columns;
43
44 Preamble preamble;
45
46 namespace {
47
48 // CJK languages are handled in text.cpp, polyglossia languages are listed
49 // further down.
50 /**
51  * known babel language names (including synonyms)
52  * not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai
53  * please keep this in sync with known_coded_languages line by line!
54  */
55 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
56 "american", "arabic", "arabtex", "australian", "austrian", "bahasa", "bahasai",
57 "bahasam", "basque", "belarusian", "brazil", "brazilian", "breton", "british",
58 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
59 "dutch", "english", "esperanto", "estonian", "farsi", "finnish", "francais",
60 "french", "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb",
61 "greek", "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
62 "irish", "italian", "japanese", "kazakh", "kurmanji", "latin", "latvian", "lithuanian",
63 "lowersorbian", "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian",
64 "newzealand", "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
65 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
66 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
67 "swedish", "thai", "turkish", "turkmen", "ukraineb", "ukrainian",
68 "uppersorbian", "UKenglish", "USenglish", "usorbian", "vietnam", "welsh",
69 0};
70
71 /**
72  * the same as known_languages with .lyx names
73  * please keep this in sync with known_languages line by line!
74  */
75 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
76 "american", "arabic_arabi", "arabic_arabtex", "australian", "austrian", "bahasa", "bahasa",
77 "bahasam", "basque", "belarusian", "brazilian", "brazilian", "breton", "british",
78 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
79 "dutch", "english", "esperanto", "estonian", "farsi", "finnish", "french",
80 "french", "french", "french", "french", "galician", "german", "german",
81 "greek", "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
82 "irish", "italian", "japanese", "kazakh", "kurmanji", "latin", "latvian", "lithuanian",
83 "lowersorbian", "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian",
84 "newzealand", "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
85 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
86 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
87 "swedish", "thai", "turkish", "turkmen", "ukrainian", "ukrainian",
88 "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
89 0};
90
91 /// languages with danish quotes (.lyx names)
92 const char * const known_danish_quotes_languages[] = {"danish", 0};
93
94 /// languages with english quotes (.lyx names)
95 const char * const known_english_quotes_languages[] = {"american", "australian",
96 "bahasa", "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
97 "esperanto", "hebrew", "irish", "korean", "newzealand", "portuguese", "scottish",
98 "thai", 0};
99
100 /// languages with french quotes (.lyx names)
101 const char * const known_french_quotes_languages[] = {"albanian",
102 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
103 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
104 "russian", "spanish", "spanish-mexico", "turkish", "turkmen", "ukrainian",
105 "vietnamese", 0};
106
107 /// languages with german quotes (.lyx names)
108 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
109 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
110 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
111
112 /// languages with polish quotes (.lyx names)
113 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
114 "dutch", "estonian", "magyar", "polish", "romanian", 0};
115
116 /// languages with swedish quotes (.lyx names)
117 const char * const known_swedish_quotes_languages[] = {"finnish",
118 "swedish", 0};
119
120 /// known language packages from the times before babel
121 const char * const known_old_language_packages[] = {"french", "frenchle",
122 "frenchpro", "german", "ngerman", "pmfrench", 0};
123
124 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
125
126 const char * const known_roman_fonts[] = { "ae", "beraserif", "bookman",
127 "ccfonts", "chancery", "charter", "cmr", "fourier", "lmodern", "mathpazo",
128 "mathptmx", "newcent", "utopia", 0};
129
130 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
131 "helvet", "lmss", 0};
132
133 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
134 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
135 "newcent", 0};
136
137 const char * const known_paper_sizes[] = { "a0paper", "b0paper", "c0paper",
138 "a1paper", "b1paper", "c1paper", "a2paper", "b2paper", "c2paper", "a3paper",
139 "b3paper", "c3paper", "a4paper", "b4paper", "c4paper", "a5paper", "b5paper",
140 "c5paper", "a6paper", "b6paper", "c6paper", "executivepaper", "legalpaper",
141 "letterpaper", "b0j", "b1j", "b2j", "b3j", "b4j", "b5j", "b6j", 0};
142
143 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
144 "executivepaper", "legalpaper", "letterpaper", 0};
145
146 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
147 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
148
149 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
150 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
151 "columnsep", 0};
152
153 /// commands that can start an \if...\else...\endif sequence
154 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
155 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
156 "ifsidecap", "ifupgreek", 0};
157
158 const char * const known_basic_colors[] = {"blue", "black", "cyan", "green",
159 "magenta", "red", "white", "yellow", 0};
160
161 const char * const known_basic_color_codes[] = {"#0000ff", "#000000", "#00ffff", "#00ff00",
162 "#ff00ff", "#ff0000", "#ffffff", "#ffff00", 0};
163
164 /// conditional commands with three arguments like \@ifundefined{}{}{}
165 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
166 0};
167
168 /// packages that work only in xetex
169 /// polyglossia is handled separately
170 const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
171 "fontbook", "fontwrap", "mathspec", "philokalia", "unisugar",
172 "xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
173
174 /// packages that are automatically skipped if loaded by LyX
175 const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
176 "amstext", "amsthm", "array", "babel", "booktabs", "calc", "CJK", "color", "float",
177 "fontspec", "graphicx", "hhline", "ifthen", "longtable", "makeidx", "multirow",
178 "nomencl", "pdfpages", "rotating", "rotfloat", "splitidx", "setspace",
179 "subscript", "textcomp", "ulem", "url", "varioref", "verbatim", "wrapfig",
180 "xunicode", 0};
181
182 // codes used to remove packages that are loaded automatically by LyX.
183 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
184 const char package_beg_sep = '\001';
185 const char package_mid_sep = '\002';
186 const char package_end_sep = '\003';
187
188
189 // returns true if at least one of the options in what has been found
190 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
191 {
192         if (opts.empty())
193                 return false;
194
195         bool found = false;
196         // the last language option is the document language (for babel and LyX)
197         // the last size option is the document font size
198         vector<string>::iterator it;
199         vector<string>::iterator position = opts.begin();
200         for (; *what; ++what) {
201                 it = find(opts.begin(), opts.end(), *what);
202                 if (it != opts.end()) {
203                         if (it >= position) {
204                                 found = true;
205                                 target = *what;
206                                 position = it;
207                         }
208                 }
209         }
210         return found;
211 }
212
213
214 void delete_opt(vector<string> & opts, char const * const * what)
215 {
216         if (opts.empty())
217                 return;
218
219         // remove found options from the list
220         // do this after handle_opt to avoid potential memory leaks
221         vector<string>::iterator it;
222         for (; *what; ++what) {
223                 it = find(opts.begin(), opts.end(), *what);
224                 if (it != opts.end())
225                         opts.erase(it);
226         }
227 }
228
229
230 /*!
231  * Split a package options string (keyval format) into a vector.
232  * Example input:
233  *   authorformat=smallcaps,
234  *   commabeforerest,
235  *   titleformat=colonsep,
236  *   bibformat={tabular,ibidem,numbered}
237  */
238 vector<string> split_options(string const & input)
239 {
240         vector<string> options;
241         string option;
242         Parser p(input);
243         while (p.good()) {
244                 Token const & t = p.get_token();
245                 if (t.asInput() == ",") {
246                         options.push_back(trimSpaceAndEol(option));
247                         option.erase();
248                 } else if (t.asInput() == "=") {
249                         option += '=';
250                         p.skip_spaces(true);
251                         if (p.next_token().asInput() == "{")
252                                 option += '{' + p.getArg('{', '}') + '}';
253                 } else if (t.cat() != catSpace)
254                         option += t.asInput();
255         }
256
257         if (!option.empty())
258                 options.push_back(trimSpaceAndEol(option));
259
260         return options;
261 }
262
263
264 /*!
265  * Retrieve a keyval option "name={value with=sign}" named \p name from
266  * \p options and return the value.
267  * The found option is also removed from \p options.
268  */
269 string process_keyval_opt(vector<string> & options, string name)
270 {
271         for (size_t i = 0; i < options.size(); ++i) {
272                 vector<string> option;
273                 split(options[i], option, '=');
274                 if (option.size() < 2)
275                         continue;
276                 if (option[0] == name) {
277                         options.erase(options.begin() + i);
278                         option.erase(option.begin());
279                         return join(option, "=");
280                 }
281         }
282         return "";
283 }
284
285 } // anonymous namespace
286
287
288 /**
289  * known polyglossia language names (including variants)
290  */
291 const char * const Preamble::polyglossia_languages[] = {
292 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
293 "nynorsk", "syriac", "arabic", "danish", "icelandic", "occitan", "tamil",
294 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
295 "irish", "portuges", "thai", "bahasai", "english", "italian", "romanian", "turkish",
296 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
297 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazil",
298 "brazilian", "finnish", "lithuanian", "scottish", "usorbian", "breton", "french",
299 "lsorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
300 "welsh", "catalan", "german", "malayalam", "slovenian", "coptic", "greek",
301 "marathi", "spanish",
302 "american", "ancient", "australian", "british", "monotonic", "newzealand",
303 "polytonic", 0};
304
305 /**
306  * the same as polyglossia_languages with .lyx names
307  * please keep this in sync with polyglossia_languages line by line!
308  */
309 const char * const Preamble::coded_polyglossia_languages[] = {
310 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
311 "nynorsk", "syriac", "arabic_arabi", "danish", "icelandic", "occitan", "tamil",
312 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
313 "irish", "portuges", "thai", "bahasa", "english", "italian", "romanian", "turkish",
314 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
315 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazilian",
316 "brazilian", "finnish", "lithuanian", "scottish", "uppersorbian", "breton", "french",
317 "lowersorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
318 "welsh", "catalan", "ngerman", "malayalam", "slovene", "coptic", "greek",
319 "marathi", "spanish",
320 "american", "ancientgreek", "australian", "british", "greek", "newzealand",
321 "polutonikogreek", 0};
322
323
324 bool Preamble::indentParagraphs() const
325 {
326         return h_paragraph_separation == "indent";
327 }
328
329
330 bool Preamble::isPackageUsed(string const & package) const
331 {
332         return used_packages.find(package) != used_packages.end();
333 }
334
335
336 vector<string> Preamble::getPackageOptions(string const & package) const
337 {
338         map<string, vector<string> >::const_iterator it = used_packages.find(package);
339         if (it != used_packages.end())
340                 return it->second;
341         return vector<string>();
342 }
343
344
345 void Preamble::registerAutomaticallyLoadedPackage(std::string const & package)
346 {
347         auto_packages.insert(package);
348 }
349
350
351 void Preamble::addModule(string const & module)
352 {
353         used_modules.push_back(module);
354 }
355
356
357 void Preamble::suppressDate(bool suppress)
358 {
359         if (suppress)
360                 h_suppress_date = "true";
361         else
362                 h_suppress_date = "false";
363 }
364
365
366 void Preamble::registerAuthor(std::string const & name)
367 {
368         Author author(from_utf8(name), empty_docstring());
369         author.setUsed(true);
370         authors_.record(author);
371         h_tracking_changes = "true";
372         h_output_changes = "true";
373 }
374
375
376 Author const & Preamble::getAuthor(std::string const & name) const
377 {
378         Author author(from_utf8(name), empty_docstring());
379         for (AuthorList::Authors::const_iterator it = authors_.begin();
380              it != authors_.end(); ++it)
381                 if (*it == author)
382                         return *it;
383         static Author const dummy;
384         return dummy;
385 }
386
387
388 void Preamble::add_package(string const & name, vector<string> & options)
389 {
390         // every package inherits the global options
391         if (used_packages.find(name) == used_packages.end())
392                 used_packages[name] = split_options(h_options);
393
394         vector<string> & v = used_packages[name];
395         v.insert(v.end(), options.begin(), options.end());
396         if (name == "jurabib") {
397                 // Don't output the order argument (see the cite command
398                 // handling code in text.cpp).
399                 vector<string>::iterator end =
400                         remove(options.begin(), options.end(), "natbiborder");
401                 end = remove(options.begin(), end, "jurabiborder");
402                 options.erase(end, options.end());
403         }
404 }
405
406
407 namespace {
408
409 // Given is a string like "scaled=0.9" or "Scale=0.9", return 0.9 * 100
410 bool scale_as_percentage(string const & scale, string & percentage)
411 {
412         string::size_type pos = scale.find('=');
413         if (pos != string::npos) {
414                 string value = scale.substr(pos + 1);
415                 if (isStrDbl(value)) {
416                         percentage = convert<string>(100 * convert<double>(value));
417                         return true;
418                 }
419         }
420         return false;
421 }
422
423
424 string remove_braces(string const & value)
425 {
426         if (value.empty())
427                 return value;
428         if (value[0] == '{' && value[value.length()-1] == '}')
429                 return value.substr(1, value.length()-2);
430         return value;
431 }
432
433 } // anonymous namespace
434
435
436 Preamble::Preamble() : one_language(true), title_layout_found(false),
437         h_font_cjk_set(false)
438 {
439         //h_backgroundcolor;
440         //h_boxbgcolor;
441         h_biblio_style            = "plain";
442         h_bibtex_command          = "default";
443         h_cite_engine             = "basic";
444         h_cite_engine_type        = "numerical";
445         h_color                   = "#008000";
446         h_defskip                 = "medskip";
447         //h_float_placement;
448         //h_fontcolor;
449         h_fontencoding            = "default";
450         h_font_roman              = "default";
451         h_font_sans               = "default";
452         h_font_typewriter         = "default";
453         h_font_math               = "auto";
454         h_font_default_family     = "default";
455         h_use_non_tex_fonts       = "false";
456         h_font_sc                 = "false";
457         h_font_osf                = "false";
458         h_font_sf_scale           = "100";
459         h_font_tt_scale           = "100";
460         //h_font_cjk
461         h_graphics                = "default";
462         h_default_output_format   = "default";
463         h_html_be_strict          = "false";
464         h_html_css_as_file        = "0";
465         h_html_math_output        = "0";
466         h_index                   = "Index";
467         h_index_command           = "default";
468         h_inputencoding           = "auto";
469         h_justification           = "true";
470         h_language                = "english";
471         h_language_package        = "none";
472         //h_listings_params;
473         h_maintain_unincluded_children = "false";
474         //h_margins;
475         //h_notefontcolor;
476         //h_options;
477         h_output_changes          = "false";
478         h_output_sync             = "0";
479         //h_output_sync_macro
480         h_papercolumns            = "1";
481         h_paperfontsize           = "default";
482         h_paperorientation        = "portrait";
483         h_paperpagestyle          = "default";
484         //h_papersides;
485         h_papersize               = "default";
486         h_paragraph_indentation   = "default";
487         h_paragraph_separation    = "indent";
488         //h_pdf_title;
489         //h_pdf_author;
490         //h_pdf_subject;
491         //h_pdf_keywords;
492         h_pdf_bookmarks           = "1";
493         h_pdf_bookmarksnumbered   = "0";
494         h_pdf_bookmarksopen       = "0";
495         h_pdf_bookmarksopenlevel  = "1";
496         h_pdf_breaklinks          = "0";
497         h_pdf_pdfborder           = "0";
498         h_pdf_colorlinks          = "0";
499         h_pdf_backref             = "section";
500         h_pdf_pdfusetitle         = "1";
501         //h_pdf_pagemode;
502         //h_pdf_quoted_options;
503         h_quotes_language         = "english";
504         h_secnumdepth             = "3";
505         h_shortcut                = "idx";
506         h_spacing                 = "single";
507         h_suppress_date           = "false";
508         h_textclass               = "article";
509         h_tocdepth                = "3";
510         h_tracking_changes        = "false";
511         h_use_bibtopic            = "false";
512         h_use_indices             = "false";
513         h_use_geometry            = "false";
514         h_use_default_options     = "false";
515         h_use_hyperref            = "false";
516         h_use_refstyle            = "0";
517         h_use_packages["amsmath"]    = "1";
518         h_use_packages["amssymb"]    = "0";
519         h_use_packages["esint"]      = "1";
520         h_use_packages["mhchem"]     = "0";
521         h_use_packages["mathdots"]   = "0";
522         h_use_packages["mathtools"]  = "0";
523         h_use_packages["stackrel"]   = "0";
524         h_use_packages["stmaryrd"]   = "0";
525         h_use_packages["undertilde"] = "0";
526 }
527
528
529 void Preamble::handle_hyperref(vector<string> & options)
530 {
531         // FIXME swallow inputencoding changes that might surround the
532         //       hyperref setup if it was written by LyX
533         h_use_hyperref = "true";
534         // swallow "unicode=true", since LyX does always write that
535         vector<string>::iterator it =
536                 find(options.begin(), options.end(), "unicode=true");
537         if (it != options.end())
538                 options.erase(it);
539         it = find(options.begin(), options.end(), "pdfusetitle");
540         if (it != options.end()) {
541                 h_pdf_pdfusetitle = "1";
542                 options.erase(it);
543         }
544         string bookmarks = process_keyval_opt(options, "bookmarks");
545         if (bookmarks == "true")
546                 h_pdf_bookmarks = "1";
547         else if (bookmarks == "false")
548                 h_pdf_bookmarks = "0";
549         if (h_pdf_bookmarks == "1") {
550                 string bookmarksnumbered =
551                         process_keyval_opt(options, "bookmarksnumbered");
552                 if (bookmarksnumbered == "true")
553                         h_pdf_bookmarksnumbered = "1";
554                 else if (bookmarksnumbered == "false")
555                         h_pdf_bookmarksnumbered = "0";
556                 string bookmarksopen =
557                         process_keyval_opt(options, "bookmarksopen");
558                 if (bookmarksopen == "true")
559                         h_pdf_bookmarksopen = "1";
560                 else if (bookmarksopen == "false")
561                         h_pdf_bookmarksopen = "0";
562                 if (h_pdf_bookmarksopen == "1") {
563                         string bookmarksopenlevel =
564                                 process_keyval_opt(options, "bookmarksopenlevel");
565                         if (!bookmarksopenlevel.empty())
566                                 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
567                 }
568         }
569         string breaklinks = process_keyval_opt(options, "breaklinks");
570         if (breaklinks == "true")
571                 h_pdf_breaklinks = "1";
572         else if (breaklinks == "false")
573                 h_pdf_breaklinks = "0";
574         string pdfborder = process_keyval_opt(options, "pdfborder");
575         if (pdfborder == "{0 0 0}")
576                 h_pdf_pdfborder = "1";
577         else if (pdfborder == "{0 0 1}")
578                 h_pdf_pdfborder = "0";
579         string backref = process_keyval_opt(options, "backref");
580         if (!backref.empty())
581                 h_pdf_backref = backref;
582         string colorlinks = process_keyval_opt(options, "colorlinks");
583         if (colorlinks == "true")
584                 h_pdf_colorlinks = "1";
585         else if (colorlinks == "false")
586                 h_pdf_colorlinks = "0";
587         string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
588         if (!pdfpagemode.empty())
589                 h_pdf_pagemode = pdfpagemode;
590         string pdftitle = process_keyval_opt(options, "pdftitle");
591         if (!pdftitle.empty()) {
592                 h_pdf_title = remove_braces(pdftitle);
593         }
594         string pdfauthor = process_keyval_opt(options, "pdfauthor");
595         if (!pdfauthor.empty()) {
596                 h_pdf_author = remove_braces(pdfauthor);
597         }
598         string pdfsubject = process_keyval_opt(options, "pdfsubject");
599         if (!pdfsubject.empty())
600                 h_pdf_subject = remove_braces(pdfsubject);
601         string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
602         if (!pdfkeywords.empty())
603                 h_pdf_keywords = remove_braces(pdfkeywords);
604         if (!options.empty()) {
605                 if (!h_pdf_quoted_options.empty())
606                         h_pdf_quoted_options += ',';
607                 h_pdf_quoted_options += join(options, ",");
608                 options.clear();
609         }
610 }
611
612
613 void Preamble::handle_geometry(vector<string> & options)
614 {
615         h_use_geometry = "true";
616         vector<string>::iterator it;
617         // paper orientation
618         if ((it = find(options.begin(), options.end(), "landscape")) != options.end()) {
619                 h_paperorientation = "landscape";
620                 options.erase(it);
621         }
622         // paper size
623         // keyval version: "paper=letter"
624         string paper = process_keyval_opt(options, "paper");
625         if (!paper.empty())
626                 h_papersize = paper + "paper";
627         // alternative version: "letterpaper"
628         handle_opt(options, known_paper_sizes, h_papersize);
629         delete_opt(options, known_paper_sizes);
630         // page margins
631         char const * const * margin = known_paper_margins;
632         for (; *margin; ++margin) {
633                 string value = process_keyval_opt(options, *margin);
634                 if (!value.empty()) {
635                         int k = margin - known_paper_margins;
636                         string name = known_coded_paper_margins[k];
637                         h_margins += '\\' + name + ' ' + value + '\n';
638                 }
639         }
640 }
641
642
643 void Preamble::handle_package(Parser &p, string const & name,
644                               string const & opts, bool in_lyx_preamble)
645 {
646         vector<string> options = split_options(opts);
647         add_package(name, options);
648         char const * const * where = 0;
649
650         if (is_known(name, known_xetex_packages)) {
651                 xetex = true;
652                 h_use_non_tex_fonts = "true";
653                 registerAutomaticallyLoadedPackage("fontspec");
654                 if (h_inputencoding == "auto")
655                         p.setEncoding("UTF-8");
656         }
657
658         // roman fonts
659         if (is_known(name, known_roman_fonts))
660                 h_font_roman = name;
661
662         if (name == "fourier") {
663                 h_font_roman = "utopia";
664                 // when font uses real small capitals
665                 if (opts == "expert")
666                         h_font_sc = "true";
667         }
668
669         else if (name == "mathpazo")
670                 h_font_roman = "palatino";
671
672         else if (name == "mathptmx")
673                 h_font_roman = "times";
674
675         // sansserif fonts
676         if (is_known(name, known_sans_fonts)) {
677                 h_font_sans = name;
678                 if (options.size() == 1) {
679                         if (scale_as_percentage(opts, h_font_sf_scale))
680                                 options.clear();
681                 }
682         }
683
684         // typewriter fonts
685         if (is_known(name, known_typewriter_fonts)) {
686                 // fourier can be set as roman font _only_
687                 // fourier as typewriter is handled in handling of \ttdefault
688                 if (name != "fourier") {
689                         h_font_typewriter = name;
690                         if (options.size() == 1) {
691                                 if (scale_as_percentage(opts, h_font_tt_scale))
692                                         options.clear();
693                         }
694                 }
695         }
696
697         // font uses old-style figure
698         if (name == "eco")
699                 h_font_osf = "true";
700
701         if (name == "refstyle")
702                 h_use_refstyle = "1";
703
704         // after the detection and handling of special cases, we can remove the
705         // fonts, otherwise they would appear in the preamble, see bug #7856
706         if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
707                 ||      is_known(name, known_typewriter_fonts))
708                 ;
709
710         else if (name == "amsmath" || name == "amssymb" ||
711                  name == "esint" || name == "mhchem" || name == "mathdots" ||
712                  name == "mathtools" || name == "stackrel" ||
713                  name == "stmaryrd" || name == "undertilde")
714                 h_use_packages[name] = "2";
715
716         else if (name == "babel") {
717                 h_language_package = "default";
718                 // One might think we would have to do nothing if babel is loaded
719                 // without any options to prevent pollution of the preamble with this
720                 // babel call in every roundtrip.
721                 // But the user could have defined babel-specific things afterwards. So
722                 // we need to keep it in the preamble to prevent cases like bug #7861.
723                 if (!opts.empty()) {
724                         // check if more than one option was used - used later for inputenc
725                         if (options.begin() != options.end() - 1)
726                                 one_language = false;
727                         // babel takes the last language of the option of its \usepackage
728                         // call as document language. If there is no such language option, the
729                         // last language in the documentclass options is used.
730                         handle_opt(options, known_languages, h_language);
731                         // translate the babel name to a LyX name
732                         h_language = babel2lyx(h_language);
733                         if (h_language == "japanese") {
734                                 // For Japanese, the encoding isn't indicated in the source
735                                 // file, and there's really not much we can do. We could
736                                 // 1) offer a list of possible encodings to choose from, or
737                                 // 2) determine the encoding of the file by inspecting it.
738                                 // For the time being, we leave the encoding alone so that
739                                 // we don't get iconv errors when making a wrong guess, and
740                                 // we will output a note at the top of the document
741                                 // explaining what to do.
742                                 Encoding const * const enc = encodings.fromIconvName(
743                                         p.getEncoding(), Encoding::japanese, false);
744                                 if (enc)
745                                         h_inputencoding = enc->name();
746                                 is_nonCJKJapanese = true;
747                                 // in this case babel can be removed from the preamble
748                                 registerAutomaticallyLoadedPackage("babel");
749                         } else {
750                                 // If babel is called with options, LyX puts them by default into the
751                                 // document class options. This works for most languages, except
752                                 // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
753                                 // perhaps in future others.
754                                 // Therefore keep the babel call as it is as the user might have
755                                 // reasons for it.
756                                 h_preamble << "\\usepackage[" << opts << "]{babel}\n";
757                         }
758                         delete_opt(options, known_languages);
759                 }
760                 else
761                         h_preamble << "\\usepackage{babel}\n";
762         }
763
764         else if (name == "polyglossia") {
765                 h_language_package = "default";
766                 h_default_output_format = "pdf4";
767                 h_use_non_tex_fonts = "true";
768                 xetex = true;
769                 registerAutomaticallyLoadedPackage("xunicode");
770                 if (h_inputencoding == "auto")
771                         p.setEncoding("UTF-8");
772         }
773
774         else if (name == "CJK") {
775                 // set the encoding to "auto" because it might be set to "default" by the babel handling
776                 // and this would not be correct for CJK
777                 if (h_inputencoding == "default")
778                         h_inputencoding = "auto";
779                 registerAutomaticallyLoadedPackage("CJK");
780         }
781
782         else if (name == "CJKutf8") {
783                 h_inputencoding = "UTF8";
784                 p.setEncoding("UTF-8");
785                 registerAutomaticallyLoadedPackage("CJKutf8");
786         }
787
788         else if (name == "fontenc") {
789                 h_fontencoding = getStringFromVector(options, ",");
790                 /* We could do the following for better round trip support,
791                  * but this makes the document less portable, so I skip it:
792                 if (h_fontencoding == lyxrc.fontenc)
793                         h_fontencoding = "global";
794                  */
795                 options.clear();
796         }
797
798         else if (name == "inputenc" || name == "luainputenc") {
799                 // h_inputencoding is only set when there is not more than one
800                 // inputenc option because otherwise h_inputencoding must be
801                 // set to "auto" (the default encoding of the document language)
802                 // Therefore check for the "," character.
803                 // It is also only set when there is not more than one babel
804                 // language option.
805                 if (opts.find(",") == string::npos && one_language == true)
806                         h_inputencoding = opts;
807                 if (!options.empty())
808                         p.setEncoding(options.back(), Encoding::inputenc);
809                 options.clear();
810         }
811
812         else if (name == "srcltx") {
813                 h_output_sync = "1";
814                 if (!opts.empty()) {
815                         h_output_sync_macro = "\\usepackage[" + opts + "]{srcltx}";
816                         options.clear();
817                 } else
818                         h_output_sync_macro = "\\usepackage{srcltx}";
819         }
820
821         else if (is_known(name, known_old_language_packages)) {
822                 // known language packages from the times before babel
823                 // if they are found and not also babel, they will be used as
824                 // custom language package
825                 h_language_package = "\\usepackage{" + name + "}";
826         }
827
828         else if (name == "prettyref")
829                 ; // ignore this FIXME: Use the package separator mechanism instead
830
831         else if (name == "lyxskak") {
832                 // ignore this and its options
833                 const char * const o[] = {"ps", "mover", 0};
834                 delete_opt(options, o);
835         }
836
837         else if (is_known(name, known_lyx_packages) && options.empty()) {
838                 if (name == "splitidx")
839                         h_use_indices = "true";
840                 if (!in_lyx_preamble) {
841                         h_preamble << package_beg_sep << name
842                                    << package_mid_sep << "\\usepackage{"
843                                    << name << '}';
844                         if (p.next_token().cat() == catNewline ||
845                             (p.next_token().cat() == catSpace &&
846                              p.next_next_token().cat() == catNewline))
847                                 h_preamble << '\n';
848                         h_preamble << package_end_sep;
849                 }
850         }
851
852         else if (name == "geometry")
853                 handle_geometry(options);
854
855         else if (name == "subfig")
856                 ; // ignore this FIXME: Use the package separator mechanism instead
857
858         else if ((where = is_known(name, known_languages)))
859                 h_language = known_coded_languages[where - known_languages];
860
861         else if (name == "natbib") {
862                 h_biblio_style = "plainnat";
863                 h_cite_engine = "natbib";
864                 h_cite_engine_type = "authoryear";
865                 vector<string>::iterator it =
866                         find(options.begin(), options.end(), "authoryear");
867                 if (it != options.end())
868                         options.erase(it);
869                 else {
870                         it = find(options.begin(), options.end(), "numbers");
871                         if (it != options.end()) {
872                                 h_cite_engine_type = "numerical";
873                                 options.erase(it);
874                         }
875                 }
876         }
877
878         else if (name == "jurabib") {
879                 h_biblio_style = "jurabib";
880                 h_cite_engine = "jurabib";
881                 h_cite_engine_type = "authoryear";
882         }
883
884         else if (name == "hyperref")
885                 handle_hyperref(options);
886
887         else if (!in_lyx_preamble) {
888                 if (options.empty())
889                         h_preamble << "\\usepackage{" << name << '}';
890                 else {
891                         h_preamble << "\\usepackage[" << opts << "]{"
892                                    << name << '}';
893                         options.clear();
894                 }
895                 if (p.next_token().cat() == catNewline ||
896                     (p.next_token().cat() == catSpace &&
897                      p.next_next_token().cat() == catNewline))
898                         h_preamble << '\n';
899         }
900
901         // We need to do something with the options...
902         if (!options.empty())
903                 cerr << "Ignoring options '" << join(options, ",")
904                      << "' of package " << name << '.' << endl;
905
906         // remove the whitespace
907         p.skip_spaces();
908 }
909
910
911 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
912 {
913         while (p.good()) {
914                 Token t = p.get_token();
915                 if (t.cat() == catEscape &&
916                     is_known(t.cs(), known_if_commands))
917                         handle_if(p, in_lyx_preamble);
918                 else {
919                         if (!in_lyx_preamble)
920                                 h_preamble << t.asInput();
921                         if (t.cat() == catEscape && t.cs() == "fi")
922                                 return;
923                 }
924         }
925 }
926
927
928 bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
929 {
930         // set the quote language
931         // LyX only knows the following quotes languages:
932         // english, swedish, german, polish, french and danish
933         // (quotes for "japanese" and "chinese-traditional" are missing because
934         //  they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
935         // conversion list taken from
936         // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
937         // (quotes for kazakh and interlingua are unknown)
938         // danish
939         if (is_known(h_language, known_danish_quotes_languages))
940                 h_quotes_language = "danish";
941         // french
942         else if (is_known(h_language, known_french_quotes_languages))
943                 h_quotes_language = "french";
944         // german
945         else if (is_known(h_language, known_german_quotes_languages))
946                 h_quotes_language = "german";
947         // polish
948         else if (is_known(h_language, known_polish_quotes_languages))
949                 h_quotes_language = "polish";
950         // swedish
951         else if (is_known(h_language, known_swedish_quotes_languages))
952                 h_quotes_language = "swedish";
953         //english
954         else if (is_known(h_language, known_english_quotes_languages))
955                 h_quotes_language = "english";
956
957         if (contains(h_float_placement, "H"))
958                 registerAutomaticallyLoadedPackage("float");
959         if (h_spacing != "single" && h_spacing != "default")
960                 registerAutomaticallyLoadedPackage("setspace");
961         if (h_use_packages["amsmath"] == "2") {
962                 // amsbsy and amstext are already provided by amsmath
963                 registerAutomaticallyLoadedPackage("amsbsy");
964                 registerAutomaticallyLoadedPackage("amstext");
965         }
966
967         // output the LyX file settings
968         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
969            << "\\lyxformat " << LYX_FORMAT << '\n'
970            << "\\begin_document\n"
971            << "\\begin_header\n"
972            << "\\textclass " << h_textclass << "\n";
973         string const raw = subdoc ? empty_string() : h_preamble.str();
974         if (!raw.empty()) {
975                 os << "\\begin_preamble\n";
976                 for (string::size_type i = 0; i < raw.size(); ++i) {
977                         if (raw[i] == package_beg_sep) {
978                                 // Here follows some package loading code that
979                                 // must be skipped if the package is loaded
980                                 // automatically.
981                                 string::size_type j = raw.find(package_mid_sep, i);
982                                 if (j == string::npos)
983                                         return false;
984                                 string::size_type k = raw.find(package_end_sep, j);
985                                 if (k == string::npos)
986                                         return false;
987                                 string const package = raw.substr(i + 1, j - i - 1);
988                                 string const replacement = raw.substr(j + 1, k - j - 1);
989                                 if (auto_packages.find(package) == auto_packages.end())
990                                         os << replacement;
991                                 i = k;
992                         } else
993                                 os.put(raw[i]);
994                 }
995                 os << "\n\\end_preamble\n";
996         }
997         if (!h_options.empty())
998                 os << "\\options " << h_options << "\n";
999         os << "\\use_default_options " << h_use_default_options << "\n";
1000         if (!used_modules.empty()) {
1001                 os << "\\begin_modules\n";
1002                 vector<string>::const_iterator const end = used_modules.end();
1003                 vector<string>::const_iterator it = used_modules.begin();
1004                 for (; it != end; ++it)
1005                         os << *it << '\n';
1006                 os << "\\end_modules\n";
1007         }
1008         os << "\\maintain_unincluded_children " << h_maintain_unincluded_children << "\n"
1009            << "\\language " << h_language << "\n"
1010            << "\\language_package " << h_language_package << "\n"
1011            << "\\inputencoding " << h_inputencoding << "\n"
1012            << "\\fontencoding " << h_fontencoding << "\n"
1013            << "\\font_roman " << h_font_roman << "\n"
1014            << "\\font_sans " << h_font_sans << "\n"
1015            << "\\font_typewriter " << h_font_typewriter << "\n"
1016            << "\\font_math " << h_font_math << "\n"
1017            << "\\font_default_family " << h_font_default_family << "\n"
1018            << "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n"
1019            << "\\font_sc " << h_font_sc << "\n"
1020            << "\\font_osf " << h_font_osf << "\n"
1021            << "\\font_sf_scale " << h_font_sf_scale << "\n"
1022            << "\\font_tt_scale " << h_font_tt_scale << '\n';
1023         if (!h_font_cjk.empty())
1024                 os << "\\font_cjk " << h_font_cjk << '\n';
1025         os << "\\graphics " << h_graphics << '\n'
1026            << "\\default_output_format " << h_default_output_format << "\n"
1027            << "\\output_sync " << h_output_sync << "\n";
1028         if (h_output_sync == "1")
1029                 os << "\\output_sync_macro \"" << h_output_sync_macro << "\"\n";
1030         os << "\\bibtex_command " << h_bibtex_command << "\n"
1031            << "\\index_command " << h_index_command << "\n";
1032         if (!h_float_placement.empty())
1033                 os << "\\float_placement " << h_float_placement << "\n";
1034         os << "\\paperfontsize " << h_paperfontsize << "\n"
1035            << "\\spacing " << h_spacing << "\n"
1036            << "\\use_hyperref " << h_use_hyperref << '\n';
1037         if (h_use_hyperref == "true") {
1038                 if (!h_pdf_title.empty())
1039                         os << "\\pdf_title \"" << h_pdf_title << "\"\n";
1040                 if (!h_pdf_author.empty())
1041                         os << "\\pdf_author \"" << h_pdf_author << "\"\n";
1042                 if (!h_pdf_subject.empty())
1043                         os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
1044                 if (!h_pdf_keywords.empty())
1045                         os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
1046                 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
1047                       "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
1048                       "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
1049                       "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
1050                       "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
1051                       "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
1052                       "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
1053                       "\\pdf_backref " << h_pdf_backref << "\n"
1054                       "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
1055                 if (!h_pdf_pagemode.empty())
1056                         os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
1057                 if (!h_pdf_quoted_options.empty())
1058                         os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
1059         }
1060         os << "\\papersize " << h_papersize << "\n"
1061            << "\\use_geometry " << h_use_geometry << '\n';
1062         for (map<string, string>::const_iterator it = h_use_packages.begin();
1063              it != h_use_packages.end(); ++it)
1064                 os << "\\use_package " << it->first << ' ' << it->second << '\n';
1065         os << "\\cite_engine " << h_cite_engine << '\n'
1066            << "\\cite_engine_type " << h_cite_engine_type << '\n'
1067            << "\\biblio_style " << h_biblio_style << "\n"
1068            << "\\use_bibtopic " << h_use_bibtopic << "\n"
1069            << "\\use_indices " << h_use_indices << "\n"
1070            << "\\paperorientation " << h_paperorientation << '\n'
1071            << "\\suppress_date " << h_suppress_date << '\n'
1072            << "\\justification " << h_justification << '\n'
1073            << "\\use_refstyle " << h_use_refstyle << '\n';
1074         if (!h_fontcolor.empty())
1075                 os << "\\fontcolor " << h_fontcolor << '\n';
1076         if (!h_notefontcolor.empty())
1077                 os << "\\notefontcolor " << h_notefontcolor << '\n';
1078         if (!h_backgroundcolor.empty())
1079                 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
1080         if (!h_boxbgcolor.empty())
1081                 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
1082         os << "\\index " << h_index << '\n'
1083            << "\\shortcut " << h_shortcut << '\n'
1084            << "\\color " << h_color << '\n'
1085            << "\\end_index\n";
1086         os << h_margins
1087            << "\\secnumdepth " << h_secnumdepth << "\n"
1088            << "\\tocdepth " << h_tocdepth << "\n"
1089            << "\\paragraph_separation " << h_paragraph_separation << "\n";
1090         if (h_paragraph_separation == "skip")
1091                 os << "\\defskip " << h_defskip << "\n";
1092         else
1093                 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
1094         os << "\\quotes_language " << h_quotes_language << "\n"
1095            << "\\papercolumns " << h_papercolumns << "\n"
1096            << "\\papersides " << h_papersides << "\n"
1097            << "\\paperpagestyle " << h_paperpagestyle << "\n";
1098         if (!h_listings_params.empty())
1099                 os << "\\listings_params " << h_listings_params << "\n";
1100         os << "\\tracking_changes " << h_tracking_changes << "\n"
1101            << "\\output_changes " << h_output_changes << "\n"
1102            << "\\html_math_output " << h_html_math_output << "\n"
1103            << "\\html_css_as_file " << h_html_css_as_file << "\n"
1104            << "\\html_be_strict " << h_html_be_strict << "\n"
1105            << authors_
1106            << "\\end_header\n\n"
1107            << "\\begin_body\n";
1108         return true;
1109 }
1110
1111
1112 void Preamble::parse(Parser & p, string const & forceclass,
1113                      TeX2LyXDocClass & tc)
1114 {
1115         // initialize fixed types
1116         special_columns['D'] = 3;
1117         bool is_full_document = false;
1118         bool is_lyx_file = false;
1119         bool in_lyx_preamble = false;
1120
1121         // determine whether this is a full document or a fragment for inclusion
1122         while (p.good()) {
1123                 Token const & t = p.get_token();
1124
1125                 if (t.cat() == catEscape && t.cs() == "documentclass") {
1126                         is_full_document = true;
1127                         break;
1128                 }
1129         }
1130         p.reset();
1131
1132         while (is_full_document && p.good()) {
1133                 Token const & t = p.get_token();
1134
1135 #ifdef FILEDEBUG
1136                 cerr << "t: " << t << "\n";
1137 #endif
1138
1139                 //
1140                 // cat codes
1141                 //
1142                 if (!in_lyx_preamble &&
1143                     (t.cat() == catLetter ||
1144                      t.cat() == catSuper ||
1145                      t.cat() == catSub ||
1146                      t.cat() == catOther ||
1147                      t.cat() == catMath ||
1148                      t.cat() == catActive ||
1149                      t.cat() == catBegin ||
1150                      t.cat() == catEnd ||
1151                      t.cat() == catAlign ||
1152                      t.cat() == catParameter))
1153                         h_preamble << t.cs();
1154
1155                 else if (!in_lyx_preamble &&
1156                          (t.cat() == catSpace || t.cat() == catNewline))
1157                         h_preamble << t.asInput();
1158
1159                 else if (t.cat() == catComment) {
1160                         static regex const islyxfile("%% LyX .* created this file");
1161                         static regex const usercommands("User specified LaTeX commands");
1162
1163                         string const comment = t.asInput();
1164
1165                         // magically switch encoding default if it looks like XeLaTeX
1166                         static string const magicXeLaTeX =
1167                                 "% This document must be compiled with XeLaTeX ";
1168                         if (comment.size() > magicXeLaTeX.size()
1169                                   && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1170                                   && h_inputencoding == "auto") {
1171                                 cerr << "XeLaTeX comment found, switching to UTF8\n";
1172                                 h_inputencoding = "utf8";
1173                         }
1174                         smatch sub;
1175                         if (regex_search(comment, sub, islyxfile)) {
1176                                 is_lyx_file = true;
1177                                 in_lyx_preamble = true;
1178                         } else if (is_lyx_file
1179                                    && regex_search(comment, sub, usercommands))
1180                                 in_lyx_preamble = false;
1181                         else if (!in_lyx_preamble)
1182                                 h_preamble << t.asInput();
1183                 }
1184
1185                 else if (t.cs() == "pagestyle")
1186                         h_paperpagestyle = p.verbatim_item();
1187
1188                 else if (t.cs() == "setdefaultlanguage") {
1189                         xetex = true;
1190                         // We don't yet care about non-language variant options
1191                         // because LyX doesn't support this yet, see bug #8214
1192                         if (p.hasOpt()) {
1193                                 string langopts = p.getOpt();
1194                                 // check if the option contains a variant, if yes, extract it
1195                                 string::size_type pos_var = langopts.find("variant");
1196                                 string::size_type i = langopts.find(',', pos_var);
1197                                 string::size_type k = langopts.find('=', pos_var);
1198                                 if (pos_var != string::npos){
1199                                         string variant;
1200                                         if (i == string::npos)
1201                                                 variant = langopts.substr(k + 1, langopts.length() - k - 2);
1202                                         else
1203                                                 variant = langopts.substr(k + 1, i - k - 1);
1204                                         h_language = variant;
1205                                 }
1206                                 p.verbatim_item();
1207                         } else
1208                                 h_language = p.verbatim_item();
1209                         //finally translate the poyglossia name to a LyX name
1210                         h_language = polyglossia2lyx(h_language);
1211                 }
1212
1213                 else if (t.cs() == "setotherlanguage") {
1214                         // We don't yet care about the option because LyX doesn't
1215                         // support this yet, see bug #8214
1216                         p.hasOpt() ? p.getOpt() : string();
1217                         p.verbatim_item();
1218                 }
1219
1220                 else if (t.cs() == "setmainfont") {
1221                         // we don't care about the option
1222                         p.hasOpt() ? p.getOpt() : string();
1223                         h_font_roman = p.getArg('{', '}');
1224                 }
1225
1226                 else if (t.cs() == "setsansfont" || t.cs() == "setmonofont") {
1227                         // LyX currently only supports the scale option
1228                         string scale;
1229                         if (p.hasOpt()) {
1230                                 string fontopts = p.getArg('[', ']');
1231                                 // check if the option contains a scaling, if yes, extract it
1232                                 string::size_type pos = fontopts.find("Scale");
1233                                 if (pos != string::npos) {
1234                                         string::size_type i = fontopts.find(',', pos);
1235                                         if (i == string::npos)
1236                                                 scale_as_percentage(fontopts.substr(pos + 1), scale);
1237                                         else
1238                                                 scale_as_percentage(fontopts.substr(pos, i - pos), scale);
1239                                 }
1240                         }
1241                         if (t.cs() == "setsansfont") {
1242                                 if (!scale.empty())
1243                                         h_font_sf_scale = scale;
1244                                 h_font_sans = p.getArg('{', '}');
1245                         } else {
1246                                 if (!scale.empty())
1247                                         h_font_tt_scale = scale;
1248                                 h_font_typewriter = p.getArg('{', '}');
1249                         }
1250                 }
1251
1252                 else if (t.cs() == "date") {
1253                         string argument = p.getArg('{', '}');
1254                         if (argument.empty())
1255                                 h_suppress_date = "true";
1256                         else
1257                                 h_preamble << t.asInput() << '{' << argument << '}';
1258                 }
1259
1260                 else if (t.cs() == "color") {
1261                         string const space =
1262                                 (p.hasOpt() ? p.getOpt() : string());
1263                         string argument = p.getArg('{', '}');
1264                         // check the case that a standard color is used
1265                         if (space.empty() && is_known(argument, known_basic_colors)) {
1266                                 h_fontcolor = rgbcolor2code(argument);
1267                                 preamble.registerAutomaticallyLoadedPackage("color");
1268                         } else if (space.empty() && argument == "document_fontcolor")
1269                                 preamble.registerAutomaticallyLoadedPackage("color");
1270                         // check the case that LyX's document_fontcolor is defined
1271                         // but not used for \color
1272                         else {
1273                                 h_preamble << t.asInput();
1274                                 if (!space.empty())
1275                                         h_preamble << space;
1276                                 h_preamble << '{' << argument << '}';
1277                                 // the color might already be set because \definecolor
1278                                 // is parsed before this
1279                                 h_fontcolor = "";
1280                         }
1281                 }
1282
1283                 else if (t.cs() == "pagecolor") {
1284                         string argument = p.getArg('{', '}');
1285                         // check the case that a standard color is used
1286                         if (is_known(argument, known_basic_colors)) {
1287                                 h_backgroundcolor = rgbcolor2code(argument);
1288                         } else if (argument == "page_backgroundcolor")
1289                                 preamble.registerAutomaticallyLoadedPackage("color");
1290                         // check the case that LyX's page_backgroundcolor is defined
1291                         // but not used for \pagecolor
1292                         else {
1293                                 h_preamble << t.asInput() << '{' << argument << '}';
1294                                 // the color might already be set because \definecolor
1295                                 // is parsed before this
1296                                 h_backgroundcolor = "";
1297                         }
1298                 }
1299
1300                 else if (t.cs() == "makeatletter") {
1301                         // LyX takes care of this
1302                         p.setCatCode('@', catLetter);
1303                 }
1304
1305                 else if (t.cs() == "makeatother") {
1306                         // LyX takes care of this
1307                         p.setCatCode('@', catOther);
1308                 }
1309
1310                 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1311                       || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1312                       || t.cs() == "providecommand" || t.cs() == "providecommandx"
1313                                 || t.cs() == "DeclareRobustCommand"
1314                       || t.cs() == "DeclareRobustCommandx"
1315                                 || t.cs() == "ProvideTextCommandDefault"
1316                                 || t.cs() == "DeclareMathAccent") {
1317                         bool star = false;
1318                         if (p.next_token().character() == '*') {
1319                                 p.get_token();
1320                                 star = true;
1321                         }
1322                         string const name = p.verbatim_item();
1323                         string const opt1 = p.getFullOpt();
1324                         string const opt2 = p.getFullOpt();
1325                         string const body = p.verbatim_item();
1326                         // font settings
1327                         if (name == "\\rmdefault")
1328                                 if (is_known(body, known_roman_fonts))
1329                                         h_font_roman = body;
1330                         if (name == "\\sfdefault")
1331                                 if (is_known(body, known_sans_fonts))
1332                                         h_font_sans = body;
1333                         if (name == "\\ttdefault")
1334                                 if (is_known(body, known_typewriter_fonts))
1335                                         h_font_typewriter = body;
1336                         if (name == "\\familydefault") {
1337                                 string family = body;
1338                                 // remove leading "\"
1339                                 h_font_default_family = family.erase(0,1);
1340                         }
1341
1342                         // remove the lyxdot definition that is re-added by LyX
1343                         // if necessary
1344                         if (name == "\\lyxdot")
1345                                 in_lyx_preamble = true;
1346
1347                         // Add the command to the known commands
1348                         add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1349
1350                         // only non-lyxspecific stuff
1351                         if (!in_lyx_preamble) {
1352                                 ostringstream ss;
1353                                 ss << '\\' << t.cs();
1354                                 if (star)
1355                                         ss << '*';
1356                                 ss << '{' << name << '}' << opt1 << opt2
1357                                    << '{' << body << "}";
1358                                 h_preamble << ss.str();
1359 /*
1360                                 ostream & out = in_preamble ? h_preamble : os;
1361                                 out << "\\" << t.cs() << "{" << name << "}"
1362                                     << opts << "{" << body << "}";
1363 */
1364                         }
1365                 }
1366
1367                 else if (t.cs() == "documentclass") {
1368                         vector<string>::iterator it;
1369                         vector<string> opts = split_options(p.getArg('[', ']'));
1370                         handle_opt(opts, known_fontsizes, h_paperfontsize);
1371                         delete_opt(opts, known_fontsizes);
1372                         // delete "pt" at the end
1373                         string::size_type i = h_paperfontsize.find("pt");
1374                         if (i != string::npos)
1375                                 h_paperfontsize.erase(i);
1376                         // The documentclass options are always parsed before the options
1377                         // of the babel call so that a language cannot overwrite the babel
1378                         // options.
1379                         handle_opt(opts, known_languages, h_language);
1380                         delete_opt(opts, known_languages);
1381
1382                         // paper orientation
1383                         if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1384                                 h_paperorientation = "landscape";
1385                                 opts.erase(it);
1386                         }
1387                         // paper sides
1388                         if ((it = find(opts.begin(), opts.end(), "oneside"))
1389                                  != opts.end()) {
1390                                 h_papersides = "1";
1391                                 opts.erase(it);
1392                         }
1393                         if ((it = find(opts.begin(), opts.end(), "twoside"))
1394                                  != opts.end()) {
1395                                 h_papersides = "2";
1396                                 opts.erase(it);
1397                         }
1398                         // paper columns
1399                         if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1400                                  != opts.end()) {
1401                                 h_papercolumns = "1";
1402                                 opts.erase(it);
1403                         }
1404                         if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1405                                  != opts.end()) {
1406                                 h_papercolumns = "2";
1407                                 opts.erase(it);
1408                         }
1409                         // paper sizes
1410                         // some size options are known to any document classes, other sizes
1411                         // are handled by the \geometry command of the geometry package
1412                         handle_opt(opts, known_class_paper_sizes, h_papersize);
1413                         delete_opt(opts, known_class_paper_sizes);
1414                         // the remaining options
1415                         h_options = join(opts, ",");
1416                         // FIXME This does not work for classes that have a
1417                         //       different name in LyX than in LaTeX
1418                         h_textclass = p.getArg('{', '}');
1419                 }
1420
1421                 else if (t.cs() == "usepackage") {
1422                         string const options = p.getArg('[', ']');
1423                         string const name = p.getArg('{', '}');
1424                         vector<string> vecnames;
1425                         split(name, vecnames, ',');
1426                         vector<string>::const_iterator it  = vecnames.begin();
1427                         vector<string>::const_iterator end = vecnames.end();
1428                         for (; it != end; ++it)
1429                                 handle_package(p, trimSpaceAndEol(*it), options,
1430                                                in_lyx_preamble);
1431                 }
1432
1433                 else if (t.cs() == "inputencoding") {
1434                         string const encoding = p.getArg('{','}');
1435                         h_inputencoding = encoding;
1436                         p.setEncoding(encoding, Encoding::inputenc);
1437                 }
1438
1439                 else if (t.cs() == "newenvironment") {
1440                         string const name = p.getArg('{', '}');
1441                         string const opt1 = p.getFullOpt();
1442                         string const opt2 = p.getFullOpt();
1443                         string const beg = p.verbatim_item();
1444                         string const end = p.verbatim_item();
1445                         if (!in_lyx_preamble) {
1446                                 h_preamble << "\\newenvironment{" << name
1447                                            << '}' << opt1 << opt2 << '{'
1448                                            << beg << "}{" << end << '}';
1449                         }
1450                         add_known_environment(name, opt1, !opt2.empty(),
1451                                               from_utf8(beg), from_utf8(end));
1452
1453                 }
1454
1455                 else if (t.cs() == "newtheorem") {
1456                         string const name = p.getArg('{', '}');
1457                         string const opt1 = p.getFullOpt();
1458                         string const opt2 = p.getFullOpt();
1459                         string const body = p.verbatim_item();
1460                         string const opt3 = p.getFullOpt();
1461
1462                         add_known_theorem(name, opt1, !opt2.empty(),
1463                                 from_utf8("\\newtheorem{" + name + '}' +
1464                                           opt1 + opt2 + '{' + body + '}' + opt3));
1465
1466                         if (!in_lyx_preamble)
1467                                 h_preamble << "\\newtheorem{" << name << '}'
1468                                            << opt1 << opt2 << '{' << '}' << opt3;
1469                 }
1470
1471                 else if (t.cs() == "def") {
1472                         string name = p.get_token().cs();
1473                         // In fact, name may be more than the name:
1474                         // In the test case of bug 8116
1475                         // name == "csname SF@gobble@opt \endcsname".
1476                         // Therefore, we need to use asInput() instead of cs().
1477                         while (p.next_token().cat() != catBegin)
1478                                 name += p.get_token().asInput();
1479                         if (!in_lyx_preamble)
1480                                 h_preamble << "\\def\\" << name << '{'
1481                                            << p.verbatim_item() << "}";
1482                 }
1483
1484                 else if (t.cs() == "newcolumntype") {
1485                         string const name = p.getArg('{', '}');
1486                         trimSpaceAndEol(name);
1487                         int nargs = 0;
1488                         string opts = p.getOpt();
1489                         if (!opts.empty()) {
1490                                 istringstream is(string(opts, 1));
1491                                 is >> nargs;
1492                         }
1493                         special_columns[name[0]] = nargs;
1494                         h_preamble << "\\newcolumntype{" << name << "}";
1495                         if (nargs)
1496                                 h_preamble << "[" << nargs << "]";
1497                         h_preamble << "{" << p.verbatim_item() << "}";
1498                 }
1499
1500                 else if (t.cs() == "setcounter") {
1501                         string const name = p.getArg('{', '}');
1502                         string const content = p.getArg('{', '}');
1503                         if (name == "secnumdepth")
1504                                 h_secnumdepth = content;
1505                         else if (name == "tocdepth")
1506                                 h_tocdepth = content;
1507                         else
1508                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1509                 }
1510
1511                 else if (t.cs() == "setlength") {
1512                         string const name = p.verbatim_item();
1513                         string const content = p.verbatim_item();
1514                         // the paragraphs are only not indented when \parindent is set to zero
1515                         if (name == "\\parindent" && content != "") {
1516                                 if (content[0] == '0')
1517                                         h_paragraph_separation = "skip";
1518                                 else
1519                                         h_paragraph_indentation = translate_len(content);
1520                         } else if (name == "\\parskip") {
1521                                 if (content == "\\smallskipamount")
1522                                         h_defskip = "smallskip";
1523                                 else if (content == "\\medskipamount")
1524                                         h_defskip = "medskip";
1525                                 else if (content == "\\bigskipamount")
1526                                         h_defskip = "bigskip";
1527                                 else
1528                                         h_defskip = content;
1529                         } else
1530                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1531                 }
1532
1533                 else if (t.cs() == "onehalfspacing")
1534                         h_spacing = "onehalf";
1535
1536                 else if (t.cs() == "doublespacing")
1537                         h_spacing = "double";
1538
1539                 else if (t.cs() == "setstretch")
1540                         h_spacing = "other " + p.verbatim_item();
1541
1542                 else if (t.cs() == "synctex") {
1543                         // the scheme is \synctex=value
1544                         // where value can only be "1" or "-1"
1545                         h_output_sync = "1";
1546                         // there can be any character behind the value (e.g. a linebreak or a '\'
1547                         // therefore we extract it char by char
1548                         p.get_token();
1549                         string value = p.get_token().asInput();
1550                         if (value == "-")
1551                                 value += p.get_token().asInput();
1552                         h_output_sync_macro = "\\synctex=" + value;
1553                 }
1554
1555                 else if (t.cs() == "begin") {
1556                         string const name = p.getArg('{', '}');
1557                         if (name == "document")
1558                                 break;
1559                         h_preamble << "\\begin{" << name << "}";
1560                 }
1561
1562                 else if (t.cs() == "geometry") {
1563                         vector<string> opts = split_options(p.getArg('{', '}'));
1564                         handle_geometry(opts);
1565                 }
1566
1567                 else if (t.cs() == "definecolor") {
1568                         string const color = p.getArg('{', '}');
1569                         string const space = p.getArg('{', '}');
1570                         string const value = p.getArg('{', '}');
1571                         if (color == "document_fontcolor" && space == "rgb") {
1572                                 RGBColor c(RGBColorFromLaTeX(value));
1573                                 h_fontcolor = X11hexname(c);
1574                         } else if (color == "note_fontcolor" && space == "rgb") {
1575                                 RGBColor c(RGBColorFromLaTeX(value));
1576                                 h_notefontcolor = X11hexname(c);
1577                         } else if (color == "page_backgroundcolor" && space == "rgb") {
1578                                 RGBColor c(RGBColorFromLaTeX(value));
1579                                 h_backgroundcolor = X11hexname(c);
1580                         } else if (color == "shadecolor" && space == "rgb") {
1581                                 RGBColor c(RGBColorFromLaTeX(value));
1582                                 h_boxbgcolor = X11hexname(c);
1583                         } else {
1584                                 h_preamble << "\\definecolor{" << color
1585                                            << "}{" << space << "}{" << value
1586                                            << '}';
1587                         }
1588                 }
1589
1590                 else if (t.cs() == "bibliographystyle")
1591                         h_biblio_style = p.verbatim_item();
1592
1593                 else if (t.cs() == "jurabibsetup") {
1594                         // FIXME p.getArg('{', '}') is most probably wrong (it
1595                         //       does not handle nested braces).
1596                         //       Use p.verbatim_item() instead.
1597                         vector<string> jurabibsetup =
1598                                 split_options(p.getArg('{', '}'));
1599                         // add jurabibsetup to the jurabib package options
1600                         add_package("jurabib", jurabibsetup);
1601                         if (!jurabibsetup.empty()) {
1602                                 h_preamble << "\\jurabibsetup{"
1603                                            << join(jurabibsetup, ",") << '}';
1604                         }
1605                 }
1606
1607                 else if (t.cs() == "hypersetup") {
1608                         vector<string> hypersetup =
1609                                 split_options(p.verbatim_item());
1610                         // add hypersetup to the hyperref package options
1611                         handle_hyperref(hypersetup);
1612                         if (!hypersetup.empty()) {
1613                                 h_preamble << "\\hypersetup{"
1614                                            << join(hypersetup, ",") << '}';
1615                         }
1616                 }
1617
1618                 else if (is_known(t.cs(), known_if_3arg_commands)) {
1619                         // prevent misparsing of \usepackage if it is used
1620                         // as an argument (see e.g. our own output of
1621                         // \@ifundefined above)
1622                         string const arg1 = p.verbatim_item();
1623                         string const arg2 = p.verbatim_item();
1624                         string const arg3 = p.verbatim_item();
1625                         // test case \@ifundefined{date}{}{\date{}}
1626                         if (t.cs() == "@ifundefined" && arg1 == "date" &&
1627                             arg2.empty() && arg3 == "\\date{}") {
1628                                 h_suppress_date = "true";
1629                         // older tex2lyx versions did output
1630                         // \@ifundefined{definecolor}{\usepackage{color}}{}
1631                         } else if (t.cs() == "@ifundefined" &&
1632                                    arg1 == "definecolor" &&
1633                                    arg2 == "\\usepackage{color}" &&
1634                                    arg3.empty()) {
1635                                 if (!in_lyx_preamble)
1636                                         h_preamble << package_beg_sep
1637                                                    << "color"
1638                                                    << package_mid_sep
1639                                                    << "\\@ifundefined{definecolor}{color}{}"
1640                                                    << package_end_sep;
1641                         // test for case
1642                         //\@ifundefined{showcaptionsetup}{}{%
1643                         // \PassOptionsToPackage{caption=false}{subfig}}
1644                         // that LyX uses for subfloats
1645                         } else if (t.cs() == "@ifundefined" &&
1646                                    arg1 == "showcaptionsetup" && arg2.empty()
1647                                 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1648                                 ; // do nothing
1649                         } else if (!in_lyx_preamble) {
1650                                 h_preamble << t.asInput()
1651                                            << '{' << arg1 << '}'
1652                                            << '{' << arg2 << '}'
1653                                            << '{' << arg3 << '}';
1654                         }
1655                 }
1656
1657                 else if (is_known(t.cs(), known_if_commands)) {
1658                         // must not parse anything in conditional code, since
1659                         // LyX would output the parsed contents unconditionally
1660                         if (!in_lyx_preamble)
1661                                 h_preamble << t.asInput();
1662                         handle_if(p, in_lyx_preamble);
1663                 }
1664
1665                 else if (!t.cs().empty() && !in_lyx_preamble)
1666                         h_preamble << '\\' << t.cs();
1667         }
1668
1669         // remove the whitespace
1670         p.skip_spaces();
1671
1672         // Force textclass if the user wanted it
1673         if (!forceclass.empty())
1674                 h_textclass = forceclass;
1675         tc.setName(h_textclass);
1676         if (!tc.load()) {
1677                 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1678                 exit(EXIT_FAILURE);
1679         }
1680         if (h_papersides.empty()) {
1681                 ostringstream ss;
1682                 ss << tc.sides();
1683                 h_papersides = ss.str();
1684         }
1685
1686         // If the CJK package is used we cannot set the document language from
1687         // the babel options. Instead, we guess which language is used most
1688         // and set this one.
1689         default_language = h_language;
1690         if (is_full_document &&
1691             (auto_packages.find("CJK") != auto_packages.end() ||
1692              auto_packages.find("CJKutf8") != auto_packages.end())) {
1693                 p.pushPosition();
1694                 h_language = guessLanguage(p, default_language);
1695                 p.popPosition();
1696         }
1697 }
1698
1699
1700 string babel2lyx(string const & language)
1701 {
1702         char const * const * where = is_known(language, known_languages);
1703         if (where)
1704                 return known_coded_languages[where - known_languages];
1705         return language;
1706 }
1707
1708
1709 string Preamble::polyglossia2lyx(string const & language)
1710 {
1711         char const * const * where = is_known(language, polyglossia_languages);
1712         if (where)
1713                 return coded_polyglossia_languages[where - polyglossia_languages];
1714         return language;
1715 }
1716
1717
1718 string rgbcolor2code(string const & name)
1719 {
1720         char const * const * where = is_known(name, known_basic_colors);
1721         if (where) {
1722                 // "red", "green" etc
1723                 return known_basic_color_codes[where - known_basic_colors];
1724         }
1725         // "255,0,0", "0,255,0" etc
1726         RGBColor c(RGBColorFromLaTeX(name));
1727         return X11hexname(c);
1728 }
1729
1730 // }])
1731
1732
1733 } // namespace lyx