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