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