]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/text.C
f2cc658129591e36c78148f7eb26863d41e1bd6e
[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.has_item = true;
444                         context.check_layout(os);
445                         if (s.size())
446                                 os << s << ' ';
447                 }
448
449                 else if (t.cs() == "bibitem") {
450                         context.need_layout = true;
451                         context.has_item = true;
452                         context.check_layout(os);
453                         os << "\\bibitem ";
454                         os << p.getOpt();
455                         os << '{' << p.verbatim_item() << '}' << "\n";
456                 }
457
458                 else if (t.cs() == "def") {
459                         string name = p.get_token().cs();
460                         while (p.next_token().cat() != catBegin)
461                                 name += p.get_token().asString();
462                         handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
463                 }
464
465                 else if (t.cs() == "par") {
466                         p.skip_spaces();
467                         context.check_end_layout(os);
468                         context.need_layout = true;
469                 }
470
471                 // Must attempt to parse "Section*" before "Section".
472                 else if ((p.next_token().asInput() == "*") &&
473                          // The single '=' is meant here.
474                          (newlayout = findLayout(context.textclass,
475                                                  t.cs() + '*')).get() &&
476                          newlayout->isCommand()) {
477                         p.get_token();
478                         output_command_layout(os, p, outer, context, newlayout);
479                 }
480
481                 // The single '=' is meant here.
482                 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
483                          newlayout->isCommand()) {
484                         output_command_layout(os, p, outer, context, newlayout);
485                 }
486
487                 else if (t.cs() == "includegraphics") {
488                         map<string, string> opts = split_map(p.getArg('[', ']'));
489                         string name = p.verbatim_item();
490                         
491                         context.check_layout(os);
492                         begin_inset(os, "Graphics ");
493                         os << "\n\tfilename " << name << '\n';
494                         if (opts.find("width") != opts.end())
495                                 os << "\twidth " << opts["width"] << '\n';
496                         if (opts.find("height") != opts.end())
497                                 os << "\theight " << opts["height"] << '\n';
498                         end_inset(os);
499                 }
500                 
501                 else if (t.cs() == "footnote") {
502                         context.check_layout(os);
503                         begin_inset(os, "Foot\n");
504                         os << "collapsed true\n";
505                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
506                         end_inset(os);
507                 }
508
509                 else if (t.cs() == "marginpar") {
510                         context.check_layout(os);
511                         begin_inset(os, "Marginal\n");
512                         os << "collapsed true\n";
513                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
514                         end_inset(os);
515                 }
516
517                 else if (t.cs() == "ensuremath") {
518                         context.check_layout(os);
519                         Context newcontext(false, context.textclass);
520                         string s = parse_text(p, FLAG_ITEM, false, newcontext);
521                         if (s == "±" || s == "³" || s == "²" || s == "µ")
522                                 os << s;
523                         else
524                                 handle_ert(os, "\\ensuremath{" + s + "}",
525                                            context);
526                 }
527
528                 else if (t.cs() == "hfill") {
529                         context.check_layout(os);
530                         os << "\n\\hfill\n";
531                         skip_braces(p);
532                 }
533
534                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
535                         skip_braces(p); // swallow this
536
537                 else if (t.cs() == "tableofcontents") {
538                         context.check_layout(os);
539                         begin_inset(os, "LatexCommand ");
540                         os << '\\' << t.cs() << "{}\n";
541                         end_inset(os);
542                         skip_braces(p); // swallow this
543                 }
544
545
546                 else if (t.cs() == "textrm") {
547                         context.check_layout(os);
548                         os << "\n\\family roman \n";
549                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
550                         os << "\n\\family default \n";
551                 }
552
553                 else if (t.cs() == "textsf") {
554                         context.check_layout(os);
555                         os << "\n\\family sans \n";
556                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
557                         os << "\n\\family default \n";
558                 }
559
560                 else if (t.cs() == "texttt") {
561                         context.check_layout(os);
562                         os << "\n\\family typewriter \n";
563                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
564                         os << "\n\\family default \n";
565                 }
566
567                 else if (t.cs() == "textit") {
568                         context.check_layout(os);
569                         os << "\n\\shape italic \n";
570                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
571                         os << "\n\\shape default \n";
572                 }
573
574                 else if (t.cs() == "textsc") {
575                         context.check_layout(os);
576                         os << "\n\\noun on \n";
577                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
578                         os << "\n\\noun default \n";
579                 }
580
581                 else if (t.cs() == "textbf") {
582                         context.check_layout(os);
583                         os << "\n\\series bold \n";
584                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
585                         os << "\n\\series default \n";
586                 }
587
588                 else if (t.cs() == "underbar") {
589                         context.check_layout(os);
590                         os << "\n\\bar under \n";
591                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
592                         os << "\n\\bar default \n";
593                 }
594
595                 else if (t.cs() == "emph" || t.cs() == "noun") {
596                         context.check_layout(os);
597                         os << "\n\\" << t.cs() << " on \n";
598                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
599                         os << "\n\\" << t.cs() << " default \n";
600                 }
601
602                 else if (is_known(t.cs(), known_latex_commands)) {
603                         context.check_layout(os);
604                         begin_inset(os, "LatexCommand ");
605                         os << '\\' << t.cs();
606                         os << p.getOpt();
607                         os << p.getOpt();
608                         os << '{' << p.verbatim_item() << "}\n";
609                         end_inset(os);
610                 }
611
612                 else if (is_known(t.cs(), known_quotes)) {
613                   char const ** where = is_known(t.cs(), known_quotes);
614                         begin_inset(os, "Quotes ");
615                         os << known_coded_quotes[where - known_quotes];
616                         end_inset(os);
617                         skip_braces(p);
618                 }
619
620                 else if (is_known(t.cs(), known_sizes)) {
621                         char const ** where = is_known(t.cs(), known_sizes);
622                         context.check_layout(os);
623                         os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
624                 }
625
626                 else if (t.cs() == "LyX" || t.cs() == "TeX" 
627                          || t.cs() == "LaTeX") {
628                         context.check_layout(os);
629                         os << t.cs();
630                         skip_braces(p); // eat {}
631                 }
632
633                 else if (t.cs() == "LaTeXe") {
634                         context.check_layout(os);
635                         os << "LaTeX2e";
636                         skip_braces(p); // eat {}
637                 }
638
639                 else if (t.cs() == "ldots") {
640                         context.check_layout(os);
641                         skip_braces(p);
642                         os << "\\SpecialChar \\ldots{}\n";
643                 }
644
645                 else if (t.cs() == "lyxarrow") {
646                         context.check_layout(os);
647                         os << "\\SpecialChar \\menuseparator\n";
648                         skip_braces(p);
649                 }
650
651                 else if (t.cs() == "ldots") {
652                         context.check_layout(os);
653                         os << "\\SpecialChar \\ldots{}\n";
654                         skip_braces(p);
655                 }
656
657                 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
658                         context.check_layout(os);
659                         os << "\\SpecialChar \\@.\n";
660                         p.get_token();
661                 }
662
663                 else if (t.cs() == "-") {
664                         context.check_layout(os);
665                         os << "\\SpecialChar \\-\n";
666                 }
667
668                 else if (t.cs() == "textasciitilde") {
669                         context.check_layout(os);
670                         os << '~';
671                         skip_braces(p);
672                 }
673
674                 else if (t.cs() == "textasciicircum") {
675                         context.check_layout(os);
676                         os << '^';
677                         skip_braces(p);
678                 }
679
680                 else if (t.cs() == "textbackslash") {
681                         context.check_layout(os);
682                         os << "\n\\backslash \n";
683                         skip_braces(p);
684                 }
685
686                 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#" 
687                             || t.cs() == "$" || t.cs() == "{" || t.cs() == "}" 
688                             || t.cs() == "%") {
689                         context.check_layout(os);
690                         os << t.cs();
691                 }
692
693                 else if (t.cs() == "char") {
694                         context.check_layout(os);
695                         if (p.next_token().character() == '`') {
696                                 p.get_token();
697                                 if (p.next_token().cs() == "\"") {
698                                         p.get_token();
699                                         os << '"';
700                                         skip_braces(p);
701                                 } else {
702                                         handle_ert(os, "\\char`", context);
703                                 }
704                         } else {
705                                 handle_ert(os, "\\char", context);
706                         }
707                 }
708
709                 else if (t.cs() == "\"") {
710                         context.check_layout(os);
711                         string const name = p.verbatim_item();
712                              if (name == "a") os << 'ä';
713                         else if (name == "o") os << 'ö';
714                         else if (name == "u") os << 'ü';
715                         else if (name == "A") os << 'Ä';
716                         else if (name == "O") os << 'Ö';
717                         else if (name == "U") os << 'Ü';
718                         else handle_ert(os, "\"{" + name + "}", context);
719                 }
720
721                 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
722                       || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
723                         // we need the trim as the LyX parser chokes on such spaces
724                         context.check_layout(os);
725                         os << "\n\\i \\" << t.cs() << "{"
726                            << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
727                 }
728
729                 else if (t.cs() == "ss") {
730                         context.check_layout(os);
731                         os << "ß";
732                 }
733
734                 else if (t.cs() == "i" || t.cs() == "j") {
735                         context.check_layout(os);
736                         os << "\\" << t.cs() << ' ';
737                 }
738
739                 else if (t.cs() == "\\") {
740                         context.check_layout(os);
741                         os << "\n\\newline \n";
742                 }
743         
744                 else if (t.cs() == "input") {
745                         context.check_layout(os);
746                         handle_ert(os, "\\input{" + p.verbatim_item() + "}\n",
747                                    context);
748                 }
749                 else if (t.cs() == "fancyhead") {
750                         context.check_layout(os);
751                         ostringstream ss;
752                         ss << "\\fancyhead";
753                         ss << p.getOpt();
754                         ss << '{' << p.verbatim_item() << "}\n";
755                         handle_ert(os, ss.str(), context);
756                 }
757
758                 else {
759                         //cerr << "#: " << t << " mode: " << mode << endl;
760                         // heuristic: read up to next non-nested space
761                         /*
762                         string s = t.asInput();
763                         string z = p.verbatim_item();
764                         while (p.good() && z != " " && z.size()) {
765                                 //cerr << "read: " << z << endl;
766                                 s += z;
767                                 z = p.verbatim_item();
768                         }
769                         cerr << "found ERT: " << s << endl;
770                         handle_ert(os, s + ' ', context);
771                         */
772                         context.check_layout(os);
773                         handle_ert(os, t.asInput() + ' ', context);
774                 }
775
776                 if (flags & FLAG_LEAVE) {
777                         flags &= ~FLAG_LEAVE;
778                         break;
779                 }
780         }
781 }
782
783
784 // }])