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