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