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