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