]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/text.C
make nesting work in tex2lyx
[lyx.git] / src / tex2lyx / text.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 // {[(
6
7 #include <config.h>
8
9 #include "tex2lyx.h"
10 #include "context.h"
11 #include "FloatList.h"
12 #include "support/lstrings.h"
13 #include "support/tostr.h"
14
15 #include <iostream>
16 #include <map>
17 #include <sstream>
18 #include <vector>
19
20 using std::cerr;
21 using std::endl;
22 using std::map;
23 using std::ostream;
24 using std::ostringstream;
25 using std::string;
26 using std::vector;
27
28 using lyx::support::rtrim;
29 using lyx::support::suffixIs;
30
31
32 // thin wrapper around parse_text using a string
33 string parse_text(Parser & p, unsigned flags, const bool outer,
34                   Context & context)
35 {
36         ostringstream os;
37         parse_text(p, os, flags, outer, context);
38         return os.str();
39 }
40
41 // parses a subdocument, usually useful in insets (whence the name)
42 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
43                 Context & context)
44 {
45         Context newcontext(true, context.textclass);
46         parse_text(p, os, flags, outer, newcontext);
47         newcontext.check_end_layout(os);
48 }
49
50
51 // parses a paragraph snippet, useful for example for \emph{...}
52 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
53                 Context & context)
54 {
55         Context newcontext(false, context.textclass);
56         parse_text(p, os, flags, outer, newcontext);
57         // should not be needed
58         newcontext.check_end_layout(os);
59 }
60
61
62 namespace {
63
64 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
65 "printindex", "pageref", "url", 0 };
66
67 // LaTeX names for quotes
68 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
69 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
70
71 // the same as known_quotes with .lyx names
72 char const * known_coded_quotes[] = { "gld", "grd", "gld",
73 "grd", "gls", "fls", "frd", 0};
74
75 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
76 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
77
78 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
79 "small", "normal", "large", "larger", "largest",  "huge", "giant", 0};
80
81 // splits "x=z, y=b" into a map
82 map<string, string> split_map(string const & s)
83 {
84         map<string, string> res;
85         vector<string> v;
86         split(s, v);
87         for (size_t i = 0; i < v.size(); ++i) {
88                 size_t const pos   = v[i].find('=');
89                 string const index = v[i].substr(0, pos);
90                 string const value = v[i].substr(pos + 1, string::npos);
91                 res[trim(index)] = trim(value);
92         }
93         return res;
94 }
95
96
97 void begin_inset(ostream & os, string const & name)
98 {
99         os << "\n\\begin_inset " << name;
100 }
101
102
103 void end_inset(ostream & os)
104 {
105         os << "\n\\end_inset \n\n";
106 }
107
108
109 void skip_braces(Parser & p)
110 {
111         if (p.next_token().cat() != catBegin)
112                 return;
113         p.get_token();
114         if (p.next_token().cat() == catEnd) {
115                 p.get_token();
116                 return;
117         }
118         p.putback();
119 }
120
121
122 void handle_ert(ostream & os, string const & s, Context const & context)
123 {
124         Context newcontext(true, context.textclass);
125         begin_inset(os, "ERT");
126         os << "\nstatus Collapsed\n";
127         newcontext.check_layout(os);
128         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
129                 if (*it == '\\')
130                         os << "\n\\backslash \n";
131                 else
132                         os << *it;
133         }
134         newcontext.check_end_layout(os);
135         end_inset(os);
136 }
137
138
139 struct isLayout {
140         isLayout(string const name) : name_(name) {}
141         bool operator()(LyXLayout_ptr const & ptr) {
142                 return ptr.get() && ptr->latexname() == name_;
143         }
144 private:
145         string const name_;
146 };
147
148
149 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
150                          string const & name) 
151 {
152         LyXTextClass::const_iterator it  = textclass.begin();
153         LyXTextClass::const_iterator end = textclass.end();
154         it = std::find_if(it, end, isLayout(name));
155         return (it == end) ? LyXLayout_ptr() : *it;
156 }
157
158
159 void output_command_layout(ostream & os, Parser & p, bool outer,
160                            Context & parent_context,
161                            LyXLayout_ptr newlayout)
162 {
163         parent_context.check_end_layout(os);
164         Context context(true, parent_context.textclass, newlayout,
165                         parent_context.layout);
166         context.check_deeper(os);
167         context.check_layout(os);
168         if (context.layout->optionalargs > 0) {
169                 string s; 
170                 if (p.next_token().character() == '[') {
171                         p.get_token(); // eat '['
172                         begin_inset(os, "OptArg\n");
173                         os << "collapsed true\n";
174                         parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
175                         end_inset(os);
176                 }
177         }
178         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
179         context.check_end_layout(os);
180         context.check_end_deeper(os);
181 }
182
183
184 void parse_environment(Parser & p, ostream & os, bool outer,
185                        Context & parent_context)
186 {
187         LyXLayout_ptr newlayout;
188         string const name = p.getArg('{', '}');
189         const bool is_starred = suffixIs(name, '*');
190         string const unstarred_name = rtrim(name, "*");
191         active_environments.push_back(name);
192         if (is_math_env(name)) {
193                 parent_context.check_layout(os);
194                 begin_inset(os, "Formula ");
195                 os << "\\begin{" << name << "}";
196                 parse_math(p, os, FLAG_END, MATH_MODE);
197                 os << "\\end{" << name << "}";
198                 end_inset(os);
199         } else if (name == "tabular") {
200                 parent_context.check_layout(os);
201                 begin_inset(os, "Tabular ");
202                 handle_tabular(p, os, parent_context);
203                 end_inset(os);
204         } else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
205                 parent_context.check_layout(os);
206                 begin_inset(os, "Float " + unstarred_name + "\n");
207                 if (p.next_token().asInput() == "[") {
208                         os << "placement " << p.getArg('[', ']') << '\n';
209                 }
210                 os << "wide " << tostr(is_starred)
211                    << "\ncollapsed false\n";
212                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
213                         end_inset(os);
214         } else if (name == "center") {
215                 parse_text(p, os, FLAG_END, outer, parent_context);
216                 // The single '=' is meant here.
217         } else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
218                    newlayout->isEnvironment()) {
219                 Context context(true, parent_context.textclass, newlayout,
220                                 parent_context.layout);
221                 parent_context.check_end_layout(os);
222                 switch (context.layout->latextype) {
223                 case  LATEX_LIST_ENVIRONMENT:
224                         context.extra_stuff = "\\labelwidthstring "
225                                 + p.verbatim_item() + '\n';
226                         break;
227                 case  LATEX_BIB_ENVIRONMENT:
228                         p.verbatim_item(); // swallow next arg
229                         break;
230                 default:
231                         break;
232                 }
233                 context.check_deeper(os);
234                 parse_text(p, os, FLAG_END, outer, context);
235                 context.check_end_layout(os);
236                 context.check_end_deeper(os);
237         } else {
238                 parent_context.check_layout(os);
239                 handle_ert(os, "\\begin{" + name + "}", parent_context);
240                 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
241                 handle_ert(os, "\\end{" + name + "}", parent_context);
242         }
243 }
244
245 } // anonymous namespace
246
247
248
249
250 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
251                 Context & context)
252 {
253         LyXLayout_ptr newlayout;
254         while (p.good()) {
255                 Token const & t = p.get_token();
256
257 #ifdef FILEDEBUG
258                 cerr << "t: " << t << " flags: " << flags << "\n";
259 #endif
260
261                 if (flags & FLAG_ITEM) {
262                         if (t.cat() == catSpace)
263                                 continue;
264
265                         flags &= ~FLAG_ITEM;
266                         if (t.cat() == catBegin) {
267                                 // skip the brace and collect everything to the next matching
268                                 // closing brace
269                                 flags |= FLAG_BRACE_LAST;
270                                 continue;
271                         }
272
273                         // handle only this single token, leave the loop if done
274                         flags |= FLAG_LEAVE;
275                 }
276
277                 if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) 
278                         return;
279
280                 //
281                 // cat codes
282                 //
283                 if (t.cat() == catMath) {
284                         // we are inside some text mode thingy, so opening new math is allowed
285                         context.check_layout(os);
286                         begin_inset(os, "Formula ");
287                         Token const & n = p.get_token();
288                         if (n.cat() == catMath && outer) {
289                                 // TeX's $$...$$ syntax for displayed math
290                                 os << "\\[";
291                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
292                                 os << "\\]";
293                                 p.get_token(); // skip the second '$' token
294                         } else {
295                                 // simple $...$  stuff
296                                 p.putback();
297                                 os << '$';
298                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
299                                 os << '$';
300                         }
301                         end_inset(os);
302                 }
303
304                 else if (t.cat() == catSuper || t.cat() == catSub)
305                         cerr << "catcode " << t << " illegal in text mode\n";
306
307                 // Basic support for english quotes. This should be
308                 // extended to other quotes, but is not so easy (a
309                 // left english quote is the same as a right german
310                 // quote...)
311                 else if (t.asInput() == "`" 
312                          && p.next_token().asInput() == "`") {
313                         context.check_layout(os);
314                         begin_inset(os, "Quotes ");
315                         os << "eld";
316                         end_inset(os);
317                         p.get_token();
318                         skip_braces(p);
319                 }       
320                 else if (t.asInput() == "'" 
321                          && p.next_token().asInput() == "'") {
322                         context.check_layout(os);
323                         begin_inset(os, "Quotes ");
324                         os << "erd";
325                         end_inset(os);
326                         p.get_token();
327                         skip_braces(p);
328                 }       
329
330
331                 else if (t.cat() == catLetter ||
332                                t.cat() == catSpace ||
333                                t.cat() == catOther ||
334                                t.cat() == catAlign ||
335                                t.cat() == catParameter) {
336                         context.check_layout(os);
337                         os << t.character();
338                 }
339
340                 else if (t.cat() == catNewline) {
341                         if (p.next_token().cat() == catNewline) {
342                                 // this should have been be done by
343                                 // the parser already
344                                 cerr << "what are we doing here?" << endl;
345                                 p.get_token();
346                                 context.need_layout = true;
347                         } else {
348                                 os << " "; // note the space
349                         }
350                 }
351
352                 else if (t.cat() == catActive) {
353                         context.check_layout(os);
354                         if (t.character() == '~') {
355                                 if (context.layout->free_spacing)
356                                         os << ' ';
357                                 else 
358                                         os << "\\InsetSpace ~\n";
359                         } else
360                                 os << t.character();
361                 }
362
363                 else if (t.cat() == catBegin) {
364 // FIXME??? 
365                         // special handling of size changes
366                         context.check_layout(os);
367                         bool const is_size = is_known(p.next_token().cs(), known_sizes);
368                         Context newcontext(false, context.textclass);
369 //                      need_end_layout = false;
370                         string const s = parse_text(p, FLAG_BRACE_LAST, outer, newcontext);
371 //                      need_end_layout = true;
372                         if (s.empty() && p.next_token().character() == '`')
373                                 ; // ignore it in  {}``
374                         else if (is_size || s == "[" || s == "]" || s == "*")
375                                 os << s;
376                         else {
377                                 handle_ert(os, "{", context);
378                                 os << s;
379                                 handle_ert(os, "}", context);
380                         }
381                 }
382
383                 else if (t.cat() == catEnd) {
384                         if (flags & FLAG_BRACE_LAST) {
385                                 context.check_end_layout(os);
386                                 return;
387                         }
388                         cerr << "stray '}' in text\n";
389                         handle_ert(os, "}", context);
390                 }
391
392                 else if (t.cat() == catComment)
393                         handle_comment(p);
394
395                 //
396                 // control sequences
397                 //
398
399                 else if (t.cs() == "(") {
400                         context.check_layout(os);
401                         begin_inset(os, "Formula");
402                         os << " \\(";
403                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
404                         os << "\\)";
405                         end_inset(os);
406                 }
407
408                 else if (t.cs() == "[") {
409                         context.check_layout(os);
410                         begin_inset(os, "Formula");
411                         os << " \\[";
412                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
413                         os << "\\]";
414                         end_inset(os);
415                 }
416
417                 else if (t.cs() == "begin")
418                         parse_environment(p, os, outer, context);
419
420                 else if (t.cs() == "end") {
421                         if (flags & FLAG_END) {
422                                 // eat environment name
423                                 string const name = p.getArg('{', '}');
424                                 if (name != active_environment())
425                                         cerr << "\\end{" + name + "} does not match \\begin{"
426                                                 + active_environment() + "}\n";
427                                 active_environments.pop_back();
428                                 return;
429                         }
430                         p.error("found 'end' unexpectedly");
431                 }
432
433                 else if (t.cs() == "item") {
434                         // should be done automatically by Parser::tokenize
435                         //p.skip_spaces();
436                         string s; 
437                         if (p.next_token().character() == '[') {
438                                 p.get_token(); // eat '['
439                                 Context newcontext(false, context.textclass);
440                                 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
441                         }
442                         context.need_layout = true;
443                         context.check_layout(os);
444                         if (s.size())
445                                 os << s << ' ';
446                 }
447
448                 else if (t.cs() == "def") {
449                         string name = p.get_token().cs();
450                         while (p.next_token().cat() != catBegin)
451                                 name += p.get_token().asString();
452                         handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
453                 }
454
455                 else if (t.cs() == "par") {
456                         p.skip_spaces();
457                         context.check_end_layout(os);
458                         context.need_layout = true;
459                 }
460
461                 // Must attempt to parse "Section*" before "Section".
462                 else if ((p.next_token().asInput() == "*") &&
463                          // The single '=' is meant here.
464                          (newlayout = findLayout(context.textclass,
465                                                  t.cs() + '*')).get() &&
466                          newlayout->isCommand()) {
467                         p.get_token();
468                         output_command_layout(os, p, outer, context, newlayout);
469                 }
470
471                 // The single '=' is meant here.
472                 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
473                          newlayout->isCommand()) {
474                         output_command_layout(os, p, outer, context, newlayout);
475                 }
476
477                 else if (t.cs() == "includegraphics") {
478                         map<string, string> opts = split_map(p.getArg('[', ']'));
479                         string name = p.verbatim_item();
480                         
481                         context.check_layout(os);
482                         begin_inset(os, "Graphics ");
483                         os << "\n\tfilename " << name << '\n';
484                         if (opts.find("width") != opts.end())
485                                 os << "\twidth " << opts["width"] << '\n';
486                         if (opts.find("height") != opts.end())
487                                 os << "\theight " << opts["height"] << '\n';
488                         end_inset(os);
489                 }
490                 
491                 else if (t.cs() == "footnote") {
492                         context.check_layout(os);
493                         begin_inset(os, "Foot\n");
494                         os << "collapsed true\n";
495                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
496                         end_inset(os);
497                 }
498
499                 else if (t.cs() == "marginpar") {
500                         context.check_layout(os);
501                         begin_inset(os, "Marginal\n");
502                         os << "collapsed true\n";
503                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
504                         end_inset(os);
505                 }
506
507                 else if (t.cs() == "ensuremath") {
508                         context.check_layout(os);
509                         Context newcontext(false, context.textclass);
510                         string s = parse_text(p, FLAG_ITEM, false, newcontext);
511                         if (s == "±" || s == "³" || s == "²" || s == "µ")
512                                 os << s;
513                         else
514                                 handle_ert(os, "\\ensuremath{" + s + "}",
515                                            context);
516                 }
517
518                 else if (t.cs() == "hfill") {
519                         context.check_layout(os);
520                         os << "\n\\hfill\n";
521                         skip_braces(p);
522                 }
523
524                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
525                         skip_braces(p); // swallow this
526
527                 else if (t.cs() == "tableofcontents") {
528                         context.check_layout(os);
529                         begin_inset(os, "LatexCommand ");
530                         os << '\\' << t.cs() << "{}\n";
531                         end_inset(os);
532                         skip_braces(p); // swallow this
533                 }
534
535
536                 else if (t.cs() == "textrm") {
537                         context.check_layout(os);
538                         os << "\n\\family roman \n";
539                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
540                         os << "\n\\family default \n";
541                 }
542
543                 else if (t.cs() == "textsf") {
544                         context.check_layout(os);
545                         os << "\n\\family sans \n";
546                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
547                         os << "\n\\family default \n";
548                 }
549
550                 else if (t.cs() == "texttt") {
551                         context.check_layout(os);
552                         os << "\n\\family typewriter \n";
553                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
554                         os << "\n\\family default \n";
555                 }
556
557                 else if (t.cs() == "textit") {
558                         context.check_layout(os);
559                         os << "\n\\shape italic \n";
560                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
561                         os << "\n\\shape default \n";
562                 }
563
564                 else if (t.cs() == "textsc") {
565                         context.check_layout(os);
566                         os << "\n\\noun on \n";
567                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
568                         os << "\n\\noun default \n";
569                 }
570
571                 else if (t.cs() == "textbf") {
572                         context.check_layout(os);
573                         os << "\n\\series bold \n";
574                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
575                         os << "\n\\series default \n";
576                 }
577
578                 else if (t.cs() == "underbar") {
579                         context.check_layout(os);
580                         os << "\n\\bar under \n";
581                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
582                         os << "\n\\bar default \n";
583                 }
584
585                 else if (t.cs() == "emph" || t.cs() == "noun") {
586                         context.check_layout(os);
587                         os << "\n\\" << t.cs() << " on \n";
588                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
589                         os << "\n\\" << t.cs() << " default \n";
590                 }
591
592                 else if (t.cs() == "bibitem") {
593                         context.check_layout(os);
594                         os << "\\bibitem ";
595                         os << p.getOpt();
596                         os << '{' << p.verbatim_item() << '}' << "\n";
597                 }
598
599                 else if (is_known(t.cs(), known_latex_commands)) {
600                         context.check_layout(os);
601                         begin_inset(os, "LatexCommand ");
602                         os << '\\' << t.cs();
603                         os << p.getOpt();
604                         os << p.getOpt();
605                         os << '{' << p.verbatim_item() << "}\n";
606                         end_inset(os);
607                 }
608
609                 else if (is_known(t.cs(), known_quotes)) {
610                   char const ** where = is_known(t.cs(), known_quotes);
611                         begin_inset(os, "Quotes ");
612                         os << known_coded_quotes[where - known_quotes];
613                         end_inset(os);
614                         skip_braces(p);
615                 }
616
617                 else if (is_known(t.cs(), known_sizes)) {
618                         char const ** where = is_known(t.cs(), known_sizes);
619                         context.check_layout(os);
620                         os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
621                 }
622
623                 else if (t.cs() == "LyX" || t.cs() == "TeX" 
624                          || t.cs() == "LaTeX") {
625                         context.check_layout(os);
626                         os << t.cs();
627                         skip_braces(p); // eat {}
628                 }
629
630                 else if (t.cs() == "LaTeXe") {
631                         context.check_layout(os);
632                         os << "LaTeX2e";
633                         skip_braces(p); // eat {}
634                 }
635
636                 else if (t.cs() == "ldots") {
637                         context.check_layout(os);
638                         skip_braces(p);
639                         os << "\\SpecialChar \\ldots{}\n";
640                 }
641
642                 else if (t.cs() == "lyxarrow") {
643                         context.check_layout(os);
644                         os << "\\SpecialChar \\menuseparator\n";
645                         skip_braces(p);
646                 }
647
648                 else if (t.cs() == "ldots") {
649                         context.check_layout(os);
650                         os << "\\SpecialChar \\ldots{}\n";
651                         skip_braces(p);
652                 }
653
654                 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
655                         context.check_layout(os);
656                         os << "\\SpecialChar \\@.\n";
657                         p.get_token();
658                 }
659
660                 else if (t.cs() == "-") {
661                         context.check_layout(os);
662                         os << "\\SpecialChar \\-\n";
663                 }
664
665                 else if (t.cs() == "textasciitilde") {
666                         context.check_layout(os);
667                         os << '~';
668                         skip_braces(p);
669                 }
670
671                 else if (t.cs() == "textasciicircum") {
672                         context.check_layout(os);
673                         os << '^';
674                         skip_braces(p);
675                 }
676
677                 else if (t.cs() == "textbackslash") {
678                         context.check_layout(os);
679                         os << "\n\\backslash \n";
680                         skip_braces(p);
681                 }
682
683                 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#" 
684                             || t.cs() == "$" || t.cs() == "{" || t.cs() == "}" 
685                             || t.cs() == "%") {
686                         context.check_layout(os);
687                         os << t.cs();
688                 }
689
690                 else if (t.cs() == "char") {
691                         context.check_layout(os);
692                         if (p.next_token().character() == '`') {
693                                 p.get_token();
694                                 if (p.next_token().cs() == "\"") {
695                                         p.get_token();
696                                         os << '"';
697                                         skip_braces(p);
698                                 } else {
699                                         handle_ert(os, "\\char`", context);
700                                 }
701                         } else {
702                                 handle_ert(os, "\\char", context);
703                         }
704                 }
705
706                 else if (t.cs() == "\"") {
707                         context.check_layout(os);
708                         string const name = p.verbatim_item();
709                              if (name == "a") os << 'ä';
710                         else if (name == "o") os << 'ö';
711                         else if (name == "u") os << 'ü';
712                         else if (name == "A") os << 'Ä';
713                         else if (name == "O") os << 'Ö';
714                         else if (name == "U") os << 'Ü';
715                         else handle_ert(os, "\"{" + name + "}", context);
716                 }
717
718                 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
719                       || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
720                         // we need the trim as the LyX parser chokes on such spaces
721                         context.check_layout(os);
722                         os << "\n\\i \\" << t.cs() << "{"
723                            << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
724                 }
725
726                 else if (t.cs() == "ss") {
727                         context.check_layout(os);
728                         os << "ß";
729                 }
730
731                 else if (t.cs() == "i" || t.cs() == "j") {
732                         context.check_layout(os);
733                         os << "\\" << t.cs() << ' ';
734                 }
735
736                 else if (t.cs() == "\\") {
737                         context.check_layout(os);
738                         os << "\n\\newline \n";
739                 }
740         
741                 else if (t.cs() == "input") {
742                         context.check_layout(os);
743                         handle_ert(os, "\\input{" + p.verbatim_item() + "}\n",
744                                    context);
745                 }
746                 else if (t.cs() == "fancyhead") {
747                         context.check_layout(os);
748                         ostringstream ss;
749                         ss << "\\fancyhead";
750                         ss << p.getOpt();
751                         ss << '{' << p.verbatim_item() << "}\n";
752                         handle_ert(os, ss.str(), context);
753                 }
754
755                 else {
756                         //cerr << "#: " << t << " mode: " << mode << endl;
757                         // heuristic: read up to next non-nested space
758                         /*
759                         string s = t.asInput();
760                         string z = p.verbatim_item();
761                         while (p.good() && z != " " && z.size()) {
762                                 //cerr << "read: " << z << endl;
763                                 s += z;
764                                 z = p.verbatim_item();
765                         }
766                         cerr << "found ERT: " << s << endl;
767                         handle_ert(os, s + ' ', context);
768                         */
769                         context.check_layout(os);
770                         handle_ert(os, t.asInput() + ' ', context);
771                 }
772
773                 if (flags & FLAG_LEAVE) {
774                         flags &= ~FLAG_LEAVE;
775                         break;
776                 }
777         }
778 }
779
780
781 // }])