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