]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
some small improvements
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 // {[(
6
7 #include <config.h>
8
9 #include <algorithm>
10 #include <cctype>
11 #include <fstream>
12 #include <iostream>
13 #include <stack>
14 #include <string>
15 #include <vector>
16
17 #include "Lsstream.h"
18
19 #include "texparser.h"
20
21 using std::count_if;
22 using std::cout;
23 using std::cerr;
24 using std::endl;
25 using std::fill;
26 using std::getline;
27 using std::ios;
28 using std::ifstream;
29 using std::istream;
30 using std::istringstream;
31 using std::ostream;
32 using std::ostringstream;
33 using std::stack;
34 using std::string;
35 using std::vector;
36
37
38 namespace {
39
40 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode);
41
42 char const OPEN = '<';
43 char const CLOSE = '>';
44
45 // rather brutish way to code table structure in a string:
46 //
47 //  \begin{tabular}{ccc}
48 //    1 & 2 & 3\\ 
49 //    \multicolumn{2}{c}{4} & 5\\ 
50 //    6 & 7 \\ 
51 //  \end{tabular}
52 //
53 // gets "translated" to:
54 //  
55 //  1 TAB 2 TAB 3 LINE
56 //  2 MULT c MULT 4 TAB 5 LINE
57 //  5 TAB 7 LINE
58
59 char const TAB  = '\001';
60 char const LINE = '\002';
61 char const MULT = '\003';
62
63 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
64 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
65 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
66 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
67 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
68 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
69 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
70 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
71 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
72 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
73 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
74 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
75
76 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
77
78 char const * known_headings[] = { "caption", "title", "author", "date",
79 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
80
81 char const * known_math_envs[] = { "equation", "equation*",
82 "eqnarray", "eqnarray*", "align", "align*", 0};
83
84 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
85 "printindex", 0 };
86
87 // LaTeX names for quotes
88 char const * known_quotes[] = { "glqq", "grqq", 0};
89
90 // the same as known_quotes with .lyx names
91 char const * known_coded_quotes[] = { "gld", "grd", 0};
92
93
94
95 // some ugly stuff
96 ostringstream h_preamble;
97 string h_textclass               = "article";
98 string h_options                 = "";
99 string h_language                = "english";
100 string h_inputencoding           = "latin1";
101 string h_fontscheme              = "default";
102 string h_graphics                = "default";
103 string h_paperfontsize           = "default";
104 string h_spacing                 = "single";
105 string h_papersize               = "default";
106 string h_paperpackage            = "default";
107 string h_use_geometry            = "0";
108 string h_use_amsmath             = "0";
109 string h_use_natbib              = "0";
110 string h_use_numerical_citations = "0";
111 string h_paperorientation        = "portrait";
112 string h_secnumdepth             = "3";
113 string h_tocdepth                = "3";
114 string h_paragraph_separation    = "indent";
115 string h_defskip                 = "medskip";
116 string h_quotes_language         = "english";
117 string h_quotes_times            = "1";
118 string h_papercolumns            = "1";
119 string h_papersides              = "1";
120 string h_paperpagestyle          = "default";
121 string h_tracking_changes        = "0";
122
123 // indicates whether we are in the preamble
124 bool in_preamble = true;
125
126 // current stack of nested environments
127 stack<string> active_environments;
128
129
130 string cap(string s)
131 {
132         if (s.size())
133                 s[0] = toupper(s[0]);
134         return s;
135 }
136
137
138 string const trim(string const & a, char const * p = " ")
139 {
140         // lyx::Assert(p);
141
142         if (a.empty() || !*p)
143                 return a;
144
145         string::size_type r = a.find_last_not_of(p);
146         string::size_type l = a.find_first_not_of(p);
147
148         // Is this the minimal test? (lgb)
149         if (r == string::npos && l == string::npos)
150                 return string();
151
152         return a.substr(l, r - l + 1);
153 }
154
155
156 void split(string const & s, vector<string> & result, char delim)
157 {
158         istringstream is(s);    
159         string t;
160         while (getline(is, t, delim))
161                 result.push_back(t);
162 }
163
164
165 string join(vector<string> const & input, char delim)
166 {
167         ostringstream os;
168         for (size_t i = 0; i != input.size(); ++i) {
169                 if (i)
170                         os << delim;    
171                 os << input[i]; 
172         }
173         return os.str();
174 }
175
176
177 char const ** is_known(string const & str, char const ** what)
178 {
179         for ( ; *what; ++what)
180                 if (str == *what)
181                         return what;
182         return 0;
183 }
184
185
186 void handle_opt(vector<string> & opts, char const ** what, string & target)
187 {
188         if (opts.empty())
189                 return;
190
191         for ( ; *what; ++what) {
192                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
193                 if (it != opts.end()) {
194                         //cerr << "### found option '" << *what << "'\n";
195                         target = *what;
196                         opts.erase(it);
197                         return;
198                 }
199         }
200 }
201
202
203 bool is_math_env(string const & name)
204 {
205         for (char const ** what = known_math_envs; *what; ++what)
206                 if (*what == name)
207                         return true;
208         return false;
209 }
210
211
212 void begin_inset(ostream & os, string const & name)
213 {
214         os << "\n\\begin_inset " << name;
215 }
216
217
218 void end_inset(ostream & os)
219 {
220         os << "\n\\end_inset\n\n";
221 }
222
223
224 string curr_env()
225 {
226         return active_environments.empty() ? string() : active_environments.top();
227 }
228
229
230 void handle_ert(ostream & os, string const & s)
231 {
232         begin_inset(os, "ERT");
233         os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
234         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
235                 if (*it == '\\')
236                         os << "\n\\backslash\n";
237                 else
238                         os << *it;
239         }
240         end_inset(os);
241 }
242
243
244 void handle_tex(ostream & os, string const & s)
245 {
246         //os << "handle_tex(" << s << ")\n";
247         if (in_preamble)
248                 h_preamble << s;
249         else
250                 handle_ert(os, s);
251 }
252                 
253
254 void handle_par(ostream & os)
255 {
256         if (active_environments.empty())
257                 return;
258         os << "\n\\layout ";
259         string s = curr_env();
260         if (s == "document" || s == "table") {
261                 os << "Standard\n\n";
262                 return;
263         }
264         if (s == "lyxcode") {
265                 os << "LyX-Code\n\n";
266                 return;
267         }
268         os << cap(s) << "\n\n";
269 }
270
271
272 void handle_package(string const & name, string const & options)
273 {
274         //cerr << "handle_package: '" << name << "'\n";
275         if (name == "a4wide") {
276                 h_papersize = "a4paper";
277                 h_paperpackage = "widemarginsa4";
278         } else if (name == "ae") 
279                 h_fontscheme = "ae";
280         else if (name == "aecompl") 
281                 h_fontscheme = "ae";
282         else if (name == "amsmath") 
283                 h_use_amsmath = "1";
284         else if (name == "amssymb") 
285                 h_use_amsmath = "1";
286         else if (name == "babel") 
287                 ; // ignore this
288         else if (name == "fontenc") 
289                 ; // ignore this
290         else if (name == "inputenc") 
291                 h_inputencoding = options;
292         else if (name == "makeidx") 
293                 ; // ignore this
294         else if (name == "verbatim") 
295                 ; // ignore this
296         else if (is_known(name, known_languages)) {
297                 h_language = name;
298                 h_quotes_language = name;
299         } else {
300                 if (options.size())
301                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
302                 else
303                         h_preamble << "\\usepackage{" << name << "}\n";
304         }
305 }
306
307
308 vector<string> extract_col_align(string const & s)
309 {
310         vector<string> res;
311         for (size_t i = 0; i < s.size(); ++i) {
312                 switch (s[i]) {
313                         case 'c': res.push_back("center"); break;
314                         case 'l': res.push_back("left");   break;
315                         case 'r': res.push_back("right");  break;
316                         default : res.push_back("right");  break;
317                 }
318         }
319         return res;
320 }
321
322
323 void handle_tabular(Parser & p, ostream & os, mode_type mode)
324 {
325         begin_inset(os, "Tabular \n");
326         string colopts = p.verbatimItem();
327         vector<string> colalign = extract_col_align(colopts);
328         ostringstream ss;
329         parse(p, ss, FLAG_END, mode);
330         vector<string> lines;
331         split(ss.str(), lines, LINE);
332         const size_t cols = colalign.size();
333         const size_t rows = lines.size();
334         os << "<lyxtabular version=\"3\" rows=\"" << rows
335                  << "\" columns=\"" << cols << "\">\n"
336                  << "<features>\n";
337         for (size_t c = 0; c < cols; ++c)
338                 os << "<column alignment=\"" << colalign[c] << "\""
339                          << " valignment=\"top\""
340                          << " width=\"0pt\""
341                          << ">\n";
342         for (size_t r = 0; r < rows; ++r) {
343                 vector<string> cells;
344                 split(lines[r], cells, TAB);
345                 while (cells.size() < cols)
346                         cells.push_back(string());
347                 //os << "<row bottomline=\"true\">\n";
348                 os << "<row>\n";
349                 for (size_t c = 0; c < cols; ++c) {
350                         os << "<cell";
351                         string alignment = "center";
352                         vector<string> parts;
353                         split(cells[c], parts, MULT);
354                         if (parts.size() > 2) {
355                                 os << " multicolumn=\"" << parts[0] << "\"";
356                                 alignment = parts[1];
357                         }
358                         os << " alignment=\"" << alignment << "\""
359                                  << " valignment=\"top\""
360                                  << " topline=\"true\""
361                                  << " leftline=\"true\""
362                                  << " usebox=\"none\""
363                                  << ">";
364                         begin_inset(os, "Text");
365                         os << "\n\n\\layout Standard\n\n";
366                         if (parts.size())
367                                 os << parts.back();
368                         end_inset(os);
369                         os << "</cell>\n";
370                 }
371                 os << "</row>\n";
372         }
373         os << "</lyxtabular>\n";
374         end_inset(os);  
375 }
376
377
378 string wrap(string const & cmd, string const & str)
379 {
380         return OPEN + cmd + ' ' + str + CLOSE;
381 }
382
383
384 void end_preamble(ostream & os)
385 {
386         in_preamble = false;
387         os << "# tex2lyx 0.0.2 created this file\n"
388            << "\\lyxformat 222\n"
389            << "\\textclass " << h_textclass << "\n"
390            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
391            << "\\options " << h_options << "\n"
392            << "\\language " << h_language << "\n"
393            << "\\inputencoding " << h_inputencoding << "\n"
394            << "\\fontscheme " << h_fontscheme << "\n"
395            << "\\graphics " << h_graphics << "\n"
396            << "\\paperfontsize " << h_paperfontsize << "\n"
397            << "\\spacing " << h_spacing << "\n"
398            << "\\papersize " << h_papersize << "\n"
399            << "\\paperpackage " << h_paperpackage << "\n"
400            << "\\use_geometry " << h_use_geometry << "\n"
401            << "\\use_amsmath " << h_use_amsmath << "\n"
402            << "\\use_natbib " << h_use_natbib << "\n"
403            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
404            << "\\paperorientation " << h_paperorientation << "\n"
405            << "\\secnumdepth " << h_secnumdepth << "\n"
406            << "\\tocdepth " << h_tocdepth << "\n"
407            << "\\paragraph_separation " << h_paragraph_separation << "\n"
408            << "\\defskip " << h_defskip << "\n"
409            << "\\quotes_language " << h_quotes_language << "\n"
410            << "\\quotes_times " << h_quotes_times << "\n"
411            << "\\papercolumns " << h_papercolumns << "\n"
412            << "\\papersides " << h_papersides << "\n"
413            << "\\paperpagestyle " << h_paperpagestyle << "\n"
414            << "\\tracking_changes " << h_tracking_changes << "\n";
415 }
416
417
418 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
419 {
420         while (p.good()) {
421                 Token const & t = p.getToken();
422
423 #ifdef FILEDEBUG
424                 cerr << "t: " << t << " flags: " << flags << "\n";
425                 //cell->dump();
426 #endif
427
428                 if (flags & FLAG_ITEM) {
429                         if (t.cat() == catSpace)
430                                 continue;
431
432                         flags &= ~FLAG_ITEM;
433                         if (t.cat() == catBegin) {
434                                 // skip the brace and collect everything to the next matching
435                                 // closing brace
436                                 flags |= FLAG_BRACE_LAST;
437                                 continue;
438                         }
439
440                         // handle only this single token, leave the loop if done
441                         flags |= FLAG_LEAVE;
442                 }
443
444
445                 if (flags & FLAG_BRACED) {
446                         if (t.cat() == catSpace)
447                                 continue;
448
449                         if (t.cat() != catBegin) {
450                                 p.error("opening brace expected");
451                                 return;
452                         }
453
454                         // skip the brace and collect everything to the next matching
455                         // closing brace
456                         flags = FLAG_BRACE_LAST;
457                 }
458
459
460                 if (flags & FLAG_OPTION) {
461                         if (t.cat() == catOther && t.character() == '[') {
462                                 parse(p, os, FLAG_BRACK_LAST, mode);
463                         } else {
464                                 // no option found, put back token and we are done
465                                 p.putback();
466                         }
467                         return;
468                 }
469
470                 //
471                 // cat codes
472                 //
473                 if (t.cat() == catMath) {
474                         if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
475                                 // we are inside some text mode thingy, so opening new math is allowed
476                                 if (mode == TEXT_MODE)
477                                         begin_inset(os, "Formula ");
478                                 Token const & n = p.getToken();
479                                 if (n.cat() == catMath) {
480                                         // TeX's $$...$$ syntax for displayed math
481                                         os << "\\[";
482                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
483                                         os << "\\]";
484                                         p.getToken(); // skip the second '$' token
485                                 } else {
486                                         // simple $...$  stuff
487                                         p.putback();
488                                         os << '$';
489                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
490                                         os << '$';
491                                 }
492                                 if (mode == TEXT_MODE)
493                                         end_inset(os);
494                         }
495
496                         else if (flags & FLAG_SIMPLE) {
497                                 // this is the end of the formula
498                                 return;
499                         }
500
501                         else {
502                                 cerr << "mode: " << mode << endl;
503                                 p.error("something strange in the parser\n");
504                                 break;
505                         }
506                 }
507
508                 else if (t.cat() == catLetter)
509                         os << t.character();
510
511                 else if (t.cat() == catSpace) 
512                         os << t.character();
513
514                 else if (t.cat() == catNewline)
515                         os << ' ';
516
517                 else if (t.cat() == catSuper)
518                         os << t.character();
519
520                 else if (t.cat() == catSub)
521                         os << t.character();
522
523                 else if (t.cat() == catParameter) {
524                         Token const & n = p.getToken();
525                         os << wrap("macroarg", string(1, n.character()));
526                 }
527
528                 else if (t.cat() == catActive) {
529                         if (t.character() == '~')
530                                 os << (curr_env() == "lyxcode" ? ' ' : '~');
531                         else
532                                 os << t.asInput();
533                 }
534
535                 else if (t.cat() == catBegin) {
536                         if (mode == MATH_MODE)
537                                 os << '{';
538                         else
539                                 handle_tex(os, "{");
540                 }
541
542                 else if (t.cat() == catEnd) {
543                         if (flags & FLAG_BRACE_LAST)
544                                 return;
545                         if (mode == MATH_MODE)
546                                 os << '}';
547                         else
548                                 handle_tex(os, "}");
549                 }
550
551                 else if (t.cat() == catAlign) {
552                         if (mode == MATH_MODE)
553                                 os << t.character();
554                         else
555                                 os << TAB;
556                 }
557
558                 else if (t.cs() == "tabularnewline") {
559                         if (mode == MATH_MODE)
560                                 os << t.asInput();
561                         else
562                                 os << LINE;
563                 }
564
565                 else if (t.cs() == "\\" && mode == MATH_MODE)
566                         os << t.asInput();
567
568                 else if (t.cs() == "\\" && curr_env() == "tabular")
569                         os << LINE;
570
571                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
572                         //cerr << "finished reading option\n";
573                         return;
574                 }
575
576                 else if (t.cat() == catOther)
577                         os << string(1, t.character());
578
579                 else if (t.cat() == catComment) {
580                         string s;
581                         while (p.good()) {
582                                 Token const & t = p.getToken();
583                                 if (t.cat() == catNewline)
584                                         break;
585                                 s += t.asString();
586                         }
587                         //os << wrap("comment", s);
588                         p.skipSpaces();
589                 }
590
591                 //
592                 // control sequences
593                 //
594
595                 else if (t.cs() == "lyxlock")
596                         ; // ignored
597
598                 else if (t.cs() == "makeatletter") {
599                         p.setCatCode('@', catLetter);
600                         handle_tex(os, "\\makeatletter\n");
601                 }
602                         
603                 else if (t.cs() == "makeatother") {
604                         p.setCatCode('@', catOther);
605                         handle_tex(os, "\\makeatother\n");
606                 }
607
608                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
609                             || t.cs() == "providecommand") {
610                         string const name = p.verbatimItem();
611                         string const opts = p.getOpt();
612                         string const body = p.verbatimItem();
613                         // only non-lyxspecific stuff
614                         if (name != "\\noun " && name != "\\tabularnewline ") {
615                                 ostream & out = in_preamble ? h_preamble : os;
616                                 if (!in_preamble)
617                                         begin_inset(os, "FormulaMacro\n");
618                                 out << "\\" << t.cs() << "{" << name << "}"
619                                     << opts << "{" << body << "}\n";
620                                 if (!in_preamble)
621                                         end_inset(os);
622                         }
623                 }
624
625                 else if (t.cs() == "newtheorem") {
626                         ostringstream ss;
627                         ss << "\\newtheorem";
628                         ss << '{' << p.verbatimItem() << '}';
629                         ss << p.getOpt();
630                         ss << '{' << p.verbatimItem() << '}';
631                         ss << p.getOpt();
632                         ss << '\n';
633                         handle_tex(os, ss.str());
634                 }
635
636                 else if (t.cs() == "(") {
637                         begin_inset(os, "Formula");
638                         os << " \\(";
639                         parse(p, os, FLAG_SIMPLE2, MATH_MODE);
640                         os << "\\)";
641                         end_inset(os);
642                 }
643
644                 else if (t.cs() == "[") {
645                         begin_inset(os, "Formula");
646                         os << " \\[";
647                         parse(p, os, FLAG_EQUATION, MATH_MODE);
648                         os << "\\]";
649                         end_inset(os);
650                 }
651
652                 else if (t.cs() == "protect")
653                         // ignore \\protect, will hopefully be re-added during output
654                         ;
655
656                 else if (t.cs() == "begin") {
657                         string const name = p.getArg('{', '}');
658                         active_environments.push(name);
659                         if (name == "document") {
660                                 end_preamble(os);
661                                 parse(p, os, FLAG_END, mode);
662                         } else if (name == "abstract") {
663                                 handle_par(os);
664                                 parse(p, os, FLAG_END, mode);
665                         } else if (is_math_env(name)) {
666                                 begin_inset(os, "Formula ");    
667                                 os << "\\begin{" << name << "}";
668                                 parse(p, os, FLAG_END, MATH_MODE);
669                                 os << "\\end{" << name << "}";
670                                 end_inset(os);  
671                         } else if (name == "tabular") {
672                                 if (mode == TEXT_MODE)
673                                         handle_tabular(p, os, mode);
674                                 else {
675                                         os << "\\begin{" << name << "}";
676                                         parse(p, os, FLAG_END, MATHTEXT_MODE);
677                                         os << "\\end{" << name << "}";
678                                 }
679                         } else if (name == "table") {
680                                 begin_inset(os, "Float table\n");       
681                                 os << "wide false\n"
682                                    << "collapsed false\n"
683                                    << "\n"
684                                    << "\\layout Standard\n";
685                                 parse(p, os, FLAG_END, mode);
686                                 end_inset(os);  
687                         } else if (name == "thebibliography") {
688                                 p.verbatimItem(); // swallow next arg
689                                 parse(p, os, FLAG_END, mode);
690                                 os << "\n\\layout Standard\n\n";
691                         } else if (mode == MATH_MODE) {
692                                 os << "\\begin{" << name << "}";
693                                 parse(p, os, FLAG_END, mode);
694                                 os << "\\end{" << name << "}";
695                         } else {
696                                 parse(p, os, FLAG_END, mode);
697                         }
698                 }
699
700                 else if (t.cs() == "end") {
701                         if (flags & FLAG_END) {
702                                 // eat environment name
703                                 string const name = p.getArg('{', '}');
704                                 if (name != curr_env())
705                                         p.error("\\end{" + name + "} does not match \\begin{"
706                                                 + curr_env() + "}");
707                                 active_environments.pop();
708                                 return;
709                         }
710                         p.error("found 'end' unexpectedly");
711                 }
712
713
714                 else if (t.cs() == "item")
715                         handle_par(os);
716
717                 else if (t.cs() == ")") {
718                         if (flags & FLAG_SIMPLE2)
719                                 return;
720                         p.error("found '\\)' unexpectedly");
721                 }
722
723                 else if (t.cs() == "]") {
724                         if (flags & FLAG_EQUATION)
725                                 return;
726                         p.error("found '\\]' unexpectedly");
727                 }
728
729                 else if (t.cs() == "documentclass") {
730                         vector<string> opts;
731                         split(p.getArg('[', ']'), opts, ',');
732                         handle_opt(opts, known_languages, h_language); 
733                         handle_opt(opts, known_fontsizes, h_paperfontsize); 
734                         h_quotes_language = h_language;
735                         h_options = join(opts, ',');
736                         h_textclass = p.getArg('{', '}');
737                 }
738
739                 else if (t.cs() == "usepackage") {
740                         string const options = p.getArg('[', ']');
741                         string const name = p.getArg('{', '}');
742                         if (options.empty() && name.find(',')) {
743                                 vector<string> vecnames;
744                                 split(name, vecnames, ',');
745                                 vector<string>::const_iterator it  = vecnames.begin();
746                                 vector<string>::const_iterator end = vecnames.end();
747                                 for (; it != end; ++it)
748                                         handle_package(trim(*it), string());
749                         } else {
750                                 handle_package(name, options);
751                         }
752                 }
753
754                 else if (t.cs() == "newenvironment") {
755                         string const name = p.getArg('{', '}');
756                         if (name != "lyxcode") {
757                                 ostringstream ss;
758                                 ss << "\\newenvironment{" << name << "}";
759                                 ss << p.getOpt();
760                                 ss << p.getOpt();
761                                 ss << '{' << p.verbatimItem() << '}';
762                                 ss << '{' << p.verbatimItem() << '}';
763                                 ss << '\n';
764                                 handle_tex(os, ss.str());
765                         }
766                 }
767
768                 else if (t.cs() == "newfont") {
769                         ostringstream ss;
770                         ss << "\\newfont";
771                         ss << '{' << p.verbatimItem() << '}';
772                         ss << '{' << p.verbatimItem() << '}';
773                         ss << '\n';
774                         handle_tex(os, ss.str());
775                 }
776
777                 else if (t.cs() == "newcounter") {
778                         ostringstream ss;
779                         ss << "\\newcounter";
780                         ss << '{' << p.verbatimItem() << '}';
781                         ss << p.getOpt();
782                         ss << '\n';
783                         handle_tex(os, ss.str());
784                 }
785
786                 else if (t.cs() == "def") {
787                         string name = p.getToken().cs();
788                         while (p.nextToken().cat() != catBegin)
789                                 name += p.getToken().asString();
790                         handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
791                 }
792
793                 else if (t.cs() == "setcounter") {
794                         string const name = p.getArg('{', '}');
795                         string const content = p.getArg('{', '}');
796                         if (name == "secnumdepth") 
797                                 h_secnumdepth = content;
798                         else if (name == "tocdepth") 
799                                 h_tocdepth = content;
800                         else
801                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
802                 }
803
804                 else if (t.cs() == "setlength") {
805                         string const name = p.verbatimItem();
806                         string const content = p.verbatimItem();
807                         if (in_preamble && name == "parskip")
808                                 h_paragraph_separation = "skip";
809                         else if (in_preamble && name == "parindent")
810                                 h_paragraph_separation = "skip";
811                         else 
812                                 handle_tex(os, "\\setlength{" + name + "}{" + content + "}\n");
813                 }
814         
815                 else if (t.cs() == "par")
816                         handle_par(os);
817
818                 else if (is_known(t.cs(), known_headings)) {
819                         string name = t.cs();
820                         if (p.nextToken().asInput() == "*") {
821                                 p.getToken();
822                                 name += "*";
823                         }
824                         os << "\n\n\\layout " << cap(name) << "\n\n";
825                         parse(p, os, FLAG_ITEM, mode);
826                 }
827
828                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
829                         ; // swallow this
830
831                 else if (t.cs() == "tableofcontents")
832                         p.verbatimItem(); // swallow this
833
834                 else if (t.cs() == "multicolumn" && mode == TEXT_MODE) {
835                         // brutish...
836                         parse(p, os, FLAG_ITEM, mode); 
837                         os << MULT;
838                         parse(p, os, FLAG_ITEM, mode); 
839                         os << MULT;
840                         parse(p, os, FLAG_ITEM, mode); 
841                 }
842
843                 else if (t.cs() == "textrm") {
844                         os << '\\' << t.cs() << '{';
845                         parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
846                         os << '}';
847                 }
848
849                 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
850                         os << "\n\\" << t.cs() << " on\n";
851                         parse(p, os, FLAG_ITEM, mode);
852                         os << "\n\\" << t.cs() << " default\n";
853                 }
854
855                 else if (is_known(t.cs(), known_latex_commands) && mode == TEXT_MODE) {
856                         begin_inset(os, "LatexCommand ");
857                         os << '\\' << t.cs() << '{';
858                         parse(p, os, FLAG_ITEM, TEXT_MODE);
859                         os << '}';
860                         end_inset(os);
861                 }
862
863                 else if (t.cs() == "bibitem") {
864                         os << "\n\\layout Bibliography\n\\bibitem ";
865                         string opt = p.getArg('[',']');
866                         if (opt.size())
867                                 os << '[' << opt << ']';
868                         os << '{' << p.getArg('{','}') << '}' << "\n\n";
869                 }
870
871                 else if (char const ** where = is_known(t.cs(), known_quotes)) {
872                         begin_inset(os, "Quotes ");
873                         os << known_coded_quotes[where - known_quotes];
874                         end_inset(os);
875                 }
876
877                 else if (t.cs() == "textasciitilde")
878                         os << '~';
879
880                 else if (t.cs() == "_" && mode == TEXT_MODE)
881                         os << '_';
882
883                 else if (t.cs() == "&" && mode == TEXT_MODE)
884                         os << '&';
885
886                 else if (t.cs() == "input")
887                         handle_tex(os, "\\input{" + p.verbatimItem() + "}\n");
888
889                 else if (t.cs() == "pagestyle" && in_preamble)
890                         h_paperpagestyle == p.getArg('{','}');
891
892                 else if (t.cs() == "fancyhead") {
893                         ostringstream ss;
894                         ss << "\\fancyhead";
895                         ss << p.getOpt();
896                         ss << '{' << p.verbatimItem() << "}\n";
897                         handle_tex(os, ss.str());
898                 }
899
900                 else {
901                         if (mode == MATH_MODE)
902                                 os << t.asInput();
903                         else if (in_preamble)
904                                 h_preamble << t.asInput();
905                         else {
906                                 // heuristic: read up to next non-nested space
907                                 /*
908                                 string s = t.asInput();
909                                 string z = p.verbatimItem();
910                                 while (p.good() && z != " " && z.size()) {
911                                         //cerr << "read: " << z << endl;
912                                         s += z;
913                                         z = p.verbatimItem();
914                                 }
915                                 cerr << "found ERT: " << s << endl;
916                                 handle_ert(os, s + ' ');
917                                 */
918                                 handle_ert(os, t.asInput() + ' ');
919                         }
920                 }
921
922                 if (flags & FLAG_LEAVE) {
923                         flags &= ~FLAG_LEAVE;
924                         break;
925                 }
926         }
927 }
928
929
930
931 } // anonymous namespace
932
933
934 int main(int argc, char * argv[])
935 {
936         if (argc <= 1) {
937                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
938                 return 2;
939         }
940
941         ifstream is(argv[1]);
942         Parser p(is);
943         parse(p, cout, 0, TEXT_MODE);
944         cout << "\n\\the_end";
945
946         return 0;       
947 }       
948
949 // }])