]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/Preamble.cpp
Update all layouts to format 36.
[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 "LayoutFile.h"
20 #include "Layout.h"
21 #include "Lexer.h"
22 #include "TextClass.h"
23
24 #include "support/convert.h"
25 #include "support/FileName.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
28
29 #include "support/regex.h"
30
31 #include <algorithm>
32 #include <iostream>
33
34 using namespace std;
35 using namespace lyx::support;
36
37
38 namespace lyx {
39
40 // special columntypes
41 extern map<char, int> special_columns;
42
43 Preamble preamble;
44
45 namespace {
46
47 //add this to known_languages when updating to lyxformat 266:
48 // "armenian" (needs special handling since not supported by standard babel)
49 //add these to known_languages when updating to lyxformat 268:
50 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
51 // Both changes require first that support for non-babel languages (CJK,
52 // armtex) is added.
53 /**
54  * known babel language names (including synonyms)
55  * not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai
56  * not yet supported by LyX: kurmanji
57  * please keep this in sync with known_coded_languages line by line!
58  */
59 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
60 "american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
61 "basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
62 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
63 "english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
64 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
65 "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
66 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
67 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
68 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
69 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
70 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
71 "swedish", "thai", "turkish", "turkmen", "ukraineb", "ukrainian",
72 "uppersorbian", "UKenglish", "USenglish", "usorbian", "vietnam", "welsh",
73 0};
74
75 /**
76  * the same as known_languages with .lyx names
77  * please keep this in sync with known_languages line by line!
78  */
79 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
80 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
81 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
82 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
83 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
84 "french", "french", "french", "galician", "german", "german", "greek",
85 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
86 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
87 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
88 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
89 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
90 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
91 "swedish", "thai", "turkish", "turkmen", "ukrainian", "ukrainian",
92 "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
93 0};
94
95 /// languages with english quotes (.lyx names)
96 const char * const known_english_quotes_languages[] = {"american", "bahasa",
97 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
98 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "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 const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
170 "fontbook", "fontwrap", "mathspec", "philokalia", "polyglossia", "unisugar",
171 "xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
172
173 // codes used to remove packages that are loaded automatically by LyX.
174 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
175 const char package_beg_sep = '\001';
176 const char package_mid_sep = '\002';
177 const char package_end_sep = '\003';
178
179
180 // returns true if at least one of the options in what has been found
181 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
182 {
183         if (opts.empty())
184                 return false;
185
186         bool found = false;
187         // the last language option is the document language (for babel and LyX)
188         // the last size option is the document font size
189         vector<string>::iterator it;
190         vector<string>::iterator position = opts.begin();
191         for (; *what; ++what) {
192                 it = find(opts.begin(), opts.end(), *what);
193                 if (it != opts.end()) {
194                         if (it >= position) {
195                                 found = true;
196                                 target = *what;
197                                 position = it;
198                         }
199                 }
200         }
201         return found;
202 }
203
204
205 void delete_opt(vector<string> & opts, char const * const * what)
206 {
207         if (opts.empty())
208                 return;
209
210         // remove found options from the list
211         // do this after handle_opt to avoid potential memory leaks
212         vector<string>::iterator it;
213         for (; *what; ++what) {
214                 it = find(opts.begin(), opts.end(), *what);
215                 if (it != opts.end())
216                         opts.erase(it);
217         }
218 }
219
220
221 /*!
222  * Split a package options string (keyval format) into a vector.
223  * Example input:
224  *   authorformat=smallcaps,
225  *   commabeforerest,
226  *   titleformat=colonsep,
227  *   bibformat={tabular,ibidem,numbered}
228  */
229 vector<string> split_options(string const & input)
230 {
231         vector<string> options;
232         string option;
233         Parser p(input);
234         while (p.good()) {
235                 Token const & t = p.get_token();
236                 if (t.asInput() == ",") {
237                         options.push_back(trimSpaceAndEol(option));
238                         option.erase();
239                 } else if (t.asInput() == "=") {
240                         option += '=';
241                         p.skip_spaces(true);
242                         if (p.next_token().asInput() == "{")
243                                 option += '{' + p.getArg('{', '}') + '}';
244                 } else if (t.cat() != catSpace)
245                         option += t.asInput();
246         }
247
248         if (!option.empty())
249                 options.push_back(trimSpaceAndEol(option));
250
251         return options;
252 }
253
254
255 /*!
256  * Retrieve a keyval option "name={value with=sign}" named \p name from
257  * \p options and return the value.
258  * The found option is also removed from \p options.
259  */
260 string process_keyval_opt(vector<string> & options, string name)
261 {
262         for (size_t i = 0; i < options.size(); ++i) {
263                 vector<string> option;
264                 split(options[i], option, '=');
265                 if (option.size() < 2)
266                         continue;
267                 if (option[0] == name) {
268                         options.erase(options.begin() + i);
269                         option.erase(option.begin());
270                         return join(option, "=");
271                 }
272         }
273         return "";
274 }
275
276 } // anonymous namespace
277
278
279 bool Preamble::indentParagraphs() const
280 {
281         return h_paragraph_separation == "indent";
282 }
283
284
285 bool Preamble::isPackageUsed(string const & package) const
286 {
287         return used_packages.find(package) != used_packages.end();
288 }
289
290
291 vector<string> Preamble::getPackageOptions(string const & package) const
292 {
293         map<string, vector<string> >::const_iterator it = used_packages.find(package);
294         if (it != used_packages.end())
295                 return it->second;
296         return vector<string>();
297 }
298
299
300 void Preamble::registerAutomaticallyLoadedPackage(std::string const & package)
301 {
302         auto_packages.insert(package);
303 }
304
305
306 void Preamble::addModule(string const & module)
307 {
308         used_modules.push_back(module);
309 }
310
311
312 void Preamble::suppressDate(bool suppress)
313 {
314         if (suppress)
315                 h_suppress_date = "true";
316         else
317                 h_suppress_date = "false";
318 }
319
320
321 void Preamble::registerAuthor(std::string const & name)
322 {
323         Author author(from_utf8(name), empty_docstring());
324         author.setUsed(true);
325         authors_.record(author);
326         h_tracking_changes = "true";
327         h_output_changes = "true";
328 }
329
330
331 Author const & Preamble::getAuthor(std::string const & name) const
332 {
333         Author author(from_utf8(name), empty_docstring());
334         for (AuthorList::Authors::const_iterator it = authors_.begin();
335              it != authors_.end(); it++)
336                 if (*it == author)
337                         return *it;
338         static Author const dummy;
339         return dummy;
340 }
341
342
343 void Preamble::add_package(string const & name, vector<string> & options)
344 {
345         // every package inherits the global options
346         if (used_packages.find(name) == used_packages.end())
347                 used_packages[name] = split_options(h_options);
348
349         vector<string> & v = used_packages[name];
350         v.insert(v.end(), options.begin(), options.end());
351         if (name == "jurabib") {
352                 // Don't output the order argument (see the cite command
353                 // handling code in text.cpp).
354                 vector<string>::iterator end =
355                         remove(options.begin(), options.end(), "natbiborder");
356                 end = remove(options.begin(), end, "jurabiborder");
357                 options.erase(end, options.end());
358         }
359 }
360
361
362 namespace {
363
364 // Given is a string like "scaled=0.9", return 0.9 * 100
365 string const scale_as_percentage(string const & scale)
366 {
367         string::size_type pos = scale.find('=');
368         if (pos != string::npos) {
369                 string value = scale.substr(pos + 1);
370                 if (isStrDbl(value))
371                         return convert<string>(100 * convert<double>(value));
372         }
373         // If the input string didn't match our expectations.
374         // return the default value "100"
375         return "100";
376 }
377
378
379 string remove_braces(string const & value)
380 {
381         if (value.empty())
382                 return value;
383         if (value[0] == '{' && value[value.length()-1] == '}')
384                 return value.substr(1, value.length()-2);
385         return value;
386 }
387
388 } // anonymous namespace
389
390
391 Preamble::Preamble() : one_language(true)
392 {
393         //h_backgroundcolor;
394         //h_boxbgcolor;
395         h_cite_engine             = "basic";
396         h_defskip                 = "medskip";
397         //h_float_placement;
398         //h_fontcolor;
399         h_fontencoding            = "default";
400         h_font_roman              = "default";
401         h_font_sans               = "default";
402         h_font_typewriter         = "default";
403         h_font_default_family     = "default";
404         h_font_sc                 = "false";
405         h_font_osf                = "false";
406         h_font_sf_scale           = "100";
407         h_font_tt_scale           = "100";
408         h_graphics                = "default";
409         h_html_be_strict          = "false";
410         h_html_css_as_file        = "0";
411         h_html_math_output        = "0";
412         h_inputencoding           = "auto";
413         h_justification           = "true";
414         h_language                = "english";
415         h_language_package        = "none";
416         //h_listings_params;
417         //h_margins;
418         //h_notefontcolor;
419         //h_options;
420         h_output_changes          = "false";
421         h_papercolumns            = "1";
422         h_paperfontsize           = "default";
423         h_paperorientation        = "portrait";
424         h_paperpagestyle          = "default";
425         //h_papersides;
426         h_papersize               = "default";
427         h_paragraph_indentation   = "default";
428         h_paragraph_separation    = "indent";
429         //h_pdf_title;
430         //h_pdf_author;
431         //h_pdf_subject;
432         //h_pdf_keywords;
433         h_pdf_bookmarks           = "1";
434         h_pdf_bookmarksnumbered   = "0";
435         h_pdf_bookmarksopen       = "0";
436         h_pdf_bookmarksopenlevel  = "1";
437         h_pdf_breaklinks          = "0";
438         h_pdf_pdfborder           = "0";
439         h_pdf_colorlinks          = "0";
440         h_pdf_backref             = "section";
441         h_pdf_pdfusetitle         = "1";
442         //h_pdf_pagemode;
443         //h_pdf_quoted_options;
444         h_quotes_language         = "english";
445         h_secnumdepth             = "3";
446         h_spacing                 = "single";
447         h_suppress_date           = "false";
448         h_textclass               = "article";
449         h_tocdepth                = "3";
450         h_tracking_changes        = "false";
451         h_use_bibtopic            = "false";
452         h_use_indices             = "false";
453         h_use_geometry            = "false";
454         h_use_amsmath             = "1";
455         h_use_default_options     = "false";
456         h_use_esint               = "1";
457         h_use_hyperref            = "0";
458         h_use_mhchem              = "0";
459         h_use_mathdots            = "0";
460         h_use_refstyle            = "0";
461         h_use_undertilde          = "0";
462 }
463
464
465 void Preamble::handle_hyperref(vector<string> & options)
466 {
467         // FIXME swallow inputencoding changes that might surround the
468         //       hyperref setup if it was written by LyX
469         h_use_hyperref = "1";
470         // swallow "unicode=true", since LyX does always write that
471         vector<string>::iterator it =
472                 find(options.begin(), options.end(), "unicode=true");
473         if (it != options.end())
474                 options.erase(it);
475         it = find(options.begin(), options.end(), "pdfusetitle");
476         if (it != options.end()) {
477                 h_pdf_pdfusetitle = "1";
478                 options.erase(it);
479         }
480         string bookmarks = process_keyval_opt(options, "bookmarks");
481         if (bookmarks == "true")
482                 h_pdf_bookmarks = "1";
483         else if (bookmarks == "false")
484                 h_pdf_bookmarks = "0";
485         if (h_pdf_bookmarks == "1") {
486                 string bookmarksnumbered =
487                         process_keyval_opt(options, "bookmarksnumbered");
488                 if (bookmarksnumbered == "true")
489                         h_pdf_bookmarksnumbered = "1";
490                 else if (bookmarksnumbered == "false")
491                         h_pdf_bookmarksnumbered = "0";
492                 string bookmarksopen =
493                         process_keyval_opt(options, "bookmarksopen");
494                 if (bookmarksopen == "true")
495                         h_pdf_bookmarksopen = "1";
496                 else if (bookmarksopen == "false")
497                         h_pdf_bookmarksopen = "0";
498                 if (h_pdf_bookmarksopen == "1") {
499                         string bookmarksopenlevel =
500                                 process_keyval_opt(options, "bookmarksopenlevel");
501                         if (!bookmarksopenlevel.empty())
502                                 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
503                 }
504         }
505         string breaklinks = process_keyval_opt(options, "breaklinks");
506         if (breaklinks == "true")
507                 h_pdf_breaklinks = "1";
508         else if (breaklinks == "false")
509                 h_pdf_breaklinks = "0";
510         string pdfborder = process_keyval_opt(options, "pdfborder");
511         if (pdfborder == "{0 0 0}")
512                 h_pdf_pdfborder = "1";
513         else if (pdfborder == "{0 0 1}")
514                 h_pdf_pdfborder = "0";
515         string backref = process_keyval_opt(options, "backref");
516         if (!backref.empty())
517                 h_pdf_backref = backref;
518         string colorlinks = process_keyval_opt(options, "colorlinks");
519         if (colorlinks == "true")
520                 h_pdf_colorlinks = "1";
521         else if (colorlinks == "false")
522                 h_pdf_colorlinks = "0";
523         string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
524         if (!pdfpagemode.empty())
525                 h_pdf_pagemode = pdfpagemode;
526         string pdftitle = process_keyval_opt(options, "pdftitle");
527         if (!pdftitle.empty()) {
528                 h_pdf_title = remove_braces(pdftitle);
529         }
530         string pdfauthor = process_keyval_opt(options, "pdfauthor");
531         if (!pdfauthor.empty()) {
532                 h_pdf_author = remove_braces(pdfauthor);
533         }
534         string pdfsubject = process_keyval_opt(options, "pdfsubject");
535         if (!pdfsubject.empty())
536                 h_pdf_subject = remove_braces(pdfsubject);
537         string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
538         if (!pdfkeywords.empty())
539                 h_pdf_keywords = remove_braces(pdfkeywords);
540         if (!options.empty()) {
541                 if (!h_pdf_quoted_options.empty())
542                         h_pdf_quoted_options += ',';
543                 h_pdf_quoted_options += join(options, ",");
544                 options.clear();
545         }
546 }
547
548
549 void Preamble::handle_geometry(vector<string> & options)
550 {
551         h_use_geometry = "true";
552         vector<string>::iterator it;
553         // paper orientation
554         if ((it = find(options.begin(), options.end(), "landscape")) != options.end()) {
555                 h_paperorientation = "landscape";
556                 options.erase(it);
557         }
558         // paper size
559         // keyval version: "paper=letter"
560         string paper = process_keyval_opt(options, "paper");
561         if (!paper.empty())
562                 h_papersize = paper + "paper";
563         // alternative version: "letterpaper"
564         handle_opt(options, known_paper_sizes, h_papersize);
565         delete_opt(options, known_paper_sizes);
566         // page margins
567         char const * const * margin = known_paper_margins;
568         for (; *margin; ++margin) {
569                 string value = process_keyval_opt(options, *margin);
570                 if (!value.empty()) {
571                         int k = margin - known_paper_margins;
572                         string name = known_coded_paper_margins[k];
573                         h_margins += '\\' + name + ' ' + value + '\n';
574                 }
575         }
576 }
577
578
579 void Preamble::handle_package(Parser &p, string const & name,
580                               string const & opts, bool in_lyx_preamble)
581 {
582         vector<string> options = split_options(opts);
583         add_package(name, options);
584         string scale;
585
586         if (is_known(name, known_xetex_packages))
587                 xetex = true;
588
589         // roman fonts
590         if (is_known(name, known_roman_fonts)) {
591                 h_font_roman = name;
592                 p.skip_spaces();
593         }
594
595         if (name == "fourier") {
596                 h_font_roman = "utopia";
597                 // when font uses real small capitals
598                 if (opts == "expert")
599                         h_font_sc = "true";
600         }
601
602         if (name == "mathpazo")
603                 h_font_roman = "palatino";
604
605         if (name == "mathptmx")
606                 h_font_roman = "times";
607
608         // sansserif fonts
609         if (is_known(name, known_sans_fonts)) {
610                 h_font_sans = name;
611                 if (!opts.empty()) {
612                         scale = opts;
613                         h_font_sf_scale = scale_as_percentage(scale);
614                 }
615         }
616
617         // typewriter fonts
618         if (is_known(name, known_typewriter_fonts)) {
619                 // fourier can be set as roman font _only_
620                 // fourier as typewriter is handled in handling of \ttdefault
621                 if (name != "fourier") {
622                         h_font_typewriter = name;
623                         if (!opts.empty()) {
624                                 scale = opts;
625                                 h_font_tt_scale = scale_as_percentage(scale);
626                         }
627                 }
628         }
629
630         // font uses old-style figure
631         if (name == "eco")
632                 h_font_osf = "true";
633
634         // after the detection and handling of special cases, we can remove the
635         // fonts, otherwise they would appear in the preamble, see bug #7856
636         if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
637                 ||      is_known(name, known_typewriter_fonts))
638                 ;
639
640         else if (name == "amsmath" || name == "amssymb")
641                 h_use_amsmath = "2";
642
643         else if (name == "esint")
644                 h_use_esint = "2";
645
646         else if (name == "mhchem")
647                 h_use_mhchem = "2";
648
649         else if (name == "mathdots")
650                 h_use_mathdots = "2";
651
652         else if (name == "undertilde")
653                 h_use_undertilde = "2";
654
655         else if (name == "babel") {
656                 h_language_package = "default";
657                 // One might think we would have to do nothing if babel is loaded
658                 // without any options to prevent pollution of the preamble with this
659                 // babel call in every roundtrip.
660                 // But the user could have defined babel-specific things afterwards. So
661                 // we need to keep it in the preamble to prevent cases like bug #7861.
662                 if (!opts.empty()) {
663                         // check if more than one option was used - used later for inputenc
664                         if (options.begin() != options.end() - 1)
665                                 one_language = false;
666                         // babel takes the last language of the option of its \usepackage
667                         // call as document language. If there is no such language option, the
668                         // last language in the documentclass options is used.
669                         handle_opt(options, known_languages, h_language);
670                         // If babel is called with options, LyX puts them by default into the
671                         // document class options. This works for most languages, except
672                         // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
673                         // perhaps in future others.
674                         // Therefore keep the babel call as it is as the user might have
675                         // reasons for it.
676                         h_preamble << "\\usepackage[" << opts << "]{babel}\n";
677                         delete_opt(options, known_languages);
678                 }
679                 else
680                         h_preamble << "\\usepackage{babel}\n";                                          
681         }
682
683         else if (name == "fontenc") {
684                 h_fontencoding = getStringFromVector(options, ",");
685                 /* We could do the following for better round trip support,
686                  * but this makes the document less portable, so I skip it:
687                 if (h_fontencoding == lyxrc.fontenc)
688                         h_fontencoding = "global";
689                  */
690                 options.clear();
691         }
692
693         else if (name == "inputenc" || name == "luainputenc") {
694                 // h_inputencoding is only set when there is not more than one
695                 // inputenc option because otherwise h_inputencoding must be
696                 // set to "auto" (the default encoding of the document language)
697                 // Therefore check for the "," character.
698                 // It is also only set when there is not more than one babel
699                 // language option.
700                 if (opts.find(",") == string::npos && one_language == true)
701                         h_inputencoding = opts;
702                 if (!options.empty())
703                         p.setEncoding(options.back());
704                 options.clear();
705         }
706
707         else if (is_known(name, known_old_language_packages)) {
708                 // known language packages from the times before babel
709                 // if they are found and not also babel, they will be used as
710                 // custom language package
711                 h_language_package = "\\usepackage{" + name + "}";
712         }
713
714         else if (name == "prettyref")
715                 ; // ignore this FIXME: Use the package separator mechanism instead
716
717         else if (name == "varioref")
718                 ; // ignore this FIXME: Use the package separator mechanism instead
719
720         else if (name == "verbatim")
721                 ; // ignore this FIXME: Use the package separator mechanism instead
722
723         else if (name == "textcomp")
724                 ; // ignore this FIXME: Use the package separator mechanism instead
725
726         else if (name == "pdfpages")
727                 ; // ignore this
728
729         else if (name == "lyxskak") {
730                 // ignore this and its options
731                 if (!options.empty())
732                         options.clear();
733         }
734
735         else if (name == "array" || name == "booktabs" || name == "calc" ||
736                      name == "color" || name == "hhline" || name == "ifthen" ||
737                      name == "float" || name == "longtable" || name == "makeidx" ||
738                      name == "multirow" || name == "nomencl" || name == "setspace" ||
739                      name == "splitidx" || name == "subscript" || name == "ulem" ||
740                      name == "url") {
741                 if (!in_lyx_preamble)
742                         h_preamble << package_beg_sep << name
743                                    << package_mid_sep << "\\usepackage{"
744                                    << name << '}' << package_end_sep;
745         }
746
747         else if (name == "graphicx")
748                 ; // ignore this FIXME: Use the package separator mechanism instead
749
750         else if (name == "geometry")
751                 handle_geometry(options);
752
753         else if (name == "rotfloat")
754                 ; // ignore this FIXME: Use the package separator mechanism instead
755
756         else if (name == "wrapfig")
757                 ; // ignore this FIXME: Use the package separator mechanism instead
758
759         else if (name == "subfig")
760                 ; // ignore this FIXME: Use the package separator mechanism instead
761
762         else if (is_known(name, known_languages))
763                 h_language = name;
764
765         else if (name == "natbib") {
766                 h_cite_engine = "natbib_authoryear";
767                 vector<string>::iterator it =
768                         find(options.begin(), options.end(), "authoryear");
769                 if (it != options.end())
770                         options.erase(it);
771                 else {
772                         it = find(options.begin(), options.end(), "numbers");
773                         if (it != options.end()) {
774                                 h_cite_engine = "natbib_numerical";
775                                 options.erase(it);
776                         }
777                 }
778         }
779
780         else if (name == "jurabib")
781                 h_cite_engine = "jurabib";
782
783         else if (name == "hyperref")
784                 handle_hyperref(options);
785
786         else if (!in_lyx_preamble) {
787                 if (options.empty())
788                         h_preamble << "\\usepackage{" << name << "}";
789                 else {
790                         h_preamble << "\\usepackage[" << opts << "]{" 
791                                    << name << "}";
792                         options.clear();
793                 }
794         }
795
796         // We need to do something with the options...
797         if (!options.empty())
798                 cerr << "Ignoring options '" << join(options, ",")
799                      << "' of package " << name << '.' << endl;
800
801         // remove the whitespace
802         p.skip_spaces();
803 }
804
805
806 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
807 {
808         while (p.good()) {
809                 Token t = p.get_token();
810                 if (t.cat() == catEscape &&
811                     is_known(t.cs(), known_if_commands))
812                         handle_if(p, in_lyx_preamble);
813                 else {
814                         if (!in_lyx_preamble)
815                                 h_preamble << t.asInput();
816                         if (t.cat() == catEscape && t.cs() == "fi")
817                                 return;
818                 }
819         }
820 }
821
822
823 bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
824 {
825         // translate from babel to LyX names
826         h_language = babel2lyx(h_language);
827
828         // set the quote language
829         // LyX only knows the following quotes languages:
830         // english, swedish, german, polish, french and danish
831         // (quotes for "japanese" and "chinese-traditional" are missing because
832         //  they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
833         // conversion list taken from
834         // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
835         // (quotes for kazakh and interlingua are unknown)
836         // danish
837         if (h_language == "danish")
838                 h_quotes_language = "danish";
839         // french
840         else if (is_known(h_language, known_french_quotes_languages))
841                 h_quotes_language = "french";
842         // german
843         else if (is_known(h_language, known_german_quotes_languages))
844                 h_quotes_language = "german";
845         // polish
846         else if (is_known(h_language, known_polish_quotes_languages))
847                 h_quotes_language = "polish";
848         // swedish
849         else if (is_known(h_language, known_swedish_quotes_languages))
850                 h_quotes_language = "swedish";
851         //english
852         else if (is_known(h_language, known_english_quotes_languages))
853                 h_quotes_language = "english";
854
855         if (contains(h_float_placement, "H"))
856                 registerAutomaticallyLoadedPackage("float");
857         if (h_spacing != "single" && h_spacing != "default")
858                 registerAutomaticallyLoadedPackage("setspace");
859
860         // output the LyX file settings
861         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
862            << "\\lyxformat " << LYX_FORMAT << '\n'
863            << "\\begin_document\n"
864            << "\\begin_header\n"
865            << "\\textclass " << h_textclass << "\n";
866         string const raw = subdoc ? empty_string() : h_preamble.str();
867         if (!raw.empty()) {
868                 os << "\\begin_preamble\n";
869                 for (string::size_type i = 0; i < raw.size(); ++i) {
870                         if (raw[i] == package_beg_sep) {
871                                 // Here follows some package loading code that
872                                 // must be skipped if the package is loaded
873                                 // automatically.
874                                 string::size_type j = raw.find(package_mid_sep, i);
875                                 if (j == string::npos)
876                                         return false;
877                                 string::size_type k = raw.find(package_end_sep, j);
878                                 if (k == string::npos)
879                                         return false;
880                                 string const package = raw.substr(i + 1, j - i - 1);
881                                 string const replacement = raw.substr(j + 1, k - j - 1);
882                                 if (auto_packages.find(package) == auto_packages.end())
883                                         os << replacement;
884                                 i = k;
885                         } else
886                                 os.put(raw[i]);
887                 }
888                 os << "\n\\end_preamble\n";
889         }
890         if (!h_options.empty())
891                 os << "\\options " << h_options << "\n";
892         os << "\\use_default_options " << h_use_default_options << "\n";
893         if (!used_modules.empty()) {
894                 os << "\\begin_modules\n";
895                 vector<string>::const_iterator const end = used_modules.end();
896                 vector<string>::const_iterator it = used_modules.begin();
897                 for (; it != end; it++)
898                         os << *it << '\n';
899                 os << "\\end_modules\n";
900         }
901         os << "\\language " << h_language << "\n"
902            << "\\language_package " << h_language_package << "\n"
903            << "\\inputencoding " << h_inputencoding << "\n"
904            << "\\fontencoding " << h_fontencoding << "\n"
905            << "\\font_roman " << h_font_roman << "\n"
906            << "\\font_sans " << h_font_sans << "\n"
907            << "\\font_typewriter " << h_font_typewriter << "\n"
908            << "\\font_default_family " << h_font_default_family << "\n"
909            << "\\font_sc " << h_font_sc << "\n"
910            << "\\font_osf " << h_font_osf << "\n"
911            << "\\font_sf_scale " << h_font_sf_scale << "\n"
912            << "\\font_tt_scale " << h_font_tt_scale << "\n"
913            << "\\graphics " << h_graphics << "\n";
914         if (!h_float_placement.empty())
915                 os << "\\float_placement " << h_float_placement << "\n";
916         os << "\\paperfontsize " << h_paperfontsize << "\n"
917            << "\\spacing " << h_spacing << "\n"
918            << "\\use_hyperref " << h_use_hyperref << '\n';
919         if (h_use_hyperref == "1") {
920                 if (!h_pdf_title.empty())
921                         os << "\\pdf_title \"" << h_pdf_title << "\"\n";
922                 if (!h_pdf_author.empty())
923                         os << "\\pdf_author \"" << h_pdf_author << "\"\n";
924                 if (!h_pdf_subject.empty())
925                         os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
926                 if (!h_pdf_keywords.empty())
927                         os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
928                 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
929                       "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
930                       "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
931                       "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
932                       "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
933                       "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
934                       "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
935                       "\\pdf_backref " << h_pdf_backref << "\n"
936                       "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
937                 if (!h_pdf_pagemode.empty())
938                         os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
939                 if (!h_pdf_quoted_options.empty())
940                         os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
941         }
942         os << "\\papersize " << h_papersize << "\n"
943            << "\\use_geometry " << h_use_geometry << "\n"
944            << "\\use_amsmath " << h_use_amsmath << "\n"
945            << "\\use_esint " << h_use_esint << "\n"
946            << "\\use_mhchem " << h_use_mhchem << "\n"
947            << "\\use_mathdots " << h_use_mathdots << "\n"
948            << "\\use_undertilde " << h_use_undertilde << "\n"
949            << "\\cite_engine " << h_cite_engine << "\n"
950            << "\\use_bibtopic " << h_use_bibtopic << "\n"
951            << "\\use_indices " << h_use_indices << "\n"
952            << "\\paperorientation " << h_paperorientation << '\n'
953            << "\\suppress_date " << h_suppress_date << '\n'
954            << "\\use_refstyle " << h_use_refstyle << '\n';
955         if (!h_fontcolor.empty())
956                 os << "\\fontcolor " << h_fontcolor << '\n';
957         if (!h_notefontcolor.empty())
958                 os << "\\notefontcolor " << h_notefontcolor << '\n';
959         if (!h_backgroundcolor.empty())
960                 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
961         if (!h_boxbgcolor.empty())
962                 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
963         os << h_margins
964            << "\\secnumdepth " << h_secnumdepth << "\n"
965            << "\\tocdepth " << h_tocdepth << "\n"
966            << "\\paragraph_separation " << h_paragraph_separation << "\n";
967         if (h_paragraph_separation == "skip")
968                 os << "\\defskip " << h_defskip << "\n";
969         else
970                 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
971         os << "\\quotes_language " << h_quotes_language << "\n"
972            << "\\papercolumns " << h_papercolumns << "\n"
973            << "\\papersides " << h_papersides << "\n"
974            << "\\paperpagestyle " << h_paperpagestyle << "\n";
975         if (!h_listings_params.empty())
976                 os << "\\listings_params " << h_listings_params << "\n";
977         os << "\\tracking_changes " << h_tracking_changes << "\n"
978            << "\\output_changes " << h_output_changes << "\n"
979            << "\\html_math_output " << h_html_math_output << "\n"
980            << "\\html_css_as_file " << h_html_css_as_file << "\n"
981            << "\\html_be_strict " << h_html_be_strict << "\n"
982            << authors_
983            << "\\end_header\n\n"
984            << "\\begin_body\n";
985         return true;
986 }
987
988
989 void Preamble::parse(Parser & p, string const & forceclass,
990                      TeX2LyXDocClass & tc)
991 {
992         // initialize fixed types
993         special_columns['D'] = 3;
994         bool is_full_document = false;
995         bool is_lyx_file = false;
996         bool in_lyx_preamble = false;
997
998         // determine whether this is a full document or a fragment for inclusion
999         while (p.good()) {
1000                 Token const & t = p.get_token();
1001
1002                 if (t.cat() == catEscape && t.cs() == "documentclass") {
1003                         is_full_document = true;
1004                         break;
1005                 }
1006         }
1007         p.reset();
1008
1009         while (is_full_document && p.good()) {
1010                 Token const & t = p.get_token();
1011
1012 #ifdef FILEDEBUG
1013                 cerr << "t: " << t << "\n";
1014 #endif
1015
1016                 //
1017                 // cat codes
1018                 //
1019                 if (!in_lyx_preamble &&
1020                     (t.cat() == catLetter ||
1021                      t.cat() == catSuper ||
1022                      t.cat() == catSub ||
1023                      t.cat() == catOther ||
1024                      t.cat() == catMath ||
1025                      t.cat() == catActive ||
1026                      t.cat() == catBegin ||
1027                      t.cat() == catEnd ||
1028                      t.cat() == catAlign ||
1029                      t.cat() == catParameter))
1030                         h_preamble << t.cs();
1031
1032                 else if (!in_lyx_preamble && 
1033                          (t.cat() == catSpace || t.cat() == catNewline))
1034                         h_preamble << t.asInput();
1035
1036                 else if (t.cat() == catComment) {
1037                         static regex const islyxfile("%% LyX .* created this file");
1038                         static regex const usercommands("User specified LaTeX commands");
1039
1040                         string const comment = t.asInput();
1041
1042                         // magically switch encoding default if it looks like XeLaTeX
1043                         static string const magicXeLaTeX =
1044                                 "% This document must be compiled with XeLaTeX ";
1045                         if (comment.size() > magicXeLaTeX.size() 
1046                                   && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1047                                   && h_inputencoding == "auto") {
1048                                 cerr << "XeLaTeX comment found, switching to UTF8\n";
1049                                 h_inputencoding = "utf8";
1050                         }
1051                         smatch sub;
1052                         if (regex_search(comment, sub, islyxfile)) {
1053                                 is_lyx_file = true;
1054                                 in_lyx_preamble = true;
1055                         } else if (is_lyx_file
1056                                    && regex_search(comment, sub, usercommands))
1057                                 in_lyx_preamble = false;
1058                         else if (!in_lyx_preamble)
1059                                 h_preamble << t.asInput();
1060                 }
1061
1062                 else if (t.cs() == "pagestyle")
1063                         h_paperpagestyle = p.verbatim_item();
1064
1065                 else if (t.cs() == "date") {
1066                         string argument = p.getArg('{', '}');
1067                         if (argument.empty())
1068                                 h_suppress_date = "true";
1069                         else
1070                                 h_preamble << t.asInput() << '{' << argument << '}';
1071                 }
1072
1073                 else if (t.cs() == "color") {
1074                         string const space =
1075                                 (p.hasOpt() ? p.getOpt() : string());
1076                         string argument = p.getArg('{', '}');
1077                         // check the case that a standard color is used
1078                         if (space.empty() && is_known(argument, known_basic_colors)) {
1079                                 h_fontcolor = rgbcolor2code(argument);
1080                                 preamble.registerAutomaticallyLoadedPackage("color");
1081                         } else if (space.empty() && argument == "document_fontcolor")
1082                                 preamble.registerAutomaticallyLoadedPackage("color");
1083                         // check the case that LyX's document_fontcolor is defined
1084                         // but not used for \color
1085                         else {
1086                                 h_preamble << t.asInput();
1087                                 if (!space.empty())
1088                                         h_preamble << space;
1089                                 h_preamble << '{' << argument << '}';
1090                                 // the color might already be set because \definecolor
1091                                 // is parsed before this
1092                                 h_fontcolor = "";
1093                         }
1094                 }
1095
1096                 else if (t.cs() == "pagecolor") {
1097                         string argument = p.getArg('{', '}');
1098                         // check the case that a standard color is used
1099                         if (is_known(argument, known_basic_colors)) {
1100                                 h_backgroundcolor = rgbcolor2code(argument);
1101                         } else if (argument == "page_backgroundcolor")
1102                                 preamble.registerAutomaticallyLoadedPackage("color");
1103                         // check the case that LyX's page_backgroundcolor is defined
1104                         // but not used for \pagecolor
1105                         else {
1106                                 h_preamble << t.asInput() << '{' << argument << '}';
1107                                 // the color might already be set because \definecolor
1108                                 // is parsed before this
1109                                 h_backgroundcolor = "";
1110                         }
1111                 }
1112
1113                 else if (t.cs() == "makeatletter") {
1114                         // LyX takes care of this
1115                         p.setCatCode('@', catLetter);
1116                 }
1117
1118                 else if (t.cs() == "makeatother") {
1119                         // LyX takes care of this
1120                         p.setCatCode('@', catOther);
1121                 }
1122
1123                 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1124                       || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1125                       || t.cs() == "providecommand" || t.cs() == "providecommandx"
1126                                 || t.cs() == "DeclareRobustCommand"
1127                       || t.cs() == "DeclareRobustCommandx"
1128                                 || t.cs() == "ProvideTextCommandDefault"
1129                                 || t.cs() == "DeclareMathAccent") {
1130                         bool star = false;
1131                         if (p.next_token().character() == '*') {
1132                                 p.get_token();
1133                                 star = true;
1134                         }
1135                         string const name = p.verbatim_item();
1136                         string const opt1 = p.getFullOpt();
1137                         string const opt2 = p.getFullOpt();
1138                         string const body = p.verbatim_item();
1139                         // font settings
1140                         if (name == "\\rmdefault")
1141                                 if (is_known(body, known_roman_fonts))
1142                                         h_font_roman = body;
1143                         if (name == "\\sfdefault")
1144                                 if (is_known(body, known_sans_fonts))
1145                                         h_font_sans = body;
1146                         if (name == "\\ttdefault")
1147                                 if (is_known(body, known_typewriter_fonts))
1148                                         h_font_typewriter = body;
1149                         if (name == "\\familydefault") {
1150                                 string family = body;
1151                                 // remove leading "\"
1152                                 h_font_default_family = family.erase(0,1);
1153                         }
1154
1155                         // remove the lyxdot definition that is re-added by LyX
1156                         // if necessary
1157                         if (name == "\\lyxdot")
1158                                 in_lyx_preamble = true;
1159
1160                         // Add the command to the known commands
1161                         add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1162
1163                         // only non-lyxspecific stuff
1164                         if (!in_lyx_preamble) {
1165                                 ostringstream ss;
1166                                 ss << '\\' << t.cs();
1167                                 if (star)
1168                                         ss << '*';
1169                                 ss << '{' << name << '}' << opt1 << opt2
1170                                    << '{' << body << "}";
1171                                 h_preamble << ss.str();
1172 /*
1173                                 ostream & out = in_preamble ? h_preamble : os;
1174                                 out << "\\" << t.cs() << "{" << name << "}"
1175                                     << opts << "{" << body << "}";
1176 */
1177                         }
1178                 }
1179
1180                 else if (t.cs() == "documentclass") {
1181                         vector<string>::iterator it;
1182                         vector<string> opts = split_options(p.getArg('[', ']'));
1183                         handle_opt(opts, known_fontsizes, h_paperfontsize);
1184                         delete_opt(opts, known_fontsizes);
1185                         // delete "pt" at the end
1186                         string::size_type i = h_paperfontsize.find("pt");
1187                         if (i != string::npos)
1188                                 h_paperfontsize.erase(i);
1189                         // The documentclass options are always parsed before the options
1190                         // of the babel call so that a language cannot overwrite the babel
1191                         // options.
1192                         handle_opt(opts, known_languages, h_language);
1193                         delete_opt(opts, known_languages);
1194
1195                         // paper orientation
1196                         if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1197                                 h_paperorientation = "landscape";
1198                                 opts.erase(it);
1199                         }
1200                         // paper sides
1201                         if ((it = find(opts.begin(), opts.end(), "oneside"))
1202                                  != opts.end()) {
1203                                 h_papersides = "1";
1204                                 opts.erase(it);
1205                         }
1206                         if ((it = find(opts.begin(), opts.end(), "twoside"))
1207                                  != opts.end()) {
1208                                 h_papersides = "2";
1209                                 opts.erase(it);
1210                         }
1211                         // paper columns
1212                         if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1213                                  != opts.end()) {
1214                                 h_papercolumns = "1";
1215                                 opts.erase(it);
1216                         }
1217                         if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1218                                  != opts.end()) {
1219                                 h_papercolumns = "2";
1220                                 opts.erase(it);
1221                         }
1222                         // paper sizes
1223                         // some size options are known to any document classes, other sizes
1224                         // are handled by the \geometry command of the geometry package
1225                         handle_opt(opts, known_class_paper_sizes, h_papersize);
1226                         delete_opt(opts, known_class_paper_sizes);
1227                         // the remaining options
1228                         h_options = join(opts, ",");
1229                         // FIXME This does not work for classes that have a
1230                         //       different name in LyX than in LaTeX
1231                         h_textclass = p.getArg('{', '}');
1232                 }
1233
1234                 else if (t.cs() == "usepackage") {
1235                         string const options = p.getArg('[', ']');
1236                         string const name = p.getArg('{', '}');
1237                         vector<string> vecnames;
1238                         split(name, vecnames, ',');
1239                         vector<string>::const_iterator it  = vecnames.begin();
1240                         vector<string>::const_iterator end = vecnames.end();
1241                         for (; it != end; ++it)
1242                                 handle_package(p, trimSpaceAndEol(*it), options, 
1243                                                in_lyx_preamble);
1244                 }
1245
1246                 else if (t.cs() == "inputencoding") {
1247                         string const encoding = p.getArg('{','}');
1248                         h_inputencoding = encoding;
1249                         p.setEncoding(encoding);
1250                 }
1251
1252                 else if (t.cs() == "newenvironment") {
1253                         string const name = p.getArg('{', '}');
1254                         string const opt1 = p.getFullOpt();
1255                         string const opt2 = p.getFullOpt();
1256                         string const beg = p.verbatim_item();
1257                         string const end = p.verbatim_item();
1258                         if (!in_lyx_preamble) {
1259                                 h_preamble << "\\newenvironment{" << name
1260                                            << '}' << opt1 << opt2 << '{'
1261                                            << beg << "}{" << end << '}';
1262                         }
1263                         add_known_environment(name, opt1, !opt2.empty(),
1264                                               from_utf8(beg), from_utf8(end));
1265
1266                 }
1267
1268                 else if (t.cs() == "def") {
1269                         string name = p.get_token().cs();
1270                         while (p.next_token().cat() != catBegin)
1271                                 name += p.get_token().cs();
1272                         if (!in_lyx_preamble)
1273                                 h_preamble << "\\def\\" << name << '{'
1274                                            << p.verbatim_item() << "}";
1275                 }
1276
1277                 else if (t.cs() == "newcolumntype") {
1278                         string const name = p.getArg('{', '}');
1279                         trimSpaceAndEol(name);
1280                         int nargs = 0;
1281                         string opts = p.getOpt();
1282                         if (!opts.empty()) {
1283                                 istringstream is(string(opts, 1));
1284                                 is >> nargs;
1285                         }
1286                         special_columns[name[0]] = nargs;
1287                         h_preamble << "\\newcolumntype{" << name << "}";
1288                         if (nargs)
1289                                 h_preamble << "[" << nargs << "]";
1290                         h_preamble << "{" << p.verbatim_item() << "}";
1291                 }
1292
1293                 else if (t.cs() == "setcounter") {
1294                         string const name = p.getArg('{', '}');
1295                         string const content = p.getArg('{', '}');
1296                         if (name == "secnumdepth")
1297                                 h_secnumdepth = content;
1298                         else if (name == "tocdepth")
1299                                 h_tocdepth = content;
1300                         else
1301                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1302                 }
1303
1304                 else if (t.cs() == "setlength") {
1305                         string const name = p.verbatim_item();
1306                         string const content = p.verbatim_item();
1307                         // the paragraphs are only not indented when \parindent is set to zero
1308                         if (name == "\\parindent" && content != "") {
1309                                 if (content[0] == '0')
1310                                         h_paragraph_separation = "skip";
1311                                 else
1312                                         h_paragraph_indentation = translate_len(content);
1313                         } else if (name == "\\parskip") {
1314                                 if (content == "\\smallskipamount")
1315                                         h_defskip = "smallskip";
1316                                 else if (content == "\\medskipamount")
1317                                         h_defskip = "medskip";
1318                                 else if (content == "\\bigskipamount")
1319                                         h_defskip = "bigskip";
1320                                 else
1321                                         h_defskip = content;
1322                         } else
1323                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1324                 }
1325
1326                 else if (t.cs() == "onehalfspacing")
1327                         h_spacing = "onehalf";
1328
1329                 else if (t.cs() == "doublespacing")
1330                         h_spacing = "double";
1331
1332                 else if (t.cs() == "setstretch")
1333                         h_spacing = "other " + p.verbatim_item();
1334
1335                 else if (t.cs() == "begin") {
1336                         string const name = p.getArg('{', '}');
1337                         if (name == "document")
1338                                 break;
1339                         h_preamble << "\\begin{" << name << "}";
1340                 }
1341
1342                 else if (t.cs() == "geometry") {
1343                         vector<string> opts = split_options(p.getArg('{', '}'));
1344                         handle_geometry(opts);
1345                 }
1346
1347                 else if (t.cs() == "definecolor") {
1348                         string const color = p.getArg('{', '}');
1349                         string const space = p.getArg('{', '}');
1350                         string const value = p.getArg('{', '}');
1351                         if (color == "document_fontcolor" && space == "rgb") {
1352                                 RGBColor c(RGBColorFromLaTeX(value));
1353                                 h_fontcolor = X11hexname(c);
1354                         } else if (color == "note_fontcolor" && space == "rgb") {
1355                                 RGBColor c(RGBColorFromLaTeX(value));
1356                                 h_notefontcolor = X11hexname(c);
1357                         } else if (color == "page_backgroundcolor" && space == "rgb") {
1358                                 RGBColor c(RGBColorFromLaTeX(value));
1359                                 h_backgroundcolor = X11hexname(c);
1360                         } else if (color == "shadecolor" && space == "rgb") {
1361                                 RGBColor c(RGBColorFromLaTeX(value));
1362                                 h_boxbgcolor = X11hexname(c);
1363                         } else {
1364                                 h_preamble << "\\definecolor{" << color
1365                                            << "}{" << space << "}{" << value
1366                                            << '}';
1367                         }
1368                 }
1369
1370                 else if (t.cs() == "jurabibsetup") {
1371                         // FIXME p.getArg('{', '}') is most probably wrong (it
1372                         //       does not handle nested braces).
1373                         //       Use p.verbatim_item() instead.
1374                         vector<string> jurabibsetup =
1375                                 split_options(p.getArg('{', '}'));
1376                         // add jurabibsetup to the jurabib package options
1377                         add_package("jurabib", jurabibsetup);
1378                         if (!jurabibsetup.empty()) {
1379                                 h_preamble << "\\jurabibsetup{"
1380                                            << join(jurabibsetup, ",") << '}';
1381                         }
1382                 }
1383
1384                 else if (t.cs() == "hypersetup") {
1385                         vector<string> hypersetup =
1386                                 split_options(p.verbatim_item());
1387                         // add hypersetup to the hyperref package options
1388                         handle_hyperref(hypersetup);
1389                         if (!hypersetup.empty()) {
1390                                 h_preamble << "\\hypersetup{"
1391                                            << join(hypersetup, ",") << '}';
1392                         }
1393                 }
1394
1395                 else if (is_known(t.cs(), known_if_3arg_commands)) {
1396                         // prevent misparsing of \usepackage if it is used
1397                         // as an argument (see e.g. our own output of
1398                         // \@ifundefined above)
1399                         string const arg1 = p.verbatim_item();
1400                         string const arg2 = p.verbatim_item();
1401                         string const arg3 = p.verbatim_item();
1402                         // test case \@ifundefined{date}{}{\date{}}
1403                         if (t.cs() == "@ifundefined" && arg1 == "date" &&
1404                             arg2.empty() && arg3 == "\\date{}") {
1405                                 h_suppress_date = "true";
1406                         // older tex2lyx versions did output
1407                         // \@ifundefined{definecolor}{\usepackage{color}}{}
1408                         } else if (t.cs() == "@ifundefined" &&
1409                                    arg1 == "definecolor" &&
1410                                    arg2 == "\\usepackage{color}" &&
1411                                    arg3.empty()) {
1412                                 if (!in_lyx_preamble)
1413                                         h_preamble << package_beg_sep
1414                                                    << "color"
1415                                                    << package_mid_sep
1416                                                    << "\\@ifundefined{definecolor}{color}{}"
1417                                                    << package_end_sep;
1418                         // test for case
1419                         //\@ifundefined{showcaptionsetup}{}{%
1420                         // \PassOptionsToPackage{caption=false}{subfig}}
1421                         // that LyX uses for subfloats
1422                         } else if (t.cs() == "@ifundefined" &&
1423                                    arg1 == "showcaptionsetup" && arg2.empty()
1424                                 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1425                                 ; // do nothing
1426                         } else if (!in_lyx_preamble) {
1427                                 h_preamble << t.asInput()
1428                                            << '{' << arg1 << '}'
1429                                            << '{' << arg2 << '}'
1430                                            << '{' << arg3 << '}';
1431                         }
1432                 }
1433
1434                 else if (is_known(t.cs(), known_if_commands)) {
1435                         // must not parse anything in conditional code, since
1436                         // LyX would output the parsed contents unconditionally
1437                         if (!in_lyx_preamble)
1438                                 h_preamble << t.asInput();
1439                         handle_if(p, in_lyx_preamble);
1440                 }
1441
1442                 else if (!t.cs().empty() && !in_lyx_preamble)
1443                         h_preamble << '\\' << t.cs();
1444         }
1445
1446         // remove the whitespace
1447         p.skip_spaces();
1448
1449         // Force textclass if the user wanted it
1450         if (!forceclass.empty())
1451                 h_textclass = forceclass;
1452         if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1453                 h_textclass.insert(0, "literate-");
1454         tc.setName(h_textclass);
1455         if (!tc.load()) {
1456                 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1457                 exit(EXIT_FAILURE);
1458         }
1459         if (h_papersides.empty()) {
1460                 ostringstream ss;
1461                 ss << tc.sides();
1462                 h_papersides = ss.str();
1463         }
1464 }
1465
1466
1467 string babel2lyx(string const & language)
1468 {
1469         char const * const * where = is_known(language, known_languages);
1470         if (where)
1471                 return known_coded_languages[where - known_languages];
1472         return language;
1473 }
1474
1475
1476 string rgbcolor2code(string const & name)
1477 {
1478         char const * const * where = is_known(name, known_basic_colors);
1479         if (where) {
1480                 // "red", "green" etc
1481                 return known_basic_color_codes[where - known_basic_colors];
1482         }
1483         // "255,0,0", "0,255,0" etc
1484         RGBColor c(RGBColorFromLaTeX(name));
1485         return X11hexname(c);
1486 }
1487
1488 // }])
1489
1490
1491 } // namespace lyx