]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.C
Add a Buffer::fully_loaded member function, returning true only when
[lyx.git] / src / tex2lyx / preamble.C
1 /**
2  * \file preamble.C
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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16
17 #include "layout.h"
18 #include "lyxtextclass.h"
19 #include "lyxlex.h"
20 #include "support/filetools.h"
21
22 #include <algorithm>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include <map>
28
29 using std::istringstream;
30 using std::ostream;
31 using std::ostringstream;
32 using std::string;
33 using std::vector;
34
35 using lyx::support::LibFileSearch;
36
37 // special columntypes
38 extern std::map<char, int> special_columns;
39
40 namespace {
41
42 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
43 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
44 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
45 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
46 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
47 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
48 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
49 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
50 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
51 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
52 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
53 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
54
55 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
56
57 // some ugly stuff
58 ostringstream h_preamble;
59 string h_textclass               = "article";
60 string h_options                 = string();
61 string h_language                = "english";
62 string h_inputencoding           = "latin1";
63 string h_fontscheme              = "default";
64 string h_graphics                = "default";
65 string h_paperfontsize           = "default";
66 string h_spacing                 = "single";
67 string h_papersize               = "default";
68 string h_paperpackage            = "default";
69 string h_use_geometry            = "0";
70 string h_use_amsmath             = "0";
71 string h_use_natbib              = "0";
72 string h_use_numerical_citations = "0";
73 string h_paperorientation        = "portrait";
74 string h_secnumdepth             = "3";
75 string h_tocdepth                = "3";
76 string h_paragraph_separation    = "indent";
77 string h_defskip                 = "medskip";
78 string h_quotes_language         = "english";
79 string h_quotes_times            = "2";
80 string h_papercolumns            = "1";
81 string h_papersides              = "1";
82 string h_paperpagestyle          = "default";
83 string h_tracking_changes        = "0";
84
85
86 void handle_opt(vector<string> & opts, char const ** what, string & target)
87 {
88         if (opts.empty())
89                 return;
90
91         for ( ; *what; ++what) {
92                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
93                 if (it != opts.end()) {
94                         //cerr << "### found option '" << *what << "'\n";
95                         target = *what;
96                         opts.erase(it);
97                         return;
98                 }
99         }
100 }
101
102
103 void handle_package(string const & name, string const & options)
104 {
105         //cerr << "handle_package: '" << name << "'\n";
106         if (name == "a4wide") {
107                 h_papersize = "a4paper";
108                 h_paperpackage = "widemarginsa4";
109         } else if (name == "ae")
110                 h_fontscheme = "ae";
111         else if (name == "aecompl")
112                 h_fontscheme = "ae";
113         else if (name == "amsmath")
114                 h_use_amsmath = "1";
115         else if (name == "amssymb")
116                 h_use_amsmath = "1";
117         else if (name == "babel")
118                 ; // ignore this
119         else if (name == "fontenc")
120                 ; // ignore this
121         else if (name == "inputenc")
122                 h_inputencoding = options;
123         else if (name == "makeidx")
124                 ; // ignore this
125         else if (name == "verbatim")
126                 ; // ignore this
127         else if (is_known(name, known_languages)) {
128                 h_language = name;
129                 h_quotes_language = name;
130         } else {
131                 if (options.size())
132                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
133                 else
134                         h_preamble << "\\usepackage{" << name << "}\n";
135         }
136 }
137
138
139
140 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
141 {
142         os << "#LyX file created by  tex2lyx 0.1.2 \n"
143            << "\\lyxformat 225\n"
144            << "\\textclass " << h_textclass << "\n"
145            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
146         if (h_options.size())
147            os << "\\options " << h_options << "\n";
148         os << "\\language " << h_language << "\n"
149            << "\\inputencoding " << h_inputencoding << "\n"
150            << "\\fontscheme " << h_fontscheme << "\n"
151            << "\\graphics " << h_graphics << "\n"
152            << "\\paperfontsize " << h_paperfontsize << "\n"
153            << "\\spacing " << h_spacing << "\n"
154            << "\\papersize " << h_papersize << "\n"
155            << "\\paperpackage " << h_paperpackage << "\n"
156            << "\\use_geometry " << h_use_geometry << "\n"
157            << "\\use_amsmath " << h_use_amsmath << "\n"
158            << "\\use_natbib " << h_use_natbib << "\n"
159            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
160            << "\\paperorientation " << h_paperorientation << "\n"
161            << "\\secnumdepth " << h_secnumdepth << "\n"
162            << "\\tocdepth " << h_tocdepth << "\n"
163            << "\\paragraph_separation " << h_paragraph_separation << "\n"
164            << "\\defskip " << h_defskip << "\n"
165            << "\\quotes_language " << h_quotes_language << "\n"
166            << "\\quotes_times " << h_quotes_times << "\n"
167            << "\\papercolumns " << h_papercolumns << "\n"
168            << "\\papersides " << h_papersides << "\n"
169            << "\\paperpagestyle " << h_paperpagestyle << "\n"
170            << "\\tracking_changes " << h_tracking_changes << "\n"
171            << "\\end_header\n";
172 }
173
174 } // anonymous namespace
175
176 LyXTextClass const parse_preamble(Parser & p, ostream & os)
177 {
178         // initialize fixed types
179         special_columns['D'] = 3;
180
181         while (p.good()) {
182                 Token const & t = p.get_token();
183
184 #ifdef FILEDEBUG
185                 cerr << "t: " << t << " flags: " << flags << "\n";
186                 //cell->dump();
187 #endif
188
189                 //
190                 // cat codes
191                 //
192                 if (t.cat() == catLetter ||
193                           t.cat() == catSpace ||
194                           t.cat() == catSuper ||
195                           t.cat() == catSub ||
196                           t.cat() == catOther ||
197                           t.cat() == catMath ||
198                           t.cat() == catActive ||
199                           t.cat() == catBegin ||
200                           t.cat() == catEnd ||
201                           t.cat() == catAlign ||
202                           t.cat() == catNewline ||
203                           t.cat() == catParameter)
204                 h_preamble << t.character();
205
206                 else if (t.cat() == catComment)
207                         handle_comment(p);
208
209                 else if (t.cs() == "pagestyle")
210                         h_paperpagestyle == p.verbatim_item();
211
212                 else if (t.cs() == "makeatletter") {
213                         p.setCatCode('@', catLetter);
214                         h_preamble << "\\makeatletter\n";
215                 }
216
217                 else if (t.cs() == "makeatother") {
218                         p.setCatCode('@', catOther);
219                         h_preamble << "\\makeatother\n";
220                 }
221
222                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
223                             || t.cs() == "providecommand") {
224                         bool star = false;
225                         if (p.next_token().character() == '*') {
226                                 p.get_token();
227                                 star = true;
228                         }
229                         string const name = p.verbatim_item();
230                         string const opts = p.getOpt();
231                         string const body = p.verbatim_item();
232                         // only non-lyxspecific stuff
233                         if (name != "\\noun "
234                                   && name != "\\tabularnewline "
235                             && name != "\\LyX "
236                                   && name != "\\lyxline "
237                                   && name != "\\lyxaddress "
238                                   && name != "\\lyxrightaddress "
239                                   && name != "\\boldsymbol "
240                                   && name != "\\lyxarrow ") {
241                                 ostringstream ss;
242                                 ss << '\\' << t.cs();
243                                 if (star)
244                                         ss << '*';
245                                 ss << '{' << name << '}' << opts << '{' << body << "}\n";
246                                 h_preamble << ss.str();
247 /*
248                                 ostream & out = in_preamble ? h_preamble : os;
249                                 out << "\\" << t.cs() << "{" << name << "}"
250                                     << opts << "{" << body << "}\n";
251 */
252                         }
253                 }
254
255                 else if (t.cs() == "documentclass") {
256                         vector<string> opts;
257                         split(p.getArg('[', ']'), opts, ',');
258                         handle_opt(opts, known_languages, h_language);
259                         handle_opt(opts, known_fontsizes, h_paperfontsize);
260                         h_quotes_language = h_language;
261                         h_options = join(opts, ",");
262                         h_textclass = p.getArg('{', '}');
263                 }
264
265                 else if (t.cs() == "usepackage") {
266                         string const options = p.getArg('[', ']');
267                         string const name = p.getArg('{', '}');
268                         if (options.empty() && name.find(',')) {
269                                 vector<string> vecnames;
270                                 split(name, vecnames, ',');
271                                 vector<string>::const_iterator it  = vecnames.begin();
272                                 vector<string>::const_iterator end = vecnames.end();
273                                 for (; it != end; ++it)
274                                         handle_package(trim(*it), string());
275                         } else {
276                                 handle_package(name, options);
277                         }
278                 }
279
280                 else if (t.cs() == "newenvironment") {
281                         string const name = p.getArg('{', '}');
282                         ostringstream ss;
283                         ss << "\\newenvironment{" << name << "}";
284                         ss << p.getOpt();
285                         ss << p.getOpt();
286                         ss << '{' << p.verbatim_item() << '}';
287                         ss << '{' << p.verbatim_item() << '}';
288                         ss << '\n';
289                         if (name != "lyxcode" && name != "lyxlist"
290                                         && name != "lyxrightadress" && name != "lyxaddress")
291                                 h_preamble << ss.str();
292                 }
293
294                 else if (t.cs() == "def") {
295                         string name = p.get_token().cs();
296                         while (p.next_token().cat() != catBegin)
297                                 name += p.get_token().asString();
298                         h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
299                 }
300
301                 else if (t.cs() == "newcolumntype") {
302                         string const name = p.getArg('{', '}');
303                         trim(name);
304                         int nargs = 0;
305                         string opts = p.getOpt();
306                         if (opts.size()) {
307                                 istringstream is(string(opts, 1));
308                                 //cerr << "opt: " << is.str() << "\n";
309                                 is >> nargs;
310                         }
311                         special_columns[name[0]] = nargs;
312                         h_preamble << "\\newcolumntype{" << name << "}";
313                         if (nargs)
314                                 h_preamble << "[" << nargs << "]";
315                         h_preamble << "{" << p.verbatim_item() << "}\n";
316                 }
317
318                 else if (t.cs() == "setcounter") {
319                         string const name = p.getArg('{', '}');
320                         string const content = p.getArg('{', '}');
321                         if (name == "secnumdepth")
322                                 h_secnumdepth = content;
323                         else if (name == "tocdepth")
324                                 h_tocdepth = content;
325                         else
326                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
327                 }
328
329                 else if (t.cs() == "setlength") {
330                         string const name = p.verbatim_item();
331                         string const content = p.verbatim_item();
332                         if (name == "parskip")
333                                 h_paragraph_separation = "skip";
334                         else if (name == "parindent")
335                                 h_paragraph_separation = "skip";
336                         else
337                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
338                 }
339
340                 else if (t.cs() == "par")
341                         h_preamble << '\n';
342
343                 else if (t.cs() == "begin") {
344                         string const name = p.getArg('{', '}');
345                         if (name == "document")
346                                 break;
347                         h_preamble << "\\begin{" << name << "}";
348                 }
349
350                 else if (t.cs().size())
351                         h_preamble << '\\' << t.cs() << ' ';
352         }
353
354         LyXTextClass textclass;
355         textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
356         end_preamble(os, textclass);
357         return textclass;
358 }
359
360 // }])