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