]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.cpp
getting rid of superfluous lyx::support:: statements.
[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 "Layout.h"
19 #include "Lexer.h"
20 #include "TextClass.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24
25 #include <algorithm>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 #include <map>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37 // special columntypes
38 extern map<char, int> special_columns;
39
40 map<string, vector<string> > used_packages;
41
42 // needed to handle encodings with babel
43 bool one_language = true;
44
45 // to avoid that the babel options overwrite the documentclass options
46 bool documentclass_language;
47
48 namespace {
49
50 const char * const known_languages[] = { "afrikaans", "american", "arabic",
51 "austrian", "bahasa", "basque", "belarusian", "brazil", "breton", "british",
52 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
53 "dutch", "english", "esperanto", "estonian", "finnish", "francais", "french",
54 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
55 "hebrew", "icelandic", "irish", "italian", "lsorbian", "magyar", "naustrian",
56 "ngerman", "ngermanb", "norsk", "nynorsk", "polish", "portuges", "romanian",
57 "russian", "russianb", "scottish", "serbian", "slovak", "slovene", "spanish",
58 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "usorbian", "welsh", 0};
59
60 //note this when updating to lyxformat 305:
61 //bahasai, indonesian, and indon = equal to bahasa
62 //malay, and meyalu = equal to bahasam
63
64 const char * const known_french_languages[] = {"french", "frenchb", "francais",
65                                                 "frenchle", "frenchpro", 0};
66 const char * const known_german_languages[] = {"german", "germanb", 0};
67 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
68 const char * const known_russian_languages[] = {"russian", "russianb", 0};
69 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
70
71 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
72
73 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
74 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
75
76 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
77 "helvet", "lmss", 0};
78
79 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
80 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
81 "newcent", 0};
82
83 // some ugly stuff
84 ostringstream h_preamble;
85 string h_textclass               = "article";
86 string h_options                 = string();
87 string h_language                = "english";
88 string h_inputencoding           = "auto";
89 string h_font_roman              = "default";
90 string h_font_sans               = "default";
91 string h_font_typewriter         = "default";
92 string h_font_default_family     = "default";
93 string h_font_sc                 = "false";
94 string h_font_osf                = "false";
95 string h_font_sf_scale           = "100";
96 string h_font_tt_scale           = "100";
97 string h_graphics                = "default";
98 string h_paperfontsize           = "default";
99 string h_spacing                 = "single";
100 string h_papersize               = "default";
101 string h_use_geometry            = "false";
102 string h_use_amsmath             = "0";
103 string h_cite_engine             = "basic";
104 string h_use_bibtopic            = "false";
105 string h_paperorientation        = "portrait";
106 string h_secnumdepth             = "3";
107 string h_tocdepth                = "3";
108 string h_paragraph_separation    = "indent";
109 string h_defskip                 = "medskip";
110 string h_quotes_language         = "english";
111 string h_papercolumns            = "1";
112 string h_papersides              = string();
113 string h_paperpagestyle          = "default";
114 string h_tracking_changes        = "false";
115 string h_output_changes          = "false";
116
117
118 void handle_opt(vector<string> & opts, char const * const * what, string & target)
119 {
120         if (opts.empty())
121                 return;
122
123         // the last language option is the document language (for babel and LyX)
124         // the last size option is the document font size
125         vector<string>::iterator it;
126         vector<string>::iterator position = opts.begin();
127         for (; *what; ++what) {
128                 it = find(opts.begin(), opts.end(), *what);
129                 if (it != opts.end()) {
130                         documentclass_language = true;
131                         if (it >= position) {
132                                 target = *what;
133                                 position = it;
134                         }
135                 }
136         }
137 }
138
139
140 void delete_opt(vector<string> & opts, char const * const * what)
141 {
142         if (opts.empty())
143                 return;
144
145         // remove found options from the list
146         // do this after handle_opt to avoid potential memory leaks and to be able
147         // to find in every case the last language option
148         vector<string>::iterator it;
149         for (; *what; ++what) {
150                 it = find(opts.begin(), opts.end(), *what);
151                 if (it != opts.end())
152                         opts.erase(it);
153         }
154 }
155
156
157 /*!
158  * Split a package options string (keyval format) into a vector.
159  * Example input:
160  *   authorformat=smallcaps,
161  *   commabeforerest,
162  *   titleformat=colonsep,
163  *   bibformat={tabular,ibidem,numbered}
164  */
165 vector<string> split_options(string const & input)
166 {
167         vector<string> options;
168         string option;
169         Parser p(input);
170         while (p.good()) {
171                 Token const & t = p.get_token();
172                 if (t.asInput() == ",") {
173                         options.push_back(trim(option));
174                         option.erase();
175                 } else if (t.asInput() == "=") {
176                         option += '=';
177                         p.skip_spaces(true);
178                         if (p.next_token().asInput() == "{")
179                                 option += '{' + p.getArg('{', '}') + '}';
180                 } else if (t.cat() != catSpace)
181                         option += t.asInput();
182         }
183
184         if (!option.empty())
185                 options.push_back(trim(option));
186
187         return options;
188 }
189
190
191 /*!
192  * Add package \p name with options \p options to used_packages.
193  * Remove options from \p options that we don't want to output.
194  */
195 void add_package(string const & name, vector<string> & options)
196 {
197         // every package inherits the global options
198         if (used_packages.find(name) == used_packages.end())
199                 used_packages[name] = split_options(h_options);
200
201         vector<string> & v = used_packages[name];
202         v.insert(v.end(), options.begin(), options.end());
203         if (name == "jurabib") {
204                 // Don't output the order argument (see the cite command
205                 // handling code in text.cpp).
206                 vector<string>::iterator end =
207                         remove(options.begin(), options.end(), "natbiborder");
208                 end = remove(options.begin(), end, "jurabiborder");
209                 options.erase(end, options.end());
210         }
211 }
212
213
214 // Given is a string like "scaled=0.9", return 0.9 * 100
215 string const scale_as_percentage(string const & scale)
216 {
217         string::size_type pos = scale.find('=');
218         if (pos != string::npos) {
219                 string value = scale.substr(pos + 1);
220                 if (isStrDbl(value))
221                         return convert<string>(100 * convert<double>(value));
222         }
223         // If the input string didn't match our expectations.
224         // return the default value "100"
225         return "100";
226 }
227
228
229 void handle_package(string const & name, string const & opts)
230 {
231         vector<string> options = split_options(opts);
232         add_package(name, options);
233         string scale;
234
235         // roman fonts
236         if (is_known(name, known_roman_fonts))
237                 h_font_roman = name;
238         if (name == "fourier") {
239                 h_font_roman = "utopia";
240                 // when font uses real small capitals
241                 if (opts == "expert")
242                         h_font_sc = "true";
243         }
244         if (name == "mathpazo")
245                 h_font_roman = "palatino";
246         if (name == "mathptmx")
247                 h_font_roman = "times";
248         // sansserif fonts
249         if (is_known(name, known_sans_fonts)) {
250                 h_font_sans = name;
251                 if (!opts.empty()) {
252                         scale = opts;
253                         h_font_sf_scale = scale_as_percentage(scale);
254                 }
255         }
256         // typewriter fonts
257         if (is_known(name, known_typewriter_fonts)) {
258                 h_font_typewriter = name;
259                 if (!opts.empty()) {
260                         scale = opts;
261                         h_font_tt_scale = scale_as_percentage(scale);
262                 }
263         }
264         // font uses old-style figure
265         if (name == "eco")
266                 h_font_osf = "true";
267
268         else if (name == "amsmath" || name == "amssymb")
269                 h_use_amsmath = "1";
270         else if (name == "babel" && !opts.empty()) {
271                 // check if more than one option was used - used later for inputenc
272                 // in case inputenc is parsed before babel, set the encoding to auto
273                 if (options.begin() != options.end() - 1) {
274                         one_language = false;
275                         h_inputencoding = "auto";
276                 }
277                 // only set the document language when there was not already one set
278                 // via the documentclass options
279                 // babel takes the the last language given in the documentclass options
280                 // as document language. If there is no such language option, the last
281                 // option of its \usepackage call is used.
282                 if (documentclass_language == false) {
283                         handle_opt(options, known_languages, h_language);
284                         delete_opt(options, known_languages);
285                         if (is_known(h_language, known_french_languages))
286                                 h_language = "french";
287                         else if (is_known(h_language, known_german_languages))
288                                 h_language = "german";
289                         else if (is_known(h_language, known_ngerman_languages))
290                                 h_language = "ngerman";
291                         else if (is_known(h_language, known_russian_languages))
292                                 h_language = "russian";
293                         else if (is_known(h_language, known_ukrainian_languages))
294                                 h_language = "ukrainian";
295                         h_quotes_language = h_language;
296                 }
297         }
298         else if (name == "fontenc")
299                 ; // ignore this
300         else if (name == "inputenc") {
301                 // only set when there is not more than one inputenc option
302                 // therefore check for the "," character
303                 // also only set when there is not more then one babel language option
304                 if (opts.find(",") == string::npos && one_language == true)
305                         h_inputencoding = opts;
306                 options.clear();
307         } else if (name == "makeidx")
308                 ; // ignore this
309         else if (name == "verbatim")
310                 ; // ignore this
311         else if (name == "graphicx")
312                 ; // ignore this
313         else if (is_known(name, known_languages)) {
314                 if (is_known(name, known_french_languages))
315                         h_language = "french";
316                 else if (is_known(name, known_german_languages))
317                         h_language = "german";
318                 else if (is_known(name, known_ngerman_languages))
319                         h_language = "ngerman";
320                 else if (is_known(name, known_russian_languages))
321                         h_language = "russian";
322                 else if (is_known(name, known_ukrainian_languages))
323                         h_language = "ukrainian";
324                 else
325                         h_language = name;
326                 h_quotes_language = h_language;
327
328         } else if (name == "natbib") {
329                 h_cite_engine = "natbib_authoryear";
330                 vector<string>::iterator it =
331                         find(options.begin(), options.end(), "authoryear");
332                 if (it != options.end())
333                         options.erase(it);
334                 else {
335                         it = find(options.begin(), options.end(), "numbers");
336                         if (it != options.end()) {
337                                 h_cite_engine = "natbib_numerical";
338                                 options.erase(it);
339                         }
340                 }
341         } else if (name == "jurabib") {
342                 h_cite_engine = "jurabib";
343         } else if (options.empty())
344                 h_preamble << "\\usepackage{" << name << "}\n";
345         else {
346                 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
347                 options.clear();
348         }
349
350         // We need to do something with the options...
351         if (!options.empty())
352                 cerr << "Ignoring options '" << join(options, ",")
353                      << "' of package " << name << '.' << endl;
354 }
355
356
357
358 void end_preamble(ostream & os, TextClass const & /*textclass*/)
359 {
360         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
361            << "\\lyxformat 247\n"
362            << "\\begin_document\n"
363            << "\\begin_header\n"
364            << "\\textclass " << h_textclass << "\n";
365         if (!h_preamble.str().empty())
366                 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
367         if (!h_options.empty())
368                 os << "\\options " << h_options << "\n";
369         os << "\\language " << h_language << "\n"
370            << "\\inputencoding " << h_inputencoding << "\n"
371            << "\\font_roman " << h_font_roman << "\n"
372            << "\\font_sans " << h_font_sans << "\n"
373            << "\\font_typewriter " << h_font_typewriter << "\n"
374            << "\\font_default_family " << h_font_default_family << "\n"
375            << "\\font_sc " << h_font_sc << "\n"
376            << "\\font_osf " << h_font_osf << "\n"
377            << "\\font_sf_scale " << h_font_sf_scale << "\n"
378            << "\\font_tt_scale " << h_font_tt_scale << "\n"
379            << "\\graphics " << h_graphics << "\n"
380            << "\\paperfontsize " << h_paperfontsize << "\n"
381            << "\\spacing " << h_spacing << "\n"
382            << "\\papersize " << h_papersize << "\n"
383            << "\\use_geometry " << h_use_geometry << "\n"
384            << "\\use_amsmath " << h_use_amsmath << "\n"
385            << "\\cite_engine " << h_cite_engine << "\n"
386            << "\\use_bibtopic " << h_use_bibtopic << "\n"
387            << "\\paperorientation " << h_paperorientation << "\n"
388            << "\\secnumdepth " << h_secnumdepth << "\n"
389            << "\\tocdepth " << h_tocdepth << "\n"
390            << "\\paragraph_separation " << h_paragraph_separation << "\n"
391            << "\\defskip " << h_defskip << "\n"
392            << "\\quotes_language " << h_quotes_language << "\n"
393            << "\\papercolumns " << h_papercolumns << "\n"
394            << "\\papersides " << h_papersides << "\n"
395            << "\\paperpagestyle " << h_paperpagestyle << "\n"
396            << "\\tracking_changes " << h_tracking_changes << "\n"
397            << "\\output_changes " << h_output_changes << "\n"
398            << "\\end_header\n\n"
399            << "\\begin_body\n";
400         // clear preamble for subdocuments
401         h_preamble.str("");
402 }
403
404 } // anonymous namespace
405
406 TextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
407 {
408         // initialize fixed types
409         special_columns['D'] = 3;
410         bool is_full_document = false;
411
412         // determine whether this is a full document or a fragment for inclusion
413         while (p.good()) {
414                 Token const & t = p.get_token();
415
416                 if (t.cat() == catEscape && t.cs() == "documentclass") {
417                         is_full_document = true;
418                         break;
419                 }
420         }
421         p.reset();
422
423         while (is_full_document && p.good()) {
424                 Token const & t = p.get_token();
425
426 #ifdef FILEDEBUG
427                 cerr << "t: " << t << "\n";
428 #endif
429
430                 //
431                 // cat codes
432                 //
433                 if (t.cat() == catLetter ||
434                           t.cat() == catSuper ||
435                           t.cat() == catSub ||
436                           t.cat() == catOther ||
437                           t.cat() == catMath ||
438                           t.cat() == catActive ||
439                           t.cat() == catBegin ||
440                           t.cat() == catEnd ||
441                           t.cat() == catAlign ||
442                           t.cat() == catParameter)
443                 h_preamble << t.character();
444
445                 else if (t.cat() == catSpace || t.cat() == catNewline)
446                         h_preamble << t.asInput();
447
448                 else if (t.cat() == catComment)
449                         h_preamble << t.asInput();
450
451                 else if (t.cs() == "pagestyle")
452                         h_paperpagestyle = p.verbatim_item();
453
454                 else if (t.cs() == "makeatletter") {
455                         p.setCatCode('@', catLetter);
456                 }
457
458                 else if (t.cs() == "makeatother") {
459                         p.setCatCode('@', catOther);
460                 }
461
462                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
463                             || t.cs() == "providecommand") {
464                         bool star = false;
465                         if (p.next_token().character() == '*') {
466                                 p.get_token();
467                                 star = true;
468                         }
469                         string const name = p.verbatim_item();
470                         string const opt1 = p.getOpt();
471                         string const opt2 = p.getFullOpt();
472                         string const body = p.verbatim_item();
473                         // font settings
474                         if (name == "\\rmdefault")
475                                 if (is_known(body, known_roman_fonts))
476                                         h_font_roman = body;
477
478                         if (name == "\\sfdefault")
479                                 if (is_known(body, known_sans_fonts))
480                                         h_font_sans = body;
481
482                         if (name == "\\ttdefault")
483                                 if (is_known(body, known_typewriter_fonts))
484                                         h_font_typewriter = body;
485
486                         if (name == "\\familydefault") {
487                                 string family = body;
488                                 // remove leading "\"
489                                 h_font_default_family = family.erase(0,1);
490                         }
491                         // only non-lyxspecific stuff
492                         if (   name != "\\noun"
493                             && name != "\\tabularnewline"
494                             && name != "\\LyX"
495                             && name != "\\lyxline"
496                             && name != "\\lyxaddress"
497                             && name != "\\lyxrightaddress"
498                             && name != "\\lyxdot"
499                             && name != "\\boldsymbol"
500                             && name != "\\lyxarrow"
501                             && name != "\\rmdefault"
502                             && name != "\\sfdefault"
503                             && name != "\\ttdefault"
504                             && name != "\\familydefault") {
505                                 ostringstream ss;
506                                 ss << '\\' << t.cs();
507                                 if (star)
508                                         ss << '*';
509                                 ss << '{' << name << '}' << opt1 << opt2
510                                    << '{' << body << "}";
511                                 h_preamble << ss.str();
512
513                                 // Add the command to the known commands
514                                 add_known_command(name, opt1, !opt2.empty());
515 /*
516                                 ostream & out = in_preamble ? h_preamble : os;
517                                 out << "\\" << t.cs() << "{" << name << "}"
518                                     << opts << "{" << body << "}";
519 */
520                         }
521                 }
522
523                 else if (t.cs() == "documentclass") {
524                         vector<string> opts = split_options(p.getArg('[', ']'));
525                         handle_opt(opts, known_fontsizes, h_paperfontsize);
526                         delete_opt(opts, known_fontsizes);
527                         // delete "pt" at the end
528                         string::size_type i = h_paperfontsize.find("pt");
529                         if (i != string::npos)
530                                 h_paperfontsize.erase(i);
531                         // to avoid that the babel options overwrite the documentclass options
532                         documentclass_language = false;
533                         handle_opt(opts, known_languages, h_language);
534                         delete_opt(opts, known_languages);
535                         if (is_known(h_language, known_french_languages))
536                                 h_language = "french";
537                         else if (is_known(h_language, known_german_languages))
538                                 h_language = "german";
539                         else if (is_known(h_language, known_ngerman_languages))
540                                 h_language = "ngerman";
541                         else if (is_known(h_language, known_russian_languages))
542                                 h_language = "russian";
543                         else if (is_known(h_language, known_ukrainian_languages))
544                                 h_language = "ukrainian";
545                         h_quotes_language = h_language;
546                         h_options = join(opts, ",");
547                         h_textclass = p.getArg('{', '}');
548                 }
549
550                 else if (t.cs() == "usepackage") {
551                         string const options = p.getArg('[', ']');
552                         string const name = p.getArg('{', '}');
553                         if (options.empty() && name.find(',')) {
554                                 vector<string> vecnames;
555                                 split(name, vecnames, ',');
556                                 vector<string>::const_iterator it  = vecnames.begin();
557                                 vector<string>::const_iterator end = vecnames.end();
558                                 for (; it != end; ++it)
559                                         handle_package(trim(*it), string());
560                         } else {
561                                 handle_package(name, options);
562                         }
563                 }
564
565                 else if (t.cs() == "newenvironment") {
566                         string const name = p.getArg('{', '}');
567                         ostringstream ss;
568                         ss << "\\newenvironment{" << name << "}";
569                         ss << p.getOpt();
570                         ss << p.getOpt();
571                         ss << '{' << p.verbatim_item() << '}';
572                         ss << '{' << p.verbatim_item() << '}';
573                         if (name != "lyxcode" && name != "lyxlist" &&
574                             name != "lyxrightadress" &&
575                             name != "lyxaddress" && name != "lyxgreyedout")
576                                 h_preamble << ss.str();
577                 }
578
579                 else if (t.cs() == "def") {
580                         string name = p.get_token().cs();
581                         while (p.next_token().cat() != catBegin)
582                                 name += p.get_token().asString();
583                         h_preamble << "\\def\\" << name << '{'
584                                    << p.verbatim_item() << "}";
585                 }
586
587                 else if (t.cs() == "newcolumntype") {
588                         string const name = p.getArg('{', '}');
589                         trim(name);
590                         int nargs = 0;
591                         string opts = p.getOpt();
592                         if (!opts.empty()) {
593                                 istringstream is(string(opts, 1));
594                                 is >> nargs;
595                         }
596                         special_columns[name[0]] = nargs;
597                         h_preamble << "\\newcolumntype{" << name << "}";
598                         if (nargs)
599                                 h_preamble << "[" << nargs << "]";
600                         h_preamble << "{" << p.verbatim_item() << "}";
601                 }
602
603                 else if (t.cs() == "setcounter") {
604                         string const name = p.getArg('{', '}');
605                         string const content = p.getArg('{', '}');
606                         if (name == "secnumdepth")
607                                 h_secnumdepth = content;
608                         else if (name == "tocdepth")
609                                 h_tocdepth = content;
610                         else
611                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
612                 }
613
614                 else if (t.cs() == "setlength") {
615                         string const name = p.verbatim_item();
616                         string const content = p.verbatim_item();
617                         // Is this correct?
618                         if (name == "parskip")
619                                 h_paragraph_separation = "skip";
620                         else if (name == "parindent")
621                                 h_paragraph_separation = "skip";
622                         else
623                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
624                 }
625
626                 else if (t.cs() == "begin") {
627                         string const name = p.getArg('{', '}');
628                         if (name == "document")
629                                 break;
630                         h_preamble << "\\begin{" << name << "}";
631                 }
632
633                 else if (t.cs() == "jurabibsetup") {
634                         vector<string> jurabibsetup =
635                                 split_options(p.getArg('{', '}'));
636                         // add jurabibsetup to the jurabib package options
637                         add_package("jurabib", jurabibsetup);
638                         if (!jurabibsetup.empty()) {
639                                 h_preamble << "\\jurabibsetup{"
640                                            << join(jurabibsetup, ",") << '}';
641                         }
642                 }
643
644                 else if (!t.cs().empty())
645                         h_preamble << '\\' << t.cs();
646         }
647         p.skip_spaces();
648
649         // Force textclass if the user wanted it
650         if (!forceclass.empty())
651                 h_textclass = forceclass;
652         if (noweb_mode && !prefixIs(h_textclass, "literate-"))
653                 h_textclass.insert(0, "literate-");
654         FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
655         if (layoutfilename.empty()) {
656                 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
657                 exit(1);
658         }
659         TextClass textclass;
660         textclass.read(layoutfilename);
661         if (h_papersides.empty()) {
662                 ostringstream ss;
663                 ss << textclass.sides();
664                 h_papersides = ss.str();
665         }
666         end_preamble(os, textclass);
667         return textclass;
668 }
669
670 // }])
671
672
673 } // namespace lyx