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