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