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