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