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