]> git.lyx.org Git - features.git/blob - src/tex2lyx/text.cpp
Fix invalid \size output with no active layout before sectioning environments.
[features.git] / src / tex2lyx / text.cpp
1 /**
2  * \file tex2lyx/text.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Jean-Marc Lasgouttes
8  * \author Uwe Stöhr
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 // {[(
14
15 #include <config.h>
16
17 #include "tex2lyx.h"
18
19 #include "Context.h"
20 #include "Encoding.h"
21 #include "FloatList.h"
22 #include "Layout.h"
23 #include "Length.h"
24
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30
31 #include <algorithm>
32 #include <iostream>
33 #include <map>
34 #include <sstream>
35 #include <vector>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44                 Context const & context)
45 {
46         Context newcontext(true, context.textclass);
47         newcontext.font = context.font;
48         parse_text(p, os, flags, outer, newcontext);
49         newcontext.check_end_layout(os);
50 }
51
52
53 namespace {
54
55 /// parses a paragraph snippet, useful for example for \\emph{...}
56 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
57                 Context & context)
58 {
59         Context newcontext(context);
60         // Don't inherit the paragraph-level extra stuff
61         newcontext.par_extra_stuff.clear();
62         parse_text(p, os, flags, outer, newcontext);
63         // Make sure that we don't create invalid .lyx files
64         context.need_layout = newcontext.need_layout;
65         context.need_end_layout = newcontext.need_end_layout;
66 }
67
68
69 /*!
70  * Thin wrapper around parse_text_snippet() using a string.
71  *
72  * We completely ignore \c context.need_layout and \c context.need_end_layout,
73  * because our return value is not used directly (otherwise the stream version
74  * of parse_text_snippet() could be used). That means that the caller needs
75  * to do layout management manually.
76  * This is intended to parse text that does not create any layout changes.
77  */
78 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
79                   Context & context)
80 {
81         Context newcontext(context);
82         newcontext.need_layout = false;
83         newcontext.need_end_layout = false;
84         newcontext.new_layout_allowed = false;
85         // Avoid warning by Context::~Context()
86         newcontext.par_extra_stuff.clear();
87         ostringstream os;
88         parse_text_snippet(p, os, flags, outer, newcontext);
89         return os.str();
90 }
91
92
93 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
94  "vpageref", "prettyref", "eqref", 0 };
95
96 /*!
97  * natbib commands.
98  * The starred forms are also known except for "citefullauthor",
99  * "citeyear" and "citeyearpar".
100  */
101 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
102 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
103 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
104
105 /*!
106  * jurabib commands.
107  * No starred form other than "cite*" known.
108  */
109 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
110 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
111 // jurabib commands not (yet) supported by LyX:
112 // "fullcite",
113 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
114 // "footciteauthor", "footciteyear", "footciteyearpar",
115 "citefield", "citetitle", 0 };
116
117 /// LaTeX names for quotes
118 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
119 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
120 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
121 "guilsinglleft", "frq", "guilsinglright", 0};
122
123 /// the same as known_quotes with .lyx names
124 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
125 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
126 "gld", "grd", "gls", "ers", "fls",
127 "fls", "frs", "frs", 0};
128
129 /// LaTeX names for font sizes
130 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
131 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
132
133 /// the same as known_sizes with .lyx names
134 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
135 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
136
137 /// LaTeX 2.09 names for font families
138 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
139
140 /// LaTeX names for font families
141 char const * const known_font_families[] = { "rmfamily", "sffamily",
142 "ttfamily", 0};
143
144 /// the same as known_old_font_families and known_font_families with .lyx names
145 char const * const known_coded_font_families[] = { "roman", "sans",
146 "typewriter", 0};
147
148 /// LaTeX 2.09 names for font series
149 char const * const known_old_font_series[] = { "bf", 0};
150
151 /// LaTeX names for font series
152 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
153
154 /// the same as known_old_font_series and known_font_series with .lyx names
155 char const * const known_coded_font_series[] = { "bold", "medium", 0};
156
157 /// LaTeX 2.09 names for font shapes
158 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
159
160 /// LaTeX names for font shapes
161 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
162 "upshape", 0};
163
164 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
165 char const * const known_coded_font_shapes[] = { "italic", "slanted",
166 "smallcaps", "up", 0};
167
168 /*!
169  * Graphics file extensions known by the dvips driver of the graphics package.
170  * These extensions are used to complete the filename of an included
171  * graphics file if it does not contain an extension.
172  * The order must be the same that latex uses to find a file, because we
173  * will use the first extension that matches.
174  * This is only an approximation for the common cases. If we would want to
175  * do it right in all cases, we would need to know which graphics driver is
176  * used and know the extensions of every driver of the graphics package.
177  */
178 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
179 "ps.gz", "eps.Z", "ps.Z", 0};
180
181 /*!
182  * Graphics file extensions known by the pdftex driver of the graphics package.
183  * \sa known_dvips_graphics_formats
184  */
185 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
186 "mps", "tif", 0};
187
188 /*!
189  * Known file extensions for TeX files as used by \\include.
190  */
191 char const * const known_tex_extensions[] = {"tex", 0};
192
193 /// spaces known by InsetSpace
194 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
195 "qquad", "enspace", "enskip", "negthinspace", "hfill", "dotfill", "hrulefill",
196 "leftarrowfill", "rightarrowfill", "upbracefill", "downbracefill", 0};
197
198 /// the same as known_spaces with .lyx names
199 char const * const known_coded_spaces[] = { "space{}", "space{}",
200 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
201 "negthinspace{}", "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}",
202 "rightarrowfill{}", "upbracefill{}", "downbracefill{}", 0};
203
204 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
205 /// them in ERT. "LaTeXe" must come before "LaTeX"!
206 char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0};
207 char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0};
208 int const known_phrase_lengths[] = {3, 5, 7, 0};
209
210
211 /// splits "x=z, y=b" into a map and an ordered keyword vector
212 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
213 {
214         vector<string> v;
215         split(s, v);
216         res.clear();
217         keys.resize(v.size());
218         for (size_t i = 0; i < v.size(); ++i) {
219                 size_t const pos   = v[i].find('=');
220                 string const index = trim(v[i].substr(0, pos));
221                 string const value = trim(v[i].substr(pos + 1, string::npos));
222                 res[index] = value;
223                 keys[i] = index;
224         }
225 }
226
227
228 /*!
229  * Split a LaTeX length into value and unit.
230  * The latter can be a real unit like "pt", or a latex length variable
231  * like "\textwidth". The unit may contain additional stuff like glue
232  * lengths, but we don't care, because such lengths are ERT anyway.
233  * \returns true if \p value and \p unit are valid.
234  */
235 bool splitLatexLength(string const & len, string & value, string & unit)
236 {
237         if (len.empty())
238                 return false;
239         const string::size_type i = len.find_first_not_of(" -+0123456789.,");
240         //'4,5' is a valid LaTeX length number. Change it to '4.5'
241         string const length = subst(len, ',', '.');
242         if (i == string::npos)
243                 return false;
244         if (i == 0) {
245                 if (len[0] == '\\') {
246                         // We had something like \textwidth without a factor
247                         value = "1.0";
248                 } else {
249                         return false;
250                 }
251         } else {
252                 value = trim(string(length, 0, i));
253         }
254         if (value == "-")
255                 value = "-1.0";
256         // 'cM' is a valid LaTeX length unit. Change it to 'cm'
257         if (contains(len, '\\'))
258                 unit = trim(string(len, i));
259         else
260                 unit = ascii_lowercase(trim(string(len, i)));
261         return true;
262 }
263
264
265 /// A simple function to translate a latex length to something LyX can
266 /// understand. Not perfect, but rather best-effort.
267 bool translate_len(string const & length, string & valstring, string & unit)
268 {
269         if (!splitLatexLength(length, valstring, unit))
270                 return false;
271         // LyX uses percent values
272         double value;
273         istringstream iss(valstring);
274         iss >> value;
275         value *= 100;
276         ostringstream oss;
277         oss << value;
278         string const percentval = oss.str();
279         // a normal length
280         if (unit.empty() || unit[0] != '\\')
281                 return true;
282         string::size_type const i = unit.find(' ');
283         string const endlen = (i == string::npos) ? string() : string(unit, i);
284         if (unit == "\\textwidth") {
285                 valstring = percentval;
286                 unit = "text%" + endlen;
287         } else if (unit == "\\columnwidth") {
288                 valstring = percentval;
289                 unit = "col%" + endlen;
290         } else if (unit == "\\paperwidth") {
291                 valstring = percentval;
292                 unit = "page%" + endlen;
293         } else if (unit == "\\linewidth") {
294                 valstring = percentval;
295                 unit = "line%" + endlen;
296         } else if (unit == "\\paperheight") {
297                 valstring = percentval;
298                 unit = "pheight%" + endlen;
299         } else if (unit == "\\textheight") {
300                 valstring = percentval;
301                 unit = "theight%" + endlen;
302         }
303         return true;
304 }
305
306 }
307
308
309 string translate_len(string const & length)
310 {
311         string unit;
312         string value;
313         if (translate_len(length, value, unit))
314                 return value + unit;
315         // If the input is invalid, return what we have.
316         return length;
317 }
318
319
320 namespace {
321
322 /*!
323  * Translates a LaTeX length into \p value, \p unit and
324  * \p special parts suitable for a box inset.
325  * The difference from translate_len() is that a box inset knows about
326  * some special "units" that are stored in \p special.
327  */
328 void translate_box_len(string const & length, string & value, string & unit, string & special)
329 {
330         if (translate_len(length, value, unit)) {
331                 if (unit == "\\height" || unit == "\\depth" ||
332                     unit == "\\totalheight" || unit == "\\width") {
333                         special = unit.substr(1);
334                         // The unit is not used, but LyX requires a dummy setting
335                         unit = "in";
336                 } else
337                         special = "none";
338         } else {
339                 value.clear();
340                 unit = length;
341                 special = "none";
342         }
343 }
344
345
346 /*!
347  * Find a file with basename \p name in path \p path and an extension
348  * in \p extensions.
349  */
350 string find_file(string const & name, string const & path,
351                  char const * const * extensions)
352 {
353         for (char const * const * what = extensions; *what; ++what) {
354                 string const trial = addExtension(name, *what);
355                 if (makeAbsPath(trial, path).exists())
356                         return trial;
357         }
358         return string();
359 }
360
361
362 void begin_inset(ostream & os, string const & name)
363 {
364         os << "\n\\begin_inset " << name;
365 }
366
367
368 void begin_command_inset(ostream & os, string const & name,
369                          string const & latexname)
370 {
371         begin_inset(os, "CommandInset ");
372         os << name << "\nLatexCommand " << latexname << '\n';
373 }
374
375
376 void end_inset(ostream & os)
377 {
378         os << "\n\\end_inset\n\n";
379 }
380
381
382 bool skip_braces(Parser & p)
383 {
384         if (p.next_token().cat() != catBegin)
385                 return false;
386         p.get_token();
387         if (p.next_token().cat() == catEnd) {
388                 p.get_token();
389                 return true;
390         }
391         p.putback();
392         return false;
393 }
394
395
396 void handle_ert(ostream & os, string const & s, Context & context)
397 {
398         // We must have a valid layout before outputting the ERT inset.
399         context.check_layout(os);
400         Context newcontext(true, context.textclass);
401         begin_inset(os, "ERT");
402         os << "\nstatus collapsed\n";
403         newcontext.check_layout(os);
404         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
405                 if (*it == '\\')
406                         os << "\n\\backslash\n";
407                 else if (*it == '\n') {
408                         newcontext.new_paragraph(os);
409                         newcontext.check_layout(os);
410                 } else
411                         os << *it;
412         }
413         newcontext.check_end_layout(os);
414         end_inset(os);
415 }
416
417
418 void handle_comment(ostream & os, string const & s, Context & context)
419 {
420         // TODO: Handle this better
421         Context newcontext(true, context.textclass);
422         begin_inset(os, "ERT");
423         os << "\nstatus collapsed\n";
424         newcontext.check_layout(os);
425         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
426                 if (*it == '\\')
427                         os << "\n\\backslash\n";
428                 else
429                         os << *it;
430         }
431         // make sure that our comment is the last thing on the line
432         newcontext.new_paragraph(os);
433         newcontext.check_layout(os);
434         newcontext.check_end_layout(os);
435         end_inset(os);
436 }
437
438
439 Layout const * findLayout(TextClass const & textclass, string const & name)
440 {
441         DocumentClass::const_iterator lit = textclass.begin();
442         DocumentClass::const_iterator len = textclass.end();
443         for (; lit != len; ++lit)
444                 if (lit->latexname() == name)
445                         return &*lit;
446         return 0;
447 }
448
449
450 void eat_whitespace(Parser &, ostream &, Context &, bool);
451
452
453 /*!
454  * Skips whitespace and braces.
455  * This should be called after a command has been parsed that is not put into
456  * ERT, and where LyX adds "{}" if needed.
457  */
458 void skip_spaces_braces(Parser & p, bool keepws = false)
459 {
460         /* The following four examples produce the same typeset output and
461            should be handled by this function:
462            - abc \j{} xyz
463            - abc \j {} xyz
464            - abc \j 
465              {} xyz
466            - abc \j %comment
467              {} xyz
468          */
469         // Unfortunately we need to skip comments, too.
470         // We can't use eat_whitespace since writing them after the {}
471         // results in different output in some cases.
472         bool const skipped_spaces = p.skip_spaces(true);
473         bool const skipped_braces = skip_braces(p);
474         if (keepws && skipped_spaces && !skipped_braces)
475                 // put back the space (it is better handled by check_space)
476                 p.unskip_spaces(true);
477 }
478
479
480 void output_command_layout(ostream & os, Parser & p, bool outer,
481                            Context & parent_context,
482                            Layout const * newlayout)
483 {
484         TeXFont const oldFont = parent_context.font;
485         // save the current font size
486         string const size = oldFont.size;
487         // reset the font size to default, because the font size switches
488         // don't affect section headings and the like
489         parent_context.font.size = Context::normalfont.size;
490         // we only need to write the font change if we have an open layout
491         if (!parent_context.atParagraphStart())
492                 output_font_change(os, oldFont, parent_context.font);
493         parent_context.check_end_layout(os);
494         Context context(true, parent_context.textclass, newlayout,
495                         parent_context.layout, parent_context.font);
496         if (parent_context.deeper_paragraph) {
497                 // We are beginning a nested environment after a
498                 // deeper paragraph inside the outer list environment.
499                 // Therefore we don't need to output a "begin deeper".
500                 context.need_end_deeper = true;
501         }
502         context.check_deeper(os);
503         context.check_layout(os);
504         unsigned int optargs = 0;
505         while (optargs < context.layout->optargs) {
506                 eat_whitespace(p, os, context, false);
507                 if (p.next_token().cat() == catEscape ||
508                     p.next_token().character() != '[') 
509                         break;
510                 p.get_token(); // eat '['
511                 begin_inset(os, "OptArg\n");
512                 os << "status collapsed\n\n";
513                 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
514                 end_inset(os);
515                 eat_whitespace(p, os, context, false);
516                 ++optargs;
517         }
518         unsigned int reqargs = 0;
519         while (LYX_FORMAT >= 392 && reqargs < context.layout->reqargs) {
520                 eat_whitespace(p, os, context, false);
521                 if (p.next_token().cat() != catBegin)
522                         break;
523                 p.get_token(); // eat '{'
524                 begin_inset(os, "OptArg\n");
525                 os << "status collapsed\n\n";
526                 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
527                 end_inset(os);
528                 eat_whitespace(p, os, context, false);
529                 ++reqargs;
530         }
531         parse_text(p, os, FLAG_ITEM, outer, context);
532         context.check_end_layout(os);
533         if (parent_context.deeper_paragraph) {
534                 // We must suppress the "end deeper" because we
535                 // suppressed the "begin deeper" above.
536                 context.need_end_deeper = false;
537         }
538         context.check_end_deeper(os);
539         // We don't need really a new paragraph, but
540         // we must make sure that the next item gets a \begin_layout.
541         parent_context.new_paragraph(os);
542         // Set the font size to the original value. No need to output it here
543         // (Context::begin_layout() will do that if needed)
544         parent_context.font.size = size;
545 }
546
547
548 /*!
549  * Output a space if necessary.
550  * This function gets called for every whitespace token.
551  *
552  * We have three cases here:
553  * 1. A space must be suppressed. Example: The lyxcode case below
554  * 2. A space may be suppressed. Example: Spaces before "\par"
555  * 3. A space must not be suppressed. Example: A space between two words
556  *
557  * We currently handle only 1. and 3 and from 2. only the case of
558  * spaces before newlines as a side effect.
559  *
560  * 2. could be used to suppress as many spaces as possible. This has two effects:
561  * - Reimporting LyX generated LaTeX files changes almost no whitespace
562  * - Superflous whitespace from non LyX generated LaTeX files is removed.
563  * The drawback is that the logic inside the function becomes
564  * complicated, and that is the reason why it is not implemented.
565  */
566 void check_space(Parser & p, ostream & os, Context & context)
567 {
568         Token const next = p.next_token();
569         Token const curr = p.curr_token();
570         // A space before a single newline and vice versa must be ignored
571         // LyX emits a newline before \end{lyxcode}.
572         // This newline must be ignored,
573         // otherwise LyX will add an additional protected space.
574         if (next.cat() == catSpace ||
575             next.cat() == catNewline ||
576             (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
577                 return;
578         }
579         context.check_layout(os);
580         os << ' ';
581 }
582
583
584 /*!
585  * Parse all arguments of \p command
586  */
587 void parse_arguments(string const & command,
588                      vector<ArgumentType> const & template_arguments,
589                      Parser & p, ostream & os, bool outer, Context & context)
590 {
591         string ert = command;
592         size_t no_arguments = template_arguments.size();
593         for (size_t i = 0; i < no_arguments; ++i) {
594                 switch (template_arguments[i]) {
595                 case required:
596                         // This argument contains regular LaTeX
597                         handle_ert(os, ert + '{', context);
598                         eat_whitespace(p, os, context, false);
599                         parse_text(p, os, FLAG_ITEM, outer, context);
600                         ert = "}";
601                         break;
602                 case item:
603                         // This argument consists only of a single item.
604                         // The presence of '{' or not must be preserved.
605                         p.skip_spaces();
606                         if (p.next_token().cat() == catBegin)
607                                 ert += '{' + p.verbatim_item() + '}';
608                         else
609                                 ert += p.verbatim_item();
610                         break;
611                 case verbatim:
612                         // This argument may contain special characters
613                         ert += '{' + p.verbatim_item() + '}';
614                         break;
615                 case optional:
616                         // true because we must not eat whitespace
617                         ert += p.getOpt(true);
618                         break;
619                 }
620         }
621         handle_ert(os, ert, context);
622 }
623
624
625 /*!
626  * Check whether \p command is a known command. If yes,
627  * handle the command with all arguments.
628  * \return true if the command was parsed, false otherwise.
629  */
630 bool parse_command(string const & command, Parser & p, ostream & os,
631                    bool outer, Context & context)
632 {
633         if (known_commands.find(command) != known_commands.end()) {
634                 parse_arguments(command, known_commands[command], p, os,
635                                 outer, context);
636                 return true;
637         }
638         return false;
639 }
640
641
642 /// Parses a minipage or parbox
643 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
644                Context & parent_context, bool use_parbox)
645 {
646         string position;
647         string inner_pos;
648         // We need to set the height to the LaTeX default of 1\\totalheight
649         // for the case when no height argument is given
650         string height_value = "1";
651         string height_unit = "in";
652         string height_special = "totalheight";
653         string latex_height;
654         if (p.hasOpt()) {
655                 position = p.getArg('[', ']');
656                 if (position != "t" && position != "c" && position != "b") {
657                         position = "c";
658                         cerr << "invalid position for minipage/parbox" << endl;
659                 }
660                 if (p.hasOpt()) {
661                         latex_height = p.getArg('[', ']');
662                         translate_box_len(latex_height, height_value, height_unit, height_special);
663
664                         if (p.hasOpt()) {
665                                 inner_pos = p.getArg('[', ']');
666                                 if (inner_pos != "c" && inner_pos != "t" &&
667                                     inner_pos != "b" && inner_pos != "s") {
668                                         inner_pos = position;
669                                         cerr << "invalid inner_pos for minipage/parbox"
670                                              << endl;
671                                 }
672                         }
673                 }
674         }
675         string width_value;
676         string width_unit;
677         string const latex_width = p.verbatim_item();
678         translate_len(latex_width, width_value, width_unit);
679         if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
680                 // LyX can't handle length variables
681                 ostringstream ss;
682                 if (use_parbox)
683                         ss << "\\parbox";
684                 else
685                         ss << "\\begin{minipage}";
686                 if (!position.empty())
687                         ss << '[' << position << ']';
688                 if (!latex_height.empty())
689                         ss << '[' << latex_height << ']';
690                 if (!inner_pos.empty())
691                         ss << '[' << inner_pos << ']';
692                 ss << "{" << latex_width << "}";
693                 if (use_parbox)
694                         ss << '{';
695                 handle_ert(os, ss.str(), parent_context);
696                 parent_context.new_paragraph(os);
697                 parse_text_in_inset(p, os, flags, outer, parent_context);
698                 if (use_parbox)
699                         handle_ert(os, "}", parent_context);
700                 else
701                         handle_ert(os, "\\end{minipage}", parent_context);
702         } else {
703                 // LyX does not like empty positions, so we have
704                 // to set them to the LaTeX default values here.
705                 if (position.empty())
706                         position = "c";
707                 if (inner_pos.empty())
708                         inner_pos = position;
709                 parent_context.check_layout(os);
710                 begin_inset(os, "Box Frameless\n");
711                 os << "position \"" << position << "\"\n";
712                 os << "hor_pos \"c\"\n";
713                 os << "has_inner_box 1\n";
714                 os << "inner_pos \"" << inner_pos << "\"\n";
715                 os << "use_parbox " << use_parbox << "\n";
716                 os << "width \"" << width_value << width_unit << "\"\n";
717                 os << "special \"none\"\n";
718                 os << "height \"" << height_value << height_unit << "\"\n";
719                 os << "height_special \"" << height_special << "\"\n";
720                 os << "status open\n\n";
721                 parse_text_in_inset(p, os, flags, outer, parent_context);
722                 end_inset(os);
723 #ifdef PRESERVE_LAYOUT
724                 // LyX puts a % after the end of the minipage
725                 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
726                         // new paragraph
727                         //handle_comment(os, "%dummy", parent_context);
728                         p.get_token();
729                         p.skip_spaces();
730                         parent_context.new_paragraph(os);
731                 }
732                 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
733                         //handle_comment(os, "%dummy", parent_context);
734                         p.get_token();
735                         p.skip_spaces();
736                         // We add a protected space if something real follows
737                         if (p.good() && p.next_token().cat() != catComment) {
738                                 begin_inset(os, "space ~\n");
739                                 end_inset(os);
740                         }
741                 }
742 #endif
743         }
744 }
745
746
747 /// parse an unknown environment
748 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
749                                unsigned flags, bool outer,
750                                Context & parent_context)
751 {
752         if (name == "tabbing")
753                 // We need to remember that we have to handle '\=' specially
754                 flags |= FLAG_TABBING;
755
756         // We need to translate font changes and paragraphs inside the
757         // environment to ERT if we have a non standard font.
758         // Otherwise things like
759         // \large\begin{foo}\huge bar\end{foo}
760         // will not work.
761         bool const specialfont =
762                 (parent_context.font != parent_context.normalfont);
763         bool const new_layout_allowed = parent_context.new_layout_allowed;
764         if (specialfont)
765                 parent_context.new_layout_allowed = false;
766         handle_ert(os, "\\begin{" + name + "}", parent_context);
767         parse_text_snippet(p, os, flags, outer, parent_context);
768         handle_ert(os, "\\end{" + name + "}", parent_context);
769         if (specialfont)
770                 parent_context.new_layout_allowed = new_layout_allowed;
771 }
772
773
774 void parse_environment(Parser & p, ostream & os, bool outer,
775                        string & last_env, Context & parent_context)
776 {
777         Layout const * newlayout;
778         string const name = p.getArg('{', '}');
779         const bool is_starred = suffixIs(name, '*');
780         string const unstarred_name = rtrim(name, "*");
781         active_environments.push_back(name);
782
783         if (is_math_env(name)) {
784                 parent_context.check_layout(os);
785                 begin_inset(os, "Formula ");
786                 os << "\\begin{" << name << "}";
787                 parse_math(p, os, FLAG_END, MATH_MODE);
788                 os << "\\end{" << name << "}";
789                 end_inset(os);
790         }
791
792         else if (name == "tabular" || name == "longtable") {
793                 eat_whitespace(p, os, parent_context, false);
794                 parent_context.check_layout(os);
795                 begin_inset(os, "Tabular ");
796                 handle_tabular(p, os, name == "longtable", parent_context);
797                 end_inset(os);
798                 p.skip_spaces();
799         }
800
801         else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
802                 eat_whitespace(p, os, parent_context, false);
803                 parent_context.check_layout(os);
804                 begin_inset(os, "Float " + unstarred_name + "\n");
805                 if (p.hasOpt())
806                         os << "placement " << p.getArg('[', ']') << '\n';
807                 os << "wide " << convert<string>(is_starred)
808                    << "\nsideways false"
809                    << "\nstatus open\n\n";
810                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
811                 end_inset(os);
812                 // We don't need really a new paragraph, but
813                 // we must make sure that the next item gets a \begin_layout.
814                 parent_context.new_paragraph(os);
815                 p.skip_spaces();
816         }
817
818         else if (name == "minipage") {
819                 eat_whitespace(p, os, parent_context, false);
820                 parse_box(p, os, FLAG_END, outer, parent_context, false);
821                 p.skip_spaces();
822         }
823
824         else if (name == "comment") {
825                 eat_whitespace(p, os, parent_context, false);
826                 parent_context.check_layout(os);
827                 begin_inset(os, "Note Comment\n");
828                 os << "status open\n";
829                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
830                 end_inset(os);
831                 p.skip_spaces();
832                 skip_braces(p); // eat {} that might by set by LyX behind comments
833         }
834
835         else if (name == "lyxgreyedout") {
836                 eat_whitespace(p, os, parent_context, false);
837                 parent_context.check_layout(os);
838                 begin_inset(os, "Note Greyedout\n");
839                 os << "status open\n";
840                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
841                 end_inset(os);
842                 p.skip_spaces();
843         }
844
845         else if (name == "framed" || name == "shaded") {
846                 eat_whitespace(p, os, parent_context, false);
847                 parent_context.check_layout(os);
848                 if (name == "framed")
849                         begin_inset(os, "Box Framed\n");
850                 else
851                         begin_inset(os, "Box Shaded\n");
852                 os << "position \"t\"\n"
853                       "hor_pos \"c\"\n"
854                       "has_inner_box 0\n"
855                       "inner_pos \"t\"\n"
856                       "use_parbox 0\n"
857                       "width \"100col%\"\n"
858                       "special \"none\"\n"
859                       "height \"1in\"\n"
860                       "height_special \"totalheight\"\n"
861                       "status open\n";
862                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
863                 end_inset(os);
864                 p.skip_spaces();
865         }
866
867         else if (!parent_context.new_layout_allowed)
868                 parse_unknown_environment(p, name, os, FLAG_END, outer,
869                                           parent_context);
870
871         // Alignment and spacing settings
872         // FIXME (bug xxxx): These settings can span multiple paragraphs and
873         //                                       therefore are totally broken!
874         // Note that \centering, raggedright, and raggedleft cannot be handled, as
875         // they are commands not environments. They are furthermore switches that
876         // can be ended by another switches, but also by commands like \footnote or
877         // \parbox. So the only safe way is to leave them untouched.
878         else if (name == "center" || name == "centering" ||
879                  name == "flushleft" || name == "flushright" ||
880                  name == "singlespace" || name == "onehalfspace" ||
881                  name == "doublespace" || name == "spacing") {
882                 eat_whitespace(p, os, parent_context, false);
883                 // We must begin a new paragraph if not already done
884                 if (! parent_context.atParagraphStart()) {
885                         parent_context.check_end_layout(os);
886                         parent_context.new_paragraph(os);
887                 }
888                 if (name == "flushleft")
889                         parent_context.add_extra_stuff("\\align left\n");
890                 else if (name == "flushright")
891                         parent_context.add_extra_stuff("\\align right\n");
892                 else if (name == "center" || name == "centering")
893                         parent_context.add_extra_stuff("\\align center\n");
894                 else if (name == "singlespace")
895                         parent_context.add_extra_stuff("\\paragraph_spacing single\n");
896                 else if (name == "onehalfspace")
897                         parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
898                 else if (name == "doublespace")
899                         parent_context.add_extra_stuff("\\paragraph_spacing double\n");
900                 else if (name == "spacing")
901                         parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
902                 parse_text(p, os, FLAG_END, outer, parent_context);
903                 // Just in case the environment is empty
904                 parent_context.extra_stuff.erase();
905                 // We must begin a new paragraph to reset the alignment
906                 parent_context.new_paragraph(os);
907                 p.skip_spaces();
908         }
909
910         // The single '=' is meant here.
911         else if ((newlayout = findLayout(parent_context.textclass, name)) &&
912                   newlayout->isEnvironment()) {
913                 eat_whitespace(p, os, parent_context, false);
914                 Context context(true, parent_context.textclass, newlayout,
915                                 parent_context.layout, parent_context.font);
916                 if (parent_context.deeper_paragraph) {
917                         // We are beginning a nested environment after a
918                         // deeper paragraph inside the outer list environment.
919                         // Therefore we don't need to output a "begin deeper".
920                         context.need_end_deeper = true;
921                 }
922                 parent_context.check_end_layout(os);
923                 if (last_env == name) {
924                         // we need to output a separator since LyX would export
925                         // the two environments as one otherwise (bug 5716)
926                         docstring const sep = from_ascii("--Separator--");
927                         TeX2LyXDocClass const & textclass(parent_context.textclass);
928                         if (textclass.hasLayout(sep)) {
929                                 Context newcontext(parent_context);
930                                 newcontext.layout = &(textclass[sep]);
931                                 newcontext.check_layout(os);
932                                 newcontext.check_end_layout(os);
933                         } else {
934                                 parent_context.check_layout(os);
935                                 begin_inset(os, "Note Note\n");
936                                 os << "status closed\n";
937                                 Context newcontext(true, textclass,
938                                                 &(textclass.defaultLayout()));
939                                 newcontext.check_layout(os);
940                                 newcontext.check_end_layout(os);
941                                 end_inset(os);
942                                 parent_context.check_end_layout(os);
943                         }
944                 }
945                 switch (context.layout->latextype) {
946                 case  LATEX_LIST_ENVIRONMENT:
947                         context.add_par_extra_stuff("\\labelwidthstring "
948                                                     + p.verbatim_item() + '\n');
949                         p.skip_spaces();
950                         break;
951                 case  LATEX_BIB_ENVIRONMENT:
952                         p.verbatim_item(); // swallow next arg
953                         p.skip_spaces();
954                         break;
955                 default:
956                         break;
957                 }
958                 context.check_deeper(os);
959                 parse_text(p, os, FLAG_END, outer, context);
960                 context.check_end_layout(os);
961                 if (parent_context.deeper_paragraph) {
962                         // We must suppress the "end deeper" because we
963                         // suppressed the "begin deeper" above.
964                         context.need_end_deeper = false;
965                 }
966                 context.check_end_deeper(os);
967                 parent_context.new_paragraph(os);
968                 p.skip_spaces();
969         }
970
971         else if (name == "appendix") {
972                 // This is no good latex style, but it works and is used in some documents...
973                 eat_whitespace(p, os, parent_context, false);
974                 parent_context.check_end_layout(os);
975                 Context context(true, parent_context.textclass, parent_context.layout,
976                                 parent_context.layout, parent_context.font);
977                 context.check_layout(os);
978                 os << "\\start_of_appendix\n";
979                 parse_text(p, os, FLAG_END, outer, context);
980                 context.check_end_layout(os);
981                 p.skip_spaces();
982         }
983
984         else if (known_environments.find(name) != known_environments.end()) {
985                 vector<ArgumentType> arguments = known_environments[name];
986                 // The last "argument" denotes wether we may translate the
987                 // environment contents to LyX
988                 // The default required if no argument is given makes us
989                 // compatible with the reLyXre environment.
990                 ArgumentType contents = arguments.empty() ?
991                         required :
992                         arguments.back();
993                 if (!arguments.empty())
994                         arguments.pop_back();
995                 // See comment in parse_unknown_environment()
996                 bool const specialfont =
997                         (parent_context.font != parent_context.normalfont);
998                 bool const new_layout_allowed =
999                         parent_context.new_layout_allowed;
1000                 if (specialfont)
1001                         parent_context.new_layout_allowed = false;
1002                 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1003                                 outer, parent_context);
1004                 if (contents == verbatim)
1005                         handle_ert(os, p.verbatimEnvironment(name),
1006                                    parent_context);
1007                 else
1008                         parse_text_snippet(p, os, FLAG_END, outer,
1009                                            parent_context);
1010                 handle_ert(os, "\\end{" + name + "}", parent_context);
1011                 if (specialfont)
1012                         parent_context.new_layout_allowed = new_layout_allowed;
1013         }
1014
1015         else
1016                 parse_unknown_environment(p, name, os, FLAG_END, outer,
1017                                           parent_context);
1018
1019         last_env = name;
1020         active_environments.pop_back();
1021 }
1022
1023
1024 /// parses a comment and outputs it to \p os.
1025 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1026 {
1027         LASSERT(t.cat() == catComment, return);
1028         if (!t.cs().empty()) {
1029                 context.check_layout(os);
1030                 handle_comment(os, '%' + t.cs(), context);
1031                 if (p.next_token().cat() == catNewline) {
1032                         // A newline after a comment line starts a new
1033                         // paragraph
1034                         if (context.new_layout_allowed) {
1035                                 if(!context.atParagraphStart())
1036                                         // Only start a new paragraph if not already
1037                                         // done (we might get called recursively)
1038                                         context.new_paragraph(os);
1039                         } else
1040                                 handle_ert(os, "\n", context);
1041                         eat_whitespace(p, os, context, true);
1042                 }
1043         } else {
1044                 // "%\n" combination
1045                 p.skip_spaces();
1046         }
1047 }
1048
1049
1050 /*!
1051  * Reads spaces and comments until the first non-space, non-comment token.
1052  * New paragraphs (double newlines or \\par) are handled like simple spaces
1053  * if \p eatParagraph is true.
1054  * Spaces are skipped, but comments are written to \p os.
1055  */
1056 void eat_whitespace(Parser & p, ostream & os, Context & context,
1057                     bool eatParagraph)
1058 {
1059         while (p.good()) {
1060                 Token const & t = p.get_token();
1061                 if (t.cat() == catComment)
1062                         parse_comment(p, os, t, context);
1063                 else if ((! eatParagraph && p.isParagraph()) ||
1064                          (t.cat() != catSpace && t.cat() != catNewline)) {
1065                         p.putback();
1066                         return;
1067                 }
1068         }
1069 }
1070
1071
1072 /*!
1073  * Set a font attribute, parse text and reset the font attribute.
1074  * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1075  * \param currentvalue Current value of the attribute. Is set to the new
1076  * value during parsing.
1077  * \param newvalue New value of the attribute
1078  */
1079 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1080                            Context & context, string const & attribute,
1081                            string & currentvalue, string const & newvalue)
1082 {
1083         context.check_layout(os);
1084         string const oldvalue = currentvalue;
1085         currentvalue = newvalue;
1086         os << '\n' << attribute << ' ' << newvalue << "\n";
1087         parse_text_snippet(p, os, flags, outer, context);
1088         context.check_layout(os);
1089         os << '\n' << attribute << ' ' << oldvalue << "\n";
1090         currentvalue = oldvalue;
1091 }
1092
1093
1094 /// get the arguments of a natbib or jurabib citation command
1095 void get_cite_arguments(Parser & p, bool natbibOrder,
1096         string & before, string & after)
1097 {
1098         // We need to distinguish "" and "[]", so we can't use p.getOpt().
1099
1100         // text before the citation
1101         before.clear();
1102         // text after the citation
1103         after = p.getFullOpt();
1104
1105         if (!after.empty()) {
1106                 before = p.getFullOpt();
1107                 if (natbibOrder && !before.empty())
1108                         swap(before, after);
1109         }
1110 }
1111
1112
1113 /// Convert filenames with TeX macros and/or quotes to something LyX
1114 /// can understand
1115 string const normalize_filename(string const & name)
1116 {
1117         Parser p(trim(name, "\""));
1118         ostringstream os;
1119         while (p.good()) {
1120                 Token const & t = p.get_token();
1121                 if (t.cat() != catEscape)
1122                         os << t.asInput();
1123                 else if (t.cs() == "lyxdot") {
1124                         // This is used by LyX for simple dots in relative
1125                         // names
1126                         os << '.';
1127                         p.skip_spaces();
1128                 } else if (t.cs() == "space") {
1129                         os << ' ';
1130                         p.skip_spaces();
1131                 } else
1132                         os << t.asInput();
1133         }
1134         return os.str();
1135 }
1136
1137
1138 /// Convert \p name from TeX convention (relative to master file) to LyX
1139 /// convention (relative to .lyx file) if it is relative
1140 void fix_relative_filename(string & name)
1141 {
1142         if (FileName::isAbsolute(name))
1143                 return;
1144
1145         name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1146                                    from_utf8(getParentFilePath())));
1147 }
1148
1149
1150 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1151 void parse_noweb(Parser & p, ostream & os, Context & context)
1152 {
1153         // assemble the rest of the keyword
1154         string name("<<");
1155         bool scrap = false;
1156         while (p.good()) {
1157                 Token const & t = p.get_token();
1158                 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1159                         name += ">>";
1160                         p.get_token();
1161                         scrap = (p.good() && p.next_token().asInput() == "=");
1162                         if (scrap)
1163                                 name += p.get_token().asInput();
1164                         break;
1165                 }
1166                 name += t.asInput();
1167         }
1168
1169         if (!scrap || !context.new_layout_allowed ||
1170             !context.textclass.hasLayout(from_ascii("Scrap"))) {
1171                 cerr << "Warning: Could not interpret '" << name
1172                      << "'. Ignoring it." << endl;
1173                 return;
1174         }
1175
1176         // We use new_paragraph instead of check_end_layout because the stuff
1177         // following the noweb chunk needs to start with a \begin_layout.
1178         // This may create a new paragraph even if there was none in the
1179         // noweb file, but the alternative is an invalid LyX file. Since
1180         // noweb code chunks are implemented with a layout style in LyX they
1181         // always must be in an own paragraph.
1182         context.new_paragraph(os);
1183         Context newcontext(true, context.textclass,
1184                 &context.textclass[from_ascii("Scrap")]);
1185         newcontext.check_layout(os);
1186         os << name;
1187         while (p.good()) {
1188                 Token const & t = p.get_token();
1189                 // We abuse the parser a bit, because this is no TeX syntax
1190                 // at all.
1191                 if (t.cat() == catEscape)
1192                         os << subst(t.asInput(), "\\", "\n\\backslash\n");
1193                 else {
1194                         ostringstream oss;
1195                         begin_inset(oss, "Newline newline");
1196                         end_inset(oss);
1197                         os << subst(t.asInput(), "\n", oss.str());
1198                 }
1199                 // The scrap chunk is ended by an @ at the beginning of a line.
1200                 // After the @ the line may contain a comment and/or
1201                 // whitespace, but nothing else.
1202                 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1203                     (p.next_token().cat() == catSpace ||
1204                      p.next_token().cat() == catNewline ||
1205                      p.next_token().cat() == catComment)) {
1206                         while (p.good() && p.next_token().cat() == catSpace)
1207                                 os << p.get_token().asInput();
1208                         if (p.next_token().cat() == catComment)
1209                                 // The comment includes a final '\n'
1210                                 os << p.get_token().asInput();
1211                         else {
1212                                 if (p.next_token().cat() == catNewline)
1213                                         p.get_token();
1214                                 os << '\n';
1215                         }
1216                         break;
1217                 }
1218         }
1219         newcontext.check_end_layout(os);
1220 }
1221
1222
1223 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1224 bool is_macro(Parser & p)
1225 {
1226         Token first = p.curr_token();
1227         if (first.cat() != catEscape || !p.good())
1228                 return false;
1229         if (first.cs() == "def")
1230                 return true;
1231         if (first.cs() != "global" && first.cs() != "long")
1232                 return false;
1233         Token second = p.get_token();
1234         int pos = 1;
1235         while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1236                second.cat() == catNewline || second.cat() == catComment)) {
1237                 second = p.get_token();
1238                 pos++;
1239         }
1240         bool secondvalid = second.cat() == catEscape;
1241         Token third;
1242         bool thirdvalid = false;
1243         if (p.good() && first.cs() == "global" && secondvalid &&
1244             second.cs() == "long") {
1245                 third = p.get_token();
1246                 pos++;
1247                 while (p.good() && !p.isParagraph() &&
1248                        (third.cat() == catSpace ||
1249                         third.cat() == catNewline ||
1250                         third.cat() == catComment)) {
1251                         third = p.get_token();
1252                         pos++;
1253                 }
1254                 thirdvalid = third.cat() == catEscape;
1255         }
1256         for (int i = 0; i < pos; ++i)
1257                 p.putback();
1258         if (!secondvalid)
1259                 return false;
1260         if (!thirdvalid)
1261                 return (first.cs() == "global" || first.cs() == "long") &&
1262                        second.cs() == "def";
1263         return first.cs() == "global" && second.cs() == "long" &&
1264                third.cs() == "def";
1265 }
1266
1267
1268 /// Parse a macro definition (assumes that is_macro() returned true)
1269 void parse_macro(Parser & p, ostream & os, Context & context)
1270 {
1271         context.check_layout(os);
1272         Token first = p.curr_token();
1273         Token second;
1274         Token third;
1275         string command = first.asInput();
1276         if (first.cs() != "def") {
1277                 p.get_token();
1278                 eat_whitespace(p, os, context, false);
1279                 second = p.curr_token();
1280                 command += second.asInput();
1281                 if (second.cs() != "def") {
1282                         p.get_token();
1283                         eat_whitespace(p, os, context, false);
1284                         third = p.curr_token();
1285                         command += third.asInput();
1286                 }
1287         }
1288         eat_whitespace(p, os, context, false);
1289         string const name = p.get_token().cs();
1290         eat_whitespace(p, os, context, false);
1291
1292         // parameter text
1293         bool simple = true;
1294         string paramtext;
1295         int arity = 0;
1296         while (p.next_token().cat() != catBegin) {
1297                 if (p.next_token().cat() == catParameter) {
1298                         // # found
1299                         p.get_token();
1300                         paramtext += "#";
1301
1302                         // followed by number?
1303                         if (p.next_token().cat() == catOther) {
1304                                 char c = p.getChar();
1305                                 paramtext += c;
1306                                 // number = current arity + 1?
1307                                 if (c == arity + '0' + 1)
1308                                         ++arity;
1309                                 else
1310                                         simple = false;
1311                         } else
1312                                 paramtext += p.get_token().cs();
1313                 } else {
1314                         paramtext += p.get_token().cs();
1315                         simple = false;
1316                 }
1317         }
1318
1319         // only output simple (i.e. compatible) macro as FormulaMacros
1320         string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1321         if (simple) {
1322                 context.check_layout(os);
1323                 begin_inset(os, "FormulaMacro");
1324                 os << "\n\\def" << ert;
1325                 end_inset(os);
1326         } else
1327                 handle_ert(os, command + ert, context);
1328 }
1329
1330 } // anonymous namespace
1331
1332
1333 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1334                 Context & context)
1335 {
1336         Layout const * newlayout = 0;
1337         // Store the latest bibliographystyle and nocite{*} option
1338         // (needed for bibtex inset)
1339         string btprint;
1340         string bibliographystyle;
1341         bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1342         bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1343         string last_env;
1344         while (p.good()) {
1345                 Token const & t = p.get_token();
1346
1347 #ifdef FILEDEBUG
1348                 debugToken(cerr, t, flags);
1349 #endif
1350
1351                 if (flags & FLAG_ITEM) {
1352                         if (t.cat() == catSpace)
1353                                 continue;
1354
1355                         flags &= ~FLAG_ITEM;
1356                         if (t.cat() == catBegin) {
1357                                 // skip the brace and collect everything to the next matching
1358                                 // closing brace
1359                                 flags |= FLAG_BRACE_LAST;
1360                                 continue;
1361                         }
1362
1363                         // handle only this single token, leave the loop if done
1364                         flags |= FLAG_LEAVE;
1365                 }
1366
1367                 if (t.cat() != catEscape && t.character() == ']' &&
1368                     (flags & FLAG_BRACK_LAST))
1369                         return;
1370                 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1371                         return;
1372
1373                 // If there is anything between \end{env} and \begin{env} we
1374                 // don't need to output a separator.
1375                 if (t.cat() != catSpace && t.cat() != catNewline &&
1376                     t.asInput() != "\\begin")
1377                         last_env = "";
1378
1379                 //
1380                 // cat codes
1381                 //
1382                 if (t.cat() == catMath) {
1383                         // we are inside some text mode thingy, so opening new math is allowed
1384                         context.check_layout(os);
1385                         begin_inset(os, "Formula ");
1386                         Token const & n = p.get_token();
1387                         if (n.cat() == catMath && outer) {
1388                                 // TeX's $$...$$ syntax for displayed math
1389                                 os << "\\[";
1390                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1391                                 os << "\\]";
1392                                 p.get_token(); // skip the second '$' token
1393                         } else {
1394                                 // simple $...$  stuff
1395                                 p.putback();
1396                                 os << '$';
1397                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1398                                 os << '$';
1399                         }
1400                         end_inset(os);
1401                 }
1402
1403                 else if (t.cat() == catSuper || t.cat() == catSub)
1404                         cerr << "catcode " << t << " illegal in text mode\n";
1405
1406                 // Basic support for english quotes. This should be
1407                 // extended to other quotes, but is not so easy (a
1408                 // left english quote is the same as a right german
1409                 // quote...)
1410                 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1411                         context.check_layout(os);
1412                         begin_inset(os, "Quotes ");
1413                         os << "eld";
1414                         end_inset(os);
1415                         p.get_token();
1416                         skip_braces(p);
1417                 }
1418                 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1419                         context.check_layout(os);
1420                         begin_inset(os, "Quotes ");
1421                         os << "erd";
1422                         end_inset(os);
1423                         p.get_token();
1424                         skip_braces(p);
1425                 }
1426
1427                 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1428                         context.check_layout(os);
1429                         begin_inset(os, "Quotes ");
1430                         os << "ald";
1431                         end_inset(os);
1432                         p.get_token();
1433                         skip_braces(p);
1434                 }
1435
1436                 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1437                         context.check_layout(os);
1438                         begin_inset(os, "Quotes ");
1439                         os << "ard";
1440                         end_inset(os);
1441                         p.get_token();
1442                         skip_braces(p);
1443                 }
1444
1445                 else if (t.asInput() == "<"
1446                          && p.next_token().asInput() == "<" && noweb_mode) {
1447                         p.get_token();
1448                         parse_noweb(p, os, context);
1449                 }
1450
1451                 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1452                         check_space(p, os, context);
1453
1454                 else if (t.character() == '[' && noweb_mode &&
1455                          p.next_token().character() == '[') {
1456                         // These can contain underscores
1457                         p.putback();
1458                         string const s = p.getFullOpt() + ']';
1459                         if (p.next_token().character() == ']')
1460                                 p.get_token();
1461                         else
1462                                 cerr << "Warning: Inserting missing ']' in '"
1463                                      << s << "'." << endl;
1464                         handle_ert(os, s, context);
1465                 }
1466
1467                 else if (t.cat() == catLetter) {
1468                         context.check_layout(os);
1469                         // Workaround for bug 4752.
1470                         // FIXME: This whole code block needs to be removed
1471                         //        when the bug is fixed and tex2lyx produces
1472                         //        the updated file format.
1473                         // The replacement algorithm in LyX is so stupid that
1474                         // it even translates a phrase if it is part of a word.
1475                         bool handled = false;
1476                         for (int const * l = known_phrase_lengths; *l; ++l) {
1477                                 string phrase = t.cs();
1478                                 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1479                                         phrase += p.get_token().cs();
1480                                 if (is_known(phrase, known_coded_phrases)) {
1481                                         handle_ert(os, phrase, context);
1482                                         handled = true;
1483                                         break;
1484                                 } else {
1485                                         for (size_t i = 1; i < phrase.length(); ++i)
1486                                                 p.putback();
1487                                 }
1488                         }
1489                         if (!handled)
1490                                 os << t.cs();
1491                 }
1492
1493                 else if (t.cat() == catOther ||
1494                                t.cat() == catAlign ||
1495                                t.cat() == catParameter) {
1496                         // This translates "&" to "\\&" which may be wrong...
1497                         context.check_layout(os);
1498                         os << t.cs();
1499                 }
1500
1501                 else if (p.isParagraph()) {
1502                         if (context.new_layout_allowed)
1503                                 context.new_paragraph(os);
1504                         else
1505                                 handle_ert(os, "\\par ", context);
1506                         eat_whitespace(p, os, context, true);
1507                 }
1508
1509                 else if (t.cat() == catActive) {
1510                         context.check_layout(os);
1511                         if (t.character() == '~') {
1512                                 if (context.layout->free_spacing)
1513                                         os << ' ';
1514                                 else {
1515                                         begin_inset(os, "space ~\n");
1516                                         end_inset(os);
1517                                 }
1518                         } else
1519                                 os << t.cs();
1520                 }
1521
1522                 else if (t.cat() == catBegin &&
1523                          p.next_token().cat() == catEnd) {
1524                         // {}
1525                         Token const prev = p.prev_token();
1526                         p.get_token();
1527                         if (p.next_token().character() == '`' ||
1528                             (prev.character() == '-' &&
1529                              p.next_token().character() == '-'))
1530                                 ; // ignore it in {}`` or -{}-
1531                         else
1532                                 handle_ert(os, "{}", context);
1533
1534                 }
1535
1536                 else if (t.cat() == catBegin) {
1537                         context.check_layout(os);
1538                         // special handling of font attribute changes
1539                         Token const prev = p.prev_token();
1540                         Token const next = p.next_token();
1541                         TeXFont const oldFont = context.font;
1542                         if (next.character() == '[' ||
1543                             next.character() == ']' ||
1544                             next.character() == '*') {
1545                                 p.get_token();
1546                                 if (p.next_token().cat() == catEnd) {
1547                                         os << next.cs();
1548                                         p.get_token();
1549                                 } else {
1550                                         p.putback();
1551                                         handle_ert(os, "{", context);
1552                                         parse_text_snippet(p, os,
1553                                                         FLAG_BRACE_LAST,
1554                                                         outer, context);
1555                                         handle_ert(os, "}", context);
1556                                 }
1557                         } else if (! context.new_layout_allowed) {
1558                                 handle_ert(os, "{", context);
1559                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1560                                                    outer, context);
1561                                 handle_ert(os, "}", context);
1562                         } else if (is_known(next.cs(), known_sizes)) {
1563                                 // next will change the size, so we must
1564                                 // reset it here
1565                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1566                                                    outer, context);
1567                                 if (!context.atParagraphStart())
1568                                         os << "\n\\size "
1569                                            << context.font.size << "\n";
1570                         } else if (is_known(next.cs(), known_font_families)) {
1571                                 // next will change the font family, so we
1572                                 // must reset it here
1573                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1574                                                    outer, context);
1575                                 if (!context.atParagraphStart())
1576                                         os << "\n\\family "
1577                                            << context.font.family << "\n";
1578                         } else if (is_known(next.cs(), known_font_series)) {
1579                                 // next will change the font series, so we
1580                                 // must reset it here
1581                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1582                                                    outer, context);
1583                                 if (!context.atParagraphStart())
1584                                         os << "\n\\series "
1585                                            << context.font.series << "\n";
1586                         } else if (is_known(next.cs(), known_font_shapes)) {
1587                                 // next will change the font shape, so we
1588                                 // must reset it here
1589                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1590                                                    outer, context);
1591                                 if (!context.atParagraphStart())
1592                                         os << "\n\\shape "
1593                                            << context.font.shape << "\n";
1594                         } else if (is_known(next.cs(), known_old_font_families) ||
1595                                    is_known(next.cs(), known_old_font_series) ||
1596                                    is_known(next.cs(), known_old_font_shapes)) {
1597                                 // next will change the font family, series
1598                                 // and shape, so we must reset it here
1599                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1600                                                    outer, context);
1601                                 if (!context.atParagraphStart())
1602                                         os <<  "\n\\family "
1603                                            << context.font.family
1604                                            << "\n\\series "
1605                                            << context.font.series
1606                                            << "\n\\shape "
1607                                            << context.font.shape << "\n";
1608                         } else {
1609                                 handle_ert(os, "{", context);
1610                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1611                                                    outer, context);
1612                                 handle_ert(os, "}", context);
1613                         }
1614                 }
1615
1616                 else if (t.cat() == catEnd) {
1617                         if (flags & FLAG_BRACE_LAST) {
1618                                 return;
1619                         }
1620                         cerr << "stray '}' in text\n";
1621                         handle_ert(os, "}", context);
1622                 }
1623
1624                 else if (t.cat() == catComment)
1625                         parse_comment(p, os, t, context);
1626
1627                 //
1628                 // control sequences
1629                 //
1630
1631                 else if (t.cs() == "(") {
1632                         context.check_layout(os);
1633                         begin_inset(os, "Formula");
1634                         os << " \\(";
1635                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1636                         os << "\\)";
1637                         end_inset(os);
1638                 }
1639
1640                 else if (t.cs() == "[") {
1641                         context.check_layout(os);
1642                         begin_inset(os, "Formula");
1643                         os << " \\[";
1644                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1645                         os << "\\]";
1646                         end_inset(os);
1647                 }
1648
1649                 else if (t.cs() == "begin")
1650                         parse_environment(p, os, outer, last_env, context);
1651
1652                 else if (t.cs() == "end") {
1653                         if (flags & FLAG_END) {
1654                                 // eat environment name
1655                                 string const name = p.getArg('{', '}');
1656                                 if (name != active_environment())
1657                                         cerr << "\\end{" + name + "} does not match \\begin{"
1658                                                 + active_environment() + "}\n";
1659                                 return;
1660                         }
1661                         p.error("found 'end' unexpectedly");
1662                 }
1663
1664                 else if (t.cs() == "item") {
1665                         p.skip_spaces();
1666                         string s;
1667                         bool optarg = false;
1668                         if (p.next_token().cat() != catEscape &&
1669                             p.next_token().character() == '[') {
1670                                 p.get_token(); // eat '['
1671                                 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1672                                                        outer, context);
1673                                 optarg = true;
1674                         }
1675                         context.set_item();
1676                         context.check_layout(os);
1677                         if (context.has_item) {
1678                                 // An item in an unknown list-like environment
1679                                 // FIXME: Do this in check_layout()!
1680                                 context.has_item = false;
1681                                 if (optarg)
1682                                         handle_ert(os, "\\item", context);
1683                                 else
1684                                         handle_ert(os, "\\item ", context);
1685                         }
1686                         if (optarg) {
1687                                 if (context.layout->labeltype != LABEL_MANUAL) {
1688                                         // LyX does not support \item[\mybullet]
1689                                         // in itemize environments
1690                                         handle_ert(os, "[", context);
1691                                         os << s;
1692                                         handle_ert(os, "]", context);
1693                                 } else if (!s.empty()) {
1694                                         // The space is needed to separate the
1695                                         // item from the rest of the sentence.
1696                                         os << s << ' ';
1697                                         eat_whitespace(p, os, context, false);
1698                                 }
1699                         }
1700                 }
1701
1702                 else if (t.cs() == "bibitem") {
1703                         context.set_item();
1704                         context.check_layout(os);
1705                         begin_command_inset(os, "bibitem", "bibitem");
1706                         os << "label \"" << p.getOptContent() << "\"\n";
1707                         os << "key \"" << p.verbatim_item() << "\"\n";
1708                         end_inset(os);
1709                 }
1710
1711                 else if (is_macro(p))
1712                         parse_macro(p, os, context);
1713
1714                 else if (t.cs() == "noindent") {
1715                         p.skip_spaces();
1716                         context.add_par_extra_stuff("\\noindent\n");
1717                 }
1718
1719                 else if (t.cs() == "appendix") {
1720                         context.add_par_extra_stuff("\\start_of_appendix\n");
1721                         // We need to start a new paragraph. Otherwise the
1722                         // appendix in 'bla\appendix\chapter{' would start
1723                         // too late.
1724                         context.new_paragraph(os);
1725                         // We need to make sure that the paragraph is
1726                         // generated even if it is empty. Otherwise the
1727                         // appendix in '\par\appendix\par\chapter{' would
1728                         // start too late.
1729                         context.check_layout(os);
1730                         // FIXME: This is a hack to prevent paragraph
1731                         // deletion if it is empty. Handle this better!
1732                         handle_comment(os,
1733                                 "%dummy comment inserted by tex2lyx to "
1734                                 "ensure that this paragraph is not empty",
1735                                 context);
1736                         // Both measures above may generate an additional
1737                         // empty paragraph, but that does not hurt, because
1738                         // whitespace does not matter here.
1739                         eat_whitespace(p, os, context, true);
1740                 }
1741
1742                 // Starred section headings
1743                 // Must attempt to parse "Section*" before "Section".
1744                 else if ((p.next_token().asInput() == "*") &&
1745                          context.new_layout_allowed &&
1746                          (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1747                          newlayout->isCommand()) {
1748                         // write the layout
1749                         p.get_token();
1750                         output_command_layout(os, p, outer, context, newlayout);
1751                         p.skip_spaces();
1752                 }
1753
1754                 // Section headings and the like
1755                 else if (context.new_layout_allowed &&
1756                          (newlayout = findLayout(context.textclass, t.cs())) &&
1757                          newlayout->isCommand()) {
1758                         // write the layout
1759                         output_command_layout(os, p, outer, context, newlayout);
1760                         p.skip_spaces();
1761                 }
1762
1763                 else if (t.cs() == "caption") {
1764                         p.skip_spaces();
1765                         context.check_layout(os);
1766                         p.skip_spaces();
1767                         begin_inset(os, "Caption\n\n");
1768                         Context newcontext(true, context.textclass);
1769                         newcontext.font = context.font;
1770                         newcontext.check_layout(os);
1771                         if (p.next_token().cat() != catEscape &&
1772                             p.next_token().character() == '[') {
1773                                 p.get_token(); // eat '['
1774                                 begin_inset(os, "OptArg\n");
1775                                 os << "status collapsed\n";
1776                                 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1777                                 end_inset(os);
1778                                 eat_whitespace(p, os, context, false);
1779                         }
1780                         parse_text(p, os, FLAG_ITEM, outer, context);
1781                         context.check_end_layout(os);
1782                         // We don't need really a new paragraph, but
1783                         // we must make sure that the next item gets a \begin_layout.
1784                         context.new_paragraph(os);
1785                         end_inset(os);
1786                         p.skip_spaces();
1787                         newcontext.check_end_layout(os);
1788                 }
1789
1790                 else if (t.cs() == "includegraphics") {
1791                         bool const clip = p.next_token().asInput() == "*";
1792                         if (clip)
1793                                 p.get_token();
1794                         string const arg = p.getArg('[', ']');
1795                         map<string, string> opts;
1796                         vector<string> keys;
1797                         split_map(arg, opts, keys);
1798                         if (clip)
1799                                 opts["clip"] = string();
1800                         string name = normalize_filename(p.verbatim_item());
1801
1802                         string const path = getMasterFilePath();
1803                         // We want to preserve relative / absolute filenames,
1804                         // therefore path is only used for testing
1805                         if (!makeAbsPath(name, path).exists()) {
1806                                 // The file extension is probably missing.
1807                                 // Now try to find it out.
1808                                 string const dvips_name =
1809                                         find_file(name, path,
1810                                                   known_dvips_graphics_formats);
1811                                 string const pdftex_name =
1812                                         find_file(name, path,
1813                                                   known_pdftex_graphics_formats);
1814                                 if (!dvips_name.empty()) {
1815                                         if (!pdftex_name.empty()) {
1816                                                 cerr << "This file contains the "
1817                                                         "latex snippet\n"
1818                                                         "\"\\includegraphics{"
1819                                                      << name << "}\".\n"
1820                                                         "However, files\n\""
1821                                                      << dvips_name << "\" and\n\""
1822                                                      << pdftex_name << "\"\n"
1823                                                         "both exist, so I had to make a "
1824                                                         "choice and took the first one.\n"
1825                                                         "Please move the unwanted one "
1826                                                         "someplace else and try again\n"
1827                                                         "if my choice was wrong."
1828                                                      << endl;
1829                                         }
1830                                         name = dvips_name;
1831                                 } else if (!pdftex_name.empty()) {
1832                                         name = pdftex_name;
1833                                         pdflatex = true;
1834                                 }
1835                         }
1836
1837                         if (makeAbsPath(name, path).exists())
1838                                 fix_relative_filename(name);
1839                         else
1840                                 cerr << "Warning: Could not find graphics file '"
1841                                      << name << "'." << endl;
1842
1843                         context.check_layout(os);
1844                         begin_inset(os, "Graphics ");
1845                         os << "\n\tfilename " << name << '\n';
1846                         if (opts.find("width") != opts.end())
1847                                 os << "\twidth "
1848                                    << translate_len(opts["width"]) << '\n';
1849                         if (opts.find("height") != opts.end())
1850                                 os << "\theight "
1851                                    << translate_len(opts["height"]) << '\n';
1852                         if (opts.find("scale") != opts.end()) {
1853                                 istringstream iss(opts["scale"]);
1854                                 double val;
1855                                 iss >> val;
1856                                 val = val*100;
1857                                 os << "\tscale " << val << '\n';
1858                         }
1859                         if (opts.find("angle") != opts.end()) {
1860                                 os << "\trotateAngle "
1861                                    << opts["angle"] << '\n';
1862                                 vector<string>::const_iterator a =
1863                                         find(keys.begin(), keys.end(), "angle");
1864                                 vector<string>::const_iterator s =
1865                                         find(keys.begin(), keys.end(), "width");
1866                                 if (s == keys.end())
1867                                         s = find(keys.begin(), keys.end(), "height");
1868                                 if (s == keys.end())
1869                                         s = find(keys.begin(), keys.end(), "scale");
1870                                 if (s != keys.end() && distance(s, a) > 0)
1871                                         os << "\tscaleBeforeRotation\n";
1872                         }
1873                         if (opts.find("origin") != opts.end()) {
1874                                 ostringstream ss;
1875                                 string const opt = opts["origin"];
1876                                 if (opt.find('l') != string::npos) ss << "left";
1877                                 if (opt.find('r') != string::npos) ss << "right";
1878                                 if (opt.find('c') != string::npos) ss << "center";
1879                                 if (opt.find('t') != string::npos) ss << "Top";
1880                                 if (opt.find('b') != string::npos) ss << "Bottom";
1881                                 if (opt.find('B') != string::npos) ss << "Baseline";
1882                                 if (!ss.str().empty())
1883                                         os << "\trotateOrigin " << ss.str() << '\n';
1884                                 else
1885                                         cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1886                         }
1887                         if (opts.find("keepaspectratio") != opts.end())
1888                                 os << "\tkeepAspectRatio\n";
1889                         if (opts.find("clip") != opts.end())
1890                                 os << "\tclip\n";
1891                         if (opts.find("draft") != opts.end())
1892                                 os << "\tdraft\n";
1893                         if (opts.find("bb") != opts.end())
1894                                 os << "\tBoundingBox "
1895                                    << opts["bb"] << '\n';
1896                         int numberOfbbOptions = 0;
1897                         if (opts.find("bbllx") != opts.end())
1898                                 numberOfbbOptions++;
1899                         if (opts.find("bblly") != opts.end())
1900                                 numberOfbbOptions++;
1901                         if (opts.find("bburx") != opts.end())
1902                                 numberOfbbOptions++;
1903                         if (opts.find("bbury") != opts.end())
1904                                 numberOfbbOptions++;
1905                         if (numberOfbbOptions == 4)
1906                                 os << "\tBoundingBox "
1907                                    << opts["bbllx"] << " " << opts["bblly"] << " "
1908                                    << opts["bburx"] << " " << opts["bbury"] << '\n';
1909                         else if (numberOfbbOptions > 0)
1910                                 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1911                         numberOfbbOptions = 0;
1912                         if (opts.find("natwidth") != opts.end())
1913                                 numberOfbbOptions++;
1914                         if (opts.find("natheight") != opts.end())
1915                                 numberOfbbOptions++;
1916                         if (numberOfbbOptions == 2)
1917                                 os << "\tBoundingBox 0bp 0bp "
1918                                    << opts["natwidth"] << " " << opts["natheight"] << '\n';
1919                         else if (numberOfbbOptions > 0)
1920                                 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1921                         ostringstream special;
1922                         if (opts.find("hiresbb") != opts.end())
1923                                 special << "hiresbb,";
1924                         if (opts.find("trim") != opts.end())
1925                                 special << "trim,";
1926                         if (opts.find("viewport") != opts.end())
1927                                 special << "viewport=" << opts["viewport"] << ',';
1928                         if (opts.find("totalheight") != opts.end())
1929                                 special << "totalheight=" << opts["totalheight"] << ',';
1930                         if (opts.find("type") != opts.end())
1931                                 special << "type=" << opts["type"] << ',';
1932                         if (opts.find("ext") != opts.end())
1933                                 special << "ext=" << opts["ext"] << ',';
1934                         if (opts.find("read") != opts.end())
1935                                 special << "read=" << opts["read"] << ',';
1936                         if (opts.find("command") != opts.end())
1937                                 special << "command=" << opts["command"] << ',';
1938                         string s_special = special.str();
1939                         if (!s_special.empty()) {
1940                                 // We had special arguments. Remove the trailing ','.
1941                                 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1942                         }
1943                         // TODO: Handle the unknown settings better.
1944                         // Warn about invalid options.
1945                         // Check whether some option was given twice.
1946                         end_inset(os);
1947                 }
1948
1949                 else if (t.cs() == "footnote" ||
1950                          (t.cs() == "thanks" && context.layout->intitle)) {
1951                         p.skip_spaces();
1952                         context.check_layout(os);
1953                         begin_inset(os, "Foot\n");
1954                         os << "status collapsed\n\n";
1955                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1956                         end_inset(os);
1957                 }
1958
1959                 else if (t.cs() == "marginpar") {
1960                         p.skip_spaces();
1961                         context.check_layout(os);
1962                         begin_inset(os, "Marginal\n");
1963                         os << "status collapsed\n\n";
1964                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1965                         end_inset(os);
1966                 }
1967
1968                 else if (t.cs() == "ensuremath") {
1969                         p.skip_spaces();
1970                         context.check_layout(os);
1971                         string const s = p.verbatim_item();
1972                         //FIXME: this never triggers in UTF8
1973                         if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1974                                 os << s;
1975                         else
1976                                 handle_ert(os, "\\ensuremath{" + s + "}",
1977                                            context);
1978                 }
1979
1980                 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1981                         // FIXME: Somehow prevent title layouts if
1982                         // "maketitle" was not found
1983                         // swallow this
1984                         skip_spaces_braces(p);
1985                 }
1986
1987                 else if (t.cs() == "tableofcontents") {
1988                         context.check_layout(os);
1989                         begin_command_inset(os, "toc", "tableofcontents");
1990                         end_inset(os);
1991                         skip_spaces_braces(p);
1992                 }
1993
1994                 else if (t.cs() == "listoffigures") {
1995                         context.check_layout(os);
1996                         begin_inset(os, "FloatList figure\n");
1997                         end_inset(os);
1998                         skip_spaces_braces(p);
1999                 }
2000
2001                 else if (t.cs() == "listoftables") {
2002                         context.check_layout(os);
2003                         begin_inset(os, "FloatList table\n");
2004                         end_inset(os);
2005                         skip_spaces_braces(p);
2006                 }
2007
2008                 else if (t.cs() == "listof") {
2009                         p.skip_spaces(true);
2010                         string const name = p.get_token().cs();
2011                         if (context.textclass.floats().typeExist(name)) {
2012                                 context.check_layout(os);
2013                                 begin_inset(os, "FloatList ");
2014                                 os << name << "\n";
2015                                 end_inset(os);
2016                                 p.get_token(); // swallow second arg
2017                         } else
2018                                 handle_ert(os, "\\listof{" + name + "}", context);
2019                 }
2020
2021                 else if (t.cs() == "textrm")
2022                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2023                                               context, "\\family",
2024                                               context.font.family, "roman");
2025
2026                 else if (t.cs() == "textsf")
2027                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2028                                               context, "\\family",
2029                                               context.font.family, "sans");
2030
2031                 else if (t.cs() == "texttt")
2032                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2033                                               context, "\\family",
2034                                               context.font.family, "typewriter");
2035
2036                 else if (t.cs() == "textmd")
2037                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2038                                               context, "\\series",
2039                                               context.font.series, "medium");
2040
2041                 else if (t.cs() == "textbf")
2042                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2043                                               context, "\\series",
2044                                               context.font.series, "bold");
2045
2046                 else if (t.cs() == "textup")
2047                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2048                                               context, "\\shape",
2049                                               context.font.shape, "up");
2050
2051                 else if (t.cs() == "textit")
2052                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2053                                               context, "\\shape",
2054                                               context.font.shape, "italic");
2055
2056                 else if (t.cs() == "textsl")
2057                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2058                                               context, "\\shape",
2059                                               context.font.shape, "slanted");
2060
2061                 else if (t.cs() == "textsc")
2062                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2063                                               context, "\\shape",
2064                                               context.font.shape, "smallcaps");
2065
2066                 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2067                         context.check_layout(os);
2068                         TeXFont oldFont = context.font;
2069                         context.font.init();
2070                         context.font.size = oldFont.size;
2071                         os << "\n\\family " << context.font.family << "\n";
2072                         os << "\n\\series " << context.font.series << "\n";
2073                         os << "\n\\shape " << context.font.shape << "\n";
2074                         if (t.cs() == "textnormal") {
2075                                 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2076                                 output_font_change(os, context.font, oldFont);
2077                                 context.font = oldFont;
2078                         } else
2079                                 eat_whitespace(p, os, context, false);
2080                 }
2081
2082                 else if (t.cs() == "textcolor") {
2083                         // scheme is \textcolor{color name}{text}
2084                         string const color = p.verbatim_item();
2085                         // we only support the predefined colors of the color package
2086                         if (color == "black" || color == "blue" || color == "cyan"
2087                                 || color == "green" || color == "magenta" || color == "red"
2088                                 || color == "white" || color == "yellow") {
2089                                         context.check_layout(os);
2090                                         os << "\n\\color " << color << "\n";
2091                                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2092                                         context.check_layout(os);
2093                                         os << "\n\\color inherit\n";
2094                         } else
2095                                 // for custom defined colors
2096                                 handle_ert(os, t.asInput() + "{" + color + "}", context);
2097                 }
2098
2099                 else if (t.cs() == "underbar") {
2100                         // Do NOT handle \underline.
2101                         // \underbar cuts through y, g, q, p etc.,
2102                         // \underline does not.
2103                         context.check_layout(os);
2104                         os << "\n\\bar under\n";
2105                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2106                         context.check_layout(os);
2107                         os << "\n\\bar default\n";
2108                 }
2109
2110                 else if (t.cs() == "emph" || t.cs() == "noun") {
2111                         context.check_layout(os);
2112                         os << "\n\\" << t.cs() << " on\n";
2113                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2114                         context.check_layout(os);
2115                         os << "\n\\" << t.cs() << " default\n";
2116                 }
2117
2118                 else if (t.cs() == "lyxline") {
2119                         context.check_layout(os);
2120                         os << "\\lyxline";
2121                 }
2122
2123                 else if (is_known(t.cs(), known_phrases)) {
2124                         // FIXME: This needs to be changed when bug 4752 is fixed.
2125                         char const * const * where = is_known(t.cs(), known_phrases);
2126                         context.check_layout(os);
2127                         os << known_coded_phrases[where - known_phrases];
2128                         skip_spaces_braces(p);
2129                 }
2130
2131                 else if (is_known(t.cs(), known_ref_commands)) {
2132                         context.check_layout(os);
2133                         begin_command_inset(os, "ref", t.cs());
2134                         // LyX cannot handle newlines in a latex command
2135                         // FIXME: Move the substitution into parser::getOpt()?
2136                         os << subst(p.getOpt(), "\n", " ");
2137                         os << "reference " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2138                         end_inset(os);
2139                 }
2140
2141                 else if (use_natbib &&
2142                          is_known(t.cs(), known_natbib_commands) &&
2143                          ((t.cs() != "citefullauthor" &&
2144                            t.cs() != "citeyear" &&
2145                            t.cs() != "citeyearpar") ||
2146                           p.next_token().asInput() != "*")) {
2147                         context.check_layout(os);
2148                         string command = t.cs();
2149                         if (p.next_token().asInput() == "*") {
2150                                 command += '*';
2151                                 p.get_token();
2152                         }
2153                         if (command == "citefullauthor")
2154                                 // alternative name for "\\citeauthor*"
2155                                 command = "citeauthor*";
2156
2157                         // text before the citation
2158                         string before;
2159                         // text after the citation
2160                         string after;
2161                         get_cite_arguments(p, true, before, after);
2162
2163                         if (command == "cite") {
2164                                 // \cite without optional argument means
2165                                 // \citet, \cite with at least one optional
2166                                 // argument means \citep.
2167                                 if (before.empty() && after.empty())
2168                                         command = "citet";
2169                                 else
2170                                         command = "citep";
2171                         }
2172                         if (before.empty() && after == "[]")
2173                                 // avoid \citet[]{a}
2174                                 after.erase();
2175                         else if (before == "[]" && after == "[]") {
2176                                 // avoid \citet[][]{a}
2177                                 before.erase();
2178                                 after.erase();
2179                         }
2180                         // remove the brackets around after and before
2181                         if (!after.empty()) {
2182                                 after.erase(0, 1);
2183                                 after.erase(after.length() - 1, 1);
2184                                 // LyX cannot handle newlines in the parameter
2185                                 after = subst(after, "\n", " ");
2186                         }
2187                         if (!before.empty()) {
2188                                 before.erase(0, 1);
2189                                 before.erase(before.length() - 1, 1);
2190                                 // LyX cannot handle newlines in the parameter
2191                                 before = subst(before, "\n", " ");
2192                         }
2193                         begin_command_inset(os, "citation", command);
2194                         os << "after " << '"' << after << '"' << "\n";
2195                         os << "before " << '"' << before << '"' << "\n";
2196                         os << "key " << '"' << p.verbatim_item() << '"' << "\n";
2197                         end_inset(os);
2198                 }
2199
2200                 else if (use_jurabib &&
2201                          is_known(t.cs(), known_jurabib_commands) &&
2202                          (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2203                         context.check_layout(os);
2204                         string command = t.cs();
2205                         if (p.next_token().asInput() == "*") {
2206                                 command += '*';
2207                                 p.get_token();
2208                         }
2209                         char argumentOrder = '\0';
2210                         vector<string> const & options = used_packages["jurabib"];
2211                         if (find(options.begin(), options.end(),
2212                                       "natbiborder") != options.end())
2213                                 argumentOrder = 'n';
2214                         else if (find(options.begin(), options.end(),
2215                                            "jurabiborder") != options.end())
2216                                 argumentOrder = 'j';
2217
2218                         // text before the citation
2219                         string before;
2220                         // text after the citation
2221                         string after;
2222                         get_cite_arguments(p, argumentOrder != 'j', before, after);
2223
2224                         string const citation = p.verbatim_item();
2225                         if (!before.empty() && argumentOrder == '\0') {
2226                                 cerr << "Warning: Assuming argument order "
2227                                         "of jurabib version 0.6 for\n'"
2228                                      << command << before << after << '{'
2229                                      << citation << "}'.\n"
2230                                         "Add 'jurabiborder' to the jurabib "
2231                                         "package options if you used an\n"
2232                                         "earlier jurabib version." << endl;
2233                         }
2234                         if (!after.empty()) {
2235                                 after.erase(0, 1);
2236                                 after.erase(after.length() - 1, 1);
2237                         }
2238                         if (!before.empty()) {
2239                                 before.erase(0, 1);
2240                                 before.erase(before.length() - 1, 1);
2241                         }
2242                         begin_command_inset(os, "citation", command);
2243                         os << "after " << '"' << after << '"' << "\n";
2244                         os << "before " << '"' << before << '"' << "\n";
2245                         os << "key " << '"' << citation << '"' << "\n";
2246                         end_inset(os);
2247                 }
2248
2249                 else if (t.cs() == "cite"
2250                         || t.cs() == "nocite") {
2251                         context.check_layout(os);
2252                         // LyX cannot handle newlines in a latex command
2253                         string after = subst(p.getOptContent(), "\n", " ");
2254                         string key = subst(p.verbatim_item(), "\n", " ");
2255                         // store the case that it is "\nocite{*}" to use it later for
2256                         // the BibTeX inset
2257                         if (key != "*") {
2258                                 begin_command_inset(os, "citation", t.cs());
2259                                 os << "after " << '"' << after << '"' << "\n";
2260                                 os << "key " << '"' << key << '"' << "\n";
2261                                 end_inset(os);
2262                         } else if (t.cs() == "nocite")
2263                                 btprint = key;
2264                 }
2265
2266                 else if (t.cs() == "index") {
2267                         context.check_layout(os);
2268                         begin_inset(os, "Index\n");
2269                         os << "status collapsed\n";
2270                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2271                         end_inset(os);
2272                 }
2273
2274                 else if (t.cs() == "nomenclature") {
2275                         context.check_layout(os);
2276                         begin_command_inset(os, "nomenclature", "nomenclature");
2277                         // LyX cannot handle newlines in a latex command
2278                         string prefix = subst(p.getOptContent(), "\n", " ");
2279                         if (!prefix.empty())
2280                                 os << "prefix " << '"' << prefix << '"' << "\n";
2281                         os << "symbol " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2282                         os << "description " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2283                         end_inset(os);
2284                 }
2285                 
2286                 else if (t.cs() == "label") {
2287                         context.check_layout(os);
2288                         begin_command_inset(os, "label", "label");
2289                         // LyX cannot handle newlines in a latex command
2290                         os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2291                         end_inset(os);
2292                 }
2293
2294                 else if (t.cs() == "printindex") {
2295                         context.check_layout(os);
2296                         begin_command_inset(os, "index_print", "printindex");
2297                         end_inset(os);
2298                         skip_spaces_braces(p);
2299                 }
2300
2301                 else if (t.cs() == "printnomenclature") {
2302                         context.check_layout(os);
2303                         begin_command_inset(os, "nomencl_print", "printnomenclature");
2304                         end_inset(os);
2305                         skip_spaces_braces(p);
2306                 }
2307
2308                 else if (t.cs() == "url") {
2309                         context.check_layout(os);
2310                         begin_inset(os, "Flex URL\n");
2311                         os << "status collapsed\n";
2312                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2313                         end_inset(os);
2314                 }
2315
2316                 else if (LYX_FORMAT >= 408 &&
2317                          (t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2318                         context.check_layout(os);
2319                         begin_inset(os, "script ");
2320                         os << t.cs().substr(4) << '\n';
2321                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2322                         end_inset(os);
2323                 }
2324
2325                 else if (is_known(t.cs(), known_quotes)) {
2326                         char const * const * where = is_known(t.cs(), known_quotes);
2327                         context.check_layout(os);
2328                         begin_inset(os, "Quotes ");
2329                         os << known_coded_quotes[where - known_quotes];
2330                         end_inset(os);
2331                         // LyX adds {} after the quote, so we have to eat
2332                         // spaces here if there are any before a possible
2333                         // {} pair.
2334                         eat_whitespace(p, os, context, false);
2335                         skip_braces(p);
2336                 }
2337
2338                 else if (is_known(t.cs(), known_sizes) &&
2339                          context.new_layout_allowed) {
2340                         char const * const * where = is_known(t.cs(), known_sizes);
2341                         context.check_layout(os);
2342                         TeXFont const oldFont = context.font;
2343                         context.font.size = known_coded_sizes[where - known_sizes];
2344                         output_font_change(os, oldFont, context.font);
2345                         eat_whitespace(p, os, context, false);
2346                 }
2347
2348                 else if (is_known(t.cs(), known_font_families) &&
2349                          context.new_layout_allowed) {
2350                         char const * const * where =
2351                                 is_known(t.cs(), known_font_families);
2352                         context.check_layout(os);
2353                         TeXFont const oldFont = context.font;
2354                         context.font.family =
2355                                 known_coded_font_families[where - known_font_families];
2356                         output_font_change(os, oldFont, context.font);
2357                         eat_whitespace(p, os, context, false);
2358                 }
2359
2360                 else if (is_known(t.cs(), known_font_series) &&
2361                          context.new_layout_allowed) {
2362                         char const * const * where =
2363                                 is_known(t.cs(), known_font_series);
2364                         context.check_layout(os);
2365                         TeXFont const oldFont = context.font;
2366                         context.font.series =
2367                                 known_coded_font_series[where - known_font_series];
2368                         output_font_change(os, oldFont, context.font);
2369                         eat_whitespace(p, os, context, false);
2370                 }
2371
2372                 else if (is_known(t.cs(), known_font_shapes) &&
2373                          context.new_layout_allowed) {
2374                         char const * const * where =
2375                                 is_known(t.cs(), known_font_shapes);
2376                         context.check_layout(os);
2377                         TeXFont const oldFont = context.font;
2378                         context.font.shape =
2379                                 known_coded_font_shapes[where - known_font_shapes];
2380                         output_font_change(os, oldFont, context.font);
2381                         eat_whitespace(p, os, context, false);
2382                 }
2383                 else if (is_known(t.cs(), known_old_font_families) &&
2384                          context.new_layout_allowed) {
2385                         char const * const * where =
2386                                 is_known(t.cs(), known_old_font_families);
2387                         context.check_layout(os);
2388                         TeXFont const oldFont = context.font;
2389                         context.font.init();
2390                         context.font.size = oldFont.size;
2391                         context.font.family =
2392                                 known_coded_font_families[where - known_old_font_families];
2393                         output_font_change(os, oldFont, context.font);
2394                         eat_whitespace(p, os, context, false);
2395                 }
2396
2397                 else if (is_known(t.cs(), known_old_font_series) &&
2398                          context.new_layout_allowed) {
2399                         char const * const * where =
2400                                 is_known(t.cs(), known_old_font_series);
2401                         context.check_layout(os);
2402                         TeXFont const oldFont = context.font;
2403                         context.font.init();
2404                         context.font.size = oldFont.size;
2405                         context.font.series =
2406                                 known_coded_font_series[where - known_old_font_series];
2407                         output_font_change(os, oldFont, context.font);
2408                         eat_whitespace(p, os, context, false);
2409                 }
2410
2411                 else if (is_known(t.cs(), known_old_font_shapes) &&
2412                          context.new_layout_allowed) {
2413                         char const * const * where =
2414                                 is_known(t.cs(), known_old_font_shapes);
2415                         context.check_layout(os);
2416                         TeXFont const oldFont = context.font;
2417                         context.font.init();
2418                         context.font.size = oldFont.size;
2419                         context.font.shape =
2420                                 known_coded_font_shapes[where - known_old_font_shapes];
2421                         output_font_change(os, oldFont, context.font);
2422                         eat_whitespace(p, os, context, false);
2423                 }
2424
2425                 else if (t.cs() == "selectlanguage") {
2426                         context.check_layout(os);
2427                         // save the language for the case that a
2428                         // \foreignlanguage is used 
2429
2430                         // FIXME: \lang needs a LyX name, but we set a LaTeX name
2431                         context.font.language = subst(p.verbatim_item(), "\n", " ");
2432                         os << "\\lang " << context.font.language << "\n";
2433                 }
2434
2435                 else if (t.cs() == "foreignlanguage") {
2436                         context.check_layout(os);
2437                         // FIXME: \lang needs a LyX name, but we set a LaTeX name
2438                         os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2439                         os << subst(p.verbatim_item(), "\n", " ");
2440                         // FIXME: the second argument of selectlanguage
2441                         // has to be parsed (like for \textsf, for
2442                         // example). 
2443                         // set back to last selectlanguage
2444                         os << "\n\\lang " << context.font.language << "\n";
2445                 }
2446
2447                 else if (t.cs() == "inputencoding") {
2448                         // nothing to write here
2449                         string const enc = subst(p.verbatim_item(), "\n", " ");
2450                         p.setEncoding(enc);
2451                 }
2452
2453                 else if (t.cs() == "ldots") {
2454                         context.check_layout(os);
2455                         os << "\\SpecialChar \\ldots{}\n";
2456                         skip_spaces_braces(p);
2457                 }
2458
2459                 else if (t.cs() == "lyxarrow") {
2460                         context.check_layout(os);
2461                         os << "\\SpecialChar \\menuseparator\n";
2462                         skip_spaces_braces(p);
2463                 }
2464
2465                 else if (t.cs() == "textcompwordmark") {
2466                         context.check_layout(os);
2467                         os << "\\SpecialChar \\textcompwordmark{}\n";
2468                         skip_spaces_braces(p);
2469                 }
2470
2471                 else if (t.cs() == "slash") {
2472                         context.check_layout(os);
2473                         os << "\\SpecialChar \\slash{}\n";
2474                         skip_spaces_braces(p);
2475                 }
2476
2477                 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2478                         context.check_layout(os);
2479                         os << "\\SpecialChar \\nobreakdash-\n";
2480                         p.get_token();
2481                 }
2482
2483                 else if (t.cs() == "textquotedbl") {
2484                         context.check_layout(os);
2485                         os << "\"";
2486                         skip_braces(p);
2487                 }
2488
2489                 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2490                         context.check_layout(os);
2491                         os << "\\SpecialChar \\@.\n";
2492                         p.get_token();
2493                 }
2494
2495                 else if (t.cs() == "-") {
2496                         context.check_layout(os);
2497                         os << "\\SpecialChar \\-\n";
2498                 }
2499
2500                 else if (t.cs() == "textasciitilde") {
2501                         context.check_layout(os);
2502                         os << '~';
2503                         skip_spaces_braces(p);
2504                 }
2505
2506                 else if (t.cs() == "textasciicircum") {
2507                         context.check_layout(os);
2508                         os << '^';
2509                         skip_spaces_braces(p);
2510                 }
2511
2512                 else if (t.cs() == "textbackslash") {
2513                         context.check_layout(os);
2514                         os << "\n\\backslash\n";
2515                         skip_spaces_braces(p);
2516                 }
2517
2518                 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2519                             || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2520                             || t.cs() == "%") {
2521                         context.check_layout(os);
2522                         os << t.cs();
2523                 }
2524
2525                 else if (t.cs() == "char") {
2526                         context.check_layout(os);
2527                         if (p.next_token().character() == '`') {
2528                                 p.get_token();
2529                                 if (p.next_token().cs() == "\"") {
2530                                         p.get_token();
2531                                         os << '"';
2532                                         skip_braces(p);
2533                                 } else {
2534                                         handle_ert(os, "\\char`", context);
2535                                 }
2536                         } else {
2537                                 handle_ert(os, "\\char", context);
2538                         }
2539                 }
2540
2541                 else if (t.cs() == "verb") {
2542                         context.check_layout(os);
2543                         char const delimiter = p.next_token().character();
2544                         string const arg = p.getArg(delimiter, delimiter);
2545                         ostringstream oss;
2546                         oss << "\\verb" << delimiter << arg << delimiter;
2547                         handle_ert(os, oss.str(), context);
2548                 }
2549
2550                 // Problem: \= creates a tabstop inside the tabbing environment
2551                 // and else an accent. In the latter case we really would want
2552                 // \={o} instead of \= o.
2553                 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2554                         handle_ert(os, t.asInput(), context);
2555
2556                 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2557                 else if (t.cs().size() == 1 
2558                          && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2559                         context.check_layout(os);
2560                         // try to see whether the string is in unicodesymbols
2561                         docstring rem;
2562                         string command = t.asInput() + "{" 
2563                                 + trim(p.verbatim_item())
2564                                 + "}";
2565                         docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2566                         if (!s.empty()) {
2567                                 if (!rem.empty())
2568                                         cerr << "When parsing " << command 
2569                                              << ", result is " << to_utf8(s)
2570                                              << "+" << to_utf8(rem) << endl;
2571                                 os << to_utf8(s);
2572                         } else
2573                                 // we did not find a non-ert version
2574                                 handle_ert(os, command, context);
2575                 }
2576
2577                 else if (t.cs() == "\\") {
2578                         context.check_layout(os);
2579                         if (p.hasOpt())
2580                                 handle_ert(os, "\\\\" + p.getOpt(), context);
2581                         else if (p.next_token().asInput() == "*") {
2582                                 p.get_token();
2583                                 // getOpt() eats the following space if there
2584                                 // is no optional argument, but that is OK
2585                                 // here since it has no effect in the output.
2586                                 handle_ert(os, "\\\\*" + p.getOpt(), context);
2587                         }
2588                         else {
2589                                 begin_inset(os, "Newline newline");
2590                                 end_inset(os);
2591                         }
2592                 }
2593
2594                 else if (t.cs() == "newline" ||
2595                          (t.cs() == "linebreak" && !p.hasOpt())) {
2596                         context.check_layout(os);
2597                         begin_inset(os, "Newline ");
2598                         os << t.cs();
2599                         end_inset(os);
2600                         skip_spaces_braces(p);
2601                 }
2602
2603                 else if (t.cs() == "input" || t.cs() == "include"
2604                          || t.cs() == "verbatiminput") {
2605                         string name = t.cs();
2606                         if (t.cs() == "verbatiminput"
2607                             && p.next_token().asInput() == "*")
2608                                 name += p.get_token().asInput();
2609                         context.check_layout(os);
2610                         string filename(normalize_filename(p.getArg('{', '}')));
2611                         string const path = getMasterFilePath();
2612                         // We want to preserve relative / absolute filenames,
2613                         // therefore path is only used for testing
2614                         if ((t.cs() == "include" || t.cs() == "input") &&
2615                             !makeAbsPath(filename, path).exists()) {
2616                                 // The file extension is probably missing.
2617                                 // Now try to find it out.
2618                                 string const tex_name =
2619                                         find_file(filename, path,
2620                                                   known_tex_extensions);
2621                                 if (!tex_name.empty())
2622                                         filename = tex_name;
2623                         }
2624                         bool external = false;
2625                         string outname;
2626                         if (makeAbsPath(filename, path).exists()) {
2627                                 string const abstexname =
2628                                         makeAbsPath(filename, path).absFileName();
2629                                 string const abslyxname =
2630                                         changeExtension(abstexname, ".lyx");
2631                                 string const absfigname =
2632                                         changeExtension(abstexname, ".fig");
2633                                 fix_relative_filename(filename);
2634                                 string const lyxname =
2635                                         changeExtension(filename, ".lyx");
2636                                 bool xfig = false;
2637                                 external = FileName(absfigname).exists();
2638                                 if (t.cs() == "input") {
2639                                         string const ext = getExtension(abstexname);
2640
2641                                         // Combined PS/LaTeX:
2642                                         // x.eps, x.pstex_t (old xfig)
2643                                         // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2644                                         FileName const absepsname(
2645                                                 changeExtension(abstexname, ".eps"));
2646                                         FileName const abspstexname(
2647                                                 changeExtension(abstexname, ".pstex"));
2648                                         bool const xfigeps =
2649                                                 (absepsname.exists() ||
2650                                                  abspstexname.exists()) &&
2651                                                 ext == "pstex_t";
2652
2653                                         // Combined PDF/LaTeX:
2654                                         // x.pdf, x.pdftex_t (old xfig)
2655                                         // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
2656                                         FileName const abspdfname(
2657                                                 changeExtension(abstexname, ".pdf"));
2658                                         bool const xfigpdf =
2659                                                 abspdfname.exists() &&
2660                                                 (ext == "pdftex_t" || ext == "pdf_t");
2661                                         if (xfigpdf)
2662                                                 pdflatex = true;
2663
2664                                         // Combined PS/PDF/LaTeX:
2665                                         // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
2666                                         string const absbase2(
2667                                                 removeExtension(abstexname) + "_pspdftex");
2668                                         FileName const abseps2name(
2669                                                 addExtension(absbase2, ".eps"));
2670                                         FileName const abspdf2name(
2671                                                 addExtension(absbase2, ".pdf"));
2672                                         bool const xfigboth =
2673                                                 abspdf2name.exists() &&
2674                                                 abseps2name.exists() && ext == "pspdftex";
2675
2676                                         xfig = xfigpdf || xfigeps || xfigboth;
2677                                         external = external && xfig;
2678                                 }
2679                                 if (external) {
2680                                         outname = changeExtension(filename, ".fig");
2681                                 } else if (xfig) {
2682                                         // Don't try to convert, the result
2683                                         // would be full of ERT.
2684                                         outname = filename;
2685                                 } else if (t.cs() != "verbatiminput" &&
2686                                     tex2lyx(abstexname, FileName(abslyxname),
2687                                             p.getEncoding())) {
2688                                         outname = lyxname;
2689                                 } else {
2690                                         outname = filename;
2691                                 }
2692                         } else {
2693                                 cerr << "Warning: Could not find included file '"
2694                                      << filename << "'." << endl;
2695                                 outname = filename;
2696                         }
2697                         if (external) {
2698                                 begin_inset(os, "External\n");
2699                                 os << "\ttemplate XFig\n"
2700                                    << "\tfilename " << outname << '\n';
2701                         } else {
2702                                 begin_command_inset(os, "include", name);
2703                                 os << "preview false\n"
2704                                       "filename \"" << outname << "\"\n";
2705                         }
2706                         end_inset(os);
2707                 }
2708
2709                 else if (t.cs() == "bibliographystyle") {
2710                         // store new bibliographystyle
2711                         bibliographystyle = p.verbatim_item();
2712                         // If any other command than \bibliography and
2713                         // \nocite{*} follows, we need to output the style
2714                         // (because it might be used by that command).
2715                         // Otherwise, it will automatically be output by LyX.
2716                         p.pushPosition();
2717                         bool output = true;
2718                         for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
2719                                 if (t2.cat() == catBegin)
2720                                         break;
2721                                 if (t2.cat() != catEscape)
2722                                         continue;
2723                                 if (t2.cs() == "nocite") {
2724                                         if (p.getArg('{', '}') == "*")
2725                                                 continue;
2726                                 } else if (t2.cs() == "bibliography")
2727                                         output = false;
2728                                 break;
2729                         }
2730                         p.popPosition();
2731                         if (output) {
2732                                 handle_ert(os,
2733                                         "\\bibliographystyle{" + bibliographystyle + '}',
2734                                         context);
2735                         }
2736                 }
2737
2738                 else if (t.cs() == "bibliography") {
2739                         context.check_layout(os);
2740                         begin_command_inset(os, "bibtex", "bibtex");
2741                         if (!btprint.empty()) {
2742                                 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
2743                                 // clear the string because the next BibTeX inset can be without the
2744                                 // \nocite{*} option
2745                                 btprint.clear();
2746                         }
2747                         os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
2748                         // Do we have a bibliographystyle set?
2749                         if (!bibliographystyle.empty())
2750                                 os << "options " << '"' << bibliographystyle << '"' << "\n";
2751                         end_inset(os);
2752                 }
2753
2754                 else if (t.cs() == "parbox")
2755                         parse_box(p, os, FLAG_ITEM, outer, context, true);
2756                 
2757                 //\makebox() is part of the picture environment and different from \makebox{}
2758                 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2759                 else if (t.cs() == "makebox") {
2760                         string arg = t.asInput();
2761                         if (p.next_token().character() == '(')
2762                                 //the syntax is: \makebox(x,y)[position]{content}
2763                                 arg += p.getFullParentheseArg();
2764                         else
2765                                 //the syntax is: \makebox[width][position]{content}
2766                                 arg += p.getFullOpt();
2767                         handle_ert(os, arg + p.getFullOpt(), context);
2768                 }
2769
2770                 else if (t.cs() == "smallskip" ||
2771                          t.cs() == "medskip" ||
2772                          t.cs() == "bigskip" ||
2773                          t.cs() == "vfill") {
2774                         context.check_layout(os);
2775                         begin_inset(os, "VSpace ");
2776                         os << t.cs();
2777                         end_inset(os);
2778                         skip_spaces_braces(p);
2779                 }
2780
2781                 else if (is_known(t.cs(), known_spaces)) {
2782                         char const * const * where = is_known(t.cs(), known_spaces);
2783                         context.check_layout(os);
2784                         begin_inset(os, "space ");
2785                         os << '\\' << known_coded_spaces[where - known_spaces]
2786                            << '\n';
2787                         end_inset(os);
2788                         // LaTeX swallows whitespace after all spaces except
2789                         // "\\,". We have to do that here, too, because LyX
2790                         // adds "{}" which would make the spaces significant.
2791                         if (t.cs() !=  ",")
2792                                 eat_whitespace(p, os, context, false);
2793                         // LyX adds "{}" after all spaces except "\\ " and
2794                         // "\\,", so we have to remove "{}".
2795                         // "\\,{}" is equivalent to "\\," in LaTeX, so we
2796                         // remove the braces after "\\,", too.
2797                         if (t.cs() != " ")
2798                                 skip_braces(p);
2799                 }
2800
2801                 else if (t.cs() == "newpage" ||
2802                          (t.cs() == "pagebreak" && !p.hasOpt()) ||
2803                          t.cs() == "clearpage" ||
2804                          t.cs() == "cleardoublepage") {
2805                         context.check_layout(os);
2806                         begin_inset(os, "Newpage ");
2807                         os << t.cs();
2808                         end_inset(os);
2809                         skip_spaces_braces(p);
2810                 }
2811
2812                 else if (t.cs() == "newcommand" ||
2813                          t.cs() == "providecommand" ||
2814                          t.cs() == "renewcommand") {
2815                         // providecommand could be handled by parse_command(),
2816                         // but we need to call add_known_command() here.
2817                         string name = t.asInput();
2818                         if (p.next_token().asInput() == "*") {
2819                                 // Starred form. Eat '*'
2820                                 p.get_token();
2821                                 name += '*';
2822                         }
2823                         string const command = p.verbatim_item();
2824                         string const opt1 = p.getOpt();
2825                         string const opt2 = p.getFullOpt();
2826                         add_known_command(command, opt1, !opt2.empty());
2827                         string const ert = name + '{' + command + '}' +
2828                                            opt1 + opt2 +
2829                                            '{' + p.verbatim_item() + '}';
2830
2831                         if (t.cs() == "providecommand")
2832                                 handle_ert(os, ert, context);
2833                         else {
2834                                 context.check_layout(os);
2835                                 begin_inset(os, "FormulaMacro");
2836                                 os << "\n" << ert;
2837                                 end_inset(os);
2838                         }
2839                 }
2840
2841                 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
2842                         // let could be handled by parse_command(),
2843                         // but we need to call add_known_command() here.
2844                         string ert = t.asInput();
2845                         string name;
2846                         p.skip_spaces();
2847                         if (p.next_token().cat() == catBegin) {
2848                                 name = p.verbatim_item();
2849                                 ert += '{' + name + '}';
2850                         } else {
2851                                 name = p.verbatim_item();
2852                                 ert += name;
2853                         }
2854                         string command;
2855                         p.skip_spaces();
2856                         if (p.next_token().cat() == catBegin) {
2857                                 command = p.verbatim_item();
2858                                 ert += '{' + command + '}';
2859                         } else {
2860                                 command = p.verbatim_item();
2861                                 ert += command;
2862                         }
2863                         // If command is known, make name known too, to parse
2864                         // its arguments correctly. For this reason we also
2865                         // have commands in syntax.default that are hardcoded.
2866                         CommandMap::iterator it = known_commands.find(command);
2867                         if (it != known_commands.end())
2868                                 known_commands[t.asInput()] = it->second;
2869                         handle_ert(os, ert, context);
2870                 }
2871
2872                 else if (t.cs() == "hspace" || t.cs() == "vspace") {
2873                         bool starred = false;
2874                         if (p.next_token().asInput() == "*") {
2875                                 p.get_token();
2876                                 starred = true;
2877                         }
2878                         string name = t.asInput();
2879                         string const length = p.verbatim_item();
2880                         string unit;
2881                         string valstring;
2882                         bool valid = splitLatexLength(length, valstring, unit);
2883                         bool known_hspace = false;
2884                         bool known_vspace = false;
2885                         bool known_unit = false;
2886                         double value;
2887                         if (valid) {
2888                                 istringstream iss(valstring);
2889                                 iss >> value;
2890                                 if (value == 1.0) {
2891                                         if (t.cs()[0] == 'h') {
2892                                                 if (unit == "\\fill") {
2893                                                         if (!starred) {
2894                                                                 unit = "";
2895                                                                 name = "\\hfill";
2896                                                         }
2897                                                         known_hspace = true;
2898                                                 }
2899                                         } else {
2900                                                 if (unit == "\\smallskipamount") {
2901                                                         unit = "smallskip";
2902                                                         known_vspace = true;
2903                                                 } else if (unit == "\\medskipamount") {
2904                                                         unit = "medskip";
2905                                                         known_vspace = true;
2906                                                 } else if (unit == "\\bigskipamount") {
2907                                                         unit = "bigskip";
2908                                                         known_vspace = true;
2909                                                 } else if (unit == "\\fill") {
2910                                                         unit = "vfill";
2911                                                         known_vspace = true;
2912                                                 }
2913                                         }
2914                                 }
2915                                 if (!known_hspace && !known_vspace) {
2916                                         switch (unitFromString(unit)) {
2917                                         case Length::SP:
2918                                         case Length::PT:
2919                                         case Length::BP:
2920                                         case Length::DD:
2921                                         case Length::MM:
2922                                         case Length::PC:
2923                                         case Length::CC:
2924                                         case Length::CM:
2925                                         case Length::IN:
2926                                         case Length::EX:
2927                                         case Length::EM:
2928                                         case Length::MU:
2929                                                 known_unit = true;
2930                                                 break;
2931                                         default:
2932                                                 break;
2933                                         }
2934                                 }
2935                         }
2936
2937                         if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
2938                                 // Literal horizontal length or known variable
2939                                 context.check_layout(os);
2940                                 begin_inset(os, "space ");
2941                                 os << name;
2942                                 if (starred)
2943                                         os << '*';
2944                                 os << '{';
2945                                 if (known_hspace)
2946                                         os << unit;
2947                                 os << "}";
2948                                 if (known_unit && !known_hspace)
2949                                         os << "\n\\length "
2950                                            << translate_len(length);
2951                                 end_inset(os);
2952                         } else if (known_unit || known_vspace) {
2953                                 // Literal vertical length or known variable
2954                                 context.check_layout(os);
2955                                 begin_inset(os, "VSpace ");
2956                                 if (known_unit)
2957                                         os << value;
2958                                 os << unit;
2959                                 if (starred)
2960                                         os << '*';
2961                                 end_inset(os);
2962                         } else {
2963                                 // LyX can't handle other length variables in Inset VSpace/space
2964                                 if (starred)
2965                                         name += '*';
2966                                 if (valid) {
2967                                         if (value == 1.0)
2968                                                 handle_ert(os, name + '{' + unit + '}', context);
2969                                         else if (value == -1.0)
2970                                                 handle_ert(os, name + "{-" + unit + '}', context);
2971                                         else
2972                                                 handle_ert(os, name + '{' + valstring + unit + '}', context);
2973                                 } else
2974                                         handle_ert(os, name + '{' + length + '}', context);
2975                         }
2976                 }
2977
2978                 else {
2979                         // try to see whether the string is in unicodesymbols
2980                         // Only use text mode commands, since we are in text mode here,
2981                         // and math commands may be invalid (bug 6797)
2982                         docstring rem;
2983                         docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
2984                                                                  rem, Encodings::TEXT_CMD);
2985                         if (!s.empty()) {
2986                                 if (!rem.empty())
2987                                         cerr << "When parsing " << t.cs() 
2988                                              << ", result is " << to_utf8(s)
2989                                              << "+" << to_utf8(rem) << endl;
2990                                 context.check_layout(os);
2991                                 os << to_utf8(s);
2992                                 skip_spaces_braces(p);
2993                         }
2994                         //cerr << "#: " << t << " mode: " << mode << endl;
2995                         // heuristic: read up to next non-nested space
2996                         /*
2997                         string s = t.asInput();
2998                         string z = p.verbatim_item();
2999                         while (p.good() && z != " " && z.size()) {
3000                                 //cerr << "read: " << z << endl;
3001                                 s += z;
3002                                 z = p.verbatim_item();
3003                         }
3004                         cerr << "found ERT: " << s << endl;
3005                         handle_ert(os, s + ' ', context);
3006                         */
3007                         else {
3008                                 string name = t.asInput();
3009                                 if (p.next_token().asInput() == "*") {
3010                                         // Starred commands like \vspace*{}
3011                                         p.get_token();  // Eat '*'
3012                                         name += '*';
3013                                 }
3014                                 if (!parse_command(name, p, os, outer, context))
3015                                         handle_ert(os, name, context);
3016                         }
3017                 }
3018
3019                 if (flags & FLAG_LEAVE) {
3020                         flags &= ~FLAG_LEAVE;
3021                         break;
3022                 }
3023         }
3024 }
3025
3026 // }])
3027
3028
3029 } // namespace lyx