]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.cpp
Increase tex2lyx output format to 378.
[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 "tex2lyx.h"
17
18 #include "LayoutFile.h"
19 #include "Layout.h"
20 #include "Lexer.h"
21 #include "TextClass.h"
22
23 #include "support/convert.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27
28 #include "support/regex.h"
29
30 #include <algorithm>
31 #include <iostream>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 #include <map>
36
37 using namespace std;
38 using namespace lyx::support;
39
40
41 namespace lyx {
42
43 // special columntypes
44 extern map<char, int> special_columns;
45
46 map<string, vector<string> > used_packages;
47 const char * const modules_placeholder = "\001modules\001";
48
49 // needed to handle encodings with babel
50 bool one_language = true;
51 string h_inputencoding = "auto";
52
53 namespace {
54
55 //add this to known_languages when updating to lyxformat 266:
56 // "armenian"
57 //add these to known_languages when updating to lyxformat 268:
58 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
59 // Both changes require first that support for non-babel languages (CJK,
60 // armtex) is added.
61 // add turkmen for lyxformat 383
62 /**
63  * known babel language names (including synonyms)
64  * not in standard babel: arabic, arabtex, belarusian, serbian-latin, thai
65  * not yet supported by LyX: kurmanji
66  * please keep this in sync with known_coded_languages line by line!
67  */
68 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
69 "american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
70 "basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
71 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
72 "english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
73 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
74 "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
75 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
76 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
77 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
78 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
79 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
80 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "uppersorbian",
81 "UKenglish", "USenglish", "usorbian", "vietnam", "welsh", 0};
82
83 /**
84  * the same as known_languages with .lyx names
85  * please keep this in sync with known_languages line by line!
86  */
87 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
88 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
89 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
90 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
91 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
92 "french", "french", "french", "galician", "german", "german", "greek",
93 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
94 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
95 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
96 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
97 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
98 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
99 "swedish", "thai", "turkish", "ukrainian", "ukrainian", "uppersorbian",
100 "uppersorbian", "english", "english", "vietnamese", "welsh", 0};
101
102 /// languages with english quotes (.lyx names)
103 const char * const known_english_quotes_languages[] = {"american", "bahasa",
104 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
105 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
106
107 //add this to known_french_quotes_languages when updating to
108 //lyxformat 383: "turkmen"
109 /// languages with french quotes (.lyx names)
110 const char * const known_french_quotes_languages[] = {"albanian",
111 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
112 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
113 "russian", "spanish", "spanish-mexico", "turkish", "ukrainian", "vietnamese", 0};
114
115 /// languages with german quotes (.lyx names)
116 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
117 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
118 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
119
120 /// languages with polish quotes (.lyx names)
121 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
122 "dutch", "estonian", "magyar", "polish", "romanian", 0};
123
124 /// languages with swedish quotes (.lyx names)
125 const char * const known_swedish_quotes_languages[] = {"finnish",
126 "swedish", 0};
127
128 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
129
130 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
131 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
132
133 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
134 "helvet", "lmss", 0};
135
136 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
137 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
138 "newcent", 0};
139
140 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
141 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
142 "letterpaper", 0};
143
144 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
145 "executivepaper", "legalpaper", "letterpaper", 0};
146
147 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin", 
148 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
149
150 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
151 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
152 "columnsep", 0};
153
154 /// commands that can start an \if...\else...\endif sequence
155 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
156 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
157 "ifsidecap", "ifupgreek", 0};
158
159 /// conditional commands with three arguments like \@ifundefined{}{}{}
160 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
161 0};
162
163 // default settings
164 ostringstream h_preamble;
165 string h_textclass               = "article";
166 string h_use_default_options     = "false";
167 string h_options;
168 string h_language                = "english";
169 string h_font_roman              = "default";
170 string h_font_sans               = "default";
171 string h_font_typewriter         = "default";
172 string h_font_default_family     = "default";
173 string h_font_sc                 = "false";
174 string h_font_osf                = "false";
175 string h_font_sf_scale           = "100";
176 string h_font_tt_scale           = "100";
177 string h_graphics                = "default";
178 string h_float_placement;
179 string h_paperfontsize           = "default";
180 string h_spacing                 = "single";
181 string h_use_hyperref            = "0";
182 string h_pdf_title;
183 string h_pdf_author;
184 string h_pdf_subject;
185 string h_pdf_keywords;
186 string h_pdf_bookmarks           = "1";
187 string h_pdf_bookmarksnumbered   = "0";
188 string h_pdf_bookmarksopen       = "0";
189 string h_pdf_bookmarksopenlevel  = "1";
190 string h_pdf_breaklinks          = "0";
191 string h_pdf_pdfborder           = "0";
192 string h_pdf_colorlinks          = "0";
193 string h_pdf_backref             = "section";
194 string h_pdf_pdfusetitle         = "1";
195 string h_pdf_pagemode;
196 string h_pdf_quoted_options;
197 string h_papersize               = "default";
198 string h_use_geometry            = "false";
199 string h_use_amsmath             = "1";
200 string h_use_esint               = "1";
201 string h_use_mhchem              = "0";
202 string h_cite_engine             = "basic";
203 string h_use_bibtopic            = "false";
204 string h_paperorientation        = "portrait";
205 string h_notefontcolor;
206 string h_secnumdepth             = "3";
207 string h_tocdepth                = "3";
208 string h_paragraph_separation    = "indent";
209 string h_defskip                 = "medskip";
210 string h_paragraph_indentation   = "default";
211 string h_quotes_language         = "english";
212 string h_papercolumns            = "1";
213 string h_papersides;
214 string h_paperpagestyle          = "default";
215 string h_listings_params;
216 string h_tracking_changes        = "false";
217 string h_output_changes          = "false";
218 string h_margins;
219
220
221 // returns true if at least one of the options in what has been found
222 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
223 {
224         if (opts.empty())
225                 return false;
226
227         bool found = false;
228         // the last language option is the document language (for babel and LyX)
229         // the last size option is the document font size
230         vector<string>::iterator it;
231         vector<string>::iterator position = opts.begin();
232         for (; *what; ++what) {
233                 it = find(opts.begin(), opts.end(), *what);
234                 if (it != opts.end()) {
235                         if (it >= position) {
236                                 found = true;
237                                 target = *what;
238                                 position = it;
239                         }
240                 }
241         }
242         return found;
243 }
244
245
246 void delete_opt(vector<string> & opts, char const * const * what)
247 {
248         if (opts.empty())
249                 return;
250
251         // remove found options from the list
252         // do this after handle_opt to avoid potential memory leaks
253         vector<string>::iterator it;
254         for (; *what; ++what) {
255                 it = find(opts.begin(), opts.end(), *what);
256                 if (it != opts.end())
257                         opts.erase(it);
258         }
259 }
260
261
262 /*!
263  * Split a package options string (keyval format) into a vector.
264  * Example input:
265  *   authorformat=smallcaps,
266  *   commabeforerest,
267  *   titleformat=colonsep,
268  *   bibformat={tabular,ibidem,numbered}
269  */
270 vector<string> split_options(string const & input)
271 {
272         vector<string> options;
273         string option;
274         Parser p(input);
275         while (p.good()) {
276                 Token const & t = p.get_token();
277                 if (t.asInput() == ",") {
278                         options.push_back(trimSpaceAndEol(option));
279                         option.erase();
280                 } else if (t.asInput() == "=") {
281                         option += '=';
282                         p.skip_spaces(true);
283                         if (p.next_token().asInput() == "{")
284                                 option += '{' + p.getArg('{', '}') + '}';
285                 } else if (t.cat() != catSpace)
286                         option += t.asInput();
287         }
288
289         if (!option.empty())
290                 options.push_back(trimSpaceAndEol(option));
291
292         return options;
293 }
294
295
296 /*!
297  * Retrieve a keyval option "name={value with=sign}" named \p name from
298  * \p options and return the value.
299  * The found option is also removed from \p options.
300  */
301 string process_keyval_opt(vector<string> & options, string name)
302 {
303         for (size_t i = 0; i < options.size(); ++i) {
304                 vector<string> option;
305                 split(options[i], option, '=');
306                 if (option.size() < 2)
307                         continue;
308                 if (option[0] == name) {
309                         options.erase(options.begin() + i);
310                         option.erase(option.begin());
311                         return join(option, "=");
312                 }
313         }
314         return "";
315 }
316
317
318 /*!
319  * Add package \p name with options \p options to used_packages.
320  * Remove options from \p options that we don't want to output.
321  */
322 void add_package(string const & name, vector<string> & options)
323 {
324         // every package inherits the global options
325         if (used_packages.find(name) == used_packages.end())
326                 used_packages[name] = split_options(h_options);
327
328         vector<string> & v = used_packages[name];
329         v.insert(v.end(), options.begin(), options.end());
330         if (name == "jurabib") {
331                 // Don't output the order argument (see the cite command
332                 // handling code in text.cpp).
333                 vector<string>::iterator end =
334                         remove(options.begin(), options.end(), "natbiborder");
335                 end = remove(options.begin(), end, "jurabiborder");
336                 options.erase(end, options.end());
337         }
338 }
339
340
341 // Given is a string like "scaled=0.9", return 0.9 * 100
342 string const scale_as_percentage(string const & scale)
343 {
344         string::size_type pos = scale.find('=');
345         if (pos != string::npos) {
346                 string value = scale.substr(pos + 1);
347                 if (isStrDbl(value))
348                         return convert<string>(100 * convert<double>(value));
349         }
350         // If the input string didn't match our expectations.
351         // return the default value "100"
352         return "100";
353 }
354
355
356 string remove_braces(string const & value)
357 {
358         if (value.empty())
359                 return value;
360         if (value[0] == '{' && value[value.length()-1] == '}')
361                 return value.substr(1, value.length()-2);
362         return value;
363 }
364
365
366 void handle_hyperref(vector<string> & options)
367 {
368         // FIXME swallow inputencoding changes that might surround the
369         //       hyperref setup if it was written by LyX
370         h_use_hyperref = "1";
371         // swallow "unicode=true", since LyX does always write that
372         vector<string>::iterator it =
373                 find(options.begin(), options.end(), "unicode=true");
374         if (it != options.end())
375                 options.erase(it);
376         it = find(options.begin(), options.end(), "pdfusetitle");
377         if (it != options.end()) {
378                 h_pdf_pdfusetitle = "1";
379                 options.erase(it);
380         }
381         string bookmarks = process_keyval_opt(options, "bookmarks");
382         if (bookmarks == "true")
383                 h_pdf_bookmarks = "1";
384         else if (bookmarks == "false")
385                 h_pdf_bookmarks = "0";
386         if (h_pdf_bookmarks == "1") {
387                 string bookmarksnumbered =
388                         process_keyval_opt(options, "bookmarksnumbered");
389                 if (bookmarksnumbered == "true")
390                         h_pdf_bookmarksnumbered = "1";
391                 else if (bookmarksnumbered == "false")
392                         h_pdf_bookmarksnumbered = "0";
393                 string bookmarksopen =
394                         process_keyval_opt(options, "bookmarksopen");
395                 if (bookmarksopen == "true")
396                         h_pdf_bookmarksopen = "1";
397                 else if (bookmarksopen == "false")
398                         h_pdf_bookmarksopen = "0";
399                 if (h_pdf_bookmarksopen == "1") {
400                         string bookmarksopenlevel =
401                                 process_keyval_opt(options, "bookmarksopenlevel");
402                         if (!bookmarksopenlevel.empty())
403                                 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
404                 }
405         }
406         string breaklinks = process_keyval_opt(options, "breaklinks");
407         if (breaklinks == "true")
408                 h_pdf_breaklinks = "1";
409         else if (breaklinks == "false")
410                 h_pdf_breaklinks = "0";
411         string pdfborder = process_keyval_opt(options, "pdfborder");
412         if (pdfborder == "{0 0 0}")
413                 h_pdf_pdfborder = "1";
414         else if (pdfborder == "{0 0 1}")
415                 h_pdf_pdfborder = "0";
416         string backref = process_keyval_opt(options, "backref");
417         if (!backref.empty())
418                 h_pdf_backref = backref;
419         string colorlinks = process_keyval_opt(options, "colorlinks");
420         if (colorlinks == "true")
421                 h_pdf_colorlinks = "1";
422         else if (colorlinks == "false")
423                 h_pdf_colorlinks = "0";
424         string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
425         if (!pdfpagemode.empty())
426                 h_pdf_pagemode = pdfpagemode;
427         string pdftitle = process_keyval_opt(options, "pdftitle");
428         if (!pdftitle.empty()) {
429                 h_pdf_title = remove_braces(pdftitle);
430         }
431         string pdfauthor = process_keyval_opt(options, "pdfauthor");
432         if (!pdfauthor.empty()) {
433                 h_pdf_author = remove_braces(pdfauthor);
434         }
435         string pdfsubject = process_keyval_opt(options, "pdfsubject");
436         if (!pdfsubject.empty())
437                 h_pdf_subject = remove_braces(pdfsubject);
438         string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
439         if (!pdfkeywords.empty())
440                 h_pdf_keywords = remove_braces(pdfkeywords);
441         if (!options.empty()) {
442                 if (!h_pdf_quoted_options.empty())
443                         h_pdf_quoted_options += ',';
444                 h_pdf_quoted_options += join(options, ",");
445                 options.clear();
446         }
447 }
448
449
450 void handle_package(Parser &p, string const & name, string const & opts,
451                     bool in_lyx_preamble)
452 {
453         vector<string> options = split_options(opts);
454         add_package(name, options);
455         string scale;
456
457         // roman fonts
458         if (is_known(name, known_roman_fonts)) {
459                 h_font_roman = name;
460                 p.skip_spaces();
461         }
462
463         if (name == "fourier") {
464                 h_font_roman = "utopia";
465                 // when font uses real small capitals
466                 if (opts == "expert")
467                         h_font_sc = "true";
468         }
469
470         if (name == "mathpazo")
471                 h_font_roman = "palatino";
472
473         if (name == "mathptmx")
474                 h_font_roman = "times";
475
476         // sansserif fonts
477         if (is_known(name, known_sans_fonts)) {
478                 h_font_sans = name;
479                 if (!opts.empty()) {
480                         scale = opts;
481                         h_font_sf_scale = scale_as_percentage(scale);
482                 }
483         }
484
485         // typewriter fonts
486         if (is_known(name, known_typewriter_fonts)) {
487                 h_font_typewriter = name;
488                 if (!opts.empty()) {
489                         scale = opts;
490                         h_font_tt_scale = scale_as_percentage(scale);
491                 }
492         }
493
494         // font uses old-style figure
495         if (name == "eco")
496                 h_font_osf = "true";
497
498         else if (name == "amsmath" || name == "amssymb")
499                 h_use_amsmath = "2";
500
501         else if (name == "esint")
502                 h_use_esint = "2";
503
504         else if (name == "mhchem")
505                 h_use_mhchem = "2";
506
507         else if (name == "babel" && !opts.empty()) {
508                 // check if more than one option was used - used later for inputenc
509                 // in case inputenc is parsed before babel, set the encoding to auto
510                 if (options.begin() != options.end() - 1) {
511                         one_language = false;
512                         h_inputencoding = "auto";
513                 }
514                 // babel takes the last language of the option of its \usepackage
515                 // call as document language. If there is no such language option, the
516                 // last language in the documentclass options is used.
517                 handle_opt(options, known_languages, h_language);
518                 delete_opt(options, known_languages);
519         }
520
521         else if (name == "fontenc")
522                  ;// ignore this
523
524         else if (name == "inputenc" || name == "luainputenc") {
525                 // h_inputencoding is only set when there is not more than one
526                 // inputenc option because otherwise h_inputencoding must be
527                 // set to "auto" (the default encoding of the document language)
528                 // Therefore check for the "," character.
529                 // It is also only set when there is not more then one babel
530                 // language option but this is handled in the routine for babel.
531                 if (opts.find(",") == string::npos && one_language == true)
532                         h_inputencoding = opts;
533                 if (!options.empty())
534                         p.setEncoding(options.back());
535                 options.clear();
536         }
537
538         else if (name == "makeidx")
539                 ; // ignore this
540
541         else if (name == "prettyref")
542                 ; // ignore this
543
544         else if (name == "varioref")
545                 ; // ignore this
546
547         else if (name == "verbatim")
548                 ; // ignore this
549
550         else if (name == "nomencl")
551                 ; // ignore this
552
553         else if (name == "textcomp")
554                 ; // ignore this
555
556         else if (name == "url")
557                 ; // ignore this
558
559         else if (LYX_FORMAT >= 408 && name == "subscript")
560                 ; // ignore this
561
562         else if (name == "color") {
563                 // with the following command this package is only loaded when needed for
564                 // undefined colors, since we only support the predefined colors
565                 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
566         }
567
568         else if (name == "graphicx")
569                 ; // ignore this
570
571         else if (name == "setspace")
572                 ; // ignore this
573
574         else if (name == "geometry")
575                 ; // Ignore this, the geometry settings are made by the \geometry
576                   // command. This command is handled below.
577
578         else if (is_known(name, known_languages))
579                 h_language = name;
580
581         else if (name == "natbib") {
582                 h_cite_engine = "natbib_authoryear";
583                 vector<string>::iterator it =
584                         find(options.begin(), options.end(), "authoryear");
585                 if (it != options.end())
586                         options.erase(it);
587                 else {
588                         it = find(options.begin(), options.end(), "numbers");
589                         if (it != options.end()) {
590                                 h_cite_engine = "natbib_numerical";
591                                 options.erase(it);
592                         }
593                 }
594         }
595
596         else if (name == "jurabib")
597                 h_cite_engine = "jurabib";
598
599         else if (name == "hyperref")
600                 handle_hyperref(options);
601
602         else if (!in_lyx_preamble) {
603                 if (options.empty())
604                         h_preamble << "\\usepackage{" << name << "}";
605                 else {
606                         h_preamble << "\\usepackage[" << opts << "]{" 
607                                    << name << "}";
608                         options.clear();
609                 }
610         }
611
612         // We need to do something with the options...
613         if (!options.empty())
614                 cerr << "Ignoring options '" << join(options, ",")
615                      << "' of package " << name << '.' << endl;
616
617         // remove the whitespace
618         p.skip_spaces();
619 }
620
621
622 void handle_if(Parser & p, bool in_lyx_preamble)
623 {
624         while (p.good()) {
625                 Token t = p.get_token();
626                 if (t.cat() == catEscape &&
627                     is_known(t.cs(), known_if_commands))
628                         handle_if(p, in_lyx_preamble);
629                 else {
630                         if (!in_lyx_preamble)
631                                 h_preamble << t.asInput();
632                         if (t.cat() == catEscape && t.cs() == "fi")
633                                 return;
634                 }
635         }
636 }
637
638
639 void end_preamble(ostream & os, TextClass const & /*textclass*/)
640 {
641         // translate from babel to LyX names
642         h_language = babel2lyx(h_language);
643
644         // set the quote language
645         // LyX only knows the following quotes languages:
646         // english, swedish, german, polish, french and danish
647         // (quotes for "japanese" and "chinese-traditional" are missing because
648         //  they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
649         // conversion list taken from
650         // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
651         // (quotes for kazakh and interlingua are unknown)
652         // danish
653         if (h_language == "danish")
654                 h_quotes_language = "danish";
655         // french
656         else if (is_known(h_language, known_french_quotes_languages))
657                 h_quotes_language = "french";
658         // german
659         else if (is_known(h_language, known_german_quotes_languages))
660                 h_quotes_language = "german";
661         // polish
662         else if (is_known(h_language, known_polish_quotes_languages))
663                 h_quotes_language = "polish";
664         // swedish
665         else if (is_known(h_language, known_swedish_quotes_languages))
666                 h_quotes_language = "swedish";
667         //english
668         else if (is_known(h_language, known_english_quotes_languages))
669                 h_quotes_language = "english";
670
671         // output the LyX file settings
672         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
673            << "\\lyxformat " << LYX_FORMAT << '\n'
674            << "\\begin_document\n"
675            << "\\begin_header\n"
676            << "\\textclass " << h_textclass << "\n";
677         if (!h_preamble.str().empty())
678                 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
679         if (!h_options.empty())
680                 os << "\\options " << h_options << "\n";
681         os << "\\use_default_options " << h_use_default_options << "\n"
682            << modules_placeholder
683            << "\\language " << h_language << "\n"
684            << "\\inputencoding " << h_inputencoding << "\n"
685            << "\\font_roman " << h_font_roman << "\n"
686            << "\\font_sans " << h_font_sans << "\n"
687            << "\\font_typewriter " << h_font_typewriter << "\n"
688            << "\\font_default_family " << h_font_default_family << "\n"
689            << "\\font_sc " << h_font_sc << "\n"
690            << "\\font_osf " << h_font_osf << "\n"
691            << "\\font_sf_scale " << h_font_sf_scale << "\n"
692            << "\\font_tt_scale " << h_font_tt_scale << "\n"
693            << "\\graphics " << h_graphics << "\n";
694         if (!h_float_placement.empty())
695                 os << "\\float_placement " << h_float_placement << "\n";
696         os << "\\paperfontsize " << h_paperfontsize << "\n"
697            << "\\spacing " << h_spacing << "\n"
698            << "\\use_hyperref " << h_use_hyperref << '\n';
699         if (h_use_hyperref == "1") {
700                 if (!h_pdf_title.empty())
701                         os << "\\pdf_title \"" << h_pdf_title << "\"\n";
702                 if (!h_pdf_author.empty())
703                         os << "\\pdf_author \"" << h_pdf_author << "\"\n";
704                 if (!h_pdf_subject.empty())
705                         os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
706                 if (!h_pdf_keywords.empty())
707                         os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
708                 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
709                       "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
710                       "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
711                       "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
712                       "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
713                       "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
714                       "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
715                       "\\pdf_backref " << h_pdf_backref << "\n"
716                       "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
717                 if (!h_pdf_pagemode.empty())
718                         os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
719                 if (!h_pdf_quoted_options.empty())
720                         os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
721         }
722         os << "\\papersize " << h_papersize << "\n"
723            << "\\use_geometry " << h_use_geometry << "\n"
724            << "\\use_amsmath " << h_use_amsmath << "\n"
725            << "\\use_esint " << h_use_esint << "\n"
726            << "\\use_mhchem " << h_use_mhchem << "\n"
727            << "\\cite_engine " << h_cite_engine << "\n"
728            << "\\use_bibtopic " << h_use_bibtopic << "\n"
729            << "\\paperorientation " << h_paperorientation << '\n';
730         if (LYX_FORMAT >= 382 && !h_notefontcolor.empty())
731                 os << "\\notefontcolor " << h_notefontcolor << '\n';
732         os << h_margins
733            << "\\secnumdepth " << h_secnumdepth << "\n"
734            << "\\tocdepth " << h_tocdepth << "\n"
735            << "\\paragraph_separation " << h_paragraph_separation << "\n";
736         if (LYX_FORMAT < 365 || h_paragraph_separation == "skip")
737                 os << "\\defskip " << h_defskip << "\n";
738         else
739                 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
740         os << "\\quotes_language " << h_quotes_language << "\n"
741            << "\\papercolumns " << h_papercolumns << "\n"
742            << "\\papersides " << h_papersides << "\n"
743            << "\\paperpagestyle " << h_paperpagestyle << "\n";
744         if (!h_listings_params.empty())
745                 os << "\\listings_params " << h_listings_params << "\n";
746         os << "\\tracking_changes " << h_tracking_changes << "\n"
747            << "\\output_changes " << h_output_changes << "\n"
748            << "\\end_header\n\n"
749            << "\\begin_body\n";
750         // clear preamble for subdocuments
751         h_preamble.str("");
752 }
753
754 } // anonymous namespace
755
756
757 void parse_preamble(Parser & p, ostream & os, 
758         string const & forceclass, TeX2LyXDocClass & tc)
759 {
760         // initialize fixed types
761         special_columns['D'] = 3;
762         bool is_full_document = false;
763         bool is_lyx_file = false;
764         bool in_lyx_preamble = false;
765
766         // determine whether this is a full document or a fragment for inclusion
767         while (p.good()) {
768                 Token const & t = p.get_token();
769
770                 if (t.cat() == catEscape && t.cs() == "documentclass") {
771                         is_full_document = true;
772                         break;
773                 }
774         }
775         p.reset();
776
777         while (is_full_document && p.good()) {
778                 Token const & t = p.get_token();
779
780 #ifdef FILEDEBUG
781                 cerr << "t: " << t << "\n";
782 #endif
783
784                 //
785                 // cat codes
786                 //
787                 if (!in_lyx_preamble &&
788                     (t.cat() == catLetter ||
789                      t.cat() == catSuper ||
790                      t.cat() == catSub ||
791                      t.cat() == catOther ||
792                      t.cat() == catMath ||
793                      t.cat() == catActive ||
794                      t.cat() == catBegin ||
795                      t.cat() == catEnd ||
796                      t.cat() == catAlign ||
797                      t.cat() == catParameter))
798                         h_preamble << t.cs();
799
800                 else if (!in_lyx_preamble && 
801                          (t.cat() == catSpace || t.cat() == catNewline))
802                         h_preamble << t.asInput();
803
804                 else if (t.cat() == catComment) {
805                         static regex const islyxfile("%% LyX .* created this file");
806                         static regex const usercommands("User specified LaTeX commands");
807
808                         string const comment = t.asInput();
809
810                         // magically switch encoding default if it looks like XeLaTeX
811                         static string const magicXeLaTeX =
812                                 "% This document must be compiled with XeLaTeX ";
813                         if (comment.size() > magicXeLaTeX.size() 
814                                   && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
815                                   && h_inputencoding == "auto") {
816                                 cerr << "XeLaTeX comment found, switching to UTF8\n";
817                                 h_inputencoding = "utf8";
818                         }
819                         smatch sub;
820                         if (regex_search(comment, sub, islyxfile)) {
821                                 is_lyx_file = true;
822                                 in_lyx_preamble = true;
823                         } else if (is_lyx_file
824                                    && regex_search(comment, sub, usercommands))
825                                 in_lyx_preamble = false;
826                         else if (!in_lyx_preamble)
827                                 h_preamble << t.asInput();
828                 }
829
830                 else if (t.cs() == "pagestyle")
831                         h_paperpagestyle = p.verbatim_item();
832
833                 else if (t.cs() == "makeatletter") {
834                         // LyX takes care of this
835                         p.setCatCode('@', catLetter);
836                 }
837
838                 else if (t.cs() == "makeatother") {
839                         // LyX takes care of this
840                         p.setCatCode('@', catOther);
841                 }
842
843                 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
844                       || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
845                       || t.cs() == "providecommand" || t.cs() == "providecommandx"
846                                 || t.cs() == "DeclareRobustCommand"
847                       || t.cs() == "DeclareRobustCommandx"
848                                 || t.cs() == "ProvideTextCommandDefault"
849                                 || t.cs() == "DeclareMathAccent") {
850                         bool star = false;
851                         if (p.next_token().character() == '*') {
852                                 p.get_token();
853                                 star = true;
854                         }
855                         string const name = p.verbatim_item();
856                         string const opt1 = p.getFullOpt();
857                         string const opt2 = p.getFullOpt();
858                         string const body = p.verbatim_item();
859                         // font settings
860                         if (name == "\\rmdefault")
861                                 if (is_known(body, known_roman_fonts))
862                                         h_font_roman = body;
863                         if (name == "\\sfdefault")
864                                 if (is_known(body, known_sans_fonts))
865                                         h_font_sans = body;
866                         if (name == "\\ttdefault")
867                                 if (is_known(body, known_typewriter_fonts))
868                                         h_font_typewriter = body;
869                         if (name == "\\familydefault") {
870                                 string family = body;
871                                 // remove leading "\"
872                                 h_font_default_family = family.erase(0,1);
873                         }
874
875                         // Add the command to the known commands
876                         add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
877
878                         // only non-lyxspecific stuff
879                         if (!in_lyx_preamble) {
880                                 ostringstream ss;
881                                 ss << '\\' << t.cs();
882                                 if (star)
883                                         ss << '*';
884                                 ss << '{' << name << '}' << opt1 << opt2
885                                    << '{' << body << "}";
886                                 h_preamble << ss.str();
887 /*
888                                 ostream & out = in_preamble ? h_preamble : os;
889                                 out << "\\" << t.cs() << "{" << name << "}"
890                                     << opts << "{" << body << "}";
891 */
892                         }
893                 }
894
895                 else if (t.cs() == "documentclass") {
896                         vector<string>::iterator it;
897                         vector<string> opts = split_options(p.getArg('[', ']'));
898                         handle_opt(opts, known_fontsizes, h_paperfontsize);
899                         delete_opt(opts, known_fontsizes);
900                         // delete "pt" at the end
901                         string::size_type i = h_paperfontsize.find("pt");
902                         if (i != string::npos)
903                                 h_paperfontsize.erase(i);
904                         // The documentclass options are always parsed before the options
905                         // of the babel call so that a language cannot overwrite the babel
906                         // options.
907                         handle_opt(opts, known_languages, h_language);
908                         delete_opt(opts, known_languages);
909
910                         // paper orientation
911                         if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
912                                 h_paperorientation = "landscape";
913                                 opts.erase(it);
914                         }
915                         // paper sides
916                         if ((it = find(opts.begin(), opts.end(), "oneside"))
917                                  != opts.end()) {
918                                 h_papersides = "1";
919                                 opts.erase(it);
920                         }
921                         if ((it = find(opts.begin(), opts.end(), "twoside"))
922                                  != opts.end()) {
923                                 h_papersides = "2";
924                                 opts.erase(it);
925                         }
926                         // paper columns
927                         if ((it = find(opts.begin(), opts.end(), "onecolumn"))
928                                  != opts.end()) {
929                                 h_papercolumns = "1";
930                                 opts.erase(it);
931                         }
932                         if ((it = find(opts.begin(), opts.end(), "twocolumn"))
933                                  != opts.end()) {
934                                 h_papercolumns = "2";
935                                 opts.erase(it);
936                         }
937                         // paper sizes
938                         // some size options are know to any document classes, other sizes
939                         // are handled by the \geometry command of the geometry package
940                         handle_opt(opts, known_class_paper_sizes, h_papersize);
941                         delete_opt(opts, known_class_paper_sizes);
942                         // the remaining options
943                         h_options = join(opts, ",");
944                         // FIXME This does not work for classes that have a
945                         //       different name in LyX than in LaTeX
946                         h_textclass = p.getArg('{', '}');
947                 }
948
949                 else if (t.cs() == "usepackage") {
950                         string const options = p.getArg('[', ']');
951                         string const name = p.getArg('{', '}');
952                         vector<string> vecnames;
953                         split(name, vecnames, ',');
954                         vector<string>::const_iterator it  = vecnames.begin();
955                         vector<string>::const_iterator end = vecnames.end();
956                         for (; it != end; ++it)
957                                 handle_package(p, trimSpaceAndEol(*it), options, 
958                                                in_lyx_preamble);
959                 }
960
961                 else if (t.cs() == "inputencoding") {
962                         string const encoding = p.getArg('{','}');
963                         h_inputencoding = encoding;
964                         p.setEncoding(encoding);
965                 }
966
967                 else if (t.cs() == "newenvironment") {
968                         string const name = p.getArg('{', '}');
969                         string const opt1 = p.getFullOpt();
970                         string const opt2 = p.getFullOpt();
971                         string const beg = p.verbatim_item();
972                         string const end = p.verbatim_item();
973                         if (!in_lyx_preamble) {
974                                 h_preamble << "\\newenvironment{" << name
975                                            << '}' << opt1 << opt2 << '{'
976                                            << beg << "}{" << end << '}';
977                         }
978                         add_known_environment(name, opt1, !opt2.empty(),
979                                               from_utf8(beg), from_utf8(end));
980
981                 }
982
983                 else if (t.cs() == "def") {
984                         string name = p.get_token().cs();
985                         while (p.next_token().cat() != catBegin)
986                                 name += p.get_token().cs();
987                         if (!in_lyx_preamble)
988                                 h_preamble << "\\def\\" << name << '{'
989                                            << p.verbatim_item() << "}";
990                 }
991
992                 else if (t.cs() == "newcolumntype") {
993                         string const name = p.getArg('{', '}');
994                         trimSpaceAndEol(name);
995                         int nargs = 0;
996                         string opts = p.getOpt();
997                         if (!opts.empty()) {
998                                 istringstream is(string(opts, 1));
999                                 is >> nargs;
1000                         }
1001                         special_columns[name[0]] = nargs;
1002                         h_preamble << "\\newcolumntype{" << name << "}";
1003                         if (nargs)
1004                                 h_preamble << "[" << nargs << "]";
1005                         h_preamble << "{" << p.verbatim_item() << "}";
1006                 }
1007
1008                 else if (t.cs() == "setcounter") {
1009                         string const name = p.getArg('{', '}');
1010                         string const content = p.getArg('{', '}');
1011                         if (name == "secnumdepth")
1012                                 h_secnumdepth = content;
1013                         else if (name == "tocdepth")
1014                                 h_tocdepth = content;
1015                         else
1016                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1017                 }
1018
1019                 else if (t.cs() == "setlength") {
1020                         string const name = p.verbatim_item();
1021                         string const content = p.verbatim_item();
1022                         // the paragraphs are only not indented when \parindent is set to zero
1023                         if (name == "\\parindent" && content != "") {
1024                                 if (content[0] == '0')
1025                                         h_paragraph_separation = "skip";
1026                                 else if (LYX_FORMAT >= 365)
1027                                         h_paragraph_indentation = translate_len(content);
1028                                 else
1029                                         h_preamble << "\\setlength{" << name
1030                                                    << "}{" << content << "}";
1031                         } else if (name == "\\parskip") {
1032                                 if (content == "\\smallskipamount")
1033                                         h_defskip = "smallskip";
1034                                 else if (content == "\\medskipamount")
1035                                         h_defskip = "medskip";
1036                                 else if (content == "\\bigskipamount")
1037                                         h_defskip = "bigskip";
1038                                 else
1039                                         h_defskip = content;
1040                         } else
1041                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1042                 }
1043
1044                 else if (t.cs() == "onehalfspacing")
1045                         h_spacing = "onehalf";
1046
1047                 else if (t.cs() == "doublespacing")
1048                         h_spacing = "double";
1049
1050                 else if (t.cs() == "setstretch")
1051                         h_spacing = "other " + p.verbatim_item();
1052
1053                 else if (t.cs() == "begin") {
1054                         string const name = p.getArg('{', '}');
1055                         if (name == "document")
1056                                 break;
1057                         h_preamble << "\\begin{" << name << "}";
1058                 }
1059
1060                 else if (t.cs() == "geometry") {
1061                         h_use_geometry = "true";
1062                         vector<string> opts = split_options(p.getArg('{', '}'));
1063                         vector<string>::iterator it;
1064                         // paper orientation
1065                         if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1066                                 h_paperorientation = "landscape";
1067                                 opts.erase(it);
1068                         }
1069                         // paper size
1070                         handle_opt(opts, known_paper_sizes, h_papersize);
1071                         delete_opt(opts, known_paper_sizes);
1072                         // page margins
1073                         char const * const * margin = known_paper_margins;
1074                         int k = -1;
1075                         for (; *margin; ++margin) {
1076                                 k += 1;
1077                                 // search for the "=" in e.g. "lmargin=2cm" to get the value
1078                                 for(size_t i = 0; i != opts.size(); i++) {
1079                                         if (opts.at(i).find(*margin) != string::npos) {
1080                                                 string::size_type pos = opts.at(i).find("=");
1081                                                 string value = opts.at(i).substr(pos + 1);
1082                                                 string name = known_coded_paper_margins[k];
1083                                                 h_margins += "\\" + name + " " + value + "\n";
1084                                         }
1085                                 }
1086                         }
1087                 }
1088
1089                 else if (t.cs() == "definecolor") {
1090                         string const color = p.getArg('{', '}');
1091                         string const space = p.getArg('{', '}');
1092                         string const value = p.getArg('{', '}');
1093                         if (LYX_FORMAT >= 382 &&
1094                             color == "note_fontcolor" && space == "rgb") {
1095                                 RGBColor c(RGBColorFromLaTeX(value));
1096                                 h_notefontcolor = X11hexname(c);
1097                         } else {
1098                                 h_preamble << "\\definecolor{" << color
1099                                            << "}{" << space << "}{" << value
1100                                            << '}';
1101                         }
1102                 }
1103
1104                 else if (t.cs() == "jurabibsetup") {
1105                         // FIXME p.getArg('{', '}') is most probably wrong (it
1106                         //       does not handle nested braces).
1107                         //       Use p.verbatim_item() instead.
1108                         vector<string> jurabibsetup =
1109                                 split_options(p.getArg('{', '}'));
1110                         // add jurabibsetup to the jurabib package options
1111                         add_package("jurabib", jurabibsetup);
1112                         if (!jurabibsetup.empty()) {
1113                                 h_preamble << "\\jurabibsetup{"
1114                                            << join(jurabibsetup, ",") << '}';
1115                         }
1116                 }
1117
1118                 else if (t.cs() == "hypersetup") {
1119                         vector<string> hypersetup =
1120                                 split_options(p.verbatim_item());
1121                         // add hypersetup to the hyperref package options
1122                         handle_hyperref(hypersetup);
1123                         if (!hypersetup.empty()) {
1124                                 h_preamble << "\\hypersetup{"
1125                                            << join(hypersetup, ",") << '}';
1126                         }
1127                 }
1128
1129                 else if (is_known(t.cs(), known_if_3arg_commands)) {
1130                         // prevent misparsing of \usepackage if it is used
1131                         // as an argument (see e.g. our own output of
1132                         // \@ifundefined above)
1133                         string const arg1 = p.verbatim_item();
1134                         string const arg2 = p.verbatim_item();
1135                         string const arg3 = p.verbatim_item();
1136                         if (!in_lyx_preamble) {
1137                                 h_preamble << t.asInput()
1138                                            << '{' << arg1 << '}'
1139                                            << '{' << arg2 << '}'
1140                                            << '{' << arg3 << '}';
1141                         }
1142                 }
1143
1144                 else if (is_known(t.cs(), known_if_commands)) {
1145                         // must not parse anything in conditional code, since
1146                         // LyX would output the parsed contents unconditionally
1147                         if (!in_lyx_preamble)
1148                                 h_preamble << t.asInput();
1149                         handle_if(p, in_lyx_preamble);
1150                 }
1151
1152                 else if (!t.cs().empty() && !in_lyx_preamble)
1153                         h_preamble << '\\' << t.cs();
1154         }
1155
1156         // remove the whitespace
1157         p.skip_spaces();
1158
1159         // Force textclass if the user wanted it
1160         if (!forceclass.empty())
1161                 h_textclass = forceclass;
1162         if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1163                 h_textclass.insert(0, "literate-");
1164         tc.setName(h_textclass);
1165         if (!tc.load()) {
1166                 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1167                 exit(EXIT_FAILURE);
1168         }
1169         if (h_papersides.empty()) {
1170                 ostringstream ss;
1171                 ss << tc.sides();
1172                 h_papersides = ss.str();
1173         }
1174         end_preamble(os, tc);
1175 }
1176
1177
1178 /// translates a babel language name to a LyX language name
1179 string babel2lyx(string const & language)
1180 {
1181         char const * const * where = is_known(language, known_languages);
1182         if (where)
1183                 return known_coded_languages[where - known_languages];
1184         return language;
1185 }
1186
1187 // }])
1188
1189
1190 } // namespace lyx